<!-- //author: jamesng
     //version: 1.0 May 20, 2007
function isNumeric(strString)
{
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
          blnResult = false;
      }
   }
   return blnResult;
}

function validateNumeric(strFieldName)
{
    alert('You must enter a numeric value for ' + strFieldName);
    return false;
}     
     
function calculatePremium()
{
    var intSelectedCoverageType = document.formMain.intCoverageType.selectedIndex;
    var intCoverageTypeValue = Number(document.formMain.intCoverageType.options[intSelectedCoverageType].value);
    switch (intCoverageTypeValue)
    {
        case 0:
            disableAllAdditionalBenefitFields();
            break;
            
        case 1:
            calculateComprehensivePremium(); 
            break;    
        
        case 2:
            calculate3rdPartyTheftAndFire();
            break;
        
        case 3:
            calculate3rdParty();
            break;
    }  
}

function calculate3rdParty()
{   
    //sum insured premium
    if (!isNumeric(document.formMain.floatSumInsured.value))
    {
        alert('You must enter a numeric value for Sum Insured');
        return false;
    }
    
    //Base Premium after loading and NCD
    if (!isNumeric(document.formMain.floatLoading.value))
    {
        alert('You must enter a numeric value for Loading Percentage');
        return false;
    }
    
    //engine cc premium
    var intEngineType = num(document.formMain.intEngineType.options[document.formMain.intEngineType.selectedIndex].value);
    var floatPassengersLiabilityPremium = 0.00;
    
    switch(intEngineType)
    {
        case -1:
            float3rdPartyEngineCCPremium = 0;            
            break;
            
        case 0:
            float3rdPartyEngineCCPremium = 72;
            break;
        
        case 1401:
            float3rdPartyEngineCCPremium = 81;
            break;  
            
        case 1651:
            float3rdPartyEngineCCPremium = 90;
            break;
            
        case 2201:
            float3rdPartyEngineCCPremium = 99;
            break;    
            
        case 3051:
            float3rdPartyEngineCCPremium = 108;
            break;    
            
        case 4101:
            float3rdPartyEngineCCPremium = 117;
            break;    
            
        case 4251:
            float3rdPartyEngineCCPremium = 126;
            break;
        
        case 4401:;
            float3rdPartyEngineCCPremium = 135;
            break;
    }
    
    //Base Premium
    var floatBasePremium = parseFloat(float3rdPartyEngineCCPremium);
    document.getElementById('floatBasePremium').innerHTML = currency(floatBasePremium);
    
    
    //Base Premium after loading and NCD
    var floatLoading = parseFloat(document.formMain.floatLoading.value);
    var floatNCD = parseFloat(document.formMain.floatNCD.options[document.formMain.floatNCD.selectedIndex].value);
    var floatBasePremiumAfterNCD = floatBasePremium;
    var part1 = parseFloat(1+floatLoading/100);
    var part2 = parseFloat(1-floatNCD/100);
    var part3 = part1 * part2;
    floatBasePremiumAfterNCD = parseFloat(floatBasePremium) * parseFloat(part3);
    document.getElementById('floatBasePremiumAfterNCD').innerHTML = currency(floatBasePremiumAfterNCD);
    
    var floatTotalPremium = floatBasePremiumAfterNCD;
    document.getElementById('floatTotalPremium').innerHTML = currency(floatTotalPremium);
}

