//----------------------------------------------------------------------------------
function validateExpression (val, RE) {
	var re = new RegExp(RE);
	if (val.search(RE) == -1) { return false; }
	else { return true; }
}
//----------------------------------------------------------------------------------

// this was snagged from www.siteExperts.com and modified
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator is /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
function validateUSDate(strValue) {
	var RE = /\b\d{1,2}\/\d{1,2}\/\d{4}\b/;
	
	if (validateExpression(strValue, RE) == false) return false; //doesn't match pattern, bad date
	else{
		var arrayDate = strValue.split('/'); //split date into month, day, year
		var intDay = parseInt(arrayDate[1],10); 
		var intYear = parseInt(arrayDate[2],10);
		var intMonth = parseInt(arrayDate[0],10);
		
		//check for valid month
		if(intMonth > 12 || intMonth < 1) return false;
	
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
	
		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null) {
			if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0) return true; //found in lookup table, good date
		}
		//check for February
		var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
		if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
			return true; //Feb. had valid number of days
	}
	return false; //any other values, bad date
}
// this was snagged from www.siteExperts.com and modified
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator is /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm-yyyy
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
function validateUSMMYYYY(strValue) {
	var RE = /\b\d{1,2}-\d{4}\b/;
	
	if (validateExpression(strValue, RE) == false) return false; //doesn't match pattern, bad date
	else{
		var arrayDate = strValue.split('-'); //split date into month, day, year
		var intYear = parseInt(arrayDate[1],10);
		var intMonth = parseInt(arrayDate[0],10);
		
		//check for valid month
		if(intMonth > 12 || intMonth < 1) return false;
		return true;
	}
	return false; //any other values, bad date
}
//----------------------------------------------------------------------------------
/*=======================================================
description:
	this validates a 12-hour time format. hh:mm am|pm
	also, it only validates valid time ranges
	
parameters:
	str - string to be tested
	
returns:
	true if valid, oterwise false
=======================================================*/
function validate12hourTime(str) {
	return validateExpression(str, /\b^(([1-9]|1[0-2]):[0-5]\d{1}\s(am|pm)\b){1}$\b/i);
}

function validateUSDateTime(strValue) {
	var RE = /\b\d{1,2}\/\d{1,2}\/\d{4}\s{1}(([1-9]|1[0-2]):[0-5]\d{1}(:[0-5]\d{1})?\s(am|pm)\b){1}$\b/i;
	
	if (validateExpression(strValue, RE) == false) return false; //doesn't match pattern, bad date
	else{
		var arrayDate = strValue.split('/'); //split date into month, day, year
		var intDay = parseInt(arrayDate[1],10); 
		var intYear = parseInt(arrayDate[2],10);
		var intMonth = parseInt(arrayDate[0],10);
		
		//check for valid month
		if(intMonth > 12 || intMonth < 1) return false;
	
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
	
		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null) {
			if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0) return true; //found in lookup table, good date
		}
		//check for February
		var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
		if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
			return true; //Feb. had valid number of days
	}
	return false; //any other values, bad date
}

function validateNumeric( strValue ) {
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
	return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
	var objRegExp  = /(^-?\d\d*$)/;
	return objRegExp.test(strValue);
}
//----------------------------------------------------------------------------------
/*
function validateEmail( strValue ) {
	var objRegExp = /[\w]{1,}(@){1}([\w]{1,}(\.){1}){1,}(com|net|edu|org|gov|mil|tv){1}$/gi;
	return objRegExp.test(strValue);
}
*/
//Old validateEmail function was having problems with dashes and/or periods per WO#73249
//The new function sets up two regular expressions.  The first makes sure there is at least
//an @ and a . and that the . is after the @.  The second looks for at least 1 character before
//the @, an optional [ and ] to enclose the domain (IP Addresses), some sequence of letters, numbers,
//dashes and periods, and a 2 or 3 letter extension.  Adapted from http://www.webreference.com/js/column5/form.html
function validateEmail(strValue) {
    var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
    var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
    if (!reg1.test(strValue) && reg2.test(strValue)) { // if syntax is valid
        return true;
    }
    return false;
}

//----------------------------------------------------------------------------------
// this is another way of checking for a real date
// returns true or false
function isValidDate(frmValue) {
	// this is based off the fact that an invalid date (like 6/31/2002) will skip over to the next month
	// when a date object is created based on that date. compare the pre-object month with the object month 
	// to know
	// date coming in in mm/dd/yyyy format; grab the month and store away for future matching
	if (frmValue.indexOf('/') == -1) { return false; }
	var iMonth = parseInt(frmValue.split('/')[0]);
	// create a new date object based on the date
	var myDate =  new Date(frmValue);
	if ((iMonth-1) != myDate.getMonth()) { return false; }
	else { return true; }
}

