<!--
/*	-------------------------------------------------------------
	OUTRIGHTPLC
	e-commerce
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Copyright:		Tom Carrington <tom.carrington@outrightltd.com>
	Description:	Generic Scripts / Functions
	Filename:		Library.js
	Version:		0.5
	Date:			Nov 01, 2006
	-------------------------------------------------------------	
	FUNCTIONS:		$F()
					$()
					SelectSet()
					IsNumeric()
					hide()
					show()
					toggle()
	-------------------------------------------------------------
	FUNCTION:		$F()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	These functions returns the value of a form
					element (pass it the name attribute)
					
	Requires:		NAME
	-------------------------------------------------------------*/
	function $F(str_var){
		var elements = document.getElementsByName(str_var);
		var el = elements[0];
		switch(el.type) {
			case 'text':   				return el.value; break
			case 'hidden':   			return el.value; break
			case 'select-one':  		return el.options[el.selectedIndex].value; break
			case 'select-multiple':  	return el.options[el.selectedIndex].value; break
			case 'radio':  				return get_selected(elements); break  
		}
	}
	
	function get_selected(radio_group){
		selected_element = -1;
		for (i=radio_group.length-1; i > -1; i--) {
			if (radio_group[i].checked){selected_element = i;}
		}
		if (selected_element == -1) {
			return "none selected";
		}else{
			return radio_group[selected_element].value;
		}
	}
/*	-------------------------------------------------------------
	FUNCTION:		SelectSet()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function sets the value of a select box
					
	Requires:		Name, Value
	-------------------------------------------------------------*/	
	function SelectSet(SelectName, Value){
	var elements = document.getElementsByName(SelectName);
	var el = elements[0];
	
	for(index = 0; index < el.options.length; index++){
		if(el.options[index].value == Value)
			el.selectedIndex = index;
		}
	}
/*	-------------------------------------------------------------
	FUNCTION:		$()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function returns the form element 
					object.
					
	Requires:		ID
	-------------------------------------------------------------*/
	function $() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (arguments.length == 1)
				return element;
			elements.push(element);
		}
		return elements;
	}	
/*	-------------------------------------------------------------
	FUNCTION:		IsNumeric()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function returns true or false depending 
					if the passed number is numeric
	-------------------------------------------------------------*/
	function IsNumeric(sText){
		var ValidChars = "0123456789";
		var IsNumber=true;
		var Char;
		
		for (i = 0; i < sText.length && IsNumber == true; i++){ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1){
				IsNumber = false;
			}
		}
		return IsNumber;
	}
/*	-------------------------------------------------------------

	FUNCTION:		hide(), show(), toggle()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	These little functions hide or show 
					elements (pass the id attribute)
					
	Requires:		ID (Single || Array )
	-------------------------------------------------------------*/
	function hide(){
		for (var i = 0; i < arguments.length; i++) {
			var el = $(arguments[i]);
			el.style.display = 'none';
		}
	}
	
	function show(){
		for (var i = 0; i < arguments.length; i++) {
			var el = $(arguments[i]);
			el.style.display = '';
		}
	}
	
	function toggle() {
		for (var i = 0; i < arguments.length; i++) {
			var el = $(arguments[i]);
			if ( el.style.display != 'none' ) {
				el.style.display = 'none';
			}
			else {
				el.style.display = '';
			}
		}
	}
