function TryToCreateAccount()
{
	//1.	Ensure fields are populated
	//2.	Check if e-mail address already exists in system via AJAX
	//		This continues in AfterTryToCreateAccount()
	//3.	Submit the form (move onto the next step)
	
	//1.
	if (!IsValidEmail(document.CreateAccountForm.Email.value))
	{
		ShowPopup("Oops", "Please enter a valid e-mail address.");
		FocusOnField(document.CreateAccountForm.Email)
		return false;
	}
	else if (document.CreateAccountForm.Password.value == "" || document.CreateAccountForm.Password.value == "Password" )
	{
		ShowPopup("Oops", "Please enter your new password.");
		FocusOnField(document.CreateAccountForm.Password)
		return false;
	}
	
	//2.
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_check_email.php", "CheckEmail=1&EmailAddress=" + document.CreateAccountForm.Email.value, "AfterTryToCreateAccount"); 
}


function AfterTryToCreateAccount(xml, text)
{
	var ErrorMessage = "";
	
	if (xml.getElementsByTagName('ErrorMessage')[0].childNodes[0])
		ErrorMessage = xml.getElementsByTagName('ErrorMessage')[0].childNodes[0].nodeValue;
	
	if (ErrorMessage.length == 0)
	{
		//3.
		document.CreateAccountForm.submit();
	}
	else
	{
		ShowPopup("Oops", ErrorMessage);
		return false;
	}

}


function TryToLogin()
{
	//1.	Ensure fields are populated
	//2.	Validate login via AJAX
	//		This continues in AfterTryToLogin()
	//3.	Submit the form (move onto the next step)

	//1.
	if (!IsValidEmail(document.LoginForm.Email.value))
	{
		ShowPopup("Oops", "Please enter a valid e-mail address.");
		FocusOnField(document.LoginForm.Email)
		return false;
	}
	else if (document.LoginForm.Password.value == "" || document.LoginForm.Password.value == "Password" )
	{
		ShowPopup("Oops", "Please enter your password.");
		FocusOnField(document.LoginForm.Password)
		return false;
	}

	//2.
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_login.php", "Login=1&EmailAddress=" + document.LoginForm.Email.value + "&Password=" + document.LoginForm.Password.value, "AfterTryToLogin");
}


function AfterTryToLogin(xml, text)
{
	var ErrorMessage = "";
	
	if (xml.getElementsByTagName('ErrorMessage')[0].childNodes[0])
		ErrorMessage = xml.getElementsByTagName('ErrorMessage')[0].childNodes[0].nodeValue;

	if (ErrorMessage.length == 0)
	{
		//3.
		document.LoginForm.submit();
	}
	else
	{
		ShowPopup("Oops", ErrorMessage);
		FocusOnField(document.LoginForm.Password)
		return false;
	}
	
}


function SetShippingAddress(IsChecked)
{
	if (IsChecked)
	{
		//Copy from billing
		document.AddressForm.ShippingCompany.value	= document.AddressForm.BillingCompany.value;
		document.AddressForm.ShippingFirstName.value	= document.AddressForm.BillingFirstName.value;
		document.AddressForm.ShippingLastName.value	= document.AddressForm.BillingLastName.value;
		document.AddressForm.ShippingStreetNumber.value	= document.AddressForm.BillingStreetNumber.value;
		document.AddressForm.ShippingStreetName.value	= document.AddressForm.BillingStreetName.value;
		document.AddressForm.ShippingStreetType.value	= document.AddressForm.BillingStreetType.value;
		document.AddressForm.ShippingUnit.value		= document.AddressForm.BillingUnit.value;
		document.AddressForm.ShippingCity.value		= document.AddressForm.BillingCity.value;
		document.AddressForm.ShippingCountry.value	= document.AddressForm.BillingCountry.value;
		document.AddressForm.ShippingProvince.value	= document.AddressForm.BillingProvince.value;
		document.AddressForm.ShippingPostalCode.value	= document.AddressForm.BillingPostalCode.value;
	}
	else
	{
		//Empty
		document.AddressForm.ShippingCompany.value	= "";
		document.AddressForm.ShippingFirstName.value	= "";
		document.AddressForm.ShippingLastName.value	= "";
		document.AddressForm.ShippingStreetNumber.value	= "";
		document.AddressForm.ShippingStreetName.value	= "";
		document.AddressForm.ShippingStreetType.value	= "";
		document.AddressForm.ShippingUnit.value		= "";
		document.AddressForm.ShippingCity.value		= "";
		document.AddressForm.ShippingCountry.value	= "";
		document.AddressForm.ShippingProvince.value	= "";
		document.AddressForm.ShippingPostalCode.value	= "";
	}
}


