function Login(url, email, password) {
	if (CheckEmail(email) && CheckNotEmpty(password)) {
		location.href = url + "?login=" + email + "&password=" + password;
	}
}

function Register(userName, userPass, userPassCnf, firstName, lastName,
		company, phone, address1, address2, city, state, zip) {
	if (CheckEmail(userName) && CheckSame(userPass, userPassCnf)
			&& CheckNotEmpty(firstName) && CheckNotEmpty(lastName)
			&& CheckNotEmpty(company) && CheckTelNo(phone)
			&& CheckNotEmpty(address1) && CheckNotEmpty(address2)
			&& CheckNotEmpty(city) && CheckNotEmpty(state) && CheckZipCode(zip)) {
		$("#frmRegister").submit();
	}
}

function PreValidateRegister(userName, userNameEr, userPass, userPassEr,
		userPassSpEr, userPassCnf, userPassCnfEr, firstName, firstNameEr,
		lastName, lastNameEr, company, companyEr, phone, phoneEr, address1,
		address1Er, address2, address2Er, city, cityEr, state, stateEr, zip,
		zipEr) {
	if (CheckEmail(userName.val())) {
		userNameEr.hide();
	}
	if (CheckNotEmpty(userPass.val())) {
		userPassEr.hide();
	}
	if (CheckNoSpaces(userPass.val())) {
		userPassSpEr.hide();
	}
	if (CheckSame(userPass.val(), userPassCnf.val())
			&& CheckNotEmpty(userPassCnf.val())) {
		userPassCnfEr.hide();
	}
	if (CheckNotEmpty(firstName.val())) {
		firstNameEr.hide();
	}
	if (CheckNotEmpty(lastName.val())) {
		lastNameEr.hide();
	}
	if (CheckNotEmpty(company.val())) {
		companyEr.hide();
	}
	if (CheckNotEmpty(address1.val())) {
		address1Er.hide();
	}
	if (CheckNotEmpty(address2.val())) {
		address2Er.hide();
	}
	if (CheckNotEmpty(city.val())) {
		cityEr.hide();
	}
	if (CheckNotEmpty(state.val())) {
		stateEr.hide();
	}
	if (CheckZipCode(zip.val())) {
		zipEr.hide();
	}
	if (CheckTelNo(phone.val())) {
		phoneEr.hide();
	}
}

function ChangePassword(newPassword, newPasswordCnf) {
	if (CheckSame(newPassword, newPasswordCnf)) {
		$("#frmChange").submit();
	}
}

function LostEmail(userName) {
	if (CheckEmail(userName)) {
		$("#frmLostPassword").submit();
	}
}

function CheckEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	return false;
}

function CheckNotEmpty(value) {
	if (value != null && value != '') {
		return true;
	}
	return false;
}

function CheckSame(value1, value2) {
	if (value1 == value2) {
		return true;
	}
	return false;
}

function CheckNoSpaces(value) {
	if (!(value.indexOf(" ") > -1)) {
		return true;
	}
	return false;
}

function FormatTelNo(telNo) {
	if (telNo.value == "")
		return;

	var phone = new String(telNo.value);

	phone = phone.substring(0, 14);

	if (phone.match(".[0-9]{3}.[0-9]{3}-[0-9]{4}") == null) {
		if (phone.match(".[0-9]{2}.[0-9]{3}-[0-9]{4}|"
				+ ".[0-9].[0-9]{3}-[0-9]{4}|" + ".[0-9]{3}.[0-9]{2}-[0-9]{4}|"
				+ ".[0-9]{3}.[0-9]-[0-9]{4}") == null) {
			var phoneNumeric = phoneChar = "", i;

			for (i = 0; i < phone.length; i++) {
				phoneChar = phone.substr(i, 1);

				if (!isNaN(phoneChar) && (phoneChar != " "))
					phoneNumeric = phoneNumeric + phoneChar;
			}

			phone = "";

			for (i = 0; i < phoneNumeric.length; i++) {
				if (i == 0)
					phone = phone + "(";
				if (i == 3)
					phone = phone + ") ";
				if (i == 6)
					phone = phone + "-";

				phone = phone + phoneNumeric.substr(i, 1);
			}
		}
	} else {
		phone = "(" + phone.substring(1, 4) + ") " + phone.substring(5, 8)
				+ "-" + phone.substring(9, 13);
	}

	if (phone != telNo.value)
		telNo.value = phone;
}

function CheckTelNo(telNo) {
	var value = telNo.value;
	if (value == null) {
		value = telNo;
	}

	if (value == "")
		return false;
	if (value.match("\\([0-9]{3}\\) [0-9]{3}-[0-9]{4}") == null) {
		if (value.match("[0-9]{10}") != null) {
			if (telNo.value == null) {
				FormatTelNo(value);
				return true;
			} else {
				FormatTelNo(telNo);
				return true;
			}
		}
	} else {
		return true;
	}
	return false;
}

function CheckZipCode(zipCode) {
	var filter = /^\d{5}([\-]\d{4})?$/;
	if (filter.test(zipCode)) {
		return true;
	}
	return false;
}
