// check whether the textField is with empty value
function isEmpty(fieldName) {
	if ((fieldName == null) || (fieldName.length == 0)) {
		return true;
	}
	else {	return false;	}
}

function checkEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)){ return true; }
	return false;
}

/* emergency e-mail validator which allows apostrophes */
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >= 0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_&'";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
/* emergency e-mail validator which allows apostrophes */


function popupURL(url,winWidth,winHeight,resizable) {
	if(winWidth == null){winWidth = '500';}
	if(winHeight == null){winHeight = '600';}
	if(resizable == null){resizable = 'yes';}
	myWindow=window.open(url,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=' + resizable + ',copyhistory=yes,width=' + winWidth + ',height=' + winHeight);
	myWindow.focus();
}

function checkDates(f)	{
	var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var day   = f.day.value;
	var month = f.month.value;
	var year  = f.year.value;

	if ((year % 4) == 0 || (year % 400) == 0)	// leap year
		daysInMonth[1]++;
	if (day > daysInMonth[month - 1] || month > 12 || day > 31  || isNaN(day) || isNaN(month) || isNaN(year) || day.length == 0 || month.length == 0 || year.length == 0  ) {
		return true;
	}
	return false;
// Include the following parameters when checking for a birthdate || year > 2002 || year < 1880
}

/*
Function Name:	doCount
Description:	Requires 3 inputs. Counts the number of words in a form textarea.
*/
function doCount(formField, formFieldWordCounter, wordLimit)	{
	var wordArray = formField.value.split(/\s+/g);	// split on spaces to put words into an array, thus getting number of words
	formFieldWordCounter.value = wordLimit - wordArray.length;
	var warningName = formField.name + "WordWarning";

	if(wordArray.length > wordLimit)	{			// word limit exceeded
		document.getElementById(warningName).style.visibility = "visible";	// display warning message
	}
	else{
		document.getElementById(warningName).style.visibility = "hidden";	// hide warning message
	}
}


/* 
	Navigation amongst the results (and away to refine search) is done by form submission to allow
	the criteria that has been entered to be passed around (ie: maintain state).
	
	Haran Jan 2005: orderColumnSearchResults() - see function after navigateSearchResults() - submits form sorting by column

*/
function navigateSearchResults(formName, actionPage, startRow){
	// Set the startRow variable in the form
	if(startRow){
		document[formName].startRow.value = startRow;
	}	
	
	submitForm(formName, actionPage);
}

function orderColumnSearchResults(formName, actionPage, sortColumn){
	// Set the sortColumn variable in the form (and reset startRow to 1)
	if(sortColumn){
		document[formName].sortColumn.value = sortColumn;
		document[formName].startRow.value = 1;
	}	
	
	submitForm(formName, actionPage);
}

function orderNewsArchiveSearchResults(formName, actionPage, sortColumn){
	// Set the sortColumn variable in the form (and reset startRow to 1)
	if(sortColumn){
		document[formName].sortByDate.value = sortColumn;
		document[formName].startRow.value = 1;
	}	
	
	submitForm(formName, actionPage);
}

function submitForm(formName, actionPage){
	// Adjust the action page
	if(actionPage){
		document[formName].action = actionPage;
	}
	document[formName].submit();
}

function populateSelectBox(selectBoxObj,optionsValueArray,optionsIdArray){
	selectBoxObj.length = optionsValueArray.length;
	for(var i=0; i < optionsValueArray.length; i++){
		selectBoxObj.options[i] = new Option(optionsValueArray[i],optionsIdArray[i]);
	}
}

function setSelectBoxDefault(selectBoxObj,defaultArray){
	//alert(selectBoxObj + ', ' + defaultArray);
	//Loop over the array of selected items
	for(var i = 0; i < defaultArray.length; i++){
		var thisSelection = defaultArray[i];
		//alert('thisSelection = ' + thisSelection);
		//Loop over all items in the list and see if any value matches thisSelection
		for(var j = 0; j < selectBoxObj.length; j++){
			if(selectBoxObj.options[j].value == thisSelection){
				selectBoxObj.options[j].selected = true;
				break;
			}
		}
	}
}

/* toggles the display property of a particular object in the page */
function toggleDisplay(objectId) {
	if ( objectId.style.display == 'none' ){
		objectId.style.display = 'block';
	} else {
		objectId.style.display = 'none';
	}
}

function toggleSectionVisibility(sectionName){
	// toggle the visibility of the html content
	toggleDisplay(document.getElementById(sectionName));
}


// Used in the job search section to save the search criteria to a psa
function saveJobSearch(formName, actionPage){
	submitForm(formName, actionPage);
}
function leftTrim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	return sString;
}
// added by A Livie to do string trims
function trim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
return sString;
}
// added by A Livie to do textarea counter
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}

function OpenNewWindow_OnClick( url, width, height )
{
    var _PageName = url;
	var _Width    = width;
	var _Height   = height;
	var _WndName  = 'Wnd';

	var scrWidth  = screen.width;
	var scrHeight = screen.height;

	var topLeft_X = Math.round( ( scrWidth  - _Width  ) / 2 );
	var topLeft_Y = Math.round( ( scrHeight - _Height ) / 2 );

	var features  = 'toolbar=0,' 	  +
			        'scrollbars=1,' +
			        'location=0,'   +
			        'status=0,'  +
			        'menubar=0,'    +
			        'resizable=0,'  +
			        'width='  + _Width    + ','   +
			        'height=' + _Height   + ','   +
			        'left='   + topLeft_X + ','   +
			        'top='    + topLeft_Y + '';

	window.open( _PageName, _WndName, features );
}

function isValidURL(s) {
	if (s.indexOf("@") >= 0) return false
	var re = /^(http(s?)\:\/\/)?(([0-9a-zA-Z\-]+\.)+)([a-zA-Z]{2,})(\/.*)*?(\?.*)?$/i
	return (re.test(s))
}