function calculate3rdPartyTheftAndFire()
{
    //sum insured premium
    if (!isNumeric(document.formMain.floatSumInsured.value))
    {
        alert('You must enter a numeric value for Sum Insured');
        return false;
    }
    
    //Base Premium after loading and NCD
    if (!isNumeric(document.formMain.floatLoading.value))
    {
        alert('You must enter a numeric value for Loading Percentage');
        return false;
    }
    
    
    
    //engine cc premium
    var intEngineType = num(document.formMain.intEngineType.options[document.formMain.intEngineType.selectedIndex].value);
    var floatPassengersLiabilityPremium = 0.00;
    
    switch(intEngineType)
    {
        case -1:
            float3rdPartyTheftAndFireEngineCCPremium = 0;            
            break;
            
        case 0:
            float3rdPartyTheftAndFireEngineCCPremium = 225.20;
            break;
        
        case 1401:
            float3rdPartyTheftAndFireEngineCCPremium = 251.50;
            break;  
            
        case 1651:
            float3rdPartyTheftAndFireEngineCCPremium = 277.90;
            break;
            
        case 2201:
            float3rdPartyTheftAndFireEngineCCPremium = 304.20;
            break;    
            
        case 3051:
            float3rdPartyTheftAndFireEngineCCPremium = 330.50;
            break;    
            
        case 4101:
            float3rdPartyTheftAndFireEngineCCPremium = 356.80;
            break;    
            
        case 4251:
            float3rdPartyTheftAndFireEngineCCPremium = 383.20;
            break;
        
        case 4401:
            float3rdPartyTheftAndFireEngineCCPremium = 409.50;
            break;   
            
    }
   
    //sum insured premium
    floatSumInsured = parseFloat((document.formMain.floatSumInsured.value/1000)*1000);
    //floatSumInsured = document.formMain.floatSumInsured.value;
    floatSumInsuredPremium = parseFloat((floatSumInsured - 1000)/1000 * 26);
    
    //fix < 1000 sum insured bug
    //if (floatSumInsuredPremium < 0) 
    //{
    //    floatSumInsuredPremium = 0;
    //}
    
    //Base Premium
    var floatBasePremium = (parseFloat(float3rdPartyTheftAndFireEngineCCPremium) + parseFloat(floatSumInsuredPremium)) * 0.75;
    if (intEngineType == -1)
    {    
        floatBasePremium = 0;  
    }
    document.getElementById('floatBasePremium').innerHTML = currency(floatBasePremium);
    
    
    var floatLoading = parseFloat(document.formMain.floatLoading.value);
    var floatNCD = parseFloat(document.formMain.floatNCD.options[document.formMain.floatNCD.selectedIndex].value);
    var floatBasePremiumAfterNCD = floatBasePremium;
    var part1 = parseFloat(1+floatLoading/100);
    var part2 = parseFloat(1-floatNCD/100);
    var part3 = part1 * part2;
    floatBasePremiumAfterNCD = parseFloat(floatBasePremium) * parseFloat(part3);
    document.getElementById('floatBasePremiumAfterNCD').innerHTML = currency(floatBasePremiumAfterNCD);
    
    var floatTotalPremium = floatBasePremiumAfterNCD;
    document.getElementById('floatTotalPremium').innerHTML = currency(floatTotalPremium);
}


