// JavaScript source code
function isNumUpperDashSpaceKey(e){
	var key = String.fromCharCode(e.which?e.which:e.keyCode);
	if ((key>='0' && key<='9') || (key>='A' && key<='Z') || key=='-' || key==' ') {
		return true;
	}
	return false;
}
function isnumerickey(e) {
	if (isnavkey(e))
		return true;
	var key = String.fromCharCode(e.which?e.which:e.keyCode);
	if ((key>='0' && String.fromCharCode(window.event.keyCode)<='9') || key=='.' || key=='-' || key==',') {
		return true;
	}
	return false;
}	
function isvalidfilenamekey(e) {
	if (isnavkey(e))
		return true;
	var key = String.fromCharCode(e.which?e.which:e.keyCode);
	if ((key>='0' && String.fromCharCode(window.event.keyCode)<='9') || key=='.' || key=='-' || key=='_' || (key>='A' && key<='Z') || (key>='a' && key<='z')) {
		return true;
	}
	return false;
}	
function cleanupnumeric(thisptr){
	if (thisptr){
		var s=String(thisptr.value);
		var snew='';
		var isneg=false;
		var gotpoint=false;
		for(i=0;i<s.length;i++){
			if (s.charAt(i)=='-'){
				isneg=true;
			}else if(s.charAt(i)=='.'){
				if (gotpoint==false){
					snew += '.';
					gotpoint = true;
				}
			}else if (s.charAt(i)==','){
				// no-op, we'll put commas back in later.
			}else{
				snew += s.charAt(i);
			}
		}
		snew = insertcommas(snew);
		if (isneg)
			snew = "-"+snew;
		thisptr.value = snew;
	}
}
function cleanupinteger(thisptr){
	if (thisptr){
		var s=String(thisptr.value);
		var snew='';
		var isneg=false;
		for(i=0;i<s.length;i++){
			if (s.charAt(i)=='-'){
				isneg=true;
			}else if(s.charAt(i)=='.'){
				// no-op.  not allowed.
			}else if (s.charAt(i)==','){
				// no-op, we'll put commas back in later.
			}else{
				snew += s.charAt(i);
			}
		}
		snew = insertcommas(snew);
		if (isneg)
			snew = "-"+snew;
		thisptr.value = snew;
	}
}
function isintegerkey(e) {
	if (isnavkey(e))
		return true;
	var key = String.fromCharCode(e.which?e.which:e.keyCode);
	if ((key>='0' && key<='9')|| key=='-' || key==','){
		return true;
	}
	return false;
}	
function isintegeronlykey(e) {
	if (isnavkey(e))
		return true;
	var key = String.fromCharCode(e.which?e.which:e.keyCode);
	if ((key>='0' && key<='9')){
		return true;
	}
	return false;
}	
function isnavkey(e) {
	// this is primarily executed for netscape because IE doesnt give us nav keys.
	if (document.all) {
		return false;  // ie never uses this.
	} else if (parseInt(e.which)>0 && parseInt(e.which)!=8) {
		return false;
	}
	return true;		
//	if (k==8 || k==0)//(k>33 && k<47))
//		return true;
}
function isfilenamekey(e) {
	if (isnavkey(e))
		return true;
	var checkstr = new String("/\\:*?\"<>|");
	var key = String.fromCharCode(e.which?e.which:e.keyCode);
	if (checkstr.indexOf(key)!=-1) {
		if (e.keyCode)
			e.returnValue=false;
		return false;
	}
	return true;
}
function iscurrencykey(e,thisptr) {
	if (isnavkey(e))
		return true;
	var haspoint=false;
	var decimalplaces=0;
	var isinteger=isintegerkey(e);
	var key = String.fromCharCode(e.which?e.which:e.keyCode);
	var ispoint=(key=='.');
	var isnegative=(key=='-');
	var iscomma=(key==',');
	
	if (!isinteger && !ispoint && !isnegative && !iscomma)
		return false;
		
	var s = new String(thisptr.value);
	
	if (isnegative) {
		if (s.length==0)
			return true;
		else {
			if (s.charAt(0)!='-') {
				var ts = '-'+s;
				thisptr.value=ts;
			}
			return false;
		}
	}

	for(i=0;i<s.length;i++) {
		if (haspoint) {
			decimalplaces++;
			// only allow 2 decimal places.
			//if (decimalplaces==2) 
			//	return false;
		} else if (s.charAt(i)=='.') {
			// if we already have a decimal point and we are trying to enter another, stop.
			if (ispoint)
				return false;
			haspoint=true;
		}
	}

	return true;
}	

