//
// Andrew D. Goodfellow 05/03/2001
//
// Changes a forms field value and a forms action attribute
//  then submits the form
//
function setAction(formObj, formField, formValue, formAction)
{
 formField.value = formValue;
 formObj.action = formAction;
 
 formObj.submit();
}

//
// Andrew D. Goodfellow 05/29/2001
//
// Changes a forms field value
//
function setValue(formObj, formField, formValue)
{
 formField.value = formValue;
}

//
// Andrew D. Goodfellow 05/03/2001
//
// Swaps items between two select lists
//
function moveSelectedItem(formObj, formFieldFrom, formFieldTo)
{

 // find all currently selected items...
 for (var i = 0; i < formFieldFrom.options.length; i++) 
 {
  if (formFieldFrom.options[i].selected)
  {

    // create a new object for the target list
    // syntax: option(text,value,defaultSelected,selected)
    newOption = new Option(formFieldFrom.options[i].text, formFieldFrom.options[i].value, false, true);
 
    // add it to the target list
    formFieldTo.options[formFieldTo.length] = newOption;
 
    // remove it from the current list
    formFieldFrom.options[i] = null;
  }
 }
}

//
// Andrew D. Goodfellow 05/08/2001
//
// Selects all items in a multiselect list
//
function selectAll(formObj, formFieldList)
{

 // go through all items...
 for (var i = 0; i < formFieldList.options.length; i++) 
 {
  formFieldList.options[i].selected = 1;
 }
}

//
// Andrew D. Goodfellow 05/29/2001
//
// Move an item up in a select list
//
function moveSelectedUp(formObj, formFieldList)
{

 // go through all items...
 for (var i = 0; i < formFieldList.options.length; i++) 
 {
  if( (formFieldList.options[i].selected) && (i > 0) )
  {
    // create a new option object and save the selected item in it.
    // syntax: option(text,value,defaultSelected,selected)
    currentOption = new Option(formFieldList.options[i].text, formFieldList.options[i].value, false, true);
    
    // create a new option object and save the item above the selected item in it.
    // syntax: option(text,value,defaultSelected,selected)
    aboveOption = new Option(formFieldList.options[i-1].text, formFieldList.options[i-1].value, false, true);
    
    formFieldList.options[i] = aboveOption;
    formFieldList.options[i].selected = 0;

    formFieldList.options[i-1] = currentOption;
    formFieldList.options[i-1].selected = 1;

  }
 }
}

//
// Andrew D. Goodfellow 05/29/2001
//
// Move an item down in a select list
//
function moveSelectedDown(formObj, formFieldList)
{

 // go through all items...
 for (var i = formFieldList.options.length-1; i >= 0 ; i--) 
 {
  if( (formFieldList.options[i].selected) && (i < formFieldList.options.length-1) )
  {
    // create a new option object and save the selected item in it.
    // syntax: option(text,value,defaultSelected,selected)
    currentOption = new Option(formFieldList.options[i].text, formFieldList.options[i].value, false, true);
    
    // create a new option object and save the item below the selected item in it.
    // syntax: option(text,value,defaultSelected,selected)
    belowOption = new Option(formFieldList.options[i+1].text, formFieldList.options[i+1].value, false, true);
    
    formFieldList.options[i] = belowOption;
    formFieldList.options[i].selected = 0;

    formFieldList.options[i+1] = currentOption;
    formFieldList.options[i+1].selected = 1;

  }
 }
}


//
// Andrew D. Goodfellow 01/02/2002
//
// Encode a url so Netscape can handle it
//
function encodeURL(formValue) {
  var charect = "";
  for (var n = 1 ; n <= formValue.length ; n++) 
  {
    if (formValue.substring(n-1,n) == " ") {
      charect+="+"; 
    } else { 
      charect+=formValue.substring(n-1,n); 
    }
  }
  return charect;
} 	 


var popUpWin = null;
// may want to implement popup a little differently
// this function opens a new window and checks to see if the window already exists
function OpenWindow(mypage,myname,w,h) { // could add scroll - ",scroll"
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',resizable,menubar=yes'; // ',scrollbars='+scroll+

  if(!popUpWin || popUpWin.closed) {
    popUpWin = window.open(mypage,myname,settings);
    if(popUpWin.window.focus){ popUpWin.window.focus(); }
  }
  else {
    popUpWin.close();
    popUpWin = null;
    //  "Why", you may ask, "is all this in a setTimeout?"
    //  "Because Macs are sluggish and unresponsive!", I reply.
    //  [sethd] 19th July 2001
    setTimeout("popUpWin = window.open('"+mypage+"','"+myname+"','"+settings+"');if(popUpWin.window.focus) { popUpWin.window.focus(); }",400);
  }
}



