lineF= "\r\n";
var errorWline=''
var errorFSline=''
var slash= '/';
reEmail=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
reNumb=/^\w+([\. -]*\w+)$/

reNumb2=/^\$[0-9,]+(\.[0-9][0-9])?$/


//FUNC
function getFSvars(id)
{
	eval('adpnumb=f.' + id + '_numb.value.toUpperCase()')
	eval('adback=f.' + id + '_back[f.' + id + '_back.selectedIndex].value')
	eval('adtext=f.' + id + '_text[f.' + id + '_text.selectedIndex].value')
	eval('adcondi=f.' + id + '_condi[f.' + id + '_condi.selectedIndex].value')
	eval('adtype=f.' + id + '_type[f.' + id + '_type.selectedIndex].value')
	eval('adstate=f.' + id + '_state[f.' + id + '_state.selectedIndex].value')

	//for (k=0; k<2; k++)
	//{
	//eval('pos=f.' + id + '_duration[' + k + '].checked');
	//if (pos)
		{eval('addura=f.' + id + '_duration.value;');}
	//}
	//addura="25.00"
	eval('adprice=f.' + id + '_price.value')
	eval('addescr=f.' + id + '_descr.value')
}

//FUNC
function getWvars(id)
{
	eval('adpnumb=f.' + id + '_numb.value.toUpperCase()')
	eval('adtype=f.' + id + '_type[f.' + id + '_type.selectedIndex].value')
	eval('adstate=f.' + id + '_state[f.' + id + '_state.selectedIndex].value')
	eval('addura=f.' + id + '_duration.value;')
}

//FUNC
function validateFSline(id)
{
	f=document.adForm;
	errorFSline=''

	getFSvars(id)
	if (reNumb.test(adpnumb))
		{}
		else
		{errorFSline+= "* Number Plate Number must have A-Z, 0-9 or hyphen, fullstop or space characters"+lineF;errorFound=true;}
	if (adpnumb=='')
		{errorFSline+= "* Number Plate Number"+lineF;errorFound=true;}
	if (adback=='')
		{errorFSline+= "* Background Colour (use black if your colour not available)"+lineF;errorFound=true;}
	if (adtext=='')
		{errorFSline+= "* Number Plate Digit Colour (use white if your colour not available)"+lineF;errorFound=true;}
	if (adcondi=='')
		{errorFSline+= "* Condition"+lineF;errorFound=true;}
	if (adstate=='')
		{errorFSline+= "* State"+lineF;errorFound=true;}
	if (adtype=='')
		{errorFSline+= "* Type"+lineF;errorFound=true;}
	if (reNumb2.test(adprice))
		{}
		else
		{errorFSline+= "* Price must have 0-9 or comma, decimal point and $ characters only"+lineF;errorFound=true;}
	//if (adprice=='')
	//	{errorFSline+= "* Price (also include $) "+lineF;errorFound=true;}
	if (addescr=='')
		{errorFSline+= "* Description"+lineF;errorFound=true;}
}

//FUNC
function validateWline(id)
{
	f=document.adForm;
	eval('adtype=f.' + id + '_type[f.' + id + '_type.selectedIndex].value')
	eval('adstate=f.' + id + '_state[f.' + id + '_state.selectedIndex].value')
	if (adstate=='')
		{errorWline+= "* WANTED advert line #" + id.charAt(1) + " - State missing"+lineF;errorFound=true;}
	if (adtype=='')
		{errorWline+= "* WANTED advert line #" + id.charAt(1) + " - Type missing"+lineF;errorFound=true;}
}

//FUNC
function calcTotal()
{
	f=document.adForm;
	totalCharge=0

	if ((f.chk_ebay1.checked) && (f.chk_ebay2.checked) && (f.chk_ebay3.checked))
		{ebayFee=1500;}
	else
		{ebayFee=0;}

	for (i=1; i<6; i++)
	{
	id='p'+i;
	eval('adpnumb=f.' + id + '_numb.value')
	if (adpnumb!='')
		{
		  val = 25;	
		  totalCharge+=parseFloat(val*100);
		  totalCharge+=ebayFee;
		}
	}

	for (i=1; i<3; i++)
	{
	id='w'+i;
	eval('adpnumb=f.' + id + '_numb.value')
	if (adpnumb!='')
		{
		eval('val=f.' + id + '_duration.value;')
		totalCharge+=parseFloat(val.substring(0,4)*100)
		}
	}
	f.cc_total.value=totalCharge/100
}

