/**
 * @author minh
 */
var keysPressed = [];

var errColor = "#FF8888";
var normColor = "#F6F6F6";

//Some min and max values for fields

function initialize_selects(){

}

function clear_rates(){
	document.getElementById("rates_wrapper").innerHTML = "&nbsp;";
//document.getElementById(id).style.visibility = "visible";
}
	
function crosscheck_form(element)
{

	var productType = document.getElementById('producttype');
	var tableDetails = document.getElementById('life_insured_details');
	var tableLife = document.getElementById('life_cover');
	var tableLifeTPD = document.getElementById('life_cover_tpd');
	var tableLifeTrauma = document.getElementById('life_cover_trauma');
	var tableLifeTPDTrauma = document.getElementById('life_cover_tpd_trauma');
	var tableSATPD = document.getElementById('SAtpd');
	var tableSATrauma = document.getElementById('SAtrauma');
	var tableIP = document.getElementById('ip');
	var tableBE = document.getElementById('be');
	var tableResults = document.getElementById('results');
	var tableNotes = document.getElementById('results_notes');
	var tableOp = document.getElementById('results_op');
	var tablePDF = document.getElementById('summary_pdf');	
	var cellSATrauma = document.getElementById('SAT');
	var Child1 = document.getElementById('Child1');
	var Child2 = document.getElementById('Child2');
	
	var Submit = document.getElementById('submitSection');
	var SATName = document.getElementById('SATName');
	tableDetails.style.display = 'none';
	tableLife.style.display = 'none';
	tableLifeTPD.style.display = 'none';
	tableLifeTrauma.style.display = 'none';
	tableLifeTPDTrauma.style.display = 'none';
	tableSATPD.style.display = 'none';
	tableSATrauma.style.display = 'none';
	tableIP.style.display = 'none';
    tableBE.style.display = 'none';
    Child1.style.display = 'none';
    Child2.style.display = 'none';
    Submit.style.display = 'none';
    
     
    if(productType.value != 0){
   		tableDetails.style.display = 'block';
   		    Submit.style.display = 'block';
    }
    if(productType.value == 'Life Cover'){
		tableLife.style.display = 'block';			
	}else if(productType.value == 'Life Cover and TPD Any Occupation' || productType.value == 'Life Cover and TPD Own Occupation'){
		tableLifeTPD.style.display = 'block';			
	}else if(productType.value == 'Stand Alone TPD Any Occupation' || productType.value == 'Stand Alone TPD Own Occupation'){
		tableSATPD.style.display = 'block';			
	}else if(productType.value == 'Life Cover and Trauma Standard'){
		tableLifeTrauma.style.display = 'block';			
	}else if(productType.value == 'Life Cover, TPD Own Occupation and Trauma Standard' || productType.value == 'Life Cover, TPD Any Occupation and Trauma Standard'){
		tableLifeTPDTrauma.style.display = 'block';			
	}else if(productType.value == 'Life Cover, TPD Own Occupation, Trauma Standard and Trauma Partial' || productType.value == 'Life Cover, TPD Any Occupation, Trauma Standard and Trauma Partial'){		
		tableLifeTPDTrauma.style.display = 'block';					
	}else if(productType.value == 'Stand Alone Trauma Standard and Trauma Partial' ||productType.value == 'Stand Alone Trauma Standard'){
		tableSATrauma.style.display = 'block';			
					    cellSATrauma.style.display = 'block';	
					    		    	    SATName.innerHTML = ' SA Trauma';
	}else if(productType.value == 'Income Protection Comprehensive - Agreed Value' || productType.value == 'Income Protection Comprehensive - Indemnity' || productType.value == 'Income Protection Standard - Agreed Value' || productType.value == 'Income Protection Standard - Indemnity'){
		tableIP.style.display = 'block';			
	}else if(productType.value == 'Business Expenses' || productType.value == 'Business Expenses Platinum'){
		tableBE.style.display = 'block';			
	}else if(productType.value == 'Income Protection and Business Expenses Cover'){
		tableIP.style.display = 'block';			
		tableBE.style.display = 'block';			
	}else if(productType.value == 'Overall Package'){
		tableLifeTPDTrauma.style.display = 'block';			
				tableIP.style.display = 'block';			
		tableBE.style.display = 'block';			
	}else if(productType.value == 'Trauma and TPD Cover'){
		tableLife.style.display = 'block';			
	}else if(productType.value == 'Stand Alone Trauma Standard and Child Cover (2 children)'){
		tableSATrauma.style.display = 'block';		
			    cellSATrauma.style.display = 'block';	
		    Child1.style.display = 'block';
		    Child2.style.display = 'block';
		    	    SATName.innerHTML = ' SA Trauma';
	}else if(productType.value == 'Stand Alone Trauma Standard and Child Cover'){
		tableSATrauma.style.display = 'block';			
			    cellSATrauma.style.display = 'block';
				    Child1.style.display = 'block';
				    		    	    SATName.innerHTML = ' SA Trauma';
	}else if(productType.value == 'Child Cover'){
		tableSATrauma.style.display = 'block';			
	    cellSATrauma.style.display = 'none';
	    Child1.style.display = 'block';
	    Child2.style.display = 'block';
	    SATName.innerHTML = ' Child Cover';
	}
	if(element == null || element.name == 'producttype'){
	    //tableResults.style.display = 'none';
	    //tableOp.style.display = 'none';
	    //tableNotes.style.display = 'none';
	    //tablePDF.style.display = 'none';
	}
//    	document.getElementById("summary_cell").innerHTML = "<a id='summary_pdf' class='pdf_link' target='_blank' href='../pdfreport/report.php?idpdf=1'>View PDF</a>";


}

