

//this function toggles the limits label... see submission info
function switchLayer(val)
{
 	if(val=="one")
	{
		document.all.content1.style.display = "none";
		document.all.content2.style.display = "inline";
		document.all.content3.style.display = "none";
	}
	else if(val=="two")
	{
		document.all.content1.style.display = "none";
		document.all.content2.style.display = "none";
		document.all.content3.style.display = "inline";
	}
}

//this function allows menu swapping...see edit endorsements

function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "") {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].text;
fLength++;
   }
}
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
   }
}

//this function creates mouseover descriptions... see lic and appts info

function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=lib_bwcheck()

/***************************************************************************************
Variables to set:
***************************************************************************************/
messages=new Array()
//Write your descriptions in here.
messages[0] = "If license is continuous, enter N/A"
//To have more descriptions just add to the array.

fromX = -150 //How much from the actual mouse X should the description box appear?
fromY = -60 //How much from the actual mouse Y should the description box appear?

//To set the font size, font type, border color or remove the border or whatever,
//change the clDescription class in the stylesheet.

//Makes crossbrowser object.
function makeObj(obj){								
   	this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
	if(!this.evnt) return false
	this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;	
   	this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;		
	this.writeIt=b_writeIt;																
	return this
}

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

function b_writeIt(text){
	if (bw.ns4){this.wref.write(text);this.wref.close()}
	else this.wref.innerHTML = text
}

//Capturing mousemove
var descx = 0
var descy = 0
function popmousemove(e){descx=bw.ns4||bw.ns6?e.pageX:event.x; descy=bw.ns4||bw.ns6?e.pageY:event.y}

var oDesc;
//Shows the messages
function popup(num){
    if(oDesc){
		oDesc.writeIt('<div class="clDescription">'+messages[num]+'</div>')
		if(bw.ie5||bw.ie6) descy = descy+document.body.scrollTop
		oDesc.css.left = (descx+fromX)+px
		oDesc.css.top = (descy+fromY)+px
		oDesc.css.visibility = "visible"
    }
}
//Hides it
function popout(){
	if(oDesc) oDesc.css.visibility = "hidden"
}
function setPopup(){
   	if(bw.ns4)document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = popmousemove;
	oDesc = new makeObj('divDescription')
}



//this function checks all checkboxes in an array... see user mgmt

function checkall(formname,checkname,thestate){
var el_collection=eval("document.forms."+formname+"."+checkname)
for (c=0;c<el_collection.length;c++)
el_collection[c].checked=thestate
}

//this function places a cursor in a specified field

 function putFocus(formInst, elementInst) {
  if (document.forms.length > 0) {
   document.forms[formInst].elements[elementInst].focus();
  }
 }


function val_sum_total_double()
{	
// This function is an extension of the "val_sum_total". Didn't know if that would work so wrote this

	// the first arg is the total field
	// then on is the individual fields
	// if there's only one blank ind. field,
	// we calculate it
	// otherwise we leave it alone

	var tfield = val_sum_total_double.arguments[0];
	var field = null;
	var idx;
	var sum = 0;

	for (idx = 1; idx < val_sum_total_double.arguments.length; idx++) {
		var ifield = val_sum_total_double.arguments[idx];
		var ival = ifield.value;
		if (!ival) {
			if (field != null) return true;	// more than one fields
			field = ifield;
			continue;
		}
		if (isFloat(ival) == false) continue;
		sum = eval(sum) + eval(ival);
	}
	val_sum_total_double.arguments[0].value = sum;
	return true;
}

function isFloat(str)
{
	// return true if the string contains a Float Value
	// otherwise false	

	var dot = 0;
	var idx;
	var nval = "";
	for (idx = 0; idx < str.length; idx++) {
		var digit = str.charAt(idx);
		if (digit == '-') continue;
		if (digit == '.') {
			dot++;
			continue;
		}		
		if (digit < '0' || digit > '9')
			return false;
	}
	if (dot > 1)
	return false;
	
	return true;
}

function isDigit(str)
{
	// return true if the string contains all digits
	// otherwise false

	var idx;
	for (idx = 0; idx < str.length; idx++) {
		var digit = str.charAt(idx);
		if (digit == '-') continue;
		if (digit < '0' || digit > '9')
			return false;
	}
	return true;
}

function stay_field(field, desc)
{
	alert(desc);
	field.focus();
	return false;
}

function date_format(field)
{
//	return stay_field(field, "Date field must be in the format mm/dd/yyyy");
	return stay_field(field, "Enter a date (month, day, year) in the format mm/dd/yyyy.\n\nExample: 01/01/2001");
}

