function ValidateEmail(theinput)
{
	s=theinput.value

	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(s))) { 
       return false
	}

	if(s.indexOf)
	{
		at_character=s.indexOf('@')
		if(at_character<=0 || at_character+4>s.length)
			return false
	}
	if(s.length<6)
		return false
	else
		return true
}
function ValidateForm() {
var theform  = document.getElementById('cForm');

	if(ValidateEmail(theform['posEmail'])==false)
	{
		if(theform['posEmail'].focus)
			theform['posEmail'].focus()
		alert('Appears to be an invalid email address')
		return false
	}
/*	if(theform['email_2'].value!=theform['email_1'].value)
	{
		if(theform['email_2'].focus)
			theform['email_2'].focus()
		alert('You have entered two different email addresses')
		return false
	} */
	if(theform['posName'].value=='')
	{
		if(theform['posName'].focus)
			theform['posName'].focus()
		alert('Please enter your Name')
		return false	
	}
	if(theform['posOrg'].value=='')
	{
		if(theform['posOrg'].focus)
			theform['posOrg'].focus()
		alert('Please enter your Organisation')
		return false	
	}
	if(theform['posPhone'].value=='')
	{
		if(theform['posPhone'].focus)
			theform['posPhone'].focus()
		alert('Please enter your Phone number')
		return false	
	}
	sendPosEmail();
	
}
function sendPosEmail () {
	var success = document.getElementById('emailSuccess');
	var posName = document.getElementById('posName');
	var posEmail = document.getElementById('posEmail');
	var posOrg = document.getElementById('posOrg');
	var posType = document.getElementById('posType');
	var posPhone = document.getElementById('posPhone');
	//var strCC = document.getElementById('selfCC').value;
	var page = "scripts/xmlHttpRequest.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 = posEmail.value;
	str2 = str2.replace(/&/g,"**am**");
	str2 = str2.replace(/=/g,"**eq**");
	str2 = str2.replace(/\+/g,"**pl**");
	var str3 = posOrg.value;
	str3 = str3.replace(/&/g,"**am**");
	str3 = str3.replace(/=/g,"**eq**");
	str3 = str3.replace(/\+/g,"**pl**");
	var str4 = posType.value;
	str4 = str4.replace(/&/g,"**am**");
	str4 = str4.replace(/=/g,"**eq**");
	str4 = str4.replace(/\+/g,"**pl**");
	var str5 = posPhone.value;
	str5 = str5.replace(/&/g,"**am**");
	str5 = str5.replace(/=/g,"**eq**");
	str5 = str5.replace(/\+/g,"**pl**");	
	
	var stuff = "posName="+str1+"&posEmail="+str2+"&posOrg="+str3+"&posType="+str4+"&posPhone="+str5;
	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 ajaxContact() {
var frmEl = document.getElementById('cForm');
addEvent(frmEl, 'submit', ValidateForm, false);
frmEl.onsubmit = function() { return false; }
}
addEvent(window, 'load',ajaxContact, false);
