/*****************************************************************
Purpose:	File used for registration page on Lift Directory, 
			calculates the amount required to list company on 
			site.  Includes a function that validates the page as well.
Author:		Arminder S Dahul (Using code written by Pete Maden)
Date: 		23/03/2005
*****************************************************************/

// variables used for checking which sections have been selected
var maintaincheck = 0, installcheck = 0, ccheck = 0, brochureCheck = 0; websiteCheck = 0;

// calculate the registration fee...
function calcAmount()
{
	maintaincheck = 0;
	installcheck = 0;
	ccheck = 0;
	brochureCheck = 0;
	website = 0;

	var theForm = document.regform;
	// running total value
	var totalVal = 0;

	theForm.desc.value = "Lift Directory Registration ";
	theForm.M_details.value = "";
	
	/*** CHECK WHICH OPTION WAS SELECTED ***/
	if (theForm.listing[1].checked == true || theForm.listing[0].checked == false)	//"||"fix for n6 radio button bug
	{	// user wants to register with name, email and website address
		totalVal = 140;
		theForm.desc.value="Option 2, listing(s) to show name, email and website address.";
		theForm.M_details.value="Option 2\n";
		theForm.listing[0].checked = false; //fix for n6 radio button bug
		
		// enable the text field
		theForm.website.disabled = false;
		// change colour for IE
		theForm.website.style.backgroundColor = '#FFFFFF';
		
		website=1;
		
	}
	// user wants to just use option 1 which is name and email only...
	if (theForm.listing[0].checked == true || theForm.listing[1].checked == false)	//"||"fix for n6 radio button bug
	{
		totalVal = 80;
		theForm.desc.value="Option 1, listing(s) to show name and email address.";
		theForm.M_details.value="Option 1\n";
		theForm.listing[1].checked = false; //fix for n6 radio button bug]
		
		// disable the text field
		theForm.website.disabled = true;
		// change colour for IE 
		theForm.website.style.backgroundColor = '#CCCCCC';
	}
	
	if (website == 1)
	{
		//theForm.M_details.value= (theForm.M_details.value + "Website address = " + trim(theForm.website.value) + ", "); 
		
		// set global variable
		websiteCheck = 1;
	}
	
	/*** check which option was selected ***/
	// check if 'Installations' was selected
	if (theForm.Installations.checked)
	{
		theForm.M_details.value= ( theForm.M_details.value + theForm.Installations.value + ", ");
		installcheck=1;
		// set hidden value...
		theForm.instCheck.value = 1; 
	}
	if (installcheck == 1)
		totalVal = totalVal + 10;
	// check if 'Maintainance' was selected
	if (theForm.Maintenance.checked)
	{
		theForm.M_details.value= ( theForm.M_details.value + theForm.Maintenance.value + ", ");
		maintaincheck=1;
		// set hidden value...
		theForm.mCheck.value = 1; 
	}
	if (maintaincheck == 1)
		totalVal = totalVal + 10;
	// check if 'Building Access' was selected
	/*if (theForm.BuildingAccess.checked)
	{
		theForm.M_details.value= ( theForm.M_details.value + theForm.BuildingAccess.value + ", ");
		baccesscheck=1;
		// set hidden value...
		theForm.baccessCheck.value = 1; 
	}
	if (baccesscheck == 1)
		totalVal = totalVal + 10;
	// check if 'Catalogues' was selected
	if (theForm.Catalogues.checked)
	{
		//alert("catalogues was selected!!!!! catcheck = " + catcheck);
		theForm.M_details.value= ( theForm.M_details.value + theForm.Catalogues.value + ", ");
		catcheck=1;
		// set hidden value...
		theForm.catalogueCheck.value = 1; 
		//alert("catcheck = " + catcheck);
		if (catcheck == 1)
		{
			totalVal = totalVal + 10;
			//alert("add or take away 10.00 totalVal= " + totalVal);
		}
	}*/
	
	// check other categories - each section selected adds £10.00 to the existing price....
	for( var i = 1; i <= 28; i++)
	{	
		var chkselection = ("subcat_" + i);
		if (theForm[chkselection].checked)
		{
			// add £80.00 if Brochures are selected.
			//alert("something was checked -> " + chkselection);
			if (theForm[chkselection].value == "Brochures")
			{
				brochureCheck = 1;
				//alert("brochureCheck -> inside the for loop " + brochureCheck);
				theForm.M_details.value= ( theForm.M_details.value + theForm[chkselection].value + ", ");
				totalVal = totalVal + 80;
			}
			else if (theForm[chkselection].value == "Employment - Submit Vacancy")	// add £20.00 for submitting a vacancy
				totalVal = totalVal + 20;
			else	// all others are £10.00...
			{
				ccheck = 1;
				theForm.M_details.value= ( theForm.M_details.value + theForm[chkselection].value + ", ");
				totalVal = totalVal + 10;
			}
			
			// set hidden value...
			theForm.cCheck.value = 1;
		}
	}

	// show the alert for testing purposes!
	//alert("Total value = " + totalVal + "\ntheForm.M_details.value=" + theForm.M_details.value);

	//final payment calculation
	//if statement ensures amount shows minimum value before any categories selected
	if (maintaincheck==0 && installcheck==0 && ccheck==0 && brochureCheck==0)
	{
		//theForm.amount.value = ( Math.round ( ( totalVal ) * 1.175 * 100 ) ) / 100;
		var finalVal = ( Math.round ( ( totalVal ) * 1.175 * 100 ) ) / 100;
		document.getElementById("amountSpan").innerHTML = pence(finalVal);
		theForm.amount.value = pence(finalVal);
	} 
	else 
	{
		//theForm.amount.value = ( Math.round ( ( totalVal - 10 ) * 1.175 * 100 ) ) / 100;
		var finalVal = ( Math.round ( ( totalVal - 10 ) * 1.175 * 100 ) ) / 100;
		document.getElementById("amountSpan").innerHTML = pence(finalVal);
		theForm.amount.value = pence(finalVal);
	}
	
	
	// print the email details...
	//alert(theForm.M_details.value + ", " + theForm.desc.value);
}