function val_date(field)
{
	var errorDesc="Enter a date (month, day, year) in the format mm/dd/yyyy.\n\nExample: 01/01/2001";
	
	// if the field is blank, ignore it

	var val = field.value;
	_parsedDate = null;

	if (!val)	return true;

	// date has to follow mm/dd/yyyy

	var year, month, day;
	var idx;

	idx = val.indexOf("/");
	if (idx < 0) {

	    var Yr,Dt,Mm;
        Mm = val.substring(0,2);

        if (isDigit(Mm) == false)
          return stay_field(field, errorDesc);
          
        Dt = val.substring(2,4)
        if (isDigit(Dt) == false)
          return stay_field(field, errorDesc);

/*	    if (val.length == 6) {
           Yr = val.substring(4,6);
		   if (isDigit(Yr) == false)
			  return stay_field(field, errorDesc);
           // give 100 years time frame to decide which century to select.
           // 50 years in 20th and 50 years in 21st
           if ( Yr > 50) {
             Yr = "19" + Yr.toString();  
           }
           else {
             Yr = "20" + Yr.toString();  
           }  
	    }
*/	    if(val.length == 8) {
	       Yr = val.substring(4,8);
		   if (isDigit(Yr) == false)
			  return stay_field(field, errorDesc);
	    }
	    else {
		   return date_format(field);
		}	
		val = Mm.toString() + "/" + Dt.toString() +  "/" + Yr.toString();
		field.value = val;
		//alert(val);
		idx = val.indexOf("/");
	}	
	
	var str = val.substring(0, idx);
	if (isDigit(str) == false)
//		return stay_field(field, "Invalid month field");
		return stay_field(field, errorDesc);

	month = parseInt(str, 10);
	month--;
	var idx2 = val.indexOf("/", idx+1);
	if (idx2 < 0)
		return date_format(field);

	str = val.substring(idx+1, idx2);
	if (isDigit(str) == false)
//		return stay_field(field, "Invalid day field");
		return stay_field(field, errorDesc);

	day = parseInt(str, 10);

	str = val.substring(idx2+1);
	if (isDigit(str) == false)
//		return stay_field(field, "Invalid year");
		return stay_field(field, errorDesc);

	if (str.length < 4)
//		return stay_field(field, "Year has to be 4 digits");
		return stay_field(field, errorDesc);

	year = parseInt(str, 10);

	// now create the date object and make sure
	// the month/day is consistent

	var d = new Date(year, month, day);
	if (year != d.getFullYear() || month != d.getMonth() || day != d.getDate())
//		return stay_field(field, "Invalid month/day/year combination");
		return stay_field(field, "Enter a valid month/day/year combination.");

	// we are cool
	_parsedDate = d;
	return true;
}

/*function oldval_date(field)
{
	var errorDesc="Enter a date (month, day, year) in the format mm/dd/yyyy.\n\nExample: 01/01/2001";
	
	// if the field is blank, ignore it

	var val = field.value;
	_parsedDate = null;

	if (!val)	return true;

	// date has to follow mm/dd/yyyy

	var year, month, day;
	var idx;

	if (val.length != 10)
		return stay_field(field, errorDesc);
		
	idx = val.indexOf("/");

	if (idx < 0) {

	    var Yr,Dt,Mm;
        Mm = val.substring(0,2);

        if (isDigit(Mm) == false)
          return stay_field(field, errorDesc);
          
        Dt = val.substring(2,4)
        if (isDigit(Dt) == false)
          return stay_field(field, errorDesc);

	    if (val.length == 6) {
           Yr = val.substring(4,6);
		   if (isDigit(Yr) == false)
			  return stay_field(field, errorDesc);
           // give 100 years time frame to decide which century to select.
           // 50 years in 20th and 50 years in 21st
           if ( Yr > 50) {
             Yr = "19" + Yr.toString();  
           }
           else {
             Yr = "20" + Yr.toString();  
           }  
	    } 
	    if(val.length == 8) {
	       Yr = val.substring(4,8);
		   if (isDigit(Yr) == false)
			  return stay_field(field, errorDesc);
	    }
	    else {
		   return date_format(field);
		}	
		val = Mm.toString() + "/" + Dt.toString() +  "/" + Yr.toString();
		field.value = val;
		alert(val);
		idx = val.indexOf("/");
	}	
	

	var str = val.substring(0, idx);
	if (isDigit(str) == false)
//		return stay_field(field, "Invalid month field");
		return stay_field(field, errorDesc);

	month = parseInt(str, 10);
	month--;
	var idx2 = val.indexOf("/", idx+1);
	if (idx2 < 0)
		return date_format(field);

	str = val.substring(idx+1, idx2);
	if (isDigit(str) == false)
//		return stay_field(field, "Invalid day field");
		return stay_field(field, errorDesc);

	day = parseInt(str, 10);

	str = val.substring(idx2+1);
	if (isDigit(str) == false)
//		return stay_field(field, "Invalid year");
		return stay_field(field, errorDesc);

	if (str.length < 4)
//		return stay_field(field, "Year has to be 4 digits");
		return stay_field(field, errorDesc);

	year = parseInt(str, 10);

	// now create the date object and make sure
	// the month/day is consistent

	var d = new Date(year, month, day);
	if (year != d.getFullYear() || month != d.getMonth() || day != d.getDate())
//		return stay_field(field, "Invalid month/day/year combination");
		return stay_field(field, "Enter a valid month/day/year combination.");

	// we are cool
	_parsedDate = d;
	return true;
}
*/

