/**
 * validateJS
 *
 * Utility function to validate form input
 */

// Sniff out the browser
	var browserName = navigator.appName;
	browserVer = parseInt(navigator.appVersion);
	var ie = "Microsoft Internet Explorer";
	var nn = "Netscape";
	
	if (browserName == nn) {
		document.write('<link rel=stylesheet type="text/css" href="styleNN.css">');
		window.captureEvents(Event.KEYPRESS);
	} 
	else {
		document.write('<link rel=stylesheet type="text/css" href="style.css">');
	}

//Browser sniffer for rollover effects

	if (browserName == "Netscape" && browserVer >= 3) browserVer = "1";
	else if (browserName == "Microsoft Internet Explorer" && browserVer == 4) browserVer = "1";
	else browserVer = "2"; 
	
//image swapping functions
	function findObj(n, d) { 
		var p,i,x;
		if(!d)
			d=document;
		if((p=n.indexOf("?"))>0&&parent.frames.length) {
			d=parent.frames[n.substring(p+1)].document;
			n=n.substring(0,p);
		}
		if(!(x=d[n])&&d.all)
			x=d.all[n];
		for (i=0;!x&&i<d.forms.length;i++)
			x=d.forms[i][n];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++)
			x=findObj(n,d.layers[i].document);
		return x;
	}

	function swapImage() { 
		var i,j=0,x,a=swapImage.arguments;
		document.sr=new Array;
		for(i=0;i<(a.length-2);i+=3)
			if ((x=findObj(a[i]))!=null){
				document.sr[j++]=x;
			if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];
		}
	}

	function swapImgRestore() { 
	var i,x,a=document.sr;
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
		x.src=x.oSrc;
	}

	function preloadImages() { 
		var d=document;
		if(d.images){
			if(!d.p)
				d.p=new Array();
			var i,j=d.p.length,a=preloadImages.arguments;
			for(i=0; i<a.length; i++)
				if (a[i].indexOf("#")!=0){
					d.p[j]=new Image; d.p[j++].src=a[i];
				}
		}
	}
	
// Allow cursor to auto tab over to next text input field
// Requires 'window.captureEvents(Event.KEYPRESS);' above
	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();
		}
		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;
		}
		return true;
	}
	
// Test field input for numeric entry
	function isNum(n) {
		if (n == "") {
			return false;
		}
		for (i=0; i<n.length; i++) {
			if (n.charAt(i) < "0") {
				return false;
			}
			if (n.charAt(i) > "9") {
				return false;
			}
		}
		return true;
	}

// Test field input for numeric entry
	function isNumeric(field) {
		var valid = "0123456789"
		var ok = "yes";
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			alert("Invalid entry!\nOnly numbers are accepted.");
			field.value = "";
			field.focus();
			field.select();
		}
	}
	
// Test field input for valid SSN entry
	function isSSN(field) {
		var valid = "0123456789- "
		var ok = "yes";
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			alert("Invalid entry!\nOnly numbers and dashes are accepted.");
			field.value = "";
			field.focus();
			field.select();
		}
	}
	
// Test for alphanumeric entry for userids and passwords
	function isAlphaNumeric(field) {
		var valid = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var ok = "yes";
		var temp;
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
			alert("Invalid entry!  Only characters and numbers are accepted!");
			field.value = "";
			field.focus();
			field.select();
		}
	}

// Test input for valid email address
// Must contain an @ and .
	function isEmailValid(f,e)
	{
	var EmailOk  = true
	var Temp     = document.forms[0].elements[e]
	var AtSym    = Temp.value.indexOf('@')
	var Period   = Temp.value.lastIndexOf('.')
	var Space    = Temp.value.indexOf(' ')
	var Length   = Temp.value.length - 1   // Array is from 0 to length-1
	
	if ((AtSym < 1) ||                     // '@' cannot be in first position
	    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
	    (Period == Length ) ||             // Must be atleast one valid char after '.'
	    (Space  != -1))                    // No empty spaces permitted
	   {  
	      EmailOk = false;
	   }
	return EmailOk;
	}
	
	// Open new window; smaller size for Mission Statement
	function jsNewWindow(jsTarget, jsWidth, jsHeight) {
		if (jsWidth == null) {
			jsWidth = 600;
		}
		if (jsHeight == null) {
			jsHeight = 400;
		}
		window.open(jsTarget,"","width=" + jsWidth + ",height=" + jsHeight + ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	}

	// Extract substrings
	function getSubString(str, num1, num2) {
	alert("str = " + str);
		if (str.length > 1 ) {
			var newSubString = str.substr(num1,num2);
			alert("newSubString = " + newSubString);
			return newSubString;
		}
	}

// function to select a specific option in a select-one element
	function selectOption( field, selectValue ) {
		if ( field.type == "select-one" ) {
			for ( i = 0; i < field.length; i++ ) {
				if ( field.options[ i ].value == selectValue ) {
					field.options[ i ].selected = true;
				}
			}
		}
	}

// function to get the value of a cookie
	function getCookieVal (offset) {
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1)
			endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
	}

// function to get the value of a cookie
	function getCookie (name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg)
				return getCookieVal (j);
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break;
		}
		return null;
	}

// function to set the value of a cookie
	function setCookie (name, value) {
		document.cookie = name + "=" + value;
	}

// function to check the value of a cookie and hide a section of html
	function hideSectionFromCookie( cookie, section ) {
		if ( getCookie( cookie ) == 'true' ) {
			if ( document.getElementById( section ) ) {
				document.getElementById( section ).style.display= 'none';
			}
		}
	}

// function to check the value of a cookie and set the href of a link
	function setPageFromCookie( cookie ) {
		if ( getCookie( cookie ) == null ) {
			if ( cookie == 'homePage' ) {
				window.location.href= 'index.html';
			}
			else if ( cookie == 'providerLocatorPage' ) {
				window.location.href= 'providerLocatorInput.jsp';
			}
		}
		else {
			window.location.href = getCookie( cookie );
		}
	}
