
/* ======================================================================
FUNCTION:	checkStringLength 
 
INPUT: 		string value 
			int value


DESC:		This function checks the value length against the given
			length and returns a message
====================================================================== */
function checkStringLength(strValue, strValueName, intCharLimit){
	var errMsg = "";
	var intCharCount = strValue.length;
	if (intCharCount > intCharLimit) {
		var intAmountOverLimit = intCharCount - intCharLimit;
		errMsg = " -"+ strValueName +" has exceeded the maximum length by " + intAmountOverLimit + " characters.\n"; 
	}
	return errMsg
}

/* ======================================================================
FUNCTION:	setColor 
 
INPUT: 		form element (object) 
			color (string)


DESC:			This function sets the background color of the object
				(used in form validation) 
====================================================================== */
var bgNotValid = "#FFE6B5";
var bgValid = "white";

function utility_setColor(el, bg) {
	  if (el.style) {el.style.backgroundColor = bg;}
}

/*======================================================================
Function: validateDate

Input: string

Desc:  function to check whether the date is valid
		it must be in the format mm/dd/yyyy
		
=======================================================================*/
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function utility_isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}




/* ======================================================================
FUNCTION:	validateEmail 
 
INPUT: 		string


DESC:		function to check whether the e-mail address is valid
			it must have at least this format ( char + @ + char + . + char )
====================================================================== */
function utility_validateEmail(eml) {

  a = eml.search(/@/i);
  a1 = eml.substring(0,a);
  a2 = a1.length;
  
  a = a + 1;
  b = eml.indexOf(".");
  b1 = eml.substring(a,b);
  b2 = b1.length;
  
  b = b + 1;
  c = eml.length;
  c1 = eml.substring(b,c);
  c2 = c1.length;
 
  if(eml.indexOf("@") != "-1" && eml.indexOf(".") != "-1" && a2 >= 1 && b2 >= 1 && c2 >= 1) {
	return true;
  } else {
	return false;
  }
 }
 
 function button_over(eButton){
	if (eButton.style.borderBottom != "#ffffff 1px solid")
	{
	eButton.style.borderBottom = "#808080 solid 1px";
	eButton.style.borderLeft = "#D3D3C3 solid 1px";
	eButton.style.borderRight = "#808080 solid 1px";
	eButton.style.borderTop = "#D3D3C3 solid 1px";
	}
}
	function button_out(eButton){
	eButton.style.borderColor = "#D3D3C3";
}

function open_Window(url)
{
	var popleft=String(Math.round((screen.availWidth)/2)-370);
	var poptop=String(Math.round((screen.availHeight)/2)-250);
	var newWin = window.open(url,"NewWindow","scrollbars=YES,width=740,height=500,left="+popleft+",top="+poptop+",resizable");
}	
	
function ListFind(list, thevalue)
{
  var i = 0;
  var delimiter = ',';
  var returnValue = -1;
  var _tempArray = new Array();
  _tempArray = list.split(delimiter);
  for(i = 0; i < _tempArray.length; i++)
  {//alert(_tempArray[i]);
    if(_tempArray[i] == thevalue)
    {
      returnValue = i;
      break;
    }
  }
  //alert(returnValue);
  return returnValue;
}

function ListFindNoCase(list, value)
{
  var i = 0;
  var delimiter = ',';
  var returnValue = -1;
  var _tempArray = new Array();
  if(ListFindNoCase.arguments.length == 3)
    delimiter = ListFindNoCase.arguments[2].toLowerCase();
  list = list.toLowerCase();
  value = value.toLowerCase();
  _tempArray = list.split(delimiter);
  for(i = 0; i < _tempArray.length; i++)
  {	
    if(_tempArray[i] == value)
    {
      returnValue = i;
      break;
    }
  }
  return returnValue;
}

function ListLast(list)
{
  var delimiter = ',';
  var returnValue = 'noext';
  var _tempArray = new Array();
  if(ListLast.arguments.length == 2) delimiter = ListLast.arguments[1].toLowerCase();
  _tempArray = list.split(delimiter);
 
  if(_tempArray.length>1)
    returnValue = _tempArray[_tempArray.length - 1];
  //else
   // returnValue = list;
  return returnValue;
}

function utility_getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0].toLowerCase() == variable.toLowerCase()) {
  //alert('Query Variable ' + variable + '= ' + pair[1]);
      return pair[1];
    }
  } 
}


function open_Photo(intImageId,intImageVersion,strCurrentLanguage)
{
	var newWin;
	var loc ="/hbcheritage/learning/gallery/photo.asp?intImageId="+intImageId+"&intImageVersion="+intImageVersion+"&strCurrentLanguage="+strCurrentLanguage;
	var popleft=((document.body.clientWidth - this.ImagePageWidth) / 2)+window.screenLeft;
	var poptop=(((document.body.clientHeight - this.ImagePageHeight) / 2))+window.screenTop-40;	
	newWin = window.open(loc,"PhotoWindow","scrollbars=YES,width=600,height=600,left="+popleft+",top="+poptop+",resizable");
	newWin.focus();
}

function open_Link(url)
{
	var newWin;
	var loc = url
	var popleft=((document.body.clientWidth - this.ImagePageWidth) / 2)+window.screenLeft;
	var poptop=(((document.body.clientHeight - this.ImagePageHeight) / 2))+window.screenTop-40;	
	newWin = window.open(loc,"window","scrollbars=YES,width=700,height=500,left="+popleft+",top="+poptop+",resizable");
	newWin.focus();
}

function utility_validateTelephone(text)
{
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(text);
   
}

function validateUSPhone( strValue ) {
/************************************************
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.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(strValue);
}
