// -----------------------------------------
//                  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"
		}
	}
}

function reFreshFormField(vfrm) {
	var field = vfrm;
	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 provide " + vfldName + ".")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	return true	
}

function validateFormPresent(vfrm, vfld, vfldName, required) {
	reFreshFormField(vfrm)
	if (required == 1 && isEmpty(vfld)) {
		alert("Field Required" + "\n" + "Please provide " + vfldName + ".")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	return true	
}

function validateFormPresentNoAlert(vfrm, vfld, required) {
	reFreshFormField(vfrm)
	if (required == 1 && isEmpty(vfld)) {
		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 provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	
	if (digitalNum != -9 && tfld.length != digitalNum) {
		alert("ERROR: not a valid " + vfldName + "" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	return true
}

function  validateFormNumeric(vfrm, vfld, vfldName, digitalNum, required) {
	reFreshFormField(vfrm)
	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 (!validateFormPresent(vfrm, 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 provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	
	if (digitalNum != -9 && tfld.length != digitalNum) {
		alert("ERROR: not a valid " + vfldName + "" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	return true
}

function  validateFormNumericNoAlert(vfrm, vfld, digitalNum, required) {
	reFreshFormField(vfrm)
	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 (!validateFormPresentNoAlert(vfrm, vfld, required)) {return false }//if required, check if empty
	
	
	//check for numeric characters
	if (!objRegExp.test(tfld)) {
		//alert("ERROR: not a valid " + vfldName + "" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	
	if (digitalNum != -9 && tfld.length != digitalNum) {
		//alert("ERROR: not a valid " + vfldName + "" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	return true
}

function  validateNumericWithMinMax(vfld, vfldName, digitalNum, required, minNum, maxNum) {
	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 provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	
	if (digitalNum != -9 && tfld.length != digitalNum) {
		alert("ERROR: not a valid " + vfldName + "" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC";
		vfld.focus()
		return false
	}
	
	if ( tfld < minNum || tfld > maxNum ) {
		alert("ERROR: day must between " + minNum + " and " + maxNum + "\n" + "Please provide " + 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("Not a valid email address" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	
	var email2 = /^[A-Za-z0-9][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
	if (!email2.test(tfld)) {
		alert("Unusual email address - check if correct" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	
	return true	
}

function validateFormEMail(vfrm, vfld, vfldName, required) {
	reFreshFormField(vfrm)
	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 (!validateFormPresent(vfrm, vfld, vfldName, required)) return false //if required, check if empty
	
	if (!email.test(tfld)) {
		alert("Not a valid email address" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	
	var email2 = /^[A-Za-z0-9][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
	if (!email2.test(tfld)) {
		alert("Unusual email address - check if correct" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	
	return true	
}

function validateFormEMailNoAlert(vfrm, vfld, required) {
	reFreshFormField(vfrm)
	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 (!validateFormPresentNoAlert(vfrm, vfld, required)) {return false }//if required, check if empty
	
	if (!email.test(tfld)) {
		//alert("Not a valid e-mail address" + "\n" + "Please provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	
	var email2 = /^[A-Za-z0-9][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
	if (!email2.test(tfld)) {
		//alert("Unusual e-mail address - check if correct" + "\n" + "Please provide " + 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("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("Please select " + errorString + ".")
		window.location = bookmark
		focusRadio.focus()
		return false
	}
	return true
}

// -----------------------------------------
//               validateCheckBox
// Validate if Radio Button is selected correct
// Returns true if so 
// -----------------------------------------
function validateCheckBox(vfld, focusRadio, errorString) {

	reFreshField()
	var myOption = -1
	for (i=0; i<vfld.length; i++) {
		if (vfld[i].checked) {
			myOption = i;
		}
	}
	if (myOption == -1) {
		alert("Please select " + errorString + ".")
		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("Not a valid date" + "\n" + "Please provide " + 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("Not a valid date" + "\n" + "Please provide " + 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 provide " + vfldName + " again.")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	return true

}

function validateMaxLength(vfld, vfldName, maxLength) {
	var tfld = trim(vfld.value)  // value of field with whitespace trimmed off
	if (tfld.length > maxLength) {
		alert("Exceed max " + maxLength + " characters" + "\n" + "Please modify " + vfldName + ".")
		vfld.style.background = "#FFFFCC"
		vfld.focus()
		return false
	}
	return true

}

function validateReturn(vfld, TrueOrFalse, errorString) {
	var tfld = trim(vfld.value)  // value of field with whitespace trimmed off
	if (!TrueOrFalse) {
		alert(errorString)
		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 provide 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();
}

function AnchorPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

// -----------------------------------------
//   AnchorPosY(obj)
// 	 Returns an object having y properties which is the coordinates
//   relative to the window
// -----------------------------------------
function AnchorPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop-document.documentElement.scrollTop;
}

// -----------------------------------------
//   DeleteContact(deleteItem)
// 	 Confirm box before submiting a delete request
// -----------------------------------------
function DeleteConfirm(deleteItem) {
	if (confirm('Are you sure you want to delete this ' + deleteItem + '?')) {
		return true
	}
	else {
		return false
	}
}

function NextPhoneNumberField(LengthRequired, curfield, nextfield) {
	if (curfield.value.length == LengthRequired) {
		nextfield.focus()
	}
}

function OpenDataCenterServicesClips() {
	window.open('/co-lo/movies.asp','_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=200, height=200')
	
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}
function getCookiePara(cookie, name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(cookie);
  if( results == null )
    return "";
  else
    return results[1];
}

function getURLPara(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
