//Checks for numeric digits
//Returns false if it does not, and true if not
//Can also pass other values to check
function checkNumeric(valToChk, otherVals) 
{
  var validChars = "0123456789" + otherVals;
  var temp;

  for (var count=0; count < valToChk.length; count++) 
  {
    temp = "" + valToChk.substring(count, count+1);
    if (validChars.indexOf(temp) == "-1") 
      return false;
  }
    
  return true;
}

//=================================================================================

//Checks for characters
//Returns false if it does not, and true if not
//Can also pass other values to check
function checkCharacter(valToChk, otherVals) 
{
  var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + otherVals;
  var temp;

  for (var count=0; count < valToChk.length; count++) 
  {
    temp = "" + valToChk.substring(count, count+1);
    if (validChars.indexOf(temp) == "-1") 
      return false;
  }
    
  return true;
}

//=================================================================================

//Checks the length of the current field.
//Useful for textareas which cannot have a maxlength element.
//onkeydown="checkLength(NUMBER, this);"
function checkLength(length, obj) 
{
	if(obj.value.length > length)
	{
		obj.value = obj.value.substring(0,obj.value.length-1);
		return false;
	}
}

//=================================================================================

//Checks for a valid postal code
//Returns true if it is a valid postal code and false if not
function validatePostalCode(pc)
{
  var test1 = pc.match(/[A-Z][0-9][A-Z][0-9][A-Z][0-9]/i);
  var test2 = pc.match(/[A-Z][0-9][A-Z] [0-9][A-Z][0-9]/i);
    
  if(test1 || test2)
    return true;
      
  return false;
}

//=================================================================================

//Gets all the elements from the querystring
//Call the QueryString() with the key you want to get, and it will return the value
//Call QueryString_Parse() to set up the arrays

QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}

function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}

//=================================================================================

/**************************************************************************/
//fieldJumper   - a function used to jump from one field to another when
//                a certain length has been reached.
//
//Parameters
//----------
//numOnJump     - a number used to determine when to move to the next field
//fieldToJumpTo - this is used to determine the next field to move the 
//                cursor to.
//location      - is the location of the field on the document.
//
//example: onkeyup="fieldJumper(3, 'txtAreaCode', 'txtPhoneNumber', 'document.form');"
//
/**************************************************************************/
function fieldJumper(numOnJump, currentFieldName, fieldToJumpTo, location) 
{
	var value     = eval(location + "." + currentFieldName + ".value");

	var intLength   = value.length;
	var keyCode		  = event.keyCode;
  
	if((keyCode != 9) && (keyCode !=16)) 
		if(intLength == numOnJump) 
		{
			eval(location + "." + fieldToJumpTo + ".focus()");
			eval(location + "." + fieldToJumpTo + ".select()");
		}
}

//=================================================================================

function checkValidTime(timeStr) 
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	
	var timePat = /^(\d{1,2})(:(\d{2}))?(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	
	var matchArray = timeStr.match(timePat);
	if (matchArray == null)
		return false;
	
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	
	if(second=="") 
		second = null;
	if(ampm=="") 
		ampm = null;
	
	if(hour < 0  || hour > 23) 
	{
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if(hour <= 12 && ampm == null) 
	{
		if(confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) 
		{
			alert("You must specify AM or PM.");
			return false;
		}
	}
	if(hour > 12 && ampm != null)
	{
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	if(minute != null && (minute<0 || minute > 59))
	{
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	
	if(second != null && (second < 0 || second > 59)) 
	{
		alert ("Second must be between 0 and 59.");
		return false;
	}
	
	return true;
}

//=================================================================================

/*
		var dateCheck = checkValidDate(form..value);
		
		if(dateCheck == 1)
		{
			alert("An invalid date was provided.\n\nPlease make sure you put the date in the following format...\n\n(mm/dd/yyyy)");
			form..focus();
			return false;
		}
		else if(dateCheck == 2)
		{
			alert("An invalid month was provided.\n\nPlease make sure you have entered a value between 1 and 12.");
			form..focus();
			return false;
		}
		else if(dateCheck == 3)
		{
			alert("An invalid day was provided.\n\nPlease make sure you have entered a value between 1 and 31.");
			form..focus();
			return false;
		}
		else if(dateCheck == 4)
		{
			alert("An invalid year was provided.\n\nPlease make sure you have entered a year between 1890 and 2100.");
			form..focus();
			return false;
		}
*/

//if 0 is returned, it was a valid date
//if 1 is returned, its an invalid date
//if 2 is returned, it is an invalid month (1-12 are valid)
//if 3 is returned, it is an invalid day (1-31 are valid)
//if 4 is returned, it is an invalid year (1890-2100 are valid)
function checkValidDate(theDate)
{
	var datePat = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	
	var matchArray = theDate.match(datePat);
	
	if (matchArray == null)
		return 1;
	
	var month = matchArray[1];
	var day = matchArray[2];
	var year = matchArray[3];
	
	if(month < 0 || month > 12)
		return 2;
	
	if(day < 0 || day > 31)
		return 3;
		
	if(year < 1890 || year > 2100)
		return 4;
		
	return 0;
}

//=================================================================================

//Checks for a valid postal code
//Returns true if it is a valid postal code and false if not
function validatePhoneExchange(exchange)
{
  var test1 = exchange.match(/[0-9][0-9][0-9][0-9][0-9][0-9][0-9]/);
  var test2 = exchange.match(/[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]/);
  var test3 = exchange.match(/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/);
  
  if(test3)
		return false;
    
  if(test1 || test2)
    return true;
      
  return false;
}

//=================================================================================
//Checks an email address to see if it meets these requirments... x@x.xx

function chkEmail(email)
{
	var atSymbol = email.indexOf('@');
	var dot = email.lastIndexOf('.');
	var space = email.indexOf(' ');
	var length = email.length;

	// at least one @ must be present and not before position 2
	if (atSymbol < 1 ) 
		return false;

	// at least one char must be after the @ and before the dot
	if(dot - atSymbol <= 1)
		return false;

	// there must be at least one dot after the @
	if (dot < atSymbol) 
		return false;

	//two characters must appear after the dot.
	if (length - dot <= 2) 
		return false;

	// no spaces are allowed
	if (space != -1) 
		return false;

	return true;
}