function validateFields() {
var frmEl = document.getElementById('cForm');
var posEmail = document.getElementById('email');
var posText = document.getElementById('question');
var posFirstName = document.getElementById('firstname');
var posSurname = document.getElementById('surname');
var posJobTitle = document.getElementById('jobtitle');
var posCompany = document.getElementById('company');
var posWebsite = document.getElementById('website');
var posAddress = document.getElementById('address');
var posCity = document.getElementById('city');
var posState = document.getElementById('state');
var posPostCode = document.getElementById('postcode');
var posCountry = document.getElementById('country');
var posTel = document.getElementById('tel');
var strCC = document.getElementById('selfCC');
var posNature = document.getElementById('nature');
var posSource = document.getElementById('source');
if ( posNature.value == 'Sample Request' ) {
	var posQuantity = document.getElementById('quantity');
	var posEstpodate = document.getElementById('estpodate');
	var posApplication = document.getElementById('application');
}
var whiteSpace = /^[\s]+$/;
	
// If sample request form, require pretty much everything
if ( posNature.value == 'Sample Request' ) {
	if ( ( posFirstName.value == '' || whiteSpace.test(posFirstName.value) ) && ( posSurname.value == '' || whiteSpace.test(posSurname.value) ) ) {
		alert("Please enter your name.");
		posFirstName.focus();
	}
	else if ( posJobTitle.value == '' ) {
		alert("Please specify your job title.");
		posJobTitle.focus();
	}
	else if ( posCompany.value == '' ) {
		alert("Please specify your company name.");
		posCompany.focus();
	}
	else if ( posAddress.value == '' ) {
		alert("Please supply a postal address.");
		posAddress.focus();
	}
	else if ( posCity.value == '' ) {
		alert("Please specify your town / city.");
		posCity.focus();
	}
	else if ( posState.value == '' ) {
		alert("Please specify your county / state.");
		posState.focus();
	}
	else if ( posPostCode.value == '' ) {
		alert("Please supply a postal code / ZIP.");
		posPostCode.focus();
	}
	else if ( posCountry.value == '' ) {
		alert("Please specify your country.");
		posCountry.focus();
	}
	else if ( posTel.value == '' ) {
		alert("Please supply a telephone number.");
		posTel.focus();
	}
	else if ( posEmail.value == '' ) {
		alert("Please supply an email address.");
		posEmail.focus();
	}
	else if ( posWebsite.value == '' ) {
		alert("Please specify your company website - or type 'none' if you do not have one.");
		posWebsite.focus();
	}
	else if ( posText.value == '' || whiteSpace.test(posText.value) ) {
		alert("You have not specified which parts you require.  Please fill in the Parts Required section.");
		posText.focus();
	}
	else if ( posQuantity.value == '' ) {
		alert("Please specify the estimated quantity required for your application.");
		posQuantity.focus();
	}
	else if ( posEstpodate.value == '' ) {
		alert("Please supply an estimated purchase order date.");
		posEstpodate.focus();
	}
	else if ( posApplication.value == '' ) {
		alert("Please describe your intended application.");
		posApplication.focus();
	}
	else {
		sendPosEmail();
	}
	return false;  // Don't submit the form via php.  
	// Whether or not validation is successful, the form action is for non-javascript browsers, so this cancels it.
}

// Optosource also require slightly more information than others for enquiries
else if ( ( posNature.value == 'Enquiry' ) && ( posSource.value == 'optosource' ) ) {
	if ( ( posFirstName.value == '' || whiteSpace.test(posFirstName.value) ) && ( posSurname.value == '' || whiteSpace.test(posSurname.value) ) ) {
		alert("Please enter your name.");
		posFirstName.focus();
	}
	else if ( posJobTitle.value == '' ) {
		alert("Please specify your job title.");
		posJobTitle.focus();
	}
	else if ( posCompany.value == '' ) {
		alert("Please specify your company name.");
		posCompany.focus();
	}
	else if ( posPostCode.value == '' ) {
		alert("Please supply a postal code / ZIP.");
		posPostCode.focus();
	}
	else if ( posTel.value == '' ) {
		alert("Please supply a telephone number.");
		posTel.focus();
	}
	else if ( posEmail.value == '' ) {
		alert("Please supply an email address.");
		posEmail.focus();
	}
	else if ( posWebsite.value == '' ) {
		alert("Please specify your company website - or type 'none' if you do not have one.");
		posWebsite.focus();
	}
	else if ( posText.value == '' || whiteSpace.test(posText.value) ) {
		alert("You have not included your message.  Please fill in the Message section.");
		posText.focus();
	}
	else {
		sendPosEmail();
	}
	return false;  // Don't submit the form via php.  
	// Whether or not validation is successful, the form action is for non-javascript browsers, so this cancels it.
}

// Otherwise just require email and message
else {
	if ( posEmail.value == '' ) {
		alert("Please supply an email address.");
		posEmail.focus();
	}
	else if ( posText.value == '' || whiteSpace.test(posText.value) ) {
		alert("You have not included your message.  Please fill in the Message section.");
		posText.focus();
	}
	else {
		sendPosEmail();
	}
	return false;  // Don't submit the form via php.  
	// Whether or not validation is successful, the form action is for non-javascript browsers, so this cancels it.
}

} // END field validation

