
function GLFormUpdate() {
    SelectPlan();
    GLQuoteCalc(document.GLQuoteCalc);
}

function SelectPlan() {

if ( !document.GLQuoteCalc.rdPlanType[0].checked && !document.GLQuoteCalc.rdPlanType[1].checked) { // neither?
  document.GLQuoteCalc.rdPlanType[0].checked = true;
}


if (document.GLQuoteCalc.rdPlanType[0].checked) { // GoldenLife
    changeDisplay("GLKidsFuneral","none");
    changeDisplay("GLPremium","inline");
    changeDisplay("GLSum","none");
  } else {
    changeDisplay("GLKidsFuneral","inline");
    changeDisplay("GLPremium","none");
    changeDisplay("GLSum","inline");
    document.GLQuoteCalc.GLSumInsured1.value = 12000.00;
    document.GLQuoteCalc.GLSumInsured2.value = 12000.00;
  }
  ValidateAge(document.GLQuoteCalc.GLAge1);
  ValidateAge(document.GLQuoteCalc.GLAge2);

}

// NO LONGER USED
function SelectPlan2() {
if (document.GLQuoteCalc.rdPlanType2[0].checked) { // GoldenLife
    changeDisplay("GLKidsFuneral2","none");
  } else {
    changeDisplay("GLKidsFuneral2","inline");
    document.GLQuoteCalc.GLSumInsured2.value = 12000.00;
  }
}

function GLQuoteCalc(form) {

	var GLSumInsured1 = parseFloat(form.GLSumInsured1.value);
	var GLSumInsured2 = parseFloat(form.GLSumInsured2.value);
	var GLPremium1 = parseFloat(form.GLPremium1.value);
	var GLPremium2 = parseFloat(form.GLPremium2.value);
	var isMonthly = true;

//	alert(document.GLQuoteCalc.rbGLPaymentOptionA[1].value);

        if (document.GLQuoteCalc.rbGLPaymentOptionA[1].checked) {
	   isMonthly = false
	}

	if(!ValidateNum(GLSumInsured1)) {
		GLSumInsured1 = 0;
	} else {

//	  if (document.GLQuoteCalc.rdPlanType[0].checked) { // goldenlife, not funeral
//		GLSumInsured1 = GLSumInsured1 / 1.2; // reduce cover by 20% Sept 10
//	  }
	  
	  if(GLSumInsured1 > 50000){
		alert('Maximum cover is $50,000');
		form.GLSumInsured1.value = "";
	  } 
	  else {
		form.GLSumInsured1.value = FmtPrice(GLSumInsured1);
	  }
	}
		
	if(!ValidateNum(GLSumInsured2)) {
		GLSumInsured2 = 0;
	}
	else {

//	  if (document.GLQuoteCalc.rdPlanType[0].checked) { // goldenlife, not funeral
//		GLSumInsured2 = GLSumInsured2 / 1.2; // reduce cover by 20% Sept 10
//	  }
	  
	  if(GLSumInsured2 > 50000){
		alert('Maximum cover is $50,000');
		form.GLSumInsured2.value = "";
	  }
	  else {
		form.GLSumInsured2.value = FmtPrice(GLSumInsured2);
	  }
	}
	if(!ValidateNum(GLPremium1)) {
		GLPremium1 = 0;
	}
	else if (document.GLQuoteCalc.rdPlanType[0].checked) { // goldenlife, not funeral
	  
//	  GLPremium1 = GLPremium1 * 1.2;	 // 20% added Sept 2010

	  if((GLPremium1 < 17.33) && (isMonthly)){
		alert('Minimum monthly premium is $17.33');
		form.GLPremium1.value = "";
	  }
	  else if((GLPremium1 < 8) && (!isMonthly)){
		alert('Minimum fortnightly premium is $8');
		form.GLPremium1.value = "";
  	  }
	  else {
		form.GLPremium1.value = FmtPrice(GLPremium1);
	  }	
	}
	else { // funeral is acceptable at any value
		form.GLPremium1.value = FmtPrice(GLPremium1);
	}
	
	if(!ValidateNum(GLPremium2)) {
		GLPremium2 = 0;
	}
	else if (document.GLQuoteCalc.rdPlanType[0].checked) { // goldenlife, not funeral

//	  GLPremium2 = GLPremium2 * 1.2;	 // 20% added Sept 2010

	  if((GLPremium2 < 17.33) && (isMonthly)){
		alert('Minimum monthly premium is $17.33');
		form.GLPremium1.value = "";
	  }
	  else if((GLPremium2 < 8) && (!isMonthly)){
		alert('Minimum fortnightly premium is $8');
		form.GLPremium2.value = "";
	  }
	  else {
		form.GLPremium2.value = FmtPrice(GLPremium2);
	  }
	} else { // funeral premium isn't limited
		form.GLPremium2.value = FmtPrice(GLPremium2);
	}
	
	var GLFortnight1 = (GLPremium1*12)/26;
	var GLFortnight2 = (GLPremium2*12)/26;

	var GLTotalFortnight = FmtPrice(GLFortnight1+GLFortnight2);
	var GLTotalMonthly = FmtPrice(GLPremium1+GLPremium2);
	
	if (isMonthly){
		form.GLTotalPremium.value = GLTotalMonthly;
		document.getElementById('totalLife1').innerHTML = "$" + FmtPrice(GLPremium1) + " per month";
		document.getElementById('totalLife2').innerHTML = "$" + FmtPrice(GLPremium2) + " per month";
		document.getElementById('totalPremiums').innerHTML = "$" + GLTotalMonthly + " per month";
	}
	else {
// commented out because now using rbGLPaymentOptionA which does NOT cause premiums to be rejigged to fortnightly in CSV app!
//		form.GLTotalPremium.value = GLTotalMonthly;
//		document.getElementById('totalLife1').innerHTML = "$" + FmtPrice(GLPremium1) + " per fortnight";
//		document.getElementById('totalLife2').innerHTML = "$" + FmtPrice(GLPremium2) + " per fortnight";
//		document.getElementById('totalPremiums').innerHTML = "$" + GLTotalMonthly + " per fortnight";
		form.GLTotalPremium.value = GLTotalFortnight;
		document.getElementById('totalLife1').innerHTML = "$" + FmtPrice(GLFortnight1) + " per fortnight";
		document.getElementById('totalLife2').innerHTML = "$" + FmtPrice(GLFortnight2) + " per fortnight";
		document.getElementById('totalPremiums').innerHTML = "$" + GLTotalFortnight  + " per fortnight";
	}
	
	form.GLTotalMonth1.value = FmtPrice(GLPremium1);
	form.GLTotalFortnight1.value = FmtPrice(GLFortnight1);
	form.GLTotalMonth2.value = FmtPrice(GLPremium2);
	form.GLTotalFortnight2.value = FmtPrice(GLFortnight2);

}