//FUNC
function buildFSplateDetails(p,equ,sem,lf)
{
	npString= 'stateRegion="' + adstate + '"' + sem + lf
	npString+= 'plateType="' + adtype + '"' + sem + lf
	npString+= 'duration="$' + addura + '"' + sem + lf + lf
	
	if (document.adForm.chk_hire.checked)
		{hireStatus='yesHire';}
 	else
		{hireStatus='noHire';}
 
	if (sem=='')
		{
		npString= 'The following entry to be added to the ' + adstate.toUpperCase() + ' ' + adtype.toUpperCase() + ' SALES datafile. Fee/Duration=$' + addura + sem + lf + lf
		npString+= '' + slash + slash + '------------------' + sem + lf + lf;
		npString+= 'position=	"0"' + sem + lf + lf
		npString+= 'x=position' + sem + lf
		npString+= 'nptID[' + p + ']' + equ + '"' + hireStatus + '"' + sem + lf
		npString+= 'eDate[' + p + ']' + equ + '"YYYY' + slash + 'MM' + slash + 'DD"' + sem + lf
		}

	npString+= 'pnumb[' + p + ']' + equ + '"' + adpnumb + '"' + sem + lf
	npString+= 'ptext[' + p + ']' + equ + '"' + adtext + '"' + sem + lf
	npString+= 'pback[' + p + ']' + equ + '"' + adback + '"' + sem + lf
	npString+= 'descr[' + p + ']' + equ + '"' + addescr + '"' + sem + lf
	npString+= 'condi[' + p + ']' + equ + '"' + adcondi + '"' + sem + lf
	npString+= 'price[' + p + ']' + equ + '"' + adprice + '"' + sem + lf
}

//FUNC
function buildWplateDetails(p,equ,sem,lf)
{	npString= 'stateRegion="' + adstate + '"' + sem + lf
	npString+= 'plateType="' + adtype + '"' + sem + lf
	npString+= 'duration="$' + addura + '"' + sem + lf

	if (sem=='')
		{
		npString= 'The following entry to be added to the ' + adstate.toUpperCase() + ' ' + adtype.toUpperCase() + ' WANTS datafile. Fee/Duration=$' + addura + sem + lf + lf
		npString+= '' + slash + slash + '------------------' + sem + lf + lf;
		npString+= 'position=	"0"' + sem + lf + lf
		npString+= 'x=position' + sem + lf
		npString+= 'nptID[' + p + ']' + equ + '"enter NPT id"' + sem + lf
		npString+= 'eDate[' + p + ']' + equ + '"YYYY' + slash + 'MM' + slash + 'DD"' + sem + lf
		}


	npString+= 'pnumb[' + p + ']' + equ + '"' + adpnumb + '"' + sem + lf
	npString+= 'ptext[' + p + ']' + equ + '"white"' + sem + lf
	npString+= 'pback[' + p + ']' + equ + '"black"' + sem + lf
}

//FUNC
function buildContactDetails(p,equ,sem,lf)
{	contString= 'cname[' + p + ']' + equ + '"' + f.c_name.value + '"' + sem + lf
	contString+= 'cphon[' + p + ']' + equ + '"' + f.c_phon.value + '"' + sem + lf
	contString+= 'cmobi[' + p + ']' + equ + '"' + f.c_mobi.value + '"' + sem + lf
	contString+= 'cemai[' + p + ']' + equ + '"' + f.c_emai.value + '"' + sem + lf
	contString+= 'cothr[' + p + ']' + equ + '"' + f.c_othr.value + '"' + sem + lf + lf
}