function TryToSubmitAddress()
{
	//1.	Ensure mandatory fields are populated
	//2.	Submit
	
	//1.
	if (!IsValidEmail(document.AddressForm.Email.value))
	{
		ShowPopup("Oops", "Please enter a valid e-mail address.");
		FocusOnField(document.AddressForm.Email)
		return false;
	}
	else if (document.AddressForm.Password.value == "")
	{
		ShowPopup("Oops", "Please enter your password.");
		FocusOnField(document.AddressForm.Password)
		return false;
	}
	else if (document.AddressForm.PhoneNumber.value.length < 7)
	{
		ShowPopup("Oops", "Please enter a valid phone number.");
		FocusOnField(document.AddressForm.PhoneNumber)
		return false;
	}
	else if (document.AddressForm.BillingFirstName.value == "")
	{
		ShowPopup("Oops", "Please enter the billing first name.");
		FocusOnField(document.AddressForm.BillingFirstName)
		return false;
	}
	else if (document.AddressForm.BillingLastName.value == "")
	{
		ShowPopup("Oops", "Please enter the billing last name.");
		FocusOnField(document.AddressForm.BillingLastName)
		return false;
	}
	else if (document.AddressForm.BillingStreetNumber.value == "")
	{
		ShowPopup("Oops", "Please enter the billing street number.");
		FocusOnField(document.AddressForm.BillingStreetNumber)
		return false;
	}
	else if (document.AddressForm.BillingStreetName.value == "")
	{
		ShowPopup("Oops", "Please enter the billing street name.");
		FocusOnField(document.AddressForm.BillingStreetName)
		return false;
	}
	else if (document.AddressForm.BillingStreetType.value == "")
	{
		ShowPopup("Oops", "Please choose the billing street type.");
		FocusOnField(document.AddressForm.BillingStreetType)
		return false;
	}
	else if (document.AddressForm.BillingCity.value == "")
	{
		ShowPopup("Oops", "Please enter the billing city.");
		FocusOnField(document.AddressForm.BillingCity)
		return false;
	}
	else if (document.AddressForm.BillingCountry.value == "")
	{
		ShowPopup("Oops", "Please choose the billing country.");
		FocusOnField(document.AddressForm.BillingCountry)
		return false;
	}
	else if (document.AddressForm.BillingProvince.value == "")
	{
		ShowPopup("Oops", "Please choose the billing province/state.");
		FocusOnField(document.AddressForm.BillingProvince)
		return false;
	}
	else if (document.AddressForm.BillingPostalCode.value.length < 5)
	{
		ShowPopup("Oops", "Please enter a valid billing postal/zip code.");
		FocusOnField(document.AddressForm.BillingPostalCode)
		return false;
	}
	
	else if (document.AddressForm.ShippingFirstName.value == "")
	{
		ShowPopup("Oops", "Please enter the shipping first name.");
		FocusOnField(document.AddressForm.ShippingFirstName)
		return false;
	}
	else if (document.AddressForm.ShippingLastName.value == "")
	{
		ShowPopup("Oops", "Please enter the shipping last name.");
		FocusOnField(document.AddressForm.ShippingLastName)
		return false;
	}

	else if (document.AddressForm.ShippingStreetNumber.value == "")
	{
		ShowPopup("Oops", "Please enter the shipping street number.");
		FocusOnField(document.AddressForm.ShippingStreetNumber)
		return false;
	}
	else if (document.AddressForm.ShippingStreetName.value == "")
	{
		ShowPopup("Oops", "Please enter the shipping street name.");
		FocusOnField(document.AddressForm.ShippingStreetName)
		return false;
	}
	else if (document.AddressForm.ShippingStreetType.value == "")
	{
		ShowPopup("Oops", "Please choose the shipping street type.");
		FocusOnField(document.AddressForm.ShippingStreetType)
		return false;
	}
	else if (document.AddressForm.ShippingCity.value == "")
	{
		ShowPopup("Oops", "Please enter the shipping city.");
		FocusOnField(document.AddressForm.ShippingCity)
		return false;
	}
	else if (document.AddressForm.ShippingCountry.value == "")
	{
		ShowPopup("Oops", "Please choose the shipping country.");
		FocusOnField(document.AddressForm.ShippingCountry)
		return false;
	}
	else if (document.AddressForm.ShippingProvince.value == "")
	{
		ShowPopup("Oops", "Please choose the shipping province/state.");
		FocusOnField(document.AddressForm.ShippingProvince)
		return false;
	}
	else if (document.AddressForm.ShippingPostalCode.value.length < 5)
	{
		ShowPopup("Oops", "Please enter a valid shipping postal/zip code.");
		FocusOnField(document.AddressForm.ShippingPostalCode)
		return false;
	}
	//2.
	document.AddressForm.submit();
}