function val_past_date(field)
{
	// make sure we have a past date
	// first, make sure it is a date

	var _today = new Date();
	
	if (val_date(field) == false)
		return false;	// bad date

	if (_parsedDate == null) return true;	// assume empty

	// make sure the date is past
	
	if (_today.getTime() < _parsedDate.getTime() )
        {
		return stay_field(field, "Please enter the Date Received that is today's date or prior.");
        }
		
	return true;
}


//#NAME:val_past_year
function val_past_year(field)
{
	var val = field.value;
	if (!val) return true;

	if (isDigit(val) == false || val.length < 4)
		return stay_field(field, "Enter a date (year) in the format yyyy.");

	var year = parseInt(val, 10);
	//alert ('year= ' + year + 'today= ' + _today);
	if (year > _today.getFullYear())
		return stay_field(field, "Enter a date (year) in the past.");
		
	return true;
}



//#NAME:val_year
function val_year(field)
{
	var val = field.value;
	if (!val) return true;
	
	if (isDigit(val) == false || val.length < 4)
		return stay_field(field, "Enter a date (year) in the format yyyy.");
	if  (val.substring(0,2) != '19' && val.substring(0,2) != '20')
		return stay_field(field, "Enter a date (year) starting with 19 or 20.");
	return true;
}



//#NAME:val_zip
function val_zip(field)
{	
	var val = field.value;
	if (!val) return true;

	if (isDigit(val) == false || val.length != 5)
		return stay_field(field, "Enter a 5-digit ZIP code.\n\nExample: 98765");

	return true;
}




//#NAME:val_phone
function val_phone(field)
{
	var val = field.value;
	if (!val) return true;

	// only allow digit, () and -
	var nval = "";
	var idx;
    if (event && event.keyCode == 13) { 
     return true;
    }

	for (idx = 0; idx < val.length; idx++) {
		var ch = val.substring(idx, idx+1);
		if (ch != '(' && ch != ')' && ch != '-' && (ch < '0' || ch > '9'))
//			return stay_field(field, "Please enter phone number as (999)999-9999");
			return stay_field(field, "Enter a 10-digit phone number in the format (nnn)nnn-nnnn.\n\nExample: (213)555-5555");
			
		if (ch >= '0' && ch <= '9')
			nval = nval + ch;
	}
	if (nval.length != 10)
//			return stay_field(field, "Please enter phone number as (999)999-9999");
			return stay_field(field, "Enter a 10-digit phone number in the format (nnn)nnn-nnnn.\n\nExample: (213)555-5555");
			
	// reformat the number
	field.value = "(" + nval.substring(0, 3) + ")" + 
		nval.substring(3, 6) + "-" + nval.substring(6);
	return true;
}



//#NAME:phone_key_down
// added by senthil on 05/10/01 to support key down 
function phone_key_down(event,Field)
{
 
  if (event && event.keyCode == 13) { 
     val_phone(Field); 
     return false;
  } 
  return true; 
}  



//#NAME:val_ssn
function val_ssn(field)
{
	var val = field.value;
	if (!val) return true;

	// only allow digit, () and -
	var nval = "";
	var idx;
   if (event && event.keyCode == 13) { 
     return true;
   }
	for (idx = 0; idx < val.length; idx++) {
		var ch = val.substring(idx, idx+1);
		if (ch != '-' && (ch < '0' || ch > '9'))
//			return stay_field(field, "Please enter SSN as 999-99-9999");
			return stay_field(field, "Enter a 9-digit Social Security Number in the format nnn-nn-nnnn.\n\nExample: 999-99-9999");
			
		if (ch >= '0' && ch <= '9')
			nval = nval + ch;
	}
	if (nval.length != 9)
//			return stay_field(field, "Please enter SSN as 999-99-9999");
			return stay_field(field, "Enter a 9-digit Social Security Number in the format nnn-nn-nnnn.\n\nExample: 999-99-9999");
			
	// reformat the number
	field.value = nval.substring(0, 3) + "-" + 
		nval.substring(3, 5) + "-" + nval.substring(5);

	return true;
}

function val_db(field)
{
	var val = field.value;
	if (!val) return true;

	// only allow digit, () and -
	var nval = "";
	var idx;
   if (event && event.keyCode == 13) { 
     return true;
   }
	for (idx = 0; idx < val.length; idx++) {
		var ch = val.substring(idx, idx+1);
		if (ch != '-' && (ch < '0' || ch > '9'))
//			return stay_field(field, "Please enter SSN as 999-99-9999");
			return stay_field(field, "Enter a 9-digit D & B Number in the format nn-nnn-nnnn.\n\nExample: 99-999-9999");
			
		if (ch >= '0' && ch <= '9')
			nval = nval + ch;
	}
	if (nval.length != 9)
//			return stay_field(field, "Please enter SSN as 999-99-9999");
			return stay_field(field, "Enter a 9-digit D & B Number in the format nn-nnn-nnnn.\n\nExample: 99-999-9999");
			
	// reformat the number
	field.value = nval.substring(0, 2) + "-" + 
		nval.substring(2, 5) + "-" + nval.substring(5);

	return true;
}