function calculateComprehensivePremium()
{
    //sum insured premium
    if (!isNumeric(document.formMain.floatSumInsured.value))
    {
        alert('You must enter a numeric value for Sum Insured');
        return false;
    }
    
    //Base Premium after loading and NCD
    if (!isNumeric(document.formMain.floatLoading.value))
    {
        alert('You must enter a numeric value for Loading Percentage');
        return false;
    }
        
    //engine cc premium
    var intEngineType = num(document.formMain.intEngineType.options[document.formMain.intEngineType.selectedIndex].value);
    var floatPassengersLiabilityPremium = 0.00;
    
    switch(intEngineType)
    {
        case -1:
            floatComprehensiveEngineCCPremium = 0;
            break;
            
        case 0:
            floatComprehensiveEngineCCPremium = 225.20;
            floatPassengersLiabilityPremium = 18.00;
            break;
        
        case 1401:
            floatComprehensiveEngineCCPremium = 251.50;
            floatPassengersLiabilityPremium = 20.25;
            break;  
            
        case 1651:
            floatComprehensiveEngineCCPremium = 277.90;
            floatPassengersLiabilityPremium = 22.50;
            break;
            
        case 2201:
            floatComprehensiveEngineCCPremium = 304.20;
            floatPassengersLiabilityPremium = 24.75;
            break;    
            
        case 3051:
            floatComprehensiveEngineCCPremium = 330.50;
            floatPassengersLiabilityPremium = 27.00;
            break;    
            
        case 4101:
            floatComprehensiveEngineCCPremium = 356.80;
            floatPassengersLiabilityPremium = 29.25;
            break;    
            
        case 4251:
            floatComprehensiveEngineCCPremium = 383.20;
            floatPassengersLiabilityPremium = 31.50;
            break;
        
        case 4401:
            floatComprehensiveEngineCCPremium = 409.50;
            floatPassengersLiabilityPremium = 33.75;
            break;   
            
    }
   
    //document.MotorForm.txtEngineCCPremium.value = strComprehensiveEngineCCPremium
    
    
    floatSumInsured = document.formMain.floatSumInsured.value;
    floatSumInsuredPremium = parseFloat((floatSumInsured - 1000)/1000 * 26);
    
    //fix < 1000 sum insured bug
    //if (floatSumInsuredPremium < 0) 
    //{
    //    floatSumInsuredPremium = 0;
    //}
    
    //Base Premium
    var floatBasePremium = parseFloat(floatComprehensiveEngineCCPremium) + parseFloat(floatSumInsuredPremium);
    if (intEngineType == -1)
    {    
        floatBasePremium = 0;  
    }
    document.getElementById('floatBasePremium').innerHTML = currency(floatBasePremium);
    
    //Base Premium after loading and NCD
    
    var floatLoading = parseFloat(document.formMain.floatLoading.value);
    var floatNCD = parseFloat(document.formMain.floatNCD.options[document.formMain.floatNCD.selectedIndex].value);
    var floatBasePremiumAfterNCD = floatBasePremium;
    var part1 = parseFloat(1+floatLoading/100);
    var part2 = parseFloat(1-floatNCD/100);
    var part3 = part1 * part2;
    floatBasePremiumAfterNCD = parseFloat(floatBasePremium) * parseFloat(part3);
    document.getElementById('floatBasePremiumAfterNCD').innerHTML = currency(floatBasePremiumAfterNCD);
    
    var floatTotalPremium = floatBasePremiumAfterNCD;
    
    //calculate additional benefits if coverage is comprehensive
    var intSelectedCoverageType = document.formMain.intCoverageType.selectedIndex;
    var intCoverageTypeValue = Number(document.formMain.intCoverageType.options[intSelectedCoverageType].value);
    if (intCoverageTypeValue == 1)
    {
        floatTotalWindscreenPremium = calculateWindscreenPremium();
        floatTotalAccessoriesPremium = calculateAccessoriesPremium();
        floatTotalFloodPremium = calculateFloodPremium();
        floatTotalPassengersLiabilityPremium = calculatePassengersLiabilityPremium(floatPassengersLiabilityPremium);
        
        //calculate total premium
        floatTotalPremium = floatTotalPremium + floatTotalWindscreenPremium + floatTotalAccessoriesPremium + floatTotalFloodPremium +floatTotalPassengersLiabilityPremium;
    }
    
    document.getElementById('floatTotalPremium').innerHTML = currency(floatTotalPremium);
}



function calculateWindscreenPremium()
{
    //Base Premium after loading and NCD
    if (!document.formMain.floatWindScreenInsured.disabled)
    {
        if (!isNumeric(document.formMain.floatWindScreenInsured.value))
        {
            alert('You must enter a numeric value for Windscreen Sum Insured');
            return false;
        }
    }
    
    //windscreen
    var floatWindscreenPremium = 0.00;
    if (document.formMain.boolWindscreenCover.checked)
    {
        document.formMain.floatWindScreenInsured.disabled = false; 
		
		//suffian recode for windscreen insurance calculation
		if ( document.formMain.floatWindScreenInsured.value >= 200 )
		{
			floatWindscreenPremium = document.formMain.floatWindScreenInsured.value * 0.15;
		}
		if ( document.formMain.floatWindScreenInsured.value >= 1 && document.formMain.floatWindScreenInsured.value <= 199 )
		{
        	floatWindscreenPremium = 30.00;
		}
        document.getElementById('floatWindScreenPremium').innerHTML = currency(floatWindscreenPremium); 
    }
    else
    {
        document.formMain.floatWindScreenInsured.disabled = true;
        document.formMain.floatWindScreenInsured.value = '';
        document.getElementById('floatWindScreenPremium').innerHTML = currency(0); 
    }
    return floatWindscreenPremium;
}

