//-----------------------------------------------------------------------------

function LoadField(field, value){
 if ((field.type=='text') || (field.type=='textarea') ||(field.type=='hidden') ||(field.type=='password'))
  field.value=value;
 else if (field.type=='select-one') {
    selectItem(field, value);
 }
 else if (field.type=='checkbox'){
  field.checked = ('False'!=value);
 }
 else {
  selectRadio(field, value);
 }
}

//-----------------------------------------------------------------------------

function selectRadio(field,value) {
 for (idx=0;idx<field.length;idx++) {
   if (field[idx].value==value) {
     field[idx].checked = true;
     break;
   }
   else
     field[idx].checked = false;
  }     
}

//-----------------------------------------------------------------------------
function selectItem(field,value) {
 for (idx=0;idx<field.length;idx++) {
   if (field[idx].value==value) {
     field[idx].selected = true;
     break;
   }
   else
     field[idx].selected = false;
  }     
}

//-----------------------------------------------------------------------------

function integerBlur(field) {
	if (!intCheck(field.value)) {
	 alert(field.value + ' is not a valid whole number.\nPlease re-enter a new value.');
	 field.focus(); 	
	}
}

//-----------------------------------------------------------------------------

function intCheck (strValue) {
	// if there is a negative sign (dash) after the 1st char it is not valid
	if (strValue.indexOf('-') >= 0)
		if (strValue.indexOf('-') != 0)
			return false;
		else
			strValue = strValue.substring(1);
	
	// if there are comma's in the value then tack one on the end 
	//  and make sure the fitst starts in the 4th position
	if (strValue.indexOf(',') > -1){
		strValue = strValue + ','; 
		while ((strValue.indexOf(',') > -1) && (strValue.indexOf(',') < 3))
			strValue = '0' + strValue;
	}

	// check each character to make sure they are valid
	for (var i = 0; i < strValue.length; i++) {
		// if the char is a comma make sure it is in a valid position
		if (strValue.charAt(i) == ',') {
			if (((i+1)%4) != 0)
				return false;
		}
		else
			// if the character is not a number it fails check
			if (isNaN(strValue.charAt(i)))
				return false;
	}
	return true;
}

//-----------------------------------------------------------------------------

function floatBlur(field) {
	if (!floatCheck(field.value)) {
	 alert(field.value + ' is not a valid real number.\nPlease re-enter a new value.');
	 field.focus(); 	
	}
}

//-----------------------------------------------------------------------------

function floatCheck (strValue) {
	// if there is more than one decimal in the number it is not valid
	if (strValue.indexOf('.') != strValue.lastIndexOf('.'))
		return false;
	
	// why replicate code? remove the decimal and pass it to the incheck routine
	if (strValue.indexOf('.') > -1)
		if (strValue.indexOf('.') == 0)
			strValue = strValue.substring(1);
		else {
			var strTemp = strValue.substring(0, strValue.indexOf('.')) + strValue.substring(strValue.indexOf('.')+1);
			strValue = strTemp;
		}
	return intCheck(strValue);
}

//-----------------------------------------------------------------------------

function floatCurrency() {
	if (!currencyCheck(this.value)) {
	 alert(this.value + ' is not a valid currency amount.\nPlease re-enter a new amount.');
	 this.focus(); 	
	}
}

//-----------------------------------------------------------------------------

function currencyCheck (strValue) {
	// make sure there is only one $ (if any) and it is 1st char
	if ((strValue.indexOf('$') != strValue.lastIndexOf('$')) || (strValue.indexOf('$') > 0))
		return false;
	if (strValue.indexOf('$') == 0)
		strValue = strValue.substring(1);
		
	// why replicate code? remove the decimal and pass it to the floatcheck routine
	return floatCheck(strValue);
}

//-----------------------------------------------------------------------------

function dateCheck (field) {
	if (!isDate(field.value)){
		alert(field.value + ' is not a valid date.\nPlease re-enter the date in mm/dd/yyyy format.');
		field.focus(); 	
	}
}

//=================================================================
// maxFebDays
//	Parameters: y = Year. Must be full year (yyyy). 
//
//	returns: 28 or 29
//
// Notes: Function is only valid back to 1582 due to calander changes prior to this date
//=================================================================
function maxFebDays(y) {
	var numDays = 28;
	// Leapyear check.. If year is not a century and is divisibe by 4, or
	//	if year is a century and is divisible by 400 (so 1900 was NOT a leap year)
	if (y%4 == 0) {
		numDays = 29;
		if (y%100 == 0) {
			numDays = 28;
			if (y%400 == 0) {
				numDays = 29;
			}
		}
	}
	return numDays;
}

//=================================================================
// isLeapYear
//	Parameters: y = Year. Must be full year (yyyy). 
//
//	returns: true/false
//
// Notes: Function is only valid back to 1582 due to calander changes prior to this date
//=================================================================
function isLeapYeay(y){
	return (maxFebDays==29);
}