//FUNC
function viewAdvert(id)
{
	f=document.adForm
	errorFound=false

	validateFSline(id)

	if (errorFound)
		{alert("The following advert fields are incorrect or missing:" +lineF +lineF+ errorFSline + lineF + "Please correct the entries and try again" + lineF)}
	else
		{
		buildFSplateDetails('1','=',';','')
		buildContactDetails('1','=',';','')
		linkString= contString + npString;
		linkRef=  'fcnswpop.htm?sample='+escape(linkString); //any *.pop.htm page will work
		popUpH(linkRef , 'npt', 315,330,'tr');
		}
}

//FUNC
function validateForm()
{
	for (i=1; i<6; i++)
		{
		id='p'+i;
		eval('adpnumb=f.' + id + '_numb.value')
		if (adpnumb!='')
			{
			errorFound=false
			validateFSline(id)
			if (errorFound)
				{errorFSline2+= "* FOR SALE advert #" + i +  " is incorrect" +lineF;errorFound2=true;}
			}
		}

	for (i=1; i<3; i++)
		{
		id='w'+i;
		eval('adpnumb=f.' + id + '_numb.value')
		if (adpnumb!='')
			{
			errorFound=false
			validateWline(id)
			if (errorFound)
				{errorFound2=true;}
			}
		}

	if (f.pd_name.value=='')
		{errorList+= "* Fullname"+lineF;errorFound2=true;}
	if (f.pd_address.value=='')
		{errorList+= "* Address"+lineF;errorFound2=true;}
	if (f.pd_pcode.value=='')
		{errorList+= "* Post Code"+lineF;errorFound2=true;}
	if (f.pd_state.value=='')
		{errorList+= "* State/Country"+lineF;errorFound2=true;}
	//if (f.email.value=='')
	//	{errorList+= "* Personal Email Address"+lineF;errorFound2=true;}
	if (reEmail.test(f.email.value))
		{}
		else
		{errorList+= "* Personal Email Address invalid"+lineF;errorFound2=true;}
	if (f.c_name.value=='')
		{errorList+= "* Contact Name"+lineF;errorFound2=true;}

	if (f.c_emai.value=='' && f.c_phon.value=='' && f.c_mobi.value=='' && f.c_othr.value=='' )
		{errorList+= "* Contact details missing"+lineF;errorFound2=true;}

	if (f.c_emai.value!='' && reEmail.test(f.c_emai.value))
		{}
		else
		{errorList+= "* Contact Email Address invalid"+lineF;errorFound2=true;}

	pm= -1
	for (i=0; i<f.pay_method.length; i++)
		{
		if (f.pay_method[i].checked)
		{pm=i}
		}

	//if (pm == -1)
	//	{errorList+= "* Payment Method"+lineF;errorFound2=true;}

	///if (pm == 4 && f.cc_name.value=='')
	///	{errorList+= "* Name on Credit Card"+lineF;errorFound2=true;}
	///if (pm == 4 && f.cc_number.value=='')
	///	{errorList+= "* Credit Card Number"+lineF;errorFound2=true;}
	///if (pm == 4 && f.cc_exp.value=='')
	///	{errorList+= "* Credit Card Expiry Date"+lineF;errorFound2=true;}
	//if (pm == 4 && f.cc_type[f.selectedIndex].value=='')
	//	{errorList+= "* Credit Card Type"+lineF;errorFound2=true;}

	if ((f.chk_ebay1.checked) || (f.chk_ebay2.checked) || (f.chk_ebay3.checked))
		{
		if ((!f.chk_ebay1.checked) || (!f.chk_ebay2.checked) || (!f.chk_ebay3.checked))
			{errorList+= "* Ebay terms and conditions not correctly set"+lineF;errorFound2=true;}
		}

	if ((f.chk_ebay1.checked) && (f.chk_ebay2.checked) && (f.chk_ebay3.checked) && (pm!=4))
		{errorList+= "* Credit Card details must accompany an Ebay advert"+lineF;errorFound2=true;}
}

