/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var typeS="";

function loadPopups(type){
	typeS=type;
}
//loading popup with jQuery magic!
function loadPopup(){
	var html = "";
	//if(typeS =='form1'){
		//$("#contloader #test-list").load(base_path+"ajaxhandler.php?method=bookmark&bookid="+bookid);
		if(typeS =='map'){
			html = $("#map").html();	
			$("#popupContact").css("height", "500px");
			$("#popupContact").css("width", "500px");
		}
		else if(typeS =='vehicle'){
			html = $("#form2").html();
		}
		else if(typeS =='national'){
			html = $("#form3").html();
		}
		else if(typeS =='appoint'){
			html = $("#form15").html();
		}	
		else		
			html = $("#form1").html();
		
		$("#contloader").html(html);
		//$("#contloader")
	//}
		
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.2"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$(".buttonin").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$(".popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

function submitContactForm(){
	//alert($("#popupContact .fname").val());
	//e.preventDefault();
	//var a1= $("#popupContact .fname").val();
	//var a2= $("#popupContact .email").val();
	//var a3= $("#popupContact .contact").val();
	//alert($("#popupContact .fname").val()); 
	//alert(a1);
	if((($("#popupContact .fnamei").val()!="")&&($("#popupContact .emaili").val()!="")&&($("#popupContact .contacti").val()!=""))){
		if(isValidEmailAddress($("#popupContact .emaili").val())){}
		else{
			$("#popupContact .useractionstat").html("Invald Email ID");
			$("#popupContact .useractionstat").show();
			$("#popupContact .useractionstat").fadeOut(5000);
			return false;
		}
		
		//var re10digit= "/^\d{3}-\d{3}-\d{4}$/";
		//alert(validatePhoneNumber($("#popupContact .contacti").val()));
		if (!validatePhoneNumber($("#popupContact .contacti").val()))
		{
			$("#popupContact .useractionstat").html("Invalid Contact No.");
			$("#popupContact .useractionstat").show();
			$("#popupContact .useractionstat").fadeOut(5000);
			return false;
		}
		//alert("No");



		$("#popupContact .useractionstat").html("");
		
		if(typeS =='vehicle'){
			$.ajax({
			   type: "POST",
			   url: "ajaxhandler.php",
			   data: "method=vehiclefinder&qc_name="+$("#popupContact .fnamei").val()+"&qc_email="+$("#popupContact .emaili").val()+"&qc_contact="+$("#popupContact .contacti").val()+"&make="+$("#popupContact .makei").val()+"&model="+$("#popupContact .modeli").val()+"&year="+$("#popupContact .yeari").val()+"&min="+$("#popupContact .mini").val()+"&max="+$("#popupContact .maxi").val(),
			   success: function(msg){
			     $("#popupContact form").html(msg);
			   }
			 });
			 //$("#popupContact form").load("ajaxhandler.php?method=vehiclefinder&qc_name="+$("#popupContact .fnamei").val()+"&qc_email="+$("#popupContact .emaili").val()+"&qc_contact="+$("#popupContact .contacti").val()+"&make="+$("#popupContact .makei").val()+"&model="+$("#popupContact .modeli").val()+"&year="+$("#popupContact .yeari").val()+"&min="+$("#popupContact .mini").val()+"&max="+$("#popupContact .maxi").val());		
		}			
		else if(typeS =='national'){
			$.ajax({
			   type: "POST",
			   url: "ajaxhandler.php",
			   data: "method=national&action="+typeS+"&qc_name="+$("#popupContact .fnamei").val()+"&qc_email="+$("#popupContact .emaili").val()+"&qc_contact="+$("#popupContact .contacti").val()+"&qc_address="+$("#popupContact .addressi").val(),
			   success: function(msg){
			     $("#popupContact form").html(msg);
			   }
			 });
			 //$("#popupContact form").load("ajaxhandler.php?method=national&action="+typeS+"&qc_name="+$("#popupContact .fnamei").val()+"&qc_email="+$("#popupContact .emaili").val()+"&qc_contact="+$("#popupContact .contacti").val()+"&qc_address="+$("#popupContact .addressi").val());		
		}			
		else{
			$.ajax({
			   type: "POST",
			   url: "ajaxhandler.php",
			   data: "method=sendfrom&action="+typeS+"&qc_name="+$("#popupContact .fnamei").val()+"&qc_email="+$("#popupContact .emaili").val()+"&qc_contact="+$("#popupContact .contacti").val(),
			   success: function(msg){
			     $("#popupContact form").html(msg);
			   }
			 });
			//$("#popupContact form").load("ajaxhandler.php?method=sendfrom&action="+typeS+"&qc_name="+$("#popupContact .fnamei").val()+"&qc_email="+$("#popupContact .emaili").val()+"&qc_contact="+$("#popupContact .contacti").val());		
		}
	}
	else
	{
		$("#popupContact .useractionstat").show();
		$("#popupContact .useractionstat").html("Please fill all the information");
		$("#popupContact .useractionstat").fadeOut(5000);
		return false;
	}
}

function submitAppointForm(){
	if((($("#popupContact .fnamei").val()!="")&&($("#popupContact .emaili").val()!="")&&($("#popupContact .contacti").val()!=""))){
	if(isValidEmailAddress($("#popupContact .emaili").val())){}
	else{
		$("#popupContact .useractionstat").html("Invald Email ID");
		$("#popupContact .useractionstat").show();
		$("#popupContact .useractionstat").fadeOut(5000);
		return false;
	}
	
	if (!validatePhoneNumber($("#popupContact .contacti").val()))
	{
		$("#popupContact .useractionstat").html("Invalid Contact No.");
		$("#popupContact .useractionstat").show();
		$("#popupContact .useractionstat").fadeOut(5000);
		return false;
	}
		
	if(jQuery.trim($("#popupContact .month").val())==""){$("#popupContact .useractionstat").html("Please pick month.");$("#popupContact .month").focus();return false;}
	if(jQuery.trim($("#popupContact .day").val())==""){$("#popupContact .useractionstat").html("Please pick day.");$("#popupContact .day").focus();return false;}
	if(jQuery.trim($("#popupContact .year").val())==""){$("#popupContact .useractionstat").html("Please pick year.");$("#popupContact .year").focus();return false;}
        if(!isDate($("#popupContact .day").val(), $("#popupContact .month").val(), $("#popupContact .year").val())){$("#popupContact .useractionstat").html ("Invalid date specified.");$("#popupContact .month").focus();return false;} 
        if(($("#popupContact .time").val()=="")){$("#popupContact .useractionstat").html("Please specify time");$("#popupContact .time").focus();return false;}
        $("#popupContact .useractionstat").show();
	$("#popupContact .useractionstat").fadeOut(5000);
	$("#popupContact .useractionstat").html("");		
	
	$.ajax({
	   type: "POST",
	   url: "ajaxhandler.php",
	   data: "method=appoint&action="+typeS+"&qc_name="+$("#popupContact .fnamei").val()+"&qc_email="+$("#popupContact .emaili").val()+"&qc_contact="+$("#popupContact .contacti").val()+"&month="+$("#popupContact .month").val()+"&day="+$("#popupContact .day").val()+"&year="+$("#popupContact .year").val()+"&time="+$("#popupContact .time").val(),
	   success: function(msg){
	     $("#popupContact form").html(msg);
	   }
	 });		
	}
	else
	{
		$("#popupContact .useractionstat").show();
		$("#popupContact .useractionstat").html("Please fill all the information");
		$("#popupContact .useractionstat").fadeOut(5000);
		return false;
	}
}
function isDate(day, month, year) { 

    try  {
    
       if ((day < 1) || (day < 1 || day > 30) && (month == 4 || month == 6 || month == 9 || month == 11) || day > 31) { 
       
           return false; 
       } 
    
       if (month < 1 || month > 12) { 
       
           return false;
       } 
    
       if(year == '' || year < 1900)   {
       
           return false;
       }
           
       if (month == 2 && (day < 1 || day > 29 || (day > 28 && (parseInt(year / 4) != year / 4)))) { 
       
           return false;
       } 
   }
   catch(Exception) {
   
        return false;
   }
   
   return true; 
}
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}	
function validatePhoneNumber(elementValue){ // alert(elementValue);
	var phoneNumberPattern = /^\(?(\d{3})\)?[- .]?(\d{3})[- .]?(\d{4})$/;
	return phoneNumberPattern.test(elementValue);
}	