function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function largeContent() {
		document.getElementById("content").style.width = '780px'
}

function changeContentSize(width,height) {
	if (width != 0) {
			document.getElementById("content").style.width =width
	}
	if (height != 0) {
			document.getElementById("content").style.height = height
	}
}

function deleteContentBoeder() {
		document.getElementById("content").style.border = 'none'
}


// -----------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----------------------------------------
function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};


// -----------------------------------------
//               reFreshField
// Set all fields' background to white
// -----------------------------------------
function reFreshField() {
	var field = document.form;
	for (i = 0; i < field.length; i++) {
		if (field.elements[i].type != 'submit' && field.elements[i].type != 'button') {
			field.elements[i].style.background = "white"
		}
	}
}

// -----------------------------------------
//            isEmpty
// Validate if field is empty
// Returns true if the field is empty
// -----------------------------------------
function isEmpty(vfld) {
	var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
	if ((tfld == null) || (tfld.length == 0)) {
		return true
	}
	return false
}


// -----------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// require: 0:no   1:yes
// -----------------------------------------
function validatePresent(vfld, vfldName, required) {
	reFreshField()
	if (required == 1 && isEmpty(vfld)) {
		alert("Field Required" + "\n" + "Please input " + vfldName + ".")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	return true	
}

// -----------------------------------------
//               validateNumeric
// Validates that a string contains only valid numbers
// Returns true if so 
// digitalNum: -9 means no lenght limited
// required: 0:no   1:yes
// -----------------------------------------
function  validateNumeric(vfld, vfldName, digitalNum, required) {
	reFreshField()
	var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	
	if (required == 0 && isEmpty(vfld)) return true //check if required
	else if (!validatePresent(vfld, vfldName, required)) return false //if required, check if empty
	
	//check for numeric characters
	if (!objRegExp.test(tfld)) {
		alert("ERROR: not a valid " + vfldName + "" + "\n" + "Please input " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	
	if (digitalNum != -9 && tfld.length != digitalNum) {
		alert("ERROR: not a valid " + vfldName + "" + "\n" + "Please input " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	return true
}

// -----------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so 
// required: 0:no   1:yes
// -----------------------------------------
function validateEMail(vfld, vfldName, required) {
	reFreshField()
	var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
	var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
	
	if (required == 0 && isEmpty(vfld)) return true //check if required
	else if (!validatePresent(vfld, vfldName, required)) return false //if required, check if empty
	
	if (!email.test(tfld)) {
		alert("ERROR: not a valid e-mail address" + "\n" + "Please input " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	
	var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
	if (!email2.test(tfld)) {
		alert("Unusual e-mail address - check if correct" + "\n" + "Please input " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	
	return true	
}

// -----------------------------------------
//               validateSelectBox
// Validate if SelectBox is selected correct
// Returns true if so 
// -----------------------------------------
function validateSelectBox(vfld, vfldName) {
	reFreshField()
	var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
	if (tfld == "-9") {
		alert("ERROR: not a valid selection" + "\n" + "Please select " + vfldName + ".")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	return true	
}

// -----------------------------------------
//               validateRadioButton
// Validate if Radio Button is selected correct
// Returns true if so 
// -----------------------------------------
function validateRadioButton(vfld, bookmark, focusRadio, errorString) {

	reFreshField()
	var myOption = -1
	for (i=0; i<vfld.length; i++) {
		if (vfld[i].checked) {
			myOption = i;
		}
	}
	if (myOption == -1) {
		alert("ERROR: not a valid selection" + "\n" + "Please select " + errorString + ".")
		window.location = bookmark
		focusRadio.focus()
		return false
	}
	return true
}


/************************************************
		validateDate(vfld, vfldName, required)
		
DESCRIPTION:  (m/d/yy or yyyy)
			  (m-d-yy or yyyy)
PARAMETERS:
   strValue - String to be tested for validity
   required: 0:no   1:yes

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
function validateDate(vfld, vfldName, required) {
	if (required == 0 && isEmpty(vfld)) return true //check if required
	else if (!validatePresent(vfld, vfldName, required)) return false //if required, check if empty

	 // (\d{1,2}) means 4 or 12
     // (\/|-) means either (/ or -), 4-12 or 4/12 
     // NOTE: we have to escape / (\/)
     // or else pattern matching will interpret it to mean the end instead of the literal "/"
     // \2 use the 2nd placeholder (\/|-) "here"
     // (\d{2}|\d{4}) means 02 or 2002
     var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	 var tfld = trim(vfld.value)	 
     var matchArray = tfld.match(datePat);
	 var IsTrue = true;
     if (matchArray == null) {
	 	alert("ERROR: not a valid date" + "\n" + "Please input " + vfldName + " again.");
		vfld.style.background = "#FFFFCC";
		vfld.focus();
		return false;
	 }

     // matchArray[0] will be the original entire string, for example, 4-12-02 or 4/12/2002
     var month = matchArray[1];     // (\d{1,2}) - 1st parenthesis set - 4
     var day = matchArray[3];         // (\d{1,2}) - 3rd parenthesis set - 12
     var year = matchArray[4];        // (\d{2}|\d{4}) - 5th parenthesis set - 02 or 2002
     if (month < 1 || month > 12) IsTrue = false;
     if (day < 1 || day > 31)  IsTrue = false;
     if ((month == 4 || month == 6 || month==9 || month == 11) && day == 31) IsTrue = false;
     if (month == 2) {
          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
          if (day > 29 || (day == 29 && !isleap)) IsTrue = false;
     }
	 if ( IsTrue == false ) {
	 	alert("ERROR: not a valid date" + "\n" + "Please input " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	 }
	 else {
		 return true
	 }
}


// -----------------------------------------
//               validateUSPhone(vfld, vfldName, required)
// DESCRIPTION: Validates that a string contains valid
//  US phone pattern.
//  Ex. (999) 999-9999 or (999)999-9999
//
//PARAMETERS:
//   strValue - String to be tested for validity
//
//RETURNS:
//  True if valid, otherwise false.
// -----------------------------------------
function validateUSPhone(vfld, vfldName, required) {
	
	var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
	var tfld = trim(vfld.value)  // value of field with whitespace trimmed off

	if (required == 0 && isEmpty(vfld)) return true //check if required
	else if (!validatePresent(vfld, vfldName, required)) return false //if required, check if empty

	//check for valid us phone with or without space between
	//area code
	if (!objRegExp.test(tfld)) {
		alert("ERROR: not a valid phone number" + "\n" + "Please input " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	return true


}

// -----------------------------------------
//               Display(id,dis)
// Display or unDisplay the form component
// dis: 0-undisplay  1-display
// -----------------------------------------
function Display(id,dis) {
	if (dis == 0) {
		document.getElementById(id).style.display = "none"
	}
	else if (dis == 1) {
		document.getElementById(id).style.display = "inline"
	}
}

// -----------------------------------------
//               OnloadDisplay(id)
// Display select box when page onload
// -----------------------------------------
function OnloadDisplay(id) {
	var id_SelectBox = id + "_SelectBox"
	var id_Text = id + "_Text"
	Display(id_Text,0)
	Display(id_SelectBox,1)
}

// -----------------------------------------
//               PrintDisplay(id)
// Display text for select box when printing
// -----------------------------------------
function PrintDisplay(id) {
	var id_SelectBox = id + "_SelectBox"
	var id_Text = id + "_Text"
	if (document.getElementById(id_SelectBox).value == 0) {
		document.getElementById(id_Text).value = ""
	}
	else {
		document.getElementById(id_Text).value = document.getElementById(id_SelectBox).value
	}
	Display(id_Text,1)
	Display(id_SelectBox,0)
	Display("PrintButton",0)
}

// -----------------------------------------
//   DeleteChecking(CheckName, delectName)
// -----------------------------------------
function DeleteChecking(CheckName, delectName) {
	if (CheckName.checked == true) {
		if (confirm('Are you sure you want to delete this ' + delectName + '?')) {
			CheckName.checked = true
		}
		else {
			CheckName.checked = false
		}
	}	
}

// ------------------------------------------------------
//               vValidateEndDate(StartDate,EndDate)
// DESCRIPTION: Validates the Event End Date 
//				
//RETURNS:
//  True if valid, otherwise false.
// --------------------------------------------------
function validateEndDate(StartDate,EndDate) {
	if ( DateDiff(EndDate.value,StartDate.value) < 0 ) {
		alert("ERROR: End Date can't be set up before Start Date" + "\n" + "Please input End Date again.")
		EndDate.style.background = "#FFFFCC"
		EndDate.focus()
		return false
	}
	return true
}


// -----------------------------------------
//   DateDiff(date1, date2)
// 	 Calculate dates difference
// -----------------------------------------
function DateDiff(date1, date2) {
	var objDate1=new Date(date1);
	var objDate2=new Date(date2);
	return (objDate1.getTime()-objDate2.getTime())/86400000;
}

   /****************************************************
   
   Use Javascript method (window.open) to PopUp a new window 
   which contain a Calendar Control. In the meantime, we'll 
   pass the Parent Form Name and Request Control Name in the QueryString!
   
   *****************************************************/
function GetDate(CtrlName)
{
	ChildWindow = window.open('EventSmallCalendar.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName, "PopUpCalendar", "width=270,height=250,top=300,left=300,toolbars=no,scrollbars=no,status=no,resizable=no");
}

function CheckWindow()
{
  ChildWindow.close();
}
