var cur;

function chgColor(oname){
	o = getObject(oname);
	o.style.backgroundColor="#FF9999";
	o.focus();
	cur = o;
}

function rstColor(o){
	o.style.backgroundColor="";
}

function fvalidate(f,misc){
	var o = eval("document."+f);
	if(cur!=null)rstColor(cur);
	switch(f){
		case "fContact":

			txtName = o.txtName.value;
			txtJobTitle = o.txtJobTitle.value;
			txtCompany = o.txtCompany.value;
			txtAddress = o.txtAddress.value;
			txtTel = o.txtTel.value;
			txtEmail = o.txtEmail.value;
			txtMessage = o.txtMessage.value;

			if(txtName==""){
				chgColor('txtName')
				alert("Please enter your name. \t");
				return false;
			}
			else if(txtAddress==""){
				chgColor('txtAddress')
				alert("Please enter your address. \t");
				return false;
			}
			else if(txtAddress.length>300){
				chgColor('txtAddress')
				alert("The address field is limited to 300 characters but you have entered "+o.txtAddress.value.length+". Please reduce this field. \t");
				return false;
			}
			else if(txtEmail==""){
				chgColor('txtEmail')
				alert("Please enter your email address. \t");
				return false;
			}
			else if(!isValidEmail(txtEmail)){
				chgColor('txtEmail')
				alert("Please enter a valid email address. \t");
				return false;
			}
			else if(txtMessage==""){
				chgColor('txtMessage')
				alert("Please enter your message. \t");
				return false;
			}
			else if(txtMessage.length>1000){
				chgColor('txtMessage')
				alert("The message field is limited to 1000 characters but you have entered "+o.txtMessage.value.length+". Please reduce this field. \t");
				return false;
			}
			else{
				o.hdnMailTo.value = "info@pdcbs.net";
				o.hdnMailFrom.value = "info@pdcbs.net";
				o.hdnMailFromName.value = "PDCBS Contact";
				o.hdnMailSubject.value = "Message from PDCBS Contact";
				s = "Name: " + txtName + "\n";
				if(txtJobTitle!="")s += "Job Title: " + txtJobTitle + "\n";
				if(txtCompany!="")s += "Company: " + txtCompany + "\n";
				s+= "\n" + "Address:\n" + txtAddress + "\n";
				s+= "\nTelephone: " + txtTel + "\n";
				s+= "\nEmail: " + txtEmail + "\n";
				s+= "\nMessage:\n" + txtMessage + "\n\n";
				o.hdnMailBody.value = s;
				return true;
			}
			break;

		case "fOnline50":

			txtName = o.txtName.value;
			txtJobTitle = o.txtJobTitle.value;
			txtCompany = o.txtCompany.value;
			txtAddress = o.txtAddress.value;
			txtTel = o.txtTel.value;
			txtEmail = o.txtEmail.value;
			txtMessage = o.txtMessage.value;
			cmbCustType =  o.cmbCustType.value;
			chkLine50 = o.chkLine50.checked;
			chkLine50Cover = o.chkLine50Cover.checked;
			chkSAC = o.chkLine50.checked;
			chkCall = o.chkCall.checked;
			chkDemo = o.chkDemo.checked;
			chkUpdate = o.chkUpdate.checked;
			chkUpdateOther = o.chkUpdateOther.checked;

			if(txtName==""){
				chgColor('txtName');
				alert("Please enter your name. \t");
				return false;
			}
			else if(txtCompany==""){
				chgColor('txtCompany');
				alert("Please enter your company name. \t");
				return false;
			}
			else if(txtAddress==""){
				chgColor('txtAddress');
				alert("Please enter your address. \t");
				return false;
			}
			else if(txtAddress.length>300){
				chgColor('txtAddress');
				alert("The address field is limited to 300 characters but you have entered "+o.txtAddress.value.length+". Please reduce this field. \t");
				return false;
			}
			else if(txtTel==""){
				chgColor('txtTel');
				alert("Please enter your telephone number. \t");
				return false;
			}
			else if(txtEmail==""){
				chgColor('txtEmail');
				alert("Please enter your email address. \t");
				return false;
			}
			else if(!isValidEmail(txtEmail)){
				chgColor('txtEmail');
				alert("Please enter a valid email address. \t");
				return false;
			}
			else if(txtMessage.length>1000){
				chgColor('txtMessage')
				alert("The message field is limited to 1000 characters but you have entered "+o.txtMessage.value.length+". Please reduce this field. \t");
				return false;
			}
			else if(cmbCustType==""){
				chgColor('cmbCustType');
				alert("Please choose the description which fits you best. \t");
				return false;
			}
			else{
				//build email
				
				mailto = (misc!=1)?"online50@pdcbs.net":"training@pdcbs.net"; 
				mailfromname = (misc!=1)?"PDCOnline50 Contact":"PDC Seminar Booking Form";
				mailsubject = (misc!=1)?"Message from PDC Online50 Contact":"Message from PDC Seminar Booking Form";
				
				o.hdnMailTo.value = mailto;
				o.hdnMailFrom.value = mailto;
				o.hdnMailFromName.value = mailfromname;
				o.hdnMailSubject.value = mailsubject;
				s = "Name: " + txtName + "\n";
				if(txtJobTitle!="")s += "Job Title: " + txtJobTitle + "\n";
				if(txtCompany!="")s += "Company: " + txtCompany + "\n";
				s+= "\n" + "Address:\n" + txtAddress + "\n";
				s+= "\nTelephone: " + txtTel + "\n";
				s+= "\nEmail: " + txtEmail + "\n";
				s+= "\nMessage:\n" + txtMessage + "\n\n";
				s+= "\nCustomer Type:" + cmbCustType + "\n";
				s+= "\nLine50 User:" + chkLine50 + "\n";
				s+= "\nLine50 Cover:" + chkLine50Cover + "\n";
				s+= "\nSage Accountants Club Member:" + chkSAC + "\n";
				s+= "\nTelephone Call Required:" + chkCall + "\n";
				s+= "\nPDC OnLine50 Demo Required:" + chkDemo + "\n";
				s+= "\nUpdates Required:" + chkUpdate + "\n";
				s+= "\nUpdates Required on other services:" + chkUpdateOther + "\n";
				o.hdnMailBody.value = s;
				return true;
			}
			break;

		case "fSearch":
			txtSearch = o.search.value;

			if(txtSearch==""){
				chgColor('search')
				alert("Please enter your search criteria. \t");
				return false;
			}
			else{
				return true;
			}
			break;

		case "fSageShop":
			chkLine50Acct = o.chkLine50Acct.checked;
			chkLine50AcctPlus = o.chkLine50AcctPlus.checked;
			chkLine50FinCont = o.chkLine50FinCont.checked;

			if(!chkLine50Acct && !chkLine50AcctPlus && !chkLine50FinCont){
				alert("Please choose a product to purchase. \t");
				return false;
			}
			else{
				return true;
			}
			break;

		case "fSageShopBill":

			txtName = o.txtName.value;
			txtJobTitle = o.txtJobTitle.value;
			txtCompany = o.txtCompany.value;
			txtHouse = o.txtHouse.value;
			txtStreet = o.txtStreet.value;
			txtTown = o.txtTown.value;
			txtCounty = o.txtCounty.value;
			txtCountry = o.txtCountry.value;
			txtPostCode = o.txtPostCode.value;
			txtTel = o.txtTel.value;
			txtEmail = o.txtEmail.value;

			if(txtName==""){
				chgColor('txtName')
				alert("Please enter your name. \t");
				return false;
			}
			else if(txtHouse==""){
				chgColor('txtHouse')
				alert("Please enter your house/building name or number. \t");
				return false;
			}
			else if(txtStreet==""){
				chgColor('txtStreet')
				alert("Please enter your street. \t");
				return false;
			}
			else if(txtTown==""){
				chgColor('txtTown')
				alert("Please enter your town. \t");
				return false;
			}
			else if(txtCounty==""){
				chgColor('txtCounty')
				alert("Please enter your county/state. \t");
				return false;
			}
			else if(txtCountry==""){
				chgColor('txtCountry')
				alert("Please enter your country. \t");
				return false;
			}
			else if(txtPostCode==""){
				chgColor('txtPostCode')
				alert("Please enter your post code. \t");
				return false;
			}
			else if(txtEmail==""){
				chgColor('txtEmail')
				alert("Please enter your email address. \t");
				return false;
			}
			else if(!isValidEmail(txtEmail)){
				chgColor('txtEmail')
				alert("Please enter a valid email address. \t");
				return false;
			}
			else{
				return true;
			}
			break;
	}
}

function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}