function TryToSubmitShippingMethod()
{
	//1.	Ensure a shipping method is chosen
	//2.	Set cost and delivery date
	//3.	Submit form
	
	//1.
	var RadioSelectedValue = GetRadioSelectedValue(document.ShippingMethodForm.ShippingType);
	
	if (RadioSelectedValue == "")
	{
		ShowPopup("Oops", "Please choose a shipping method.");
		FocusOnField(document.ShippingMethodForm.ShippingType)
		return false;
	}

	
	//2.
	var RadioSelectedPrice;
	
	for(var i = 0; i < document.ShippingMethodForm.ShippingType.length; i++)
	{
		if(document.ShippingMethodForm.ShippingType[i].checked)
		{
			RadioSelectedPrice = document.ShippingMethodForm.ShippingType[i].getAttribute('price');
			RadioSelectedArrivalDate = document.ShippingMethodForm.ShippingType[i].getAttribute('date');
			continue;
		}
	}

	document.ShippingMethodForm.ShippingCost.value = RadioSelectedPrice;
	document.ShippingMethodForm.ShippingArrivalDate.value = RadioSelectedArrivalDate;

	//3.
	document.ShippingMethodForm.submit();
}


function TryToAddToCart()
{
	//1.	If there are dimensions 1, ensure it is selected
	//2.	If there are dimensions 2, ensure it is selected
	//3.	Ensure a quantity is selected
	//4.	If a second dimension exists, get the pd_id from it.  Otherwise if a first dimension exists, get it from the first.  Otherwise, get it from the hidden field.
	//5.	Try to add to cart (pending quantity) via AJAX

	//Get values of drop downs (extra code for IE)
	var Dimension1Value = "";
	var Dimension2Value = "";

	if (document.getElementById('Dimension1'))
	{
		Dimension1Value = document.getElementById('Dimension1').value;
		if (Dimension1Value == "")
			Dimension1Value = document.getElementById('Dimension1').options[document.getElementById('Dimension1').selectedIndex].innerHTML
	}

	if (document.getElementById('Dimension2'))
	{
		Dimension2Value = document.getElementById('Dimension2').value;
		if (Dimension2Value == "")
			Dimension2Value = document.getElementById('Dimension2').options[document.getElementById('Dimension2').selectedIndex].innerHTML
	}


	//1.
	if (document.getElementById('Dimension1') && Dimension1Value == "")
	{
		ShowPopup("Oops", "Please choose a " + document.getElementById('Dimension1').getAttribute("DimensionName"))
		return;
	}

	//2.
	if (document.getElementById('Dimension2') && Dimension2Value == "")
	{
		ShowPopup("Oops", "Please choose a " + document.getElementById('Dimension2').getAttribute("DimensionName"))
		return;
	}

	//3.
	if (document.getElementById('Quantity').value == "")
	{
		ShowPopup("Oops", "Please choose a quantity.")
		return;
	}

	//4.
	var DimensionID = "";

	if (document.getElementById('Dimension2'))
		DimensionID = document.getElementById('Dimension2').options[document.getElementById('Dimension2').selectedIndex].getAttribute("DimId");
	else if  (document.getElementById('Dimension1'))
		DimensionID = document.getElementById('Dimension1').options[document.getElementById('Dimension1').selectedIndex].getAttribute("DimId");
	else
		DimensionID = document.getElementById('FirstProductDimensionID').value;

	//5.
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_add_to_cart.php", "AddToCart=1&pd_id=" + DimensionID + "&Quantity=" + document.getElementById('Quantity').value, "AfterTryToAddToCart");

	return false;
}


function AfterTryToAddToCart(xml, text)
{
	//1.	Update cart count
	//2.	Show error/success message

	var ErrorMessage			= "";
	var NumberOfProductsInCart		= 0;

	if (xml.getElementsByTagName('ErrorMessage')[0].childNodes[0])
		ErrorMessage = xml.getElementsByTagName('ErrorMessage')[0].childNodes[0].nodeValue;

	if (xml.getElementsByTagName('NumberOfProductsInCart')[0].childNodes[0])
		NumberOfProductsInCart = xml.getElementsByTagName('NumberOfProductsInCart')[0].childNodes[0].nodeValue;

	//1.
	document.getElementById('itm_num').innerHTML = NumberOfProductsInCart;

	//2.
	if (ErrorMessage.length > 0)
		ShowPopup("Oops", ErrorMessage)
	else
	{
		//ShowPopup("Success!", "Item added to cart.")

		options = { to: "#cart", className: "ui-effects-transfer" };
		//$("#ProductImage").effect("transfer", options, 500, callbackFunctionName );
		$("#ProductImage").effect("transfer", options, 500);
	}
}


