<!--

//
// THIS FILE CONTAINS GENERIC VALIDATION FUNCTIONS 
//
// FUNCTIONS:
//              checkString(theField, emptyOK)
//              isEmpty(s)
//              isWhitespace(s)
//              checkEmail(theField, emptyOK)
//              isValidAmount (theField, emptyOK)
//              isRadioChecked(radioGroup)
//              isCheckBoxChecked(field)
//              checkPhoneSuffix(theField, emptyOK)
//              checkPhonePrefix(theField, emptyOK)
//              checkPhoneAreaCode(theField, emptyOK)
//              checkPhoneExtension(theField, emptyOK)
//              check5DigitZIP(theField, emptyOK)
//              checkZipCodeExt(theField, emptyOK)
//              stripCharsInBag(s, bag)
//              stripCharsNotInBag(s, bag)
//              isDigit(c)
//              isInteger(s)
//              isPriorDate(year1, month1, day1, year2, month2, day2)
//              checkDate(strYear, strMonth, strDay)
//              isLeapYear(intYear)
//              isDateNotInFuture(strYear, strMonth, strDay)
//              getStyleByName(name)
//              autoTab( input,len, e )
//              containsElement( arr, ele )
//              getIndex( input )
//              openReferencesPopup(page)
//              openNewWindow(page,name,width,height,top,left,propSet)
//              printpage()
//              openEmailColleaguePopup(page, emailPage)
//              changeImages()
//				showPrintPage(document.location)
//				openOusideLink(theURL,winName,features) {
//

// whitespace characters
var whitespace = " \t\n\r";

var defaultEmptyOK = false;

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
	
function checkString (theField, emptyOK) {
	// Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) {
		return false;
    } else return true;
}

// Check whether string s is empty.

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or
// whitespace characters only.

function isWhitespace (s) {
	var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Check Email (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// If emptyOK == true then value cannot be null

function checkEmail (theField, emptyOK) {
    if (checkEmail.arguments.length == 1) {
        emptyOK = defaultEmptyOK;
    }   
    if ((emptyOK == true) && (isEmpty(theField.value))) {
        return true;
    } else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theField.value))) {
        return false;
    } else {
        return true;
    }
}

// Check the grant amount value
//
// If emptyOK == true then value cannot be null
// Checks if value is a number

function isValidAmount (theField, emptyOK)
{   if (isValidAmount.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if ( (isNaN(theField.value)) || ((emptyOK == false) && (isEmpty(theField.value)))  ) {
                return false;
        } else 
                return true;
}

// Checks if one of the radio buttons is checked

function isRadioChecked(radioGroup) {
        for(var i=0;i<radioGroup.length;i++) {
                if (radioGroup[i].checked) 
                        return true
        }
        return false;
}

function getRadioButtonObjByValue(radioGroup, radioValue) {
	for (var i=0;i<radioGroup.length;i++) {
		if (radioGroup[i].value == radioValue) {
			return radioGroup[i];
		}
	}
	return null;
}

// Validate Check Box
function isCheckBoxChecked(field) {
     var strValue= field.checked;
     if (!strValue) {
     	return false;
     } else {
	 	return true;
	}
}
        
function checkPhoneSuffix(theField, emptyOK) {

        if (checkPhoneSuffix.arguments.length == 1) emptyOK = defaultEmptyOK;
        if ((emptyOK == true) && (isEmpty(theField.value))) {
                return true;
        } else {
                if (!isInteger(theField.value) || theField.value.length != 4) {
                        return false;
                }
        }

        return true;
}

function checkPhonePrefix(theField, emptyOK) {

        if (checkPhonePrefix.arguments.length == 1) emptyOK = defaultEmptyOK;
        if ((emptyOK == true) && (isEmpty(theField.value))) {
                return true;
        } else {
                if (!isInteger(theField.value) || theField.value.length != 3) {
                        return false;
                }
        }

        return true;
}

function checkPhoneAreaCode(theField, emptyOK) {

        if (checkPhoneAreaCode.arguments.length == 1) emptyOK = defaultEmptyOK;
        if ((emptyOK == true) && (isEmpty(theField.value))) {
                return true;
        } else {
                if (!isInteger(theField.value) || theField.value.length != 3) {
                        return false;
                }
        }

        return true;
}

function checkPhoneExtension(theField, emptyOK) {

        if (checkPhoneExtension.arguments.length == 1) emptyOK = defaultEmptyOK;
        if ((emptyOK == true) && (isEmpty(theField.value))) {
                return true;
        } else {
                if (!isInteger(theField.value)) {
                        return false;
                }
        }

        return true;
}

// check5DigitZIP(TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid ZIP code.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function check5DigitZIP(theField, emptyOK) {
        if (check5DigitZIP.arguments.length == 1) emptyOK = defaultEmptyOK;
        if ((emptyOK == true) && (isEmpty(theField.value))) {
                return true;
        } else {
                if (!isInteger(theField.value) || theField.value.length != 5) {
                        return false;
                }
        }

        return true;
}

