
var SIZE = 65000;//SIZE OF TEXT.
var error = "";

/**
 * CHECK IF AN INPUT IS A NON ZERO POSITIVE INTEGER.
 */
function isPK(name, input, required)
{
	var number = 0;
	var number_list = "0123456789";
	var is_numeric = true;
	var character = "";
	var i = 0;

	//if the min is 0 and the input.length is 0, then it is not required
	if(!required && (input.length == 0))
	{
		return;
	}

	//check if each character is a digit.
	for(i = 0; i < input.length && is_numeric; i++)
	{
		character = input.charAt(i);
		if(number_list.indexOf(character) == -1)
		{
			is_numeric = false;
		}
	}

	//if the input is not a digit, echo an error.
	if(!is_numeric)
	{
		error = error + name + " is not a positive integer.\n";
	}
	else
	{
		if(required && input.length != 0)
		{
			number = parseInt(input, 10);

			if(number < 1)
			{
				error = error + name + " is not a positive integer.\n";
			}
		}
		else if(required && input.length == 0)
		{
			error = error + name + " is required.\n";
		}
		else
		{

	}
	}
}

/**
 * CHECK IF AN INPUT IS AN INTEGER.
 */
function isInteger(name, input, min, max, required)
{
	var number_list = "0123456789";
	var is_numeric = true;
	var character = "";

	//if the min is 0 and the input.length is 0, then it is not required
	if(!required && (input.length == 0))
	{
		return;
	}

	//check if each character is a digit.
	for(var i = 0; i < input.length && is_numeric; i++)
	{
		character = input.charAt(i);
		if(number_list.indexOf(character) == -1)
		{
			is_numeric = false;
		}
	}

	//if the input is not a digit, echo an error.
	if(!is_numeric)
	{
		error = error + name + " is not numeric.\n";
	}

	//if the min and max are the same
	if(min == max)
	{
		if(input.length != max)
		{
			error = error + "Please enter a valid " + name + " of " + max + " digits.\n";
		}
	}
	else
	{
		if((input.length < min) || (input.length > max))
		{
			error = error + name + " should be between " + min + " and " + max + " digits.\n";
		}
	}
}

function isNumeric(name, input, min, max, required)
{
	var number_list = "0123456789.";
	var is_numeric = true;
	var character = "";

	//if the min is 0 and the input.length is 0, then it is not required
	if(!required && (input.length == 0))
	{
		return;
	}

	//check if each character is a digit.
	for(i = 0; i < input.length && is_numeric; i++)
	{
		character = input.charAt(i);
		if(number_list.indexOf(character) == -1)
		{
			is_numeric = false;
		}
	}

	//if the input is not a digit, echo an error.
	if(!is_numeric)
	{
		error = error + name + " is not numeric.\n";
	}

	//if the min and max are the same
	if(min == max)
	{
		if(input.length != max)
		{
			error = error + "Please enter a valid " + name + " of " + max + " digits.\n";
		}
	}
	else
	{
		if((input.length < min) || (input.length > max))
		{
			error = error + name + " should be between " + min + " and " + max + " digits.\n";
		}
	}
}

//validate the length of something.
function isLength(name, input, min, max, required)
{
	//if the min is 0 and the input.length is 0, then it is not required
	if(!required && (input.length == 0))
	{
		return;
	}
	else if(min == max)
	{
		if(input.length != max)
		{
			error = error + name + " should be " + max + " characters.\n";
		}
	}
	else
	{
		if((input.length < min) || (input.length > max))
		{
			error = error + name + " should be between " + min + " and " + max + " characters.\n";
		}
	}
}

/**
 * VALIDATE A DATE FORMAT (MM-DD-YYYY)
 */
function MMDDYYYY(name, input)
{
	//CHECK IF THE DATE IS EMPTY.
	if(input.length == 0)
	{
		error = error + name + " is empty.\n";
		return;
	}
	var date = input.split("-");

	//CHECK THE SIZE OF THE DATE ELEMENT.
	if(date.length < 3)
	{
		error = error + " invalid " + name + "\n";
		return;
	}

	var month = parseInt(date[0], 10);
	var day = parseInt(date[1], 10);
	var year = parseInt(date[2], 10);
	var today = new Date();

	//CHECK YEAR.
	if(year < today.getYear())
	{
		error = error + " invalid year on " + name + ".\n";
		return;
	}

	//CHECK MONTH.
	if(month < 1 || month > 12)
	{
		error = error + " invalid month " + name + ".\n";
		return;
	}

	//CHECK DAY.
	if(day < 1 || day > 31)
	{
		error = error + " invalid day " + name + ".\n";
	}
}