//----------------------------------------------------------------------------------
function rightTrim(str) { //Trims trailing whitespace chars.
	var objRegExp = /^([\w\W]*)(\b\s*)$/;
	
	if(objRegExp.test(str)) {
		//remove trailing a whitespace characters
		str = str.replace(objRegExp, '$1');
	}
	return str;
}
//----------------------------------------------------------------------------------
function leftTrim(str) { //Trims leading whitespace chars.
	var objRegExp = /^(\s*)(\b[\w\W]*)$/;
	
	if(objRegExp.test(str)) {
		//remove leading a whitespace characters
		str = str.replace(objRegExp, '$2');
	}
	return str;
}
//----------------------------------------------------------------------------------
function trimAll(str) { //Removes leading and trailing spaces.
	var objRegExp = /^(\s*)$/;
	
	//check for all spaces
	if(objRegExp.test(str)) {
		str = str.replace(objRegExp, '');
		if( str.length == 0)
			return str;
	}
	//check for leading & trailing spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(str)) {
		//remove leading and trailing whitespace characters
		str = str.replace(objRegExp, '$2');
	}
	return str;
}
//----------------------------------------------------------------------------------
//submitSearch() - used on all search pages to check and submit
function submitSearch() {
	var url;
	if (document.keywordSearch.qu.value == "") {
		alert("Please enter search criteria");
		document.keywordSearch.qu.focus();
		return;
	}
	document.keywordSearch.submit();
}
//----------------------------------------------------------------------------------
// submitVote() - used on all quick vote pages to verify submission
function submitVote(){
		var form = document.forms.frmSubmitVote;
		var val = "";		
		
		for(i=0;i<form.radio1.length;i++)
		{
			if(form.radio1[i].checked==true)
				val = form.radio1[i].value;
		}
		
		if (val == "")
		{
			alert('Please Select A Yes Or No Response.');
		}	
		else
		{
			form.result.value = val;
			form.submit();				
		}		
}	
//----------------------------------------------------------------------------------
//toggleQuickVote() - expand/collapse utility for all quick vote pages.
function toggleQuickVote(id) {
		var obj = document.getElementById(id);
		
		if (obj.style.display == "none")
		{	
			obj.style.display = "block";
		} 
		
		else		
		
		{			
			obj.style.display = "none";
		}
}
//----------------------------------------------------------------------------------
function getNumeric(elmnt)
{
	elm = document.getElementById(elmnt);
	var str = elm.value;
	elm.value = str.replace(/[^\d]*/gi,"");
}
//----------------------------------------------------------------------------------
function getElement(elmnt)
{
	isNS4 = (document.layers) ? true : false;
	isIE4 = (document.all && !document.getElementById) ? true : false;
	isIE5 = (document.all && document.getElementById) ? true : false;
	isNS6 = (!document.all && document.getElementById) ? true : false;
	
	if (isNS4){
	   elm = document.layers[elmnt];
	}
	else if (isIE4) {
	   elm = document.all[elmnt];
	}
	else if (isIE5 || isNS6) {
	   elm = document.getElementById(elmnt);
	}	
	else {
		elm = document.all[elmnt];
	}
	return elm;
}
//----------------------------------------------------------------------------------
function checkEmail1(strEmail) {
	var emailArr = strEmail.split(",");
	var i;
	var bln = 0;
	// create email filter regular expression & check email address
	var myRegExp = new RegExp("^[A-Za-z0-9_\.-]+@[A-Za-z0-9_\.-]+[\.][A-Za-z]+$");
		
	// loop through the email array (emailArr)
	for (i=0;i<emailArr.length;i++){
		myRegExp.test(emailArr[i]);		
		if (!myRegExp.test(emailArr[i])){
	// if the array index does not come back true then increment the counter
				bln++;
		} 
	}			
	if (bln > 0){
	// if the counter is greater than 0 return false;
		return false;
	} else {
		return true;
	}
}

function checkWeb(address) {
	var re;

    re = /(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
        if (re.test(address) == true)
           return true;
        else       
           return false;          
   }


function getDays(month,year) {		
	
		// create array to hold number of days in each month
		var ar = new Array(13);
		ar[1] = 31; 
		
		ar[2] = (leapYear(year)) ? 29 : 28; 		
		ar[3] = 31; 
		ar[4] = 30; 
		ar[5] = 31; 
		ar[6] = 30; 
		ar[7] = 31; 
		ar[8] = 31; 
		ar[9] = 30; 
		ar[10] = 31; 
		ar[11] = 30; 
		ar[12] = 31; 

		return ar[month];
	}

	function leapYear(intYear) {
		if (intYear % 100 == 0) {
			if (intYear % 400 == 0) { 
				return true;
			 }
		}
		else {
			if ((intYear % 4) == 0) { 
				return true;
			 }
		}
		return false;
	}


function checkDate(strDate){
    if(strDate.length>0){
		var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
        var match=strDate.match(dateregex);
        if (match){
			var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
			if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10)){ 
				return true; 
			}
        }        
         return false;
    }
    else{
         return false;
    }
}

function getWordCount(content){
	var i=0;
	var numberofwords=1;
	
	while(i<=content.length) {

		if (content.substring(i,i+1) == " ") {
			numberofwords++;
			i++; 
			// extra i++ makes it skip double spaces, or space/return
		}
		if (content.substring(i,i+1) == "\n") {
			numberofwords++;
			i++;
			// extra i++ makes it skip double spaces, or space/return

		}

		i++;
	}	
	return numberofwords;	
}


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

// Google dependencies.
var g = '/images/google_hd.gif';

function clearText(el) {
	if (el.style.backgroundImage.match(g)) { el.style.backgroundImage = ''; return false; }
	return true;
}
function resetText(el) {
	if (el.value.trim() == '') { el.style.backgroundImage = 'url('+g+')'; el.value=''; return false; }
	return true;
}

function searchAll() {
   var q = document.getElementById("txtSearch").value.trim();
   if (q == "") {
	alert("Please enter search criteria");
	return;
   }
   window.location="/search.aspx?qu=" + URLEncode(q);
}
function searchCollection(fromPage) {
   var q = document.getElementById("txtSearchCol").value.trim();
   if (q == "") {
	alert("Please enter search criteria");
	return;
   }
   window.location="/search.aspx?fromPage=" + fromPage + "&qu=" + URLEncode(q);
}
