var srcElem;
var intMouseX = 0;
var intMouseY = 0;
var mx=0;
var my=0;

document.onmousemove = getMouseXY;

function bOvr(obj)
{
	obj.className ='btnOvr';
	//obj.style.color = 'red';
	//obj.style.textDecoration = 'underline';
}

function bOut(obj)
{
	obj.className ='btnOut';
	//obj.style.color = 'blue';
	//obj.style.textDecoration = 'none';
}

function mOvr(obj)
{
	obj.style.color = 'red';
	obj.style.textDecoration = 'underline';
}

function mOut(obj)
{
	obj.style.color = 'blue';
	obj.style.textDecoration = 'none';
}

function mOvrI(obj)
{
	obj.style.border = 'GhostWhite thin solid';
	obj.style.borderRight = 'silver thin ridge';
	obj.style.borderBottom = 'silver thin ridge';
}

function mOutI(obj)
{
	obj.style.border = '';
	
	obj.style.borderRight = '';
	obj.style.borderBottom = '';
	obj.style.borderLeft = '';
	obj.style.borderTop = '';
}

function dayOver(obj)
{
	obj.style.border='#ffff99 double';
}

function dayOut(obj)
{
	obj.style.border='';
}

function showErr(linkObj,baseObj,xmlObjName)
{
	var popupObj = window.createPopup();
	var popupBodyObj = popupObj.document.body;
	
	var menuID = document.all.tblMemo;
	document.all.divMemo.style.visibility='visible';
	document.all.divMemo.style.display='block';
	document.all.spnMemo.innerHTML = eval('document.all.spnErr'+baseObj+getRecord(linkObj.recordNumber,xmlObjName)).innerHTML;
	
	popupBodyObj.innerHTML = menuID.outerHTML;
	popupObj.show(0, eval(linkObj.offsetHeight +10), menuID.offsetWidth, menuID.offsetHeight, linkObj);
}

function showHelp(linkObj,baseObj)
{
	var popupObj = window.createPopup();
	var popupBodyObj = popupObj.document.body;
	
	var menuID = document.all.tblMemo;
	document.all.divMemo.style.visibility='visible';
	document.all.divMemo.style.display='block';
	document.all.spnMemo.innerHTML = eval('document.all.spnHelp'+baseObj).innerHTML;
	
	popupBodyObj.innerHTML = menuID.outerHTML;
	popupObj.show(0, eval(linkObj.offsetHeight +10), menuID.offsetWidth, menuID.offsetHeight, linkObj);
}

function showWindow(strLoc)
{
	options = 'toolbar=1,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=';
	options += screen.width/1.2;
	options += ',height=';
	options += screen.height/2;
	options += ',left =';
	options += screen.width/12;
	options += ',top ='
	options += screen.height/4;

	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('../'+strLoc, '" + id + "', options);");
}

function getIntFromBool(bVal)
{
	if(bVal)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


function getBoolFromInt(iVal)
{
	if(iVal==0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isDate(dateStr) {

  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is format OK?

  if (matchArray == null) {
    //alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
    return false;
  }

  // parse date into variables
  month = matchArray[1];
  day = matchArray[3];
  year = matchArray[5];

  if (month < 1 || month > 12) { // check month range
    //alert("Month must be between 1 and 12.");
    return false;
  }

  if (day < 1 || day > 31) {
    //alert("Day must be between 1 and 31.");
    return false;
  }

  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    //alert("Month " + month + " doesn't have 31 days!")
    return false;
  }

  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
      //alert("February " + year + " doesn't have " + day + " days!");
      return false;
    }
  }
  return true;  // date is valid
}

function showReport(strLoc)
{
	options = 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=';
	options += screen.width/1.1;
	options += ',height=';
	options += screen.height/1.25;
	options += ',left =';
	options += screen.width/20;
	options += ',top ='
	options += screen.height/20;

	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(strLoc, '" + id + "', options);");
}
function getDate()
{
	var d = new Date()
	return eval(d.getMonth()+ 1)+'/'+d.getDate()+'/'+d.getFullYear()
}

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(Date.parse( startDate ));
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}

function YearAdd(startDate, numYears)
{
		return DateAdd(startDate,0,0,numYears);
}

function MonthAdd(startDate, numMonths)
{
		return DateAdd(startDate,0,numMonths,0);
}

function DayAdd(startDate, numDays)
{
		return DateAdd(startDate,numDays,0,0);
}

function addZero(vNumber){ 
	return ((vNumber < 10) ? "" : "") + vNumber 
} 
    
function formatDate(vDate, vFormat){ 
	var vDay                      = addZero(vDate.getDate()); 
	var vMonth            = addZero(vDate.getMonth()+1); 
	var vYearLong         = addZero(vDate.getFullYear()); 
	var vYearShort        = addZero(vDate.getFullYear().toString().substring(3,4)); 
	var vYear             = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort) 
	var vHour             = addZero(vDate.getHours()); 
	var vMinute           = addZero(vDate.getMinutes()); 
	var vSecond           = addZero(vDate.getSeconds()); 
	var vDateString       = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear) 
	vDateString           = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond) 
	return vDateString 
} 