function FmtPrice(result) {
                    result = ConvNumber(result, 2, ".");
                    return result;
}

function ConvNumber(expr, decplaces, point) {

	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return (str.substring(0,decpoint) + point + str.substring(decpoint,str.length));
}

function ValidateNum(checkVal) {

	returnVal = parseFloat(checkVal);

	if(isNaN(returnVal)) return false;
	else if(returnVal < 0) return false;
	else return true;
}

function ValidateAge(checkVal) {

        var maxAge = 79;
        var minAge = 20;
        
        if (document.GLQuoteCalc.rdPlanType[1].checked) {
          maxAge = 64;
          minAge = 16;
        }

	returnVal = parseFloat(checkVal.value);

	if(returnVal < minAge||returnVal > maxAge) {
		alert('Quotes for this product can only be provided between the ages of ' + minAge + ' and ' + maxAge);
		checkVal.value = minAge;
//		document.GLQuoteCalc.GLAge2.value = 20;
	}
	else return true;
}

function changeDisplay( elementId, setTo ) {
  var theElement;
  if( document.getElementById ) {
    //DOM
    theElement = document.getElementById( elementId );
  } else if( document.all ) {
    //Proprietary DOM
    theElement = document.all[ elementId ];
  }
  if( !theElement ) {
    /* The page has not loaded, or the browser claims to
    support document.getElementById or document.all but
    cannot actually use either */
    return;
  }
  //Reference the style ...
  if( theElement.style ) { theElement = theElement.style; }
  if( typeof( theElement.display ) == 'undefined' ) {
    //The browser does not allow us to change the display style
    //Alert something sensible (not what I have here ...)
    window.alert( 'Your browser does not support this' );
    return;
  }
  //Change the display style
  theElement.display = setTo;
}
