// Function that creates a highlite around text fields and input boxes

window.onload= init;

function init() {
	insertElement();
	addOnChange();
	toggleScheduledTime();
	selectFormElements();
	addOnSubmit();
	addPopup();
}

function FormHiLite(objElement)
{
  if (objElement.className=='normal')
    objElement.className='focus';
  else
    objElement.className='normal';
}

// Scripts for Form Logic
	var dom = (document.getElementById)?true:false;
		
// Show Hide Functions
	function HideDiv(id){
	if(dom){
		document.getElementById(id).className="invisible";
	}	
	}
		
	function ShowDiv(id){
	if(dom){
		document.getElementById(id).className="visible";
		
	}
	}
	
function initiate_Details_Hidden()
{
	e= document.getElementById('change_conditions');
	e.className="invisible";
}
	
	
// Taxi Postcode ON / OFF
	function shPostcode()
		{
		if	(document.quoteOne.badge.selectedIndex<2)
			{
			HideDiv('operatingPostcode');
			}
		else 
			{
			ShowDiv('operatingPostcode');
			}
		}
		

// ****************************************
// FORM CHECKER VERSION 1.1
// Released: 26 August 2004
// Author: Ted Howe
// Company: Fortune Cookie
// ****************************************


	var dom = (document.getElementById)?true:false;
	
	var submittable;
	var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var postcode = /^[a-zA-Z]{1,2}[a-zA-Z0-9]{1,2}[ ]?[0-9]{1}[a-zA-Z]{2}$/;
	var phoneNums = /^[0-9 \+\(\)]{8,}$/;
	var yearNums = /[1,2][9,0][0-9]{2}/;
	var nums = /[0-9]{1,}/
	var errorlist;
	var errorlistcount = 0;

// CLEAR FIELD
function clearField(elmnt,defaultVal){
elmnt.value="";
}
	

// CHECK NORMAL INPUT FIELDS
function chkText(elmnt,msg,defaultVal){
	elmntName = elmnt.name;
	
	
	if(elmnt.value=="" || elmnt.value==defaultVal){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}
}


// CHECK EMAIL FIELDS
function chkEmail(elmnt,msg){
	elmntName = elmnt.name;
	text = elmnt.value;
	result = email.test(text);
	if(result==false){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}			
}


// CHECK POSTCODES
function chkPostcode(elmnt,msg){
	elmntName = elmnt.name;
	text = elmnt.value;
	result = postcode.test(text);
	if(result==false){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}			
}


// CHECK NUMBER-ONLY FIELDS
function chkNums(elmnt,msg){
	elmntName = elmnt.name;
	text = elmnt.value;
	result = nums.test(text);
	if(result==false){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}			
}



// CHECK PHONE NUMBER-ONLY FIELDS
function chkPhoneNums(elmnt,msg){
	elmntName = elmnt.name;
	text = elmnt.value;
	result = phoneNums.test(text);
	if(result==false){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}			
}


// CHECK NORMAL SELECT LISTS
function chkSelect(elmnt,msg){

	elmntName = elmnt.name;
	if(elmnt.options[elmnt.selectedIndex].value==""){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}	
}


// CHECK RADIO BUTTONS
function chkRadio(elmnt,msg){
	elmntName = elmnt[0].name;
	elmntLength = elmnt.length;
	falseCheck = 0;

	for(i=0;i<elmntLength;i++){
		if(elmnt[i].checked==false){
		falseCheck++
		}
	}
	
	if(falseCheck==elmntLength){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt[0]);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}	
	
	
}


// CHECK TEXTAREAS
function chkTextArea(elmnt,msg){
	elmntName = elmnt.name;
	
	if(elmnt.innerHTML==""){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}
}






