// from Bill Dotrch public domain cookie script
// edited by Nancy Hastings-Trew
var loc = document.location.hostname;
var hrf= document.location.href;
//alert(loc);
var path = document.location.pathname;
var query = document.location.search;
if (loc.indexOf("farthingales.on.ca")>-1 && hrf.indexOf("www")<0)
{
	if (hrf.indexOf("http://")>-1) document.location.href="http://www.farthingales.on.ca"+path+query;
	else if (hrf.indexOf("https://")>-1) document.location.href="https://www.farthingales.on.ca"+path+query;
}


function SetCookie(name,value) {
	value= value.replace("\\","");
	value = unescape(value);
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (window.location.hostname.indexOf("farthingales") > -1) ? "www.farthingales.on.ca" : null;
	//(argc > 4) ? argv[4] : null; this was the domain argument
	var secure = (argc >5) ? argv[5] : false; // our cookie is not secure.
	// here's where we set the cookie.
	document.cookie = name + "=" + escape(value) + 	
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) + 
	((domain == null) ? "" : ("; domain=" + domain)) +	
	"; version=1;" + 
	((secure == true) ? "; secure" : "");
	//alert(name+"="+value);
}

function saveCookie(theName)
{
	var theValue = document.Order.MyOrder.value;
	SetCookie (theName,theValue);
}

function clearOrder()
{
  if (confirm("Do you REALLY want to empty your Shopping Cart?"))
  {
	 var len = document.Shopcart.elements.length;
	 for (var idx=0; idx < len-4; idx ++)
	 { 
	 	if (document.Shopcart.elements[idx].type == "text" || document.Shopcart.elements[idx].type == "hidden")	
					document.Shopcart.elements[idx].value="";
	 }
	 SetCookie('MyOrder',"");
	 document.Shopcart.submit();
	}
}

function delCookie(idx,badCrumb)
{
	var newOrder = new Array();
	// get the "cookie" which is the current value of text form field
	var mycookie = document.Shopcart.MyOrder.value; 
	// see if there's more than one item in it
	if (mycookie.indexOf("|") > -1) 
	{
		// split up the cookie into the order items
		var crumbs = mycookie.split("|");
		var len = crumbs.length;
		var count = 0; 
		for (x = 0; x < len; x ++) 
		{
			//if (crumbs[x].indexOf(":") > 1)
			//{
				// compare to list 
				var crumb = crumbs[x].split(":"); 
				if(crumb[0] != badCrumb) 
				{
					// make a new array out of the undeleted crumbs
					newOrder[count] = crumbs[x];
					count++;
				}
			//}
		}
		len = newOrder.length;
		if (len > 1) 
		{ 
			// put the cookie back together with new quantity
			var theValue = newOrder.join("|");
		}
		// if there's just one thing left don't join it
		else if(len == 1) {	var theValue = newOrder[0]; }
		else if(len < 1) { var theValue = ""; }
	}
	//if there was only one thing in the cookie to begin with just delete it
	else { theValue = ""; } 
	document.Shopcart.elements[idx].value="";
	document.Shopcart.MyOrder.value=theValue;
	document.Order.MyOrder.value=theValue;
	SetCookie('MyOrder',theValue);
	document.Shopcart.submit();
}

function transferCookie() 
{
	var theName = "MyOrder";
	var theValue = document.Shopcart.MyOrder.value;
	document.Order.MyOrder.value=theValue;
	SetCookie(theName,theValue);
}

function getCookie(theName) {
	var arg = theName + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i,j) == arg) { return getCookieVal(j); }
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}



function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) {		endstr = document.cookie.length;	}
	return unescape(document.cookie.substring(offset,endstr));
}

function goBack(url)
{
	var theValue = document.Shopcart.MyOrder.value; 
	SetCookie('MyOrder',theValue);
	document.location = url;
}

function checkDest()
{
	var tempobj = document.Order.shipping_Country;
	if (!(tempobj[0].checked || tempobj[1].checked))
	{
		alert("Please indicate your Shipping Destination");
	} 
	else document.Order.submit();
}