function move(url)
{
  window.location = url;
//  alert('war');
}

function openCenter(content,width,height)
{
  var id = 'wa' + Math.round(Math.random()*100);

  var ht = ($(window).height() < height ? $(window).height():height);
  var top = Math.round(($(window).height()-ht)/2);


  $("body")
    .append("<div id='"+id+"'>war</div>")
    .append("<div id='t"+id+"' style='position:fixed;width:"+parseInt(width)+"px;height:"+parseInt(ht)+"px;border:#c0c0c0;left:50%;margin-left:-"+parseInt(width/2)+"px;top:"+top+"px;'>"+content+"<div style='text-align:right;position:absolute;left:"+parseInt(width)+"px;top:0px;padding-right:20px;'><a href='javascript:;' onclick='closeCenter("+id+")' style='font-size:14px;color:#fff;font-weight:bold;margin-left: 10px;'>Закрыть</a></div></div>")
  $("#"+id)
    .css("position","absolute")
    .css("width","100%")
    .css("height",document.body.offsetHeight)
    .css("background","#000")
    .css("opacity","0.6")
    .css("top","0px")
    .css("left","0px")
    .click(function(){
      $(this).remove();
      $("#t"+id).remove();
    })
  $("#t"+id)
    .click(function(){
      $(this).remove()
      $("#"+id).remove();
    })
}

function openVideo()
{
  openCenter('<object width="560" height="349"><param name="movie" value="http://www.youtube.com/v/CFwr7DDHjeM?fs=1&amp;hl=ru_RU&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/CFwr7DDHjeM?fs=1&amp;hl=ru_RU&amp;rel=0" type="application/x-shockwave-flash" width="560" height="349" allowscriptaccess="always" allowfullscreen="true"></embed></object>',560,349)
}

function openImage(img,width,height)
{
  var ht = ($(window).height()<height?$(window).height():height);

  width = ht/height * width;

  openCenter('<img src="'+img+'" width="'+width+'" height="'+ht+'px" alt+"" />',(parseInt(width)+25),ht+100);
}
function closeCenter(id)
{
//  $("#"+id).remove();
}



/*

openCenter('<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/4LvynQOBCOg&hl=ru_RU&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/4LvynQOBCOg&hl=ru_RU&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>',480,490)

*/


function sendOrder()
{
    check = true;
    requestData = {};
    requestData.complect = $("#micro-complect").val();
    requestData.name     = $("#client-name").val();
    requestData.phone    = $("#client-phone").val();
    requestData.email    = $("#client-email").val();
    requestData.adress   = $("#client-adress").val();
    requestData.comment  = $("#client-comment").val();

    if(requestData.name.length<3) {
        $("#client-name").css("background","red")
        $("#client-name-check").text("Имя должно быть больше трех знаков.")
        check = false;
    }
    if(requestData.phone.length<7) {
        $("#client-phone").css("background","red")
        $("#client-phone-check").text("Поле должно состоять минимум из 7 символов.")
        check = false;
    }
    if(requestData.adress=='') {
        $("#client-adress").css("background","red")
        $("#client-adress-check").text("Введите свой адрес.")
        check = false;
    }

    emailCheck = false;
    reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
    t = [];
    if(t = requestData.email.match(reg)) {
        if(t[0]==requestData.email) {
            emailCheck = true;
        }
    }
    if(!emailCheck) {
        $("#client-email").css("background","red")
        $("#client-email-check").text("Введите корректный email.")
        check = false;
    }

    if(!check) {
        $("#order-status").text("")
        return;
    }

     $.ajax({
        url: "/order.php",
        dataType: 'json',
        data: requestData,
        beforeSend:function(){
          $("#order-status").text("Ждите...заказ в обработке.")
        },
        success: function(response){
            if(response.result == 1) {
                $("#order-status").css("color","green").text("Заказ отправлен. С вами свяжутся в ближайшее время.")
            } else {
                $("#order-status").text(response.error).css("color","red")
            }

        }
      })
}

$(document).ready(function(){
    $("#order-form input[type=text],#order-form textarea").keyup(function(){
        $(this).css("background","#fff");
        $(this).parent().children("span").text('');
    })
})