function editField(strObj,intNumber,strGroup)
{
	try{
		if(eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).type='checkbox')
		{
			eval('arr'+strGroup)[eval('arr'+strGroup).length]=eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).checked;
		}
		else
		{
			eval('arr'+strGroup)[eval('arr'+strGroup).length] = eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).value;
		}
	}
	catch(e)
	{
		eval('arr'+strGroup)[eval('arr'+strGroup).length] = eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).value;
	}
	
	eval('document.all.spn'+strGroup+strObj+getRecord(intNumber,strGroup)).style.visibility ='hidden';
	eval('document.all.spn'+strGroup+strObj+getRecord(intNumber,strGroup)).style.display ='none';
	eval('document.all.spnEdit'+strGroup+strObj+getRecord(intNumber,strGroup)).style.visibility ='visible';
	eval('document.all.spnEdit'+strGroup+strObj+getRecord(intNumber,strGroup)).style.display ='block';
}

function updateField(strObj,intNumber,strGroup)
{
	eval('document.all.spn'+strGroup+strObj+getRecord(intNumber,strGroup)).style.visibility ='visible';
	eval('document.all.spn'+strGroup+strObj+getRecord(intNumber,strGroup)).style.display ='block';
	eval('document.all.spnEdit'+strGroup+strObj+getRecord(intNumber,strGroup)).style.visibility ='hidden';
	eval('document.all.spnEdit'+strGroup+strObj+getRecord(intNumber,strGroup)).style.display ='none';
}

function cancelField(strObj,intNumber,strGroup,intObjCount)
{	
	//alert(eval('arr'+strGroup)[intObjCount]);
	if(eval('arr'+strGroup)[intObjCount]=='on'){eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).checked = 'checked';}
	if(eval('arr'+strGroup)[intObjCount]=='off'){eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).checked = '';}
	
	//eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).value = eval('arr'+strGroup)[intObjCount];
	eval('document.all.ipt'+strGroup+strObj+getRecord(intNumber,strGroup)).value = eval('arr'+strGroup)[intObjCount];
	eval('document.all.spn'+strGroup+strObj+getRecord(intNumber,strGroup)).style.visibility ='visible';
	eval('document.all.spn'+strGroup+strObj+getRecord(intNumber,strGroup)).style.display ='block';
	eval('document.all.spnEdit'+strGroup+strObj+getRecord(intNumber,strGroup)).style.visibility ='hidden';
	eval('document.all.spnEdit'+strGroup+strObj+getRecord(intNumber,strGroup)).style.display ='none';
}

function editClick(strGroup,intRow)
{
	eval('document.all.Edit'+strGroup+getRecord(intRow,strGroup)).style.visibility = 'hidden';
	eval('document.all.Edit'+strGroup+getRecord(intRow,strGroup)).style.display = 'none';
	eval('document.all.Delete'+strGroup+getRecord(intRow,strGroup)).style.display = 'none';
	eval('document.all.Update'+strGroup+getRecord(intRow,strGroup)).style.visibility = 'visible';
	eval('document.all.Update'+strGroup+getRecord(intRow,strGroup)).style.display = 'block';
	eval('document.all.Cancel'+strGroup+getRecord(intRow,strGroup)).style.visibility = 'visible';
	eval('document.all.Cancel'+strGroup+getRecord(intRow,strGroup)).style.display = 'block';
	
}

function updateCancelClick(strGroup,intRow)
{
	eval('document.all.Edit'+strGroup+getRecord(intRow,strGroup)).style.visibility = 'visible';
	eval('document.all.Edit'+strGroup+getRecord(intRow,strGroup)).style.display = '';
	eval('document.all.Delete'+strGroup+getRecord(intRow,strGroup)).style.visibility = 'visible';
	eval('document.all.Delete'+strGroup+getRecord(intRow,strGroup)).style.display = '';
	eval('document.all.Update'+strGroup+getRecord(intRow,strGroup)).style.visibility = 'hidden';
	eval('document.all.Update'+strGroup+getRecord(intRow,strGroup)).style.display = 'none';
	eval('document.all.Cancel'+strGroup+getRecord(intRow,strGroup)).style.visibility = 'hidden';
	eval('document.all.Cancel'+strGroup+getRecord(intRow,strGroup)).style.display = 'none';
}

function checkLocation(obj)
{
	if(obj.value >=990)
	{
		document.all.fntLocation.color = 'black';
		document.all.txtLocationNumber.disabled = true;
		document.all.txtLocationNumber.value = '[N/A]';
	}
	else
	{
		document.all.fntLocation.color = 'red';
		document.all.txtLocationNumber.disabled = false;
		document.all.txtLocationNumber.value = '';
	}
}

