/*
	The isEmpty and isWhitespace functions are from Netscape's JavaScript dev site, http://developer.netscape.com.
	Some functions from http://www.4guysfromrolla.com/webtech/091998-1.shtml, which said to take them for free.
	I've added more functions and customized things as I went along... jfd 11/21/2002.
*/

// whitespace characters
var whitespace = " \t\n\r";

/****************************************************************/
// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

/****************************************************************/
function isWhitespace (s)
{
	var i;

	// Is s empty?
	if (isEmpty(s)) return true;

	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{
			// Check that current character isn't whitespace.
			var c = s.charAt(i);

			if (whitespace.indexOf(c) == -1) return false;
	}

	// All characters are whitespace.
	return true;
}

/****************************************************************/
function isBlank(val, str) {
	var strInput = new String(val.value);

	if (isWhitespace(strInput) || isEmpty(strInput)) {
		alert(str);
		return true;
	} else {
		return false;
	}

}

/****************************************************************/
function isBlankinUSA(val, valCountry, str) {
	var strInput = new String(val.value);

	if (valCountry.value == "USA" && (isWhitespace(strInput) || isEmpty(strInput))) {
		alert(str);
		return true;
	} else {
		return false;
	}

}

/****************************************************************/
function passwordIsMismatched(val1, val2, str) {

	if (val1.value == val2.value) {
		return false;
	}
	else {
		alert(str);
		//alert("str1: " + val1.value + " str2: " + val2.value);
		return true;
	}

}

/****************************************************************/
function forbiddenValue(val, forbidStr, str) {

	if (val.value == forbidStr) {
		alert(str);
		return true;
	}
	else {
		return false;
	}

}

/****************************************************************/
function bothFilled(val1, val2, str) {
	var strInput1 = new String(val1.value);
	var strInput2 = new String(val2.value);
	
	if (isWhitespace(strInput1) || isWhitespace(strInput2)) {
		return false;
	}
	else {
		alert(str);
		return true;
	}

}

/****************************************************************/
function bothEmpty(val1, val2, str) {
	var strInput1 = new String(val1.value);
	var strInput2 = new String(val2.value);

	if (!isWhitespace(strInput1) || !isWhitespace(strInput2)) {
		return false;
	}
	else {
		alert(str);
		return true;
	}

}

/****************************************************************/
function notEmail(val1, str) {
	var strInput = new String(val1.value);

	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	
	var len = strInput.length;
	var rt = strInput.substring(len-4).toLowerCase();
	
	if (!supported) {
		if ((rt.substring(0,1) == "." || rt.substring(1,1) == ".") && strInput.indexOf("@") != -1) {
			return false;
		}
		else {
			alert(str);
			return true;
		}
	}
	else {
		// use regexp
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		if (!r1.test(strInput) && r2.test(strInput)) {
			return false;
		}
		else {
			alert(str);
			return true;
		}
	}
	
}

/****************************************************************/
function ValidateUserData() {

	// Data validations:
	if (isBlank(document.forms[0].LoginName,"LoginName cannot be blank.") ||
		isBlank(document.forms[0].Password,"Both Password and Password-confirm cannot be blank.") ||
		isBlank(document.forms[0].Password_confirm,"Both Password and Password-confirm cannot be blank.") ||
		isBlank(document.forms[0].Name,"Name cannot be blank.") ||
		passwordIsMismatched(document.forms[0].Password, document.forms[0].Password_confirm, "The passwords you entered do not match.") ||
		bothFilled(document.forms[0].SalespersonKey, document.forms[0].CustomerKey, "A user cannot be associated with both a Salesperson (Rep) and a Customer (Designer). One or the other must be blank.") ||
		bothEmpty(document.forms[0].SalespersonKey, document.forms[0].CustomerKey, "A user must be associated with either a Salesperson (Rep) or a Customer (Designer).") )
	{ 
		return false; 
	}
	else {
		return true;
	}
}

/****************************************************************/
function ValidateManufStatus() {

	// Data validations:
	if (isBlank(document.forms[0].ItemKey,"The Item Key cannot be blank.") ||
		isBlank(document.forms[0].NumPieces,"The Number of Pieces cannot be blank.") )
		// isBlank(document.forms[0].OrderNumber,"The Order Number cannot be blank.") ||
		{ 
		return false; 
	}
	else {
		return true;
	}
}

/****************************************************************/
function ValidateApplication() {

	// Data validations:
	if (isBlank(document.forms[0].uid,"The Preferred Login Name cannot be blank.") ||
		isBlank(document.forms[0].bizName,"The Business Name cannot be blank.") ||
		isBlank(document.forms[0].bizAddress1, "The Address cannot be blank.") ||
		isBlank(document.forms[0].city, "The City cannot be blank.") ||
		forbiddenValue(document.forms[0].city, "City*", "The City cannot be blank.") ||
		isBlank(document.forms[0].state, "The State or Province cannot be blank.") ||
		forbiddenValue(document.forms[0].state, "State/Province*", "The State or Province cannot be blank.") ||
		isBlank(document.forms[0].zip, "The Zip or Postal Code cannot be blank.") ||
		isBlank(document.forms[0].country, "The Country cannot be blank.") ||
		isBlank(document.forms[0].bizEmail,"Email cannot be blank.") ||
		notEmail(document.forms[0].bizEmail,"Must enter a valid e-mail address.") ||
		isBlank(document.forms[0].bizContactFname,"The Contact Person's first name cannot be blank.") ||
		forbiddenValue(document.forms[0].bizContactFname, "First Name*", "The Contact Person's first name cannot be blank.") ||
		isBlank(document.forms[0].bizContactLname,"The Contact Person's last name cannot be blank.") ||
		forbiddenValue(document.forms[0].bizContactLname, "Last Name*", "The Contact Person's last name cannot be blank.") ||
		isBlankinUSA(document.forms[0].taxIdNumber, document.forms[0].country, "The Tax Id Number cannot be blank for addresses in the USA.") )
		{ 
		return false; 
	}
	else {
		return true;
	}
}

/****************************************************************/
function ValidateContact() {

	// Data validations:
	if (isBlank(document.forms[0].fname,"The first name cannot be blank.") ||
		isBlank(document.forms[0].lname, "The last name cannot be blank.") ||
		isBlank(document.forms[0].email, "The e-mail address cannot be blank.") ||
		notEmail(document.forms[0].email, "The e-mail address is not valid.") ||
		isBlank(document.forms[0].telephone, "The telephone cannot be blank.") ||
		isBlank(document.forms[0].state, "The state or province cannot be blank.") ||
		isBlank(document.forms[0].country, "The country cannot be blank.") ||
		isBlank(document.forms[0].subject, "The subject cannot be blank.") ||
		isBlank(document.forms[0].message, "The message cannot be blank.") )
		{ 
		return false; 
	}
	else {
		return true;
	}
}

/****************************************************************/
function ValidateForgot() {

	// Data validations:
	if (isBlank(document.forms[0].email_addr, "The e-mail address cannot be blank.") )
		{ 
		return false; 
	}
	else {
		return true;
	}
}