//FUNC
function checkForm() {

	f=document.adForm;
	hf=document.hiddenForm;

	errorFound2=false;
	errorList=''
	errorWline=''
	errorFSline=''
	errorFSline2=''

	validateForm()

	if (errorFound2)
		{alert("The following important fields are incorrect or missing from the form:" +lineF+lineF + errorList + errorFSline2 + errorWline +lineF+ "Please correct the entries and resubmit"+lineF)}
	else
		{alert("The form has been completed correctly, and is now ready to be sent to NPT"+lineF)}
}


//FUNC
function processOrder() {

	f=document.adForm;
	hf=document.hiddenForm;

	errorFound2=false;
	errorList=''
	errorWline=''
	errorFSline=''
	errorFSline2=''

	validateForm()

	if (errorFound2)
		{alert("The following important fields are incorrect or missing from the form:" +lineF+lineF + errorList + errorFSline2 + errorWline +lineF+ "Please correct the entries and resubmit"+lineF)}
	else
		{
		hf.CUSTOMER_NAME.value=f.pd_name.value
		hf.realname.value=f.pd_name.value
		hf.CUSTOMER_ADDRESS.value=f.pd_address.value
		hf.CUSTOMER_POSTCODE.value=f.pd_pcode.value
		hf.email.value=f.email.value

		hf.CUSTOMER_STATE.value=f.pd_state.value
		hf.CUSTOMER_PHONE.value=f.pd_phone.value
		hf.CUSTOMER_MOBILE.value=f.pd_mobile.value
		hf.CUSTOMER_FAX.value=f.pd_fax.value
		hf.CUSTOMER_COMMENTS.value=f.cust_comments.value

		if ((f.chk_ebay1.checked) && (f.chk_ebay2.checked) && (f.chk_ebay3.checked))
			{hf.ADD_TO_EBAY_STORE.value="YES";}
 		else
			{hf.ADD_TO_EBAY_STORE.value="NO";}

		for (k=0; k<f.pay_method.length; k++)
			{
			eval('pos=f.pay_method[' + k + '].checked');
			if (pos)
				{eval('hf.PAY_METHOD.value=f.pay_method[' + k + '].value;');}
			}

		///if (hf.PAY_METHOD.value=='credit card')
		///	{
		///	hf.CARD_TYPE.value=f.cc_type.value
		///	hf.CARD_NUM.value=f.cc_number.value
		///	hf.NAME_ON_CARD.value=f.cc_name.value
		///	hf.CARD_EXPIRY.value=f.cc_exp.value
		///	}
		hf.TOTAL_COST.value='$' + f.cc_total.value

		if (f.cc_total.value==0)
			{hf.PAY_METHOD.value='no charge'}

		buildContactDetails('x','=\t','',lineF)

		for (i=1; i<6; i++)
			{
			id='p'+i;
			eval('adpnumb=f.' + id + '_numb.value')
			if (adpnumb!='')
				{
				getFSvars(id)
				buildFSplateDetails('x','=\t','',lineF)
				eval('hf.FOR_SALE_AD'+i+'.value=npString+contString')
				}
			}

		for (i=1; i<3; i++)
			{
			id='w'+i;
			eval('adpnumb=f.' + id + '_numb.value')
			if (adpnumb!='')
				{
				getWvars(id)
				buildWplateDetails('x','=\t','',lineF);
				eval('hf.WANTED_AD'+i+'.value=npString+contString')
				}
			}

		var currentTime = new Date();
		var month = currentTime.getMonth();
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		var hours = currentTime.getHours();
		var minutes = currentTime.getMinutes();
		var seconds = currentTime.getSeconds();

		JStimestamp = 'NPT'+year+''+month+''+day+''+hours+''+minutes+''+seconds;

		hf.RECEIPT_ID.value = JStimestamp;		

		if (hf.PAY_METHOD.value=='paypal')
			{
			extraVars = "?cost="+f.cc_total.value+"&rec="+JStimestamp ;
			}
		else
			{
			extraVars = "";
			}
 

		hf.redirect.value = "http://www.numberplatetrader.com.au/ethanks.htm"+extraVars;
		//alert(hf.redirect.value);
		document.hiddenForm.submit();
		}
	return false;
}