function GetDimension2(ProductID, Dimension1Value)
{
	//1.	Ensure there is a second dimension before proceeding
	//2.	Empty the dimension2 drop down (if it exists)
	//3.	Get the new drop down values via AJAX

	//1.
	if (!document.getElementById('Dimension2'))
		return;

	//2.
	document.getElementById('Dimension2').options.length = 0;

	var NewOption		= document.createElement('option');
	NewOption.value		= "";
	NewOption.text		= "--";
	NewOption.innerHTML	= "--";		//For IE
	document.getElementById('Dimension2').appendChild(NewOption);

	//3.
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_get_dimension2.php", "GetDimension2=1&ProductID=" + ProductID + "&Dimension1Value=" + Dimension1Value, "AfterGetDimension2");
}


function ChangeDimension1(MyElement, ProductID)
{
	//1.	Set the proper image (get value from the element passed in)
	//		if a second drop down exists
	//2.		Blank out the price (it will be retrieved via AJAX)
	//3.		Blank out second drop-down
	//4.		Add a blank option
	//5.		Get second drop-down options (and add a blank one)
	//		else
	//			Set the proper price (get from the element)


	//1.
	document.getElementById('ProductImage').src			= MyElement.getAttribute('DimImage');

	if (document.getElementById('Dimension2'))
	{
		//2.
		document.getElementById('ProductPrice').innerHTML	= "-";

		//3.
		document.getElementById('Dimension2').length		= 0;

		//4.
		var NewOption		= document.createElement('option');
		NewOption.value		= "";
		NewOption.text		= "--";
		NewOption.innerHTML	= "--";		//For IE
		document.getElementById('Dimension2').appendChild(NewOption);

		//5.
		var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_get_dimension2.php", "GetDimension2=1&ProductID=" + ProductID + "&Dimension1Value=" + MyElement.getAttribute('DimValue'), "AfterChangeDimension1");
	}
	else
		document.getElementById('ProductPrice').innerHTML		= MyElement.getAttribute('DimPrice');
}


function AfterChangeDimension1(xml, text)
{
	for (i = 0; i < xml.getElementsByTagName("Options")[0].childNodes.length; i++)
	{
		var NewOption		= document.createElement('option');
		NewOption.value		= xml.getElementsByTagName("Options")[0].childNodes[i].attributes.getNamedItem('pd_dimension_value2').value;
		NewOption.text		= xml.getElementsByTagName("Options")[0].childNodes[i].attributes.getNamedItem('pd_dimension_value2').value;
		NewOption.innerHTML	= xml.getElementsByTagName("Options")[0].childNodes[i].attributes.getNamedItem('pd_dimension_value2').value;		//For IE

		NewOption.setAttribute('DimId', xml.getElementsByTagName("Options")[0].childNodes[i].attributes.getNamedItem('pd_id').value);
		NewOption.setAttribute('DimValue', xml.getElementsByTagName("Options")[0].childNodes[i].attributes.getNamedItem('pd_dimension_value2').value);
		NewOption.setAttribute('DimImage', xml.getElementsByTagName("Options")[0].childNodes[i].attributes.getNamedItem('pd_image_path').value);
		NewOption.setAttribute('DimPrice', xml.getElementsByTagName("Options")[0].childNodes[i].attributes.getNamedItem('pd_price').value);

		document.getElementById('Dimension2').appendChild(NewOption);
	}
}


function ChangeDimension2(MyElement)
{
	//1.	Set the proper image (get value from the element passed in)
	//1a.	Change the zoomed in image - this is done in jquery.jqzoom1.0.1.js line 814
	//1b.	Change the source for the 'click to zoom'
	//2.	Set the proper price (get value from the element passed in)

	//1.
	document.getElementById('ProductImage').src			= MyElement.getAttribute('DimImage');

	//1b.
	document.getElementById('JamieLightbox2').href			= MyElement.getAttribute('DimImage');

	//2.
	document.getElementById('ProductPrice').innerHTML		= MyElement.getAttribute('DimPrice');
}


var SubmittedVote = false;