function calculateAccessoriesPremium()
{
    //Base Premium after loading and NCD
    if (!document.formMain.floatAccessoriesInsured.disabled)
    {
        if (!isNumeric(document.formMain.floatAccessoriesInsured.value))
        {
            alert('You must enter a numeric value for Accessories Sum Insured');
            return false;
        }
    }
    
    //accessories
    var floatAccessoriesPremium = 0.00;
    if (document.formMain.boolAccessoriesCover.checked)
    {
        document.formMain.floatAccessoriesInsured.disabled = false;
        floatAccessoriesPremium = document.formMain.floatAccessoriesInsured.value * 0.15;
        document.getElementById('floatAccessoriesPremium').innerHTML = currency(floatAccessoriesPremium);    
    }
    else
    {
        document.formMain.floatAccessoriesInsured.disabled = true;
        document.formMain.floatAccessoriesInsured.value = '';
        document.getElementById('floatAccessoriesPremium').innerHTML = currency(0);
    }
    return floatAccessoriesPremium;
}

function calculateFloodPremium()
{
    //accessories
    var floatFloodPremium = 0.00;
    if (document.formMain.boolFloodCover.checked)
    {
        floatFloodPremium = document.formMain.floatSumInsured.value * 0.005;
        document.getElementById('floatFloodPremium').innerHTML = currency(floatFloodPremium);    
    }
    else
    {
        document.getElementById('floatFloodPremium').innerHTML = currency(0);
    }
    return floatFloodPremium;
}

function calculatePassengersLiabilityPremium(floatPassengersLiabilityPremium)
{
    //accessories
    var result = 0.00;
    if (document.formMain.boolPassengersCover.checked)
    {
        result = floatPassengersLiabilityPremium;
        document.getElementById('floatPassengersPremium').innerHTML = currency(result);    
    }
    else
    {
        document.getElementById('floatPassengersPremium').innerHTML = currency(0);
    }
    return result;
}

function checkCoverageTypeFields()
{
    var intSelectedCoverageType = document.formMain.intCoverageType.selectedIndex;
    var intCoverageTypeValue = Number(document.formMain.intCoverageType.options[intSelectedCoverageType].value);
    switch (intCoverageTypeValue)
    {
        case 0:
            disableAllAdditionalBenefitFields();
            break;
            
        case 1:
            checkComprehensiveFields(); 
            break;    
        
        case 2:
            disableAllAdditionalBenefitFields();
            break;
        
        case 3:
            disableAllAdditionalBenefitFields();
            break;
    }  
}

function checkComprehensiveFields()
{
    document.formMain.boolWindscreenCover.disabled = false;
    document.formMain.boolFloodCover.disabled = false;
    document.formMain.boolAccessoriesCover.disabled = false;
    document.formMain.boolPassengersCover.disabled = false;
}

function disableAllAdditionalBenefitFields()
{
    document.formMain.boolWindscreenCover.disabled = true;
    document.formMain.boolFloodCover.disabled = true;
    document.formMain.boolAccessoriesCover.disabled = true;
    document.formMain.boolPassengersCover.disabled = true;
    document.formMain.floatWindScreenInsured.disabled = true;
    document.formMain.floatAccessoriesInsured.disabled = true; 
    
    //reset values
    document.formMain.boolWindscreenCover.checked = false;
    document.formMain.boolFloodCover.checked = false;
    document.formMain.boolAccessoriesCover.checked = false;
    document.formMain.boolPassengersCover.checked = false;
    document.formMain.floatWindScreenInsured.value = '';
    document.formMain.floatAccessoriesInsured.value = '';
    
    document.getElementById('floatWindScreenPremium').innerHTML = '0.00';
    document.getElementById('floatFloodPremium').innerHTML = '0.00';
    document.getElementById('floatAccessoriesPremium').innerHTML = '0.00';
    document.getElementById('floatPassengersPremium').innerHTML = '0.00';
}

function round(number,X) 
{
    // rounds number to X decimal places, defaults to 2
    x = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function num(obj) 
{
    val = parseFloat(obj);
    if (isNaN(val)) 
    { 
        val = 0; 
    }
    return val;
}

function currency(anynum) 
{
   //-- Returns passed number as string in $xxx,xxx.xx format.
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   
   //-- Adds comma in billions place.
   if (dNum>=1000000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000000))+","+dStr.substring(dLen-11,dLen)
   }
   
   //-- Adds comma in trillions place.
   if (dNum>=1000000000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000000000))+","+dStr.substring(dLen-15,dLen)
   }
   retval = dStr + pStr 
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return ""+retval
   //return "$"+retval
}
-->