//ShopFitter Functions Include
//Check for Referrer (entire url after the ? character
var sLocation;
var iPos;

sLocation = window.location.href;
iPos = sLocation.indexOf("?");
if(iPos != -1) SetCookie("Referrer", sLocation.substring(iPos + 1, sLocation.length));

// Product Size/Chest
// Product Finish/Colour/Style/Clan Name/Tartan
// Product Sleeve
// Product Weight

function AddToCart(iQuantity, sProductID, sProductName, sOption1, sOption2, sOption3, sOptionWeight, sOptionBand, cPrice, sOneOff, sTaxExempt) {
	var iPos;
	var sDisplay;
	var bAdded;
	var iRecordIndex;
	var iRecordCount;

	//Force data types
	iQuantity = parseInt(iQuantity);
	cPrice = parseFloat(cPrice);

	if(GetCookie("iRecordCount") == "") SetCookie("iRecordCount", "0");
	iRecordCount = parseInt(GetCookie("iRecordCount"));
	bAdded = false;

	sDisplay = sProductName + " ";
	if((sOption1 != "")||(sOption2 != "")||(sOption3 != "")) {
		sDisplay += "(";
		if(sOption1 != "") { sDisplay += sOption1; }
		if(sOption2 != "") { 
			if(sOption1 != "") {
				sDisplay += "/"; 
			}
			sDisplay += sOption2; 
		}
		if(sOption3 != "") { 
			if((sOption1 != "")||(sOption2 != "")) {
				sDisplay += "/"; 
			}
				sDisplay += sOption3; 
		}
		sDisplay += ")";
	}

	//Check to see if modifying existing record
	for(iPos = 0; iPos < iRecordCount; iPos++) {
		if(GetCookieItem("sDisplay", iPos) == sDisplay) {
			// Add to existing record
			if(GetCookieItem("sOneOff", iPos) != "True") {
				SetCookieItem("iQuantity", iPos, parseInt(GetCookieItem("iQuantity", iPos)) + iQuantity);
			}

			iRecordIndex = iPos;
			
			bAdded = true;
			break;
		}
	}

	//If no record to modify then add new one
	if(GetCookieItem("sOneOff", iRecordIndex) == "True") {
		alert(sProductName)
	} else {
		if(!bAdded) {
			//Add new record
			SetCookieItem("sDisplay", iRecordCount, sDisplay);
			SetCookieItem("iQuantity", iRecordCount, iQuantity);
			SetCookieItem("sProductID", iRecordCount, sProductID);
			SetCookieItem("sProductName", iRecordCount, sProductName);
			SetCookieItem("sOption1", iRecordCount, sOption1);
			SetCookieItem("sOption2", iRecordCount, sOption2);
			SetCookieItem("sOption3", iRecordCount, sOption3);
			SetCookieItem("sOptionWeight", iRecordCount, sOptionWeight);
			SetCookieItem("cPrice", iRecordCount, cPrice);
			SetCookieItem("sOneOff", iRecordCount, sOneOff);
			SetCookieItem("sTaxExempt", iRecordCount, sTaxExempt);
	
			iRecordIndex = iRecordCount;
			iRecordCount++;
		}
	
		//Check that iQuantity > 0
		if(parseInt(GetCookieItem("iQuantity", iRecordIndex)) < 1) {
			for(iPos = iRecordIndex; iPos < (iRecordCount - 1); iPos++) {
				SetCookieItem("sDisplay", iPos, GetCookieItem("sDisplay", iPos +1));
				SetCookieItem("iQuantity", iPos, GetCookieItem("iQuantity", iPos +1));
				SetCookieItem("sProductID", iPos, GetCookieItem("sProductID", iPos +1));
				SetCookieItem("sProductName", iPos, GetCookieItem("sProductName", iPos +1));
				SetCookieItem("sOption1", iPos, GetCookieItem("sOption1", iPos +1));
				SetCookieItem("sOption2", iPos, GetCookieItem("sOption2", iPos +1));
				SetCookieItem("sOption3", iPos, GetCookieItem("sOption3", iPos +1));
				SetCookieItem("sOptionWeight", iPos, GetCookieItem("sOptionWeight", iPos +1));
				SetCookieItem("cPrice", iPos, GetCookieItem("cPrice", iPos +1));
				SetCookieItem("sOneOff", iPos, GetCookieItem("sOneOff", iPos +1));
				SetCookieItem("sTaxExempt", iPos, GetCookieItem("sTaxExempt", iPos +1));
			}
			iRecordCount--;
		}
	
		//Persist the record count
		SetCookie("iRecordCount", iRecordCount);
		
	}
	// set page Basket
	viewBasket();
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value); +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

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 "";
}

//Internal, Do not call (used by GetCookie)
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function SetCookieItem (name, index, value) {
	var sArray;
	var sCookie = "";
	var iPos;
	var sDel = "";

	sArray = splitIntoArray(GetCookie(name), ";");
	sArray[index] = EscapeColons(value);
	//recombine to array into ; delimeted string
	for(iPos=0; iPos < sArray.length; iPos++) {
		sCookie = sCookie + sDel + sArray[iPos];
		sDel = ";";
	}
	SetCookie(name, sCookie);
}

