// JavaScript Document
function getcust_state(Idandcountry)
{
	
	var temp = Idandcountry.split('||');
	id = temp[0];
	value = temp[1];
	
	var perameter;
		perameter="id="+id;
		perameter+="&value="+value;
		
		var action ="fromcountrytostate";
			
					$.ajax({
							 type: "POST",
							 url: "escort_frontaction.php?action="+action,
							 data: perameter,
							 success: function(msg)
								 {
									  document.getElementById("divcounty").innerHTML = '';
									  //document.getElementById("divcounty").innerHTM = msg;
									  $("#divcounty").append(msg);
									  $("#cust_county").focus();
									  
								  }
						   });
					
}

function getcust_stateforreq(Idandcountry,stateId)
{

	var temp = Idandcountry.split('||');
	id = temp[0];
	value = temp[1];
	
	var perameter;
		perameter="id="+id;
		perameter+="&value="+value;
		
		var action ="fromcountrytostateforreq";
			
					$.ajax({
							 type: "POST",
							 url: "escort_frontaction.php?action="+action+"&stateId="+stateId,
							 data: perameter,
							 success: function(msg)
								 {
									  document.getElementById("divcounty").innerHTML = '';
									  //document.getElementById("divcounty").innerHTM = msg;
									  $("#divcounty").append(msg);
									  $("#cust_county").focus();
									  
								  }
						   });
						
}



function getcust_city(Idandcity)
{
		
	var temp = Idandcity.split('||');
	id = temp[0];
	value = temp[1];
	
	var perameter;
		perameter="id="+id;
		perameter+="&value="+value;
	
	var action ="fromstatetocity";
	
					$.ajax({

							 type: "POST",
							 url: "escort_frontaction.php?action="+action,
							 data: perameter,
							 success: function(msg)
								  {
									  
									  document.getElementById("divcity").innerHTML = '';
									  //document.getElementById("divcounty").innerHTM = msg;
									  $("#divcity").append(msg);
									
								  }
						});	
	  
		
		
}


function getcust_cityforreq(Idandcity,cityId)
{
		
	var temp = Idandcity.split('||');
	id = temp[0];
	value = temp[1];
	
	var perameter;
		perameter="id="+id;
		perameter+="&value="+value;
	
	var action ="fromstatetocityforreq";
	
					$.ajax({

							 type: "POST",
							 url: "escort_frontaction.php?action="+action+"&cityId="+cityId,
							 data: perameter,
							 success: function(msg)
								  {
									  document.getElementById("divcity").innerHTML = '';
									  //document.getElementById("divcounty").innerHTML = msg;
									  $("#divcity").append(msg);
									
								  }
						});	
	  
		
		
}

function getalerttonotselect(value)
{
		if(value=="select")
		{	
			alert("This city is not available");
			document.getElementById('cust_city').value = "sel";
		}
}

/// purchase.php functions