// CHECK DATE OF BIRTH
function chkDob(elmntDay,elmntMonth,elmntYear,msg,warn){
	elmntNameDay = elmntDay.name;
	elmntNameMonth = elmntMonth.name;
	elmntNameYear = elmntYear.name;
	
	text = elmntYear.value;
	result = yearNums.test(text);
	
	currentDate = new Date();
	currentYear = currentDate.getFullYear();
	
	if(text>currentYear){
	result=false
	}
	
	if(elmntYear.value=="" || elmntYear.value=="19--" || result==false || elmntDay.value=="" || elmntMonth.value==""){
	submittable=false;
	document.getElementById(warn+"_warn").className="warn";
	document.getElementById(warn+"_warn").innerHTML=msg;
	errorList(elmntDay);
	}
	else{
	
		if(elmntYear.value/4 == parseInt(elmntYear.value/4)){leapyear=true;}
		else{leapyear=false}
	

		if(elmntMonth.value=="April" || elmntMonth.value=="June" || elmntMonth.value=="September" || elmntMonth.value=="November"){
			if(elmntDay.value=="31"){
			document.getElementById(warn+"_warn").className="warn";
			document.getElementById(warn+"_warn").innerHTML=msg;
			errorList(elmntDay);
			}
			else{
			document.getElementById(warn+"_warn").innerHTML="";
			document.getElementById(warn+"_warn").className="warnEmpty";
			}
		}
		else if (elmntMonth.value=="February"){
			if(leapyear==false){
				if(elmntDay.value=="29" || elmntDay.value=="30" || elmntDay.value=="31"){
				document.getElementById(warn+"_warn").className="warn";
				document.getElementById(warn+"_warn").innerHTML=msg;
				errorList(elmntDay);
				}
				else{
				document.getElementById(warn+"_warn").innerHTML="";
				document.getElementById(warn+"_warn").className="warnEmpty";
				}		
			}
			else{
				if(elmntDay.value=="30" || elmntDay.value=="31"){
				document.getElementById(warn+"_warn").className="warn";
				document.getElementById(warn+"_warn").innerHTML=msg;
				errorList(elmntDay);
				}
				else{
				document.getElementById(warn+"_warn").innerHTML="";
				document.getElementById(warn+"_warn").className="warnEmpty";
				}	
			}		
		}
		else{
		document.getElementById(warn+"_warn").innerHTML="";
		document.getElementById(warn+"_warn").className="warnEmpty";
		}
	
	}


}



// CHECK OTHER TITLE FIELD IF TITLE FIELD IS EMPTY
function chkOtherTitle(elmnt,titleElmnt,msg,defaultVal){
	elmntName = elmnt.name;
	
	if((elmnt.value=="" || elmnt.value==defaultVal) && titleElmnt.value=="Other"){
		submittable=false;
		document.getElementById(elmntName+"_warn").className="warn";
		document.getElementById(elmntName+"_warn").innerHTML=msg;
		errorList(elmnt);
	}
	else{
		document.getElementById(elmntName+"_warn").innerHTML="";
		document.getElementById(elmntName+"_warn").className="warnEmpty";
	}
}




// CHECK DATES FOR CARDS
function chkCardDate(type,elmntMonth,elmntYear,msg,warn){
	elmntMonthName = elmntMonth.name;
	elmntYearName = elmntYear.name;
	
	if(elmntMonth.options[elmntMonth.selectedIndex].value=="" || elmntYear.options[elmntYear.selectedIndex].value==""){
		submittable=false;
		document.getElementById(warn+"_warn").className="warn";
		document.getElementById(warn+"_warn").innerHTML=msg;
		errorList(elmntMonth);
	}
	else{
	
		if(type=="expiry"){
		
			currentDate = new Date();
			currentMonth = currentDate.getMonth();
			currentYear = currentDate.getFullYear();
			currentMonth++;
			
			if(elmntMonth.options[elmntMonth.selectedIndex].value<currentMonth && elmntYear.options[elmntYear.selectedIndex].value==currentYear){
				submittable=false;
				document.getElementById(warn+"_warn").className="warn";
				document.getElementById(warn+"_warn").innerHTML=msg;
				errorList(elmntMonth);
			}
			else{
				document.getElementById(warn+"_warn").innerHTML="";
				document.getElementById(warn+"_warn").className="warnEmpty";
			}		
		
		}
		else if(type=="start"){
		
			currentDate = new Date();
			currentMonth = currentDate.getMonth();
			currentYear = currentDate.getFullYear();
			currentMonth++;
			
			if(elmntMonth.options[elmntMonth.selectedIndex].value>currentMonth && elmntYear.options[elmntYear.selectedIndex].value==currentYear){
				submittable=false;
				document.getElementById(warn+"_warn").className="warn";
				document.getElementById(warn+"_warn").innerHTML=msg;
				errorList(elmntMonth);
			}
			else{
				if(elmntYear.options[elmntYear.selectedIndex].value>currentYear){
				submittable=false;
				document.getElementById(warn+"_warn").className="warn";
				document.getElementById(warn+"_warn").innerHTML=msg;
				errorList(elmntMonth);
				}
				else{
				document.getElementById(warn+"_warn").innerHTML="";
				document.getElementById(warn+"_warn").className="warnEmpty";
				}
			}	
		}
	}	
}




