var whitespace = " \t\n\r";

var iEmail = "Your email address is not a valid one (should be like foo@bar.com).";
var iPassword = "This password is different from the one above.";

var sAddress ="an address in the first line.";
var sCity ="your city.";
var sCountry ="a country.";
var sEmail = "your e-mail address.";
var sField ="a field of interest.";
var sFirstName ="your first name.";
var sLastName ="your last name.";
var sPassword ="your password.";
var sZipCode ="your zip code.";

function checkEmail(theField)
{
	if (checkString(theField,sEmail)) {
		if (!isEmail(theField.value)) {
			warnInvalid(theField,iEmail);
			return false;
		}
		return true;
	}
	else return false;
}

function checkLastName(theField)
{
	return checkString(theField,sLastName);
}

function checkList(theList,s)
{
	if (theList.options[0].selected == true) {
		warnSelect(theList,s);
		return false;
	}
	else {
		return true;
	}
}

function checkPassword(theField)
{
	return checkString(theField,sPassword);
}

function checkPasswordCoherence(theField)
{
	if (theField.value != document.form1.pwd.value) {
		warnInvalid(theField,iPassword);
		return false;
	}
	else {
		return true;
	}
}

function checkAllDigits(theField)
{
	s = theField.value;
	snew = "";
	for (i=0; i<s.length; i++)
	{   
		c = s.charAt(i);
		if (c < "0" || c > "9") {
			continue;
		}
		else {
			snew += c;
		}
	}
	theField.value = snew;
	return true;
}

function checkString(theField, s)
{
	if (isWhitespace(theField.value)) {
		warnEmpty(theField,s);
		return false;
	}
	else {
		return true;
	}
}

function copyCodeToFax(theField)
{
	document.form1.codeFax.value = theField.value;
	return true;
}

function copyCodeToPhone(theField)
{
	document.form1.codePhone.value = theField.value;
	return true;
}

function isEmail(s)
{
	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var i = 1;
	var sLength = s.length;
	
	// look for the @ sign.
	while ((i < sLength) && (s.charAt(i) != "@")) i++;

	if ((i >= sLength) || (s.charAt(i) != "@")) {
		return false;
	}
	else {
		i += 2;
	}

	// look for the . sign.
	while ((i < sLength) && (s.charAt(i) != "."))
 	{
		i++;
	}

	// there must be at least one character after the . sign.
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
		return false;
	}
	else {
		return true;
	}
}

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 leaveASCIIOnly(theTextField) {
	theString = theTextField.value;
	snew = "";
	for (i=0; i<theString.length; i++) {
		next = theString.charAt(i);
		code = next.charCodeAt(0);
		if (code < 48 || (code > 58 && code <65) || (code > 90 && code <97) || code > 122) {
			continue;
		}
		snew += next;
	}
	theTextField.value = snew;
}

function setCountryIndex(theList)
{   
	document.form1.countryIndex.value = theList.selectedIndex;
	return true;
}

function setFieldIndex(theList)
{   
	document.form1.fieldIndex.value = theList.selectedIndex;
	return true;
}

function warnEmpty(theField,s)
{   
	var msg="This field is required. Please enter it.";
	if(warnEmpty.arguments.length > 1) msg = "Please enter "+s;
	alert(msg);
	theField.focus();
	theField.select();
	return false;
}

function warnInvalid(theField, s)
{
	alert(s);
	theField.focus();
	theField.select();
	return false;
}

function warnSelect(theList,s)
{   
	var msg="Please select "+s;
	alert(msg);
	theList.focus();
	return false;
}


function validate(form)
{
	return(
		checkString(form.lastName,sLastName)
		&& checkString(form.email,sEmail)
		&& checkEmail(form.email)
		&& checkString(form.pwd,sPassword)
		&& checkList(form.field,sField)
		&& setFieldIndex(form.field)
		&& checkString(form.address1,sAddress)
		&& checkString(form.city,sCity)
		&& checkString(form.zipCode,sZipCode)
		&& checkList(form.country,sCountry)
		&& setCountryIndex(form.country)
	);
}

