function submitenter(myfield,e)
			{
			var keycode;
			if (window.event) keycode = window.event.keyCode;
			else if (e) keycode = e.which;
			else return true;
			
			if (keycode == 13)
			   {
			   myfield.form.submit();
			   return false;
			   }
			else
			   return true;
			}
			
			
			
function RotateImages(whichHolder,Start)
  {	 
	var a = eval("ImageArr"+whichHolder);				
	b = document.getElementById('t_topright_imgrotate');
	if(Start>=a.length)
		Start=0;
	b.src = '../assets/'+a[Start];
	window.setTimeout("RotateImages("+whichHolder+","+(Start+1)+")",5000);
  }
  
  function adjustpageheight(div) {
		var div, divtop,divheight,heightreq;
		div = document.getElementById(div);
		divtop = div.offsetTop;
		divheight = div.offsetHeight;
		heightreq = divtop + divheight+150;
		if (heightreq<726) {heightreq=726};
		document.getElementById('maincolumn').style.height = heightreq+'px';
		document.getElementById('t_leftline').style.height = (heightreq-300)+'px';
		document.getElementById('t_leftline').style.height = (heightreq-300)+'px';
		document.getElementById('t_bottomline').style.top = (heightreq-99)+'px';
		document.getElementById('t_footer1').style.top = (heightreq-81)+'px';
		document.getElementById('t_footer2').style.top = (heightreq-81)+'px';
		document.getElementById('t_footer3').style.top = (heightreq-81)+'px';
		document.getElementById('t_eagle_logo').style.top = (heightreq-41)+'px';
		document.getElementById('t_footertext').style.top = (heightreq-36)+'px';
		document.getElementById('t_bottomstrip').style.top = (heightreq-11)+'px';
		document.getElementById('pageframe').style.visibility = 'visible';
  }
  
  function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
  
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function filterNum(str) {
re = /^\$|,/g;
// remove "$" and ","
return str.replace(re, "");
}

