// JavaScript Document

	function PutBasket(CLIENTID,WINEID,WINE,Price) {
		//alert("HERE");
		//document.getElementById('LaunchNotice').style.display='none';
		window.location.href = "#topofpage";
		document.trolleyicon.src="/shop/images/box_header_cart_flicker.gif"
		ob_post.AddParam('CLIENTID', CLIENTID);
		ob_post.AddParam('WINEID', WINEID);
		ob_post.AddParam('WINE', WINE);
		ob_post.AddParam('Price', Price);
		ob_post.AddParam('Special', 0);
		ob_post.post("/shop/Basket.aspx", "PutBasket", UpdateBasket);
	}
	
	function PutBasketInListing(CLIENTID,WINEID,WINE,Price) {
		//var msg = "a case of " + WINE + " has been placed in your cart.\n to see your cart please go to the shop (buy)"
		ob_post.AddParam('CLIENTID', CLIENTID);
		ob_post.AddParam('WINEID', WINEID);
		ob_post.AddParam('WINE', WINE);
		ob_post.AddParam('Price', Price);
		ob_post.AddParam('Special', 0);
		ob_post.post("/shop/Basket.aspx", "PutBasket", UpdateBasket);
		//alert(msg);
		window.location.href = "/Shop/Default.aspx";
	}
	
	
	function PutBasketSpecial(CLIENTID,WINEID,WINE,Price) {
		//alert("HERE");
		window.location.href = "#topofpage";
		document.trolleyicon.src="/shop/images/box_header_cart_flicker.gif"
		ob_post.AddParam('CLIENTID', CLIENTID);
		ob_post.AddParam('WINEID', WINEID);
		ob_post.AddParam('WINE', WINE);
		ob_post.AddParam('Price', Price);
		ob_post.AddParam('Special', 1);
		ob_post.post("Basket.aspx", "PutBasket", UpdateBasket);
	}
	
	function EmptyBasket(BASKETID) {
		ob_post.AddParam('BASKETID', BASKETID);
		ob_post.post("Basket.aspx", "EmptyBasket", UpdateBasket );
	}
					
	function UpdateBasket() {
		// explicit update customer orders details panel
		ob_post.UpdatePanelFromPage("basketpanel", "basketcontainer", "/shop/Basket.aspx");
	}
	
// Wine detail page - Tab navigation

	function GetWineTab_NonMember() {
			ob_post.UpdatePanelFromPage("loading", "tab_content", "Loading.aspx");
			ob_post.UpdatePanelFromPage("nonmember", "tab_content", "/shop/includes/rate_wine_nonmember.aspx"); 
		}

	function GetWineTab_Ratings(WINEID,RatingType) {
			ob_post.UpdatePanelFromPage("loading", "tab_content", "Loading.aspx");
			ob_post.UpdatePanelFromPage("wine_ratenow", "tab_content", "/member/rate_wine_" + RatingType + ".aspx?WINEID=" + WINEID); 
		}
	
	function GetWineTab_MemRatings(WINEID) {
			ob_post.UpdatePanelFromPage("loading", "tab_content", "Loading.aspx");
			ob_post.UpdatePanelFromPage("wine_ratings", "tab_content", "/shop/includes/wine_ratings.aspx?WINEID=" + WINEID); 
		}
	
	function GetWineTab_Analysis(WINEID) {
			ob_post.UpdatePanelFromPage("loading", "tab_content", "Loading.aspx");
			ob_post.UpdatePanelFromPage("wine_analysis", "tab_content", "/Shop/includes/technical_analysis.aspx?WINEID=" + WINEID); 
		}
	
	function GetWineTab_Producer(CLIENTID) {
			ob_post.UpdatePanelFromPage("loading", "tab_content", "Loading.aspx");
			ob_post.UpdatePanelFromPage("producer_info", "tab_content", "/Shop/includes/producer_info.aspx?CLIENTID=" + CLIENTID); 
		}
	
	function GetWineTab_Wines(CLIENTID,WINEID) {
			ob_post.UpdatePanelFromPage("loading", "tab_content", "Loading.aspx");
			ob_post.UpdatePanelFromPage("producer_wines", "tab_content", "/Shop/includes/producer_wines.aspx?CLIENTID=" + CLIENTID + "&WINEID=" + WINEID); 
		}

