function itpGetElementById(id){
	if (document.getElementById) {
		return (document.getElementById(id));
	} else if (document.all) {
		return (document.all[id]);
	} else {
		if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) {
			return (document.layers[id]);
		}
	}
}

function getDiscountProc(val){
	var dis = 0;
	if(val>=29000 && val<87000) dis = 3;
	if(val>=87000 && val<145000) dis = 5;
	if(val>=145000 && val<203000) dis = 7;
	if(val>=203000 && val<290000) dis = 10;
	if(val>=290000) dis = 13;
	dis = 0;
	return dis;
}
function getDiscount(val){
	var dis = getDiscountProc(val);
	var sum = Math.round(val*dis)/100;
	return sum;
}
function calculateAmountChange(nItemId){
		calculateAmount(nItemId);
		var oObj = itpGetElementById("oInput["+nItemId+"]");
		var oImg = itpGetElementById("chimg");
		var mr = Math.random();
		var sUrl = "index.php?par=7&op=changeAmountmini&id="+nItemId+"&q="+oObj.value+"&mr="+mr;
		oImg.src = sUrl;
		//op2(oImg);
}
function calculateAmount(nItemId) {
		var oObj = itpGetElementById("oInput["+nItemId+"]");
		var oTarget = itpGetElementById("oTDAmount["+nItemId+"]");
		var nRetail = itpGetElementById("oTDRetail["+nItemId+"]").innerHTML*1;
		oTarget.innerHTML = Math.round(nRetail*oObj.value);
		calculateTotalAmount();
}

function calcDost(nItemId, oObj){
		var oTarget = itpGetElementById("oCost");
		var oTargetD = itpGetElementById("oDcost");
		var oTargetDu = itpGetElementById("oDucost");
		var oTargetDur = itpGetElementById("oDcostreal");

		if(oObj.checked){
			oTarget.innerHTML = oTargetDu.value;
			oTargetDur.value = oTargetDu.value;
		}
		else {
			oTarget.innerHTML = oTargetD.value;
			oTargetDur.value = oTargetD.value;
		}
}
function deleteItemFromOrder(nItemId){
        itpGetElementById("oOrderAction").value = "deleteItem";
        itpGetElementById("oOrderItemId").value = nItemId;
        itpGetElementById("oOrderForm").submit();
}
	function op(object1){
		alert(object1);
		for (i in object1){
			//ax++;
//			re=re+' '+i+' = '+object1[i]+' \n'; 
			if(!confirm(i+' = '+object1[i])) break;
//			if(!(ax%10)){ if(!confirm(re)) break; else re='';}
		}
	}

function op2(object1){
	var ax=0;
	var lim=0;
	for (j in object1){
		lim++;
	}
	var re='till '+(5)+' of '+lim+'\n';
	alert(object1+" has "+lim+" props");

	for (i in object1){
		ax++;
		if(i)re=re+' '+i+' = '+object1[i]+' \n'; 
		if(!(ax%5)||ax==lim){
			if(!confirm(re)) break; 
			else re='till '+(ax+5)+' of '+lim+'\n';
		}
	}
}
function getrows(){
	var gg=itpGetElementById("oDataRowlen");
	var ar = new Array();
	var ax=0;
	for(i=0;i<gg.value;i++){
		ar[ax] = itpGetElementById("oDataRow["+i+"]");
		ax++;
	}
	//op(ar);
	return ar;
}
function calculateTotalAmount() {
		var oTarget = itpGetElementById("oOrderTotal");
		var oTarget2 = itpGetElementById("oDiscount");
		var oTotalobj = itpGetElementById("oTotal");
		var disrub = 0;
		var nTotal = 0;
		var aCells;
		var oOrdec = itpGetElementById("oOrderContent");
		var aRows = getrows();
		if(aRows.length){
				for(var i=0; i<aRows.length; i++) {
						aCells = aRows[i].cells;
						nTotal+= aCells[aCells.length-4].innerHTML*1;
						//alert("plus"+aCells[aCells.length-4].innerHTML*1);
				}
		}
		else {
				aCells = aRows.cells;
				nTotal+= aCells[aCells.length-4].innerHTML*1;
				//alert("plusit"+aCells[aCells.length-4].innerHTML*1);
		}
		//alert(nTotal);
		oTarget.innerHTML = Math.round(nTotal);
		disrub = getDiscount(Math.round(nTotal));
		oTarget2.innerHTML = disrub;
		oTotalobj.innerHTML = Math.round(nTotal) - disrub;
}
function submitQuantity(){
        itpGetElementById("oOrderAction").value = "saveQuantities";
        itpGetElementById("oOrderForm").submit();
}
function submitOrder(sMsg){
		var onaf = itpGetElementById("onamef");
		var onai = itpGetElementById("onamei");
		var onao = itpGetElementById("onameo");
		if(!onaf.value){
			alert("Заполните поле Фамилия");
			onaf.focus()
			return false;
		}
		if(!onai.value){
			alert("Заполните поле Имя");
			onai.focus()
			return false;
		}
		if(!onao.value){
			alert("Заполните поле Отчество");
			onao.focus()
			return false;
		}

		var oph = itpGetElementById("ophone");
		var oem = itpGetElementById("oemail");
		var oad = itpGetElementById("oaddr");
		if(!oph.value){
			alert("Заполните поле Телефон");
			oph.focus()
			return false;
		}
		if(!oem.value){
			alert("Заполните поле Email");
			oem.focus()
			return false;
		}
		if(!oad.value){
			alert("Заполните поле Адрес");
			oad.focus()
			return false;
		}
		itpGetElementById("oOrderAction").value = "submitOrder";
		itpGetElementById("s_order").style.width = "300px";
		itpGetElementById("s_order").value = "Подождите, идёт отправка заказа";
		itpGetElementById("s_order").disabled = true;
		itpGetElementById("oOrderForm").submit();
}
