/*AF 2009 ajax functionality*/
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function showSearch()
{
	if (document.getElementById('searchBox').style.display==''||document.getElementById('searchBox').style.display=='none')
		{
		document.getElementById('searchBox').style.display="block";
		}
	else
		{
		document.getElementById('searchBox').style.display="none";
		}
	return false;
}



/*
 * RLA QuickQuote Calculator
 *
 * BB_ March 2007
 *
 */

         function openWindow(url, name, params)
        {  
            popupWin = window.open(url, name, params);
        }


function validateQuoteForm() {

	var quoteFrm = document.forms.quickquoteFrm;
	
	//non zero length postcode
	if(trim(quoteFrm.txtPostCode.value).length == 0)
		return	"Enter <strong>Insured Postcode</strong>";		
	
    //check building sum insured is present
	if(trim(quoteFrm.txtRebuild.value).length == 0)
        return "Enter <strong>Building Sum Insured</strong>";     
	
	if(trim(quoteFrm.txtRebuild.value).length > 0 && quoteFrm.cmbTypeLet.selectedIndex == 0)
        return "Select a <strong>Tenancy Type</strong>";     
	

  	//validate building value
	var txtRebuild_val = trim(quoteFrm.txtRebuild.value);    
	if(txtRebuild_val.length > 0) { //will always be true from line 19
  	
		//check insured sum is numeric		
		if(!isNumeric(txtRebuild_val))
			return "Please ensure that the <strong>Building Sum Insured</strong> contains just numbers."; //not numeric
		
		txtRebuild_val = parseFloat(txtRebuild_val); 
		
		//check buidling sum bounds
		if(txtRebuild_val < 35000)
			return "<strong>Buiding Sum Insured</strong> must be &gt; <strong>£35,000</strong>";
		else if(txtRebuild_val > 500000)
			return "<strong>Buiding Sum Insured</strong> must be &lt; <strong>£500,000</strong>";
	}
	
	//validate contents value
    var txtContents_val = trim(quoteFrm.txtContents.value);    
	if(txtContents_val.length > 0) {
	
		//check content sum is numeric
		if(!isNumeric(txtContents_val))
			return "Please ensure that the <strong>Content Sum Insured</strong> contains just numbers."; //not numeric
			
		txtContents_val = parseFloat(txtContents_val);
		
		//check contents sum bounds
		if(txtContents_val > 40000)
			return "<strong>Content Sum Insured</strong> must be &lt; £40,000";
	}
	else
	  return "Enter <strong>Content Sum Insured</strong>";

	//all OK if this point reached
	return "OK";
}









function calcQuote() {
  
  var valstatus = validateQuoteForm();
  if(valstatus == "OK") {
    document.getElementById('quoteerr').style.display = "none";	  
	
	var dblValue = 0;   
	
	
    var txtRebuild_val = trim(document.forms.quickquoteFrm.txtRebuild.value);
	if(txtRebuild_val.length > 0) { //will always be true

      var dblBldSum = parseFloat(txtRebuild_val);
			/* NOT USED: txtRebuild.Text = Format(dblBldSum, "£##,###,##0.00") */
      
	  switch(document.forms.quickquoteFrm.cmbTypeLet.selectedIndex) {
	  	case 1:
		  dblValue = 155; break;
        case 2:
          dblValue = 175; break;
        case 3:
          dblValue = 225; break;
        case 4:
          dblValue = 190; break;
        case 5:
          dblValue = 280; break;
	  }
       
	  if(dblValue > 0) { //always TRUE
		dblBldPrem = round2dp(((dblBldSum * dblValue) / 100000) + 0.0001);
	  }    
	}
	
	
	var txtContents_val = trim(document.forms.quickquoteFrm.txtContents.value);
	if(txtContents_val.length > 0) { //will always be true
	
		var dblContSum = parseFloat(txtContents_val);
            /* NOT USED: txtContents.Text = Format(dblContSum, "£##,###,##0.00") */
		var dblContPrem = round2dp(((dblContSum * 5) / 1000) + 0.0001);
		  						
        if(dblBldPrem == 0 && dblContPrem < 50)
          dblContPrem = 50;
	}
        
	dblValue = dblContPrem + dblBldPrem;
		
	if(dblValue > 0) {
		dblValue = round2dp(dblValue * 1.05 + 0.0001);
        
		//added to format to £0.00
		finalQuote = dblValue.toFixed(2);
		
		document.getElementById('quoteval').innerHTML = "£" + finalQuote;
		document.getElementById('myquote').style.display = "block";
	}
    else {
      	document.getElementById('quoteval').innerHTML = "";
		document.getElementById('myquote').style.display = "none";
	}
	
  }
  else {
	document.getElementById('quoteerr').innerHTML = valstatus;
  	document.getElementById('quoteerr').style.display = "block";
	
	document.getElementById('quoteval').innerHTML = "";
	document.getElementById('myquote').style.display = "none";
  }
}






//Adapted From: http://www.newbiesbeware.com/idocs/forms/index_famsupp_157.shtmll
//Submits QuickQuote form when user presses enter in field
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   //myfield.form.submit();
   calcQuote();
   return false;
   }
else
   return true;
}





/******************************** String TRIM implementation ********************************/

// Removes leading whitespaces
function ltrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function rtrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return ltrim(rtrim(value));
	
}

/******************************* String IS NUMERIC Implementation ********************************************/
//from http://www.codetoad.com/javascript/isnumeric.asp


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;
}

/*************************** ASP Round(to 2 decimal places) Implementation ***************************/

function round2dp(num) {
  
  return Math.round(num*100) / 100;

}

function showMessage(divId) {

	document.getElementById('thisisabox').style.display='none';
	 document.getElementById('dimBg').style.display='block';
	document.getElementById('dimBg').style.position ='fixed';
	document.getElementById('dimBg').style.width ='100%';
	document.getElementById('dimBg').style.height ='100%';
	document.getElementById('dimBg').style.top='0';
	document.getElementById('dimBg').style.left='0';
	document.getElementById('dimBg').style.zIndex ='100';
	
	document.getElementById(divId).style.display='block';
	document.getElementById(divId).style.position='fixed';
	document.getElementById(divId).style.zIndex ='10000000';
	document.body.style.overflow='hidden';

	//alert(document.getElementById(divId).style.zIndex);
	}

function hideMessage(divId) {
	document.getElementById(divId).style.display='none';
	document.getElementById('dimBg').style.display='none';
	document.body.style.overflow='';
return false;
}

 function showCouncil(letter) {
	
	if (letter) {

		var divs = document.getElementsByTagName( 'div' );
		for(var i=0;i<divs.length;i++){
			if(divs.item(i).getAttribute( 'name' ) == 'councilLetter' ){
			divs.item(i).style.display = 'none'
			}
		}
		document.getElementById('council'+letter).style.display = 'block';
	
	} else {
	
		var divs = document.getElementsByTagName( 'div' );
		for(var i=0;i<divs.length;i++){
			if(divs.item(i).getAttribute( 'name' ) == 'councilLetter' ){
			divs.item(i).style.display = 'block'
			}
		}
	}
 }


