function IsNumeric(strString)
   //  check for valid numeric strings
{
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function ValidateNumeric(e,sAllow) {	

	KeyID = e.which||e.keyCode;

	if (KeyID > 47 && KeyID < 58 || KeyID == 8) {
		return;
	} else {
		if (window.event){
			e.returnValue = false;
		} else {
			e.preventDefault();
		}
	}
}

function CheckPlus4(oField) {
	
	thisField = oField.name;
	CurrVal = oField.value;
	CurrValLen = oField.value.length;
	if (CurrValLen > 0) {
		if (IsNumeric(CurrVal)) {
			if (CurrVal == '0000') {				
				alert("0000 is not valid. Please contact your\nlocal Post Office for correct Zip+4 information!");
				setTimeout("document.getElementById(thisField).select()", 5);				
			} else if (CurrValLen < 4) {
				alert(CurrVal + " is not valid Zip+4 information. Please contact\nyour local Post Office for correct Zip+4 information!");
				setTimeout("document.getElementById(thisField).select()", 5);				
			}
		} else {
			alert("You must provide numeric data for the zip plus 4 data!");
			setTimeout("document.getElementById(thisField).select()", 5);			
		}
	}
}

///Function: Generic Form Validation
///Purpose: This script can validate any form
///Input: Form to be checked, passed as "document.<formname>"
///Output: True/False for submittal of form
///Prerequisites: Hidden form field with name "required" that has comma separated list of fields to be checked
///Conditions: Only checks that one checkbox is checked, only one radio button is selected, and that text areas are not blank.
///Added by: Marc Giguere 
///Date Added: 6-16-2004
///Date Update: 9-15-2005
///Change Log: 09/15/05 - MG - Added ability to do text areas, passwords, and file fields...
function validateForm(what,coloroverride) {

	var tmpValue = -1;
	var valid = true;
	var fieldtofix = "";
	var highlight = "";
	var friendlyname = "";
	//Make sure that we got a value for doing the color override...
	if(coloroverride != '') {
		var colorchangecheck = coloroverride;
	} else {
		var colorchangecheck = 0;	
	}
	
	var checkBoxes = false;
	var checkboxChecked = false;
	
	var radioButtons = false;
	var radioChecked = false;
	
	var reqfields=document.getElementById('Required').value.split(',');
	
	// CHeck that we have elements to verify :)
	if (reqfields.length <= 0) {
		return valid;
	}
	
	MainLoop:
	for(i=0;i<reqfields.length;i++) {
	
		/// Check if this is new format where we pass in field name and "friendly name"
		reqcheck = reqfields[i].indexOf("|");
		if (reqcheck > 0) {
			tmpvar = reqfields[i].split('|');			
			reqfields[i] = tmpvar[0];
			friendlyname = tmpvar[1];
		} else {
			friendlyname = reqfields[i];
		}
	 
		//Get Type of field
		myType = eval("what." + reqfields[i] + ".type");
		
		//Check individual field length for fields with same name (ie: radio and checkboxes) 
		myLen = eval("what." + reqfields[i] + ".length");
		
		
		//This field has more than one name valued that is the same, but ignore this check for select boxes
		if(myLen > 0 && (myType != 'select-one' && myType != 'select-multiple')) {

			SecondLoop:
			for(var j = 0; j < myLen; j++) {
			
				if(radioChecked == true) {
					break;
				}
				
				if(checkboxChecked == true) {
					break;
				}
				
				myType = eval("what." + reqfields[i] + "[" + j + "].type");
		
				//Only check for radio/checkboxes as other fields shouldn't ever share same name
				if (myType == 'radio') {
					radioButtons = true;
					if (eval("what." + reqfields[i] + "[" + j + "].checked")) 
						radioChecked = true;						
					//else 
					//	if (fieldtofix == '') 
					//	fieldtofix = reqfields[i];
					//	highlight = 0;
				}
				
				if (myType == 'checkbox') {
					checkBoxes = true;					
					if (eval("what." + reqfields[i] + "[" + j + "].checked")) 
						checkboxChecked = true;						
					//else 
					//	if (fieldtofix == '') 
					//		fieldtofix = reqfields[i];
					//		highlight = 0;
				}			  
				
			}//End SecondLoop For
		
			// Done w/loop, check if we have to alert
			//alert(reqfields[i]);
			if ((myType == 'radio' && radioChecked == false) || (myType == 'checkbox' && checkboxChecked == false)) {
				if (fieldtofix == '')
					fieldtofix = reqfields[i];
					highlight = 0;
					valid = false;
			}
		
		//Single Named fields checks, only if we still are ok and didn't find a multi-checkbox/radio problem
		} else if (valid) {
		
	        if (myType == 'radio') {			  		
	            radioButtons = true;
	      	   if (eval("what." + reqfields[i] + ".checked")) {
						radioChecked = true;
					} else {
						valid = false;
						if (fieldtofix == '') {
							fieldtofix = reqfields[i];
							highlight = 0;
						}
					}
	        }
			  
	        if (myType == 'checkbox') {
	            checkBoxes = true;
	            if (eval("what." + reqfields[i] + ".checked")) {
						checkboxChecked = true; 
					} else {
						valid = false;
						if (fieldtofix == '') {
							fieldtofix = reqfields[i];
							highlight = 0;
						}
					}
	        }
			  
	        if (myType == 'text') {
			  		tmpValue = eval("what." + reqfields[i] + ".value");
					if (colorchangecheck) {
						eval("what." + reqfields[i] + ".style.backgroundColor = '#FFFFFF'");
						eval("what." + reqfields[i] + ".style.color = '#000000'");
					}
	            if (tmpValue == '') {
						valid = false;
						if (fieldtofix == '') {
							fieldtofix = reqfields[i]; 
							highlight = 1;
						}
					}
			  }
			  
			   if (myType == 'textarea') {
			  		tmpValue = eval("what." + reqfields[i] + ".value");
					if (colorchangecheck) {
						eval("what." + reqfields[i] + ".style.backgroundColor = '#FFFFFF'");
						eval("what." + reqfields[i] + ".style.color = '#000000'");
					}
	            if (tmpValue == '') {
						valid = false;
						if (fieldtofix == '') {
							fieldtofix = reqfields[i]; 
							highlight = 1;
						}
					}
			  }
			  
			   if (myType == 'password') {
			  		tmpValue = eval("what." + reqfields[i] + ".value");
					if (colorchangecheck) {
						eval("what." + reqfields[i] + ".style.backgroundColor = '#FFFFFF'");
						eval("what." + reqfields[i] + ".style.color = '#000000'");
					}
	            if (tmpValue == '') {
						valid = false;
						if (fieldtofix == '') {
							fieldtofix = reqfields[i]; 
							highlight = 1;
						}
					}
			  }
			  
			   if (myType == 'file') {
			  		tmpValue = eval("what." + reqfields[i] + ".value");
					if (colorchangecheck) {
						eval("what." + reqfields[i] + ".style.backgroundColor = '#FFFFFF'");
						eval("what." + reqfields[i] + ".style.color = '#000000'");
					}
	            if (tmpValue == '') {
						valid = false;
						if (fieldtofix == '') {
							fieldtofix = reqfields[i]; 
							highlight = 1;
						}
					}
			  }
			  
	        if (myType == 'select-one' || myType == 'select-multiple') {
			  		tmpValue = eval("what." + reqfields[i] + ".selectedIndex");
			  		if (colorchangecheck) {
						eval("what." + reqfields[i] + ".style.backgroundColor = '#FFFFFF'");
						eval("what." + reqfields[i] + ".style.color = '#000000'");
					}
	            if (tmpValue == '' || tmpValue == '-1') {
						valid = false;
						if (fieldtofix == '') {
							fieldtofix = reqfields[i]; 
							highlight = 1;
						}
					}
			  }
			  
		}
				
    if (!valid) {
	 	break;
	 }
	 
	 }
	
    if ((checkBoxes && !checkboxChecked)) valid = false;
	 
	if ((radioButtons && !radioChecked)) valid = false;
	 
    if (!valid)		
	 	if (highlight) {
		  eval("what." + fieldtofix + ".style.backgroundColor = '#FF0000'");
		  eval("what." + fieldtofix + ".style.color = '#FFFFFF'");
 		  eval("what." + fieldtofix + ".focus()");
        alert('You have not completely filled out the form.\n\nPlease verify that all required fields are filled\nand/or at least one option is selected.\n\nPlease correct the ' + friendlyname + ' field, which is highlighted.');		  		  
		} else {
			//alert(fieldtofix);
			alert('You have not completely filled out the form.\n\nPlease verify that all required fields are filled\nand/or at least one option is selected.\n\nPlease correct the ' + friendlyname + ' field.');
		}
    return valid;
}


///Function: Basic Email Validation
///Purpose: This script can validate any email field to ensure a properly formatted email is provided 
///Input: Email field to be checked, pass in as string
///Output: True/False for submittal of form
///Added by: Marc Giguere/Thomas Kumpik 
///Date Added: 7-20-2004 
///Date Modified: 4-6-2006
///Change Log:
///Changed to use object version
function IsEmailValid(sEmail)
{

	// Use Regular Expressions for Email Verification
	// Credit Reg-Ex Check: Gavin Sharp (http://www.glensharp.com/gavin/)
	REEmail = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/
	
	//Check if blank, if so, ignore for now
	if (sEmail.value == '') {
		return true;
	} else {
		if (!sEmail.value.match(REEmail)) {
			alert("Please enter a valid email address.");	
			sEmail.focus();
			sEmail.select();
			return false;
		} else {
			return true;
		}
	}
}
///Function: Auto-tab Fields
///
/// Input/Usage: OnKeyUp="autotab(currentfield,nextfield,lengthtocheck)"

function autotab(object1, object2, objectsize){
      if (object1.value.length == objectsize)
		    object2.focus()
}

///Monitoring Page Functions

// Drill Down Function
function ViewDetail(JobID, StateID, Zip, PieceCount, DetailType) {

	//State Drill Down
	if (DetailType == 1) {
		location.href = 'index.cfm?fuseaction=JobsManager.TrackDetails&JobID=' + JobID + '&StateID=' + StateID;
	//Five Zip Drill Down
	} else if (DetailType == 2) {
		location.href = 'index.cfm?fuseaction=JobsManager.TrackDetailsByZip&JobID=' + JobID + '&StateID=' + StateID + '&Zip=' + Zip + '&Pieces=' + PieceCount;
	//History Drill Down
	} else if (DetailType == 3) {
		location.href = 'index.cfm?fuseaction=JobsManager.TrackDetailsHistory&JobID=' + JobID + '&StateID=' + StateID + '&Zip4DP=' + Zip;
	}
	
}

function ShowHideDetails(RefID) {
	
	//Check if we are showing or hiding it
	var CurrElement = document.getElementById('ShowHide_' + RefID);
	var CurrBlock = document.getElementById('RowDetail_' + RefID);
	
	if (CurrElement.src.search("Show") > 0) {
		CurrBlock.style.display = 'block';
		CurrElement.src = "/images/but-HideDetails.gif";
	} else {
		CurrBlock.style.display = 'none';
		CurrElement.src = "/images/but-ShowDetails.gif";
	}
	
}

function GoBackNext(JobID, StateID, Zip, PieceCount, StartRow) {
	
	location.href = 'index.cfm?fuseaction=JobsManager.TrackDetailsByZip&JobID=' + JobID + '&StateID=' + StateID + '&Zip=' + Zip + '&Pieces=' + PieceCount + '&StartRow=' + StartRow;
	
}

function CheckRptType(RptType) {

	if (RptType == 1) {
	
		document.getElementById('Date1').style.display = '';
		document.getElementById('Date2').style.display = '';
		
	} else if (RptType == 2) {
	
		document.getElementById('Date1').style.display = 'none';
		document.getElementById('Date2').style.display = 'none';
	
	}

}