// validate the form before submitting....
function validForm( theForm )
{
	var errs = "";
	if ( trim(theForm.name.value) == "")
		errs += "Please enter your name\n";
	if ( trim(theForm.cartId.value) == "")
		errs += "Please enter the company name\n";
	if ( trim(theForm.addr.value) == "")
		errs += "Please enter the address\n";
	if ( trim(theForm.pcode.value) == "")
		errs += "Please enter the post code\n";
	if ( trim(theForm.country.value) == "none")
		errs += "Please select the country\n";
	// return false if invalid and true if the email is valid
	var vemail = isValidEmail(theForm.email.value);
	if ( trim(theForm.email.value) == "" || !vemail )
		errs += "Please enter your email address\n";
	if ( trim(theForm.tel.value) == "")
		errs += "Please enter the telephone number\n";
	
	// the user has selected option 2 which requires a valid website...
	if (websiteCheck == 1)
	{
		if (theForm.listing[1].checked == true && trim(theForm.website.value) == "" )
			errs += "Please enter the website URL\n";
		else
		{
			// find the index of Website address
			// divide the text into an array
			var arrStr = theForm.M_details.value.split(", ");
			theForm.M_details.value = theForm.desc.value;
			
			for (var i=0; i < arrStr.length; i++)
			{
				dividedStr += arrStr[i] + "\n";
				// put all the array elements into a new string where it doesn't have website in it...
				// -1 indicates that it wasn't found
				if (arrStr[i].indexOf("Website") == -1)
				{
					theForm.M_details.value += arrStr[i] + ",";
				}
				
			}
			//alert("divided string -> " +dividedStr );
			theForm.M_details.value= (theForm.M_details.value + "Website address = '" + trim(theForm.website.value) + "', ");
			//var index = theForm.M_details.value.indexOf("Website address");
			//alert("index = " + index);
			
			//alert(theForm.M_details.value);
		}
	}
	
	// check if categories were selected...else show an error message
	if (maintaincheck==0 && installcheck==0 && ccheck==0 && brochureCheck == 0)
		errs += "Please select at least one category\n";
	
	// check if the pages have been entered.
	if (brochureCheck == 1)
	{
		if ( trim(theForm.brochurepages.value) == "" || isNaN(trim(theForm.brochurepages.value)) )
			errs += "You have selected the Brochures category - Please enter the total number of page between 1-15\n";
		else 
		{	// update the string to show number of pages for the brochure
			theForm.M_details.value= (theForm.M_details.value + "Brochure Pages = " + theForm.brochurepages.value + ", "); 
			//alert(theForm.M_details.value);
		}
	}
	
	
	//REMOVE ONCE TESTED 
	//alert(theForm.M_details.value);
	
	if (errs != "")
	{
		alert("Please recomplete the following items\n\n" + errs);
		return false;
	}
	else
		return true;
}

// function edited from irt.org called "cent" FAQ on page -> http://developer.irt.org/script/6.htm
function pence (amount) 
{
	// returns the amount in the .99 format
    	amount -= 0;
    	return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