function chkSwitchCards(elmnt,startMonth,startYear,warn,issueNo){
		if(elmnt.options[elmnt.selectedIndex].value!=""){
		var cardTypeVal = elmnt.options[elmnt.selectedIndex].value;
			if(cardTypeVal=="Switch" || cardTypeVal=="Solo" || cardTypeVal=="Electron"){
			chkCardDate('start',startMonth,startYear,'Please enter a valid Start Date','card_start');
			chkNums(issueNo,'Please enter an Issue Number');
			}
			else{
			issueNoName = issueNo.name;
			document.getElementById(warn+"_warn").innerHTML="";
			document.getElementById(warn+"_warn").className="warnEmpty";
			errorList(startMonth);
			document.getElementById(issueNoName+"_warn").innerHTML="";
			document.getElementById(issueNoName+"_warn").className="warnEmpty";
			errorList(issueNo);
			}
		}
}




function errorList(elmnt){
	errorlist[errorlistcount]=elmnt;
	errorlistcount++;
}


function chkFocus(){
	//alert(errorlist.length);
	focuser = errorlist[0];
	focuser.focus(); 
	
	
}

function popupWindow(win,winWidth,winHeight,winName)
{
	leftPos = 0; topPos = 0;
	
	if (screen){ 
	
	  	if (winWidth>screen.width){
   		   winWidth = screen.width;
   		}
   		
   		if (winHeight>screen.height){
   		   winHeight = screen.height - 55;
   		}
   		
   		if (winWidth <= 0){
   		   winWidth = screen.width;
   		}
   		
   		if (winHeight <= 0){
   		   winHeight = screen.height - 55;
   		}
   		
		leftPos = screen.width/2 - winWidth/2;
		topPos = screen.height/2 - winHeight/2;
		
							
	}
	
	newWindow = window.open(win,winName,'toolbar=no,location=no,scrollbars=yes,resizable=yes,width='+winWidth+',height='+winHeight+',left='+leftPos+',top='+topPos+'');
	newWindow.focus();
}

//added by AK 10/04/2008
//more accessible popup code for "manage account" section, when "summary" is clicked
//the summary anchor has a rel attribute of popup

function addPopup() {
	
	var popup= document.getElementsByTagName("a");
	
	for (i=0;i<popup.length;i++)
	{
		if (popup[i].rel == "popup" || popup[i].rel == "popup-small")
		{
			popup[i].onclick = popupWin;
		}
	}
}

function popupWin() {
	var newWindow;
	var left = (screen.width-800) / 2;
	var top;
	
	if (this.rel == "popup")
	{
		top= (screen.height-800)/2;
		newWindow= window.open(this.href, 'newWin', 'scrollbars = yes, height=800, width=800, left=' + left + ', top=' + top);
	}
	else if (this.rel == "popup-small")
	{
		top= (screen.height-200)/2;
		newWindow= window.open(this.href, 'newWin', 'scrollbars = yes, height=200, width=800, left=' + left + ', top=' + top);
	}

	newWindow.focus();
	return false;
}


/*------------ADDED 19/09/06 ------------------*/



function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
	document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
	if (theValue == true || theValue == false)
	{
		if (theValue!='')
		{
			eval("obj."+theProp+"="+theValue);
		}
	}
	else
	{
		eval("obj."+theProp+"='"+theValue+"'");
	}
  }
}


