// Define the maximum price for the products
var MAXIMUM_PRICE = 1000000;
var spaces = " \t\r\n";

function empty(str)
{
	for (var i = 0; i < str.length; i++)
		if (spaces.indexOf(str.charAt(i)) < 0)
			return false;
	return true;
}

function trim(str)
{
	var i, j;
	for (i = 0; i < str.length; i++)
		if (spaces.indexOf(str.charAt(i)) < 0)
			break;
	for (j = str.length-1; j >= 0; j--)
		if (spaces.indexOf(str.charAt(j)) < 0)
			break;
	if (i <= j)
		return str.substring(i, j+1);
	return "";
}

function len(str)
{
	return str.length;
}

function checkdate(str, f) // f - date format: 1 - yyyy.mm.dd, 2 - dd/mm/yyyy, 3 - mm/dd/yyyy
{
	var x, s, s1, s2, s3, d, m, y;

	s3 = "/.-";

	str = trim(str);
	if (str == "") return true;

	for (m = 0; m < s3.length; m++) {
		s = s3.substring(m, m+1);
		x = str.indexOf(s);
		if (x >= 0) break;
	}
	if (x <= 0 || x+1 == str.length)
		return false;
	s1 = str.substring(0, x);
	x = str.indexOf(s, x+1);
	if (x < 0 || x+1 == str.length)
		return false;
	s2 = str.substring(s1.length+1, x);
	s3 = str.substring(x+1);

	if (f == 1) {
		y = s1;
		m = s2;
		d = s3;
	} else if (f == 2) {
		d = s1;
		m = s2;
		y = s3;
	} else {
		m = s1;
		d = s2;
		y = s3;
	}

	return checkinteger(m) && checkrange(m, 1, 12) && checkinteger(y) && checkrange(y, 0, null) && checkinteger(d) && checkday(y, m, d);
}

function checkday(y, m, d)
{
//	var maxDay = 31;
//	if (m == 4 || m == 6 || m == 9 || m == 11)
//		maxDay = 30;
//	else if (m == 2) {
//		if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
//			maxDay =29;
//		else
//			maxDay = 28;
//	}
//	return checkrange(d, 1, maxDay);
	if (m == 12) {
		y++;
		m = 0;
	}
	var dt = new Date(y, m, 1);
	dt.setTime(dt.getTime()-1);
	return (d <= dt.getDate());
}

function checkinteger(s)
{
	if (empty(s)) return true;
	if (s.indexOf(".") < 0)
		return checknumber(s);
	return false;
}

function numberrange(n, min, max)
{
	if (min != null && n < min || max != null && n > max)
		return false;
	return true;
}

function checknumber(s)
{
	s = trim(s);
	if (s == "") return true;
	var number_format = ".0123456789";
	var start_format = "+-" + number_format;
	var decimal = false;
	var x = start_format.indexOf(s.charAt(0))
	if (x < 0) return false;
	if (x == 2) decimal = true;
	for (var i = 1; i < s.length; i++) {
		x = number_format.indexOf(s.charAt(i))
		if (x < 0)
			return false;
		if (x == 0) {
			if (decimal) return false;
			decimal = true;
		}
	}
	return true;
}

function checkrange(s, min, max)
{
	if (empty(s)) return true;
	return checknumber(s) && numberrange(eval(s), min, max);
}

function checkemail(emailaddr)
{
	var i, c, emailbox, emaildomain;

	emailaddr = trim(emailaddr);
	if (emailaddr == "") return true;
	if (emailaddr.indexOf("@") < 0) {
		alert("No @ sign detected. An @ sign is part of every e-mail address.");
		return false;
	}
	if (emailaddr.charAt(emailaddr.length-1) == "@") {
		alert("An @ sign cannot be the last character of the e-mail address.");
		return false;
	}
	if (emailaddr.charAt(0) == "@") {
		alert("An @ sign cannot be the first character of the e-mail address.");
		return false;
	}
	if (emailaddr.indexOf(".") < 0) {
		alert("No period detected. An e-mail address contains at least one dot.");
		return false;
	}
	if (emailaddr.charAt(emailaddr.length-1) == ".") {
		alert("The last character of the e-mail address cannot be a dot.");
		return false;
	}
	if (emailaddr.charAt(0) == ".") {
		alert("The first character of the e-mail address cannot be a dot.");
		return false;
	}
	if (emailaddr.indexOf(",") >= 0) {
		alert("A valid e-mail address cannot contain a comma.\r\nIf you have a CompuServe account, substitute a period for the comma in your CompuServe ID,\r\nlike so: 12345.6789@compuserve.com.");
		return false;
	}
	if (emailaddr.indexOf(" ") >= 0) {
		alert("A valid e-mail address cannot contain a space.");
		return false;
	}
	if (emailaddr.indexOf("@",emailaddr.indexOf("@")+1) > 0) {
		alert("A valid e-mail address can only contain one @ sign.");
		return false;
	}
	for (i = 0; i < emailaddr.length; i++) {
		c = emailaddr.charAt(i);
		if (c < " " || "!#$%^&*+=|\\/:;?\"'<>(){}[]`~".indexOf(c) >= 0) {
			alert("A valid e-mail address cannot contain non-printable character or any of these characters:\r\n! # $ % ^ & * + = | \\ / : ; ? \" ' < > ( ) { } [ ] ` ~");
			return false;
		}
	}
	i = emailaddr.indexOf("@");
	emailbox = emailaddr.substring(0,i);
	emaildomain = emailaddr.substring(i+1,emailaddr.length);
	if (emaildomain.indexOf(".") < 0) {
		alert("The host portion of the e-mail address was not valid, there must be at least 2 parts to the domain (i.e. a.com).");
		return false;
	}
	c = emaildomain.substring(emaildomain.lastIndexOf(".")+1,emaildomain.length);
	if (c.length < 2 || c.length > 6) {
		alert("The top level domain of the e-mail address was not valid.");
		return false;
	}
	if (emaildomain.charAt(0) == ".") {
		alert("The first character of the host portion of the e-mail address cannot be a dot.");
		return false;
	}
	if (emailbox.charAt(emailbox.length-1) == ".") {
		alert("The last character of the mailbox portion of the e-mail address cannot be a dot.");
		return false;
	}
	return true;
}