function ShowStars(Number)
{
	//1.	If they voted, disable mouse over
	//2.	Unselect all
	//3.	Select chosen

	//1.
	if (SubmittedVote)
		return;

	//2.
	document.getElementById("Star5").src = "/~hustler/images/Hustler/Icon-StarOff.png";
	document.getElementById("Star4").src = "/~hustler/images/Hustler/Icon-StarOff.png";
	document.getElementById("Star3").src = "/~hustler/images/Hustler/Icon-StarOff.png";
	document.getElementById("Star2").src = "/~hustler/images/Hustler/Icon-StarOff.png";
	document.getElementById("Star1").src = "/~hustler/images/Hustler/Icon-StarOff.png";

	//3.
	switch(Number)
	{
		case 5:	document.getElementById("Star5").src = "/~hustler/images/Hustler/Icon-StarOn.png";
		case 4:	document.getElementById("Star4").src = "/~hustler/images/Hustler/Icon-StarOn.png";
		case 3:	document.getElementById("Star3").src = "/~hustler/images/Hustler/Icon-StarOn.png";
		case 2:	document.getElementById("Star2").src = "/~hustler/images/Hustler/Icon-StarOn.png";
		case 1:	document.getElementById("Star1").src = "/~hustler/images/Hustler/Icon-StarOn.png";
		break;
	}
}


function SubmitRating(Rating, ProductID)
{
	SubmittedVote = true;

	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_submit_rating.php", "p_id="+ProductID+"&Rating=" + Rating, "AfterSubmitRating");
}


function AfterSubmitRating(xml, text)
{
	document.getElementById('ScoreWrapper').innerHTML = "Vote submitted!";
}


function RemoveProduct(ProductID, ProductName)
{
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_remove_product.php", "RemoveProduct=1&ProductID=" + ProductID, "AfterRemoveProduct");
}


function AfterRemoveProduct(xml, text)
{
	window.location.reload();
}


function UpdateProductQuantity(ProductID)
{
	//They've changed a quantity of items in their cart
	//If they entered an integer, submit quantities via AJAX and update totals.

	if (IsNumeric(document.getElementById('Quantity' + ProductID).value))
	{
		var Parameters = "&ProductID=" + ProductID + "&Quantity=" + document.getElementById('Quantity' + ProductID).value;
		var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_update_product_quantity.php", "UpdateProductQuantity=1" + Parameters, "AfterUpdateProductQuantity");
	}
}


function AfterUpdateProductQuantity(xml, text)
{
	//The cart has been updated and the new prices have been returned in XML.  An error may have been returned, too(tried to add more products than are available in stock)

	//1.	Update screen data
	//2.	Show error, if there is one

	//1.
	UpdateTotalsFromXml(xml);

	//2.
	var ErrorMessage = "";
	
	if (xml.getElementsByTagName('ErrorMessage')[0].childNodes[0])
		ErrorMessage = xml.getElementsByTagName('ErrorMessage')[0].childNodes[0].nodeValue;

	if (ErrorMessage.length > 0)
		ShowPopup("Oops", ErrorMessage);
}

var timeoutID;

function GetCouponValue(Coupon)
{
	clearTimeout(timeoutID);

	timeoutID = setTimeout("GetCouponValueViaAjax('"+Coupon+"')", 800);
}


function GetCouponValueViaAjax(Coupon)
{
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_set_coupon_value.php", "SetCouponValue=1&CouponCode=" + Coupon, "AfterGetCouponValueViaAjax");
}


function AfterGetCouponValueViaAjax(xml, text)
{
	UpdateTotalsFromXml(xml);
}


function SetGiftWrapping(GiftWrap)
{
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_set_gift_wrapping.php", "SetGiftWrapping=1&GiftWrap=" + GiftWrap, "AfterSetGiftWrapping");
}


function AfterSetGiftWrapping(xml, text)
{
	UpdateTotalsFromXml(xml);
}


function UpdateTotalsFromXml(xml)
{
	document.getElementById('SubTotalValue').innerHTML	= "$" + xml.getElementsByTagName('SubTotal')[0].childNodes[0].nodeValue;
	
	if (document.getElementById('GiftWrapValue'))
		document.getElementById('GiftWrapValue').innerHTML	= "$" + xml.getElementsByTagName('GiftWrapCost')[0].childNodes[0].nodeValue;
		
	document.getElementById('CouponsValue').innerHTML	= "$" + xml.getElementsByTagName('CouponValue')[0].childNodes[0].nodeValue;
	document.getElementById('TaxValue').innerHTML		= "$" + xml.getElementsByTagName('Taxes')[0].childNodes[0].nodeValue;
	document.getElementById('itm_num').innerHTML		= xml.getElementsByTagName('NumberOfItemsInCart')[0].childNodes[0].nodeValue;
}