function checkRecords(strGroup)
{
	try{
		if (eval('xml'+strGroup).xml.length <= 25)
		{
			eval('document.all.div'+strGroup).style.visibility = 'hidden';
			eval('document.all.div'+strGroup).style.display = 'none';
			eval('document.all.spn'+strGroup+'NoRecords').style.visibility = 'visible';
			eval('document.all.spn'+strGroup+'NoRecords').style.display = 'block';

			return false;
		}
		else
		{
			eval('document.all.div'+strGroup).style.visibility = 'visible';
			eval('document.all.div'+strGroup).style.display = 'block';
			eval('document.all.spn'+strGroup+'NoRecords').style.visibility = 'hidden';
			eval('document.all.spn'+strGroup+'NoRecords').style.display = 'none';

			return true;

		}
	}
	catch(e)
	{
		eval('document.all.div'+strGroup).style.visibility = 'hidden';
		eval('document.all.div'+strGroup).style.display = 'none';
		eval('document.all.spn'+strGroup+'NoRecords').style.visibility = 'visible';
		eval('document.all.spn'+strGroup+'NoRecords').style.display = 'block';

		return false;
	}
}

function syncChanges(strObj)
{
 	var xmlDoc = new ActiveXObject('MSXML2.FreeThreadedDOMDocument');
	xmlDoc.async = false;
	xmlDoc.loadXML(eval('xml'+strObj).xml);
	eval('document.all.xml'+strObj+'New').value = xmlDoc.xml;
	//prompt(xmlDoc.xml,xmlDoc.xml,xmlDoc.xml);


	return true;
}
function syncChanges2(objXMLIsland,objXMLDocElem)
{
	objXMLIsland.loadXML(objXMLDocElem.xml);
}

function SyncNodes(xmlIsland, xmlInput) {
	// save to xml island...
	eval(xmlIsland).loadXML(xmlInput);
	// copy to hidden input for postback...
	try {
		eval('document.all.ipt' + xmlIsland).value = xmlInput;
	}
	catch (er) {}
}

function getElem(Obj)
{
	if(eval('xml'+Obj).recordset.RecordCount > 1)
	{
		eval('xml'+Obj).recordset.AbsolutePosition = srcElem.recordNumber;
		return '('+ eval(srcElem.recordNumber - 1)+')';
	}
	else
	{
		eval('xml'+Obj).recordset.AbsolutePosition = srcElem.recordNumber;
		return '';
	}
}

function getRecord(intNumber, Obj)
{
	if (intNumber == -1)
	{
		return '';
	}
	
	if(eval('xml'+Obj).recordset.RecordCount > 1)
	{
		return '('+(intNumber-1)+')';
	}
	else
	{
		return '';
	}
}


function getMouseXY(e)
{
    intMouseX = event.clientX + document.body.scrollLeft;
    intMouseY = event.clientY + document.body.scrollTop;
}


function Len(str)
{
	return String(str).length;
}

function Mid(str, start, len)
{
	// Make sure start and len are within proper bounds
	if (start < 0 || len <= 0) return '';

	var iEnd, iLen = String(str).length;
	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;
	return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor)
{
	for (i=0; i < Len(strSearch); i++)
	{
		if (charSearchFor == Mid(strSearch, i, String(charSearchFor).length))
		{
			return i;
		}
	}
	return -1;
}
	
function Right(str, n) 
{
	if (n <= 0)     // Invalid bound, return blank string
		return '';
    else
    {				// Valid bound, return appropriate substring
		var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function Left(str, n) 
{
	if (n <= 0)		// Invalid bound, return blank string
		return '';
	else if (n > String(str).length) // Invalid bound, return
		return str;						// entire string
	else				// Valid bound, return appropriate substring
		return String(str).substring(0,n);
}

function RTrim(str) 
{
        var whitespace = new String(" \t\n\r");
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            var i = s.length - 1;       // Get length of string
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;
            s = s.substring(0, i+1);
        }
        return s;
}

function buildXMLNodeFromList(objList, nodeRoot, nodeChild, bAllItems) 
{
	var bSelected = false;
	var strXMLNode = '';
	
	if ( bAllItems == false )
	{
		for (var i=0; i <= eval('document.all.'+objList).length - 1 ; i++)
		{
			if ( eval('document.all.'+objList+'['+i+']').selected ) {
				strXMLNode = strXMLNode + '<' + nodeRoot + '>'
										+ '<' + nodeChild + '>'
										+ eval('document.all.'+objList+'['+i+']').value
										+ '</' + nodeChild + '>'
										+ '</' + nodeRoot + '>';
				bSelected = true;
			}
		}
	}
	if ( bSelected == false || bAllItems == true )
	{
		strXMLNode = '';
		for (var i=0; i <= eval('document.all.'+objList).length - 1 ; i++)
		{
			strXMLNode = strXMLNode + '<' + nodeRoot + '>'
									+ '<' + nodeChild + '>'
									+ eval('document.all.'+objList+'['+i+']').value
									+ '</' + nodeChild + '>'
									+ '</' + nodeRoot + '>';
		}
	}
	return strXMLNode;
}

function moveListItem( srcList, destList, moveAll ) 
{
	// Do nothing if nothing is selected
	if ( ( srcList.selectedIndex == -1 ) && ( moveAll == false ) ) { return; }
	
	newDestList = new Array( destList.options.length );
	var len = 0;
	for( len = 0; len < destList.options.length; len++ ) {
		if ( destList.options[ len ] != null ) {
			newDestList[ len ] = new Option( 
				destList.options[ len ].text, 
				destList.options[ len ].value, 
				destList.options[ len ].defaultSelected, 
				destList.options[ len ].selected );
		}
	}

	for( var i = 0; i < srcList.options.length; i++ ) { 
		if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) ) {
			newDestList[ len ] = new Option( 
				srcList.options[i].text, 
				srcList.options[i].value, 
				srcList.options[i].defaultSelected, 
				srcList.options[i].selected );
			len++;
		}
	}
	newDestList.sort( compareOptionValues );   // BY VALUES
	//newDestList.sort( compareOptionText );   // BY TEXT

	// Populate the destination with the items from the new array
	for ( var j = 0; j < newDestList.length; j++ ) {
		if ( newDestList[ j ] != null )	{
			destList.options[ j ] = newDestList[ j ];
		}
	}
	// Erase source list selected elements
	for( var i = srcList.options.length - 1; i >= 0; i-- ) { 
		if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )	{
			//srcList.options[i].value = "";
			//srcList.options[i].text  = "";
			srcList.options[i]       = null;
		}
	}
}
function compareOptionValues(a, b) 
{ 
	// Radix 10: for numeric values, Radix 36: for alphanumeric values
	var sA = parseInt( a.value, 36 );  
	var sB = parseInt( b.value, 36 );  
	return sA - sB;
}
function compareOptionText(a, b) 
{ 
	
	//a = replaceChars(a);
	//b = replaceChars(b);
	// Radix 10: for numeric values, Radix 36: for alphanumeric values
	var sA = parseInt( a.text, 36 );  
	var sB = parseInt( b.text, 36 );  
	return sA - sB;
}

