/*------------==============COMMON LIB=================------------
-- Author: Lee Sinclair/Ross Crawford
-- Organisation: Method & Madness
-- Version: 1.0
-- Copyright: July 30 2001
-- Copyright (c) 2001 Method and Madness. All Rights Reserved.
--
-- Date: 5th July 2001
//------------=========================================------------
*/
//-----------------------------------------------------------------------------------------
// GLOBAL FLAG VARIABLES
//-----------------------------------------------------------------------------------------
	printingPage = false;

//-----------------------------------------------------------------------------------------
// SWAP IMAGE AND SWAPBACK FUNCTIONS
//-----------------------------------------------------------------------------------------
//START IMAGE-SWITCH
//	UPPER NAV
		icon1a = new Image(); icon1a.src="images/button_contact.gif";
		icon1b = new Image(); icon1b.src="images/button_contact_B.gif";
		icon2a = new Image(); icon2a.src="images/button_FAQ.gif";
		icon2b = new Image(); icon2b.src="images/button_FAQ_B.gif";
		icon3a = new Image(); icon3a.src="images/button_Home.gif";
		icon3b = new Image(); icon3b.src="images/button_Home_B.gif";
		icon4a = new Image(); icon4a.src="images/button_print.gif";
		icon4b = new Image(); icon4b.src="images/button_print_B.gif";
		icon5a = new Image(); icon5a.src="images/button_quoteResults.gif";
		icon5b = new Image(); icon5b.src="images/button_quoteResults_B.gif";
		icon6a = new Image(); icon6a.src="images/button_restartCalculator.gif";
		icon6b = new Image(); icon6b.src="images/button_restartCalculator_B.gif";
		icon7a = new Image(); icon7a.src="images/button_understandingNovatedLeasing.gif";
		icon7b = new Image(); icon7b.src="images/button_understandingNovatedLeasing_B.gif";
//	LOWER NAV
		icon8a = new Image(); icon8a.src="images/start_disc_start.gif";
		icon8b = new Image(); icon8b.src="images/start_disc_start_B.gif";
//	EXIT BUTTON
		icon9a = new Image(); icon9a.src="images/button_Close.gif";
		icon9b = new Image(); icon9b.src="images/button_Close_B.gif";

		function swapImage(img, target){

			if (printingPage == false) {
				img.src = target.src;
			}

		}

function findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

//-----------------------------------------------------------------------------------------
//	ADD PAGE TO FAVOURITES
//-----------------------------------------------------------------------------------------
function addToFavs(thisURL, docTitle)
{
	if (!document.all){
		alert('Please press ctrl + D to book mark this page');
	}else{
		window.external.AddFavorite(thisURL,docTitle);
	}
}
//-----------------------------------------------------------------------------------------
// SET PAGE AS DEFAULT HOMEPAGE
//-----------------------------------------------------------------------------------------
function setAsHome(thisURL)
{
	if (!document.all){
		netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite");
		navigator.preference('browser.startup.homepage',thisURL);
	}else{
      		this.setHomePage(thisURL);
	}
}
//-----------------------------------------------------------------------------------------
// OPEN WINDOW FUNCTION
//-----------------------------------------------------------------------------------------
function openWin(path, name)
{
	var pathToFile = path;
	var windowName = name;
	window.open(pathToFile , windowName, 'width = 640 , height = 480, titlebar= yes, scrollbars = yes');
}
//-----------------------------------------------------------------------------------------
// CALCULATOR FORM VALIDATION
//-----------------------------------------------------------------------------------------
var fieldNames = [['annualGrossSalary', 'Annual Gross Salary'],['finDet_amount', 'Amount to Finance'],['finDet_FBTPrice', 'FBT Price'], ['finDet_LeaseTerm', 'Lease Term'], ['finDet_totalKm', 'Total Kilometers per Annum'],['finDet_interestRate', 'Interest Rate'], ['leaseServ_tyresNum', 'Number of Tyres'], ['leaseServ_insurance', 'Insurance input per Annum'], ['vehicleDetails_vehicleName', 'Vehicle Manufacturer'], ['vehicleDetails_vehicleType', 'Vehicle Type'],['vehicleDetails_state', 'State'],['vehicleDetails_newOrUsed', 'New or Used'],['vehicleDetails_fuelType','Fuel Type'],['vehicleDetails_engineCapacity', 'Engine Capacity']]

