$('#contactfrm').ready(function(){
  $('#contact_name').blur(function(){
	if(!$('#cf_errmsg').empty()){
	  $('#cf_errmsg').html('&nbsp;');
	}
  });

  $('#contact_email').blur(function(){
	if(!$('#cf_errmsg').empty()){
	  $('#cf_errmsg').html('&nbsp;');
	}
  });

  $('#contact_email').blur(function(){
	if(!$('#cf_errmsg').empty()){
	  $('#cf_errmsg').html('&nbsp;');
	}
  });

  $('#conact_query').blur(function(){
	if(!$('#cf_errmsg').empty()){
	  $('#cf_errmsg').html('&nbsp;');
	}
  });

  $('#contact_how').blur(function(){
	if(!$('#cf_errmsg').empty()){
	  $('#cf_errmsg').html('&nbsp;');
	}
  });

  $('#contact_hear').blur(function(){
	if(!$('#cf_errmsg').empty()){
	  $('#cf_errmsg').html('&nbsp;');
	}
  });

});

function emailAddress(value){
 var emailpattern = new RegExp("^[a-zA-Z0-9-_.]+@{1}([a-zA-Z0-9-_.]+)\.([a-zA-Z]{2,4})$");
 var rst = emailpattern.exec(value);
 if (!rst)
	return false;
 return true;
}

function sendMailer() {
	var fldOK = checkContactFields();
	
	if(fldOK){
		var url = "sendmailer.php";
		var p1 = $('input').serialize();
		var p2 = $('textarea').serialize();
		//var p3 = $('select').serialize();
		var parm = p1 +"&"+ p2 +"&task=1";
		var success = initAjax('GET',url,false,"script",parm);
		//alert('Value: '+success);
		if(success){
			document.contactfrm.reset();
			$('#contact_name').focus();
			$('#cf_errmsg').html('<img src="images/tick.gif">&nbsp;<strong>Message Sent!</strong>');
		}else{
			$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>Unable to send Message, try again later.</strong>')
		}//end of if() else;
	}//end of if()
}//end of sendMailer()

function checkContactFields(){
	if($('#contact_name').val()==""){
		$('#contact_name').focus();
		$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>Your NAME is required.</strong>');
		return false;
	}else if($('#contact_number').val()==""){
		$('#contact_number').focus();
		$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>Please specify your Phone Number.</strong>');
		return false;
	}else if($('#contact_email').val()==""){
		$('#contact_email').focus();
		$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>Email Address is required.</strong>');
		return false;
	}else if(!(emailAddress($('#contact_email').val()))){
		$('#contact_email').focus();
		$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>Invalid Email Address format!</strong>');
		return false;		
	}else if($('#text_area').val()==""){
		$('#text_area').focus();
		$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>Please enter your Message/Comments.</strong>');
		return false;
	}else if($('#contact_how').val()==""){
		$('#contact_how').focus();
		$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>Please indicate how to contact you.</strong>');
		return false;
	}else if($('#contact_hear').val()==""){
		$('#contact_hear').focus();
		$('#cf_errmsg').html('<img src="images/cross.gif">&nbsp;<strong>How did you hear about us?</strong>');
		return false;
	}else{
		return true;
	}
}

function frmReset(){
	document.getElementById('contact_name').value = "";
	document.getElementById('contact_email').value = "";
	document.getElementById('contact_number').value = "";
	document.getElementById('text_area').value = "";
	document.getElementById('contact_how').value = "";
	document.getElementById('contact_hear').value = "";
}

function showStatus(msgtxt){
	window.status = msgtxt;
	return true;
}

// JavaScript Document
var win= null;
function NewWindow(mypage,myname,w,h,scroll){
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var br = navigator.product;

	if(br == "Gecko"){
		h = parseInt(h) + 1;
	}
	
	var settings ='height='+h+',';
		settings +='width='+w+',';
		settings +='top='+wint+',';
		settings +='left='+winl+',';
		settings +='scrollbars=no';
		settings +='resizable=no';
		settings +='status=no';
		settings +='location=no';
		settings +='menu=no';
		win=window.open(mypage,myname,settings);
		if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}
	
function MM_openBrWindow(theURL,winName,features) { 
	window.open(theURL,winName,features);
}

function initAjax( smethod , urllink, asynctype, dataT, parms ){
 var result;
 result = $.ajax(
	{
	 type: smethod,
	 url: urllink,
	 async: asynctype,
	 dataType: dataT,
	 data: parms
	}).responseText;
 return result;
}