function isdatekey(e){
	var dateKeys = "0123456789/-."
	if (isnavkey(e))
		return true;
	var key = String.fromCharCode(e.which ? e.which : e.keyCode);
	if (dateKeys.indexOf(key) >= 0)	{
		return true;
	}

	return false;
}
function cleanupcurrency(thisptr){
	if (thisptr){
		var s=String(thisptr.value);
		var snew='';
		var isneg=false;
		for(i=0;i<s.length;i++){
			if (s.charAt(i)=='-'){
				isneg=true;
			}else if(s.charAt(i)=='$'){
				// no-op.  not allowed.
			}else if (s.charAt(i)==','){
				// no-op, we'll put commas back in later.
			}else{
				snew += s.charAt(i);
			}
		}

		// ok, new code. make sure we're not > 1 million.
		s = snew;
		var idx = snew.indexOf(".");
		if (idx!=-1){
			s = snew.substring(0,idx);
		}else{
			s = snew;
		}
		if (s.length>7){
			// don't go over 1 million dollars.
			snew = "1000000.00";
		}else{
			var theval = parseInt(s);
			if (theval>1000000){
				snew = "1000000.00";
			}
		}
		
		snew = formatmoney(snew);
		var moneytype = thisptr.getAttribute('moneytype');
		if (moneytype==1){
			// we need to trunc the decimal
			var idx = snew.indexOf('.');
			if (idx>=0){
				snew = snew.substring(0,idx);
			}
		}
		if (isneg)
			snew = "-"+snew;
		thisptr.value = snew;
	}
}
function formatmoney(value){
	var s = new String(value);
	if (s.length>0) {
		s = insertcommas(s);
		var idx = s.indexOf('.');
		if (idx==-1) {
			s += '.00';
		} else if (s.length-idx<=2) {
			var howmanyzeros = 2-(s.length-idx-1);
			for(i=0;i<howmanyzeros;i++)
				s += "0";
		}else{
			// more numbers than we need.
			s=s.substring(0,idx+3);
		}
	
		//s= "$"+s;
	}
	if (s.indexOf('-')>=0) {
		s = ("<font color=RED>"+s+"</font>");
	}
	return s;
}	
function insertcommas(value) {
	var s = new String(value);
	var sprefix = new String();
	if (s.length>0) {
		while(true) {
			if (s[0]=='$' || s[0]=='-') {
				sprefix += s[0];
				s = s.substr(1,999);
			} else {
				break;
			}
		}
		var idx = s.indexOf('.');
//	22222.1
//	22222
		if (idx==-1) {
			idx = s.length;
		} 
		while((idx-3)>0) {
			idx -=3;
			s = (s.substring(0,idx) + "," + s.substr(idx,999));
		}
	}
	return sprefix + s;
}