function checkZipCodeExt(theField, emptyOK) {
        if (checkZipCodeExt.arguments.length == 1) emptyOK = defaultEmptyOK;
        if ((emptyOK == true) && (isEmpty(theField.value))) {
                return true;
        } else {
          if (!isInteger(theField.value) || theField.value.length != 4) {
                return false;
          }
        }

        return true;
}

// Removes all characters which appear in string bag from string s.

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++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}



// Removes all characters which do NOT appear in string bag
// from string s.

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

// Returns true if character c is a digit
// (0 .. 9).

function isDigit(c) {
        return ((c >= "0") && (c <= "9"))
}

// isInteger (STRING s [, BOOLEAN emptyOK])
//
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true),
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger(s) {

        var i;

        if (isEmpty(s)) {
                if (isInteger.arguments.length == 1) {
                        return defaultEmptyOK;
                } else {
                        return (isInteger.arguments[1] == true);
                }
        }

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++) {

        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Checks if Date1 which consists of year1, month1, and day1
// is prior to Date2 which consists of year2, month2, and day2

function isPriorDate(year1, month1, day1, year2, month2, day2) {
        
        intYear1 = parseInt(year1, 10);
        intYear2 = parseInt(year2, 10);
        
        intMonth1 = parseInt(month1, 10);
        intMonth2 = parseInt(month2, 10);
        
        intDay1 = parseInt(day1, 10);
        intDay2 = parseInt(day2, 10);
        
        if (intYear1 < intYear2)
                return true;
        else if ( (intYear1 == intYear2) && (intMonth1 < intMonth2) )
                return true;
        else if ( (intYear1 == intYear2) && (intMonth1 == intMonth2) && (intDay1 < intDay2) )
                return true;
        else 
                return false;
}

function checkDate(strYear, strMonth, strDay) {

        var intday;
        var intMonth;
        var intYear;

        intday = parseInt(strDay, 10);
        if (isNaN(intday)) {
                return false;
        }

        intMonth = parseInt(strMonth, 10);
        if (isNaN(intMonth)) {
                return false;
        }

        intYear = parseInt(strYear, 10);
        if (isNaN(intYear)) {
                return false;
        }

        if (intMonth > 12 || intMonth < 1) {
                return false;
        }

        if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
                return false;
        }

        if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
                return false;
        }

        if (intMonth == 2) {
                if (intday < 1) {
                        return false;
                }

                if (isLeapYear(intYear) == true) {
                        if (intday > 29) {
                                return false;
                        }
                } else {
                        if (intday > 28) {
                                return false;
                        }
                }
        }

        return true;
}


function isLeapYear(intYear) {
        if (intYear % 100 == 0) {
                if (intYear % 400 == 0) { return true; }
        }
        else {
                if ((intYear % 4) == 0) { return true; }
        }
        return false;
}

function leadingZero (num) {
        if (num >= 10 ) return num;
                return "0" + num;
}

