function iipGetElementById(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;
	return dis;
}
function getDiscount(val){
	var dis = getDiscountProc(val);
	var sum = Math.round(val*dis)/100;
	return sum;
}
function calculateAmountChange(oObj, sUnique, sEd){
	//alert(nItemId);
	calculateAmount(sUnique, sEd);
	//var oObj = iipGetElementById("oInput["+nItemId+"]");
	var oImg = iipGetElementById("chimg");
	var mr = Math.random();
	var sUrl = "/buy/?Action=changeAmountmini&unique="+sUnique+"&q="+oObj.value+"&mr="+mr;
	oImg.src = sUrl;
	//op2(oImg);
}
function calculateAmount(nItemIdArt, sEd) {
	var oObj = iipGetElementById("oInput['"+nItemIdArt+"']");
	var oTarget = iipGetElementById("oTDAmount['"+nItemIdArt+"']");
	var oTargetItogo = iipGetElementById("oTDItogo['"+nItemIdArt+"']");
	var nRetail = iipGetElementById("oTDRetail['"+nItemIdArt+"']").innerHTML*1.;
	var nQuant = document.getElementById("quant["+nItemIdArt+"]").value*1.;
	//op2(iipGetElementById("oTDRetail['"+nItemIdArt+"']").innerHTML);
	oTarget.innerHTML = Math.round(nRetail*oObj.value*nQuant*100)/100;
	oTargetItogo.innerHTML = (oObj.value*nQuant)+" "+sEd;
	calculateTotalAmount();
}

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

	if(oObj.checked){
		oTarget.innerHTML = oTargetDu.value;
		oTargetDur.value = oTargetDu.value;
	}
	else {
		oTarget.innerHTML = oTargetD.value;
		oTargetDur.value = oTargetD.value;
	}
}
function deleteItemFromOrder(nItemId,sUnique){
	iipGetElementById("oOrderUnique").value = sUnique;
	iipGetElementById("oOrderAction").value = "deleteItem";
	iipGetElementById("oOrderItemId").value = nItemId;
	iipGetElementById("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=iipGetElementById("oDataRowlen");
	var ar = new Array();
	var ax=0;
	for(i=0;i<gg.value;i++){
		ar[ax] = iipGetElementById("oDataRow["+i+"]");
		ax++;
	}
	return ar;
}
function calculateTotalAmount() {
		var oTarget = iipGetElementById("oOrderTotal");
		//var oTarget2 = iipGetElementById("oDiscount");
		//var oTotalobj = iipGetElementById("oTotal");
		var disrub = 0;
		var nTotal = 0;
		var aCells;
		var oOrdec = iipGetElementById("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*100)/100;
		//disrub = getDiscount(Math.round(nTotal));
		//oTarget2.innerHTML = disrub;
		//oTotalobj.innerHTML = Math.round(nTotal) - disrub;
}
function submitQuantity(){
	iipGetElementById("oOrderAction").value = "saveQuantities";
	iipGetElementById("oOrderForm").submit();
}

function fsubmitOrder(){
	var onaf = iipGetElementById("ofnamef");
	var onai = iipGetElementById("ofnamei");
	var onao = iipGetElementById("ofnameo");

	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 = iipGetElementById("ofphone");
	var oem = iipGetElementById("ofemail");
	var oad = iipGetElementById("ofaddr"); 
	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;
	}
	iipGetElementById("oltype").value = "f";
	iipGetElementById("oOrderAction").value = "submitOrder";
	iipGetElementById("oOrderForm").submit();
}

function jsubmitOrder(){
	var onaf = iipGetElementById("ojnamef");
	var onai = iipGetElementById("ojnamei");
	var onao = iipGetElementById("ojnameo");
	var oph = iipGetElementById("ojphone");
	var oem = iipGetElementById("ojemail");
	var ofna = iipGetElementById("ojfname");
	var oinn = iipGetElementById("ojinn");
	var okpp = iipGetElementById("ojkpp");
	var ofph = iipGetElementById("ojfphone");
	var ofad = iipGetElementById("ojfaddr");
	var oad = iipGetElementById("ojaddr"); 

	if(!onaf.value){
		alert("Заполните поле Фамилия");
		onaf.focus()
		return false;
	}
	if(!onai.value){
		alert("Заполните поле Имя");
		onai.focus()
		return false;
	}
	if(!onao.value){
		alert("Заполните поле Отчество");
		onao.focus()
		return false;
	}
	if(!oph.value){
		alert("Заполните поле Телефон");
		oph.focus()
		return false;
	}
	if(!oem.value){
		alert("Заполните поле Email");
		oem.focus()
		return false;
	}
	if(!ofna.value){
		alert("Заполните поле Название организации");
		ofna.focus()
		return false;
	}
	if(!oinn.value){
		alert("Заполните поле ИНН");
		oinn.focus()
		return false;
	}
	if(!okpp.value){
		alert("Заполните поле КПП");
		okpp.focus()
		return false;
	}
	if(!ofph.value){
		alert("Заполните поле Телефон организации");
		ofph.focus()
		return false;
	}
	if(!ofad.value){
		alert("Заполните поле Юр. адрес");
		ofad.focus()
		return false;
	}

	if(!oad.value){
		alert("Заполните поле Адрес доставки");
		oad.focus()
		return false;
	}
	iipGetElementById("oltype").value = "j";
	iipGetElementById("oOrderAction").value = "submitOrder";
	iipGetElementById("oOrderForm").submit();
}