function checkRequired( ){
	var required = 'Errors :\n';
	var integerMsg = '';
//	var requiredFields = new Array('annualGrossSalary', 'finDet_amount', 'finDet_LeaseTerm', 'finDet_totalKm', 'finDet_interestRate', 'vehicleDetails_vehicleName', 'vehicleDetails_vehicleType', 'vehicleDetails_state', 'vehicleDetails_newOrUsed', 'vehicleDetails_fuelType');
    var requiredFields = new Array('annualGrossSalary', 'finDet_amount', 'finDet_LeaseTerm', 'finDet_totalKm', 'vehicledetails_vehiclemake', 'vehicledetails_model', 'vehicleDetails_state', 'vehicleDetails_newOrUsed', 'vehicleDetails_fuelType');
//	var checkBoxFields = new Array(['leaseServ_motorclubCheckbox','leaseServ_motorclubLOS','Motor club level of service'],['finDet_FBTCheckbox', 'finDet_FBTPrice', 'FBT Price'], ['leaseServ_tyres', 'leaseServ_tyresNum', 'Number of Tyres'], ['leaseServ_insuranceCheckbox', 'region', 'Insurance region']);
    var checkBoxFields = new Array(['leaseServ_motorclubCheckbox','leaseServ_motorclubLOS','Motor club level of service'], ['leaseServ_tyres', 'leaseServ_tyresNum', 'Number of Tyres'], ['leaseServ_insuranceCheckbox', 'region', 'Insurance region']);
	var element;
	var thisElement = '';

	for (var n = 0; n < requiredFields.length; n++) {
		element = findObj(requiredFields[n]);
		if (element.value == "" || element.value=="none" ) { required += (findName(requiredFields[n])+': must contain a value\n'); }
	}

	for (var n = 0; n < checkBoxFields.length; n++) { //	CHECK FOR CHECKED TICKBOXES AND NO VALUES IN CORRESPONDING INPUT FIELDS
		element = findObj(checkBoxFields[n][0]);
		elementB = findObj(checkBoxFields[n][1]);
		if (element.checked == true && (elementB.value == "" || elementB.value=="none") )
        { required += ('\nThe tickBox for '+ checkBoxFields[n][2] +' is ticked. you must provide a value\n'); }
	}

	if (required == 'Errors :\n')
    {  obj = document.getElementById("vehicledetails_model");
       form1.veh_model.value = obj.value;
		document.forms[0].submit();
	}
    else{
		window.alert(required);
	}

}
//-----------------------------------------------------------------------------------------
// LOCATE A PARTICULAR FIELD IN THE FORM
//-----------------------------------------------------------------------------------------
function findName(fieldName) {
	// Date: 31/07/2001
	// Author: Lee Sinclair
	// Description: Find the display name of a real field, basically a look up field
	var tlength = fieldNames.length;
	for (var i=0;i<tlength;i++) {
		if (fieldNames[i][0]==fieldName)
			return fieldNames[i][1];
	}
	return 'Undefined';
}

