function goBack()
{
	document.getElementById('goBackButton').style.display='none';
	document.getElementById('bmi_calculation_results').style.display='none';    	
	document.getElementById('bmi_calculation_form').style.display='';
}

function BMICalculatorHomePage()
{
	document.getElementById('goBackButton').style.display='none';
	document.getElementById('bmi_calculation_results').style.display='none';    	
	document.getElementById('bmi_calculation_form').style.display='';
}

function measurementSystem() 
{
	
      if(document.getElementById('metricSystemId').checked)
      {
    	  document.getElementById('metricId').style.display='';
          document.getElementById('imperialId').style.display='none';
          document.getElementById('imperailWaistId').value = ''; //imperial
      }
      else
      {
    	  document.getElementById('metricId').style.display='none';
          document.getElementById('imperialId').style.display='';
          document.getElementById('metricWaistId').value = ''; //metric 	 
      }
}

function getBMIResults()
{
	
	var measurementSystemType = 'imperial';
	if(document.getElementById('metricSystemId').checked)
	{
		measurementSystemType = document.getElementById('metricSystemId').value;
	}
	else
	{
		measurementSystemType = document.getElementById('ImperialSystemId').value;
	}
	
	var sex = 'Male';
	if(document.getElementById('maleId').checked)
	{
		sex = document.getElementById('maleId').value;
	}
	else
	{
		sex = document.getElementById('femaleId').value;
	}	
	
	var age = document.getElementById('ageId').value;
    var height = document.getElementById('heightId').value;
    var weight = document.getElementById('weightId').value;
    var feet = document.getElementById('feetId').value;
    var inch = document.getElementById('inchId').value;        
    var stone = document.getElementById('stoneId').value;
    var pound = document.getElementById('poundId').value;        
    var lc = document.getElementById('lcId');
    var sic = document.getElementById('sicId');

    //validate fields
    var errorMessage = validateFields(measurementSystemType, age, height, weight, feet, inch, stone, pound, lc, sic);
    if( errorMessage != '')
    {
    	document.getElementById('error').innerHTML = errorMessage;
    	///document.getElementById('sicHtml').innerHTML = '';
    	///document.getElementById('lcHtml').innerHTML = '';
  	    document.getElementById('bmi_calculation_results').style.display='none';    	
  	    document.getElementById('bmi_calculation_form').style.display='';
    	return;
    }
    else
    {
    	document.getElementById('error').innerHTML = '';
    	document.getElementById('bmi_calculation_form').style.display='none';
    	document.getElementById('bmi_calculation_results').style.display='';
    	document.getElementById('goBackButton').style.display='';
    }

	var waist = 0;
	var metricWaist = document.getElementById('metricWaistId').value; //metric
	var imperailWaist = document.getElementById('imperailWaistId').value; //imperial
	if(metricWaist!='')
	{
		waist = metricWaist;
	}
	else if(imperailWaist!='')
	{
		waist = imperailWaist;
	}
	else 
	{
		waist = 0;
	}

	var bmi = calculateBmi(measurementSystemType, height, feet, inch, weight, stone, pound);//bmi value
	bmi = roundNumber(bmi, 2);//round bmi to 2 decimal places
	document.getElementById('BMI_results').innerHTML = "<font style=color:#ffffff; font-size:12px;>Your client's BMI result is </font> <font style=color:#ffffff; font-size:18px;><b>" + bmi + "</b></font><br /><br />";
	
    var standardTermsRule = "<font style=\"color:#ffffff; font-size:12px; \">This means their application is likely to be accepted <b>on standard terms</b>.</font>";
    var specialTermsRule = "<font style=\"color:#ffffff; font-size:12px; \">This means their application is likely to be accepted <b>with special terms, subject to further underwriting</b>.</font>";
    var declineRule = "<font style=\"color:#ffffff; font-size:12px; \"> This means their application is likely to be <b>declined</b>.</font>";
    
	
	//BMI for LC
	var lcResultsHtml = document.getElementById('lcHtml');
	if(lc.checked == true) //do BMI calculation for LC only if Life cover is checked 
	{
		//always do BMI guideline calculation for LC
		var results = doBMIForLCUsingCriteria1(bmi, sex, age);

		if(results == 'declined')//if BMI results guideline was decline, then apply immediate decline.
		{
			lcResultsHtml.innerHTML = "<font style=\"color:#ffffff; font-size:12px; \">Life Cover - </font>" + declineRule;
		}
		else
		{// in here it's either special or standard terms
			var lifeCoverResults = '';
			if(doWaistGender(measurementSystemType, sex, waist, bmi) == 1)//wm <89 use criteria 1(appendix 2)
			{
				if(results != 'standardTerms')
				{
					if (results != 'specialTerms')
					{
						if (results>1.5)
						{
							lifeCoverResults = declineRule;
						}
						else
						{
							lifeCoverResults = specialTermsRule;
						}							
					}
					else
					{
						lifeCoverResults = specialTermsRule;							
					}						
				}
				else
				{
					lifeCoverResults = standardTermsRule;					
				}

			}
			else //wm >89 use criteria 2 (Special terms table)
			{
				var loadings = 0;
				var resultsCriteria2 = doBMIForLCUsingCriteria2(bmi, sex, age);

				if(results != 'standardTerms' && results != 'specialTerms')
				{
					loadings = results+resultsCriteria2;
					if (loadings>1.5)
					{
						lifeCoverResults = declineRule;
					}
					else
					{
						lifeCoverResults = specialTermsRule;
					}
				}						
				else if(results = 'standardTerms')
				{
					lifeCoverResults = specialTermsRule;//override standard terms with special 		
				}							
				else if(results = 'specialTerms')
				{
					lifeCoverResults = specialTermsRule;							
				}	
				else 
				{
					//shouldn't get here
				}
			}
			//show life cover results
			lcResultsHtml.innerHTML = "<font style=\"color:#ffffff; font-size:12px; \">Life Cover - </font>" + lifeCoverResults;
		}
	}
	else //don't do BMI calculation for LC 
	{
		document.getElementById('lcHtml').innerHTML = '';
	}

	//BMI for SIC
	var sicResultsHtml = document.getElementById('sicHtml');
	if(sic.checked == true) //do BMI calculation for SIC only if SIC is checked 
	{
		//always do BMI guideline calculation for SIC
		results = doBMIForSICUsingCriteria1(bmi, sex, age);
		if(results == 'declined')//if BMI results guideline was decline, then apply immediate decline.
		{
			sicResultsHtml.innerHTML = "<font style=\"color:#ffffff; font-size:12px; \">SIC - </font>" + declineRule;
		}
		else
		{
			var sicCoverResults = '';
			if( doWaistGender(measurementSystemType, sex, waist, bmi) == 1 )//wm <89 and 101 use criteria 1 (BMI guideline table)
			{
				if(results != 'standardTerms')
				{
					if (results != 'specialTerms')
					{
						if (results>1.5)
						{
							sicResultsHtml.innerHTML = declineRule;
						}
						else
						{
							sicCoverResults = specialTermsRule;
						}
					}
					else
					{
						sicCoverResults = specialTermsRule;
					}						
				}
				else
				{
					sicCoverResults = standardTermsRule;
				}
			}
			else //wm >89 use criteria 2 (Special terms table)
			{
				var resultsCriteria2 = doBMIForSICUsingCriteria2(bmi, sex, age, 'sic');
				var loadinngs = 0;
				if(results != 'standardTerms' && results != 'specialTerms')
				{
					loadings = results+resultsCriteria2;
					if (loadings>1.5)
					{
						sicCoverResults = declineRule;
					}
					else
					{
						sicCoverResults = specialTermsRule;
					}
				}						
				else if(results = 'standardTerms')
				{
					sicCoverResults = specialTermsRule;//override standard terms with special			
				}							
				else if(results = 'specialTerms')
				{
					sicCoverResults = specialTermsRule;							
				}	
				else 
				{
					//shouldn't get here
				}
			}
			sicResultsHtml.innerHTML = "<font style=\"color:#ffffff; font-size:12px; \">SIC - </font>" + sicCoverResults; 
		}							
		
	}
	else //don't do BMI calculation for SIC 
	{
		document.getElementById('sicHtml').innerHTML = '';
	}
}