// Wine rating system

	var WineRatingStar = 0;
	
	function PutWineRatingPoint(WINEID,RatingType) {
		
		var A = document.getElementById("txt_Appearance").value;
		var N = document.getElementById("txt_Nose").value;
		var P = document.getElementById("txt_Palate").value;
		var PointRating = (A * 1) + (N * 1) + (P * 1);
		
		if (document.getElementById("txt_Appearance").value == "") {
					alert("Please select an 'appearance' rating");
					return false;
		  }
		if (document.getElementById("txt_Appearance").value > 3) {
					alert("Maximum 'appearance' rating is 3");
					return false;
		  }
		if (document.getElementById("txt_Nose").value == "") {
					alert("Please select a 'nose' rating");
					return false;
		  }
		if (document.getElementById("txt_Nose").value > 7) {
					alert("Maximum 'nose' rating is 7");
					return false;
		  }
		if (document.getElementById("txt_Palate").value == "") {
					alert("Please select an 'palate' rating");
					return false;
		  }
		if (document.getElementById("txt_Palate").value > 10) {
					alert("Maximum 'palate' rating is 10");
					return false;
		  }
		if (GetRadioButtonValue("txt_Value") != "Bargain" && GetRadioButtonValue("txt_Value") != "Fair" && GetRadioButtonValue("txt_Value") != "Expensive") {
					document.getElementById("txt_Value").focus();
					alert("Please select a wine value");
					return false;
		  }
		ob_post.AddParam('WINEID', WINEID);
		ob_post.AddParam( 'Appearance', A );
		ob_post.AddParam( 'Nose', N );
		ob_post.AddParam( 'Palate', P );
		ob_post.AddParam( 'PointRating', PointRating );
		ob_post.AddParam('Review', document.getElementById("ReviewEditor").value);
		ob_post.AddParam('RatingValue', GetRadioButtonValue("txt_Value"));
		ob_post.post("/member/rate_wine_Point.aspx", "PutWineRatingPoint");
		//ob_post.post("/member/rate_wine_update.aspx?WINEID=" + WINEID , "GetRating");
		
		GetWineTab_MemRatings(WINEID)
	}

	
	function PutWineRatingStar(WINEID,RatingType) {

		if (WineRatingStar == 0) {
					alert("Please select a star rating");
					return false;
		  }
		if (GetRadioButtonValue("txt_Value") != "Bargain" && GetRadioButtonValue("txt_Value") != "Fair" && GetRadioButtonValue("txt_Value") != "Expensive") {
					document.getElementById("txt_Value").focus();
					alert("Please select a wine value");
					return false;
		  }
		
		ob_post.AddParam('WINEID', WINEID);
		ob_post.AddParam( 'StarRating', WineRatingStar );
		ob_post.AddParam('Review', document.getElementById("ReviewEditor").value);
		ob_post.AddParam('RatingValue', GetRadioButtonValue("txt_Value"));
		ob_post.post("/member/rate_wine_Star.aspx", "PutWineRatingStar");
		
		GetWineTab_MemRatings(WINEID)

}

	function star_over(num)
	{
		for(var i=1;i<=num;i++)
			if (document.getElementById("star"+i).src.indexOf("starRed")<0)
				if (i<=num)
					document.getElementById("star"+i).src = "/Shop/images/starGold.gif";
				else
					document.getElementById("star"+i).src = "/Shop/images/starWhite.gif";
	}
	
	function star_out()
	{
		if (document.getElementById("star1").src.indexOf("starRed")<0)
			document.getElementById("star1").src = "/Shop/images/starGold.gif";
		for(var i=2;i<=5;i++)
			if (document.getElementById("star"+i).src.indexOf("starRed")<0)
				document.getElementById("star"+i).src = "/Shop/images/starWhite.gif";
	}
	
	function star_click(num)
	{
		for(var i=1;i<=5;i++)
			if (i<=num)
				document.getElementById("star"+i).src = "/Shop/images/starRed.gif";
			else
				document.getElementById("star"+i).src = "/Shop/images/starWhite.gif";
				
			WineRatingStar = num;
	}

// Wine rating system end

	function GetRadioButtonValue(id)  {
				var radio = document.getElementsByName(id);
				for (var j = 0; j < radio.length; j++)
				{
				   if (radio[j].checked)
						return (radio[j].value);
				}
		 }
	
	function isValidEmail(email, required) {
		if (required==undefined) {   // if not specified, assume it's required
			required=true;
		}
		if (email==null) {
			if (required) {
				return false;
			}
			return true;
		}
		if (email.length==0) {  
			if (required) {
				return false;
			}
			return true;
		}
		if (! allValidChars(email)) {  // check to make sure all characters are valid
			return false;
		}
		if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
			return false;
		} else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
			return false;
		} else if (email.indexOf("@") == email.length) {  // @ must not be the last character
			return false;
		} else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		return false;
		} else if (email.indexOf(".") == email.length) {  // . must not be the last character
		return false;
		}
		return true;
	}
	
	function allValidChars(email) {
	  var parsed = true;
	  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	  for (var i=0; i < email.length; i++) {
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
		  continue;
		parsed = false;
		break;
	  }
	  return parsed;
	}



