// JavaScript Document

/* global variables, etc.  */
baseCost0 = new Array(55,50,45);
baseCost1 = new Array(63,56,49);

function helpWin(strTitle,strText,strHeight,strWidth)
{
   //poppedWin = window.open("","pWin","alwaysRaised=yes,toolbar=no,scrollbars=yes,height=" + strHeight + ",width=" + strWidth);
   poppedWin = window.open("","pWin","alwaysRaised=yes,toolbar=no,scrollbars=yes,height=" + strHeight + ",width=" + strWidth);
   poppedWin.document.write("<html><head><link rel='stylesheet' href='/css/default.css'>");
   poppedWin.document.write("<title>Help: " + strTitle + "</title></head><body>");
   poppedWin.document.write("<div class=helpText>" + strText + "</div><br><center><form>");
   poppedWin.document.write("<input type=button class=generalButton value='OK' onClick='self.close();'></form>");
   poppedWin.document.write("</center></body></html>");
   poppedWin.focus();
   return true;
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}
     
function changeamount(thisform) {
	var cc = new Array();
	cc = thisform.id.split('_');
	cc = cc[1];
	if (document.getElementById('amount'+cc)) {
		var amount =  parseFloat(thisform.pch_hndl_fee.value) +  parseFloat(thisform.premium.value) + parseFloat(thisform.iapremium.value);
		document.getElementById('amount'+cc).innerHTML= "$"+amount;
	} else {
		var amount =  5 +  parseFloat(document.getElementById('p_premium').innerHTML.replace('$','')) + parseFloat(thisform.iapremium.value);
		document.getElementById('amount').innerHTML= "$"+amount;
		document.getElementById('tot').value = amount;
	}
}

function iachangeamount(thisform) {
	
	var btn = valButton(thisform.ia);
	if (btn == 1) {
		thisform.iapremium.value = 29.95;
		changeamount(thisform);
	}
	else {
		thisform.iapremium.value = 0;
		changeamount(thisform);
	}
}

function iachangeamount2(thisform) {
	if (thisform.ia.checked) {
		thisform.iapremium.value = 29.95;
		changeamount(thisform);
	}
	else {
		thisform.iapremium.value = 0;
		changeamount(thisform);
	}
}
function updatePremium(thisForm)
{ 
	var cc = new Array();
	cc = thisForm.id.split('_');
	cc = cc[1];

   sel_ind_cov = thisForm.coverage.selectedIndex;
   sel_ind_ded = thisForm.deductible.selectedIndex;
   cov_amt = thisForm.coverage.options[sel_ind_cov].value;
   ded_amt = thisForm.deductible.options[sel_ind_ded].value;      
   iCoverage = parseInt(cov_amt,10);
   iDeductible = parseInt(ded_amt,10);


   cov_type = "rc";

   cost = calcCost(cov_type,iCoverage,iDeductible);
   thisForm.premium.value = cost - 5;

   ia_cost = parseFloat(thisForm.iapremium.value);
   if (thisForm.pch_hndl_fee)
   { h_fee = parseFloat(thisForm.pch_hndl_fee.value);
   } else h_fee = 5;
  
	t_cost = cost - 5 + h_fee + ia_cost;
	if (document.getElementById('p_premium'))
	{document.getElementById('p_premium').innerHTML= "$"+thisForm.premium.value;
	}
	if (document.getElementById('sp_premium'))
	{document.getElementById('sp_premium').innerHTML= "$"+thisForm.premium.value;
	}
	if (document.getElementById('amount'+cc))
	{document.getElementById('amount'+cc).innerHTML= "$"+t_cost;
	}
	
}

/*************************************************************************
 Function calcCost ( string covType, int coverage, int deductible )
 Calculate the cost based on coverage type, coverage amount, and 
 deductible. 
*************************************************************************/
function calcCost(sCovType,iCoverage,iDeductible){
   switch(iDeductible){
      case 25:  switch(sCovType){
	           case "acv":
		   	cost = ( (iCoverage / 1000) * 2 ) * 9 ;
       		   	break;

	           case "rc":
		             baseCost = baseCost0[0];
		       	     baseSub = 2000;



			if (iCoverage <= 2500) {
		   		cost = baseCost + 16 * ( (iCoverage - baseSub ) / 1000 );
			} else {
				cost = baseCost + 8 + 24 * ( (iCoverage - 2500 ) / 1000 );
			}
		   	break;

		     default: cost = 0;
		}
		break;

      case 50:  switch(sCovType){
		     case "acv":
			cost = (( (iCoverage / 1000) * 2 ) * 9 ) - 5 ;
       		       	break;

		     case "rc":

			  baseCost = baseCost0[1];
			  baseSub = 2000;

			if (iCoverage <= 2500) {
		   		cost = baseCost + 16 * ( (iCoverage - baseSub ) / 1000 );
			} else {
				cost = baseCost + 8 + 24 * ( (iCoverage - 2500 ) / 1000 );
			}
			break;

		     default: cost = 0;
		}
		break;

      case 100: switch(sCovType){
		     case "acv":
			cost = (( (iCoverage / 1000) * 2 ) * 9 ) - 10 ;
       		       	break;

		     case "rc":
			  baseCost = baseCost0[2];
			  baseSub = 2000;

			if (iCoverage <= 2500) {
		   		cost = baseCost + 16 * ( (iCoverage - baseSub ) / 1000 );
			} else {
				cost = baseCost + 8 + 24 * ( (iCoverage - 2500 ) / 1000 );
			}
			break;

		     default: cost = 0;
		}
		break;

      default: cost = 0;
  }

cost += 5;
cost = Math.round(cost);
  /* return the cost  */
  return cost;
}