function isDateNotInFuture(strYear, strMonth, strDay) {

        var intday;
        var intMonth;
        var intYear;

        intday = parseInt(strDay, 10);
        if (isNaN(intday)) {
                return false;
        }

        intMonth = parseInt(strMonth, 10);
        if (isNaN(intMonth)) {
                return false;
        }

        intYear = parseInt(strYear, 10);
        if (isNaN(intYear)) {
                return false;
        }

        if (intMonth > 12 || intMonth < 1) {
                return false;
        }

        if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
                return false;
        }

        if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
                return false;
        }

        if (intMonth == 2) {
                if (intday < 1) {
                        return false;
                }

                if (isLeapYear(intYear) == true) {
                        if (intday > 29) {
                                return false;
                        }
                } else {
                        if (intday > 28) {
                                return false;
                        }
                }
        }

        var curDate = new Date();
        var curYear = curDate.getYear();
        var curMonth = curDate.getMonth() + 1;
        var curDay = curDate.getDate();

        // Hack to get correct year.  If year < 200, then add 1900.
        if (curYear < 200) {
                curYear = curYear + 1900;
        }

        if (intYear > curYear) {
                return false;
        } else {
                if (intYear < curYear) {
                        return true;
                }
        }

        if (intMonth > curMonth) {
                return false;
        } else {
                if (intMonth < curMonth) {
                        return true;
                }
        }

        if (intday > curDay) {
                return false;
        } else {
                if (intday < curDay) {
                        return true;
                }
        }

        // This line gets called if the date is today.
        return true;
}

// Accepts a number or string and formats it like U.S. currency
function formatCurrency(num) {
        num = num.toString().replace(/\$|\,/g,'');
        if(isNaN(num) || ( trim(num) == '') || (trim(num) == ' ')) { 
                return num = "-1";
        }
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        if(cents<10) {
                cents = "0" + cents;
        }
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                num = num.substring(0,num.length-(4*i+3))+','+
                num.substring(num.length-(4*i+3));
                
        return (((sign)?'':'-') + num + '.' + cents);
}

// String will trim spaces from the string
function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = /  /g;
   while (temp.match(obj)) { temp = temp.replace(obj, " "); }
   return temp;
}

// Converts first letter of each word to upper case
function toUpper(oldStr) {
        var pattern = /(\w)(\w*)/;
        var parts;
        var firstLetter;
        var restOfWord;
        var newStr;
        var a;

        a = oldStr.split(/\s+/g);

        for (i = 0 ; i < a.length ; i ++ ) {
                parts = a[i].match(pattern);
                firstLetter = parts[1].toUpperCase();
                restOfWord = parts[2].toLowerCase();

                a[i] = firstLetter + restOfWord;
        }

        newStr = a.join(' ');
        return newStr;
}

// Returns the style object by the given name
function getStyleByName( name )
{
        var sheetList = document.styleSheets;
        var ruleList;
        var i, j;
        
        /* look through stylesheets in reverse order that
          they appear in the document */
        for (i=sheetList.length-1; i >= 0; i--)
        {
                if (sheetList[i].cssRules != null) // for Netscape Browser
                {
                        ruleList = sheetList[i].cssRules;
                }
                else // for Internet Explorer
                {
                        ruleList = sheetList[i].rules;
                }
                for (j=0; j<ruleList.length; j++)
                {
                        if (ruleList[j].selectorText == name)
                        {
                                return ruleList[j].style;
                        }   
                }
        }
        return null;
}

// Auto tab functionality
var isNN = ( navigator.appName.indexOf( "Netscape" ) != -1 ); 
 
function autoTab( input,len, e ) { 
	var keyCode	= ( isNN ) ? e.which : e.keyCode; 
	var filter	= ( isNN ) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46]; 
	if( input.value.length >= len && !containsElement( filter, keyCode )) { 
	input.value = input.value.slice( 0, len ); 
	input.form[( getIndex( input ) + 1 ) % input.form.length].focus(); 
	} 
	return true; 
} 
 