function MM_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function populateUsage2(type){
	// Clear the usage field
	populateUsage();
	var usage='';

	MM_findObj('terms_special').value ='';
	
	usage=usage + fixUsage('d', 'D', '\n');
	usage=usage + fixUsage('clv', 'CLV', '\n');
	usage=usage + fixUsage('dmt', 'DMT', '\n');
	usage=usage + fixUsage('mc', 'MC', '\n');
	usage=usage + fixUsage('ws', 'Windscreen Cover up to £500 with £75 excess in any one period.', '\n');
	
	usage=usage + fixUsage('s2', 'S2', '\n');
	usage=usage + fixUsage('s5a', 'Public Liability up to £2M subject to a £500 excess', '\n');
	usage=usage + fixUsage('s5b', 'Public Liability up to £5M subject to a £500 excess', '\n');
	usage=usage + fixUsage('s5d', 'Public Liability up to £10M subject to a £500 excess', '\n');
		
	usage=usage + fixUsage('s6', 'S6', '\n');
	usage=usage + fixUsage('s7', 'S7', '\n');

	usage=usage + fixUsage('s8_s9', 'S8/9', '\n');
	usage=usage + fixUsage('s10', 'S10', '\n');
	usage=usage + fixUsage('s11', 'S11', '\n');
	usage=usage + fixUsage('s12', 'S12', '\n');
	usage=usage + fixUsage('s13', 'S13', '\n');
	
	if (type=='30_day_taxi')
	{
		usage=usage + fixUsage('s14', 'Legal Expenses', '\n');
		usage=usage + '\nNo return of premiums are allowed on 30 day taxi policies'
	}
	else 
	{
		// usage=usage + fixUsage('s14', 'Legal Expenses supplied by DAS Legal Expenses Ins.co. Ltd.', '\n');
		usage=usage + fixUsage('s14', 'Legal Expenses', '\n');	
	}
	
	MM_findObj('terms_special').value = usage.substring(1)
	// Populate the usage field
	//MM_changeProp('terms_special','','value',usage,'TEXT')
}

function populateUsage(){
	// Clear the usage field
	MM_findObj('specials').value='';
	
	// Build the usage field contents based on radio selections
	var usage='';
	var spacer='  ';
	usage=usage + fixUsage('d', 'D', spacer);
	usage=usage + fixUsage('clv', 'CLV', spacer);
	usage=usage + fixUsage('dmt', 'DMT', spacer);
	usage=usage + fixUsage('mc', 'MC', spacer);
	
	usage=usage + fixUsage('s2', 'S2', spacer);
	usage=usage + fixUsage('s5a', 'S5', spacer);
	usage=usage + fixUsage('s5b', 'S5', spacer);
	usage=usage + fixUsage('s5d', 'S5', spacer);
	
	usage=usage + fixUsage('s6', 'S6', spacer);
	usage=usage + fixUsage('s7', 'S7', spacer);

	usage=usage + fixUsage('s8_s9', 'S8/9', spacer);
	usage=usage + fixUsage('s10', 'S10', spacer);
	usage=usage + fixUsage('s11', 'S11', spacer);
	usage=usage + fixUsage('s12', 'S12', spacer);
	usage=usage + fixUsage('s13', 'S13', spacer);
	usage=usage + fixUsage('s14', 'S14', spacer);
	
	usage=usage + fixUsage('ws', 'WS', spacer);
	
	// Populate the usage field
	MM_changeProp('specials','','value',usage.substring(2),'TEXT')
}

function fixUsage(name, value, spacer)
{
	var usage='';
	
	x=MM_findObj('usage_'+name+'_yes');
	if (x!=null)
	{
		if(x.checked){
			usage=spacer+value;
		}
	}
	
	return usage;
}
	


//NEW INSTRUCTION OPTIONAL FIELDS

function hideLyrs(value){

	var opt = document.getElementById("type_"+value);
	if(opt.options[opt.selectedIndex].value!="commercial"){
	document.getElementById("field_gvw_"+value).className = "contentbox-small hidden";
	}
	else{
	document.getElementById("field_cc_"+value).className = "contentbox-small hidden";
	}
}

/*function updateFields(opt, value){
	var val = opt.options[opt.selectedIndex].value;
	if(val=="commercial"){
		document.getElementById("field_cc_"+value).style.display = "none";
		document.getElementById("field_gvw_"+value).style.display = "block";
	}
	else{
		document.getElementById("field_cc_"+value).style.display = "block";
		document.getElementById("field_gvw_"+value).style.display = "none";
	}
}
*/
function updateFields(opt, value){
	var val = opt.options[opt.selectedIndex].value;
	if(val=="commercial"){
		document.getElementById("field_cc_"+value).className = "contentbox-small hidden";
		document.getElementById("field_gvw_"+value).className = "contentbox-small";
	}
	else{
		document.getElementById("field_cc_"+value).className = "contentbox-small";
		document.getElementById("field_gvw_"+value).className = "contentbox-small hidden";
	}
}


