//AUTHOR: Steven Yates - Designed and coded for ACE - I.R.M Insurance Pty Ltd
//EMAIL:  stevenyates@hotmail.com
//LAST CHANGE: 26/02/2004
var form;

var nonManEle = new Array("access1", "access2", "access3", "access4", "cost1", "cost2", "cost3", "cost4", "accessValue", "modList");
var elementErrorArray = new Array();
elementErrorArray[0] = "emailAdd: You have not entered an email addresss, please enter a valid address in the space provided.";
elementErrorArray[1] = "insName: You have not entered a name for the insured.";
elementErrorArray[2] = "regOwner: You have not entered a name for the registered owner of the vehicle.";
elementErrorArray[3] = "garageAdd: You have not entered a garaging address.";
elementErrorArray[4] = "postCode: You have not entered a postcode.";
elementErrorArray[5] = "mainDriverName: You have not entered the primary driver' name.";
elementErrorArray[6] = "licnum1: Please enter a valid licence number.";
elementErrorArray[7] = "dob1: Please enter a date of birth in the space provided.";
elementErrorArray[8] = "yrMade: Please enter the year the vehicle was manufactured.";
elementErrorArray[9] = "carMake: Please enter the make of the vehicle.";
elementErrorArray[10] = "carModel: Please enter the vehicle model.";
elementErrorArray[11] = "bodyType: Please enter the type of body the vehicle has.";
elementErrorArray[12] = "ltSize: Please enter the engine capacity in Litres.";
elementErrorArray[13] = "regNumber: Please enter the vehicles registration number.";
elementErrorArray[14] = "carColour: Please enter the colour of the vehicle.";
elementErrorArray[15] = "engineNum: Please enter the vehicles engine serial number, usualy located on the top or to the side of the engine blck itself.";
elementErrorArray[16] = "vinNum: Please enter the vehicle serial number (VIN), usualy located under the bonner and against the body of the car.";
elementErrorArray[17] = "purPrice: Please enter the purchased price of the vehicle.";
elementErrorArray[18] = "insPrice: Please enter the insured price of the vehicle or agreed market value in dollars.";
elementErrorArray[19] =	"purDate: Please enter the date the vehicle was purchased.";
elementErrorArray[20] =	"ncbIns: Please enter your No Claim Bonues Insurer' name.";
elementErrorArray[21] =	"ncbDueD: Please enter the due date of your No Claim Bonus.";
elementErrorArray[22] =	"ncbPolicy: Please enter the policy number of your No Claim Bonus.";
elementErrorArray[23] =	"mooredLoc: Please enter the moored location or permant address of the craft.";
elementErrorArray[24] =	"hullYear: Please enter the year of manufacture.";
elementErrorArray[25] =	"hullMake: Please enter the make of the hull.";
elementErrorArray[26] =	"hullModel: Please enter the model type of the hull.";
elementErrorArray[27] =	"hullLength: Please enter the length of the hull in metres.";
elementErrorArray[28] =	"hullRego: Please enter the hull registration number.";
elementErrorArray[29] =	"hullSerial: Please enter the serial number of the hull.";
elementErrorArray[30] =	"cost1boat: Please enter a total amount to be insured.";
elementErrorArray[31] =	"motorYear: Please enter the year of manufacture for the motor.";
elementErrorArray[32] =	"motorMake: Please enter the make of the motor.";
elementErrorArray[33] =	"motorHorse: Please enter the horsepower rating of the motor.";
elementErrorArray[34] =	"motorSerial: Please enter the serial number of the motor.";
elementErrorArray[35] =	"cost2boat: Please enter the total amount to be insured.";
elementErrorArray[36] =	"trailerYear: Please enter the trailer's year of manufacture.";
elementErrorArray[37] =	"trailerMake: Please enter the make of the trailer.";
elementErrorArray[38] =	"trailerRego: Please enter the trailer registration number.";
elementErrorArray[39] =	"cost3boat: Please enter the total amount to be insured.";
elementErrorArray[40] =	"preMedHis: Please enter any previous medical history you have (including broken bones).";
elementErrorArray[41] =	"waitPeriod: Please enter the number of days since you last saw a doctor.";
elementErrorArray[42] =	"benPeriod: Please enter the number of weeks since you last required medical benifits.";
elementErrorArray[43] =	"insContactNo: Please enter a contact number. Please include area code.";
elementErrorArray[44] =	"suburb: Please enter your suburb.";
elementErrorArray[45] =	"yrBuilt: When was the building built.";
elementErrorArray[46] =	"personalAdd: Please enter your personal address.";
elementErrorArray[47] =	"insOccupation: Please enter your occupation.";

function ErrorInfo(id, associd) {
 	this.ID = id;
	this.AssociatedID = associd; 
}

function generateErrorCount(id) {
	var i, count = 0;
	for(i = 0; i < id+1; i++) {
		if(((form.elements[i].type == "text")||(form.elements[i].type == "textarea")) && elementMandatory(form.elements[i].id)) {
			count++;
		}
	}
	return count++;
}

function generateErrorIds(id) {
	var ids;
	var count = generateErrorCount(arrayIndexOf(id));
	ids = new Array(("errorTitle"+(count+1)), ("error"+(count+1)));
	regenerateErrorIds(arrayIndexOf(id)+1, count, 2);
	return ids;
}

