function roundSum(sum){
	sum = ""+Math.round(sum*100);
	var len = sum.length;
	if(len<2)
		sum = "0.0"+sum;
	else if (len==2)
		sum = "0."+sum;
	else
		sum = sum.substr(0,len-2)+"."+sum.substr(len-2);
	return sum;
}

function calcSum(){
	var f = document.forms.sampleorder;
	var sum = 0;
	for(var i=0;i<f.elements.length;i++){
		if(f.elements[i].checked)
			sum += parseFloat(f.elements[i].value);
	}
	var su = roundSum(sum);
	var tot = document.getElementById("stotal");
	if(tot)
		tot.innerHTML = su;
	var tot = document.getElementById("total");
	if(tot)
		tot.innerHTML = su;
}
function quantitySelectChange(select){
	var el = document.getElementById("price_"+select.name);
	if(el) {
		var discount = 0;
		if(select.PDiscount)
			discount = parseFloat(select.PDiscount);
		var psel = document.getElementById(select.PSelect);
		if(psel && psel.firstChild.selected)
			discount = 0;
		var opt = select.firstChild;
		while(opt) {
			if(opt.nodeName.toLowerCase()=="option")
				if(opt.value == select.value) {
					var price = roundSum(parseFloat(opt.title)*(1-discount/100));
					select.price = price;
					el.innerHTML = price;

					var pr = document.getElementById("promo_"+select.name);
					if(pr){
						var p = opt.title.indexOf("|");
						if(p>-1)
							pr.innerHTML = opt.title.substring(p+1);
						else
							pr.innerHTML = "";
					}
					break;
				}
			opt = opt.nextSibling;
		};
	};
	return el;
}
function calcSubTotal(){
	var subTotal = 0;
	for(var i = 0;i<pricesel.length;i++){
		subTotal += parseFloat(pricesel[i].price);
	}
	var sum = roundSum(subTotal);
	var span = document.getElementById("subtotal");
	if(span)
		span.innerHTML = sum;
	span = document.getElementById("priceitemstotal");
	if(span) {
		subTotal -= parseFloat(span.innerHTML);
		span = document.getElementById("itemdiscount");
		if(span)
			span.innerHTML = roundSum(subTotal);
	}
	span = document.getElementById("pricetotal");
	if(span) {
		var price = parseFloat(span.innerHTML);
		document.getElementById("total").innerHTML = roundSum(price);
	}
}

// mainpage
function prepareRootMenu(rootmenu0){
	var chs = rootmenu0.childNodes;
	for(var i = 0; i<chs.length;i++){
		var a = chs[i].firstChild;;
		var url = a.href;
		if((url.indexOf("wedding_stationery")>0)&& (chs[i].className.indexOf("active")>-1)){
			var ul = document.createElement("ul");
			ul.className = "rootmenu1";
			for(var j in cats){
				var li = document.createElement("li");
				li.className="leftmenu1";
				var aa = document.createElement("a");
				aa.href=url+j+"/";
				if(document.location.href == aa.href)
					li.className = "leftmenu1 leftmenuactive1";
				aa.innerHTML=cats[j];
				li.appendChild(aa);
				ul.appendChild(li);
			}
			chs[i].appendChild(ul);
		}	
	}
}


function showDialog(){
	var dia_back = document.getElementById("dialog_background");
	if(!dia_back){
		dia_back = document.createElement("div");
		dia_back.id = "dialog_background";
		document.body.appendChild(dia_back);
	}
	dia_back.style.visibility="visible";
	dia_back.style.height = document.documentElement.scrollHeight + 'px';
	var dialog = document.getElementById("dialog_form");
	if(dialog)
		do {
			child = dialog.firstChild;
			if(child)
				dialog.removeChild(child);
		} while (child)
	else {
		dialog = document.createElement("form");
		dialog.id = "dialog_form";
		dialog.onsubmit = "return false;";
		document.body.appendChild(dialog);
	}
	
	var ih = '<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background:url(/image/wa_logo.gif) 0 20px no-repeat">';
	ih += '<tr><td width="200"> </td><td width="225">';
	ih += '<div style="font-size:26px; margin:10px 0; ">Vote for Whites !</div>';
	ih += '<div style="font-size:14px; margin:20px 0; line-height:30px; ">Whites have been nominated by Perfect Wedding magazine for Best Wedding Stationer of the Year, we hope we can count on your vote, best wishes, Sue &amp; Helena.</div>';
	ih += '</td></tr><tr><td colspan="2" align="right">';
	ih += '<a href="http://www.perfectweddingmag.com/awards/"><img src="/image/vote_now.gif" alt="vote now" /></a>';
	ih += '<a href="#" onclick="closeDialog(); return false;"><img src="/image/no_thanks.gif" alt="no thanks" /></a>';
	ih += '</td></tr></table>';
	dialog.innerHTML = ih;
	
	dialog.style.top = (document.documentElement.clientHeight - dialog.offsetHeight)/2 +"px";
	dialog.style.left = (document.documentElement.clientWidth - dialog.offsetWidth)/2 +"px";
	dialog.style.visibility = "visible";
}

function closeDialog(form) {
	var dia_back = document.getElementById("dialog_background");
	if(dia_back)
		dia_back.style.visibility = "hidden";
	var ret = new Array();
	if(!form)
		form = document.getElementById("dialog_form");
	var f = form.elements;
	form.style.visibility = "hidden";
	for(var i=0; i<f.length; i++)
		ret.push(f[i].value);
	return ret;
}

function showWeddingAwards(){
	var c = getCookie("wedding_awards");
	if(!c){
		showDialog("AA","BB");
	}
	setCookie("wedding_awards","show");
}