function goforpayment(amount,packagename,discount22,discountId,ExpiryNochex,nochexcustId,nochexjobId,nochexocode)
{
	//alert(amount);
	//alert(packagename);
	var curr = $("#currencycheck").val();
    
	var package_type = $("#package_type").val();
		package_type = jQuery.trim(package_type);
		
					if(package_type==null || package_type=="" || package_type=="sel" )
					{
						alert('Please, select package type.');
						$("#package_type").focus();
						return false;
					}
				
		var pIdandtype =  package_type.split("||");
		var ptype = pIdandtype[0]; 		
		var pId = pIdandtype[1];
		var recrrring = pIdandtype[2];
			
		if(recrrring=="FREE")
		{
			var billingperiod = "FREE";
			var billingfrequency = "FREE";
		}
		
		else if(recrrring=="One Off Payment")
		{
			var billingperiod = "Month";
			var billingfrequency = "1";
		}
		else if(recrrring=="Daily")
		{
			var billingperiod = "Day";
			var billingfrequency = "1";
		}
		
		else if(recrrring=="Weekly")
		{
			var billingperiod = "Day";
			var billingfrequency = "7";
		}
		
		else if(recrrring=="Monthly")
		{
			var billingperiod = "Month";
			var billingfrequency = "1";
		}
		else if(recrrring=="Every 2 months")
		{
			var billingperiod = "Month";
			var billingfrequency = "2";
		}
		else if(recrrring=="Every Quarter")
		{
			var billingperiod = "Month";
			var billingfrequency = "3";
		}
		
		else if(recrrring=="Every 6 months")
		{
			var billingperiod = "Month";
			var billingfrequency = "6";
		}
		else if(recrrring=="Yearly")
		{
			var billingperiod = "Year";
			var billingfrequency = "1";
		}
		
		
	if(amount!='' && ptype==packagename)
	{
		var pId=amount;
		var discount="discount";
	}
	else
	{
		var discount = "nodiscount";	
	}
	
	var cust_country = $("#cust_country").val();
	cust_country = jQuery.trim(cust_country);
	
		if(cust_country=="select")
		{
			alert("Please select country");
			$("#cust_country").focus();
			return false;
		}
	
			var idandcountry = cust_country.split("||");
			var countryVal =  idandcountry[1];
	
	var cust_county = $("#cust_county").val();
	cust_county = jQuery.trim(cust_county);
	
		
		if(cust_county=="select")
		{
			alert("Please select county");
			$("#cust_county").focus();
			return false;
		}
	
			var idandcounty =  cust_county.split("||");
			countyVal = idandcounty[1]; 
			
	
	var cust_city = $("#cust_city").val();
	cust_city = jQuery.trim(cust_city);
			
		if(cust_city=="select" || cust_city=="sel")
		{
			alert("Please Select City");
			$("#cust_city").focus();
			return false;
		}	
			
			var idandcity =  cust_city.split("||");
			cityId = idandcity[0];
			cityVal = idandcity[1]; 
			
	var billing_fullname = $("#billing_fullname").val();
	billing_fullname = jQuery.trim(billing_fullname);
					
					if(billing_fullname==null || billing_fullname=="" )
					{
						alert('Please Enter Your billing full name.');
						$("#billing_fullname").focus();
						return false;
					}
			
	var email = $("#email").val();
	email = jQuery.trim(email);
	
		  if (email == null || email == "")
			{
				alert("Please Enter Your Email Address");
				$("#email").val('');
				$("#email").focus();
				return false;
			}
			else if (email == "")
			{
				alert("This field is required, please enter appropriate value in this field");
				$("#email").val('');
				$("#email").focus();
				return false;			
			}		 
			else if(email != '' && isValidEmail(email) )
			{
				alert("Invalid E-Mail Address");
				$("#email").val('');
				$("#email").focus();
				return false;
			}
			
			var email1 = email;
			var at1 = email1.lastIndexOf("@");
			var local1 = email1.substring(0, at1);
			var domain1 = email1.substring(at1 + 1);
			
			if (/(^\.|\.$)/.test(local1) || /(^\.|\.$)/.test(domain1))
			{
				alert("Invalid E-Mail Address");
				return false;
			}
			
			else if (!/^[-a-zA-Z0-9\.]*$/.test(domain1) || domain1.indexOf(".") === -1)
			{
				alert("Invalid E-Mail Address");
				return false;
			}
			
			/*var sex = $("#sex").val();
			if(sex=="select")
			{
				alert("Please enter sex");
				$("#sex").focus();
				return false;
			}
			*/
			
			var billing_address = $("#billing_address").val();
			billing_address = jQuery.trim(billing_address);
			
			if(billing_address=="")
			{
				alert('Please Enter Your Billing Address');
				$("#billing_address").focus();
				return false;
			}
			
			var billing_postcode = $("#billing_postcode").val();
			billing_postcode = jQuery.trim(billing_postcode);
					
					if(billing_postcode==null || billing_postcode=="" )
					{
						alert('Please Enter Your Postcode/Zip.');
						$("#billing_postcode").focus();
						return false;
					}
			
			var customer_phone_number = $("#customer_phone_number").val();
			customer_phone_number = jQuery.trim(customer_phone_number);
					
					if(customer_phone_number==null || customer_phone_number=="" )
					{
						alert('Please Enter a Contact Telephone Number');
						$("#customer_phone_number").focus();
						return false;
					}
					
			
			var customer_phone_number2 = $("#customer_phone_number2").val();
			customer_phone_number2 = jQuery.trim(customer_phone_number2);
					
			
			var billing_fullname2 = $("#billing_fullname2").val();
			billing_fullname2 = jQuery.trim(billing_fullname2);
			
			var email_address2 = $("#email_address2").val();
			email_address2 = jQuery.trim(email_address2);
			
			var secondary_contact = $("#secondary_contact").val();
			secondary_contact = jQuery.trim(secondary_contact);
			
			
			var optional_3 = $("#optional_3").val();
			optional_3 = jQuery.trim(optional_3);
			
			if(optional_3==null || optional_3=="" || optional_3=="http://")
			{
				alert('Please Enter Your Website  URL');
				$("#optional_3").focus();
				return false;
			}
			
			var method = '';
			if(document.getElementById('toNochex').checked==true)
			{
				var method = "Nochex";	
			}
			else if(document.getElementById('topaypal').checked==true)
			{
				var method = "Paypal";		
			}
			 
			if(method=="" && recrrring!="FREE")
			 {
				 alert('select your prefered payment service');
				 document.getElementById('topaypal').focus();
				 return false;
			 }
			 
			 var optional_4 = $("#optional_4").val();
			optional_4 = jQuery.trim(optional_4);
			
			if(document.getElementById('optional_4').checked==false)
			{
				alert('Please,accept terms and condition.');
				$("#optional_4").focus();
				return false;	
			}
		
		if(method=="Nochex" && (recrrring=="Weekly" || recrrring=="Daily"))
		{
			alert("Atleast 1 month is require for Nochex payment from package type, daily and weekly payment not allowed through nochex payment");
			return false;
		}
		
		var nocustId = nochexcustId;
		var nojobId = nochexjobId;
		var nocoDe = nochexocode;
		
		var perameter;
		perameter="pId="+pId;
		perameter+="&ptype="+ptype;
		perameter+="&countryVal="+countryVal;
		perameter+="&countyVal="+countyVal;
		perameter+="&cityVal="+cityVal;
		perameter+="&cityId="+cityId;
		perameter+="&billing_fullname="+billing_fullname;
		perameter+="&email="+email;
		//perameter+="&sex="+sex;
		perameter+="&billing_address="+billing_address;
		perameter+="&customer_phone_number="+customer_phone_number;
		perameter+="&customer_phone_number2="+customer_phone_number2;
		perameter+="&billing_postcode="+billing_postcode;
		perameter+="&website="+optional_3;
		perameter+="&method="+method;
		perameter+="&discount="+discount;
		perameter+="&discountId="+discountId;
		perameter+="&billing_fullname2="+billing_fullname2;
		perameter+="&email_address2="+email_address2;
		perameter+="&secondary_contact="+secondary_contact;
		
		perameter+="&recurrring="+recrrring;
		perameter+="&billingperiod="+billingperiod;
		perameter+="&billingfrequency="+billingfrequency;
		
		perameter+="&nochexcustId="+nochexcustId;
		perameter+="&nochexjobId="+nochexjobId;
		perameter+="&nochexcode="+nochexocode;
		
		perameter+="&expirynochex="+ExpiryNochex;
		perameter+="&currency1="+curr;
		var action ="SubscriptionofCustomer";
		
		
		if(curr!="GBP" && method=="Nochex")
		{
			alert("NOCHEX accepts only pound currency. Kindly pay through PAYPAL");
			return false;
		}
		
		
		$.ajax({
							 type: "POST",
							 url: "escort_frontaction.php?action="+action,
							 data: perameter,
							 success: function(msg)
								  {
										
										var payandform = msg.split("@!");
										var method22 = payandform[1];
										
										if(payandform[1]=="Paypal")
										{
											document.getElementById('escort_paypalform').innerHTML='';
											document.getElementById('escort_paypalform').innerHTML=payandform[0];
											//alert(document.getElementById('escort_paypalform').innerHTML);
											document.forms['_xclick'].submit();
											
										}
										
										if(payandform[1]=="Nochex")
										{
											document.getElementById('escort_nochexform').innerHTML='';
											document.getElementById('escort_nochexform').innerHTML=payandform[0];
											//alert(document.getElementById('escort_nochexform').innerHTML);
											document.forms['_nochexform'].submit();	
										}
										
								  }
					});
		
			//setTimeout(document.forms['_xclick'].submit(),1000);
		
}