function print_document()
{
	window.print();
	return false;
}


function radio_DHTMLDisplayIfTrue(rad, trueValue, divID)	{
	//ShowDiv+HideDiv do not work as visible class puts element on new line
	document.getElementById(divID + "_element").style.display=(rad.value==trueValue?"":"none");
	document.getElementById(divID + "_label").style.display=(rad.value==trueValue?"":"none");
}


function checkOnCoverTaxiType(opt)
{
	var val = opt.options[opt.selectedIndex].value;
	
	if (val=='publichirelondon')
	{
		document.getElementById("operating_postcode").value = "LBC";
	}
}


function getBase(e)
//the base rate has been selected by the user
{
	var ratingMonth = document.getElementById("rating_month");
	var baseArray = new Array();
	
	baseArray[0]= 0;
	baseArray[1]= 159;
	baseArray[2]= 156;
	baseArray[3]= 153;
	baseArray[4]= 150;
	baseArray[5]= 147;
	baseArray[6]= 144;
	baseArray[7]= 141;
	baseArray[8]= 138;
	baseArray[9]= 135;
	baseArray[10]= 132;
	baseArray[11]= 129;
	baseArray[12]= 126;
	baseArray[13]= 123;
	baseArray[14]= 120;
	baseArray[15]= 117;
	baseArray[16]= 114;
	baseArray[17]= 111;
	baseArray[18]= 108;
	baseArray[19]= 105;
	baseArray[20]= 102;
	baseArray[21]= 99;
	baseArray[22]= 96;
	baseArray[23]= 93;
	baseArray[24]= 90;
	
	return e.value = baseArray[ratingMonth.selectedIndex];
}

function numberCheck (el) {
	return (isNaN(el)) ? 0 : Number(el);
}


function calc30DayTaxi(element)
{
	//This function calculates values for 3 fields(total, ipt and total before ipt) in calcultor box on the 30 day taxi premium calculation page. The argument it receives is the form element that has just changed
	
	//get the form elements
	var baseRate = document.getElementById("base_rate_1");
	var ipt = document.getElementById("ipt");
	var hidden_ipt = document.getElementById("hidden_ipt");
	var total_less_ipt = document.getElementById("total_less_ipt");
	var hidden_total_less_ipt = document.getElementById("hidden_total_less_ipt");
	var total = document.getElementById("total");
	var hidden_total = document.getElementById("hidden_total");

	var iptRate= 0.05;
	var adjustedTotal; //used for the variable to store the two decimal places total
	var adjustedIPT;   //used for the variable to store the two decimal places ipt
	var adjustedPremium;
	
	//run through each element in the form, ensuring each value is a number, and add them together
	var runningTotal = 0;
	
	if (element && element.getAttribute("id") == "rating_month")
	//if the element that has been changed is the rating month select, then that is base value needs to be set
	{
		baseRate.value= getBase(baseRate);
	}
	
	runningTotal += numberCheck(baseRate.value);
	runningTotal += numberCheck(document.getElementById("age_load").value);
	runningTotal += numberCheck(document.getElementById("licence_load").value);
	runningTotal += numberCheck(document.getElementById("fault_claim_1").value);
	runningTotal += numberCheck(document.getElementById("fault_claim_2").value);
	runningTotal += numberCheck(document.getElementById("minor_conviction").value);

	runningTotal += numberCheck(document.getElementById("liability").value);

	runningTotal += numberCheck(document.getElementById("seat_load").value);
	runningTotal += numberCheck(document.getElementById("fee").value);
	
	//calculate the total, converting to two decimal places
	adjustedTotal = twoDec(runningTotal);
	
	if (adjustedTotal!=0)
	{
		total.value = adjustedTotal;
		hidden_total.value = adjustedTotal;
		
		//set the ipt, converting to two decimal places
		adjustedIPT = twoDec(adjustedTotal*iptRate);
		
		ipt.value = adjustedIPT;
		hidden_ipt.value = adjustedIPT;
		
		//calculate the total less ipt value, converting to two decimal places
		adjustedPremium = twoDec(adjustedTotal - adjustedIPT);
		
		total_less_ipt.value = adjustedPremium;
		hidden_total_less_ipt.value = adjustedPremium;
	}
	else
	{
		total.value = "";
		hidden_total.value = "";
		ipt.value = "";
		hidden_ipt.value = "";
		total_less_ipt.value = "";
		hidden_total_less_ipt.value = "";
	}
}


