// ===================================================================
// Author: Jeff Eaton <verb@predicate.net>
// WWW: http://www.predicate.net/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// Based on Matt Kruse's form validation library. This one handles
// Error messages, setting focus, and so on.
// modified 4/19/05 so validatenumber does not fail on negative numbers
// ===================================================================



function fieldsMatch(element1, element2){
	var passed=false;
	if (getInputValue(element1)==getInputValue(element2)){
		var passed=true;
	}
	return passed;
}




//-------------------------------------------------------------------
// verifyDate(input_object[,message[,true]])
//   Checks a form field for a value different than defaultValue. 
//   Optionally alerts and focuses
//-------------------------------------------------------------------
function verifyDate(obj) {
	var msg;
	var dofocus;
	if (arguments.length>1) { msg = arguments[1]; }
	if (arguments.length>2) { dofocus = arguments[2]; } else { dofocus=false; }
	if (!isBlank(getInputValue(obj))) {
		if (!isDate(getInputValue(obj),"M/d/y")) {
			if (!isBlank(msg)) {
				alert(msg);
				}
			if (dofocus) {
				obj.select();
				obj.focus();
				}
			setInputValue(obj,getInputDefaultValue(obj));
			return true;
			}
		}
	return false;
}



//-------------------------------------------------------------------
// verifyDate(input_object[,message[,true]])
//   Checks a form field for a value different than defaultValue. 
//   Optionally alerts and focuses
//-------------------------------------------------------------------
function verifyEmail(obj) {
	var msg;
	var dofocus;
	if (arguments.length>1) { msg = arguments[1]; }
	if (arguments.length>2) { dofocus = arguments[2]; } else { dofocus=false; }
	if (!isBlank(getInputValue(obj))) {
		if (!isValidEmail(getInputValue(obj))) {
			if (!isBlank(msg)) {
				alert(msg);
				}
			if (dofocus) {
				obj.select();
				obj.focus();
				}
			setInputValue(obj,getInputDefaultValue(obj));
			return true;
			}
		}
	return false;
}




function verifyNumber(obj) {
	var msg;
	var dofocus;
	if (arguments.length>1) { msg = arguments[1]; }
	if (arguments.length>2) { dofocus = arguments[2]; } else { dofocus=false; }
	if (!isBlank(getInputValue(obj))) {
		if (!isNumeric(Math.abs(getInputValue(obj)))) {
			if (!isBlank(msg)) {
				alert(msg);
				}
			if (dofocus) {
				obj.select();
				obj.focus();
				}
			setInputValue(obj,getInputDefaultValue(obj));
			return true;
			}
		}
	return false;
}




function verifyInteger(obj) {
	var msg;
	var dofocus;
	if (arguments.length>1) { msg = arguments[1]; }
	if (arguments.length>2) { dofocus = arguments[2]; } else { dofocus=false; }
	if (!isBlank(getInputValue(obj))) {
		if (!isInteger(getInputValue(obj)) || (isInteger(getInputValue(obj)) && (getInputValue(obj) > 32000))) {
			if (!isBlank(msg)) {
				alert(msg);
				}
			if (dofocus) {
				obj.select();
				obj.focus();
				}
			setInputValue(obj,getInputDefaultValue(obj));
			return true;
			}
		}
	return false;
}


//-------------------------------------------------------------------
// verifyDate(input_object[,message[,true]])
//   Checks a form field for a value different than defaultValue. 
//   Optionally alerts and focuses
//-------------------------------------------------------------------
function verifyUSState(obj) {
	var msg;
	var dofocus;
	if (arguments.length>1) { msg = arguments[1]; }
	if (arguments.length>2) { dofocus = arguments[2]; } else { dofocus=false; }
		if (!isStateAbbr(getInputValue(obj))) {
			if (!isBlank(msg)) {
				alert(msg);
				}
			if (dofocus) {
				obj.select();
				obj.focus();
				}
			setInputValue(obj,getInputDefaultValue(obj));
			return true;
			}
	return false;
}





/**
 * Javascript email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function isValidEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
	}


function isUS(str) {
	if (str == "United States" || str == "US" || str == "USA" || str == "U.S.A" || isBlank(str)) {
		return true;
	}
	return false;
	
	
	
}

/**
 * Nothing should stop this from working
 */