function getmsgforselectedpackage(pId,pName)
{
	
		
		var selctedvalue =   pName+"||"+pId;
		var pidandvalue = $("#package_type").val();
		//alert(pidandvalue);
		
		if(pidandvalue!=selctedvalue)
		{
			
			alert("Dear customer, you are selected for the discount in package "+pName+" Not for other package.");
		}
}

function getcurrency(urllink, curr, paypal, nochex, dblink, pack)
{
	var getcurr = $("#currencycheck").val();
	if(getcurr=="select")
	{
		alert("Please select Currency Type");
		$("#currencycheck").focus();
		return false;
	}
	
	var perameter;
		
		perameter="&ppack="+$("#package_type").val();
		perameter+="&pcountry="+$("#cust_country").val();
		perameter+="&pstate="+$("#cust_county").val();
		perameter+="&pcity="+$("#cust_city").val();
		
		perameter+="&pbillfname="+$("#billing_fullname").val();
		perameter+="&pemail="+$("#email").val();
		perameter+="&padd="+$("#billing_address").val();
		perameter+="&ppostcode="+$("#billing_postcode").val();
		perameter+="&ptelno="+$("#customer_phone_number").val();
	
		perameter+="&ptelno2="+$("#customer_phone_number2").val();
		perameter+="&billfname2="+$("#billing_fullname2").val();
		perameter+="&pemail2="+$("#email_address2").val();
		perameter+="&psecondno="+$("#secondary_contact").val();
		perameter+="&pwebadd="+$("#optional_3").val();
		
		
	if(paypal=="" || nochex=="")
	{
		
				if(pack=="")
				{
						if(curr=="")
							{
								var linkarray = urllink.split("?");
								var len = linkarray.length;
								
								if(len==1)
								{
									window.location.href = linkarray[0]+"?cur="+getcurr;	
								}
								else
								{	
									window.location.href = linkarray[0]+"&cur="+getcurr;	
								}
								
							}
				}
				else
				{
						if(curr=="")
							{
								var linkarray = urllink.split("?");
								var len = linkarray.length;
								
								if(len==1)
								{  
									window.location.href = linkarray[0]+"?pk="+pack+"&cur="+getcurr;	
								}
								else
								{
									
									window.location.href = linkarray[0]+"?pk="+pack+"&cur="+getcur;
								}
								
								
							}
				}
				
	}
	
	if(paypal=="" || nochex=="")
	{
					
					if(pack=="")
					{
								if(curr!=getcurr)
								{
									
									var linkarray = urllink.split("?");
									var len = linkarray.length;
									
									if(len==1)
									{
										
										window.location.href = linkarray[0]+"?cur="+getcurr+perameter;
									}
									
									if(len>1)
									{
										window.location.href = linkarray[0]+"?cur="+getcurr+perameter;
									}
									
								}
					
					
					
							if(curr==getcurr)
							{	
								var linkarray = urllink.split("?");
								var len = linkarray.length;
								window.location.href = linkarray[0]+"?cur="+getcurr;
							}				
					}
					else
					{
						if(curr!=getcurr)
								{
									var linkarray = urllink.split("?");
									var len = linkarray.length;
									
									if(len==1)
									{
										window.location.href = linkarray[0]+"?pk="+pack+"&cur="+getcurr;
									}
									
									if(len>1)
									{
										window.location.href = linkarray[0]+"?pk="+pack+"&cur="+getcurr;
									}
									
									
								}
							
					}
					
	}
	
	/*if(paypal!="")
	{
		if(curr=="")
		{
			//window.location.href = dblink+"&cur="+getcurr;
			window.location.href = urllink;
		}
		
		if(curr!="")
		{
				if(curr==getcurr)
				{
					window.location.href = urllink;
				}
				if(curr!=getcurr)
				{
					window.location.href = dblink+"&cur="+getcurr;
				}
		}
	}
	
	
	if(nochex!="")
	{
		if(curr=="")
		{
			//window.location.href = dblink+"&cur="+getcurr;
			window.location.href = urllink;
		}
		
		if(curr!="")
		{
				if(curr==getcurr)
				{
					window.location.href = urllink;
				}
				if(curr!=getcurr)
				{
					window.location.href = dblink+"&cur="+getcurr;
				}
		}
	}
	*/
	
	
	
}

