﻿/***************************/
//@Author: Rajesh Reddy Bathula
//@website: http://justthatidontforget.blogspot.com
//@email: rajeshbathula.r@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/


/***************************/
//@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.7"
		});
		$("#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;
		setCookie("popupshown","1",365);
	}
}

//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
	});
	
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   //var address = document.forms[form_id].elements[email].value;
   if(reg.test(email) == false) {
      
      return false;
   }
   return true;
}


function checkCookie()
{
var username=getCookie("username");
  if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("username",username,365);
    }
  }
}
function wait(msecs)
{
var start = new Date().getTime();
var cur = start
while(cur - start < msecs)
{
cur = new Date().getTime();
}
} 
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	
	if(getCookie('popupshown')!="1"){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	fix_flash();
	}
				
	//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();
		}
	});
	
		$("#ajaxload").ajaxStart(function(){
								$(this).show();
							});
							
							$("#ajaxload").ajaxStop(function(){
								$(this).hide();
							});
							
							
							
							$("#cntform").submit(function(){
							
							if($("#email").val()==""){
								alert("Email address is required.");
								$("#email").focus();
								return false;
							}
							
							if(!validateEmail($("#email").val())){
								alert("Please enter a valid email address.");
								$("#email").focus();
								return false;
							}
							
							$.ajax({
									url:'sendpopupmail.php',
									method:'post',
									data:{
										name:$("#name").val(),
										email:$("#email").val(),
										city:$("#city").val(),
										mobile:$("#mobile").val(),
										message:$("#message").val()
									},
									dataType:'html',
									success:function(data){
										if(data==1)
										{
										
											disablePopup();										
										}else{
											alert("Unable to send email. Please try again.");
										}
										
										
									},
									
									error:function(){
										alert("We are unable to perform the operation at this time. Please try again latter.");
										}
								}
							);
							
							
							return false;
						}
					);

});