function getQuote(thisForm){
   amtVal = thisForm.insureAmt.value.replace(/\$/gi,"");  
   amtVal = amtVal.replace(/-/gi,"");
   amtVal = amtVal.replace(/,/gi,"");
   if (amtVal == "" ){
     return;	
   }
	
   if ( isNaN(amtVal) ){
     alert ("Invalid amount entered--please try again.");
     return;
   }
	
   amtVal = parseInt(amtVal,10);
	
   if ( thisForm.deductible[0].checked ){
     dedVal = parseInt(thisForm.deductible[0].value,10);
   }
   else if ( thisForm.deductible[1].checked ){
     dedVal = parseInt(thisForm.deductible[1].value,10);
   }
   else {
     dedVal = parseInt(thisForm.deductible[2].value,10);
   }


      covType = "rc" ;
 
   
   if ( amtVal <= 1500 ) {
     polAmt = 2000;
   }
   else{
      polAmt = parseInt((amtVal),10);
      if ( polAmt > 20000) polAmt = 20000;
      while ( (polAmt % 500) != 0 ){  
	    if (polAmt > 20000 ){
	      polAmt = 20000;
	      break;
	    }
	    polAmt += 1;
      }     
   }
   cost = 0;
   cost = calcCost(covType,polAmt,dedVal);

   thisForm.policyAmount.value = "$" + polAmt;
   thisForm.quoteText.value = "$" + cost;

   numQuote++;

  // quoteImage = new Image();
   //quoteImage.src = "quote?numQuote=" + numQuote;
}


function populateDay(monthSel,daySel)
{
   sel_ind = monthSel.selectedIndex;
   sel_month = monthSel.options[sel_ind].value;


   switch (sel_month) {
   case "01": case "1":  
   case "03": case "3": 
   case "05": case "5": 
   case "07": case "7":
   case "08": case "8":
   case "10": 
   case "12":
	 daySel.length = 31;
	 for (i=1;i<=daySel.length;i++) {
	    daySel.options[(i-1)] = new Option(i.toString(),i.toString());
	 }
	 break;

   case "02": case "2": daySel.length = 28;
	 for (i=1;i<=daySel.length;i++) {
	    daySel.options[(i-1)] = new Option(i.toString(),i.toString());
	 }
	 break;

   default: daySel.length = 30;
       for (i=1;i<=daySel.length;i++) {
	  daySel.options[(i-1)] = new Option(i.toString(),i.toString());
       }
       break;

   }
}



function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
	offset = document.cookie.indexOf(search)
	if (offset != -1) { // if the cookie exists
	    offset += search.length
	    end = document.cookie.indexOf(";", offset); // set the index of beginning value
	    if (end == -1) // set the index of the end of cookie value
	    end = document.cookie.length;
	    returnvalue=unescape(document.cookie.substring(offset, end))
	}
    }
    return returnvalue;
}

function doPopUnder(URL,width,height)
{
    var p = 'scrollbars=yes,resizable=yes,toolbar=no,' 
	  + 'menubar=no,status=yes,location=no,left=85,top=20,height=' 
	  + height + ',width=' + width;

    win2=window.open(URL,"bw",p);
    win2.blur();
    window.focus();
}

function updatePremium2(thisForm)
{ 

   sel_ind_cov = thisForm.coverage.selectedIndex;
   sel_ind_ded = thisForm.deductible.selectedIndex;
   cov_amt = thisForm.coverage.options[sel_ind_cov].value;
   ded_amt = thisForm.deductible.options[sel_ind_ded].value;      
   iCoverage = parseInt(cov_amt,10);
   iDeductible = parseInt(ded_amt,10);


   cost = calcCost2(iCoverage,iDeductible);
   thisForm.premium.value = cost - 5;
   if (thisForm.pch_hndl_fee)
   { h_fee = parseFloat(thisForm.pch_hndl_fee.value);
   } else h_fee = 5;
  
	t_cost = cost - 5 + h_fee;
	if (document.getElementById('p_premium'))
	{document.getElementById('p_premium').innerHTML= "$"+cost;
	}
	if (document.getElementById('sp_premium'))
	{document.getElementById('sp_premium').innerHTML= "$"+thisForm.premium.value;
	}
	if (document.getElementById('amount'))
	{document.getElementById('amount').innerHTML= "$"+t_cost;
	}
	
}

/*************************************************************************
 Function calcCost ( string covType, int coverage, int deductible )
 Calculate the cost based on coverage type, coverage amount, and 
 deductible. 
*************************************************************************/
function calcCost2(iCoverage,iDeductible){
   switch(iDeductible){
      case 25: 
		   	cost = ( (iCoverage / 1000) * 2 ) * 7 + 34;
       		   	break;
      case 50:  
			cost = (( (iCoverage / 1000) * 2 ) * 7 ) + 27 ;
       		       	break;

      default: cost = 0;
  }

cost += 5;
  /* return the cost  */
  return cost;
}