function convertStrEquiv (instring) {
	// convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.
	var outstring = instring.value;
	outstring = outstring.replace(/&/g,"**am**");
	outstring = outstring.replace(/=/g,"**eq**");
	outstring = outstring.replace(/\+/g,"**pl**");
	return outstring;
}
function sendPosEmail () {
	var success = document.getElementById('emailSuccess');
	var strNameTitle = convertStrEquiv(document.getElementById('nametitle'));
	var strFirstName = convertStrEquiv(document.getElementById('firstname'));
	var strSurname = convertStrEquiv(document.getElementById('surname'));
	var strEmail = convertStrEquiv(document.getElementById('email'));
	var strJobTitle = convertStrEquiv(document.getElementById('jobtitle'));
	var strCompany = convertStrEquiv(document.getElementById('company'));
	var strAddress = convertStrEquiv(document.getElementById('address'));
	var strCity = convertStrEquiv(document.getElementById('city'));
	var strState = convertStrEquiv(document.getElementById('state'));
	var strPostCode = convertStrEquiv(document.getElementById('postcode'));
	var strCountry = convertStrEquiv(document.getElementById('country'));
	var strTel = convertStrEquiv(document.getElementById('tel'));
	var strFax = convertStrEquiv(document.getElementById('fax'));
	var strWebsite = convertStrEquiv(document.getElementById('website'));
	var strHowFound = convertStrEquiv(document.getElementById('howfound'));
	var strText = convertStrEquiv(document.getElementById('question'));
	var strSource = convertStrEquiv(document.getElementById('source'));
	var strNature = convertStrEquiv(document.getElementById('nature'));
	if ( strNature == 'Sample Request' ) {
		var strColour = convertStrEquiv(document.getElementById('colour'));
		var strVoltage = convertStrEquiv(document.getElementById('voltage'));
		var strEstpodate = convertStrEquiv(document.getElementById('estpodate'));
		var strApplication = convertStrEquiv(document.getElementById('application'));
		var strQuantity = convertStrEquiv(document.getElementById('quantity'));
	}
	
	var strCC = document.getElementById('selfCC').checked;
	var page = "/message/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
	
	var stuff = "selfCC="+strCC+"&posNameTitle="+strNameTitle+"&posFirstName="+strFirstName+"&posSurname="+strSurname+"&posEmail="+strEmail+"&posJobTitle="+strJobTitle+"&posCompany="+strCompany+"&posAddress="+strAddress+"&posCity="+strCity+"&posState="+strState+"&posPostCode="+strPostCode+"&posCountry="+strCountry+"&posTel="+strTel+"&posFax="+strFax+"&posWebsite="+strWebsite+"&posHowFound="+strHowFound+"&posText="+strText+"&posSource="+strSource+"&posNature="+strNature;
	if ( strNature == 'Sample Request' ) {
		stuff = stuff + "&posColour="+strColour+"&posVoltage="+strVoltage+"&posEstpodate="+strEstpodate+"&posApplication="+strApplication+"&posQuantity="+strQuantity;
	}
	loadXMLPosDoc(page,stuff);
}
function showContactTimer () {
	var loader = document.getElementById('loadBar');
	loader.style.display = 'block';
	sentTimer = setTimeout("hideContactTimer()",100);
}

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;
	var selects = fieldArea.getElementsByTagName('select');
	var selectsLen = selects.length;
	// Hide the load bar alas! Done Loading
	loader.style.display = "none";
	success.style.display = "block";
	success.innerHTML = '<strong style="color:#55FF55; font-size: 10px;">'+grabPosXML("confirmation")+'</strong>';
	// Now Hijack the form elements
	var tQuestion = document.getElementById('question');
	tQuestion.value = '';
//	for ( i=0;i<inputsLen;i++ ) {
//		if ( inputs[i].getAttribute('type') == 'text' ) {
//			inputs[i].value = '';
//		}
//	}
//	for ( j=0;j<tAreasLen;j++ ) {
//		tAreas[j].value = '';
//	}
//	for ( k=0;k<selectsLen;k++ ) {
//		selects[k].selectedIndex = 0;
//	}
}

function ajaxContact() {
var submitButton = document.getElementById('sendContactEmail');
addEvent(submitButton, 'click', validateFields, false);
submitButton.onclick = function() { return false; }
}
addEvent(window, 'load',ajaxContact, false);