function __cookie__ () {

	this.init = function  (howmanyday) {
		var keepTime = howmanyday * 1000 * 60 * 60 * 24; 
		var day = new Date();
		day.setTime(day.getTime() + keepTime);
		document.cookie = 'expires=' + day.toGMTString();
	}
	this.see = function  () {
		alert (unescape(document.cookie));						
	}
	this.read = function  (name) {
		var cookieall = unescape (document.cookie + ";")
		var start = cookieall.indexOf(name + "=")
		var end    = cookieall.indexOf(';', start);
		return (start>-1)? unescape(cookieall.substring(start + name.length + 1, end)) : "";
	}
	this.write = function  (name,val) {
		document.cookie = name + "=" + escape ( val )
	}
	this.clear = function  () {
		var day = new Date();
		day.setTime(day.getTime());
		document.cookie = 'expires=' + day.toGMTString()
		document.cookie =  ''
	}
	
	//Initialise
	this.init(1) // set expire 1 day
}


function chkDIV () {//if cookie, write html
	for (var i=0;i<productlist.length;i++) {
		if (document.getElementById(productlist[i])) {
			document.getElementById(productlist[i]).innerHTML = getCode (productlist[i])
		}
	}
}	

function resetorder (id) { //products page
	document.getElementById(id).value = 0
	COOKIE.write(id,0) //set cookie value = 0
}
function removeorder (id) { //viewcart page
	COOKIE.write(id,0) //set cookie value = 0
	if (chkAll () !="") { oDiv.write(  chkAll () )	} 
	else  { oDiv.write("No items")	}	
	calcAll ()
	setme () 
}

function setme () {//read from cookie and set value for dd list
	for (var i=0;i<productlist.length;i++) {
		if (COOKIE.read(productlist[i]) && COOKIE.read(productlist[i])!=0) {
			document.getElementById(productlist[i]).value = COOKIE.read (productlist[i])
		}
	}
}

function chkAll () {//read from cookie and write code for the order info
	var code = ""
	for (var i=0;i<productlist.length;i++) {
		if (COOKIE.read(productlist[i]) && COOKIE.read(productlist[i])!=0) {//存在し、かつゼロでない
			code += getList ( productlist[i] ) 
		}
	}
	return code
}

var tax;
var total;
var grandtotal;
function calcAll () {//calc sub total and output
	total = 0;tax = 0;grandtotal = 0;
	var code = ""
	for (var i=0;i<productlist.length;i++) {
		if (COOKIE.read(productlist[i]) && COOKIE.read(productlist[i])!=0) {
			subt = products[productlist[i]].price * COOKIE.read(productlist[i]) 
			total += subt
			if (document.getElementById("priceof_" + productlist[i])) {
				document.getElementById("priceof_" + productlist[i]).innerHTML = subt + " Yen"
			}
			
		}
	}
	//TAX
	tax = total * 0.05
	grandtotal = total + tax
	display = "<div style='display:block;line-height:22px;width:350px;text-align:right;color:red;font-size:13px;font-weight:bold;'> Total: " + total + " Yen </div>"
	display += "<div style='display:block;line-height:22px;width:350px;text-align:right;color:red;font-size:13px;font-weight:bold;'> Tax: " + tax + " Yen </div>"
	display +=  "<div style='display:block;line-height:22px;width:350px;text-align:right;color:red;font-size:13px;font-weight:bold;'> <span style='border:1px red solid;border-width:1px 0 0 0;' >Grand Total: " + grandtotal + " Yen </span></div>"
	document.getElementById("total").innerHTML = display
}

function summaryAll () {//calc sub total and output
	total = 0;tax = 0;grandtotal = 0;
	var code = ""
	for (var i=0;i<productlist.length;i++) {
		kosu = COOKIE.read(productlist[i])
		if (kosu && kosu!=0) {
			subt = products[productlist[i]].price * kosu
			total += subt
			code += getSummaryLine (products[productlist[i]].name,products[productlist[i]].id,products[productlist[i]].price,kosu,subt)			
		}
	}
		
	//TAX
//	tax = Math.floor (total * 0.05)
	tax = Math.round (total * 0.05)
	grandtotal = total + tax
	display = "<div style='display:block;line-height:22px;width:350px;text-align:right;color:red;font-size:13px;font-weight:bold;'> Total: " + total + " Yen </div>"
	display += "<div style='display:block;line-height:22px;width:350px;text-align:right;color:red;font-size:13px;font-weight:bold;'> Tax: " + tax + " Yen </div>"
	display +=  "<div style='display:block;line-height:22px;width:350px;text-align:right;color:red;font-size:13px;font-weight:bold;'> <span style='border:1px red solid;border-width:1px 0 0 0;' >Grand Total: " + grandtotal + " Yen </span></div>"
	document.getElementById("total").innerHTML = display

	return code
}