function replaceChars(entry,out,add) {
	//out = '\s'; // replace this
	//add = ''; // with this
	temp = '' + entry; // temporary holder

	while (temp.indexOf(out)>-1)
	{
		pos= temp.indexOf(out);
		temp = '' + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function showMessage(linkObj,intNumber,Obj) 
{
	var popupObj = window.createPopup();
	var popupBodyObj = popupObj.document.body;
	
	var menuID = document.all.tblMemo;
	document.all.divMemo.style.visibility='visible';
	document.all.divMemo.style.display='block';

	//if(Len(eval('document.all.spnErr'+ Right(linkObj.id,Len(linkObj.id)-3))+getRecord(intNumber, Obj).innerHTML)>0)
	//{
		//alert('document.all.spnErr'+ Right(linkObj.id,Len(linkObj.id)-3)+getRecord(intNumber, Obj));
		try{document.all.spnMemo.innerHTML = eval('document.all.spnErr'+ Right(linkObj.id,Len(linkObj.id)-3)+getRecord(intNumber, Obj)).innerHTML;}
		catch(e){}
		try{document.all.spnMemo.innerHTML = eval('document.all.spn'+ Right(linkObj.id,Len(linkObj.id)-3)+getRecord(intNumber, Obj)).innerHTML;}
		catch(e){}
		popupBodyObj.innerHTML = menuID.outerHTML;
		popupObj.show(0, eval(linkObj.offsetHeight +10), menuID.offsetWidth, menuID.offsetHeight, linkObj);
	//}

}

function resizeTA(TA) 
{
  var winW, winH;
  var usingIE = 0;

  // these paramaters have to match the font you specify with your
  // style tag on the textarea.
  var fontMetricWidth = 7;
  var fontMetricHeight = 14;

  // you don't want this smaller than 1,1
  var minWidthInCols = 20;
  var minHeightInRows = 7;

  // offset fudge factors. 
  // Making these bigger makes the textarea smaller.
  var leftOffsetFudge = 55;
  var topOffsetFudge = 25;


  if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape") {
      winW = window.innerWidth;
      winH = window.innerHeight; 
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {
      winW = document.body.offsetWidth;
      winH = document.body.offsetHeight;
      usingIE = 1;
    }
  }

  if (! usingIE ) {
    return; // this javascript below does not work for netscape
  }

  // this code computes the upper-left corner offset
  // by walking all the elements in the html page  
  toffset = 0;
  loffset = 0;
  offsetobj = TA;
  while (offsetobj) {
    toffset += offsetobj.offsetTop + offsetobj.clientTop;
    loffset += offsetobj.offsetLeft + offsetobj.clientLeft;
    offsetobj = offsetobj.offsetParent;
  }

  // compute and set the width
  var overhead = loffset + leftOffsetFudge;
  var ta_width = ((winW - overhead))  / fontMetricWidth;
  if (ta_width < minWidthInCols) {
    ta_width = minWidthInCols;
  }
  TA.cols = ta_width;

/*
  // compute and set the height
  var overhead = toffset + topOffsetFudge;
  var ta_height = (winH - overhead) / fontMetricHeight;
  if (ta_height < minHeightInRows) {
    ta_height = minHeightInRows;
  }
  TA.rows = ta_height;
  */
}

function resizeTAFull(TA) 
{
  var winW, winH;
  var usingIE = 0;

  // these paramaters have to match the font you specify with your
  // style tag on the textarea.
  var fontMetricWidth = 7;
  var fontMetricHeight = 14;

  // you don't want this smaller than 1,1
  var minWidthInCols = 20;
  var minHeightInRows = 7;

  // offset fudge factors. 
  // Making these bigger makes the textarea smaller.
  var leftOffsetFudge = 60;
  var topOffsetFudge = 30;


  if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape") {
      winW = window.innerWidth;
      winH = window.innerHeight; 
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {
      winW = document.body.offsetWidth;
      winH = document.body.offsetHeight;
      usingIE = 1;
    }
  }

  if (! usingIE ) {
    return; // this javascript below does not work for netscape
  }

  // this code computes the upper-left corner offset
  // by walking all the elements in the html page  
  toffset = 0;
  loffset = 0;
  offsetobj = TA;
  while (offsetobj) {
    toffset += offsetobj.offsetTop + offsetobj.clientTop;
    loffset += offsetobj.offsetLeft + offsetobj.clientLeft;
    offsetobj = offsetobj.offsetParent;
  }

  // compute and set the width
  var overhead = loffset + leftOffsetFudge;
  var ta_width = ((winW - overhead))  / fontMetricWidth;
  if (ta_width < minWidthInCols) {
    ta_width = minWidthInCols;
  }
  TA.cols = ta_width;


  // compute and set the height
  var overhead = toffset + topOffsetFudge;
  var ta_height = (winH - overhead) / fontMetricHeight;
  if (ta_height < minHeightInRows) {
    ta_height = minHeightInRows;
  }
  TA.rows = ta_height;
}

