docBody = document.getElementsByTagName("body").item(0);
var ctr = 0;

function Ribuan(num,des) {
	pengali = Math.pow(10,des);
	cents = Math.floor((num*pengali+0.5)%pengali).toString();
	num = Math.floor((num*pengali+0.5)/pengali).toString();
        smtr = ''; 
        if (des>cents.length) { 
            for (i=0; i < (des-cents.length); i++) { 
                smtr += '0';
            } 
        } 
        cents = smtr + cents; 
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	if (des==0) {
		return (num);
	} else {
		return (num + '.' + cents);
	}
}
function unRibuan(num) {
	split1 = num.split(".");
	split2 = split1[0].split(",");
	num = "";
	for (var i = 0; i < split2.length; i++) {
		num = num + split2[i];
	}
	num = num + "." + split1[1];
	num = parseFloat(num);
	return (num);
}
function trim(teks) {
	teks=teks.replace(/^(\s)+/,'');
	teks=teks.replace(/(\s)+$/,'');
	return teks;
}

function valOrder() {
	for(i=0; i<=document.order.length-1; i++) {
		nilai = document.order.elements[i].value;
		if (trim(nilai)=='') {
			nilai=0;
		}
		if (i==document.order.length-1) {
			if (isNaN(nilai) || nilai<0) {
				window.alert("Please enter a valid amount. Thank you!");
				document.order.elements[i].focus();
				return false;
			}
		} else {
			if (isNaN(nilai) || nilai<=0) {
				window.alert("Please enter a valid amount. Thank you!");
				document.order.elements[i].focus();
				return false;
			}
		}
	}
	return true;
}

function cekNaN(nilai,nama) {
	if (trim(nilai)=='') {
		nilai=0;
		eval("document.products."+nama+".value=0");
	}
	if (isNaN(nilai) || nilai<0) {
		window.alert("Please enter a valid amount. Thank you!");
		eval("document.products."+nama+".focus()");
		return false;
	} else {
		return true;
	}
}

function hitung() {
	kolom=7;
	prodesc = "";
	hasil4 = 0;
	hasil5 = 0;
	xisitabel = document.getElementById("isitabel");
	xtr = xisitabel.getElementsByTagName("tr");
	xtd = xisitabel.getElementsByTagName("td");
	if (xtd.length != 0) {
		for(i=0;i<=xtr.length-1;i++) {
			for(j=1;j<=kolom-1;j++) {	//j=0 --> "remove button"
				k=i*kolom+j;
				if (j==1 && i==0) {
					prodesc += "kolom" + j +"=" + xtd.item(k).firstChild.nodeValue;
				} else {
					prodesc += "&kolom" + j +"=" + xtd.item(k).firstChild.nodeValue;
				}
				if (j==4) {
					hasil4 += unRibuan(xtd.item(k).firstChild.nodeValue);
				}
				if (j==5) {
					hasil5 += unRibuan(xtd.item(k).firstChild.nodeValue);
				}
			}
		}
	}
	xtotaltabel = document.getElementById("totaltabel");
	xtd = xtotaltabel.getElementsByTagName("td");
	xtd.item(1).firstChild.nodeValue=Ribuan(hasil4,0);
	xtd.item(2).firstChild.nodeValue=Ribuan(hasil5,4);
	document.products.Product_Desc.value = prodesc + "&total1=" + xtd.item(1).firstChild.nodeValue + "&total2=" + xtd.item(2).firstChild.nodeValue;
	document.products.Quantity_Custom.value = hasil5;
}

function validasi() {

	//validasi field khusus
	if (document.products.Quantity[0].checked && document.products.Quantity_Custom.value<=0) {
		window.alert("Quantity: Please enter a valid entry. Thank you!");
		document.products.Quantity_Custom.focus();
		return false;
	}

	if (document.products.Pitch[0].checked && document.products.Pitch_Custom.value<=0) {
		window.alert("Pitch: Please enter a valid entry. Thank you!");
		document.products.Pitch_Custom.focus();
		return false;
	}

	if (document.products.Finish[3].checked && trim(document.products.Finish_Custom.value)=='') {
		window.alert("Finish: Please describe the custom finish wanted. Thank you!");
		document.products.Finish_Custom.focus();
		return false;
	}

	if (document.products.Packing[4].checked && trim(document.products.Packing_Custom.value)=='') {
		window.alert("Packing: Please describe the custom packing wanted. Thank you!");
		document.products.Packing_Custom.focus();
		return false;
	}

	//validasi required
	//42 = Contact Person
	for (i=42;i<=53;i++) {
		if (i!=52) {
			if (trim(document.products.elements[i].value) == '') {
				window.alert("Please fill in all required fields. Thank you!");
				document.products.elements[i].focus();
				return false;
			}
		}
	}

	//validasi email
	if (document.products.Email.value.match((/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/)) || !(document.products.Email.value.match(/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/))) {
		window.alert("You enter an invalid e-mail address. Please fix it up. Thank you!");
		document.products.Email.focus();
		return false;
	}

}

function tambah() {
	if (!valOrder()) return;
	jumlahbarismax = 15;
	if (ctr < jumlahbarismax) {
		row1 = document.createElement("tr");

		//tanda minus
		cell1 = document.createElement("td");
		linkminus = document.createElement("a"); 
		linkminus.setAttribute("href","javascript:kurang("+ctr+")"); 
		imgminus = document.createElement("img"); 
		imgminus.setAttribute("src","images/buttonmin.jpg"); 
		imgminus.setAttribute("border","0"); 
		imgminus.setAttribute("width","60"); 
		imgminus.setAttribute("height","20"); 
		imgminus.setAttribute("alt","Remove"); 
		linkminus.appendChild(imgminus);
		cell1.appendChild(linkminus);
		row1.appendChild(cell1);

		//T W L Pcs
		j=1;
		for(i = 0; i < document.order.length-1; i++) {
			cell1 = document.createElement("td");
			cell1.setAttribute("align","right"); 
			a = document.order.elements[i].value;
			document.order.elements[i].value = "";
			cell1.appendChild(document.createTextNode(Ribuan(a,0)));
			row1.appendChild(cell1);
			j=j*parseFloat(a);
		}

		//Cubic meter
		cell1 = document.createElement("td");
		cell1.setAttribute("align","right"); 
		a = j/1000000000;
		cell1.appendChild(document.createTextNode(Ribuan(a,4)));
		row1.appendChild(cell1);

		//Pcs/Bundle
		cell1 = document.createElement("td");
		cell1.setAttribute("align","right"); 
		a = document.order.elements[4].value;
		document.order.elements[4].value = "";
		cell1.appendChild(document.createTextNode(Ribuan(a,0)));
		row1.appendChild(cell1);

		tablebody1 = document.getElementById("isitabel");
		tablebody1.appendChild(row1);
		ctr++;
	} else {
		window.alert("Sorry, we can only accept " + jumlahbarismax + " items at a time");
		for(i = 0; i < document.order.length; i++) {
			document.order.elements[i].value = "";
		}
	}
	hitung();
}

function kurang(baris) {
	xisitabel = document.getElementById("isitabel");
	xtr = xisitabel.getElementsByTagName("tr");
	xisitabel.removeChild(xtr.item(baris));
	ctr--;
	linkminus = xisitabel.getElementsByTagName("a"); 
	for (i=baris;i<=xtr.length-1;i++) {
		linkminus.item(i).setAttribute("href", "javascript:kurang("+i+")"); 
	}
	hitung();
}
