// from Bill Dortch 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(num,qty,idx) {
		// var bName = "add_" + num;
		var newCrumb = num; 
		var newValue = num + ":" + qty;
		var theName="MyOrder";
		var flag = 0; 
		// see if there's already a cookie
		if  ((document.Order.MyOrder.value != "" && document.Order.MyOrder.value != null)
		  	&& (document.cookie) 
		  	&& (getCookie(theName)))
		{ 
			// get the cookie
			var mycookie =  getCookie(theName); 
			// see if there's more than one item in it
			var match = mycookie.indexOf("|");
			if (match > -1) 
			{
				// split up the cookie into the order numbers
				var crumbs = mycookie.split("|");
				var len = crumbs.length;
				for (x = 0; x < len; x ++) 
				{
					if (crumbs[x].indexOf(":") > -1)
					{
						// see if they've already added that item so don't add again
						var crumb = crumbs[x].split(":"); 
						if(crumb[0] == newCrumb) {flag = 1; break; }
					}
				}
				// if not already added then add it
				if (flag != 1) { 	crumbs[len+1] = newValue;  }
				// put the cookie back together with new item
				var theValue = crumbs.join("|");
			}
			// if there was only one thing in the cookie just add to it
			else if(mycookie != newValue && mycookie != "" ) { var theValue = mycookie + "|" + newValue; }
			// there's a cookie but it's empty
			else if(mycookie == "") { var theValue = newValue; }
		}
		// if there was no cookie to begin with so make one
		else var theValue=newValue;
		var xDate = new Date();
		xDate.setTime(xDate.getTime() +24*60*60*1000); /// expires in a day
		SetCookie(theName,theValue,xDate);
		document.Order.MyOrder.value=theValue;
		document.Order.elements[idx].value="IN CART";
		document.Order.elements[idx].className="button";
}

function getCookie(name) {
	var arg = name + "=";
	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 grabCookie(name)
{
	if (document.Order.MyOrder && document.cookie)
	{
		var theValue = getCookie(name);
		if (theValue != "" && theValue != null) 
		{ 
			document.Order.MyOrder.value=theValue; 
			// split up the cookie into the order numbers
			var crumbs = theValue.split("|");
			var len = crumbs.length;
			var flen = document.Order.elements.length;
			for (i=0; i < flen; i++)
			{
				eName = document.Order.elements[i].name;
				for (x = 0; x < len; x ++) 
				{
					var crumb = crumbs[x].split(":"); 
					var bName = "add_" + crumb[0]; 
					if (bName == eName) 
					{ 
						document.Order.elements[i].value="IN CART";  
						document.Order.elements[i].className="button";	
					}			
				}
			}
		} 
		else document.Order.MyOrder.value="";
	}
}