function regenerateErrorIds(id, count, inc) {
	var i = 0, maxCount, driverCount = 1, inc2 = 0;
	var ele;
	var testStr = "";
	maxCount = count;	
	for(i = id; i < form.elements.length; i++) {
		if(((form.elements[i].type == "text")||(form.elements[i].type == "textarea")) && elementMandatory(form.elements[i].id)) {
			ele = document.getElementById("errorTitle"+(maxCount+1));
			ele.id = "tempTitle"+(maxCount+1);
			ele = document.getElementById("error"+(maxCount+1));
			ele.id = "temperror"+(maxCount+1);
			maxCount++;
		}
		
	}
	maxCount = count;
	for(i = id; i < form.elements.length; i++) {
		if(((form.elements[i].type == "text")||(form.elements[i].type == "textarea")) && elementMandatory(form.elements[i].id)) {
			if(form.elements[i].name.substr(0,10) == "ADDITIONAL") {
					if(driverCount > 3) {
					inc2++;
					driverCount = 1;
				}
				updateDriverArray(inc2, (maxCount+inc));
				driverCount++;
			}			
			ele = document.getElementById("tempTitle"+(maxCount+1));
			ele.id = "errorTitle"+(maxCount+inc);
			ele = document.getElementById("temperror"+(maxCount+1));
			ele.id = "error"+(maxCount+inc);
			maxCount++;
		}
	}	
}

function arrayIndexOf(id) {
	var i = 0;
	for(i = 0; i < form.elements.length; i++) {
		if(form.elements[i].id == id) {
			return i;
		}
	}
	return -1;
}

function generateDriverErrorIds(id) {
	var i;
	var ids;
	var count = generateErrorCount(arrayIndexOf(id));
	ids = new Array(("errorTitle"+(count+1)), ("error"+(count+1)),("errorTitle"+(count+2)), ("error"+(count+2)),("errorTitle"+(count+3)), ("error"+(count+3)));
	regenerateErrorIds(arrayIndexOf(id)+1, count, 4);
	return ids;
}

function generateRadioIds() {
	var i, count = 0;
	var ids;
	for(i=0; i < form.elements.length; i++) {
		if(form.elements[i].type == "radio") {
			count++;
		}
	}
	ids = new Array(("addDriverSex"+(count+1)), ("addDriverSex"+(count+2)));
	return ids;
}

function advertErrors(errorArray) {
	var i;
	var errorHeader = document.getElementById("errorHeader");
	var errorDisplay = document.getElementById("errorDisplay");
	errorDisplay.innerHTML = "";
	for(i=0; i < errorArray.length ; i++) 
	{
		var errorText = document.getElementById(errorArray[i].AssociatedID).innerText;
		errorHeader.innerText = "The following fields have errors."
		errorDisplay.innerHTML += "<li>" + errorText + "</li>\n";
	}	
}

function elementMandatory(ele) {
	var i = 0;
	for(i = 0; i < nonManEle.length; i++) {
		if(nonManEle[i] == ele) {
			return false;
		}
	}
	return true;
}

function findElementErrorString(id) {
	var i = 0, delimIndex;
	var errorStr;
	for(i = 0; i < elementErrorArray.length; i++) {
		errorStr = elementErrorArray[i];
		delimIndex = errorStr.indexOf(":");
		errorStr = errorStr.substring(0, delimIndex);
		if(errorStr == id) {
			return elementErrorArray[i].substring(delimIndex+1, elementErrorArray[i].length);
		}
	}
	return "Error!, empty field.";
}

function checkErrors() {
	var errorIDArray = new Array();
	var errorImagePath = "commonimages/erroricon.jpg";
	var errorobj; 
	var verdict = true;	
	var errorBox;	
	var k = 0, i = 0;
	var formStr;	
	for(k=0; k < form.elements.length; k++) {
		switch(form.elements[k].type) {
			case("text"):
				if(elementMandatory(form.elements[k].id)) {
					if(form.elements[k].value.length == 0 ) {
						errorBox = document.getElementById("error" + (i+1));
						errorBox.innerHTML = "<img src=" + "\"" + errorImagePath + "\" alt=\""+findElementErrorString(form.elements[k].id)+"\">";
						errorobj = new ErrorInfo(("error" + (i+1)), ("errorTitle" + (i+1)));
						errorIDArray.push(errorobj);
						verdict = false;
						i++;
					}
					else  {
						i++;
					}
				}
				break;
			case("radio"):
				break;
			case("textarea"):
				if(elementMandatory(form.elements[k].id)) {
					if(form.elements[k].value.length == 0) {
						errorBox = document.getElementById("error" + (i+1));
						errorBox.innerHTML = "<img src=" + "\"" + errorImagePath + "\" alt=\""+findElementErrorString(form.elements[k].id)+"\">";
						errorobj = new ErrorInfo(("error" + (i+1)), ("errorTitle" + (i+1)));
						errorIDArray.push(errorobj);
						verdict = false;
						i++;
					}
					else {
						i++;
					}
				}
				break;							
			default: 
				break;
		}	
	}
	if(!verdict)
		alert("This form is incomplete, please see the top of page for more details.");
	advertErrors(errorIDArray);
	return verdict;
}

function setForm(f) {
	form = document.getElementById(f);
	//alert(form.name);
}
			
			