function getObject(objectId) {
	if (document.all && !document.getElementById) {
		return document.all(objectId)
	} else {
		return document.getElementById(objectId)
	}
}

//Telephone Mask Function

function ValidatePhone(p1)
{
var p = '' + p1.value;
var p2 = '';

for (var i=0; i < p.length+1; i++)
{
	if (!isNaN(p.substring(i-1,i)))
	{
		p2 = p2 + p.substring(i-1,i);
	}
}
p = p2;

if(p.length==1)
{
	pp=p;
	d4=p.indexOf('(')
	pp='('+pp;
	//p1.value=pp;
}

if(p.length==3)
{
	pp=p;
	d4=p.indexOf('(')
	d5=p.indexOf(')')
	if(d4==-1)
	{
		pp="("+pp;
	}
	if(d5==-1)
	{
		pp=pp+") ";
	}
	//p1.value="";
	//p1.value=pp;
}

if(p.length>3)
{
	d1=p.indexOf('(')
	d2=p.indexOf(')')
	if (d2==-1)
	{
		l30=p.length;
		p30=p.substring(0,5);
		p30=p30+") " 
		p31=p.substring(5,l30);
		pp=p30+p31;
		//p1.value="";
		//p1.value=pp;
	}
}
if(p.length>4){
	p11=p.substring(d1+1,d2);
	if(p11.length>3)
	{
		p12=p11;
		l12=p12.length;
		l15=p.length
		p13=p11.substring(0,3);
		p14=p11.substring(3,l12);
		p15=p.substring(d2+1,l15);
		p1.value="";
		pp="("+p13+") "+p14+p15;
		//p1.value=pp;
	}
	l16=p.length;
	p16=p.substring(d2+1,l16);
	l17=p16.length;
	if(l17>3&&p16.indexOf('-')==-1)
	{
		p17=p.substring(d2+1,d2+5);
		p18=p.substring(d2+5,l16);
		p19=p.substring(0,d2+1);
		pp=p19+p17+"-"+p18;
		//p1.value="";
		//p1.value=pp;
	}
	p1.value=pp;
}
}

function valTel(key,m)
{
	var bolVal = true;
	if ( ((key>47&&key<58)||(key>95&&key<106)))
	{
		if(m.value.length > 13){return false;}
		else{return MaskFilter(key, m, '(###) ###-####');}
		
	}
	else if ((key==16) || (key==8) || (key==9) || (key==46) || (key==37) || (key==38) || (key==39) || (key==40))
		{return true;}
	else if (key==13) 
		{MaskFilter(key,m,'(###) ###-####');
		return true;}
	else
	{
		return false;
	}
}
//End Telephone Mask Function

//Zipcode Mask Function
var n;
var p;
var p1;
function ValidateZip(){
p=p1.value

	if(p.length>4 && p.indexOf('-')>=0)
	{
		pa=p.substring(0,5);
		pb=p.substring(6,p.length);
		p1.value = pa+'-'+pb;
	}

	else if(p.length>4 && p.indexOf('-')==-1)
	{
		pa=p.substring(0,5);
		pb=p.substring(5,p.length);
		p1.value = pa+'-'+pb;
	}
	//setTimeout(ValidateZip,100)
}

function valZip(key,m)
{
	var bolVal = true;
	n=m.name;
	p1=m;

	if ( ((key>47&&key<58)||(key>95&&key<106)))
	{
		ValidateZip();
		if(m.value.length > 9){m.value = Left(m.value,Len(m.value) - 1);}
	}
	else if ((key==13) || (key==8) || (key==9) || (key==46) || (key==37) || (key==38) || (key==39) || (key==40)){return true;}
	else
	{
		return false;
	}
}
//End Zipcode Mask Function