/**
 * CHECK FOR ALPHANUMERIC.
 */
function isAlphaNumeric(name, input, min, max, required)
{
	var list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var is_alpha_numeric = true;
	var character = "";

	//if the min is 0 and the input.length is 0, then it is not required
	if(!required && (input.length == 0))
	{
		return;
	}

	//check if each character is a digit.
	for(i = 0; i < input.length && is_alpha_numeric; i++)
	{
		character = input.charAt(i);
		if(list.indexOf(character) == -1)
		{
			is_alpha_numeric = false;
		}
	}

	//if the input is not a digit, echo an error.
	if(!is_alpha_numeric)
	{
		error = error + name + " is not alpha numeric.\n";
	}

	//if the min and max are the same
	if(min == max)
	{
		if(input.length != max)
		{
			error = error + "Please enter a valid " + name + " of " + max + " characters.\n";
		}
	}
	else
	{
		if((input.length < min) || (input.length > max))
		{
			error = error + name + " should be between " + min + " and " + max + " characters.\n";
		}
	}
}

function validateAddress(form)
{
	error = "";
	with(form)
	{
		isLength("Name", b_name.value, 1, 50, 1);
		isLength("Street", b_street.value, 1, 50, 1);
		isLength("City", b_city.value, 1, 50, 1);
		isLength("State", b_state.value, 2, 2, 1);
		isInteger("Zip Code", b_zip_code.value, 5, 5, 1);
		isInteger("Phone", b_phone.value, 10, 10, 1);
		isLength("Email", b_email.value, 1, 40, 1);
		isInteger("Fax", b_fax.value, 10, 10, 1);
		isLength("Comments", comments.value, 1, 1000, 0);
		isLength("Shipping Comments", shipping_comments.value, 1, 1000, 0);

		if(same.checked == false)
		{
			isLength("Shipping Name", s_name.value, 1, 50, 1);
			isLength("Shipping Street", s_street.value, 1, 50, 1);
			isLength("Shipping City", s_city.value, 1, 50, 1);
			isLength("Shipping State", s_state.value, 2, 2, 1);
			isInteger("Shipping Zip Code", s_zip_code.value, 5, 5, 1);
			isInteger("Shipping Phone", s_phone.value, 10, 10, 1);
			isLength("Shipping Email", s_email.value, 1, 40, 0);
			isInteger("Shipping Fax", s_fax.value, 10, 10, 0);
		}
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

function validateNewUserRequest(form)
{
	error = "";
	with(form)
	{
		isLength("First Name", firstName.value, 1, 50, 1);
		isLength("Last Name", lastName.value, 1, 50, 1);
		isLength("Organization", organization.value, 1, 40, 1);
		isLength("Email", email.value, 1, 40, 1);

		if(password.value != '')
		{
			isLength("Password", password.value, 5, 20, 1);
		}
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

function validateContact(form)
{
	error = "";
	with(form)
	{
		isLength("Category", category.value, 1, 50, 1);
		isLength("Name", name.value, 1, 50, 1);
		isLength("Title", title.value, 1, 50, 1);
		isLength("Organization", organization.value, 1, 50, 1);
		isLength("Address", address.value, 1, 50, 1);
		isLength("City", city.value, 1, 50, 1);
		isLength("State", state.value, 2, 2, 1);
		isInteger("Zip Code", zip_code.value, 5, 5, 1);
		isInteger("Phone", phone.value, 10, 10, 1);
		isLength("Email", email.value, 1, 50, 0);
		isLength("Comments", comments.value, 1, 100, 0);
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

/**
 * VALIDATE EQUIPMENT QUANTITY.
 */
function validateEquipmentQuantity(form)
{
	error = "";//zero out error.

	with(form)
	{
		isPK("Quantity", quantity.value, 1);//positive integer.
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

/**
 * VALIDATE EQ98
 */
function validateEQ98(form)
{
	error = "";//zero out error.

	with(form)
	{
		isPK("Quantity", quantity.value, 1);

		//DATE IS NOT REQUIRED.
		if(date.value != "")
		{
			MMDDYYYY("Delivery Date", date.value);
		}
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

/**
 * VALIDATE 58UB
 */
function validate58UB(form)
{
	error = "";//zero out error.

	with(form)
	{
		isPK("Quantity", quantity.value, 1);
		//DATE NOT REQUIRED.
		if(date.value != "")
		{
			MMDDYYYY("Delivery Date", date.value);
		}
		isLength("Organization", organization.value, 1, 50, 1);
		isLength("Street", street.value, 1, 50, 1);
		isLength("City", city.value, 1, 50, 1);
		isLength("State", state.value, 2, 2, 1);
		isInteger("Zip Code", zip_code.value, 5, 5, 1);
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

/**
 * VALIDATE DT12
 */
function validateDT12(form)
{
	error = "";//zero out error.

	with(form)
	{
		isPK("Quantity", quantity.value, 1);
		//DATE NOT REQUIRED.
		if(date.value != "")
		{
			MMDDYYYY("Delivery Date", date.value);
		}
		isLength("Organization", organization.value, 1, 50, 1);
		isLength("Street", street.value, 1, 50, 1);
		isLength("City", city.value, 1, 50, 1);
		isLength("State", state.value, 2, 2, 1);
		isInteger("Zip Code", zip_code.value, 5, 5, 1);
		isPK("Account #", account_number.value, 1);
		isPK("MICR/Bank #", micr.value, 1);
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

/**
 * VALIDATE E32
 */
function validateE32(form)
{
	error = "";//zero out error.

	with(form)
	{
		isPK("Quantity", quantity.value, 1);
		//DATE NOT REQUIRED.
		if(date.value != "")
		{
			MMDDYYYY("Delivery Date", date.value);
		}
		isPK("Window", window.value, 1);
		isLength("Organization", organization.value, 1, 50, 1);
		isLength("Street", street.value, 1, 50, 1);
		isLength("City", city.value, 1, 50, 1);
		isLength("State", state.value, 2, 2, 1);
		isInteger("Zip Code", zip_code.value, 5, 5, 1);
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

/**
 * VALIDATE 34PNE
 */
function validate34PNE(form)
{
	error = "";//zero out error.

	with(form)
	{
		//DATE NOT REQUIRED.
		if(date.value != "")
		{
			MMDDYYYY("Delivery Date", date.value);
		}
		isLength("Ink color", ink_color.value, 2, 10, 1);
		isPK("Packaging", packaging.value, 1);
		isInteger("Routing #", account_number.value, 5, 9, 1);
		isInteger("Account #", micr.value, 5, 20, 1);
		isInteger("Starting Check #", starting_check_number.value, 1, 10, 1);
		isPK("Quantity", quantity.value, 1);
		isLength("Organization", organization.value, 1, 50, 1);
		isLength("Street", street.value, 1, 50, 1);
		isLength("City", city.value, 1, 50, 1);
		isLength("State", state.value, 2, 2, 1);
		isInteger("Zip Code", zip_code.value, 5, 5, 1);
		}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

function validateUpdateUser(form)
{
	error = "";

	with(form)
	{
		isLength("First Name", firstName.value, 1, 20, 1);
		isLength("Last Name", lastName.value, 1, 20, 1);
		isLength("Street", street.value, 1, 50, 1);
		isLength("City", city.value, 1, 50, 1);
		isLength("State", state.value, 2, 2, 1);
		isInteger("Zip Code", zipCode.value, 5, 5, 1);
		isInteger("Phone", phone.value, 10, 10, 1);
		isLength("Email", email.value, 1, 50, 1);
	}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

function validateUpdateLogin(form)
{
	error = "";

	with(form)
	{
		isLength("Password", password.value, 1, 20, 1);
		isLength("Confirm Password", confirmPassword.value, 1, 20, 1);

		if(password.value != confirmPassword.value)
		{
			error += "Passwords Do Not Match.\n";
		}
	}

	if(error == "")
	{
		return true;
	}
	else
	{
		alert(error);
		return false;
	}
}