/**
 * Check which criteria to use based on the body waist measurement, criteria 1(appendix 2), or criteria 2(unnamed)
 */
function doWaistGender(measurementSystem, gender, waist, bmi)
{
	var criteria = 1;
	var female = 'Female';
	var male = 'Male';
	if(measurementSystem == 'metric')
	{
		//Using metrics system 
        if( (waist<89 && gender == female) || (waist<101 && gender == male) || bmi<25 )
        {
        	criteria = 1; //waist measurement is less than 100cm, use appendix 2(criteria 1)
        }
        else
        {
        	criteria = 2; //waist measurement is equal or more than 100cm, use (criteria 2)
        }
	}
	else//Using imperial system 
	{
        if( (waist<35 && gender == female) || (waist<40 && gender == male) || bmi<25)
        {
        	criteria = 1; //waist measurement is less than 100cm, use appendix 2(criteria 1)
        }
        else
        {
        	criteria = 2; //waist measurement is equal or more than 100cm, use (criteria 2)
        }			
	}
	return criteria;
}	

//manipulate BMI for life - using criteria 1(appendix 2), results depends on bmin, age, and gender
function doBMIForLCUsingCriteria1(bmi, gender, age)
{
  
   var bmiForLCResults = '';
   var loadings = 0; // special terms and loadings
          
   var declined = "declined";
   var standardTerms = "standardTerms";
   var specialTerms = "specialTerms";
        
	//bmi<15
    if (bmi<15) 
    {
         bmiForLCResults = declined;
    }

    //bmi==15 && bmi<17
    else if (bmi>=15 && bmi<17) 
    {
        bmiForLCResults = specialTerms;
    }

    //bmi==19 && bmi<29 || bmi==29 && bmi<31  || bmi==31 && bmi<33 
    else if (bmi>=19 && bmi<34) 
    {
        bmiForLCResults = standardTerms;
    }

	//special terms
  	else if (bmi>=17 && bmi<19 && (age>=18)) //special terms
    {
  		loadings = 0.25;
    }     	
    //start special terms age>=18 && age<45
  	//bmi==17 && bmi<19
    else if ( (bmi>=34 && bmi<36) && (age>=18 && age<45) ) 
    {
    	loadings = 0.5;
    }
    else if ( (bmi>=36 && bmi<39) && (age>=18 && age<45) ) 
    {
    	loadings = 0.75;
    }
    else if ( (bmi>=39 && bmi<42) && (age>=18 && age<45) ) 
    {
    	loadings = 1.5;
    }        
    else if ( (bmi>=42 && bmi<45) && (age>=18 && age<45) ) 
    {
    	loadings = 2.5;
    }        
    //end special terms age>=18 && age<45
    
    //start special terms age>45
    else if ( (bmi>=34 && bmi<39) && (age>44) ) 
    {
		if(gender == 'Male')
		{
			loadings = 0.5;
		}
		else
		{
			loadings = 0.25;
		}
    }

    else if ( (bmi>=39 && bmi<42) && (age>44) ) 
    {
		if(gender == 'Male')
		{
			loadings = 1;
		}
		else
		{
			loadings = 0.75;
		}           
    }        
    else if ( (bmi>=42 && bmi<45) && (age>44) ) 
    {
		if(gender == 'Male')
		{
			loadings = 1.5;
		}
		else
		{
			loadings = 1.25;
		}          
    }        
    //end special terms age>=18 && age>45        
    
    //bmi >45
    else if (bmi>=45) 
    {
    	bmiForLCResults = declined;
    }

    else 
    {
        //not covered!!!
    }
    loadings = 0;
    if(loadings > 0)// only if special terms
    {
    	bmiForLCResults = loadings;
    }

    return bmiForLCResults;
}