function checkEnter(event)
   {
   var keynum;
   var keychar;
   var enttest;
 
   if (window.event)
      {
      keynum = event.keyCode;
      }
   else
      {
      if (event.which)
         {
         keynum = event.which;
         }
      }
	  
   if (keynum == 13)
      {
      event.srcElement.blur();
      return false;
      }
   }
  
  function CalcOwners() {
	var policy, premium, endorsetotal, total;
	policy = filterNum(document.getElementById("txtT1").value);
	if (policy >= 25000000) {premium = (85109 + ((policy - 25000000) * 0.00154)); }
	else if (policy >= 15000000) {premium = (59409 + ((policy - 15000000) * 0.00257)); }
	else if (policy >= 5000000) {premium = (23209 + ((policy - 5000000) * 0.00362)); }
	else if (policy >= 1000001) {premium = (5649 + ((policy - 1000000) * 0.00439)); }
	else {premium = (843 + ((policy - 100000) * 0.00534)); }
	
	endorsetotal = computeEndorseTotal(premium);
	total = premium+endorsetotal;

	if(policy!='') {
		document.getElementById("txtPrem1").value = addCommas(CurrencyFormatted(premium.toFixed(0)));
		document.getElementById("ownerendorse").value = addCommas(CurrencyFormatted(endorsetotal.toFixed(2)));
		document.getElementById("ownertotal").value = addCommas(CurrencyFormatted(total.toFixed(0)));
	   }
	else {
		document.getElementById("txtPrem1").value = '';
		document.getElementById("ownerendorse").value = '';
		document.getElementById("ownertotal").value = '';
	}
	
  }
  
  function computeEndorseTotal(premium) {
	var endorsetotal = 0,sel,selname;
	sel = document.getElementById('selOwnerEndorse');
	var optsLength = sel.options.length;
   for(var i=0;i<optsLength;i++){
	 if (sel.options[i].selected)  {
		selname =  sel.options[i].value;
		endorsetotal = endorsetotal + getOwnerSelectEndorseCalc(premium,selname);		 
	 }
  	}
	return endorsetotal;
  }
  
 function getOwnerSelectEndorseCalc(premium,selname) {
	var endorsetotal = 0;
	if (selname =='t4') {endorsetotal = 0;}
	else if (selname =='t191a') {endorsetotal = (premium * 0.15);}
	else if (selname =='t191b') {endorsetotal = (premium * 0.10);}
	else if (selname =='t192') {endorsetotal = 50;}
	else if (selname =='t193') {endorsetotal = 50;}
	else if (selname =='t23') {endorsetotal =  100;}
	else if (selname =='t25') {endorsetotal =  100;}
	else if (selname =='t24a') {endorsetotal = (premium * 0.05);}
	else if (selname =='t24b') {endorsetotal = (premium * 0.15);}
	else if (selname =='t26') {endorsetotal =  (premium * 0.10);}
	return endorsetotal;	 
 }
 
 function getOwnerSelectEndorseLink(selname) {
	var link;
	if (selname =='t4') {link = 'http://www.tdi.state.tx.us/title/titlem4b.html';}
	else if (selname =='t191a') {link = 'http://www.tdi.state.tx.us/title/titlem4l.html';}
	else if (selname =='t191b') {link = 'http://www.tdi.state.tx.us/title/titlem4l.html';}
	else if (selname =='t192') {link = 'http://www.tdi.state.tx.us/title/titlem4l.html';}
	else if (selname =='t193') {link = '';}
	else if (selname =='t23') {link = 'http://www.tdi.state.tx.us/title/titlem4l.html';}
	else if (selname =='t25') {link = 'http://www.tdi.state.tx.us/title/titlem4l.html';}
	else if (selname =='t24a') {link = 'http://www.tdi.state.tx.us/title/titlem4l.html';}
	else if (selname =='t24b') {link = 'http://www.tdi.state.tx.us/title/titlem4b.html';}
	else if (selname =='t26') {link = 'http://www.tdi.state.tx.us/title/titlem4l.html';}
	else {link = 'not found'};
	return link;	 
 }
 
 function viewOwnerEndorsement(selname) {
	alert('Not sure how this is to work.  It was designed to go to the Texas Title Insurance Manual for a single selected Endorsement but as a result of a late change we are offering the opportunity to select multiple endorsements.');
 }
 
 function CalcLoan() {
	var policy, premium,issuepolicy,endorsetotal,total,totalsimultaneous
	policy = filterNum(document.getElementById("txtT2").value);
		if (policy >= 25000000) {premium = (85109 + ((policy - 25000000) * 0.00154)); }
	else if (policy >= 15000000) {premium = (59409 + ((policy - 15000000) * 0.00257)); }
	else if (policy >= 5000000) {premium = (23209 + ((policy - 5000000) * 0.00362)); }
	else if (policy >= 1000001) {premium = (5649 + ((policy - 1000000) * 0.00439)); }
	else {premium = (843 + ((policy - 100000) * 0.00534)); }
	
	endorsetotal = computeEndorseLoanTotal(premium);
	total=premium+endorsetotal;
	totalsimultaneous = endorsetotal + 100;
	
	if(policy!='') {		
		document.getElementById("loanpremium").value = addCommas(CurrencyFormatted(premium.toFixed(0)));
		document.getElementById("policypremium").value = "$100.00";
		document.getElementById("loanendorse").value = addCommas(CurrencyFormatted(endorsetotal.toFixed(2)));
		document.getElementById("loantotal").value = addCommas(CurrencyFormatted(total.toFixed(0)));
		document.getElementById("loansimultaneous").value = addCommas(CurrencyFormatted(totalsimultaneous.toFixed(0)));
	}
	else {
		document.getElementById("loanpremium").value = '';
		document.getElementById("policypremium").value ='';
		document.getElementById("loanendorse").value = '';
		document.getElementById("loantotal").value = '';
		document.getElementById("loansimultaneous").value = '';
	}
 }
 
 function computeEndorseLoanTotal(premium) {
	var endorsetotal = 0,sel,selname;
	sel = document.getElementById('selLoanEndorse');
	var optsLength = sel.options.length;
   for(var i=0;i<optsLength;i++){
	 if (sel.options[i].selected)  {
		selname =  sel.options[i].value;
		endorsetotal = endorsetotal + getLoanSelectEndorseCalc(premium,selname);		 
	 }
  	}
	return endorsetotal;
  }
 
  function getLoanSelectEndorseCalc(premium,selname) {
	var endorsetotal = 0;
	if (selname =='t5') {endorsetotal = 0;}
	else if (selname =='t14') {endorsetotal = 25;}
	else if (selname =='t16') {endorsetotal = 25;}
	else if (selname =='t19') {endorsetotal = (premium * 0.10);}
	else if (selname =='t23') {endorsetotal = 100;}
	else if (selname =='t25') {endorsetotal =  100;}
	else if (selname =='t27') {endorsetotal =  0;}
	else if (selname =='t28') {endorsetotal = 0;}
	else if (selname =='t30a') {endorsetotal = 20;}
	else if (selname =='t30b') {endorsetotal =  5;}
	else if (selname =='t33') {endorsetotal =  20;}
	else if (selname =='t331') {endorsetotal =  20;}
	else if (selname =='t35') {endorsetotal =  50;}
	else if (selname =='t36') {endorsetotal =  25;}
	else if (selname =='t38') {endorsetotal =  100;}
	return endorsetotal;	 
 }
 
 function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + '.' + strCents);
}

function toggleMe(a){
  var e=document.getElementById(a);
  if(e.style.display=="none" || e.style.display==''){
    e.style.display="block"
  } else {
    e.style.display="none"
  }
  adjustpageheight('para1');
}

function displayendorsement(url) {
	if(url!='Select') {
	window.open(url);
	}
}