function disableSTGtextbox(tb) {
	tb.disabled=true;
	tb.style.borderColor = "#CCCCCC";
	tb.style.color = "#BBBBBB";
}

function loadPDF(){
	var ratesForm = document.getElementById("ratesviewer");
	document.getElementById("create_pdf").value = '1';
	document.getElementById("submit").click();
	document.getElementById("create_pdf").value = '0';
}

function getSelectOptions(){
    var idArray = [ ["life_age_next","AgeNext"], 
    				["life_occupation","TypeOccupation"], 
    				["life_cover_amount","LifeCoverAmount"],
    				["lifeTPD_life_cover_amount","LifeCoverAmount"],
    				["lifeTrauma_life_cover_amount","LifeCoverAmount"],
    				["lifeTPDTrauma_life_cover_amount","LifeCoverAmount"],
    				["lifeTPD_tpd_cover_amount","TPDRiderCoverAmount"],
    				["lifeTPDTrauma_tpd_cover_amount","TPDRiderCoverAmount"],
    				["lifeTrauma_trauma_cover_amount","TraumaRiderCoverAmount"],
    				["lifeTPDTrauma_trauma_cover_amount","TraumaRiderCoverAmount"],
    				["TPD_tpd_cover_amount","TPDStandAloneCoverAmount"],
    				["Trauma_trauma_cover_amount","TraumaStandAloneCoverAmount"],
    				["Trauma_Child1_Amount","Child1CoverAmount"],
    				["Trauma_Child2_Amount","Child2CoverAmount"],
    				["ip_monthly_benefit","IncomeProtectionCoverAmount"],
    				["be_monthly_benefit","BusinessExpensesCoverAmount"] 
    				    				];
    
    for (var i=0; i< idArray.length; i++){
        selectHTML = "<select id='"+idArray[i][0]+"' name='"+idArray[i][0]+"' >"+ "<option>loading...</option>"+ "</select>";
			   document.getElementById(idArray[i][0]+"_wrapper").innerHTML = selectHTML;
    }
     for (var i=0; i< idArray.length; i++){
	    var d = doSimpleXMLHttpRequest("../_lib/_base/getRatesViewerOptions.php", { 'type' : document.getElementById("producttype").value, 'dbfield' : idArray[i][1], 'frmfield' : idArray[i][0] } );
	    d.addCallback(handleResponse);
    }
}
    function handleResponse(res){
    	var r = res.responseText;
    	var aResult = new Array();
        r = r.replace(/\<\?xml version=\"1.0\" \?\>\n\<response\>/,"");
        r = r.replace(/<\/response>/,"");
        aResult = r.split("||",2);

        addSelectOptions(aResult[0],aResult[1]);
    }
    
function addSelectOptions(id,options){
    wid = id+"_wrapper";
    if (id=='life_occupation'){
		selectHTML = "<select id='"+id+"' name='"+id+"' onchange='clear_rates();getWBPeriod();'>"+ options	+ "</select>";
		document.getElementById(wid).innerHTML = selectHTML;
	    document.getElementById(id).style.visibility = "visible";
		document.getElementById(id).selectedIndex = 0;
		getWBPeriod();
	}else {
		selectHTML = "<select id='"+id+"' name='"+id+"' onchange='clear_rates();'>"+ options	+ "</select>";
   		document.getElementById(wid).innerHTML = selectHTML;
    	document.getElementById(id).style.visibility = "visible";
   		document.getElementById(id).selectedIndex = 0;
	}
}


function getRatesTable(dopdf){
	producttype=document.getElementById("producttype").value;
	life_age_next=document.getElementById("life_age_next").value;
	life_sex= document.getElementById("life_sex").value;
	life_smoker= document.getElementById("life_smoker").value;
	life_state=document.getElementById("life_state").value;
	life_occupation=document.getElementById("life_occupation").value;
	var params="producttype="+producttype+"&life_age_next="+life_age_next+"&life_sex="+life_sex+"&life_smoker="+life_smoker+"&life_state="+life_state+"&life_occupation="+life_occupation+"";
	
	if (producttype=="Life Cover"){
    	params=params+"&life_cover_premium_type="+document.getElementById('life_cover_premium_type').value+"&life_cover_amount="+document.getElementById('life_cover_amount').value+"";
	
	}else if (producttype=="Life Cover and TPD Any Occupation" ||  producttype=="Life Cover and TPD Own Occupation"){
    	params=params+'&lifeTPD_premium_type=' + document.getElementById("lifeTPD_premium_type").value + '&lifeTPD_life_cover_amount=' + document.getElementById("lifeTPD_life_cover_amount").value+ '&lifeTPD_tpd_cover_amount=' + document.getElementById("lifeTPD_tpd_cover_amount").value+'';
	
	}else if (producttype=="Stand Alone TPD Any Occupation" ||  producttype=="Stand Alone TPD Own Occupation"){
    	params=params+'&TPD_premium_type=' + document.getElementById("TPD_premium_type").value + '&TPD_tpd_cover_amount=' + document.getElementById("TPD_tpd_cover_amount").value+ '';
	
	}else if (producttype=="Life Cover and Trauma Standard"){
    	params=params+'&lifeTrauma_premium_type=' + document.getElementById("lifeTrauma_premium_type").value + '&lifeTrauma_life_cover_amount=' + document.getElementById("lifeTrauma_life_cover_amount").value+'&lifeTrauma_trauma_cover_amount=' + document.getElementById("lifeTrauma_trauma_cover_amount").value+ '';
	
	}else if (producttype=="Life Cover, TPD Any Occupation and Trauma Standard"){
    	params=params+'&lifeTPDTrauma_premium_type=' + document.getElementById("lifeTPDTrauma_premium_type").value + '&lifeTPDTrauma_life_cover_amount=' + document.getElementById("lifeTPDTrauma_life_cover_amount").value+'&lifeTPDTrauma_trauma_cover_amount=' + document.getElementById("lifeTPDTrauma_trauma_cover_amount").value+ '&lifeTPDTrauma_tpd_cover_amount=' + document.getElementById("lifeTPDTrauma_tpd_cover_amount").value+'';
	
	}else if (producttype=="Stand Alone Trauma Standard" || producttype=="Stand Alone Trauma Standard and Trauma Partial" ){
    	params=params+'&Trauma_premium_type=' + document.getElementById("Trauma_premium_type").value + '&Trauma_trauma_cover_amount=' + document.getElementById("Trauma_trauma_cover_amount").value+'';
	
	}else if (producttype=="Child Cover"){
    	params=params+'&Trauma_premium_type=' + document.getElementById("Trauma_premium_type").value+ '&Trauma_Child1_Amount=' + document.getElementById("Trauma_Child1_Amount").value+'&Trauma_Child2_Amount=' + document.getElementById("Trauma_Child2_Amount").value+'';
	
	}else if (producttype=="Income Protection Standard - Agreed Value" || producttype=="Income Protection Comprehensive - Indemnity" || producttype=="Income Protection Standard - Agreed Value" || producttype=="Income Protection Standard - Indemnity" || producttype== "Income Protection Comprehensive - Agreed Value"){
    	params=params+'&IP_premium_type=' + document.getElementById("IP_premium_type").value + '&ip_monthly_benefit=' + document.getElementById("ip_monthly_benefit").value+'&ip_waiting_period=' + document.getElementById("ip_waiting_period").value+'&ip_benefit_period=' + document.getElementById("ip_benefit_period").value+'';
	
	}else if (producttype=="Business Expenses" || producttype=="Business Expenses Platinum"){
    	params=params+'&be_premium_type=' + document.getElementById("be_premium_type").value+ '&be_monthly_benefit=' + document.getElementById("be_monthly_benefit").value+'&be_waiting_period=' + document.getElementById("be_waiting_period").value+'';
	}else if (producttype=="Stand Alone Trauma Standard and Child Cover (2 children)"){
    	params=params+'&Trauma_premium_type=' + document.getElementById("Trauma_premium_type").value+ '&Trauma_trauma_cover_amount=' + document.getElementById("Trauma_trauma_cover_amount").value+'&Trauma_Child1_Amount=' + document.getElementById("Trauma_Child1_Amount").value+'&Trauma_Child2_Amount=' + document.getElementById("Trauma_Child2_Amount").value+'';
	}
	   
   if (dopdf==0){
   	document.getElementById("rates_wrapper").innerHTML = "<p>Loading...</p>";
   	phplink="getRatesViewerReport.php?"+params+"&dopdf=0";
   		 var d = doSimpleXMLHttpRequest(phplink);
   		 d.addCallback(handleRatesResponse);
   }else {
   	//document.getElementById("rates_wrapper").innerHTML = "<p>Loading report...</p>";
  	params=params+"&external="+document.getElementById("external").value+"&dopdf=1";
   	phplink="getRatesViewerReport.php?"+params+"";
   	//document.location=phplink;
   	 window.open(phplink);
   }
 }

    function handleRatesResponse(res){
    	var r = res.responseText;
    	var aResult = new Array();
        r = r.replace(/\<\?xml version=\"1.0\" \?\>\n\<response\>/,"");
        r = r.replace(/<\/response>/,"");
        document.getElementById("rates_wrapper").innerHTML = r;
    } 
    
    function getWBPeriod(){
    	producttype=document.getElementById("producttype").value;
    	if (producttype=="Income Protection Standard - Agreed Value" || producttype=="Income Protection Comprehensive - Indemnity" || producttype=="Income Protection Standard - Agreed Value" || producttype=="Income Protection Standard - Indemnity" || producttype== "Income Protection Comprehensive - Agreed Value" ){
    		var idArray = [["ip_waiting_period", "WaitingPeriod"],
    				   ["ip_benefit_period", "BenefitPeriod"]
    						];
    		for (var i=0; i< idArray.length; i++){
        		selectHTML = "<select id='"+idArray[i][0]+"' name='"+idArray[i][0]+"' >"+ "<option>loading...</option>"+ "</select>";
			   	document.getElementById(idArray[i][0]+"_wrapper").innerHTML = selectHTML;
    		}
     		for (var i=0; i< idArray.length; i++){
	    		var d = doSimpleXMLHttpRequest("../_lib/_base/getRatesViewerOptions.php", { 'type' : document.getElementById("life_occupation").value, 'dbfield' : idArray[i][1], 'frmfield' : idArray[i][0] } );
	    		d.addCallback(handleResponse);
    		}
    	} else if (producttype='Business Expenses') {
    			selectHTML = "<select id='be_waiting_period' name='be_waiting_period' >"+ "<option>loading...</option>"+ "</select>";
			   	document.getElementById("be_waiting_period_wrapper").innerHTML = selectHTML;
    			var d = doSimpleXMLHttpRequest("../_lib/_base/getRatesViewerOptions.php", { 'type' : document.getElementById("life_occupation").value, 'dbfield' : 'WaitingPeriodBEX', 'frmfield' : 'be_waiting_period' } );
	    		d.addCallback(handleResponse);
    		}
    }