// this function opens a new window and checks to see if the window already exists - 
// I should have implemented the scroll option initially, but I didn't, 
// and now I need a second function to avoid errors all over the place

// John Daharsh -- 11/28/2001
function openScrollWindow(mypage,myname,w,h,scroll) {
  if(scroll == null){scroll='no';}
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',resizable,menubar=no,scrollbars='+scroll; 

  if(!popUpWin || popUpWin.closed) {
    popUpWin = window.open(mypage,myname,settings);
    if(popUpWin.window.focus){ popUpWin.window.focus(); }
  }
  else {
    popUpWin.close();
    popUpWin = null;

    popUpWin = window.open('"+mypage+"','pageBuilder','"+settings+"');
    if(popUpWin.window.focus) 
    { 
    	popUpWin.window.focus(); 
    }

  }
}

// Bob Kuchler -- 05/15/2002
function openPBWindow(mypage) 
{
  if(scroll == null){scroll='no';}
  LeftPosition = (screen.width) ? (screen.width-500)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-500)/2 : 0;
  settings = 'height=500,width=500,top='+TopPosition+',left='+LeftPosition+',resizable,menubar=yes,scrollbars=1'; 

  if(!popUpWin || popUpWin.closed) {
    popUpWin = window.open(mypage,'pageBuilder',settings);
    if(popUpWin.window.focus){ popUpWin.window.focus(); }
  }
  else {
    popUpWin.close();
    popUpWin = null;

    popUpWin = window.open('"+mypage+"','pageBuilder','"+settings+"');
    if(popUpWin.window.focus) 
    { 
    	popUpWin.window.focus(); 
    }
  }
}

function isWhole(sDate) {
  var new_msg = "true"
  inputStr = sDate.toString()
  for (var i = 0; i < inputStr.length; i++){
    var oneChar = inputStr.charAt(i) 
    if (oneChar < "0" || oneChar > "9"){
      new_msg = "false"
    }
  }
  return (new_msg)
}


	function validEmail(email) {
		invalidChars = " /:,;"

		if (email == "") {						// cannot be empty
			return false
		}
		for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) > -1) {
				return false
			}
		}
		atPos = email.indexOf("@",1)			// there must be one "@" symbol
		if (atPos == -1) {
			return false
		}
		if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
			return false
		}
		periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {					// and at least one "." after the "@"
			return false
		}
		if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
			return false
		}
		return true
	}
	
