var alphanumchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁáÉéÍíÓóÚúñÑÀÂÈÊàâèêôû0123456789";
var alphachars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁáÉéÍíÓóÚúñÑÀÂÈÊàâèêôû";

function isAlphanumeric(s, valid_chars) {
	var allValid = true;
	var ch = '';
	for (i = 0;  i < s.length;  i++) {
		ch = s.charAt(i);
		for (j = 0;  j < valid_chars.length;  j++)
			if (ch == valid_chars.charAt(j))
				break;
			if (j == valid_chars.length) {
				allValid = false;
				//alert(s.charAt(j));
			break;
		}
	}
	return allValid;
}

function isValidEmail(s){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(s)){
		return true;
	} else {
		return false;
	}

}

function LTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all leading white-space characters
    if (whitespace.indexOf(s.charAt(0)) != -1) {

        var j=0, i = s.length;
    
        // Iterate from the left until we have no more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;

        // Get the substring from the first non-whitespace 
        // character to the end of the string...
        s = s.substring(j, i);
    }
    return s;
}

function RTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all trailing white-space characters
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

        var i = s.length - 1;

        // Iterate from the right until we have no more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;

        // Get the substring from the beginning of the string to
        // where the last non-whitespace character
        s = s.substring(0, i+1);
    }
    return s;
}

function Trim(str) {
    // Strip away all leading and trailing white-space characters
    return RTrim(LTrim(str));
}

//used to control max input of characters for password
function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, field.value.length - 1)	
	//remove the last character entered because it's the maxlimit + 1 character
	alert("Your message cannot exceed " + maxlimit + " characters.");
	return true;
	}
}

function ValidateForm(form) {
	var str1 = str2 = '';
	// FIRST NAME
	if(form.first != null) {
		form.first.value = Trim(form.first.value);
		str = form.first.value;
		if(str == '' || str.length < 1) {
			alert("Please enter your first name.");
			form.first.focus();
			return false;
		}
		if(!isAlphanumeric(str, alphachars + '-.` \'')) {
			alert("Invalid character in first name.");
			form.first.focus();
			form.first.select();
			return false;    
		}
	}
	// LAST NAME
	if(form.last != null) {
		form.last.value = Trim(form.last.value);
		str = form.last.value;
		if(str == '' || str.length < 1) {
			alert("Please enter your last name.");
			form.last.focus();
			return false;
		}
		if(!isAlphanumeric(str, alphachars + '-.` \'')) {
			alert("Invalid character in last name.");
			form.last.focus();
			form.last.select();
			return false;    
		}
	}
	// E-MAIL
	if(form.email != null) {
		form.email.value = Trim(form.email.value);
		str = form.email.value;
		if(!isAlphanumeric(str.toLowerCase(), 'abcdefghijklmnopqrstuvwxyz0123456789@._-< >')) {
			alert("Invalid character in email address.");
			form.email.focus();
			form.email.select();
			return false;    
		}
		if(str == '' || !isValidEmail(str)) {
			alert("Please enter a valid email address.");
			form.email.select();
			return false;
		}
	}
	// ITEM
	if(form.item != null) {
		form.item.value = Trim(form.item.value);
		str = form.item.value;
		if(str == '' || str.length < 1) {
			alert("Please enter the item that you would like to order.");
			form.item.select();
			form.item.focus();
			return false;
		}
		// must contain alphanumeric characters only...
		if(!isAlphanumeric(str, alphanumchars + '~`!@#$*^(),.? -\'')) {
			alert("Item contains invalid character(s).");
			form.item.select();
			form.item.focus();
			return false;
		} 
	}
	// disable button
	form.xsubmit.disabled = true;

	// all is good, submit form
	return true;
}