//#NAME:ssn_key_down
// added by senthil on 05/10/01 to support key down 
function ssn_key_down(event,ssnCodeFormField)
{
 
  if (event && event.keyCode == 13) { 
     val_ssn(ssnCodeFormField); 
     return false;
  } 
  return true; 
}  



//#NAME:val_percent

function val_percent(field)
{
	var errorDesc="Enter a percentage between 0 and 100 (no % sign).";
	var val = field.value;
	if (!val) return true;

	if (isDigit(val) == false)
//		return stay_field(this, "Please enter a percentage between 0 and 100");
		return stay_field(field,errorDesc);
		
	var p = parseInt(val, 10);
	if (p < 0 || p > 100)
//		return stay_field(this, "Please enter a percentage between 0 and 100");
		return stay_field(field, errorDesc);
		
	return true;
}



//#NAME:val_number_between

function val_number_between(field,minNumber,maxNumber,massage)
{
	var errorDesc="Enter a number between "+minNumber+" and "+maxNumber+".\n"+massage;
	var val = field.value;
	if (!val) return true;

	if (isDigit(val) == false)
//		return stay_field(this, "Please enter a number between "+minNumber+" and "+maxNumber);
		return stay_field(field,errorDesc);
		
	var p = parseInt(val, 10);
	if (p < minNumber || p > maxNumber)
//		return stay_field(this, "Please enter a number between "+minNumber+" and "+maxNumber);
		return stay_field(field, errorDesc);
		
	return true;
}



//#NAME:val_float
function val_float(field, min, max)
{
	var errorDesc="Enter a number";
	if (min != "NA" && max != "NA") errorDesc += " between " + min + " and " + max;
	else if (min != "NA") errorDesc += " no less than " + min;
	else if (max != "NA") errorDesc += " no greater than " + max;
	errorDesc += ".";
	
	var val = field.value;
	
	//Vijay added logic to generate error if null is entered :: 02/06/2002
	if (val.length == 0)
		return stay_field(field, errorDesc);

	if (!val) return true;

	var dot = 0;
	var idx;
	var nval = "";
	for (idx = 0; idx < val.length; idx++) {
		var ch = val.substring(idx, idx+1);
		if (ch != ',')	nval += ch;
		if (ch >= '0' && ch <= '9') continue;
		if (ch == '-') continue;
		if (ch == '.') {
			dot++;
			continue;
		}
		if (ch == ',') continue;
//		return stay_field(field, "Please enter a number");
		return stay_field(field, errorDesc);
	}
	if (dot > 1)
//		return stay_field(field, "Please enter a number");
		return stay_field(field, errorDesc);
	
	var num = parseFloat(nval);
	// check min, max
	if (min != "NA") {
		var nmin = parseFloat(min);
		if (num < nmin)
//			return stay_field(field, "Number can't be less than " + min);
			return stay_field(field, errorDesc);

	}

	if (max != "NA") {
		var nmax = parseFloat(max);
		if (num > nmax)
//			return stay_field(field, "Number can't be greater than " + max);
			return stay_field(field, errorDesc);
	}
	field.value = nval;
	return true;
}




//#NAME:val_double
function val_double(field)
{
	var errorDesc="Enter a number";	
	errorDesc += ".";
	
	var val = field.value;
	//Biju commented as user is allowed to enter null in the application :: 03/10/2002
	//Vijay added logic to generate error if null is entered :: 02/06/2002
	//if (val.length == 0)
	//	return stay_field(field, errorDesc);

	if (!val) return true;

	var dot = 0;
	var idx;
	var nval = "";
	for (idx = 0; idx < val.length; idx++) {
		var ch = val.substring(idx, idx+1);
		if (ch != ',')	nval += ch;
		if (ch >= '0' && ch <= '9') continue;
		if (ch == '-') continue;
		if (ch == '.') {
			dot++;
			continue;
		}
		if (ch == ',') continue;
//		return stay_field(field, "Please enter a number");
		return stay_field(field, errorDesc);
	}
	if (dot > 1)
//		return stay_field(field, "Please enter a number");
		return stay_field(field, errorDesc);
	
	field.value = nval;
	return true;
}