//Number Validation
function valNum(key)
{
	if ((key>47&&key<58) || (key>95&&key<106) || (key==13) || (key==8) || (key==9) || (key==46) || (key==37) || (key==38) || (key==39) || (key==40)){return true;}
	else
	{
		alert('This field accepts only numeric values.');
		return false;
	}
}

function valDate(key)
{
	if ((key>47&&key<58) || (key>95&&key<106) || (key>36&&key<41) || (key==13) || (key==191) || (key==111) || (key==8) || (key==9) || (key==46) ){return true;}
	else
	{
		alert('Please enter a valid date in the MM/DD/YYYY format.');
		return false;
	}
}

function valPage(key)
{
	if ((key>47&&key<58) || (key>95&&key<106) || (key>36&&key<41) || (key==8) || (key==9) || (key==46) )
	{
		return true;
	}
	else if(event.keyCode=='13')
	{
		__doPostBack('cmdPage','');
		return false;
	}
	else
	{
		return false;
	}
}

function valDec(key)
{
	if ((key>47&&key<58) || (key>95&&key<106) || (key==110) || (key==190) || (key==13) || (key==8) || (key==9) || (key==46) || (key==37) || (key==38) || (key==39) || (key==40)){return true;}
	else
	{
		alert('This field accepts only numeric (decimal) values.');
		return false;
	}
}

function validateDate(DateValue)
{
	var checkstr = '0123456789';
	var seperator = '/';
	var sepCount = 0
	var day = '';
	var month = '';
	var year = '';
	var leap = 0;
	var err = 0;
	var i;

	err = 0;
	/* Delete all chars except 0..9 */
	for (i = 0; i < DateValue.length; i++) { 
		if (seperator.indexOf(DateValue.substr(i,1)) >= 0) { sepCount++; }
		if (sepCount > 2) { sepCount = 2; }
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) 
		{
			switch ( sepCount ) 
			{
				case 0: month = month + DateValue.substr(i,1); break;
				case 1: day = day + DateValue.substr(i,1); break;
				case 2: year = year + DateValue.substr(i,1); break;
			}
		}
	}
	/* if year is entered as 2-digit / always assume 20xx */
	if (year.length != 4) { year = '20' + year; }
	/* Validation of year */
	year = eval(year);
	if ((year < 1901) || (year > 9999)) { err = 20; }
	/* Validation of month*/
	month = eval(month);
	if ((month < 1) || (month > 12)) { err = 21; }
	/* Validation of day*/
	day = eval(day);
	if (day < 1) { err = 22; }
	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) { leap = 1; }
	if ((month == 2) && (leap == 1) && (day > 29)) { err = 23; }
	if ((month == 2) && (leap != 1) && (day > 28)) { err = 24; }
	/* Validation of other months */
	if ((day > 31) && (
			(month == "01") || 
			(month == "03") || 
			(month == "05") || 
			(month == "07") || 
			(month == "08") || 
			(month == "10") || 
			(month == "12"))	) { err = 25; }
	if ((day > 30) && (
			(month == "04") || 
			(month == "06") || 
			(month == "09") || 
			(month == "11"))	) { err = 26; }

	if (err == 0) {
		return true;
	}
	else {
		return false;
	}
}

function confirmLink ()
{
	try
	{
		if ( document.all.iptWizardActive.value == 'True' )
		{
			return confirm('This customer has an Affiliation but no Enrollment in the new program (Only enrolled programs are reported). \nSelect OK to leave this page without enrolling the customer, or Cancel to remain on this page add the Enrollment.');
			//Warning: You have not yet enrolled this new customer into the selected program. Do you wish to continue with this selection?
		}
		else
		{	return true; }
	}
	catch (e)
	{ return true; }
}

var dFilterStep

function dFilterStrip (dFilterTemp, dFilterMask)
{
    dFilterMask = replace(dFilterMask,'#','');
    for (dFilterStep = 0; dFilterStep < dFilterMask.length++; dFilterStep++)
	{
		dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
	}
	return dFilterTemp;
}

function dFilterMax (dFilterMask)
{
 	dFilterTemp = dFilterMask;
	for (dFilterStep = 0; dFilterStep < (dFilterMask.length+1); dFilterStep++)
	{
		if (dFilterMask.charAt(dFilterStep)!='#')
		{
		dFilterTemp = replace(dFilterTemp,dFilterMask.charAt(dFilterStep),'');
		}
	}
	return dFilterTemp.length;
}

function MaskFilter (key, textbox, dFilterMask)
{
	if (key==9){return true;}
	
	textbox.value = FilterByMask(key, textbox.value, dFilterMask);
    return false;
}

