//Online shop JavaScript routines.

var TEST_URL="https://transact.nab.com.au/test/hpp/payment";
var LIVE_URL="https://transact.nab.com.au/live/hpp/payment";


var PRIVACY_MSG = "********* Perm-a-Pleat Privacy Statement *********\n\n"+
	"As part of your order, Perm-A-Pleat will retain your\n"+
	"Name and Contact details so that should the need arise\n"+
	"Perm-A-Pleat will be able to contact you directly.\n\n"+
	"Perm-A-Pleat uses a hosted payment gateway to process\n"+
	"your credit card payments. Under no circumstances will\n"+
	"your details be divulged to a third party without your\n"+
	"express consent.\n\n"+
	"Perm-A-Pleat cannot view or access Credit Card details\n"+
	"as they are encrypted via your computer browser and are\n"+
	"forwarded directly to the credit card processor (National\n"+
	"Australia Bank) at the checkout stage of the transaction.";



//Override this function.
function clearFields()
{}

//Override this function for school-specific checks.
//Return true if all ok, false otherwise.  Alert errors as necessary.
function checksOK()
{ return true; }

//Override this function.
//Must return an array of [ ['fieldname', 'fieldvalue'], ... ]
//Not including the product-related fields.
//
//Required fields: 
//'vendor_name' (eg. TJD0010)
//'payment_alert' (eg. mowbray.uniforms@permapleat.com.au)
//'payment_reference' (eg. 'mowbray')
//'gst_rate' (eg. '10')
//'gst_added' (eg. 'true')
//function getFormArray()
//{
//	hid = new Array();
//	hid[hid.length] = ['vendor_name', 'TJD0010'];
//	hid[hid.length]= ['payment_alert', CONTACT_EMAIL+","+SUPPORT_EMAIL];
//	hid[hid.length]= ['payment_reference', "MOWBRAY"];
//	hid[hid.length]= ['receipt_address', getID("email").value];
//	hid[hid.length]= ['gst_rate', '10']
//	hid[hid.length]= ['gst_added', 'true']
//	hid[hid.length]= ['information_fields', "Parent name"];
//	hid[hid.length]= ['Parent name', getID("parentname").value];
//
//	hid[hid.length]= ['information_fields', "Student name"];
//	hid[hid.length]= ['Student name', getID("studentname").value];
//
//	hid[hid.length]= ['information_fields',  "Phone number"];
//	hid[hid.length]= ['Phone number',  getID("phone").value];
//
//	hid[hid.length]= ['information_fields',  "Home group"];
//	hid[hid.length]= ['Home group',  getID("homegroup").value];
//
//	hid[hid.length]= ['information_fields',  "Campus"];
//	hid[hid.length]= ['Campus',  getID("campus").value];
//
//	hid[hid.length]= ['information_fields',  "Address line 1"];
//	hid[hid.length]= ['Address line 1',  getID("street").value];
//
//	hid[hid.length]= ['information_fields',  "Address line 2"];
//	hid[hid.length]= ['Address line 2',  getID("suburb").value+" "+getID("state").value+" "+getID("postcode").value];	
//	return hid;"
//}


function getID(objIn)
{ return document.getElementById(objIn);}


function clearAll()
{
	clearFields();
	
	getID("parentname").value = '';
	getID("studentname").value = '';
	getID("phone").value = '';
	getID("homegroup").value = '';
	getID("email").value = '';
	getID("street").value = '';
	getID("suburb").value = '';
	getID("state").value = '';
	getID("postcode").value = '';
	
	listcontainers = ["productlistleft","productlistright"];
	for (var l = 0; l < listcontainers.length; l++)
	{
		divs = getID(listcontainers[l]).getElementsByTagName("div");

		//For each product type...
		for (var i = 0; i < divs.length; i++)
		{
			tables = divs[i].getElementsByTagName("table");
			if (tables.length > 0)
			{
				rows=tables[0].rows;
				//...For each row (skipping the header row)...
				for (var k = 1; k < rows.length; k++)
				{
					//Input is in 4th cell.
					inp = rows[k].cells[3].getElementsByTagName("input")[0];
					inp.value="";
				}
			}
		}
	}
}