function twoDec(num)
{
	orig = num;
	flr = Math.floor(num);
	n = orig-flr;
	n = Math.round(n*100)/100;
	n = flr+n;
	return n;
}


function showMinorCon30Day(elmnt){

	
	var convictions = new Array();
	am = elmnt.options[elmnt.selectedIndex].value
	
	convictions[0] = "MinorCon1";
	convictions[1] = "MinorCon2";

	for(i=0;i<convictions.length;i++){document.getElementById(convictions[i]).className = "cols invisible";}
	
	if (am<3)
	{
	    for(i=0;i<am;i++){document.getElementById(convictions[i]).className = "cols";}
	}

}

function showMajorCon30Day(elmnt){

	
	var convictions = new Array();
	am = elmnt.options[elmnt.selectedIndex].value
	
	convictions[0] = "MajorCon1";

	for(i=0;i<convictions.length;i++){document.getElementById(convictions[i]).className = "cols invisible";}
	
	if (am<2)
	{
		for(i=0;i<am;i++){document.getElementById(convictions[i]).className = "cols";}
	}

}

function showOtherFacts(elmnt)
{
    if (elmnt.checked && elmnt.value=="yes")
    {
        document.getElementById("otherfacts_textbox").style.display="block";
    }
    else
    {
        document.getElementById("otherfacts_textbox").style.display="none";
    }
}    

function selectJump(elmnt){elmnt.selectedIndex=0;}

function toggleLayer( whichLayer,state )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  
  var divs = document.getElementsByTagName("div");
  
  for(i=0;i<divs.length;i++){
  	if(divs[i].className=="helptext8" || divs[i].className=="helptext8 fixpos help-alt" || divs[i].className=="helptext8 fixpos help-alt2"){divs[i].style.display = "none";}
  }
  
  if(state=="off"){vis.display="none";}
  else{vis.display="block";}
  
  /*if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';*/
  
  /*AK 20070904
  	New Instruction for Taxi, Step 1.
	When clicking "Quoted By Tradex" link in top right corner, the help text appears behind the "Type of taxi" select in IE.
	This script hides the select when displaying the helptext and unhides the select when closing the help text 
  */
  
  if (whichLayer === 'qbt')
  {
	  taxi_type_select= document.getElementById("occupation");
	  
	  if (taxi_type_select.className == "normal fixed-width") {
		  taxi_type_select.className = "normal remove";
	  }
	  else {
		  taxi_type_select.className = "normal fixed-width";
	  }
  }
}

function checkTBA(frm, chk, itm)	{
	var val = (chk.checked?'TBA':'');
	frm.elements['taxi_plate_no_' + itm].value=val ; 
	frm.elements['taxi_plate_expiry_' + itm].value=val ;
}


function checkCoverTypeV1(chk,arrComp,arrTPFT,arrTPO,QuoteType)	{
	//1= motortrade
	//2= taxi 
	//3= taxi monthly
	var ex = chk.form.elements['excess'];
	var tpoText = (QuoteType!=1?'<br/>TP excess will be waivered if the incident is reported within 7 days':'');
	for(var i=0;i<ex.options.length;i++)	{
		ex.options[i]=null;
	}
	//do it twice for our ie friend because he simtimes misses an option
	for(var i=0;i<ex.options.length;i++)	{
		ex.options[i]=null;
	}

	var arr=null;
	
	switch(chk.value)	{
		case 'cover_tpo':
			arr=arrTPO; 
			break;
		case 'cover_tpft':
			arr=arrTPFT; 
			break;
		default:
			arr=arrComp; 
			break;
	}
	
	for(var i=0;i<arr.length;i++)	{
		var o = new Option(arr[i],arr[i]);
		ex.options[i] = o;
	}
	switch(QuoteType)	{
		case 1:
			document.getElementById("lblexcess_type").innerHTML = 'ADF &amp; T' + (chk.value=='cover_tpo'?'':'<br/> or 10% of the claim value, under section 2A/B/C whichever is greater.');
		break;
		case 2:
			chk.form.elements['excess_type'].value = 'adft';	
			//document.getElementById("lblexcess_type").innerHTML = (chk.value=='cover_tpo'?'All Sections'+tpoText:'ADF &amp; T');
		break;
		case 3:
			chk.form.elements['excess_type'].value = (chk.value=='cover_tpo'?'all':'adft');	
			document.getElementById("lblexcess_type").innerHTML = (chk.value=='cover_tpo'?'All Sections'+tpoText:'ADF &amp; T');
		break;
		case 4:
			//chk.form.elements['excess_type'].value = (chk.value=='cover_tpo'?'all':'adft');	
			//document.getElementById("lblexcess_type").innerHTML = (chk.value=='cover_tpo'?'All Sections'+tpoText:'ADF &amp; T');
		break;
		default:
		//do nothing
		break;
	}
}