//=================================================================
// isDate
//	Parameters: chkDate = The date to check in mm/dd/[yy]yy format. 
//
//	returns: true/false
//
// Notes: If yy is 00-84, 2000-2084 is assumed. If the yy is 85-99, 1985-1999 is assumed.
//			I chose not to use the Javascript Date object because of some odd behavior
//			when parsing out invalid dates.
//=================================================================
function isDate(chkDate){
	// No date Entered return
	if (chkDate.length==0)
		return true;
		
    // Set the default date delimiter
    var chrDelimiter = '/';
    if (chkDate.indexOf('-')!=-1)
      chrDelimiter = '-';
    else 
      if (chkDate.indexOf('.')!=-1)
        chrDelimiter = '.';
	var slash1 = chkDate.indexOf(chrDelimiter);
	var slash2 = chkDate.indexOf(chrDelimiter, slash1+1);

	// !=2 slashes means it is not a valid date
	if ( (slash1==-1)||(slash2==-1)||(chkDate.indexOf(chrDelimiter, slash2+1)!=-1))
		return false;

	// figure out the month, day and year
	var sm = chkDate.substring(0, slash1);
	var sd = chkDate.substring(slash1+1, slash2);
	var sy = chkDate.substring(slash2+1, chkDate.length);
	// if any is not a valid # skip out
	if (isNaN(sm)||isNaN(sd)||isNaN(sy))
		return false;
	var m = eval(sm);
	var d = eval(sd);
	var y = eval(sy);
	
	if (y<85)
		y = y + 2000;
	else
		if (y<100)
			y = y + 1900;
		
	// validate the basic values: 31 day months and years, 30 day months and leap year 
	if ((m<1)||(m>12)||(d<1)||(d>31)||(y<1582)||(y>2300) ||
		(((m==4)||(m==6)||(m==9)||(m==11)) && (d==31)) ||
		((m==2)&&(d>maxFebDays(y))))
		return false;

	return true;
}

//-----------------------------------------------------------------------------

function emailCheck(field) {
// checks if the e-mail address is valid
var emailStr = field.value;
if (emailStr.length == 0) return true;
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var matchArray = emailStr.match(emailPat);
if (matchArray == null) {
	alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
	field.focus();
	return false;
	}
// make sure the IP address domain is valid
var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
if (IPArray != null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!")
field.focus();
return false;
      }
   }
}
return true;
}

//-----------------------------------------------------------------------------

function Left(string,len) {
  return string.substr(0,len);
}

//-----------------------------------------------------------------------------

function Right(string,len) {
  return string.substring(string.length-len);
}

//-----------------------------------------------------------------------------

function Mid(string,start,len) {
  return string.substr(start-1,len);
}

//-----------------------------------------------------------------------------

function ValidPhone(field) {
 // assume a valid phone #
  var bValid  = true;
  var sPhoneNum = field.value;
  switch(sPhoneNum.length) {
  
    // phone number without area code:  no dashes
    case 7:
      sPhoneNum = Left(sPhoneNum,3) + "-" + Right(sPhoneNum,4);
      break;
      
    // it's a manual phonenumber and it's okay:  no area code
    case 8:
      if (Mid(sPhoneNum,4,1) != '-') bValid = false;
      break;
      
    // phone number with area code:  no dashes
    case 10:
      sPhoneNum = Left(sPhoneNum,3) + '-' + Mid(sPhoneNum,4,3) + '-' + Right(sPhoneNum,4);
      break;

    // it's a manual phonenumber and it's okay:  includes area code
    case 12:
      if (Mid(sPhoneNum,8,1) != '-') bValid = false;
      if (Mid(sPhoneNum,4,1) != '-') bValid = false;
      break;
    
    // ok if it is blank  
    case 0:
      break;
    //  everything else is invalid
    default:
      bValid = false;
  }

  if (bValid) { 
    field.value = sPhoneNum;
  }
  else {
    alert('Please enter a valid phone number in the format xxx-xxx-xxxx or xxx-xxxx.');
    field.focus();
  }
  
  return bValid;
} //ValidPhone

//-----------------------------------------------------------------------------

function ValidSSN(field) {
  // assume it is a valid social security number
  var bValid = true;
  var sSocSecNum = field.value;

  switch (sSocSecNum.length) {
    // numbers no dashes
    case 9:
      sSocSecNum = Left(sSocSecNum,3) + '-' + Mid(sSocSecNum,4,2) + '-' + Right(sSocSecNum,4);
      bValid = true;
      break;
      
    // make sure valid social security number with dashes
    case 11:
      if (Mid(sSocSecNum,4,1) != '-') bValid = false;
      if (Mid(sSocSecNum,7,1) != '-') bValid = false;
      break;
    
    // can be blank
    case 0:
	  bValid = true;
	  break;

    default:
      bValid = false;
  } //switch
  
  if (bValid) {
     field.value = sSocSecNum;
  }
  else {
     alert('Please enter a valid social security number.');
     field.focus();
  }
  return bValid;
} //ValidSSN

//-----------------------------------------------------------------------------

function TitleCase(string) {
  var theArray = string.split(' ');
  var result = '';
  for(i=0;i<theArray.length;i++) {
  result = result + Left(theArray[i],1).toUpperCase() + Mid(theArray[i],2,theArray[i].length).toLowerCase();
  if (i < theArray.length-1) result = result + ' ';
  }
  return result;
}


