// JavaScript Document

// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that all fields in the check online registration page are correct
function checkForm() {
		alert("Invalid entry for your first name");
		return false;
	
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that the person entered a username and password
function checkLogin() {
	if (window.document.forms["loginForm"].userName.value.length == 0) {
		alert("You must enter a username");
		return false;
	}
	
	if (window.document.forms["loginForm"].password.value.length == 0) {
		alert("You must enter a password");
		return false;
	}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that all fields in the careers form page are correct
function checkCareersForm() {
	var firstName = window.document.forms["careersForm"].firstName.value;
	var lastName = window.document.forms["careersForm"].lastName.value;
	var phone = window.document.forms["careersForm"].phone.value;
	var email = window.document.forms["careersForm"].email.value;
	var address = window.document.forms["careersForm"].address.value;
	var city = window.document.forms["careersForm"].city.value;
	var zip = window.document.forms["careersForm"].zip.value;
	var resume = window.document.forms["careersForm"].resume.value;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	if (firstName == "" || firstName == "NA" || firstName == "N/A" || firstName.length < 2) {
		alert("Invalid entry for first name");
		return false;
	}
	
	if (lastName == "" || lastName == "NA" || lastName == "N/A" || lastName.length < 2) {
		alert("Invalid entry for last name");
		return false;
	}
	
	if (phone == "" || phone == "NA" || phone == "N/A" || phone.length < 10 || phone.length > 21) {
		alert("Invalid phone number i.e. 1-800-228-0606");
		return false;
	}
	
	if (email.search(emailRegEx) == -1) {
		alert("You must enter a valid email address");
		return false;
	}
	
	if (address == "" || address == "NA" || address == "N/A" || address.length < 5 || address.length > 100) {
		alert("Invalid entry for address");
		return false;
	} 
	
	if (city == "" || city == "NA" || city == "N/A" || city.length < 4 || city.length > 40) {
		alert("Invalid entry for city");
		return false;
	} 
	
	if (zip == "" || zip == "NA" || zip == "N/A" || zip.length < 4 || zip.length > 12) {
		alert("Invalid entry for zip code");
		return false;
	}
	
	if (resume == "" || resume == "NA" || resume == "N/A" || resume.length < 10 || resume.length > 3000) {
		alert("Invalid entry for resume");
		return false;
	}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that all fields in the contact form page are correct
function checkContactForm() {
	var fullName = window.document.forms["contactform"].fullnamecontact.value;
	var companyName = window.document.forms["contactform"].companyname.value;
	var emailAddress = window.document.forms["contactform"].emailaddresscontact.value;
	var message = window.document.forms["contactform"].messagecontact.value;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	document.getElementById('fullNameContact').innerHTML = 'Full Name:';
	document.getElementById('companyNameContact').innerHTML = 'Company:';
	document.getElementById('emailContact').innerHTML = 'Email Address:';
	document.getElementById('messageContact').innerHTML = 'Message:';
	
	if (fullName == "" || fullName == "NA" || fullName == "N/A" || fullName.length < 2) {
		alert("Invalid entry for full name");
		document.getElementById("fullNameContact").innerHTML = "Full Name:".bold().fontcolor("red");
		return false;
	}
	
	if (companyName == "" || companyName == "NA" || companyName == "N/A" || companyName.length < 2) {
		alert("You must enter a company name.\nNOTE: We do not sell to the general public.");
		document.getElementById("companyNameContact").innerHTML = "Company:".bold().fontcolor("red");
		return false;
	}
	
	if (fullName == companyName) {
		alert("You must enter either a valid full name or a valid company name.");
		document.getElementById("fullNameContact").innerHTML = "Full Name:".bold().fontcolor("red");
		document.getElementById("companyNameContact").innerHTML = "Company:".bold().fontcolor("red");
		return false;
	}
	
	if (emailAddress.search(emailRegEx) == -1) {
		alert("You must enter a valid email address");
		document.getElementById("emailContact").innerHTML = "Email Address:".bold().fontcolor("red");
		return false;
	}
	
	if (message.length < 10) {
		alert("You must enter a message");
		document.getElementById("messageContact").innerHTML = "Message:".bold().fontcolor("red");
		return false;
	}
	
	if (message.length > 1000) {
		alert("You have exceeded the maximum message length: 500 characters.");
		document.getElementById("messageContact").innerHTML = "Message:".bold().fontcolor("red");
		return false;
	}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that all fields in the contact form page are correct
function checkCatalogRequestForm() {
	var fullName = window.document.forms["requestCatalogForm"].fullName.value;
	var companyName = window.document.forms["requestCatalogForm"].companyNameRequest.value;
	var emailAddress = window.document.forms["requestCatalogForm"].email.value;
	var address = window.document.forms["requestCatalogForm"].address.value;
	var city = window.document.forms["requestCatalogForm"].city.value;
	var state = window.document.forms["requestCatalogForm"].state.value;
	var zip = window.document.forms["requestCatalogForm"].zip.value;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	if (fullName == "" || fullName == "NA" || fullName == "N/A" || fullName.length < 2) {
		alert("Invalid entry for full name");
		return false;
	}
	
	if (companyName == "" || companyName == "NA" || companyName == "N/A" || companyName.length < 2) {
		alert("You must enter a company name.\nNOTE: We do not sell to the general public.");
		return false;
	}
	
	if (fullName == companyName) {
		alert("You must enter either a valid full name or a valid company name.");
		return false;
	}
	
	if (emailAddress.search(emailRegEx) == -1) {
		alert("You must enter a valid email address");
		return false;
	}
	
	if (address == "" || address == "NA" || address == "N/A" || address.length < 5 || address.length > 100) {
		alert("Invalid entry for address");
		return false;
	} 
	
	if (state == "" || state == "NA" || state == "N/A" || state.length < 4 || state.length > 14) {
		alert("Invalid entry for state");
		return false;
	}
	
	if (city == "" || city == "NA" || city == "N/A" || city.length < 4 || city.length > 40) {
		alert("Invalid entry for city");
		return false;
	} 
	
	if (zip == "" || zip == "NA" || zip == "N/A" || zip.length < 4 || zip.length > 12) {
		alert("Invalid entry for zip code");
		return false;
	}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// Cycles through the browsers to figure out what it's going to use
function readyAJAX() {
	try {
		return new XMLHttpRequest();
	} catch(e) {
		try {
			return new ActiveXObject('Msxm12.XMLHTTP');
		} catch(e) {
			try {
				return new ActiveXObject('Microsoft.XMLHTTP');
			} catch(e) {
				return "A newer browser is needed.";
			}
		}
	}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// This function grabs the info asynchronously
function displayText(param) {
	// Scrolls the screen to the top of the page
	window.document.body.parentNode.scrollTop = 0;
	window.document.body.scrollTop = 0;
	// Lets the user know that the page is currently trying to get whatever information necessary
	document.getElementById('middleColumn').innerHTML = 'Please wait...Loading';
	// Grabs the value of the readyAJAX() function
	var requestObj = readyAJAX();
	// Creates the url that it's going to send to the processPage.php
	var url = "processPage.php?" + param;
	
	requestObj.open("GET",url,true);
	requestObj.send();
	requestObj.onreadystatechange = function() {
		if (requestObj.readyState == 4) {
			if (requestObj.status == 200) {
				// Grabs the result from the processPage.php
				var textFromPHP = requestObj.responseText;
				// Writes it in the middleColumn div
				document.getElementById('middleColumn').innerHTML = textFromPHP;
				
			}
		}
	}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// This function grabs the info asynchronously
function displayOF(param) {
	// Scrolls the screen to the top of the page
	window.document.body.parentNode.scrollTop = 0;
	window.document.body.scrollTop = 0;
	// Lets the user know that the page is currently trying to get whatever information necessary
	document.getElementById('middleColumn').innerHTML = 'Please wait...Loading';
	// Grabs the value of the readyAJAX() function
	var requestObj = readyAJAX();
	// Creates the url that it's going to send to the processPage.php
	var url = "onlineoffice.php?" + param;
	
	requestObj.open("GET",url,true);
	requestObj.send();
	requestObj.onreadystatechange = function() {
		if (requestObj.readyState == 4) {
			if (requestObj.status == 200) {
				// Grabs the result from the processPage.php
				var textFromPHP = requestObj.responseText;
				// Writes it in the middleColumn div
				document.getElementById('middleColumn').innerHTML = textFromPHP;
				
			}
		}
	}
}


// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that all fields in the edit details page are correct
function checkEditDetailsForm() {
	var companyName = window.document.forms["editDetailsForm"].company.value;
	var firstName = window.document.forms["editDetailsForm"].firstName.value;
	var lastName = window.document.forms["editDetailsForm"].lastName.value;
	var address = window.document.forms["editDetailsForm"].streetAddress.value;
	var addressTwo = window.document.forms["editDetailsForm"].streetAddressTwo.value;
	var city = window.document.forms["editDetailsForm"].city.value;
	var state = window.document.forms["editDetailsForm"].state.value;
	var zip = window.document.forms["editDetailsForm"].zip.value;
	var phone = window.document.forms["editDetailsForm"].telephone.value;
	var fax = window.document.forms["editDetailsForm"].faxNumber.value;
	var emailAddress = window.document.forms["editDetailsForm"].emailAddress.value;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	if (companyName == "" || companyName == "NA" || companyName == "N/A" || companyName.length < 2) {
		alert("You must enter a company name");
		return false;
	}
	
	if (firstName == "" || firstName == "NA" || firstName == "N/A" || firstName.length < 2) {
		alert("Invalid enter your first name");
		return false;
	}
	
	if (lastName == "" || lastName == "NA" || firstName == "N/A" || lastName.length < 2) {
		alert("Invalid enter your last name");
		return false;
	}
	
	if (address == "" || address == "NA" || address == "N/A" || address.length < 5 || address.length > 100) {
		alert("Invalid entry for address");
		return false;
	} 
	
	if (addressTwo.length > 100) {
		alert("Invalid entry for second address");
	}
	
	if (city == "" || city == "NA" || city == "N/A" || city.length < 4 || city.length > 40) {
		alert("Invalid entry for city");
		return false;
	} 
	
	if (state == "" || state == "NA" || state == "N/A" || state.length < 4 || state.length > 14) {
		alert("Invalid entry for state");
		return false;
	}
	
	if (zip == "" || zip == "NA" || zip == "N/A" || zip.length < 4 || zip.length > 12) {
		alert("Invalid entry for zip code");
		return false;
	}
	
	if (phone == "" || phone == "NA" || phone == "N/A" || phone.length < 10 || phone.length > 21) {
		alert("Invalid phone number i.e. 1-800-228-0606");
		return false;
	}
	
	if (fax.length > 21) {
		alert("Invalid fax number i.e. 1-800-228-0606");
		return false;
	}
	
	if (emailAddress.search(emailRegEx) == -1) {
		alert("You must enter a valid email address");
		return false;
	}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that all fields in the forgot password form are correct
function checkForgotPasswordForm() {
	var fullName = window.document.forms["contactform"].fullnamecontact.value;
	var emailAddress = window.document.forms["contactform"].emailaddresscontact.value;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	if (fullName == "" || fullName == "NA" || fullName == "N/A" || fullName.length < 2) {
		alert("Invalid entry for full name");
		return false;
	}
	
	if (emailAddress.search(emailRegEx) == -1) {
		alert("You must enter a valid email address");
		return false;
	}
}

// ---------------------------------------------------------------------------------------------------------------------------------------------
// Makes sure that all fields in the cart checkout form are correct
function validateForm() {
	if (document.cartForm.shipment[0].checked == "" && document.cartForm.shipment[1].checked == "") {
		alert ("You must select a Shipment Type");
		return false;
	}
	
	if (document.cartForm.salesPerson.selectedIndex == "") {
		alert ("You must select a sales person");
		return false;
	}
	
	if (document.cartForm.paymentOption.selectedIndex == "") {
		if (document.cartForm.ccName.value == "" || document.cartForm.ccNumber.value == "" || document.cartForm.ccSecurity.value == "" || document.cartForm.ccExp.value == "") {
			
			document.getElementById("ccNameDemo").innerHTML = "Name on Card:".bold().fontcolor("red");
			alert ("You must enter all of the credit card information");
			return false;
		}
	}
}
