var whitespace = " \t\n\r";
var USStateCodes = "|AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AP|"
var badchars = ":{}[]*/%$!;?,\'#<>"

var mPrefix = "Se ha encontrado errores en el formulario. "
var mSuffix = " es un campo necessario."

var iEmail1 = "Se tiene que meter una direcion email valido (como "
var iEmail2 = "). mete la ahora por favor."
var iYourmail = "This field must contain YOUR e-mail address (the 'sender' of the message), not that of your intended recipient. Please enter it now."
var iZIPCode = "This field must be a 5- or 9-digit U.S. ZIP Code (like 94043). Please reenter it now."
var iStateCode = "This field must be a valid two character U.S. state abbreviation (like CA for California). Please reenter it now."


function warnEmpty (theField, s) {
	theField.focus()
	theField.select()
	alert(mPrefix + s + mSuffix)
	return false
}

function warnInvalid (theField, s) {
	theField.focus()
	theField.select()
	alert(s)
	return false
}

function checkString (theField, s) {
	if ( isEmpty(theField.value) || isWhitespace(theField.value))
		return warnEmpty (theField, s);
	else return true;
}

function checkEmail (theField, fieldName) {
	if ( isEmpty(theField.value) || isWhitespace(theField.value))
		return warnEmpty (theField, fieldName);
	theField.value = stripCharsInBag(theField.value, badchars);
	var sample = sampleEmail(theField.value);
	if ( !isEmpty(sample) ) 
		return warnInvalid (theField, iEmail1 + sample + iEmail2);
	else return true;
}

function sampleEmail (s) {
	var i = 1;
	var sLength = s.length;

	while ( (i < sLength) && (s.charAt(i) != "@") )
	{
		i++;
	}
	if ( (i >= sLength) || (s.charAt(i) != "@") ) return (s + "@mydomain.com");
	else i += 2;
	
	while ( (i < sLength) && (s.charAt(i) != ".") )
	{
		i++;
	}
	if ( (i >= sLength - 1) || (s.charAt(i) != ".") ) return (s + ".com");
	else return "";
}



function checkStateCode (theField) {
	if ( isEmpty(theField.value) || isWhitespace(theField.value))
		return warnEmpty (theField, "State");
	theField.value = theField.value.toUpperCase();
	theField.value = stripCharsInBag(theField.value, ".")
	if ( !isStateCode(theField.value) ) 
		return warnInvalid (theField, iStateCode);
	else return true;
}

function isStateCode(s) {
	var smatch = "|" + s + "|";
	return ( USStateCodes.indexOf(smatch) != -1 )
}


function checkZIPCode (azip, bzip) {
	if ( isEmpty(azip.value) || isWhitespace(azip.value))
		return warnEmpty (azip, "ZIP Code");
	var normalizedZIP = stripCharsInBag(azip.value, "-")
	if ( !isZIPCode(normalizedZIP) ) 
		return warnInvalid (azip, iZIPCode);
	else 
	{
		reformatZIPCode(normalizedZIP, azip, bzip)
		return true;
	}
}

function isZIPCode (s) {
	return	(isInteger(s) && ( (s.length == 5) || (s.length == 9) ) );
}

function isInteger (s) {
	var i;
	for (i = 0; i < s.length; i++)
	{   
		if ( !( (s.charAt(i) >= "0") && (s.charAt(i) <= "9") ) ) return false;
	}
	return true;
}

function reformatZIPCode (ZIPString, azip, bzip) {
	if (ZIPString.length == 5) {
		azip.value = ZIPString;
		return true;
	}
	else {
		azip.value = ZIPString.substring(0,5);
		bzip.value = ZIPString.substring(5,9)
		return true;
	}
}



function reformat (s) {
	var arg;
	var sPos = 0;
	var resultString = "";

	for (var i = 1; i < reformat.arguments.length; i++) {
	   arg = reformat.arguments[i];
	   if (i % 2 == 1) resultString += arg;
	   else {
		   resultString += s.substring(sPos, sPos + arg);
		   sPos += arg;
	   }
	}
	return resultString;
}

function stripCharsInBag (s, bag) {
	var i;
	var returnString = "";

	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}

	return returnString;
}

function isEmpty(s) {
	return ( (s == null) || (s.length == 0) )
}

function isWhitespace (s) {
	var i;

	if (isEmpty(s)) return true;

	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}

	return true;
}

function validateForm (form) {
	return (
		checkEmail(form.elements["youremail"], "El campo de su direcion email") &&
		checkString(form.elements["yourmessage"],"El campo del mensage")&&
		checkString(form.elements["first_name"],"Su Nombre")&&
		checkString(form.elements["last_name"],"Su Apellido")
	);
}
//		checkString(form.elements["from"],"Your E-Mail") &&
//		checkString(form.elements["to"],"Friends Address") &&

