// JavaScript Document
function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this;
}

bw = new checkBrowser();

function getElmById(aID){
	var rv = null;
	if (bw.dom){
		rv = document.getElementById(aID);
	}else if (bw.ns4){
		rv = document.layers[aID];
	}else if (bw.ie4){
		rv = document.all[aID];
	}
	return rv;
}

function calculateTotal(objForm, fieldArticleTotal, fieldArticleWeight, thisField, discount, articleWeight, divProductTotal, divArticleTotal, fieldProductTotal){
	var articleTotal = Math.round((getElmById(thisField).value*discount)*100)/100; //calculate total article price
	var articleTotalWeight = articleWeight*getElmById(thisField).value; 		   //calculate total article weight

	var productIdArray = divProductTotal.split("_");
	var productId = productIdArray[1];

	getElmById(fieldArticleTotal).value = articleTotal;
	getElmById(fieldArticleWeight).value = articleTotalWeight;
	var productTotal = Math.round((autoSumProduct(objForm, productId))*100)/100;   //calculate total product price
	//var productTotalWeight = autoSumProductWeight(objForm, productId); 		   //calculate total product weight
	var grandTotal = Math.round((autoSumTotal(objForm))*100)/100;
	var grandTotalWeight = autoSumTotalWeight(objForm);
	
	var percentage = (grandTotalWeight/5);
	if(grandTotalWeight>=500){
		percentage = 100;
	}
	getElmById('grandTotalWeight').value = grandTotalWeight;
	getElmById('meter').style.width = percentage+"%";
	getElmById('spanGrandSubTotal').innerHTML = grandTotal;
	getElmById('grandSubTotal').value = grandTotal;
	
	if(grandTotalWeight<100){
		getElmById('discountAmount').innerHTML = "0";
		getElmById('grandDiscount').value = "0";
		grandTotal = grandTotal-(grandTotal*0);
		getElmById('spanGrandDiscount').innerHTML = "0";
	}else if(grandTotalWeight<250){
		getElmById('discountAmount').innerHTML = "7";
		getElmById('grandDiscount').value = "7";
		grandTotal = Math.round((grandTotal-(grandTotal*0.07))*100)/100;
		getElmById('spanGrandDiscount').innerHTML = "7";
	}else if(grandTotalWeight<500){
		getElmById('discountAmount').innerHTML = "10";
		getElmById('grandDiscount').value = "10";
		grandTotal = Math.round((grandTotal-(grandTotal*0.10))*100)/100;
		getElmById('spanGrandDiscount').innerHTML = "10";
	}else if(grandTotalWeight>=500){
		getElmById('discountAmount').innerHTML = "15";
		getElmById('grandDiscount').value = "15";
		grandTotal = Math.round((grandTotal-(grandTotal*0.15))*100)/100;
		getElmById('spanGrandDiscount').innerHTML = "15";
	}
		
	getElmById('grandTotal').value = grandTotal;
	getElmById(divArticleTotal).innerHTML = articleTotal;
	getElmById(divProductTotal).innerHTML = productTotal;
	getElmById(fieldProductTotal).value = productTotal;
	getElmById('spanGrandTotal').innerHTML = grandTotal;
}

function checkOrder(string){
	if(getElmById('grandTotal').value < 150){
		alert(string);
		return false;
	}else{
		return true;	
	}
}

function autoSumTotal(objForm){
	var total = 0;
	for (var i = 0; i < objForm.elements.length; i++){
		var e = objForm.elements[i];
		if (e.type == 'hidden' && e.name=="articleTotal[]"){
			total += parseFloat(e.value);
		}
	}
	return total;
}

function autoSumTotalWeight(objForm){
	var total = 0;
	for (var i = 0; i < objForm.elements.length; i++){
		var e = objForm.elements[i];
		if (e.type == 'hidden' && e.name=="totalWeight[]"){
			total += parseFloat(e.value);
		}
	}
	return total;
}

function autoSumProduct(objForm, productId){
	var total = 0;
	var totalIdStart = "total_p"+productId+"_";	
	var cnt = 0;
	for (var i = 0; i < objForm.elements.length; i++){
		var e = objForm.elements[i];
		if (e.type == 'hidden' && e.id.search(totalIdStart)!=-1){
			total += parseFloat(e.value);
		}
	}
	return total;
}

function autoSumProductWeight(objForm, productId){
	var total = 0;
	var totalIdStart = "totalWeight_p"+productId+"_";	
	var cnt = 0;
	for (var i = 0; i < objForm.elements.length; i++){
		var e = objForm.elements[i];
		if (e.type == 'hidden' && e.id.search(totalIdStart)!=-1){
			total += parseFloat(e.value);
		}
	}
	return total;
}

function agree(btn, cbox){
	if(document.getElementById(''+cbox+'').checked == 1){
		document.getElementById(''+btn+'').disabled = false;
	}else{
		document.getElementById(''+btn+'').disabled = true;
	}
}

function order(action){
	if(action == "confirm"){
		document.formOrder.orderAction.value = "confirm";
		document.formOrder.action = "order.php";
		document.formOrder.submit();
	}else if(action == "cancel"){
		document.formOrder.orderAction.value = "cancel";
		document.formOrder.action = "index.php";
		document.formOrder.submit();
	}else if(action == "edit"){
		document.formOrder.orderAction.value = "edit";
		document.formOrder.submit();
	}
}

function showHide(show){ 
	var divShow = getElmById(show);
	if (divShow.style.display != 'none'){
		divShow.style.display = 'none';
	}else{
		divShow.style.display = '';
	}
}

