<!--
/*	-------------------------------------------------------------
	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()
					toTop()
					initRows()
					initHighlight()
					initErrors()
	-------------------------------------------------------------
	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:		initRows()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function performs a smooth scroll 
					from the bottom to the top of the page.
	-------------------------------------------------------------*/
	function initRows2() {
		var even = false;
		var evenColor = "#F2F2F2";
		var oddColor = "#Fafafa";
		var divs = document.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++) {
			//if (divs[i].className == "row" ){
			if (divs[i].className == "q_row" || divs[i].className == "full_row" || divs[i].className == "DecDiscq_row" || divs[i].className == "policydocs"){
				//	COLOUR DIVS
				divs[i].style.backgroundColor = even ? evenColor : oddColor;
				even =  ! even;
			}
		}
		
		initDates();
	}
	function initRows() {
		var even = false;
		var altname = " q_alt";
		var divs = document.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++) {
			if ((divs[i].className == "q_row" || divs[i].className == "full_row" ) && (divs[i].style.display != 'none') ){
				divs[i].className = even ? divs[i].className + altname : divs[i].className;
				even = ! even;
			}
		}
		initDates();
	}
/*	-------------------------------------------------------------
	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_DateOfBirth" && field.name != "JointProposer_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/spacer.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('Content').innerHTML;
			html = html + "<div id='calendar_' class='calendar_'></div>";
			document.getElementById('Content').innerHTML = html;
			
			script = document.createElement('script');
			script.type = 'text/javascript';
			script.src = '/_resources/javascript/calendar_init.js';
			document.getElementsByTagName('head')[0].appendChild(script);
		}
	}
/*	-------------------------------------------------------------
	FUNCTION:		initHighlight()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function adds onfocus/onblur methods to 
					all input/select objects adding classes of
					ipt & iptf
	-------------------------------------------------------------*/
	function initHighlight() {
		if (!document.getElementsByTagName){ return; }
		var allfields = document.getElementsByTagName("input");
		var allselects = document.getElementsByTagName("select");
		var alltextareas = document.getElementsByTagName("textarea");
	
		// loop through all input tags and add events
		for (var i=0; i<allfields.length; i++){
			var field = allfields[i];
			if ((field.getAttribute("type") == "text") || (field.getAttribute("type") == "password") ) {
				
				if (!field.getAttribute("onfocus")){
					field.onfocus = function () {this.className = 'iptf';}
				}
				if (!field.getAttribute("onBlur")){
					field.onblur = function () {this.className = 'ipt';}
				}
			}
		}
		
		// loop through all select tags and add events
		/*for (var i=0; i<allselects.length; i++){
			var field = allselects[i];
			field.onfocus = function () {this.className = 'iptf';}
			field.onblur = function () {this.className = 'ipt';}
		}*/
		
		// loop through all select tags and add events
		for (var i=0; i<alltextareas.length; i++){
			var field = alltextareas[i];
			field.onfocus = function () {this.className = 'iptf';}
			field.onblur = function () {this.className = 'ipt';}
		}
	}