function FilterByMask(key, theValue, theMask)
{
	dFilterNum = dFilterStrip(theValue, theMask);

	if (key==8&&dFilterNum.length!=0)
	{
		dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);
	}
 	else if ( (key>47&&key<58) && dFilterNum.length<dFilterMax(theMask) )
	{	//valid number and within the mask...
		dFilterNum=dFilterNum+String.fromCharCode(key);
	}
	else if ( (key>95&&key<106) && dFilterNum.length<dFilterMax(theMask) )
	{	//valid keypad number and within the mask...
		dFilterNum=dFilterNum+String.fromCharCode(key-48);
	}
	
	var dMaskOnly = '';
	for (dFilterStep = 0; dFilterStep < theMask.length; dFilterStep++)
	{	if (theMask.charAt(dFilterStep)!='#'){
			dMaskOnly = dMaskOnly + theMask.charAt(dFilterStep);}
	}

	var dFilterFinal='';
	for (dFilterStep = 0; dFilterStep < theMask.length; dFilterStep++)
	{
		if (theMask.charAt(dFilterStep)=='#')
			{
				if (dFilterNum.length!=0)
				{
					dFilterFinal = dFilterFinal + dFilterNum.charAt(0);
					dFilterNum = dFilterNum.substring(1,dFilterNum.length);
				}
				else
				{
					dFilterFinal = dFilterFinal + '';
				}
			}
		 	else if (theMask.charAt(dFilterStep)!='#')
			{
				dFilterFinal = dFilterFinal + theMask.charAt(dFilterStep); 			
			}
	}
	if(dMaskOnly==dFilterFinal){return '';}
	else{return dFilterFinal;}
}

