//cookies.js
/*Since there are no built-in cookie functions in JS (as it is in PHP), we have to create our own JS functions that will set/create a cookie, read/get the cookie's value, delete the cookie*/ 
//"expires" will accept a number of days for the cookie to last
function SetCookie(name,value,expires,path,domain,secure){
	var expireDate = new Date();//current
	expireDate.setTime(expireDate.getTime()+expires*24*3600*1000);
	//set the cookie using document.cookie property
	document.cookie = name+"=" + escape(value)+
	
	((path)?"; path="+path:"")+
	((domain)?"; domain="+domain:"")+
	((secure)?"; secure":"");
} //end SetCookie();

function DeleteCookie(name,path,domain){
	//reset the cookie with the expired date
	document.cookie = name +"="+
	((path)?"; path="+path:"")+
	((domain)?"; domain="+domain:"")+
	"; expires=Thu, 01 Jan 1970 00:00:01 GMT";
} //end DeleteCookie();

//to store cookies with multiple values, use the followin syntax:
//document.cookie = "cname1=value1; path=\; cname2=value2; path=\foldername\; cname3=value3; expires=....."
//GetCookie() has to find a certain cookie name in that list of cookies, and if that cookie is found, get its value
function GetCookie(name){
	//find the needed cookiename's length
	var arg = name +"="; //our cookie name plus =
	var alen = arg.length; //cookies length
	//find how long is the entire cookie file
	var clen = document.cookie.length; //# of chars
	//start the loop from the beginning of the cookie string
	var i=0; //the begining of the cookie
	while(i<clen){ //loop until the end of the entire cookie string
		//find where is the end of the cookie, based on alen
		var j = i+alen;
		//compare if this is what we're looking for:
		if(document.cookie.substring(i,j) == arg){
			//we found our cookie, get its value
			return getCookieVal(j); //end of function with the result of return
		} //end if
		//we didn't find the cookie, search for the next one
		//change "i" to the next position after the ; and space
		//determined by "i"
		i = document.cookie.indexOf(" ",i)+1; //new start for our cookie search 
		if (i == 0) break;		
	} //end while
	//no cookie found
	return null; //return nothing
}//end GetCookie()

/*getCookieVal() accepts the end position of the found cookie name; from that end to the semicolon is our cookie's value*/
function getCookieVal(startvaluepos){
	//find the end position of our cookie (from start until ;)
	var endvaluepos = document.cookie.indexOf(";",startvaluepos);
	if(endvaluepos == -1) {
		//our value is the last thing in this cookie file
		endvaluepos = document.cookie.length;
	}
	//get the cookie's value from start to end
	var cookievalue = unescape(document.cookie.substring(startvaluepos,endvaluepos));
	return cookievalue; //return the value
}//end getCookieVal()

function update(){
	var dom = document.forms[0];
	var elen = dom.elements.length/4;
	var subTotal = 0;
	var qtyTotal = 0;
	for (var i=0; i<dom.elements.length - 3; i+=11){
		var qty = parseFloat(dom.elements[i+7].value);
		var price = parseFloat(dom.elements[i+9].value);
		if (!qty) {
			qty = 0;
		}
		var prodTotal = qty * price;
		subTotal += prodTotal;
		qtyTotal += qty;
		//total for each product
		dom.elements[i+8].value = prodTotal;
	}
	$('subtotal').innerHTML = parseInt(subTotal*100)/100;
	$('totalqty').innerHTML = parseInt(qtyTotal);
}