function GetCookieItem (name, index) {
	var sArray;

	sArray = splitIntoArray(GetCookie(name), ";");
	return(UnescapeColons(sArray[index] + ""));
}

//Escapes semi colon to :s and colon to :c
function EscapeColons (sIn) {
	var sOut = "";
	var iPos = 0;
	var sCompare;

	sIn += "";
	while (iPos < sIn.length) {
		sCompare = sIn.charAt(iPos);
		switch(sCompare) {
			case ";":
				sOut += ":s";
				break;
			case ":":
				sOut += ":c";
				break;
			default:
				sOut += sCompare
		}
		iPos++;
	}
	return(sOut);
}

function UnescapeColons (sIn) {
	var sOut = "";
	var iPos = 0;

	sIn += "";
	while (iPos < sIn.length) {
		switch(sIn.substring(iPos, iPos + 2)) {
			case ":s":
				sOut += ";";
				iPos++;
				break;
			case ":c":
				sOut += ":";
				iPos++;
				break;
			default:
				sOut += sIn.charAt(iPos);
		}
		iPos++;
	}
	return(sOut);
}

function splitIntoArray(sString, sDelimeter) {
	var aResult;
	var s = "";
	var iIndex = 0;
	var iPos = 0
	var iEnd;

	aResult = new Array(1)
	while(iPos < sString.length) {
		iEnd=sString.indexOf(sDelimeter,iPos);
		if(iEnd==-1) iEnd=sString.length;
		s=sString.substring(iPos,iEnd);
		if(s!=""&&s!=sDelimeter) aResult[iIndex++]=s;
		iPos=iEnd+1;
	}
	return(aResult);
}

function checkoutClick() {

	if(parseInt(GetCookie("iRecordCount")) == 0) {
		alert("Your shopping trolley is empty.");
	} else {
		window.location.href = "CheckOut.htm";
	}
}

function moneyFormat(newValue) {

	var sZeros = "00000000";
	var fVal = parseFloat(newValue);
	var iDecs = 2;
	var fTemp;

	fVal = Math.floor((fVal * Math.pow(10, iDecs)) + 0.5) / Math.pow(10, iDecs);

	fVal += "";
	//No decimal places to display
	if(iDecs < 1) {
		return(fVal);
	}
	else {
		//no decimal point so put one in and add xeros for decimal places
		if(fVal.indexOf(".") == -1) {
			fVal += "." + sZeros.substring(0, iDecs);
		}
		else {
			// change .12 to 0.12
			if(fVal.indexOf(".") == 0) {
				fVal = "0" + fVal;
			}
			// padd with zeros to the number of required decimal places
			if((fVal.length - fVal.indexOf(".") - 1) < iDecs) {
				fVal += sZeros.substring(0, iDecs - (fVal.length - fVal.indexOf(".") - 1));
			}
		}
		return(fVal);
	}
}

function changeCartNumber(iCartNumberLine) {
	var iPos;
	var iRecordCount = parseInt(GetCookie("iRecordCount"));

	iCartNumberLine = parseInt(iCartNumberLine);

	if(!(isNaN(parseInt(document.areaForm.txtModify.value)))) {
		SetCookieItem("iQuantity", iCartNumberLine, parseInt(document.areaForm.txtModify.value));
	} else {
		SetCookieItem("iQuantity", iCartNumberLine, 0);
	}
	if(parseInt(GetCookieItem("iQuantity", iCartNumberLine)) < 1) {
		for(iPos = iCartNumberLine; iPos < (iRecordCount - 1); iPos++) {
			SetCookieItem("sDisplay", iPos, GetCookieItem("sDisplay", iPos +1));
			SetCookieItem("iQuantity", iPos, GetCookieItem("iQuantity", iPos +1));
			SetCookieItem("sProductID", iPos, GetCookieItem("sProductID", iPos +1));
			SetCookieItem("sProductName", iPos, GetCookieItem("sProductName", iPos +1));
			SetCookieItem("sOption1", iPos, GetCookieItem("sOption1", iPos +1));
			SetCookieItem("sOption2", iPos, GetCookieItem("sOption2", iPos +1));
			SetCookieItem("sOption3", iPos, GetCookieItem("sOption3", iPos +1));
			SetCookieItem("cPrice", iPos, GetCookieItem("cPrice", iPos +1));
			SetCookieItem("sOneOff", iPos, GetCookieItem("sOneOff", iPos +1));
			SetCookieItem("sTaxExempt", iPos, GetCookieItem("sTaxExempt", iPos +1));
		}
		iRecordCount--;
	}
	SetCookie("iRecordCount", iRecordCount);
	window.location.href = "ShoppingCart.htm";
}

function viewBasket() {
	iRecordCount = parseInt(GetCookie("iRecordCount"));
	if(iRecordCount > 0) {
		var str="";
		str="<span class=\"bluetext\">your basket contains: ("+iRecordCount+") items.&#160;</span>";
		str+="<input type=\"button\" value=\"View basket\" class=\"form_button\" onclick=\"location.href='../shopping/basket2.html'\" onMouseOut=\"this.className='form_button'\" onMouseOver=\"this.className='form_button_over'\">";
		layerRewriter("basketLyr",str);
	}
}