function calculateAge(dobID, ageID) {
    var dobObj = document.getElementById(dobID);
    if (!dobObj) { alert('Date of Birth Field Not Found'); return false; }
    var ageObj = document.getElementById(ageID);
    if (!ageObj) { alert('Age Field Not Found'); return false; }
    if (dobObj.value) {
       var dtTo   = new Date();
       var dtFrom = new Date(dobObj.value);
    
       var a = dtTo.getDate() + (dtTo.getMonth() + (dtTo.getFullYear() - 1700) * 16) * 32;
       var b = dtFrom.getDate() + (dtFrom.getMonth() + (dtFrom.getFullYear() - 1700) * 16) * 32;
       var x = Math.floor((a - b) / 32 / 16);
       ageObj.value = x < 0 ? null : x;
       ageObj.style.backgroundcolor = '#dcdcdc';    
       ageObj.style.color = 'gray';    
       }
    else {
       ageObj.style.backgroundcolor = 'white';    
       ageObj.style.color = 'black';    
    }
}

function resetTheForm (formID) {
  var fObj = document.getElementById(formID);
  if (!fObj) {
     alert('Unable to find form to reset');
  } else {
     fObj.reset();
  }
}

function copyToBuffer(id) {
  var obj = document.getElementById(id);
  var val = obj.innerText;
  window.clipboardData.setData('text',val);
}  