function checkCoverTypeSpecifiedVehicle(chk,arrComp,arrTPFT,arrTPO, defaultValue)	{
	var ex = chk.form.elements['excess'];
	for(var i=0;i<ex.options.length;i++)	{
		ex.options[i]=null;
	}
	//do it twice for our ie friend because he simtimes misses an option
	for(var i=0;i<ex.options.length;i++)	{
		ex.options[i]=null;
	}

	var arr=null;
	
	switch(chk.value)	{
		case 'cover_tpo':
			arr=arrTPO; 
			break;
		case 'cover_tpft':
			arr=arrTPFT; 
			break;
		default:
			arr=arrComp; 
			break;
	}
	
	for(var i=0;i<arr.length;i++)	{
		var o = new Option(arr[i],arr[i]);
		ex.options[i] = o;
	}
			
	switch (chk.value)
	{
		case 'cover_tpo':
			chk.form.elements['excess_type'].value = 'no_excess';
			document.getElementById("lblexcess_type").innerHTML = 'N/A';
			break;		
		case 'cover_tpft':
			chk.form.elements['excess_type'].value = 'ft';
			document.getElementById("lblexcess_type").innerHTML = 'F & T<br/> ';
			break;
		case 'cover_c':
			chk.form.elements['excess_type'].value = 'adft';
			document.getElementById("lblexcess_type").innerHTML = 'ADF & T<br/> ';
			break;
	}
	
	//See if we need to set the default item
	if (defaultValue!='')
	{
		for(var i=0;i<ex.length;i++)	{
		if (ex.options[i].value==defaultValue){
			ex.options[i].selected=true;
		}
	}
	}
}

function insertElement()
{
	if (document.getElementById && document.getElementsByTagName && document.getElementById("output"))
	{
		var output = document.getElementById("output");
		
		//create new elements that will be inserted
		var new_li= document.createElement("li");
		var input= document.createElement("input");
		input.setAttribute("type", "image");
		input.setAttribute("src", "/images/gfx_instant_quote/btn-print.gif");
		input.setAttribute("alt", "print");
		input.setAttribute("name", "print");
		input.onclick = print_document;
		
		//add elements to the div
		output.appendChild(input);
	}
}

function addOnChange()
{
	if(document.getElementById("when"))
	{
		document.getElementById("when").onchange = toggleScheduledTime;
	}
							  
}

function toggleScheduledTime()
{
	if (document.getElementById("scheduled-time") && document.getElementById("when") )
	{
		var when= document.getElementById("when");
		var scheduledTime= document.getElementById("scheduled-time");
		var p= scheduledTime.getElementsByTagName("p");
		p[0].className = "hide";
		
		if (when.value == "D")
		{
			scheduledTime.className="show";
		}
		else
		{
			scheduledTime.className="hide";
		}
	}
}

function submitClickToCall()
{
	var title = document.getElementById("udo_title");
	var firstname = document.getElementById("udr_firstname");
	var lastname = document.getElementById("udr_lastname");
	var comments = document.getElementById("udo_comments")
	var element= document.getElementById("callback");

	var agentannounce= document.getElementById("udo_agentannounce");
	
	element.onclick= function () {
		agentannounce.value= "The callers name is " + title.value + " " + firstname.value + " " + lastname.value + ". They would like to talk about " + comments.value;
	}
}
	
