/**
*
*
**/

/**
*
*
**/
function si_OnSubmitForm()
{ 
	// Change form action
	document.getElementById("si_storesearch").action = document.getElementById("store_select1").options[document.getElementById("store_select1").options.selectedIndex].value;
	
	// if a city is supplied
	if (document.getElementById("store_select1").options.selectedIndex > 0)
	{
		return true;
	}
	else
	{
		// no city was selected; state/country may or may not have been selected

		//+++ show a red message somewhere
		seterror("* Please select a Store Location");

		return false;
	}
	return true;
}

/**
 * Simple check for existence and length, much room for improvement....
 * @param
 */
function si_validateZip() {
	error = 0;
	if (document.zipsearch.zipcode.value.length < 5) error++;
	if (document.zipsearch.zipcode.value.length > 8) error++;
	
	if (error > 0) {
		seterror('Sorry, that is an invalid zip code.');
		return false;
	}
	return true;
}

/**
 * Strip the domain and directory from the urls as
 * stored in the xml.
 * @param {Object} url
 */
function si_stripDomain(url) {
	url = url.replace(/http:\/\/www.wholefoodsmarket.com/, ""); 
	url = url.replace(/http:\/\/www.wholefoods.com/, ""); 
	url = url.replace(/canada/, "stores"); 
	url = url.replace(/UK/, "stores");
	url = url.replace(/index.html/, "", url);
	return url;
}

/**
 * Initialize routines for body onload().
 */
function initialize() {	
	get_stores('1');
}

/**
 * Function to display an error message if an invalid zip code has been entered, 
 * or if the user has failed to select a store location.
 * @param {Object} str
 */
function geterror(str)
{
	var returnstr = "";

	try
	{
		if (str.indexOf("invalidzip=") > -1)
		{
				// Display the appropriate error message for an invalid zip code.
				theleft   = str.indexOf("invalidzip=") + 11;
				theright  = str.length;
				returnstr = "<br /><p class='errortext'><i>'" + str.substring(theleft, theright) + "' is not a valid ZIP Code. To locate a store, enter a valid ZIP Code, or leave the ZIP Code blank and pick a store using the State/Country and Store Location lists.</i></p>";
		}
		else
		if (str.indexOf("city=") > -1)
		// Display the appropriate error message if the user has failed to select a Store Location.
		{
			theleft   = str.indexOf("city=") + 5;
			theright  = theleft + 9;
			if (str.substring(theleft, theright) == "Select...")
			{
					returnstr = "<p class='errortext'><i>Both State/Country and Store Location must be selected to find a store.</i></p>";
			}
		}
	}
	catch(e)
	{
		returnstr = "";
	}
	finally
	{
		document.getElementById("errormsg").innerHTML = returnstr;
	}
}

/**
 * 
 * @param {Object} str
 */
function seterror(str)
{
			var returnstr = str;//"<p class='errortext'><i>"+str+"</i></p>";
			document.getElementById("zip_error").innerHTML = returnstr;
			document.getElementById("zip_error").style.display = 'block';
			return returnstr;
}

/**
 * A function to return the state/country from the document location string.
 * @param {Object} str
 */
function getstate(str)
{
		var returnstr = "";

		try
		{
			theleft   = str.indexOf("state=") + 6;
			theright  = str.indexOf("city=") - 1;
			returnstr = str.substring(theleft, theright);
			while (returnstr.indexOf("+") > -1)   returnstr = returnstr.replace("+", " ");
			while (returnstr.indexOf("%28") > -1) returnstr = returnstr.replace("%28", "(");
			while (returnstr.indexOf("%29") > -1) returnstr = returnstr.replace("%29", ")");
		}
		catch(e)
		{
			returnstr = "";
		}
		finally
		{
			return returnstr;
		}
	} 