//#NAME:val_formatNumber
function val_formatNumber(field)
{
	var errorDesc="Enter a number";	
	errorDesc += ".";
	
	var val = field.value;
	//Biju commented as user is allowed to enter null in the application :: 03/10/2002
	//Vijay added logic to generate error if null is entered :: 02/06/2002
	//if (val.length == 0)
	//	return stay_field(field, errorDesc);

	if (!val) return true;

	var dot = 0;
	var idx;
	var counter;
	var nval = "";
	var fmtVal = "";
	var cnt = 0;
	var outVal = "";
	for (idx = 0; idx < val.length; idx++) {
		var ch = val.substring(idx, idx+1);
		if (ch != ',')	nval += ch;
//		nval += ch;
		if (ch >= '0' && ch <= '9') continue;
		if (ch == '-') continue;
		if (ch == '.') {
			dot++;
			continue;
		}
		if (ch == ',') continue;
//		return stay_field(field, "Please enter a number");
		return stay_field(field, errorDesc);
	}
	if (dot > 1)
//		return stay_field(field, "Please enter a number");
		return stay_field(field, errorDesc);
	var srcFmtVal = "";
	if (nval.indexOf('.') > 0) {
		srcFmtVal = nval.substring(0,nval.indexOf('.'));
	}
	else
	{
		srcFmtVal = nval;
	}
	if (srcFmtVal.length > 3) {
		for (counter =srcFmtVal.length; counter > 0; counter--) {
			var fmtch = srcFmtVal.substring(counter, counter-1);
			if (cnt%3 == 0 && cnt != 0) {
				fmtVal = fmtch + ',' + fmtVal; 
			}	
			else {
				fmtVal = fmtch + fmtVal;
			}
			cnt ++ 
		}	
	}
	else	{
	fmtVal = srcFmtVal;
	}
	
	if (nval.indexOf('.') > 0) {
		outVal = fmtVal + nval.substring(nval.indexOf('.'), nval.length);
	}
	else
	{
		outVal = fmtVal;
	}
	field.value = outVal;
	return true;
}



//#NAME:isAlphabetic
function isAlphabetic (field, err)

{   
	return true;
	//var val = field.value;
	//if (!val) return true;
	
	//var idx;
    //for (idx = 0; idx < val.length; idx++) {
	//	var c = val.charAt(idx);
	//	if (c >= 'a' && c <= 'z') continue;	// lower letter OK
	//	if (c >= 'A' && c <= 'Z') continue;	// upper letter OK
	//	if (c >= '0' && c <= '9') continue;	// digit OK
	//	if (c == '.')	continue;
	//	if (c == '@')	continue;
	//	if (c == '_')	continue;
	//	if (c == '-') continue;
	//	if (c == ' ') continue;
		
		
		// otherwise, we got a problem
	//	return stay_field(field, err);
    //}

    // All characters are letters.
    //return true;
}




//#NAME:val_equate_dates
function val_equate_dates(field, comp, err)
{
	// field : field to check;
	// comp : field to compare to;

	var val = field.value;
	if (!val) return true;

	var ret;
	ret = val_date(field);
	if (ret == false) return false;

	// if comp is empty, return

	var cfield = field.form.elements(comp);
	var cval = cfield.value;
	if (!cval) return true;

	ret = val_date(cfield);
	if (ret == false) return false;
	
	if (cval == val) return true;
	return stay_field(cfield, err);
}



//#NAME:val_comp_dates
function val_comp_dates(field, comp, flags, err)
{
	// field : field to check;
	// comp : field to compare to;
	// flags : [Y|N][G|L] date has to be in the past; great or less than comp

	var val = field.value;
	if (!val) return true;

	var ret;
	if (flags.charAt(0) == 'Y'|| flags.charAt(0) == 'y')
		ret = val_past_date(field);
	else ret = val_date(field);
	if (ret == false) return false;

	// if comp is empty, return

	var cfield = field.form.elements(comp);
	var cval = cfield.value;
	if (!cval) return true;

	var d1 = _parsedDate;
	val_date(cfield);
	var d2 = _parsedDate;
	if (d1 == null || d2 == null) return false;

	// if there's a date compare error, we alway direct the
	// focus to the smaller date, to avoid circular loop

	if (flags.charAt(1) == 'G' || flags.charAt(1) == 'g') {
		if (d1.getTime() >= d2.getTime()) return true;
		return stay_field(cfield, err);
	}

	if (d1.getTime() <= d2.getTime()) return true;
	return stay_field(field, err);
}



//#NAME:val_sum_ind
function val_sum_ind()
{
	// the first arg is the field to check;
	// the second is the total field, in string format;
	// the third on are other individual fields, in string format

	var field = val_sum_ind.arguments[0];
	var tfield = field.form(val_sum_ind.arguments[1]);

	var val = field.value;
	var all_blank = true;
	var total = 0;

	if (val) {	// not empty
		if (isDigit(val) == false)
//			return stay_field(field, "Please enter a number");
			return stay_field(field, "Enter a number.");
			
		total = parseInt(val, 10);
		all_blank = false;
	}

	// go add others up
	var idx;
	for (idx = 2; idx < val_sum_ind.arguments.length; idx++) {
		var ifield = field.form(val_sum_ind.arguments[idx]);
		val = ifield.value;
		if (!val) continue;
		if (isDigit(val) == false) continue;
		all_blank = false;
		total += parseInt(val, 10);
	}
	if (all_blank == true) return true;
	// paste the result to the total
	tfield.value = total;
	return true;
}