// function isDate - RFernandez wrote this!
	// checks if date field contains only letters, numbers and slashes
	function isDate(sDate){
		var new_msg = "true";
		inputStr = sDate.toString()
		for (var i = 0; i < inputStr.length; i++){
		  var oneChar = inputStr.charAt(i);
		  if ((oneChar < "0" || oneChar > "9") && oneChar != "/"){
			new_msg = "false";
		  }
		}
		if(inputStr=="")
			new_msg = "false";
		return (new_msg)
	}

	// other date validations from MWelagen 
	// Mike Welagen (welagenm@hotmail.com) * http://javascript.internet.com/forms/val-date.html
	function checkdate(objName) {
		var datefield = objName;
		if (chkdate(objName) == false) {
			return "false";
		}
		else {
			return "true";
	   }
	}


	function chkdate(objName) {
		var strDatestyle = "US"; //United States date style
		//var strDatestyle = "EU";  //European date style
		var strDate;
		var strDateArray;
		var strDay;
		var strMonth;
		var strYear;
		var intday;
		var intMonth;
		var intYear;
		var booFound = false;
		var datefield = objName;
		var strSeparatorArray = new Array("-"," ","/",".");
		var intElementNr;
		var err = 0;
		var strMonthArray = new Array(12);
		strMonthArray[0] = "1";
		strMonthArray[1] = "2";
		strMonthArray[2] = "3";
		strMonthArray[3] = "4";
		strMonthArray[4] = "5";
		strMonthArray[5] = "6";
		strMonthArray[6] = "7";
		strMonthArray[7] = "8";
		strMonthArray[8] = "9";
		strMonthArray[9] = "10";
		strMonthArray[10] = "11";
		strMonthArray[11] = "12";
		strDate = datefield.value;
		if(strDate.length==1){
		  return false;
		}
		if (strDate.length < 1) {
			return true;
		}
		for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
			if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {	
				strDateArray = strDate.split(strSeparatorArray[intElementNr]);
				if (strDateArray.length != 3) {
					err = 1;
					return false;
				}
				else {
					strDay = strDateArray[0];
					strMonth = strDateArray[1];
					strYear = strDateArray[2];
				}
				booFound = true;
		   }
		}
		if (booFound == false) {
			if (strDate.length>5) {
				strDay = strDate.substr(0, 2);
				strMonth = strDate.substr(2, 2);
				strYear = strDate.substr(4);
		   }
		}
		if (strYear.length == 2) {
			strYear = '20' + strYear;
		}
		if (strYear.length > 4) {
			strYear = '20' + strYear;
			return false;
		}
		// US style
		if (strDatestyle == "US") {
			strTemp = strDay;
			strDay = strMonth;
			strMonth = strTemp;
		}
		intday = parseInt(strDay, 10);
		if (isNaN(intday)) {
			err = 2;
			return false;
		}
		intMonth = parseInt(strMonth, 10);
		if (isNaN(intMonth)) {
			for (i = 0;i<12;i++) {
				if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
					intMonth = i+1;
					strMonth = strMonthArray[i];
					i = 12;
			   }
			}
			if (isNaN(intMonth)) {
				err = 3;
				return false;
		   }
		}
		intYear = parseInt(strYear, 10);
		if (isNaN(intYear)) {
			err = 4;
			return false;
		}
		if (intMonth>12 || intMonth<1) {
			err = 5;
			return false;
		}
		if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
			err = 6;
			return false;
		}
		if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
			err = 7;
			return false;
		}
		if (intMonth == 2) {
			if (intday < 1) {
				err = 8;
				return false;
			}
			if (LeapYear(intYear) == true) {
				if (intday > 29) {
					err = 9;
					return false;
				}
			}
			else {
				if (intday > 28) {
					err = 10;
					return false;
				}
			}
		}
		if (strDatestyle == "US") {
			datefield.value = strMonthArray[intMonth-1] + "/" + intday+"/" + strYear;
		}
		else {
			datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
		}
		return true;
	}

	function LeapYear(intYear) {
		if (intYear % 100 == 0) {
			if (intYear % 400 == 0) { return true; }
		}
		else {
			if ((intYear % 4) == 0) { return true; }
		}
		return false;
	}

	
//
// Andrew D. Goodfellow 06/17/2002
//
// Checks to make sure that startValue date is before the endValue date.
//	
function SnDateDiff(formField, startValue, endValue, msg) {
  date1 = new Date();
  date2 = new Date();
  diff  = new Date();

  date1temp = new Date(startValue);
  date1.setTime(date1temp.getTime());

  date2temp = new Date(endValue);
  date2.setTime(date2temp.getTime());

  // sets difference date to difference of first date and second date

  diff.setTime(date2.getTime() - date1.getTime());

  timediff = diff.getTime();
  if(timediff < 0) {
    alert(msg);
    formField.focus();
    return false;
  }

  weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
  timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

  days = Math.floor(timediff / (1000 * 60 * 60 * 24));
  timediff -= days * (1000 * 60 * 60 * 24);

  hours = Math.floor(timediff / (1000 * 60 * 60));
  timediff -= hours * (1000 * 60 * 60);

  mins = Math.floor(timediff / (1000 * 60));
  timediff -= mins * (1000 * 60);

  secs = Math.floor(timediff / 1000);
  timediff -= secs * 1000;

  dateDiff = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";
  //alert(dateDiff);

  return true;
}

//
// Andrew D. Goodfellow 06/17/2002
//
// Checks to make sure that emailValue is actually an email address.
//	
function SnValidateEmail(formField, emailValue, required) {
  var re=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/; 

  if (emailValue == "" && required) { 
    alert("Please fill in E-mail");
    formField.focus();
    return false; 
  } 
  if (emailValue != "") {
    if (!(re.test(emailValue))) { 
      alert(" " + emailValue + " is an invalid email address! Example: Yourname@youraddress.com"); 
      formField.focus();
      return false; 
    }
  }
  return true;
}

// Dale McCrory
// Image swapping
// swap function
function swap(imgOrg,imgSwap) {
 if (document.images[imgOrg] != null)
 {
    document.images[imgOrg].src = imgSwap;
 }
} 	



