// JavaScript Document

// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
     alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}



function calc_rate()	{

var excess = document.getElementById('excess').value

//SET NUMBER OF RADIO BOXES FOR COVERLENGTH
var periodlen = document.page2.coverlength.length;

	
	//FIND ROOM VALUE BY LOOPING RADIO BOXES TO SEE WHICH ONE IS CHECKED
	for (i = 0; i <periodlen; i++) 
	{
		if (document.page2.coverlength[i].checked)
		{
		var period = document.page2.coverlength[i].value;
		}
	}


//this error is now handled by php on form submit so dis_error is set on form submit
//check to see if policy for 6 months and excess more than 30 as this is not allowed

if (period == 6 && excess > 30)	{
excess = '30';
dis_err = 'Y';
//set back to 30 days excess
document.getElementById('excess').selectedIndex = 0;
}
else
{
dis_err = 'N';	
}


var string = "&excess=" + excess + "&period=" + period + "&error=" + dis_err;

  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // make the ajax webservice call
    xmlHttp.open("GET", "./includes/calc_rate_ajax.php?<?php echo SID;?>" + string, true);  
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('calc_rate()', 4000);
}

// executed automatically when a message is received from the server
function handleServerResponse() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    { var newtext = xmlHttp.responseText;
		//alert(newtext);
  	  //document.getElementById("rate").innerHTML = ""
	  document.getElementById("rate_update").innerHTML = newtext;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

//RIcky 10/03/10 -added for the public sector exclusions
function publicsectorexcluded()	{
	

				$("#canvas").css({opacity:"0.3"});
				$("#footer").css({opacity:"0.3"});
		
				$("#overlay2").appendTo("body")
		
				$("#overlay2").css({position:"absolute",zIndex:"500",background:"#FFFFFF",border:"1px solid #CCCCCC",padding:"10px 15px 10px 15px",width:"350px"})
				myLeft = parseInt((document.body.offsetWidth-$("#overlay2").width())/2);
				myTop = parseInt((document.body.offsetHeight-$("#overlay2").height())/3);
				$("#overlay2").css({left:myLeft,top:myTop})
				if (!overlayInitialized){
					$("#overlay2").prepend("<p class=\"tar\" style=\"margin-top:0px;\"><a href=\"javascript:closeMe();\">Close</a></p>")
					$("#overlay2").append("<p class=\"tac\" style=\"margin-top:20px;\"><a href=\"javascript:closeMe();\"><img src=\"img/buttons/continue.gif\" width=\"80\" height=\"21\" alt=\"Continue\" title=\"Continue\"></a></p>")
					overlayInitialized = true;
				}
				$("#overlay2").show();
				$("#typeofcover").hide();
				$(".hidepublicsetor").hide();
				return false;
			}

function extravalue()	{
	
if (document.choosecover.loanpayment.value > 1000)	{
//if cover over 1000 then automatically hide cover and check no box automatically
document.getElementById('extra_cover').style.display='none';	
document.choosecover.extra[1].checked = true;

			//check if any error to display

				$("#canvas").css({opacity:"0.3"});
				$("#footer").css({opacity:"0.3"});
		
				$("#overlay").appendTo("body")
		
				$("#overlay").css({position:"absolute",zIndex:"500",background:"#FFFFFF",border:"1px solid #CCCCCC",padding:"10px 15px 10px 15px",width:"350px"})
				myLeft = parseInt((document.body.offsetWidth-$("#overlay").width())/2);
				myTop = parseInt((document.body.offsetHeight-$("#overlay").height())/3);
				$("#overlay").css({left:myLeft,top:myTop})
				if (!overlayInitialized){
					$("#overlay").prepend("<p class=\"tar\" style=\"margin-top:0px;\"><a href=\"javascript:closeMe();\">Close</a></p>")
					$("#overlay").append("<p class=\"tac\" style=\"margin-top:20px;\"><a href=\"javascript:closeMe();\"><img src=\"img/buttons/continue.gif\" width=\"80\" height=\"21\" alt=\"Continue\" title=\"Continue\"></a></p>")
					overlayInitialized = true;
				}
				
				$("#overlay").show();
				return false;

}
else
{
var extracoverlen = document.choosecover.extracover.length;

	//FIND ROOM VALUE BY LOOPING RADIO BOXES TO SEE WHICH ONE IS CHECKED
	for (i = 0; i <extracoverlen; i++) 
	{
		//if (document.choosecover.extracover[i].checked)
		//{
		var extracovervalue = document.choosecover.extracover[i].value;
		var extracoverlabelid = document.choosecover.extracover[i].id;

		var extracov = (document.choosecover.loanpayment.value * extracovervalue) / 100
		//extarcov = padding_dec(extracov)

		//iff an error of value = zero then revert back to standard text
		if (extracov != 0) {
			document.getElementById('label' + extracoverlabelid).innerHTML = "Monthly Loan Payment + &pound;" + padding_dec(extracov);
		}
		else
		{
			if (extracoverlabelid == 'ecv1')	{
				document.getElementById('label' + extracoverlabelid).innerHTML = "Monthly Loan Payment + 5%";
			}
			else
			if (extracoverlabelid == 'ecv2')	{
				document.getElementById('label' + extracoverlabelid).innerHTML = "Monthly Loan Payment + 15%";
			}
			else
			if (extracoverlabelid == 'ecv3')	{
				document.getElementById('label' + extracoverlabelid).innerHTML = "Monthly Loan Payment + 25%";
			}

		}
		
		
		
		//}
	}

}
}



function padding_dec(str)	{

	//round
	var str = Math.round(str*100)/100;

//change str to a string
str = str + "";
str = str.replace(" ", "")
var stripit = str.split('.')


//check to see if there is a decimal point first
if (stripit[1] != undefined)	{

	if (stripit[1].length == 1)	{
		str = stripit[0] + "." + stripit[1] + "0";
		//str = str + "0"
	}



}
	return str;

}


$("#overlay_dd").hide();
overlayInitialized1 = false;
function display_dd()	{

				$("#canvas").css({opacity:"0.3"});
				$("#footer").css({opacity:"0.3"});
		
				$("#overlay_dd").appendTo("body")
		
				$("#overlay_dd").css({position:"absolute",zIndex:"500",background:"#FFFFFF",border:"1px solid #CCCCCC",padding:"10px 15px 10px 15px",width:"350px"})
				myLeft = parseInt((document.body.offsetWidth-$("#overlay_dd").width())/2);
				myTop = parseInt((document.body.offsetHeight-$("#overlay_dd").height())/3);
				$("#overlay_dd").css({left:myLeft,top:myTop})
				if (!overlayInitialized1){
					$("#overlay_dd").prepend("<p class=\"tar\" style=\"margin-top:0px;\"><a href=\"javascript:closeMe();\">Close</a></p>")
					$("#overlay_dd").append("<p class=\"tac\" style=\"margin-top:20px;\"><a href=\"javascript:closeMe();\"><img src=\"img/buttons/continue.gif\" width=\"80\" height=\"21\" alt=\"Continue\" title=\"Continue\"></a></p>")
					overlayInitialized1 = true;
				}
				$("#overlay_dd").show();
				return false;


}


/*
 * sets wait period options list based on selected length of cover 
 *
 *  used on :
 *  /quote-choosecover.php
 *  /quote-thequote.php
 * 
 * @parm int chosen selected value
 * @param str fname html form name
 * @param str ename html select element name
*/
function setWaitPeriod(chosen,fname,ename) {

	var selbox = document.forms[fname].elements[ename];
	

	selbox.options.length = 0;

	if (chosen == "6") {
	  selbox.options[selbox.options.length] = new Option('30 days','30');
	}
	if (chosen == "12") {
		selbox.options[selbox.options.length] = new Option('30 days','30');
		selbox.options[selbox.options.length] = new Option('60 days','60');
		selbox.options[selbox.options.length] = new Option('90 days','90');
		selbox.options[selbox.options.length] = new Option('120 days','120');
	}

}