/*	-------------------------------------------------------------
	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 != "image"){			
				field.onkeypress = function (obj) {
					if (document.all){
						return event.keyCode!=13;	//IE
					}else{
						return obj.keyCode!=13;		//FIREFOX
					}
				}
			}
		}
	}
/*	-------------------------------------------------------------
	FUNCTION:		initErrors(), get_previoussibling(), 
					get_firstChild()
	+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 
	Description:	This function adds a class to inputs with 
					errors & the parent row
	-------------------------------------------------------------*/
	function initErrors(){
		var arr_span = document.getElementsByTagName("span");
		for (var i = 0; i < arr_span.length; i++) {
			if (arr_span[i].className == "error" && arr_span[i].innerHTML != "") {
				arr_span[i].style.display = "block";
				obj_Q_row = arr_span[i].parentNode;
				obj_Q_row.className = obj_Q_row.className.replace(/alt/g,"");
				//obj_Q_row.style.backgroundColor = "";
				//obj_Q_row.style.backgroundColor = "#FFFFDA !important";
				obj_Q_row.className = obj_Q_row.className + ' q_error';
				
				//	THE VALUE FIELD IS THE SECOND CHILD OF THE ROW DIV & THE INPUT IS IT'S FIRST CHILD
				/*obj_valueDiv =  get_secondChild(obj_Q_row)
				obj_input = get_firstChild(obj_valueDiv)
				if (obj_input.type == "text" || obj_input.type == "select-one" || obj_input.type == "textarea"){
					obj_input.onfocus = function () {};
					obj_input.onblur = function () {};
					obj_input.className = "inputHasErrors";
				}
				obj_secondValueDiv = get_nextsibling(obj_valueDiv)
				if (obj_secondValueDiv.className == "value"){
					obj_secondInput = get_firstChild(obj_secondValueDiv)
					if (obj_secondInput.type == "text" || obj_secondInput.type == "select-one" || obj_secondInput.type == "textarea"){
						obj_secondInput.onfocus = function () {};
						obj_secondInput.onblur = function () {};
						obj_secondInput.className = "inputHasErrors";
					}
				}*/
			}
		}
	}
	
	// Two functions for Firefox's 
	// miss-handling of whitespace in the DOM
	function get_previoussibling(n){
		var x=n.previousSibling;
		while (x.nodeType!=1){
			x = x.previousSibling;
		}
		return x;
	}
	
	function get_firstChild(n){
		for (i=0;i<n.childNodes.length;i++){
			if(n.childNodes[i].nodeType==1){
				return n.childNodes[i];
			}

		}
	}
	
	function get_nextsibling(n){
		var x=n.nextSibling;
		if(!x){return false;}
		while (x.nodeType!=1){
			x = x.nextSibling;
			if(!x){return false;}
		}
		return x;
	}
	
	function get_secondChild(n){
		firstchild = 0
		for (i=0;i<n.childNodes.length;i++){
			if(n.childNodes[i].nodeType==1 & firstchild == 0){
				firstchild = 1
			}else if(n.childNodes[i].nodeType==1 & firstchild == 1){
				return n.childNodes[i];
			}
		}
	}
	
	function isElement(n){
		if (n.nodeType==1){
			return n;
		}else{
			return false;
		}
	}
	
	
////////////////////////////////////////////////////////////////////////////////
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////////////////////////////////////////////////////////////////////
	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_Name.title") != null) {
			obj_QM = $("Proposer_SexM");
			obj_QF = $("Proposer_SexF");
		}
		else if ($("JointProposer_Name.title") != null) {
			obj_QM = $("JointProposer_SexM");
			obj_QF = $("JointProposer_SexF");
		}
		else {
			return;
		}
		//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;
		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_SexM") != null) {
			if ($("Proposer_SexM").checked) {
				insuredSex = $("Proposer_SexM").value;
			}
			else {
				insuredSex = $("Proposer_SexF").value;
			}
			
			occupationSearchField = $("Proposer_" + EmploymentScope + "TimeOccupation.occDesc");
			occupationResultsField = $("Proposer_" + EmploymentScope + "TimeOccupation.occCode");
			
			employerSearchField = $("Proposer_" + EmploymentScope + "TimeOccupation.empDesc");
			employerResultsField = $("Proposer_" + 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 ($("JointProposer_SexM").checked) {
				insuredSex = $("JointProposer_SexM").value;
			}else {
				insuredSex = $("JointProposer_SexF").value;
			}
			
			occupationSearchField = $("JointProposer_" + EmploymentScope + "TimeOccupation.occDesc");
			occupationResultsField = $("JointProposer_" + EmploymentScope + "TimeOccupation.occCode");
			
			employerSearchField = $("JointProposer_" + EmploymentScope + "TimeOccupation.empDesc");
			employerResultsField = $("JointProposer_" + 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;
				FTE
			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;
		}
	}
function showGlossary(gSection) {
	if (gSection == null) {
		gSection = '';
	}
	var wGlossary = window.open('/shared/glossary.asp?glossid='+gSection+'&q=HC','popup','scrollbars=yes,resizable=yes,location=no,width=630,height=350');
}