function isDigit(c) 
{ 
	var digitArray = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
	var i; 

	for (i = 0; i < digitArray.length; i++) 
	{
		if (c == digitArray[i]) 
			return true 
	} 

	return false 
} 
function padYear(s)
{
	if (s.length == 1)
		s = "200" + s;
	if (s.length == 2)
		s = "20" + s;
	if (s.length == 3)
		s = "2" + s;

	return s;
}
function toDateStr(s)
{
	var i;
	var tmp = "";
	var result = "";
	var mm = "";
	var dd = "";
	var yy = "";
	var yyyy = "";
	var d = new Date();
	d.setDate(1);
	d.setMonth(0);

	if (s.length == 0)
		return result;

	// replace all non-digit with '/'
	for (i = 0; i < s.length; i++)
	{
		if (isDigit(s.charAt(i)))
		{
			tmp = tmp + s.substr(i, 1);
		}
		else
		{
			tmp = tmp + "/";
		}
	}

	var a = tmp.split("/");
	var ok= true;
	switch (a.length)
	{
		case 1:	// dd
			dd = a[0];
			d.setDate(dd);
			mm = (d.getMonth()+1);
			dd = d.getDate();
			yy = d.getFullYear();
			break;

		case 2:	// mm/dd or mm/ or /dd
			mm = a[0];
			dd = a[1];
			d.setDate(dd.length ? dd : d.getDate());
			d.setMonth(mm.length ? mm - 1 : d.getMonth());
			
			mm = (d.getMonth()+1);
			dd = d.getDate();
			yy = d.getFullYear();
			break;

		case 3:	// mm/dd/yy or mm/dd/yyyy or mm/dd/
			mm = a[0];
			dd = a[1];
			yyyy = a[2];
			yyyy = padYear(yyyy);
			d.setDate(dd.length ? dd : d.getDate());
			d.setYear(yyyy.length ? yyyy : d.getYear());
			d.setMonth(mm.length ? (parseInt(mm)-1) : d.getMonth());
			mm = (d.getMonth()+1);
			dd = d.getDate();
			yy = d.getFullYear();
			break;

		default:	
			result = "";
			ok=false;
			break;
	}
	if (ok) {
		// check for leap.
//		var y4 = yy;
//		if (isNaN(y4)==false && ((y4%4)!=0)){
//			var m2  = mm;//parseInt(mm);
//			var d29 = dd;//parseInt(dd);
//			if (isNaN(m2)==false && m2==2 && isNaN(d29)==false && d29==29){
//				dd = '28';
//			}
//		}
		// complete the formatting
		result = "";
		var val = String(mm);
		if (val.length==1)
			result+="0";
		result += mm + "/";
		val = String(mm);
		if (val.length==1)
			result+="0";
		result += dd + "/";
		val = String(yy);
		result += val.substring((val.length-2),99);
		//}else{
		//	result += yy;
		//}
	}
	
	return result;
}
function checkDateCtrl(ctrl){
	var err=false;
	var ss ="";
	var today=new Date();
	var day=today.getDate();
	var month=parseInt(today.getMonth())+1;
	var year=today.getFullYear();
	
	var s = String(ctrl.value);
	if (s.length > 0){
		var idx = s.indexOf("/");
		if (idx==-1)
			idx==s.indexOf("-");
		if (idx==-1){
			day = parseInt(s);
		}else{
			// month is first
			var sm = s.substring(0,idx);
			month = parseInt(sm);
			// now get the day part.  we ignore year for now.
			idx++;
			var idx2 = s.indexOf("/",idx);
			if (idx2==-1)
				idx2==s.indexOf("-",idx);
			if (idx2==-1) 
				idx2=999;
			var sd = s.substring(idx,idx2);
			if (sd.length>0) {
				day = parseInt(sd);
			}
		}
		
		if (day==0){
			err = true;
			alert("Invalid Date\nDay cannot be 0.");
		}else if (month<1 || month>12){
			err = true;
			alert("Invalid Date\nMonth must be 1 thru 12.");
		}
		if (day>0){
			var daycheck = 31;
			if (month==0)
				month = parseInt(today.getMonth())+1;
			if ((month==4 || month==6 || month==9 || month==11)&&(day>30)){
				err = true;
				alert("Invalid Date\nDay must be 30 or less for Month "+month+".");
			}else if (month==2 && day>29){
				err = true;
				alert("Invalid Date\nDay must be 29 or less for Month "+month+".");
			}else if (day>31){
				err = true;
				alert("Invalid Date\nDay must be 31 or less for Month "+month+".");
			}
		}
		if (err==false){
			ss = toDateStr(s);
			if (!ss.length > 0) {
				err=true;
				alert("Date is not a valid mm/dd/yyyy format!");
			}
		}
	}
	
	if (err) {
		ctrl.value = s;
		ctrl.select();
		ctrl.focus();
		return false;
	}else{
		ctrl.value = ss;
	}

	return true;
}

function checkLength(pobj,msg,type)
{
	if (msg==null)
		msg = "Item";
	if (type==null)
		type="characters";
	var s = String(pobj.value);
	if (s.length==0)
		return true;
	if (s.length<pobj.maxLength){
		alert(msg+" must be " + pobj.maxLength + " " + type);
		pobj.focus();
		return false;
	}
	return true;
}

function isEmail(_emailObj) 
{
	if (_emailObj!=null)
	{
		var str = String(_emailObj.value);
		//if (str.length>0 && /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str) == false)
		if (str.length>0 && str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1)
		{
			alert("You must enter a valid email address.");
			_emailObj.focus();
			return false;
		}
	}
	
	return true;
}

function ShowErrorMsg( _ctlId, _bShow, _strMsg )
{
	var hideval = "hidden";
	if( _bShow == true )
		hideval = "visible";

	var obj = document.getElementById( _ctlId+"_emDIV" );
	if( obj )
	{
		obj.style.visibility = hideval;
		
		var ctlTag = document.getElementById( _ctlId+"_lblError" );
		if( ctlTag )
		{
			ctlTag.style.visibility = hideval;
			ctlTag.innerText = _strMsg;
		}
	}
}

