var statusTimeoutId = 0;

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 toggleLayer( whichLayer )
{
  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
  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';
}

function showsection(section){

	var elem = document.getElementById(section);

	elem.style.display = 'block';
}

function hidesection(section){

	var elem = document.getElementById(section);

	elem.style.display = 'none';
}

function submitForm(formid, doSave){

	var theform = document.getElementById(formid);

	theform.submit();
}

function saveQuote(formid, fuseaction){

	var theform = document.getElementById(formid);

	theform.action = fuseaction;


	theform.submit();
}

function updateQuote(formid){

	var theform = document.getElementById(formid);
	theform.action += '&update=yes';

	theform.submit();
}

function adjustbills(){

	var elem = document.getElementById("ProductList_ProductPickerAdditionalAmounts");
	var displayElem = document.getElementById("billstocover");
	var mortgageAmount = document.getElementById("AddInfo__SubjAmount_2");
	var coveramount = document.getElementById("coveramount");
	var billsCover = 0;

	mortgageAmount.value = stripAlphaChars(mortgageAmount.value);

	if(mortgageAmount.value != ''){

		billsCover = coveramount.value - mortgageAmount.value;

		if(billsCover > 0){
			elem.value = billsCover;
			displayElem.innerHTML = "&pound;" + billsCover.toFixed(2);
		}
		else{
			elem.value = 0;
			displayElem.innerHTML = "&pound;" + 0;
		}
	}
	else{
		elem.value = 0;
		displayElem.innerHTML = "&pound;" + 0;
	}
}

function stripAlphaChars(pstrSource)
{
	var m_strOut = new String(pstrSource);
    m_sOut = m_strOut.replace(/[^0-9\.]/g, '');

    return m_sOut;
}

function hideSubmitButton(){

	var submitButton = document.getElementById("quote-submit");

	submitButton.style.display = 'none';
}

function endStatusCheck(){
	clearTimeout(statusTimeoutId);
	statusTimeoutId = 0;
}

function checkStatus(){

	statusTimeoutId = setTimeout(checkStatus, 5000);

	$.ajax({
	   type: "GET",
	   dataType: "json",
	   url: "index.cfm?fuseaction=quote.checkstatus",
	   cache: false,
	   success: checkStatusCallback,
	   failure: ajaxError});
}

function checkStatusCallback(response){

	if(response == ""){
		endStatusCheck();
		window.location = "index.cfm";
	}
	else if(response == "COMPLETE"){
		endStatusCheck();
		window.location = "index.cfm?fuseaction=quote.policyconfirm";
	}
	else if(response == "FAILED"){
		endStatusCheck();
		window.location = "index.cfm?fuseaction=quote.step5&error=true";
	}
}

function ajaxError(){
	endStatusCheck();
}