function validateFields() {
var frmEl = document.getElementById('cForm');
var posName = document.getElementById('fullname');
var posPhone = document.getElementById('phone');
var posCountry = document.getElementById('country');
var posCity = document.getElementById('city');
var posEmail = document.getElementById('email');
var posSkype = document.getElementById('skype');
var posText = document.getElementById('message');
var evalid = document.getElementById('invalidemail');
var whiteSpace = /^[\s]+$/;
//alert(whiteSpace);
	if(posName.value == '' || posName.value == 'Full Name' || whiteSpace.test(posName.value)){
		posName.style.borderColor='RED';
	}else if(posCountry.value == '' || posCountry.value == 'Country' || whiteSpace.test(posCountry.value)){
		posName.style.borderColor='#dddddd';
		posCountry.style.borderColor='RED';
	}else if(posCity.value == '' || posCity.value == 'City' || whiteSpace.test(posCity.value)){
		posName.style.borderColor='#dddddd';
		posCountry.style.borderColor='#dddddd';
		posCity.style.borderColor='RED';
	}else if(posEmail.value == '' || posEmail.value == 'E-mail' || whiteSpace.test(posEmail.value)){
		posName.style.borderColor ='#dddddd';
		posCity.style.borderColor ='#dddddd';
		posCountry.style.borderColor='#dddddd';
		posEmail.style.borderColor ='RED';
	}else if(!echeck(posEmail.value)){
		posName.style.borderColor ='#dddddd';
		posCity.style.borderColor ='#dddddd';
		posCountry.style.borderColor='#dddddd';
		evalid.style.display ='block';
		posEmail.style.borderColor='RED';
	}else if(posText.value == '' || posText.value == 'Comment' || whiteSpace.test(posText.value)){
		evalid.style.display ='none';
		posName.style.borderColor ='#dddddd';
		posCity.style.borderColor ='#dddddd';
		posCountry.style.borderColor='#dddddd';
		posEmail.style.borderColor='#dddddd';
		posEmail.style.borderColor='#dddddd';
		posText.style.borderColor='RED';
	}else {
		if(posPhone.value== 'Phone Number'){
			posPhone.value='';
		}
		if(posCity.value== 'City'){
			posCity.value='';
		}
		posName.style.borderColor='#dddddd';
		posCity.style.borderColor='#dddddd';
		posCountry.style.borderColor='#dddddd';
		posEmail.style.borderColor='#dddddd';
		posEmail.style.borderColor='#dddddd';
		posText.style.borderColor = '#dddddd';
		sendPosEmail();
		
	}
	
	/*if ( posText.value == '' || whiteSpace.test(posText.value) ) {
		alert("You're trying to send an Empty Email. Please type something and then get on your way.");
	}
	else {
		sendPosEmail();
	}*/
}
function sendPosEmail () {
	var success = document.getElementById('emailSuccess');
	var posName = document.getElementById('fullname');
	var posPhone = document.getElementById('phone');
	var posCountry = document.getElementById('country');
	var posCity = document.getElementById('city');
	var posEmail = document.getElementById('email');
	var posSkype = document.getElementById('skype');
	var posText = document.getElementById('message');
	//var strCC = document.getElementById('selfCC').value;
	var page = "scripts/xmlHttpRequest.php?contact=true&xml=true";
	//var page = "scripts/contact.php?contact=true&xml=true";
	
	showContactTimer(); // quickly begin the load bar
	success.style.display = 'none'; // hide the success bar (incase this is a multi-email


	// convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.
	
	var str1 = posName.value;
	str1 = str1.replace(/&/g,"**am**");
	str1 = str1.replace(/=/g,"**eq**");
	str1 = str1.replace(/\+/g,"**pl**");
	var str2 = posPhone.value;
	str2 = str2.replace(/&/g,"**am**");
	str2 = str2.replace(/=/g,"**eq**");
	str2 = str2.replace(/\+/g,"**pl**");
	var str3 = posCountry.value;
	str3 = str3.replace(/&/g,"**am**");
	str3 = str3.replace(/=/g,"**eq**");
	str3 = str3.replace(/\+/g,"**pl**");
	var str4 = posCity.value;
	str4 = str4.replace(/&/g,"**am**");
	str4 = str4.replace(/=/g,"**eq**");
	str4 = str4.replace(/\+/g,"**pl**");
	var str5 = posEmail.value;
	str5 = str5.replace(/&/g,"**am**");
	str5 = str5.replace(/=/g,"**eq**");
	str5 = str5.replace(/\+/g,"**pl**");
	var str6 = posSkype.value;
	str6 = str6.replace(/&/g,"**am**");
	str6 = str6.replace(/=/g,"**eq**");
	str6 = str6.replace(/\+/g,"**pl**");
	var str7 = posText.value;
	str7 = str7.replace(/&/g,"**am**");
	str7 = str7.replace(/=/g,"**eq**");
	str7 = str7.replace(/\+/g,"**pl**");
	
	
	var stuff = "posName="+str1+"&posPhone="+str2+"&posCountry="+str3+"&posCity="+str4+"&posEmail="+str5+"&posSkype="+str6+"&posText="+str7;
	
	//var stuff = "fullname=" + str1 + "&phone_number=" + str2 + "&country=" + str3 + "&city=" + str4 + "&email=" + str5 + "&carrier=" + str6 + "&message=" + str7;
	
	loadXMLPosDoc(page, stuff)
	
	
}
function showContactTimer () {
	var loader = document.getElementById('loadBar');
	loader.style.display = 'block';
	sentTimer = setTimeout("hideContactTimer()",6000);
}

function hideContactTimer () {
	var loader = document.getElementById('loadBar');
	var success = document.getElementById('emailSuccess');
	var fieldArea = document.getElementById('contactFormArea');
	var inputs = fieldArea.getElementsByTagName('input');
	var inputsLen = inputs.length;
	var tAreas = fieldArea.getElementsByTagName('textarea');
	var tAreasLen = tAreas.length;
	// Hide the load bar alas! Done Loading
	loader.style.display = "none";
	success.style.display = "block";
	success.innerHTML = '<strong style="color:green;">'+grabPosXML("confirmation")+'</strong>';
	// Now Hijack the form elements
	for ( i=0;i<inputsLen;i++ ) {
		if ( inputs[i].getAttribute('type') == 'text' ) {
			inputs[i].value = '';
		}
	}
	for ( j=0;j<tAreasLen;j++ ) {
		tAreas[j].value = '';
	}
}
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
	
function ajaxContact() {
var frmEl = document.getElementById('cForm');
addEvent(frmEl, 'submit', validateFields, false);
frmEl.onsubmit = function() { return false; }
}
addEvent(window, 'load',ajaxContact, false);