//#NAME:val_sum_total
function val_sum_total()
{
	// the first arg is the total field
	// then on is the individual fields
	// if there's only one blank ind. field,
	// we calculate it
	// otherwise we leave it alone

	var tfield = val_sum_total.arguments[0];
	var val = tfield.value;
	if (!val) return true;
	if (isDigit(val) == false)
//		return stay_field(tfield, "Please enter a number");
		return stay_field(tfield, "Enter a number.");
	
	// find the lonely field without entry
	var field = null;
	var idx;
	var sum = 0;

	for (idx = 1; idx < val_sum_total.arguments.length; idx++) {
		var ifield = tfield.form(val_sum_total.arguments[idx]);
		var ival = ifield.value;
		if (!ival) {
			if (field != null) return true;	// more than one fields
			field = ifield;
			continue;
		}
		if (isDigit(ival) == false) continue;
		sum += parseInt(ival, 10);
	}
	if (field == null) return true;
	field.value = parseInt(val, 10) - sum;
	return true;
}



//#NAME:val_tax_id
function val_tax_id(field)
{
	// xx-x(7)
	var val = field.value;
	if (!val) return true;

	// only allow digit and -
	var nval = "";
	var idx;
	var err = "Enter a 9-digit Tax I.D. Number (FEIN) in the format nn-nnnnnnn.\n\nExample: 99-9999999";
	if (event && event.keyCode == 13) { 
	   return true;
	 }
	for (idx = 0; idx < val.length; idx++) {
			
		var ch = val.charAt(idx);
		if (ch != '-' && (ch < '0' || ch > '9'))
//			return stay_field(field, "Please enter TaxID as 99-9999999");
			return stay_field(field, err);
			
		if (ch >= '0' && ch <= '9')
			nval = nval + ch;
	}
	if (nval.length != 9)
//			return stay_field(field, "Please enter SSN as 99-9999999");
			return stay_field(field, err);
	// reformat the number
	field.value = nval.substring(0, 2) + "-" + nval.substring(2);
	return true;
}



//#NAME:tax_id_key_down
// added by senthil on 05/10/01 to support key down 

function tax_id_key_down(event,Field)
{
 
  if (event && event.keyCode == 13) { 
     val_tax_id(Field); 
     return false;
  } 
  return true; 
}  



//#NAME:convert_state
function convert_state(field, val)
{
//	field is a <select> tag; given the val,
// we compare val to description, and select the corresponding item

	var idx;
	// added by senthil to check zipcode returned state is available for a agent.
	var foundflag;
	foundflag = false;
	for (idx = 0; idx < field.length; idx++) {
	    // added by senthil to check value also
		if (field.options[idx].text == val || field.options[idx].value == val) {
			field.selectedIndex = idx;
			foundflag = true;
		}
	}
    return foundflag;
}



//#NAME:openWindow
// following code copied from Yem

function openWindow(url) {
  childWindow=open(url,"childWindow", "scrollbars=yes, width=250, height=400");
}



//#NAME:submitForm
function submitForm(formIdx) {
var key;
key=window.event.keyCode;

// Check each key pressed for character code 13 (carriage return), AND
// then SUBMIT the form for the user
if (key==13) {
  document.forms[formIdx].elements["submitPressed"].value = "Y";
  document.forms[formIdx].submit();
   }
}



//#NAME:zipOpen
function zipOpen(formIdx, zipFormField, oldZipCodeFieldName, cityFormFieldName, countyFormFieldName, stateFormFieldName, force ) {


        var oldZipCodeField = zipFormField.form(oldZipCodeFieldName);

        //alert(force);
        if( force == false )
        {
            if( oldZipCodeField.value == zipFormField.value)
            {
        	submitForm(formIdx);  
        	return false;
            }	
        }

        //alert(oldZipCodeField.value);
        //alert(zipFormField.value);

 		returnValue = showModalDialog("/iig/mga/common/zip.jhtml?zipcode=" + zipFormField.value, "dialogHeight:500;dialogWidth:500" );


        if( returnValue != null )
        {
            var stateFormField = zipFormField.form(stateFormFieldName);
            var cityFormField = zipFormField.form(cityFormFieldName);
            var countyFormField = zipFormField.form(countyFormFieldName);

            stateFormField.value= returnValue.state;


            // added by senthil to check zipcode returned state is available for a agent.
            if (stateFormField != null) {
				if (convert_state(stateFormField, returnValue.state) == false) {
	              alert("You do not have permission to clear for "  + returnValue.state + ".");
		          zipFormField.value = oldZipCodeField.value;
			      return false;
				}
			}


            if (cityFormField != null)	cityFormField.value = returnValue.city;
            if (countyFormField != null)	countyFormField.value = returnValue.county;

            if (oldZipCodeField != null)	oldZipCodeField.value = zipFormField.value;
            return true;
        }
        else
        {
        	//alert("return is null");
            zipFormField.value= oldZipCodeField.value;
            oldZipCodeField.value = zipFormField.value;
            return false;
        }
}




//#NAME:val_lookupzip    
function val_lookupzip(formIdx, field, oldField, cityField, countyField, stateField)
{
	if (val_zip(field) == false) return false;
	if (!field.value) return true;
	
	zipOpen(formIdx, field, oldField, cityField, countyField, stateField, false);
	return true;
}




