﻿// JScript File

   
   
   function validate(type, value, defValue){
    if(type == null || type == undefined) return true;
    if(value == defValue) return false;
    result = false;
    if(type == "noempty" && (value != null && value != undefined && value!= "")) result = true;
    if(type == "email"){ 
            regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            result = regex.test(value);   
    }
    return result;
   }
   
   function resetControls(){
        $("span#ctrMess").html("");
        //$(".open_popup_inp").val("");
        $(".open_popup_inp").each(function(){
       			$(this).val($(this).attr("defval"));
       			$("#"+$(this).attr("id")+"Er").html("&nbsp;");
       	});
   }
   
   function showMess(mess){
    $("span#ctrMess").html(mess);
   }
   
   function popupHide(animate){
    if(animate == true){
		$("div#ctrWinPopUp").css({"display":"none"});
		$("div#ctrOverlay").animate({opacity: 0},400,function(){
			$("div#ctrOverlay").css({"display":"none"});
			
		});
	}else{
	        $("div#ctrWinPopUp").hide();
            $("div#ctrOverlay").hide();  
	       }
	
   } 
   function popupShow(animate){
        resetControls();
        
        
        
        var w = $("div#ctrWinPopUp").width();
  		var h = $("div#ctrWinPopUp").height();
        var l = Math.floor(($(window).width()/2 - w/2)+$(document).scrollLeft());
        var t = Math.floor(($(window).height()/2 - h/2)+$(document).scrollTop());  
   
      $("div#ctrOverlay").width($(document).width());
      $("div#ctrOverlay").height($(document).height());
       $("div#ctrWinPopUp").css({
		left: l,
		top: t
	})


      if(animate == true){                                                
        $("div#ctrOverlay").css({"opacity": 0,"display":"block"});
    	$("div#ctrOverlay").animate({opacity: 0.8},400,function(){
    		$("div#ctrWinPopUp").show();
    	});
    	
      }else{
           $("div#ctrWinPopUp").show();
           $("div#ctrOverlay").css({"opacity": "0.8" ,"display":"block"});
        }
   }
   
   
   $(document).ready(function(){
      
      $(".open_popup_inp").focus(function(){
		if($(this).val()==$(this).attr("defval"))
			$(this).val("");
	});

	$(".open_popup_inp").blur(function(){
		if($(this).val()=="")
			$(this).val($(this).attr("defval"));
	});

      
      $(".anchorPopUp").click(function(){
        popupShow(true);
      });
      
      $(".anchorPClose").click(function(){
        popupHide(true);
      });

      $("#btnSendCallBackForm").click(function(){
        
         var strData = "";
         var paramsCaption = "";
         splitter = "";
         msplitter = "";
         ind=1;
         errorMess = "";
         $(".open_popup_inp").each(function(){
             if(validate($(this).attr("validation"),$(this).val(),$(this).attr("defval"))){
             	strData += splitter+"'inp"+ind+"':'"+ $(this).val()+"'";
             	paramsCaption += msplitter+$(this).attr("defval");
             	ind++;
             	splitter = ",";
             	msplitter = ";";
             	$("#"+$(this).attr("id")+"Er").html("&nbsp;");
             } else  {
             			$("#"+$(this).attr("id")+"Er").html("*");
             			errorMess +=  "1";
             		}

         });
         
         if(strData!="") strData = "'inp0':'"+paramsCaption+"',"+strData;
         strData="({"+strData+"})";
         if(errorMess==""){
        
         $.ajax({
                    type: "POST",
                    url: $.globalServicePath+'/process.ashx',
                    data: eval(strData),
                    cache: false,
                    success: function (data, textStatus){
                                if(data == "ok"){
                                    $(".open_popup_inp").each(function(){
             							 $(this).val($(this).attr("defval"));
             							});
             						$("#ctrMess").html("Ваше сообщение успешно отправлено.");
                                } //end of data ok
                    }// end of success
            });
         }else $("#ctrMess").html("Поля, обязательные для заполнения, отмечены звездочкой.");
	});
        
   }); 