function contactDetailsOK()
{
	returnVar = false;

	if (checksOK())
	{
		if (getID("parentname").value == "")
		{
			alert("Error: Please enter parent name before going to checkout.");
		}
		else if (getID("studentname").value == "")
		{
			alert("Error: Please enter student name before going to checkout.");
		}
		else if (getID("phone").value == "")
		{
			alert("Error: Please enter phone number before going to checkout.");
		}
		else if (getID("homegroup").value == "")
		{
			alert("Error: Please enter home group before going to checkout.");
		}
		else if (getID("email").value == "")
		{
			alert("Error: Please enter email before going to checkout.");
		}
		else if (getID("street").value == "")
		{
			alert("Error: Please enter street address before going to checkout.");
		}
		else if (getID("suburb").value == "")
		{
			alert("Error: Please enter suburb before going to checkout.");
		}
		else if (getID("state").value == "")
		{
			alert("Error: Please enter state before going to checkout.");
		}
		else if (getID("postcode").value == "")
		{
			alert("Error: Please enter postcode before going to checkout.");
		}
		else
		{
			returnVar = true;
		}
	}
	return returnVar;
}

function initialise()
{
	alert('Welcome to the online order form. Please select the size and quantity of each product you require, enter your contact details then click Continue To Checkout to enter payment details.');
}

function showPolicy()
{
	alert(PRIVACY_MSG);
}


function submitForm()
{
	inputErrors = false;
	otherErrors = false;

	//First clear the form, in case they tried to submit it already this session.
	while (getID("theform").childNodes.length > 0)
	{
		getID("theform").removeChild(getID('theform').childNodes[0]);
	}

	//Check for contacts...
	if (contactDetailsOK())
	{
		//First add the miscellaneous fields.
		var hid = getFormArray();
		for (var i =0; i < hid.length; i++)
		{
			inp = document.createElement("input");
			//Real: 
			inp.type = 'hidden';
			//inp.type = 'text'; //DEBUG only.
			inp.name=hid[i][0];
			inp.value=hid[i][1];
			//inp.style.display='block';
			getID("theform").appendChild(inp);
		}

		//Tristan, 2010-09-02 Bother: the following line doesn't work on IE.
		prodList = document.getElementsByName("product");
		//Filter through all divs instead...
		//allDivs = document.getElementsByTagName("div");
		//prodList=[];
		//for (var i = 0; i < allDivs.length; i++)
		//{
		//	if (allDivs[i].id=='product')
		//	{
		//		prodList[prodList.length]=allDivs[i];
		//	}
		//}

		t = 0;  //For debugging.
		prodcount =0;
		rowcount=0;

		//For each product...
		//alert('v3, '+allDivs.length+"divs, "+prodList.length+' products.');
		for (var i = 0; i < prodList.length; i++)
		{
			prodcount++;
			description = prodList[i].getElementsByTagName("div")[0].innerHTML;
			tab = prodList[i].getElementsByTagName("table")[0].tBodies[0];
			//For each size range (k=1 is to skip the header row)...
			for (var k = 1; k < tab.rows.length; k++)
			{
				if (!inputErrors && !otherErrors)
				{
					rowcount++;
					range = tab.rows[k].cells[0].innerHTML;
					sz = tab.rows[k].cells[1].getElementsByTagName("select")[0].value;
					price = tab.rows[k].cells[2].innerHTML.replace("$","").replace(" ","");
					inp = tab.rows[k].cells[3].getElementsByTagName("input")[0];
					quant = inp.value;
					//Quick error checking...
					if (price.match(/^[0-9.]*$/) == null)
					{
						//Whoops, error.
						otherErrors=true;
						alert("An unexpected error occurred. Your order could not be processed.");
					}
					else if (quant.match(/^[0-9]*$/) == null)
					{
						//Whoops, user input invalid.
						inputErrors=true;
						alert("Error: Please enter a valid quantity or leave the box empty.");
						quant = tab.rows[k].cells[3].getElementsByTagName("input")[0].select();
					}
					else 
					{ //Inputs ok.
						if (quant != "")
						{
							t += 1;
							//Add this line to the form.
							nameStr = description;
							if (range != "")
							{
								nameStr += " "+range;
							}
							nameStr += " (size "+sz+")";
	
							inp = document.createElement("input");
							inp.type = 'text';
							inp.name=nameStr;
							inp.value=parseInt(quant)+","+parseFloat(price);
							//inp.style.display='hidden';  //Test only.
							getID("theform").appendChild(inp);
						}
					} //end if (errors in this line).
				} //End if(any errors so far)
			}//End for(each line in product)

			//Get *(&@#$(@#$ IE to re-render the form...
			//if (getID("theform").normalize) 
			//{
			//	getID("theform").normalize();
			//}
		}
	}
	else
	{
		inputErrors = true; //bad contactdetails.
	}

	if (!inputErrors && !otherErrors)
	{
		//Submit form.
		getID("theform").action = LIVE_URL;
		//alert("DEBUG pretending to submit form.");
		getID("theform").submit();
	}
}