//#NAME:lookupzip
function lookupzip(formIdx, field, oldField, cityField, countyField, stateField,checkOldValue)
{
	if (val_zip(field) == false) return false;
	if (!field.value) return true;
	
	zipOpen(formIdx, field, oldField, cityField, countyField, stateField, checkOldValue);
	return true;
}




//#NAME:zip_key_down
function zip_key_down(formIdx, event,  zipCodeFormField, oldZipCodeFieldName, cityFormFieldName, countyFormFieldName, stateFormFieldName)
	{
        
        if (event && event.keyCode == 13)
		{
            //alert("!!" + cityFormFieldName );
         //   alert(oldZipCodeFieldName);

			val_lookupzip(formIdx, zipCodeFormField, oldZipCodeFieldName,  cityFormFieldName, countyFormFieldName, stateFormFieldName);
			return false;
 		}
		else
		{
			return true;
		}
		
	}




//#NAME:val_complist
function val_complist(field, comp, mode, err  )
{
//this function does a comparison between values of two list boxes
// assumption : Comparison between numbers. comp is the field with which it is compared.
//Mode is L for Less Than, G for Greater Than, E for Equals
	var cfield = field.form.elements(comp);
	
	var val1 = cfield.options[cfield.selectedIndex];
	var val2 = field.options[field.selectedIndex];
	switch (mode.toString())
	{
	case "G" :
		if (parseFloat(val1.value) > parseFloat(val2.value) && field.selectedIndex != 0) 
		{
			field.selectedIndex = 0;
			return stay_field(field, err);
		}

		break
	case "L" :
		if (parseFloat(val1.value) < parseFloat(val2.value) && field.selectedIndex != 0) 
		{
			field.selectedIndex = 0;
			return stay_field(field, err);
		}

		break
	case "E" :
		if (parseFloat(val1.value) == parseFloat(val2.value) && field.selectedIndex != 0) 
		{
			field.selectedIndex = 0;
			return stay_field(field, err);
		}
	}
	return true;
}




//#NAME:val_no_entry	
function val_no_entry(field, err)
{
	var val = field.value;
	if (!val) return true;	// we are cool
	return stay_field(field, err);
}




//#NAME:val_no_select
function val_no_select(field, err)
{

	if (!field.checked) return true;	// cool
	alert(err);
	//field.checked = false;
	return true;
}




//#NAME:val_pct_sum

function val_pct_sum(field,afields, err)
{

	if (val_percent(field) == false) return false;
	if (!field.value) return true;
	
	var total = 0;
	var idx;
	
	for (idx = 0; idx < afields.length; idx++) {
		
		var val = afields[idx].value;
		if (!val) continue;
		var nval = 	parseInt(val, 10);
		total += nval;
	}

	if (total != 100) {
		alert(err);
	}
	return true;
}




//#NAME:val_copy
function val_copy(field, dfieldn, cfieldn, id)
{
	// if cfield is checked, copy the value from field to dfield
	var cfields = field.form.elements(cfieldn);
	var cfield = cfields[id];
	if (cfield == null) return true;
	if (cfield.checked == false) return true;
	var dfield = field.form.elements(dfieldn);
	dfield.value = field.value;
	return true;
}




//#NAME:val_lookupzipCopy
function val_lookupzipCopy(field, fieldns, id)
{
	// lookup zip code and copy to another address
	// input arguements : savefield, city, county, state, copyflag, czip, ccity, ccounty, cstate
	
	var lkup = val_lookupzip(field, fieldns[0], fieldns[1], fieldns[2], fieldns[3]);
	if (lkup == false) return false;
	if (!field.value) return true;
	
	// the flag field
	var ffields = field.form.elements(fieldns[4]);
	var ffield = ffields[id];
	if (ffield == null) return false;
	if (ffield.checked == false) return true;
	// copy zip, city, county and state
	var frm = field.form.elements;
	frm(fieldns[5]).value = field.value;
	frm(fieldns[6]).value = frm(fieldns[1]).value;
	frm(fieldns[7]).value = frm(fieldns[2]).value;
	frm(fieldns[8]).selectedIndex = frm(fieldns[3]).selectedIndex;
	
	return true;
}




//#NAME:zip_key_downCopy
function zip_key_downCopy(event,  field, fieldns, id)
{

	if (event && event.keyCode == 13)
	{
    //alert("!!" + cityFormFieldName );
 //   alert(oldZipCodeFieldName);

		val_lookupzipCopy(field, fieldns,  id);
		return false;
	}
	else
	{
		return true;
	}

}

	

//#NAME:val_ssn_fein
// added by senthil on 05/10/01 to support SSN/FEIN combined field