function containsElement( arr, ele ) { 
	var found = false, index = 0; 
	while( !found && index < arr.length ) 
	if( arr[index] == ele ) { 
		found = true; 
	} else { 
		index++; 
	} 
	return found; 
} 
 
function getIndex( input ) { 
	var index = -1, i = 0, found = false; 
	while ( i < input.form.length && index == -1 ) 
	if ( input.form[i] == input ) { 
		index = i; 
	} else { 
		i++; 
	} 
	return index; 
} 

function openReferencesPopup(page) {
	OpenWin = this.open(page, "References", "scrollbars=yes,resizable=yes,width=700,height=720");
}

function openPrintableVersionPopup(page) {
	OpenWin = this.open(page, "PrintableVersion", "scrollbars=yes,toolbar=yes,resizable=yes,width=600,height=200");
}

function openNewWindow(page,name,width,height,top,left,propSet) {
	
	var windowProps = new Array (8);

	windowProps[0] = "resizable=yes";
	windowProps[1] = "scrollbars=yes";
	windowProps[2] = "titlebar=yes";
	windowProps[3] = "toolbar=yes";
	windowProps[4] = "menubar=yes";
	windowProps[5] = "location=yes";
	windowProps[6] = "status=yes";
	windowProps[7] = "directories=yes";
	
	var myProps = "";
	var mySize = "";
	
	if (propSet == 'one') {
		 myProps = ',' + windowProps[0] + ',' + windowProps[1];
	} else if (propSet == 'print') {
		 myProps = ',' + windowProps[4];
	} else if (propSet == "full") {
		myProps = ',' + windowProps.join(",");
	} else {
		myProps = "";
	}	
	
	if ((width > 50)||(height > 50)) {
		var mySize = 'width=' + width + ',' + 'height=' + height + ',' + 'top=' + top + ',' + 'left=' + left;
	}
	
	var myString = mySize + myProps;
	window.open(page,name,myString);
	
}

function openEmailColleaguePopup(page, emailPage) {
	var fullURL = page + "?url=" + emailPage;
	OpenWin = this.open(fullURL, "Email", "scrollbars=yes,resizable=yes,width=600,height=610");
}

function popProDisclaimer(url) {
	legal=window.open(url,"legal","width=440,height=240,menubar=no,status=no,scrollbars=no,scrollable=no,toolbar=no,resizable=no,location=no");
}

function printRecipe() {
	if (document.location.search != "") {
		mark = document.location.search + "&";
	} else {
		mark = "?";
	}
	var recipePage = document.location.pathname + mark +'printRecipe=yes';
	openNewWindow(recipePage,'Recipe',750,600,0,0,'full');
}

function changeImages() {
	if (document.images) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function newImage(arg) {
    if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

function showPrintPage(loc) {
	if (loc.search != "") {
		mark = loc.search + "&";
	} else {
		mark = "?";
	}
	var url = loc.pathname + mark +'printVersion=yes';
	window.open(url);
}

function openOusideLink(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
}

function openOutsideLink(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function redirect(theURL) {

	var outsideLinks = /http:/;
	
	if (theURL.search(outsideLinks) != -1) {
		window.open(theURL);
	} else {
		document.location = theURL;
	}
}

function submitSearch() {
	if(validSearchForm()) {
		document.googleSearch.submit();
	}
}

function validSearchForm() {
    var searchArg;

    searchArg = document.googleSearch.elements["searchString"];
    if(!checkString(searchArg)){
        alert("Please input search argument");
        searchArg.select();
        return false;
    }
    searchArg.value = searchArg.value.toLowerCase();
    return true;
}

function disableCheckboxes(qNamePat, disableField){

		var theForm = document.questionnaire;
		
		for(i=0; i<theForm.elements.length; i++) {
			if(theForm.elements[i].name.substring(0,3) == qNamePat){
				var element=theForm.elements[i];
				element.blur();
				
				if (element.type == "text") {
					element.value='';
				}
				
				if (element.type == "checkbox") {
					element.checked=false;
				}
				
				element.disabled=disableField;
			}
		}
	}
//-->