function check30DayTaxiBonus()
{
	var radio = document.getElementById("bonus_protection_reqd_yes");
	var title;
	
	if (radio.checked)
	{
		title='Bonus Protection to be Operative From:';
	}
	else
	{
		title='Policy to be Operative From:';
	}
	
	var header = document.getElementById("policy_operative_title");
	
	header.innerHTML = title;
}


function showPreviousPolicy(elmnt)
{
    if (elmnt.value=="previous")
    {
        document.getElementById("PreviousPolicy").style.display="block";
    }
    else
    {
        document.getElementById("PreviousPolicy").style.display="none";
    }
} 


function ShowHideDiv(elmnt, div)
{
	if	(elmnt.checked)
	{
		ShowDiv(div);
	}
	else 
	{
		HideDiv(div);
	}
}


// this function makes the schedule dropdowns default to now
function getDates(arg)
{
// create new Date() object
dtNow = new Date();
// return whatever part of the date the function call asks for.
switch (arg)
 {
   case "h":
     return dtNow.getHours()+1;
       break;
   case "d":
     return dtNow.getDate();
       break;
   case "m":
     // adds 1 to the month value because january=0 in JavaScript
     return dtNow.getMonth()+1;
       break;
   case "y":
     // I had to explicitly cast this value to a String as javascripts automatic
       // handling failed when I used this
       return String(dtNow.getFullYear());
   }
}

function addOnSubmit()
{
	if (document.getElementById("TUWTCY"))
	{
		document.getElementById("TUWTCY").onsubmit= concatendate;
	}
}

function selectFormElements()
{
	// set vars for the date parts we want to match later
	selectHour=getDates("h");
	selectDay=getDates("d");
	selectMonth=getDates("m");
	selectYear=getDates("y");

	// Get object reference for the form select element
	hourSelectObject=document.getElementById('Hour');
	daySelectObject=document.getElementById('Day');
	monthSelectObject=document.getElementById('Month');
	yearSelectObject=document.getElementById('Year');

	if (hourSelectObject)
	{
		// Get a list of options for above select element
		hourOptionArray = hourSelectObject.getElementsByTagName("OPTION");
		dayOptionArray = daySelectObject.getElementsByTagName("OPTION");
		monthOptionArray = monthSelectObject.getElementsByTagName("OPTION");
		yearOptionArray = yearSelectObject.getElementsByTagName("OPTION");

		// Walk through list of options looking for the right one
		for(i=0;i<hourOptionArray.length;i++)
		{
			if(hourOptionArray[i].value==selectHour)
			{
			   // If this option element's value equals the current hour's value
			   // set this element's select attribute to true
			   hourOptionArray[i].selected=true;
			}
		}
		
		
		for(i=0;i<dayOptionArray.length;i++)
		{
			if(dayOptionArray[i].value==selectDay)
			{
			   // If this option element's value equals the current day's value
			   // set this element's select attribute to true
			   dayOptionArray[i].selected=true;
			}
		}
		
		for(i=0;i<monthOptionArray.length;i++)
		{
			if(monthOptionArray[i].value==selectMonth)
			{
			   // If this option element's value equals the current month's value
			   // set this element's select attribute to true
			   monthOptionArray[i].selected=true;
			}
		}
		
		for(i=0;i<yearOptionArray.length;i++)
		{
			if(yearOptionArray[i].value==selectYear)
			{
			   // if this option element's value equals the current year's value
			   // set this element's select attribute to true
			   yearOptionArray[i].selected=true;
			}
		}
	}
}

function hideElement(div) {
	var el= document.getElementById(div);
	el.className="invisible";
}

function selectedCompany(element, element_to_change) {
	var el= document.getElementById(element_to_change);
	
	if (element.selectedIndex != 7) {
		el.className= "invisible";
	}
	
	else {
		el.className= "show";
	}
	
}

// this function concatenates all the schedule fields into one variable called when_date
function concatendate()
{
	if (document.TUWTCY.when.value == "D")
	{
		document.TUWTCY.when_date.value = document.TUWTCY.Year.value +	document.TUWTCY.Month.value + document.TUWTCY.Day.value + document.TUWTCY.Hour.value + document.TUWTCY.Minute.value;
			
	}
}
