function trim(string)
{
	string=string.replace(/^\s+|\s+$/g,"").replace(/\s+/g," ");
	return string;
}

Array.prototype.in_array = function(searchvalue)
{
	for(var i = 0, l = this.length; i < l; i++)
	{
		if(this[i] == searchvalue)
		{
			return true;
		}
	}
	return false;
}

function isAlphabetic(string)
{
	if (string.match(/^[^0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}

function isAlphaNumeric(string)
{
	if (string.match(/^[a-zA-Z0-9_\-]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}

function isEmailAddress(string)
{
	if (string.match(/^([a-zA-Z0-9_\.\-])+(\@)+([a-zA-Z0-9_\.\-])+(\.)+([a-zA-Z0-9]{2,4})+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}

function isAddress(string)
{
	return true;
	/*if (string.match(/^[a-zA-Z�-��-��-�0-9 _\-\'\`\.\,\#\&]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} */
}

function isNumeric(string)
{
	if (string.match(/^[0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}

function isPhone(string)
{
	if (string.match(/^[0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isPrice(string)
{
	if (string.match(/^[0-9]+(.[0-9][0-9])?$/) || string.match(/^[0-9]+(.[0-9])?$/) || string.match(/^[0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}



function validate_fullname(string)
{
	string = trim(string);
	var error = false;
	
	if (string == "") error = true;
	if (isAlphabetic(string) == false) error = true;
	if (string.split(" ").length < 2) error = true;
	
	if (error == true) { return "Please enter a valid <u>full</u> name!"; } else { return "__ok"; }
}

function validate_username(string)
{
	string = trim(string);
	var error = false;
	
	if (string == "") error = true;
	if (isAlphaNumeric(string) == false) error = true;
	if (string.length < 6) error = true;
	
	if (error == true) { return "Your user name must consist of 6 to 30 consecutive numbers and/or letters!"; } else { return "__ok"; }
}

function validate_password(string, blankok)
{
	//set default value
	if (typeof blankok == "undefined")
	{
		blankok = false;
	}
	
	var error = false;
	
	if (blankok == false)
	{
		if (string.length == 0) error = true;
	}
	if (string.length > 0 && string.length < 6) error = true;

	if (error == true) { return "Your password must be at least 6 characters long!"; } else { return "__ok"; }
}

function validate_emailaddress(string, blankok)
{
	//set default value
	if (typeof blankok == "undefined")
	{
		blankok = false;
	}
	
	string = trim(string);
	var error = false;
	
	if (blankok == false)
	{
		if (string.length == 0) error = true;
	}
	if (isEmailAddress(string) == false && string.length > 0) error = true;
	
	if (error == true) { return "Please enter a valid e-mail address!"; } else { return "__ok"; }
}

function validate_name(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isAlphabetic(string) == false) error = true;
	
	if (error == true) { return "Please enter a valid name!"; } else { return "__ok"; }
}

function validate_address(string, blankok)
{
	//set default value
	if (typeof blankok == "undefined")
	{
		blankok = false;
	}
	
	string = trim(string);
	var error = false;
	
	if (blankok == false)
	{
		if (string.length == 0) error = true;
	}
	if (isAddress(string) == false && string.length > 0) error = true;
	
	if (error == true) { return "Please enter a valid address!"; } else { return "__ok"; }
}

function validate_city(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isAlphabetic(string) == false) error = true;
	
	if (error == true) { return "Please enter a valid city name!"; } else { return "__ok"; }
}

function validate_state(string)
{
	string = trim(string);
	var states = new Array("AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "ME", "MD", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WI", "WV", "WY");
	var error = false;
	
	if (! states.in_array(string)) error = true;
	
	if (error == true) { return "Please select a U.S. state!"; } else { return "__ok"; }
}

function validate_zip(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length != 5) error = true;
	if (isNumeric(string) == false) error = true;
	
	if (error == true) { return "Please enter a valid zip code!"; } else { return "__ok"; }
}

function validate_phone(string, blankok)
{
	//set default value
	if (typeof blankok == "undefined")
	{
		blankok = false;
	}
	
	string = trim(string);
	var error = false;
	
	if (blankok == false)
	{
		if (string.length == 0) error = true;
	}
	if (isPhone(string) == false && string.length > 0) error = true;
	if (string.length != 10 && string.length != 0) error = true;
	
	if (error == true) { return "Please enter a valid 10-digit phone number!"; } else { return "__ok"; }
}

function validate_keywords(string)
{
	string = trim(string);
	var error = false;
	
	if (string.split(",").length > 3) error = true;
	if (string.length == 0) error = false; //workaround
	
	if (error == true) { return "Please enter no more than 3 keywords, separated by commas!"; } else { return "__ok"; }
}

function validate_anything(string, output)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	
	if (error == true) { return "Please enter a "+output+"!"; } else { return "__ok"; }
}

function validate_quantity(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isNumeric(string) == false) error = true;
	
	if (error == true) { return "Please enter a whole number quantity!"; } else { return "__ok"; }
}

function validate_weight(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isPrice(string) == false) error = true;
	while (string.substr(0, 1) == "0" && string.substr(1, 1) != ".")
	{
		string = string.substr(1);	
	}
	if (Math.round(parseFloat(string)*100)/100 == 0) error = true;
	
	if (error == true) { return "Please enter a valid number of pounds (lbs)!"; } else { return "__ok"; }
}

function validate_weight_zero_ok(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isPrice(string) == false) error = true;
	while (string.substr(0, 1) == "0" && string.substr(1, 1) != ".")
	{
		string = string.substr(1);	
	}
	
	if (error == true) { return "Please enter a valid number of pounds (lbs)!"; } else { return "__ok"; }
}

function validate_price(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isPrice(string) == false) error = true;
	while (string.substr(0, 1) == "0" && string.substr(1, 1) != ".")
	{
		string = string.substr(1);	
	}
	if (Math.round(parseFloat(string)*100)/100 == 0) error = true;
	
	if (error == true) { return "Please enter a valid dollar amount without commas or a currency symbol!"; } else { return "__ok"; }
}

function validate_fee(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isPrice(string) == false) error = true;
	while (string.substr(0, 1) == "0" && string.substr(1, 1) != ".")
	{
		string = string.substr(1);	
	}
	if (string > 0 && string < 1) error = true;
	if (string > 100) error = true;
	
	if (error == true) { return "Please enter a valid trasaction fee of 0% or 1.00% and above!"; } else { return "__ok"; }
}

function validate_shipping(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (isPrice(string) == false) error = true;
	while (string.substr(0, 1) == "0" && string.substr(1, 1) != ".")
	{
		string = string.substr(1);	
	}
	if (Math.round(parseFloat(string)*100)/100 == 0) error = true;
	
	if (error == true) { return "Please enter a valid shipping price without commas or a currency symbol!"; } else { return "__ok"; }
}

function validate_category(string)
{
	string = trim(string);
	var error = false;
	
	if (string.length == 0) error = true;
	if (string == "-1") error = true;
	
	if (error == true) { return "Please select a category for this item!"; } else { return "__ok"; }
}