function val_ssn_fein(field)
{
		var status;
		var val = field.value;
		if (!val) return true;
		if (event && event.keyCode == 13) { 
		  return true;
		}

		// only allow digit, () and -
		var nval = "";
		var idx;

		for (idx = 0; idx < val.length; idx++) {
			var ch = val.substring(idx, idx+1);
			if (ch != '-' && (ch < '0' || ch > '9'))
				return stay_field(field, "Enter a 9-digit Social Security Number in the format nnn-nn-nnnn.\n\nExample: 999-99-9999 \n\n                       Or            \n\n\r Enter a 9-digit Tax I.D. Number (FEIN) in the format nn-nnnnnnn.\n\nExample: 99-9999999 ");
			if (ch >= '0' && ch <= '9')
				nval = nval + ch;
		}
		// more than 9, invalid
		if (nval.length != 9)
				return stay_field(field, "Enter a 9-digit Social Security Number in the format nnn-nn-nnnn.\n\nExample: 999-99-9999 \n\n                       Or            \n\n\r Enter a 9-digit Tax I.D. Number (FEIN) in the format nn-nnnnnnn.\n\nExample: 99-9999999 ");
		// check for SSN format 
	    if (val.substring(3,4) == '-' && val.substring(6,7) == '-') {
	      return true; 
	    }  
	    // check for FEIN format 
		if (val.substring(2,3) == '-' && val.lastIndexOf('-') == 2 ) {
		  return true; 
		} 				
		// check for JUST 9 numbers
	    if (val.length == 9) {
		  return true;
		}
		// else wrong
        return stay_field(field, "Enter a 9-digit Social Security Number in the format nnn-nn-nnnn.\n\nExample: 999-99-9999 \n\n                       Or            \n\n\r Enter a 9-digit Tax I.D. Number (FEIN) in the format nn-nnnnnnn.\n\nExample: 99-9999999 ");		
}



//#NAME:ssn_fein_key_press	
function ssn_fein_key_press(field)	
{
       if (event && event.keyCode == 13)
		{
			val_ssn_fein(field);
			return false;
 		}
		else
		{
			return true;
		}
}



//#NAME:fReset	
function fReset()
{
	var i;
	for(i=0; i < document.forms(0).elements.length;i++) {
  		document.forms(0).elements(i).value= "";
	}
	return true;
}



//#NAME:formReset	
function formReset(index)
{
	var i;
	for(i=0; i < document.forms(index).elements.length;i++) {
  		document.forms(index).elements(i).value= "";
	}
	return true;
}



//#NAME:popUpWindow	
function popUpWindow(url)
{
	//alert(url);
	var w = window.open(url, 'Privilges', 'titlebar=yes,width=400,height=300,scrollbars=yes,resizable=no' );
 	if (typeof(w) == "object" && w.closed != true) {
		w.focus();
	}	
}



//#NAME:popUpWindowSize	
function popUpWindowSize(url)
{
	//alert(url);
	var w = window.open(url, 'Privilges', 'titlebar=yes,width=800,height=600,scrollbars=yes,resizable=yes' );
 	if (typeof(w) == "object" && w.closed != true) {
		w.focus();
	}	
}



//#NAME:fsubmit
function fsubmit() {

  document.forms[0].elements["submitPressed"].value = "Y";
  document.forms[0].submit();
   }
   


//#NAME:Radio_PopUp
function Radio_PopUp(field,trigger,message)
{
	var val = field.value;
	if (val == trigger)
		alert(message);
}
   


//#NAME:val_email
function val_email(field)
{
	var val = field.value;
	if (!val) return true;

	var theStr = new String(val);
	var index = theStr.indexOf("@");
	if (index <= 0)
			return stay_field(field, "Enter a valid email address.\n\nExample: jsmith@aol.com");
	else
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
			return true;
		else
	  		return stay_field(field, "Enter a valid email address.\n\nExample: jsmith@aol.com");
		return true;
	}
	return true;
}



//#NAME:maxLengthTextArea
function maxLengthTextArea(field,maxChars)
{
  var diff = maxChars - field.value.length;

  // Need to check & enforce limit here also in case user pastes data
  if (diff < 0)
  {
    field.value = field.value.substring(0,maxChars);
  }
}



//#NAME:isNumber
function isNumber(str)
{
	// return true if the string contains all digits
	// otherwise false

	var idx;
	for (idx = 0; idx < str.length; idx++) {
		var digit = str.charAt(idx);
		if (digit == '.') continue;
		if (digit < '0' || digit > '9')
			return false;
	}
	return true;
}



//#NAME:isIRMNumber
function isIRMNumber(field)
{
	// return true if the string contains all digits
	// otherwise false
	var val = field.value;
 	var digit = val.charAt(0);
 	
 	if(digit !='+' && digit !='-' ) {
 		return stay_field(field, "Enter a valid IRM component");
 	}else{ 	
		var number;
		var lastIndex = val.length-1;
		number = val.substring(0,lastIndex);
		if(!number)return stay_field(field, "Enter a valid IRM component");

	}
	return true;
}



//#NAME:pct_sum
function pct_sum(afields)
{

	var total = 0;
	var idx;
	
	for (idx = 0; idx < afields.length; idx++) {
		
		var val = afields[idx].value;
		if (!val) continue;
		var nval = 	parseInt(val, 10);
		total += nval;
	}

	if (total != 100) {
//		alert (err);
		return false;
	}
	return true;
}