clearDivAfter = function(divID,lapse) {
 var obj = getElementById.document(divID);
 setTimeout("obj.innerHTML=''",3000);
// setTimeout("theNewWin.goCommTab()",10000);
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=-1; /* expire cookie */
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function openRegistration (qstr) {
  var url = 'https://secure.annuityratewatch.com/register/index.cfm?'+qstr;
  var x = window.open (url, "_blank", "height=600,width=570,location=no,menubar=no,resizable,scrollbars,status=no,toolbar=no");
  x.focus();
 }
function openPaymentMethod (qstr) {
  var url = 'https://secure.annuityratewatch.com/payMethod/index.cfm?'+qstr;
  var x = window.open (url, "_blank", "height=450,width=650,location=no,menubar=no,resizable,scrollbars,status=no,toolbar=no");
  x.focus();
 }

var caseState = '';
var caseAge   = '';
var casePrem  = '';
function setCaseFilterValues(state,prem,age) {
  caseState = state;   
  caseAge   = age;
  casePrem  = prem;
  //alert('SetCaseFilterValues '+caseState+' '+caseAge+' '+casePrem);
}

function applyCaseFilterValues(onOff) {
  var obj = document.getElementById('fltPremium');
  // alert('applyCaseFilterValues '+caseState+' '+caseAge+' '+casePrem);
  if (obj) {
     if (onOff) {obj.value=casePrem;} else {obj.value='';}
     } 
  var obj2 = document.getElementById('fltAge'); 
  if (obj2) {
     if (onOff) {obj2.value=caseAge;} else {obj2.value='';}
     } 
  var obj3 = document.getElementById('flatState');
  //if (obj3) {obj.value=caseState;}
}


function setCaseFilter(onOff) {
  applyCaseFilterValues(onOff);
  SetCookie('caseFilter',onOff,30);
  if (onOff == true) {
    alert('The grids will be filtered by the case State, Premium and Age');
  } else {
    alert('The grids will show all products (check box to filtered products by the case values)');
  }
  var obj = document.getElementById('searchButton');
  if (obj) {obj.click()}
}

 
function actSubmitNew(url,act,targ) {
   var obj = document.getElementById("mainForm");
   if (!obj) {
      obj = document.forms["form"];
	  if (!obj) { alert('Could not find form');return false; }
      }
   if (!act) {
      obj.action=url;
	  }
   else {
      obj.action=location.href;
      obj.elements['nexturl'].value=url;
      obj.elements['fact'].value=act;
      }   
   if (targ) { obj.target=targ; }
   // document.form.fireEvent("onsubmit");
   obj.submit();
   return false;
}

function actSubmit(url,act,targ) {
   if (!act) {
      document.form.action=url;
	  }
   else {
      document.form.action=location.href;
      document.form['nexturl'].value=url;
      document.form['fact'].value=act;
      }   
   if (targ) { document.form.target=targ; }
   // document.form.fireEvent("onsubmit");
   document.form.submit();
   return false;
}

function formSubmit(formID,url,act,targ) {
   var fobj = document.getElementById(formID);
   if (!fobj) {alert('formSubmit() Error.  Unable to submit form.  Form not found'); return;}
   if (!act) {
      fobj.action=url;
	  }
   else {
      fobj.action=location.href;
	  if (url && fobj.elements["nexturl"]) { fobj['nexturl'].value=url; }
	  if (act && fobj.elements["fact"]) { fobj['fact'].value=act; }
      }   
   if (targ) { fobj.target=targ; }
   // document.form.fireEvent("onsubmit");
   fobj.submit();
   return false;
}


function rsSubmit(url,act) {
   var tmp;
   if (act && act.length > 0) {
      tmp = act;
	  tmp = tmp.replace("?","&");
	  tmp += '&useRS=1';
      window.parent.callToServer(document.location.pathname + '?fact=' + tmp);
	  }
   if (url && url.length > 0) {
      tmp = url;
	  if (tmp.indexOf('?') == -1) tmp += '?useRS=1'; 
	  else                        tmp += '&useRS=1'; 
      window.parent.callToServer(tmp);
	  }
   return false;
}

function numberRange(obj,fr,to) { 
  var nStr = obj.value.replace(/[^\d\.]+/g, '')
  if (nStr < fr || nStr > to) {
     alert('Value must be between '+fr+' and '+to);
	 obj.select();
	 return false;
	 }
  return true;
}

/* obj is the object of the field element holding the number, dec is the number of decimal places 0 or 2 most common */
function numericVal(numb,dec) { 
  var nStr = numb.toString();
  var dec = !dec ? 0 : dec;
  nStr = nStr.replace(/[^\d\.]+/g, '')
  nStr = Math.round(nStr * Math.pow(10,dec)) / Math.pow(10,dec);
  nStr += '';
  var x = nStr.split('.');
  var x1 = x[0];
  var x2 = x.length > 1 ? '.' + x[1] + '0000000000' : '.0000000000';
  x2 = x2.substring(0, dec==0 ? 0 : (dec+1));
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
	x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}  
/* obj is the object of the field element holding the number, dec is the number of decimal places 0 or 2 most common */
function numeric(obj,dec) { 
  obj.value = numericVal(obj.value,dec);
}

function numericBlank (obj,dec) {
  var x = numericVal(obj.value,dec);
  if (x == 0) obj.value = '';
  else obj.value = x;
}

/* closeTab and changeTab are used to display and hide divs on the page
   The divs do not have to be tabs
   changeTab(obj, number);  -- obj is the object of the 
   */
var tabActiveArray=new Array();
function closeTab(instance) {
  var inst = (!instance ? 1 : instance);
  var tabActiveObj = (inst <= tabActiveArray.length ? tabActiveArray[inst] : '');
     if (tabActiveObj) {
      //tabActiveObj.className='normal';
      tabActiveObj.className= tabActiveObj.className.replace('active','');
	  tabID = tabActiveObj.id;
      pageObj = document.getElementById(tabID + 'Body');
      if (pageObj) { pageObj.style.display='none'; }
      tabActiveArray[inst]='';
     }
  return true;
}
function initTab(tabObj,instance) {
  var tabObj;
  var inst = (!instance ? 1 : instance);
  if (!tabObj) { return false };
  var tabActiveObj = (inst <= tabActiveArray.length ? tabActiveArray[inst] : '');
     if (!tabActiveObj) {
        tabActiveArray[inst]=tabObj;
		}
  return true;
}
function changeTab(tabObj,instance) {
  var tabID=0;
  var pageObj;
  var tabObj;
  var inst = (!instance ? 1 : instance);
  closeTab(inst);
  if (!tabObj) { return false };
  tabID = tabObj.id;
  pageObj = document.getElementById(tabID + 'Body');
  if (!pageObj) { return false; }
  tabObj.className += 'active';
  pageObj.style.display='block';
  tabActiveArray[inst]=tabObj;
  return true;
}
/* this on is like changeTab except if the same tab is clicked again, it will close and nothing will be open (good for expand collapse lists */
function expandCollapse(tabObj,instance) {
  var tabID=0;
  var pageObj;
  var tabObj;
  var inst = (!instance ? 1 : instance);
  var tabActiveObj = (inst <= tabActiveArray.length ? tabActiveArray[inst] : '');
  closeTab(inst);
  if (!tabObj) { return false };
  if (tabObj == tabActiveObj) { return false };

  tabID = tabObj.id;
  pageObj = document.getElementById(tabID + 'Body');
  if (!pageObj) { return false; }
  tabObj.className += 'active';
  pageObj.style.display='block';
  tabActiveArray[inst]=tabObj;
  return true;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
     window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


var divTabActive = 0
function selectDivTab(id) {
    if (divTabActive != 0) {
	  document.getElementById('divTab'+divTabActive).className='normal';
	  document.getElementById('divTabBody'+divTabActive).style.display='none';
	  }
	obj1 = document.getElementById('divTab'+id);
	obj2 = document.getElementById('divTabBody'+id);
	if (obj1 && obj2) {
	   obj1.className='active';
	   obj2.style.display='block';
	   divTabActive=id;
	   }
//		document.getElementById('divTab'+id).className='active';
//		document.getElementById('divTabBody'+id).style.display='block';
}


function colorTableRows() {
  if (document.getElementsByTagName) {
	var tables = document.getElementsByTagName('table');
	for (var i = 0; i < tables.length; i++) {
	  if (tables[i].className.indexOf('alternateRows') > -1) {
		var trs = tables[i].getElementsByTagName('tr');
		for (var j = 1; j < trs.length; j++) {
			trs[j].className = (j % 2 == 0 ? 'rowEven' : 'rowOdd');
			}
		}
	  }
	}
}
//addLoadEvent(colorTableRows);

function openURL(fld) {
   var obj;
   var val;

   obj=document.getElementById(fld);   
   if (!obj) {alert('Error: Unable to open URL. Field not found.'); return false;}
   val = obj.value;
   if (val.length == 0) {return false;}
   if (val.indexOf('http') == -1) {
      val = 'http://' + val;
	  }
   var x = window.open (val, "_blank", "height=600,width=850,location,menubar,resizable,scrollbars,status,toolbar");
   x.focus();
}


/* id of element to toggle, isOn is either 1 or 0 or not present.  If not present, then will toggle */
function toggleDisplay(id,isOn) {
  var obj = document.getElementById(id);
  if (!obj) return false;

  if (isOn == 0) {	 
	 obj.style.display = 'none';
     return true;
	 }
  if (isOn == 1) {
	 obj.style.display = 'block';
     return true;
     }
  obj.style.display = (obj.style.display == 'none') ? 'block' : 'none';
  return true;
}


var classSave = '';
// a= action, o = obj, c1 = classname1, c2=classname2 
function changeClass(a,o,c1,c2) {
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1) ? o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
	  //o.bgColor='pink';
      if(!changeClass('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
    case 'save':
	  //classSave=o.bgColor;
      classSave=o.className;
    break;
    case 'restore':
      //o.bgColor=classSave;
      o.className=classSave;
    break;
  }
}

var submitClicked = 0;
function stopDoubleSubmit (obj,msgDiv) {
  if (submitClicked == 1) {
     alert ('You\'re attempting to submit this form multiple times.  Please click only once. If you need to resubmit, refresh the page and try again');
	 return false;
	 }
  submitClicked = 1;
  obj.value = 'Processing...';
  obj.style.color = '#c5c5c5';
  if (msgDiv) {
     var objM = document.getElementById(msgDiv);
     if (objM) { objM.style.display = 'block'; }
     if (objM) { objM.style.visibility = 'visible'; }
     }
  return true;
}

/*


var classSave = '';
// a= action, o = obj, c1 = classname1, c2=classname2 
function changeClass(a,o,c1,c2) {
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1) ? o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
	  o.bgColor='pink';
      //if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
    case 'save':
	  classSave=o.bgColor;
      //classSave=o.className;
    break;
    case 'restore':
      o.bgColor=classSave;
      //o.className=classSave;
    break;
  }
}


*/