//manipulate BMI for Serious illness Cover, results depends on bmin, age, and gemder
function doBMIForSICUsingCriteria1(bmi, gender, age)
{
   var loadings = 0;	
   var bmiForSICResults = '';

   var declined = "declined";
   var standardTerms = "standardTerms";
   var specialTerms = "specialTerms";

	//bmi<15
    if (bmi<15) 
    {
    	bmiForSICResults = declined;
    }

    //bmi==15 && bmi<17
    else if (bmi>=15 && bmi<17) 
    {
    	bmiForSICResults = specialTerms;
    }	    
    //bmi==19 && bmi<29 || bmi==29 && bmi<31 
    else if (bmi>=19 && bmi<31) 
    {
          bmiForSICResults = standardTerms;
    }
	
	//special terms
  	//bmi==17 && bmi<19
  	else if (bmi>=17 && bmi<19 && (age>=18)) //special terms
    {
  		loadings = 0.25;
    }  
	//start special terms age>=18 && age<45
    else if ( (bmi>=31 && bmi<33) && (age>=18 && age<45) ) 
    {
    	loadings = 0.5;
    }    	
    else if ( (bmi>=33 && bmi<36) && (age>=18 && age<45) ) 
    {
    	loadings = 0.75;
    }
    else if ( (bmi>=36 && bmi<39) && (age>=18 && age<45) ) 
    {
    	loadings = 1.25;
    }        
    //end special terms age>=18 && age<45
    //start special terms age>45
    else if ( (bmi>=31 && bmi<33) && (age>44) ) 
    {
		if(gender == 'Male')
		{
			loadings = 0.25;
		}
		else
		{
			bmiForSICResults = standardTerms;
		}
    }

    else if ( (bmi>=33 && bmi<36) && (age>44) ) 
    {
		if(gender == 'Male')
		{
			loadings = 0.5;
		}
		else
		{
			loadings = 0.25;
		}           
    }        
    else if ( (bmi>=36 && bmi<39) && (age>44) ) 
    {
		if(gender == 'Male')
		{
			loadings = 1;
		}
		else
		{
			loadings = 0.75;
		}          
    }
    else if ( (bmi>=39 && bmi<42) && (age>44) ) 
    {
		if(gender == 'Male')
		{
			loadings = 1.5;
		}
		else
		{
			loadings = 1.25;
		}          
    } 
    //end special terms age>=18 && age>45        

    //bmi between 39 && <42
    else if ((bmi>=39 && bmi<42) && (age>=18 && age<45))
    {
    	bmiForSICResults = declined;               
    }        

    //bmi >45
    if (bmi>=42) 
    {
    	bmiForSICResults = declined;
    }

    else 
    {
        //not covered!!!
    }

    if(loadings > 0)// only if special terms
    {
    	bmiForSICResults = loadings;
    }       
    return bmiForSICResults;  
}

