function FlipImg(id,cnt){
for(i=1;i<=cnt;i++){
	Img = new String(document.getElementById("img"+i).src);
	if(Img.indexOf("menuon") != -1){
		document.getElementById("img"+i).src = "images/shim.gif";
		break;
		}
}
document.getElementById("img"+id).src = "images/menuon.gif";
}

function CheckIt(Frm,html,multiform)
{
var FrmElems = Frm.elements;
var NeedVal;
var Error = 0;
var Msg = '';
var ErrorHold = '';
var i;

DateList='';

for (i=0;i<FrmElems.length;i++)
	{
		//Get the parts of the object name
		FieldType = FrmElems[i].valid;		
		Required = FrmElems[i].required;	//Get Required Value r or n	
				
		//Apply the validation
		if (FieldType != undefined)
		{
			if(Required == undefined)Required=0;
			//FrmElems[i].style.backgroundColor="#ffffff";
			Msg += Validate(FieldType,Required,FrmElems[i],Frm,html);
		}
		continue;
	}
	
	if (Msg != '')
	{
		if(multiform != 1){
			if(html){
				popup('#000000',0,1,'Validation Errors','The following errors were due to missing or invalid information:<br><br>' + Msg)		
			}else{
				alert("The following errors were due to\nmissing or invalid information:\n\n" + Msg);
			}
			return false;
		
		}else{return Msg;}
	}
	else
	{
		if(multiform == 1)return Msg;else return true;
	}
}
//====================================================================
function Validate(FldType,Req,obj,Frm,html)
{
//Assign Validations to array and loop thru them
//ValArray = Split(FldType,"|");
var ValArray = FldType.split("|");

var message = '';

  obj.style.backgroundColor="#ffffff";
  
  for(var i=0;i<ValArray.length;i++){
	
	FldType = ValArray[i];

	//format label name
	lblval = (obj.type == 'radio')?eval("Frm."+obj.name+"[0].lbl"):obj.lbl;
	if(!html){
		LblName = "[" + lblval + "] ";
		nl = "\n";		
	}else{
		LblName = "<b>" + lblval + ":</b> ";
		nl = "<br>";		
	}
		
	//Look for special validations (:)
	colon = FldType.indexOf(':');
	if(colon != -1){
		//get validation type and its value
		valCriteria = FldType.substr(colon+1);
		FldType = FldType.substr(0,colon);
		}
	
	switch (FldType)
	{
	//================ Date Check ==========================
	case 'date':  
		if(obj.value == "" && Req != 1) break;
		if(obj.value == "" && Req == 1)
		{
			message += LblName+'A date must be entered'+nl;
			break;

		}else{			
			var d = obj.value.split("/");
			mm = d[0];
			dd = d[1];
			yy = d[2];	
		
			if ((Trim(mm) == "" && Trim(dd) == "" && Trim(yy) == "") && (Req == 1))
			{
				message += LblName+'A date must be entered'+nl;
				break;
			}		
			if (!(isYear(yy) && isMonth(mm) && isDay(dd)))	
			{
				message += LblName+'Invalid date entered'+nl;
			}
			break;
		}		
	//================ Time Check =====================	
	case 'time': 		
		//hh = eval("document."+Frm+".DF_"+FldID+"x1y"+FldType+"z"+ValType+Req+".value");
		//ss = eval("document."+Frm+".DF_"+FldID+"x2y"+FldType+"z"+ValType+Req+".value");
		//ampm = eval("document."+Frm+".DF_"+FldID+"x3y"+FldType+"z"+ValType+Req+".selectedIndex");	
		//remove leading zeros
		if (hh.substring(0,1) == '0') hh = Trim(hh.substring(1));
		if (ss.substring(0,1) == '0') ss = Trim(ss.substring(1));	
		if ((Trim(hh) == "" || Trim(ss) == "" || ampm == 0) && (Req == 1))
		{
			message += LblName+'A time must be entered'+nl;
			break;
		}else{		
			if ((Trim(hh) != "" && Trim(ss) != "" && ampm != 0))		
			{
				if (!isTime(hh,ss,ampm))
				{message += LblName+'Invalid time entered'+nl;}
			}
			else
			{
				if (Req == 1)message += LblName+'Incomplete time entered'+nl;
			}
		}	
		break;
	//================ Numeric Check =====================		
	case 'numeric':		
		if ((Req == 1 && obj.value == '')){
			message += LblName+'A value must be entered'+nl;
		}else{
			if ((obj.value != '') && isNaN(obj.value)){message += LblName+'Must contain only numbers'+nl;}
		}	
		break;
	//================ Radio Check =====================		
	case 'radio':		
		
		robj=eval("Frm."+obj.name);
		
		Checked = false;		
		for(var j=0;j<valCriteria;j++){
			if (robj[0].Required == 1){
				 if (robj[j].checked){
					Checked = true;
					break;
				}
			}
		}		
		if (!Checked){message += LblName+'A value must be selected'+nl;}		
		break;	
	//================ Checkbox Check =====================		
	case 'checkbox':		
		
		var elem=Frm.elements;
		Checked = false;		
		for(var j=0;j<elem.length;j++){
			if(elem[j].type == 'checkbox' && elem[j].name.indexOf(valCriteria) != -1){
			 if (elem[j].checked){
					Checked = true;
					break;
				}
			}
		}		
		if (!Checked){message += LblName+'A value must be selected'+nl;}		
		break;	
	//================ Alphas Check =====================	
	case 'alpha':		
		if ((Req == 1 && obj.value == '')){message += LblName+'A value must be entered'+nl;
		}else{
			if ((obj.value != '') && (!isAlpha(obj.value))){message += LblName+'Can not contain numbers'+nl;}
		}	
		break;	
	//================ List Selection Check =====================		
	case 'list':		
		if (obj.selectedIndex == -1 || obj.selectedIndex == 0)
		{message += LblName+'A value must be selected'+nl;}		
		break;
	case 'listnotnull':
		if (obj.length<1)
		{message += LblName+'A value must be entered'+nl;}		
		break;
	case 'notnull':		//Must contain a value check
		if ((Req== 1) && (obj.value == ''))
		{message += LblName +'A value must be entered'+nl;
		break;}		
		if (obj.value == '') 
		{message += LblName+'Must contain a value'+nl;}		
		break;
	//================ Date Compare Check =====================		
	case 'dtcompare':	

			//End Date
			var d1 = obj.value.split("/");
			mm = d1[0];
			dd = d1[1];
			yy = d1[2];
			
			//Begin Date
			var dt2 = eval("Frm."+valCriteria);
			
			if(!html)LblName2 = "[" + dt2.lbl + "] ";else LblName2 = '<b>'+dt2.lbl+'</b>';
			
			if (obj.value != '' && dt2.value == '')
			{message += 'Cannot have '+ LblName + ' without ' +  LblName2 +nl;
			break;
			}		
			
			var d2 = dt2.value.split("/");
			mm2 = d2[0];
			dd2 = d2[1];
			yy2 = d2[2];
			
			var BegDate = new Date(mm2 +"/" + dd2 + "/" + yy2);
			var EndDate = new Date(mm +"/" + dd + "/" + yy);
			
		//Compare the dates
			if (EndDate < BegDate)
			{message += LblName + ' is prior to ' + LblName2 +nl;}		
		break;
	//================ Zip Code Check =====================	
	case 'zip':	
		var stripped;
		if ((Req == 1 && obj.value == ''))
		 {
			message += LblName+'A value must be entered'+nl;
		}		
		else
		{
			if (obj.value != '') 
			{
				stripped = removeAlphas(obj.value);
				if (!isNumeric(stripped))
				{message += LblName+'Invalid value entered'+nl;}
				else
				{
					if ((stripped.length < 5) || (stripped.length > 9) || ((5 < stripped.length) && (stripped.length < 9))) {
						message += LblName+'Invalid value entered'+nl;
					}
				}
			}
		}	

		break;
	//================ Phone Check =====================	
	case 'phone':	
		if ((Req == 1 && obj.value == ''))
		 {message += LblName+'A value must be entered'+nl;
		}else{
			if (obj.value != '') 
			{
				//look for ext. or x (extension) and remove
				var strPhone = new String(obj.value);
				var xhit;
				xhit = strPhone.indexOf("X");
				if (xhit == -1) xhit = strPhone.indexOf("x");
				if (xhit == -1) xhit = strPhone.indexOf("X");
				
				if (xhit > 0){strPhone = strPhone.substring(0,xhit);}
				
				stripped = removeAlphas(strPhone);
				
				if (!isNumeric(stripped))
				{message += LblName+'Invalid value entered'+nl;
				}else{
					if ((stripped.length < 7) || (stripped.length > 10) || ((7 < stripped.length) && (stripped.length < 10))) {
						message += LblName+'Invalid value entered'+nl;
					}
				}
			}
		}	

		break;
	//================ E-Mail Check =====================		
	case 'email':	
		if ((Req == 1 && obj.value == ''))
		 {message += LblName+'A value must be entered'+nl;
		}else{
			var mailaddr = new String(obj.value);
			//check for @ and . in address value
			atsign = mailaddr.indexOf("@");
			period = mailaddr.indexOf(".");
						
			if ((obj.value != '') && ((atsign == -1) || (period == -1) || (mailaddr.length < 5)))
			{message += LblName+'Invalid value entered'+nl;}
		}	
		break;	
	//================ SSN Check =====================		
	case 'ssn':		
		var stripped;
		if ((Req == 1 && obj.value == ''))
		 {message += LblName+'A value must be entered'+nl;
		}else{
			if (obj.value != ""){
				stripped = removeAlphas(obj.value);
				if (!isNumeric(stripped))
				{message += LblName+'Invalid value entered'+nl;}
			}
		}		
		break;	
	//================ Min Check =====================		
	case 'min':		
		if ((Req == 1 && obj.value == ''))
		 {message += LblName+'A value must be entered'+nl;
		}else{
			if (obj.value.length > 0 && obj.value.length < valCriteria)
			{message += LblName+'Value entered must contain at least '+valCriteria+' characters'+nl;}
		}		
		break;
	//================ Max Check =====================		
	case 'max':		
		if ((Req == 1 && obj.value == ''))
		 {message += LblName+'A value must be entered'+nl;
		}else{
			if (obj.value.length > 0 && obj.value.length > valCriteria)
			{message += LblName+'Value entered must not exceed '+valCriteria+' characters'+nl;}
		}		
		break;
	default:
		break;		
	}
  }
  
  if (FldType == 'radio' || FldType == 'checkbox')
	bg = eval("Frm.style.backgroundColor");
  else
	bg = "#ffffff";
  
  if (message != ''){
  		obj.style.backgroundColor = "#FFFFCC";
  		obj.style.color = "#000000";
  
  }else{
  		obj.style.backgroundColor = bg;
  }
  
  return message;
}
//====================================================================
function removeAlphas(strIn)
{
var i;
var stripped = '';
if (Trim(strIn) == '') return strIn;

   for (i = 0; i < strIn.length; i++)
   {   var c = strIn.charAt(i);
	if ((47 < strIn.charCodeAt(i)) && (strIn.charCodeAt(i) < 58) || (64 < strIn.charCodeAt(i)) && (strIn.charCodeAt(i) < 91) || (96 < strIn.charCodeAt(i)) && (strIn.charCodeAt(i) < 123)) stripped += c;		
   }
return stripped;
}
//====================================================================
function Trim (String)
{
  if (String == null) return "" ;
  var start = 0 ;
  var end = String.length - 1 ;
  while (start < end && String.charAt(start) == " ") start++ ;
  while (end >= 0 && String.charAt(end) == " ") end-- ; 
  if (end < start) return "" ;
  return String.substring(start, end+1);
}
//====================================================================
function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}
//====================================================================
function isInteger (s)
{	
	if (Trim(s) == "") return false;
	else
	{
		if (Trim(s) == "") return true;
	}
	retval = true;
	
	for (i = 0; i < s.length; i++)
	{	var c = s.charAt(i);
		if ((c < "0") || (c > "9"))
		{	retval = false;
			break;
		}
	}
	return retval;
}
//====================================================================
function isNumeric (String)
{	var i;
	var decimalPointDelimiter = ".";
	var seenDecimalPoint = false;
    if (Trim(String) == "") return false; 
    if (String == decimalPointDelimiter) return false;
    for (i = 0; i < String.length; i++)
    {   var c = String.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    return true;
}
//====================================================================
function isAlpha (String)
{	var i;
    if (Trim(String) == "") return false; 
    for (i = 0; i < String.length; i++)
    {   var c = String.charAt(i);
		if (isDigit(c)) return false;
    }
    return true;
}
//====================================================================
function isTime (hour, minute, ampm)
{
	
	if (Trim(hour) == "" || Trim(minute) == "" || ampm == 0 ) return false;
	
	// Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var inthour = parseInt(hour,10);
    var intminute = parseInt(minute,10);
	    
	var retval = true;

	if ((inthour > 12) || (intminute > 59)) retval = false;
	
	return retval;
}
//====================================================================
function isDate (year, month, day)
{
	
    if (Trim(year) == "" || Trim(month) == "" || Trim(day) == "") return false;

	// catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (!(isYear(year) && isMonth(month) && isDay(day))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year,10);
    var intMonth = parseInt(month,10);
    var intDay = parseInt(day,10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}
//====================================================================
function isYear (s)
{
   	if (Trim(s) == "") return false; 
	if (!isInteger(Trim(s),false)) return false;
    if (parseInt(s,10) < 0) return false;

    return (Trim(s).length == 4 && Trim(s) >= 1900);
}
//====================================================================
function isMonth (s)
{
   	if (Trim(s) == "") return false; 
	if (!isInteger(Trim(s))) return false;

    return (parseInt(s,10) >= 1 && parseInt(s,10) <= 12);
}
//====================================================================
function isDay (s)
{
   	if (Trim(s) == "") return false; 
	if (!isInteger(Trim(s))) return false;

    return (parseInt(s,10) >= 1 && parseInt(s,10) <= 31);
}
//====================================================================
function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
//====================================================================
function VerifyRange (start, end, string)
{
    if (Trim(string) == "") return false;
	
    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intStart = parseInt(start,10);
    var intEnd = parseInt(end,10);
    var intString = parseInt(string,10);
    
    	var retval = true;
     
    	if (intString < intStart || intString > intEnd) retval = false;
	
    	return retval;
}