//-----------------------------------------------------------------------------------------
//	ENSURE THAT THE CHECKBOX BEFORE OR AFTER A PARTICULAR FIELD HAS BEEN CHECKED
//	whichElement	- Name of field passed into the function
//	direction 		- check the field ahead or behind the tickbox
//-----------------------------------------------------------------------------------------
function checkTickBox(whichElement, direction){
var thisBox = '';
var elementNum = 0;
//	CHECK DIRECTION FORWARD (1) BACKWARD (0) FROM INITIAL FIELD
//	FORWARDs
if (direction == "1") {

		for (var i=0;i<document.forms[0].elements.length;i++){
        	if (whichElement.name == document.forms[0].elements[i].name){
				thisBox = document.forms[0].elements[i+1];
				elementNum = i;
			}
		}
		//	CHECK FIELD HAS A VALUE ELSE UNCHECK THE TICKBOX
		if (whichElement.value.length == 0 ) {
			thisBox.checked = false;
		} else {
			thisBox.checked = true;
		}

//	BACKWARDs
} else {

		for (var i=0;i<document.forms[0].elements.length;i++){
        	if (whichElement.name == document.forms[0].elements[i].name){
				thisBox = document.forms[0].elements[i-1];
				elementNum = i;
			}
		}
		//	CHECK FIELD HAS A VALUE ELSE UNCHECK THE TICKBOX
	if ( document.forms[0].elements[whichElement.name].selectedIndex >= 0) {		//	Check if element is a dropdown (selectBox)

		if ( document.forms[0].elements[whichElement.name].selectedIndex < 1 ) {  	//	TRUE - check selectbox selected index
			thisBox.checked = false;
		} else {
			thisBox.checked = true;
		}

	}else{

		if ( document.forms[0].elements[whichElement.name].value.length == 0 ) {  	//	FALSE - the element is a text field
			thisBox.checked = false;

			if (whichElement.name == 'leaseServ_tyresNum') {
				document.forms[0].elements['leaseServ_tyresNum'].value = "0";
				document.forms[0].elements['leaseServ_tyres'].checked = false;
			}

		} else {
			thisBox.checked = true;
		}
	}
}
//	END FUNCTION
}
//-----------------------------------------------------------------------------------------
// ENSURE THE TICKBOX IS UNCHECKED IF THE CORRESPONDING INPUT FIELD HAS NO VALUE
//	whichElement 	- value passed in from function
//	direction 		- check the field ahead or behind the tickbox
//	thisValue		- set the value of the input filed to a specific value
//-----------------------------------------------------------------------------------------
function ensureUnchecked(whichElement, direction, thisValue){
var thisField = whichElement.name;
var thisNum

	for (var i=0;i<document.forms[0].elements.length;i++){
       	if (whichElement.name == document.forms[0].elements[i].name){
			thisField = document.forms[0].elements[i+1].name;
			thisNum = i+1;
		}
	}

	if (document.forms[0].elements[thisField].type == 'text'){		//	CHECK FOR INPUT TYPE AND SET DEFAULT VALUE
		if (thisValue == 'undefined') {
			document.forms[0].elements[thisNum].value = '';
		} else {
			document.forms[0].elements[thisNum].value = thisValue;
		}
	}else{
		document.forms[0].elements[thisField].selectedIndex = 0;
	}
}

function checkForm(){
//	FINAL CHECK
checkRequired()
}
//-----------------------------------------------------------------------------------------
// PRINT FUNCTION
//-----------------------------------------------------------------------------------------
function printPage() {
	printingPage = true;										//	Set the printing flag to true so that the mouseover on the print button does not work
	var pageImages = new Array();

//	HIDE IMAGES ON PAGE
	for (var p = 0; p < document.images.length; p++) {
		pageImages[p] = document.images[p].src;
		if (document.images[p].name == 'title' ) {
			doNothing();
		}else{
			document.images[p].src = "images/transparant.gif";
		}
	}
//	PRINT
	window.print();

//	RESHOW THE IMAGES
	for (var j = 0; j < document.images.length; j++) {
		document.images[j].src = pageImages[j];
	}
	printingPage = false;

}
//-----------------------------------------------------------------------------------------
// DOES ABSOLUTELY NOTHING
//-----------------------------------------------------------------------------------------
function doNothing(){
}