function replace(fullString,text,by) {
// Replaces text with by in string
    var strLength = fullString.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return fullString;

    var i = fullString.indexOf(text);
    if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
    if (i == -1) return fullString;

    var newstr = fullString.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(fullString.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function ghideElement( elmID, overDiv )
{
   
    for( i = 0; i < document.all.tags( elmID ).length; i++ )
    {
        obj = document.all.tags( elmID )[i];
        if( !obj || !obj.offsetParent )
        {
        continue;
        }
        // Find the element's offsetTop and offsetLeft relative to the BODY tag.
        objLeft   = obj.offsetLeft;
        objTop    = obj.offsetTop;
        objParent = obj.offsetParent;

        while( objParent.tagName.toUpperCase() != "BODY" )
        {
        objLeft  += objParent.offsetLeft;
        objTop   += objParent.offsetTop;
        objParent = objParent.offsetParent;
        }
    
        objHeight = obj.offsetHeight;
        objWidth = obj.offsetWidth;
//overDiv.offsetTop=overDiv.offsetTop +200;
//alert(overDiv.offsetTop);
//alert(overDiv.offsetLeft);
//alert(overDiv.offsetTop+200)
//alert(overDiv.offsetHeight+200);
//alert(objTop);

        if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
        else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
        else if( overDiv.offsetTop >= ( objTop + objHeight ));
        else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
        else
        {
        obj.style.visibility = "hidden";
        }
    }
}
/*
* unhides <select> and <applet> objects (for IE only)
*/
function gshowElement( elmID )
{
    for( i = 0; i < document.all.tags( elmID ).length; i++ )
    {
        obj = document.all.tags( elmID )[i];
        
        if( !obj || !obj.offsetParent )
        {
        continue;
        }
    
        obj.style.visibility = "";
    }
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}

//Date format function
// Copyright 2001, 2003 InterAKT Online. All rights reserved.
function editDatePre(obj, mask, event) {
	var keyCode = event.keyCode;
	if (keyCode == 0) {
		keyCode=event.charCode;
	}
	
	if(keyCode == 9) {
		return;
	}

	// if the user pressed "." we autocomplete with the current date
	if ((keyCode == 190 || keyCode == 110 || keyCode == 46) && (obj.value.length == 0)) {
		date = new Date();
		y = date.getFullYear();
		m = date.getMonth()+1;
		d = date.getDate();
		if (m<10) m = "0"+m;
		if (d<10) d = "0"+d;

		tmp = mask;
		tmp = tmp.replace(/D+/i, d);
		tmp = tmp.replace(/M+/i, m);
		tmp = tmp.replace(/Y+/i, y);
		obj.value = tmp;
		hndlr_utilStopEvent(event);
		return false;
	}

	var diff = 0;

	if (keyCode == 187 || keyCode == 107 || keyCode == 61 || keyCode == 43) {
		diff = 1;
	} else if (keyCode == 189 || keyCode==109 || keyCode == 45) {
		diff = -1;
	}

	if (diff != 0) {
		var date = getInputDate(obj.value, mask);

		y = date.getFullYear();
		m = date.getMonth()+1;
		d = date.getDate();
		if (d.toString() == 'NaN') {
			d = '01';
		}
		d += diff;


		//compute date at 12:00PM to avoid time saving problems
		date = new Date(y, m-1, d, 12 , 1 ,1);
		y = date.getFullYear();
		m = date.getMonth()+1;
		d = date.getDate();

		//format the all the entries with yyyy mm and dd (4-2-2 digits)
		if (m<10) m = "0"+m;
		if (d<10) d = "0"+d;

		while (y.length < 4) {
			y = "0" + y;
		}

		tmp = mask;
		tmp = tmp.replace(/D+/i, d);
		tmp = tmp.replace(/M+/i, m);
		tmp = tmp.replace(/Y+/i, y);
		if (obj.value != tmp) {
			obj.value = tmp;
		}
		hndlr_utilStopEvent(event);
		return false;
	}
	
	//obj.exValue = obj.value;
	return true;
}

function hndlr_utilStopEvent(ev) {
	if(ev.preventDefault) {
		ev.preventDefault();
		ev.stopPropagation();
	} else {
		ev.cancelBubble = true;
	}
}


function toregexp2(txt) {
	txt = txt.replace(/([-\/\[\]()\*\+])/g, '\\$1');
	txt = txt.replace(/[MDY]/ig, '\\d');
	txt = txt.replace(/\?/g, '.');
	txt = txt.replace(/\./g, '\\\.');
	return txt;
}

function editDateBlur(obj, mask) {
	var flag = true;
	if (obj.value.length != 10 && obj.value.length > 0) {
		date = getInputDate(obj.value, mask);
		y = date.getFullYear();
		m = date.getMonth()+1;
		d = date.getDate();
		if (m<10) m = "0"+m;
		if (d<10) d = "0"+d;

		tmp = mask;
		tmp = tmp.replace(/D+/i, d);
		tmp = tmp.replace(/M+/i, m);
		tmp = tmp.replace(/Y+/i, y);
		obj.value = tmp;
	}
}

function editDate(obj, mask, event) {
	var keyCode = event.keyCode;
	if (keyCode == 0) {
		keyCode=event.charCode;
	}

	// correct the input size
	var size = obj.value.length;
	if (size > mask.length) {
		obj.value = obj.value.substr(0, mask.length);
		size = obj.value.length;
	}

	// validsate the input value with the masks' regexp
	var re = new RegExp('^' + toregexp2(mask.substr(0, size)) + '$');
	if (!obj.value.match(re)) { 
		if (obj.lastMatched) {
			obj.value = obj.lastMatched;
		} else {
			obj.value = ''; // obj.exValue;
		}
	} else {
		obj.lastMatched = obj.value;
	}

	// re-get the text size
	size = obj.value.length;

	//if we have entered 10 chars that means we have entered a potential date
	//check this date and convert it eventually to a valid date
	if (size == 10) {

		var date = getInputDate(obj.value, mask);

		y = date.getFullYear();
		m = date.getMonth()+1;
		d = date.getDate();

		if (m<10) m = "0"+m;
		if (d<10) d = "0"+d;

		while (y.length < 4) {
			y = "0" + y;
		}

		tmp = mask;
		tmp = tmp.replace(/D+/i, d);
		tmp = tmp.replace(/M+/i, m);
		tmp = tmp.replace(/Y+/i, y);
		if (obj.value != tmp) {
			obj.value = tmp;
		}
	} else {
		if (event.keyCode!=8) {
			completeSmartDate(obj,mask);
		}
	}
}

function getInputDate(value, mask) {
	value = value.substr(0,10);
	if(value.length == 0) {
		return new Date();
	}

	var d=0;
	var m=0;
	var y=0;
	if (mask.match(/D+.M+.Y+/i)) {
		d = value.substr(0,2);
		m = value.substr(3,2);
		y = value.substr(6,10);
	} else if (mask.match(/M+.D+.Y+/i)){
		m = value.substr(0,2);
		d = value.substr(3,2);
		y = value.substr(6,10);
	} else if (mask.match(/Y+.M+.D+/i)){
		y = value.substr(0,4);
		m = value.substr(5,2);
		d = value.substr(8,10);
	}

	if (y.length == 2) {
		if (y<50) {
			y = "20" + y;
		} else {
			y = "19" + y;
		}
	}

	m--;

	//compute date at 12:00 PM  to avoid time saving problems
	date = new Date(y, m, d, 12 , 1 ,1);

	return date;
}


/**
	complete the current typing text with the next char from the mask
	@param
		obj - SmartDate DOM Object
		mask - the Mask
	
**/
function completeSmartDate(obj, mask) {
	var size = obj.value.length;
	var sw=true;
	while (sw) {
		if (mask.length<=size) {
			break;
		}
		switch (mask.charAt(size)) {
			case 'M':
			case 'D':
			case 'Y':
				sw = false;
				return;
			default:
				obj.value += mask.charAt(size);
		}
		size++;
	}
	obj.lastMatched = obj.value;
}

function hideSelects()
{
	for( i = 0; i < document.all.tags( "select" ).length; i++ )
    {
        document.all.tags( "select" )[i].style.visibility = "hidden";
    }
    
    /*
    for( i = 0; i < document.all.tags( "select" ).length; i++ )
    {
        obj = document.all.tags( "select" )[i];       
        obj.style.visibility = "hidden";

    }
    */
}

function showSelects()
{
   
    for( i = 0; i < document.all.tags( "select" ).length; i++ )
    {
        obj = document.all.tags( "select" )[i].style.visibility = "visible";

    }
    /*
    for( i = 0; i < document.all.tags( "select" ).length; i++ )
    {
        obj = document.all.tags( "select" )[i];       
        obj.style.visibility = "visible";

    }
    */
}