function getCode (id) {
		var code = ""	
		code += '<div class="product_info"> ' + products[id].size + ' (' + products[id].met + ') - ' + '<b>[' + products[id].id + ']</b> ' + products[id].price + ' Yen '
		code += '<select name="' + products[id].id + '" id="' + products[id].id + '" onchange=\"COOKIE.write(\'' + products[id].id + '\', this.value)\">\n';
		code += '<option value="0">0</option>\n';
		code += '<option value="1">1</option>\n';
		code += '<option value="2">2</option>\n';
		code += '<option value="3">3</option>\n';
		code += '<option value="4">4</option>\n';
		code += '<option value="5">5</option>\n';
		code += '<option value="6">6</option>\n';
		code += '<option value="7">7</option>\n';
		code += '<option value="8">8</option>\n';
		code += '</select>\n';
		code += '<input type="button" name="" value="Cancel" onclick="resetorder(\'' + products[id].id + '\')">';
		code += '</div>\n';
	return code;
		
	}	

function getList (id) {
		var code = ""	
		var imgname = products[id].id.toLowerCase()
//		code += '<p>If you want to make a change, you can do it here.</p>'
		code += '<div class="productlist"><span>'
		code += '<div class="product_img"><img src="productsimg/' + imgname + '.gif" alt="" border="0"></div>'
		code += '<div class="product_name_s">' + products[id].name + '</div>'
		code += '<div class="product_info"> ' + products[id].size + ' (' + products[id].met + ') - <b style="color:blue;">' + products[id].id + '</b><br>' + products[id].price + ' yen X '
		code += '<select name="' + products[id].id + '" id="' + products[id].id + '" onchange=\"COOKIE.write(\'' + products[id].id + '\', this.value);calcAll ()\">\n';
		code += '<option value="0">0</option>\n';
		code += '<option value="1">1</option>\n';
		code += '<option value="2">2</option>\n';
		code += '<option value="3">3</option>\n';
		code += '<option value="4">4</option>\n';
		code += '<option value="5">5</option>\n';
		code += '<option value="6">6</option>\n';
		code += '<option value="7">7</option>\n';
		code += '<option value="8">8</option>\n';
		code += '</select><br>\n';
		code += '<div class="product_sub" id=priceof_' + products[id].id + '></div>'
		code += '<input type="button" name="" value="Cancel" onclick="removeorder(\'' + products[id].id + '\')">';
		code += '</div>\n';
		code += '</span></div>\n';
	return code;
}

function getSummaryLine (name,productid,price,kosu,subt) {//confirm page
		var code = ""	
		code += '<div class="summaryline"><span>'
		code += '<div class="sum_product_name">' + name + '</div>'
		code += '<div class="sum_product_price"><b style="color:blue;">[' + productid + ']</b> ' + price + ' Yen x ' + kosu + ' = <font style="color:red;">	' + subt + ' Yen </font></div>'
		code += '</span></div>\n';
	return code;
}
function getSummarymail (name,productid,price,kosu,subt) {
		var code = ""	
		code += name + " [" + productid + "]: " + price + " Yen x " + kosu + " = " + subt + "\n"
	return code;
}

function order (f) {
	total = 0
	var code = ""
	for (var i=0;i<productlist.length;i++) {
		kosu = COOKIE.read(productlist[i])
		if (kosu && kosu!=0) {
			subt = products[productlist[i]].price * kosu
			total += subt
			code += getSummarymail (products[productlist[i]].name,products[productlist[i]].id,products[productlist[i]].price,kosu,subt)			
		}
	}
//	f = document.forms["f_order"]
	f.frm_total.value = total 
	f.frm_tax.value = tax 
	f.frm_grandtotal.value = grandtotal 
	f.frm_text.value = code 
	
}