function getcurrencyforcust(urllink, paypal, dblink)
{
		
		var getcurr = $("#currencycheck").val();
		var action = "setcurrency";
		
			$.ajax({
					   type: "POST",
					   url: "escort_frontaction.php",
					   data: "action="+action+"&curr="+getcurr,
					   success: function(msg)
					   {
						   window.location.href = dblink;
			  	 	   }
				 });
		
}

function getcurrencyfornochecx(urllink, nochex, dblink)
{
		
		var getcurr = $("#currencycheck").val();
		var action = "setcurrencynochex";
		
			$.ajax({
					   type: "POST",
					   url: "escort_frontaction.php",
					   data: "action="+action+"&curr="+getcurr,
					   success: function(msg)
					   {
						   window.location.href = dblink;
			  	 	   }
				 });
		
}

function isValidEmail(emailStr)
		
	{
		var checkTLD=1;
		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
			return "Email address seems incorrect (check @ and .'s)";
			//alert("Email address seems incorrect (check @ and .'s)");
			//return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];
		return;
		// Start by checking that only basic ASCII characters are in the strings (0-127).
		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				return "Ths username contains invalid characters.";
				//return false;
			}
		}
		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				return "Ths domain name contains invalid characters.";
				//return false;
			}
		}
		if (user.match(userPat)==null) {
			return "The username doesn't seem to be valid.";
			//return false;
		}
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					return "Destination IP address is invalid!";
					//return false;
				}
			}
			return 0;
		}
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				return "The domain name does not seem to be valid.";
				//return false;
		   }	
		}
		if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
			return "The address must end in a well-known domain or two letter " + "country.";
			//return false;
		}
	
	// Make sure there's a host name preceding the domain.
	
		if (len<2) {
			return "This address is missing a hostname!";
			//return false;
		}	
		return 0;
	}