/*	-------------------------------------------------------------
	FUNCTION:		initDates()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function performs a smooth scroll 
					from the bottom to the top of the page.
	-------------------------------------------------------------*/
	function initDates() {
		if (!document.getElementsByTagName) {
			return;
		}
		var allfields = document.getElementsByTagName("input");
		var dateadded = false;

		for (var i = 0; i < allfields.length; i++) {
			var field = allfields[i];
			if (field.getAttribute("type") == "text" &&  field.getAttribute("maxlength") == 10 && field.name != "Proposer.id.1_DateOfBirth" && field.name != "Driver.id_DateOfBirth") {
					var link = document.createElement("a");
					link.id = field.name + ".link";
					link.href = "Javascript:calStartDate_.select('" + field.name + "','" + field.name + ".link','dd/MM/y'); //return false;";
					link.tabIndex = field.tabIndex + 1;
					link.title = "Click here to choose a date";
					link.alt = "Click here to choose a date";
					link.className = "datepicker";
					var image = document.createElement("img");
					image.src = "/resources/images/icon-calendar.gif";
					//image.className = "iconcalendar";
					image.alt = "Click here to choose a date";
					link.appendChild(image);
					if (field.nextSibling) {
						field.parentNode.insertBefore(link, field.nextSibling);
					} else {
						field.parentNode.appendChild(link);
					}
					dateadded = true;
			}
		}
		if (dateadded) {
			html = document.getElementById("width605").innerHTML;
			html = html + "<div id='calendar_' class='calendar_'></div>";
			document.getElementById("width605").innerHTML = html;
			
			script = document.createElement("script");
			script.type = "text/javascript";
			script.src = "/Resources/JS/calendar_init.js";
			document.getElementsByTagName("head")[0].appendChild(script);
		}
	}
/*	-------------------------------------------------------------
	FUNCTION:		initCancelReturnSubmit()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function stope a form from submitting 
					if  ENTER is pressed
	-------------------------------------------------------------*/	
	function initCancelReturnSubmit() {
		if (!document.getElementsByTagName){ return; }
		var allinputs = document.getElementsByTagName("input");
		// loop through all input tags and add events
		for (var i=0; i<allinputs.length; i++){
			var field = allinputs[i];
			if (field.type != "button"){			
				field.onkeypress = function (obj) {
					if (document.all){
						return event.keyCode!=13;	//IE
					}else{
						return obj.keyCode!=13;		//FIREFOX
					}
				}
			}
		}
	}
////////////////////////////////////////////////////////////////////////////////
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////////////////////////////////////////////////////////////////////
	function driver_Sex(driverTitle){
		var bln_driverM = false;
		var bln_driverF = false;
		var arr_male = new Array('Mr','Sir','Lord');
		var arr_femail = new Array('Mrs','Miss','Ms','Dame','Lady');
		var obj_QF;
		
		if ($("Proposer.id.1_Name.title") != null) {
			obj_QM = $("Proposer.id.1_SexM");
			obj_QF = $("Proposer.id.1_SexF");
		}
		else if ($("Driver.id_Name.title") != null) {
			obj_QM = $("Driver.id_SexM");
			obj_QF = $("Driver.id_SexF");
		}
		//else {
		//	sQuestion = document.xml_form.ProposerSex;
		//}
		
		if (driverTitle != "") {
			for (i=0;i<arr_male.length;i++) {
				if (driverTitle == arr_male[i]) {
					bln_driverM = true;
				}
			}
			for (j=0;j<arr_femail.length;j++) {
				if (driverTitle == arr_femail[j]) {
					bln_driverF = true;
				}
			}
			
			//obj_QM.checked = false;
			//obj_QF.checked = false;
			
			if (bln_driverM) { obj_QM.checked = true; }
			if (bln_driverF) { obj_QF.checked = true; }
		}
		
	}
