function checkEmail(email){
	var str=email;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		return true;
	else
		return false;
}

jQuery(document).ready(function(){
	hackFreeCss();
});

function hackFreeCss(){
	if(/Safari/i.test(navigator.userAgent) || window.devicePixelRatio){ //Safari or Chrome
		if(navigator.userAgent.indexOf('Chrome')==-1) //Only and only 'Apple Safari browser':
			jQuery("body").addClass('safari');
	}
	jQuery("#navigation li:last-child").addClass('last-child');
}


function contactUs(){
	var formName = document.getElementById("con_form");
	var reqFields = new Array(formName.con_name, formName.con_email, {'email': formName.con_email,'captcha': ''});
	return chkValidity(formName, reqFields, 'contact_us');
}

function contactHome(){
	var formName = document.getElementById("con_home");
	var reqFields = new Array(formName.con_fname, formName.con_lname, formName.con_pnumber, {'email': formName.con_email,'captcha': ''});
	return chkValidity(formName, reqFields, 'contact_home');
}

function chkValidity(formName, reqFields, parmsId){
	
	//check for empty fields:
	for(i=0; i < reqFields.length-1; i++){
		if(reqFields[i].value==''){
			alert('Please complete the required fields marked with (*).');
			return false;
		}		
	}
	
	//check valid email:
	if(reqFields[reqFields.length-1].email){
		if(!checkEmail(reqFields[reqFields.length-1].email.value)){
			alert('Please enter a valid Email address.');
			return false;
		}
	}
	
	//var parms = 'id=contact_us&';
	//parms = 'id=' + parmsId + '&' + jQuery(formName).serialize();
	
	if(parmsId=="contact_home")
	{
		parms = 'id=' + parmsId + "&con_fname="+document.getElementById("con_fname").value+"&con_lname="+document.getElementById("con_lname").value+"&con_pnumber="+document.getElementById("con_pnumber").value+"&con_email="+document.getElementById("con_email").value+"&con_msg="+document.getElementById("con_msg").value ;
	}
	if(parmsId=="contact_us")
	{
		parms = 'id=' + parmsId + "&con_name="+document.getElementById("con_name").value+"&con_email="+document.getElementById("con_email").value+"&con_msg="+document.getElementById("con_msg").value;		
	}	
	jQuery.ajax({
	   type: "POST",
	   url: tpath+"/ajax.php",
	   data: parms,
	   success: function(msg){
			if(msg==1){
				alert("Thank You!\n\n We've received your email and will follow up shortly.");
				//clear inputs
				jQuery(reqFields).val('');
				jQuery("#con_msg").val('');
			}
			else{
				alert('mail not sent');
			}
	   }
	 });
	return false;
}