//manipulate BMI for life - using criteria 2(unnamed), results depends on bmin, age, and gender
function doBMIForLCUsingCriteria2(bmi, gender, age)
{
    var loadings = 0;
        
    //bmi>=25 && bmi<35
    if (bmi>=25 && bmi<35)
    {
    	loadings = 0.25;               
    }
    else if(bmi>34)
    {
    	loadings = 1;
    }            
    else
    {
    	//bmi<25, not in the spec
    }
    loadings = 0;
    return loadings;//loading doesn't apply for LC
}

//manipulate BMI for SIC - using criteria 2(unnamed), results depends on bmin, age, and gender
function doBMIForSICUsingCriteria2(bmi, gender, age)
{
    var loadings = 0;
        
    //bmi>=25 && bmi<35
    if (bmi>=25 && bmi<35)
    {
          if (age<=45)//bmi>=25 && bmi<35 and age<46
          {
        	  loadings = 0.5;
          }
          else 
          {
        	  loadings = 0.25;
          }                
    }
    else if(bmi>34 && age<=45)
    {
    	loadings = 1;
    }
    else
    {
    	//bmi<25, not in the spec
    }
    return loadings;
}

//calculate BMI using imperial/metric system and return the value
function calculateBmi(measurementSystemType, heightInCm, heightInFeets, heightInInches, weightInKgs, weightInStones, weightInPounds) 
{
      var bmi = 0;
      if ( measurementSystemType == 'metric')
      {
          	var heightInMeters = heightInCm/100;
            bmi = weightInKgs/(heightInMeters*heightInMeters);
      }
      else 
      {
    	  	var height = heightInFeets*12 + eval(heightInInches);
			var weight = weightInStones*14 + eval(weightInPounds);
            bmi = weight/(height*height)*703;
      }

      return bmi;
} 

//round number to numOfDec places
function roundNumber(myNum, numOfDec) 
{ 
      var decimal = 1;
      for(i=1; i<=numOfDec;i++) 
      decimal = decimal *12; 

      var myFormattedNum = (Math.round(myNum * decimal)/decimal).toFixed(numOfDec); 
      return(myFormattedNum);
} 	

function validateFields(measurementSystem, age, height, weight, feet, inch, stone, pound, lc, sic)
{	    	
    var errorMessage = '';   

    if(age == '') 
	{
		errorMessage += "Please enter Age.<br>";
	}
	if(measurementSystem == 'metric')
	{
		if(height == '') 
		{
			errorMessage += "Please enter Height.<br>"; 
		}
		if(weight == '') 
		{
			errorMessage += "Please enter Weight.<br>"; 
		}			
	}
	else
	{
		if(feet == '') 
		{
			errorMessage += "Please enter Height.<br>"; 
		}
		if(inch == '') 
		{
			errorMessage += "Please enter Height in inches.<br>"; 
		}

		if(stone == '') 
		{
			errorMessage += "Please enter Weight in stones.<br>"; 
		}
		if(pound == '') 
		{
			errorMessage += "Please enter Weight.<br>"; 
		}
	}
	return errorMessage ;
}

function startDateOnlyIntegerExtended()
{
	// allow numbers and backspace and delete
	if ((event.keyCode < 48 && event.keyCode != 8) || (event.keyCode > 57 && event.keyCode != 127))
	{
		event.returnValue = false;
	}
}