////////////////////////////////////////////////////////////////////////////////
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////////////////////////////////////////////////////////////////////	
	function defaultOccupation(EmploymentStatusObject){
		EmploymentStatus = EmploymentStatusObject.value;
		EmploymentScope = EmploymentStatusObject.name.split("_")[1].substr(0,4);
		// test to see whether called from proposer or additional driver page
		// and setup name of form fields appropriately
		var insuredSex;
		var occupationSearchField;
		var occupationResultsField;
		var employerSearchField;
		var employerResultsField;
		if ($("Proposer.id.1_SexM") != null) 
		{
		    var alertMsgBoth = "Your occupation and employer's business have been automatically set to the most suitable for your employment status";
		    var alertMsgStudent = "Please select the most appropriate choice from the occupation selection (Your employer's business has been automatically set to the most suitable for your employment status)";
			if ($("Proposer.id.1_SexM").checked) {
				insuredSex = $("Proposer.id.1_SexM").value;
			}
			else {
				insuredSex = $("Proposer.id.1_SexF").value;
			}
			occupationSearchField = $("Proposer.id.1_" + EmploymentScope + "TimeOccupation.occDesc");
			occupationResultsField = $("Proposer.id.1_" + EmploymentScope + "TimeOccupation.occCode");
			
			employerSearchField = $("Proposer.id.1_" + EmploymentScope + "TimeOccupation.empDesc");
			employerResultsField = $("Proposer.id.1_" + EmploymentScope + "TimeOccupation.empCode");
		}else 
		{
			var alertMsgBoth = "Their occupation and employer's business have been automatically set to the most suitable for their employment status";
			var alertMsgStudent = "Please select the most appropriate choice from the occupation selection (Their employer's business has been automatically set to the most suitable for their employment status)";
			if ($("Driver.id_SexM").checked) {
				insuredSex = $("Driver.id_SexM").value;
			}else {
				insuredSex = $("Driver.id_SexF").value;
			}
			occupationSearchField = $("Driver.id_" + EmploymentScope + "TimeOccupation.occDesc");
			occupationResultsField = $("Driver.id_" + EmploymentScope + "TimeOccupation.occCode");
			
			employerSearchField = $("Driver.id_" + EmploymentScope + "TimeOccupation.empDesc");
			employerResultsField = $("Driver.id_" + EmploymentScope + "TimeOccupation.empCode");
		}

		switch (EmploymentStatus) {
			case "R":
				occupationSearchField.value = "Retired";
				occupationResultsField.options.length = 0;
				occupationResultsField.options[0] = new Option("Retired","R09");
				occupationResultsField.options[0].selected = true;
				
				employerSearchField.value = "Not In Employment";
				employerResultsField.options.length = 0;
				employerResultsField.options[0] = new Option("Not In Employment","186");
				employerResultsField.options[0].selected = true;
				alert(alertMsgBoth);
				break;
			case "FTE":
				occupationSearchField.value = "Student";
				occupationResultsField.options.length = 0;
				// CREATE AN OBJECT TO PASS TO AJAX REQUEST
				var theStatus = new Object;
				theStatus.keyCode = 0;
				
				if (EmploymentScope =="Part"){
					searchOccupationPt(theStatus);
				}else{
					searchOccupation(theStatus);
				}

				employerSearchField.value = "Not In Employment";
				employerResultsField.options.length = 0;
				employerResultsField.options[0] = new Option("Not In Employment","186");
				employerResultsField.options[0].selected = true;
				alert(alertMsgStudent);
				break;
			case "U":
				occupationSearchField.value = "Unemployed";
				occupationResultsField.options.length = 0;
				occupationResultsField.options[0] = new Option("Unemployed","U03");
				occupationResultsField.options[0].selected = true;
				
				employerSearchField.value = "Not In Employment";
				employerResultsField.options.length = 0;
				employerResultsField.options[0] = new Option("Not In Employment","186");
				employerResultsField.options[0].selected = true;
				alert(alertMsgBoth);
				break;
			case "H":
				if (insuredSex == "M") {
					occupationSearchField.value = "Househusband";
					occupationResultsField.options.length = 0;
					occupationResultsField.options[0] = new Option("Househusband","163");
					occupationResultsField.options[0].selected = true;
				}else {
					occupationSearchField.value = "Housewife";
					occupationResultsField.options.length = 0;
					occupationResultsField.options[0] = new Option("Housewife","H09");
					occupationResultsField.options[0].selected = true;
				}
				
				employerSearchField.value = "Not In Employment";
				employerResultsField.options.length = 0;
				employerResultsField.options[0] = new Option("Not In Employment","186");
				employerResultsField.options[0].selected = true;
				alert(alertMsgBoth);
				break;
			default: return false;
		}
	}
