
function setCookie(name, value) {
  var curCookie = name + "=" + escape(value);
  document.cookie = curCookie;
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    
    if (begin != 0) return "NO";
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function addtobag(iformname) {
	var dom = eval("document." + iformname);
	var pcode = '';
	
	if (dom.iselect.value > 0 ) {
	//	var list = dom.productID;
	//	var pcode = list.options[list.selectedIndex].value;
	//	}
	//else {
		var radioArray = dom.productID;
		for (var i = 0; i < radioArray.length; i++)
		{
			if (radioArray[i].checked)
			{
				pcode = radioArray[i].value;
			}
		}
	}
	else
	{
		pcode = dom.productID.value;
	}
	//	}
	
	var quantity = dom.quantity.value;
	var bagstring = getCookie('bag');
	
	//check quantity values
	if ((isNaN(Number(quantity))) || (quantity < 1)) {
	return location.reload();
	}
	//if (quantity > 50) {
	//alert ("You may only place a maximum order of 20");
	//return location.reload();
	//}
	//if bag cookie does not exist set new cookie
	if ((bagstring == "NO") || (bagstring == "")) {
		//set new cookie
		newvalue = pcode + quantity;
		setCookie('bag', newvalue);
	}
	

	if ((bagstring != "NO") && (bagstring != "")){
	//split bag cookie into array
	var bagarray = bagstring.split("/");
	//iterate through bag array to check for repeat products
	if (bagarray.length < 21)
	{
		for (var loop = 0; loop < bagarray.length; loop++)
			{
			// break array value in pID and pQ, check matches
			pID = bagarray[loop].substring(0, 10);
			pQ = bagarray[loop].substring(10, bagarray[loop].length);
			//only one mobile allowed in one order 
			if (pID == "2821107065" || pID == "2821107067" || pID == "2821206480" || pID == "2821406060" || pID == "2821406461" || pID == "2821208070" || pID == "2821208080" || pID == "2821208090")
			{
				if (pcode == "2821107065" || pcode == "2821107067" || pcode == "2821206480" || pcode == "2821406060" || pcode == "2821406461" || pcode == "2821208070" || pcode == "2821208080" || pcode == "2821208090")
			    {
			    bagarray[loop] = pcode + "1";
			    var inbag = "TRUE";
			    }
			}
			else
			{
			//if pcode matches pID, product is in bag, update quantity, set inbag variable to true
			if (pcode == pID) {
						var ipQ = parseInt(pQ); 
						var iquant = parseInt(quantity);
						newquantity = ipQ + iquant;
						if (newquantity > 20) {
						newquantity = "20";
						quantity = 20 - ipQ;
						}
						bagarray[loop] = pID + newquantity;
						var inbag = "TRUE";
					 }
			}
			}
		
		var newbagstring = "";
		
		//run through bagarray again and create newbagstring
		for (var nloop = 0; nloop < bagarray.length; nloop++)
			{
			if (nloop == 0) {
			newbagstring = bagarray[nloop];
							}
			if (nloop > 0) {
					newbagstring = newbagstring + "/" + bagarray[nloop];
					}
			}
	}
	else
		{
		alert("You can store a maximum of 20 product types in a single basket. Please place multiple orders if you require more than 20 product types.");
		return false;
		}

	//if after iterating through bagarray inbag was not set to true then add new valueset to end of newbagstring
	if (inbag != "TRUE"){
			   var newvalueset = pcode  + quantity;
			   newbagstring = newbagstring + "/" + newvalueset;
			  }
			  
	//Set Cookie
	setCookie('bag', newbagstring);
	}	
}



function addbulktobag(iformname) {
	var dom = document.forms[iformname];

	var numberofproducts = parseInt(dom.totalproducts.value);

	//alert("Quantity: " + numberofproducts);

	var newbagstring = "";
	var currProduct;
	var currQuantity;
	var currSelect;
	var bagstring = getCookie('bag');
	var quantity;
	var i;
	var ipQ;
	var iquant;
	var newquantity;
	var loop;
	var bagarray = new Array(100);
	var firsttime = 1;
	var firsttime2 = 1;

	for (var abc=0; abc < (numberofproducts); abc++)
	{
		currProduct = dom.productID[abc].value;
		currQuantity = dom.quantity[abc].value;
		currSelect = dom.iselect[abc].value;

		//alert ("This product code is " + currProduct + ", there are " + currQuantity + " to add to basket and iSelect is set to " + currSelect);

		if (currSelect > 0 ) 
		{
			radioArray = dom.productID[abc];
			for (i = 0; i < radioArray.length; i++)
			{
				if (radioArray[i].checked)
				{
					pcode = radioArray[i].value;
				}
			}
		}
		else
		{
			pcode = currProduct;
		}

		quantity = currQuantity;
		
		//check quantity values

		if (quantity > 20) 
		{
			alert ("You may only place a maximum order of 20 products. To order more please contact our Freephone service on 0800 801 901 between 9.30am and 17.30am Monday to Friday");
			return location.reload();
		}

		/*
		 If the cookie already exists look for matching product codes and update the quantities
		 */
		if ((bagstring != "NO") && (bagstring != ""))
		{
			//alert("There is a BAG cookie: " + bagstring);
			firsttime = 0;
			//split bag cookie into array
			bagarray = bagstring.split("/");
			//iterate through bag array to check for repeat products
			foundmatch = 0;
			for (loop = 0; loop < bagarray.length; loop++)
			{
				//alert ("Loop de loop");
				// break array value in pID and pQ, check matches
				pID = bagarray[loop].substring(0, 10);
				pQ = bagarray[loop].substring(10, bagarray[loop].length);
				//if pcode matches pID, product is in bag, update quantity, set inbag variable to true
				if (pcode == pID) 
				{
					foundmatch = 1;
					//alert ("Found a match: " + pcode + " + " + pID);
					ipQ = parseInt(pQ);				// convert the string quantity from the cookie to an INTeger.
					iquant = parseInt(quantity);	// convert the string quantity from the form post to an INTeger.
					newquantity = ipQ + iquant;
					if (newquantity > 20) 
					{
						newquantity = "20";
						quantity = 20 - ipQ;
					}
					bagarray[loop] = pID + newquantity;
				 } else {
					//alert ("No match");
				 }
			}

			if (foundmatch == 0)
			{
				newbagstring += currProduct + currQuantity + "/";
			}
			
			//run through bagarray again and create newbagstring
			for (nloop = 0; nloop < bagarray.length; nloop++)
			{
				if (nloop == 0) 
				{
					newbagstring += bagarray[nloop];
					//alert ("I didn't put a SLASH on the newbagstring: " + newbagstring);
				}
				if (nloop > 0) 
				{
					newbagstring += "/" + bagarray[nloop];
					//alert ("I DID put a SLASH on the newbagstring: " + newbagstring);
				}
			}
			//alert ("Adjusted bag string = " + newbagstring);
			bagstring = newbagstring;
			setCookie('bag', newbagstring);
			newbagstring = "";
		} else {
			//alert("There is NO BAG cookie");
			if (firsttime == 1)
			{
				newbagstring += pcode  + quantity;
				firsttime = 0;
				//alert ("Bag string first time in = " + newbagstring);
			} else {
				newbagstring += "/" + pcode  + quantity;
				//alert ("Bag string NOT first time = " + newbagstring);
			}
			setCookie('bag', newbagstring);
		}
	}

	dom = "";

}


