﻿
/***************************/
//@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;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		$("#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();
	var fullHeight	=$(document).height();
	//centering
	$("#popupContact").css({
		"position": "",
		"bottom": 50,
		"left": windowWidth/2-popupWidth/2				   
						   
		/*"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(){
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup(); // must call this to have SimpleModal 
	//	document.location.href	=	base_url_4_js+"/Index";
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	$("#cancel").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

function submit_enquiry()
{
	
	var str	= "";//for taking the error msg
	var ctr	= "";//for taking the controllername for focus
	
	var email	=	$("#email1").val();
	if($("#name").val()==""){
		str	= 'Please give your name';
		ctr = "name";
	}
	
	else if($("#email1").val()==""){
		str	= 'Please give your email id';
		ctr = "email1";
	}
	else if($("#email1").val()!='' && !checkemail(email) ){	
	
		str	= 'Enter a valid email id';
		ctr = "email1";
	}
	else if($("#phone").val()==""){
		str	= 'Please give your phone number';
		ctr = "phone";
	}
	else if($("#country").val()==""){
		str	= 'Please give your country';
		ctr = "country";
	}
	else if($("#comments").val()==""){
		str	= 'Please give your comments';
		ctr = "comments";
	}


	else {
		
			//$('#load_msg').html('<img src="image/al_loading.gif" />');
			var datas	= "name="+$("#name").val()+"&email="+$("#email1").val()+"&phone="+$("#phone").val()+"&country="+$("#country").val()+"&subject="+$("#subject").val()+"&comments="+$("#comments").val();
	
			$.ajax({
				   type: "POST",
					url: base_site_url+'/Rates/enquiry',
					data:  datas,
					success: function(data){
					//alert(data);
					if(data=='success'){
						$('#load_msg').html('<script type="text/javascript" language="javascript">comments2("Thank you for your enquiry, we will reply as soon as we can");</script>');
						clear_form();
					}else{				
						$('#load_msg').html('<script type="text/javascript" language="javascript">comments2("Sorry..Some error has been occured. Try again!!");</script>');
						//disablePopup();
					}
				}
			}); 
	
	}
	
	if(str!="")
	$('#err_msg').html(str+' | &nbsp;<a  href="javascript:void(0);" onClick="javascript:div_hide(\''+ctr+'\');" >Hide</a>');	
}
//function for hide message box
function div_hide(ctr){
	
	$('#err_msg').html("");
	$('#load_msg').html("");
	document.getElementById(ctr).focus();
}

function clear_form()
{
	$("#name").val('');	
	$("#email1").val('');
	$("#phone").val('');	
	$("#country").val('');
	$("#subject").val('');
	$("#comments").val('');
	$('#err_msg').html("");
	
}

function checkemail(em)   //Email validation 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(em))
	return true
	else
	return false
}	