function CheckString(Field, FieldName)
{
	if (empty(Field.value)) {
		alert("Please fill in the " + FieldName + " field.");
		Field.focus();
		return false;
	}
	return true;
}

function CheckInteger(Field, FieldName, Required)
{
	if (Required && !CheckString(Field, FieldName))
		return false;
	if (!checkinteger(Field.value)) {
		alert("Please fill in a valid value for " + FieldName + ".");
		Field.focus();
		return false;
	}
	return true;
}

function CheckPercent(Field, FieldName, Required)
{
	if (Required && !CheckString(Field, FieldName))
		return false;
	if (!checknumber(Field.value) || parseFloat(Field.value) < 0 || parseFloat(Field.value) > 100 || Field.value.indexOf(".") != -1 && Field.value.length - Field.value.indexOf(".") > 3) {
		alert ("Please enter a valid amount for " + FieldName + ".\n\n" + "Valid range of is from 1-100.");
		Field.focus();
		return false;
	}
	return true;
}

function CheckValidNumber(Field, FieldName, Required)
{
	if (Required && empty(Field.value)) {
		alert("Please fill in a value for the " + FieldName + ".");
		Field.focus();
		return false;
	}
	if (!checknumber(Field.value) || parseFloat(Field.value) < 0 || Field.value.indexOf(".") != -1 && Field.value.length - Field.value.indexOf(".") > 3) {
		alert("Please enter a valid value for the " + FieldName + ".");
		Field.focus();
		return false;
	}
	return true;
}

function CheckWeight(weight, Required)
{
	if (Required && empty(weight.value)) {
		alert("Please enter the weight of the product.");
		weight.focus();
		return false;
	}
	if (!checknumber(weight.value) || parseFloat(weight.value) < 0 || weight.value.indexOf(".") != -1 && weight.value.length - weight.value.indexOf(".") > 3) {
		alert("Please enter a valid value for the weight.");
		weight.focus();
		return false;
	}
	return true;
}

function CheckPrice(price, Required)
{
	if (Required && empty(price.value)) {
		alert("Please enter the price for the product.");
		price.focus();
		return false;
	}
	if (!checknumber(price.value) || parseFloat(price.value) < 0 || price.value.indexOf(".") != -1 && price.value.length - price.value.indexOf(".") > 3) {
		alert("Please enter a valid price for the product.");
		price.focus();
		return false;
	}
	if (parseFloat(price.value) > MAXIMUM_PRICE) {
		alert("Please enter a smaller value for the price of the product.\n\nMaximum price for a product is $" + MAXIMUM_PRICE + ".00");
		price.focus();
		return false;
	}
	return true;
}

function CheckQuantity(quantity, Required)
{
	if (Required, empty(quantity.value)) {
		alert("Please enter a quantity for the product.");
		quantity.focus();
		return false;
	}
	if (!checkinteger(quantity.value) || parseFloat(quantity.value) < 0) {
		alert("Please enter a valid quantity for the product.");
		quantity.focus();
		return false;
	}
	return true;
}

function Native2Unicode(Native)
{
	var Result = "", i;

	for (i = 0 ; i < Native.length-1; i++)
		Result += Native.charCodeAt(i) + ",";
	Result += Native.charCodeAt(Native.length-1);
	return Result;
}

function getCookie(name)
{
	var cstr = document.cookie;
	var vstr, i;
	while (cstr != "") {
		i = cstr.indexOf(";");
		if (i < 0) {
			vstr = cstr;
			cstr = "";
		} else {
			vstr = trim(cstr.substring(0, i));
			cstr = trim(cstr.substring(i+1, cstr.length));
		}
		i = vstr.indexOf("=");
		if (i >= 0 && trim(vstr.substring(0, i)) == name)
			return unescape(trim(vstr.substring(i+1, vstr.length)));
	}
	return null;
}

function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires.toGMTString() : "") +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");
}

function deleteCookie(name, path, domain)
{
	if (getCookie(name))
		document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
