/* 
	Функция записи cookie.
	setCookie(name, value[, expires date, path, domain, secure])
*/
function setCookie(name, value)
{
	var args = setCookie.arguments;
	var expires = args[2] ? '; expires=' +  args[2].toGMTString() : '';
	var path = args[3] ? '; path=' + args[3] : '';
	var domain = args[4] ? '; domain=' + args[4] : '';
	var secure = args[5] ? '; secure' : '';
	document.cookie = name + '=' + escape(value) + expires + path + domain + secure;
}
		
/* Функция чтения cookie */
function getCookie(name)
{
	name += '=';
	with(document)
	{
		var beg = cookie.indexOf(name);
		if(beg == -1) return false;
		else beg += name.length;
		var end = cookie.indexOf(';', beg);
		if(end == -1) end = cookie.length;
		return unescape(cookie.substring(beg, end));
	}
}
		
/* Функция удаления cookie */
function deleteCookie(name)
{
	var expDate = new Date();
	expDate.setTime(expDate.getTime() - 1);
	setCookie(name, getCookie(name), expDate);
	//document.cookie = "cart=;";
	//alert(document.cookie);
}

function testit(id,price){
		if(getCookie("cart"))
		{
			val=getCookie("cart")+"="+id+"-"+document.all["select"+id].value+"*"+price;
		}else{
			val=id+"-"+document.getElementById("select"+id).value+"*"+price;
		}

		setCookie("cart", val,"","/");
		tmp_arr=parse_cart();

		sum=0;
		for(var i=0; i < tmp_arr.length; i++)
		{
			subs=tmp_arr[i].substring(tmp_str.indexOf("-")+1, tmp_arr[i].length);
			sum = sum+subs.substring(0, subs.indexOf("*")) * subs.substring(subs.indexOf("*")+1,subs.length);
		}
		window.alert("В корзине наименований: "+tmp_arr.length);
}

function parse_cart(){
	if(getCookie("cart")){
		tmp_str=getCookie("cart");
		i=0;
		var arr = new Array();
		while(tmp_str.indexOf("=")!=-1){
			arr[i]=tmp_str.substring(0,tmp_str.indexOf("="));
			tmp_str=tmp_str.substring(tmp_str.indexOf("=")+1,tmp_str.length);
			i=i+1;
		}
		arr[i]=tmp_str;
		return arr; //*/
	}else{
		deleteCookie("cart");	
	}
}

function submit_cart(){
	if(getCookie("cart"))
		{

			tmp_str=getCookie("cart");
			document.all.carta.value=tmp_str;
			//alert (document.all.carta.value);
			document.all.cart_form.submit();
		}else{
			window.alert("Корзина пуста!\nВыберите интересующий вас товар.\nУкажите количество и нажмите \"Заказать\"\nЗатем перейдите к вашему заказу."); 
		}
}

function recount(){
	i1=0;
	i2=0;
	i3=0;
	if(document.all.amount.length){
		for(var i=0; i<document.all.amount.length; i++){
			document.all.summ[i].value = document.all.price[i].value*document.all.amount[i].value;
			document.all.sweight[i].value = document.all.weight[i].value*document.all.amount[i].value;
			i1=i1+ parseInt(document.all.amount[i].value)
			i2=i2+ parseInt(document.all.summ[i].value)
			i3=i3+ parseInt(document.all.sweight[i].value)
		}
	}
	else{
			document.all.summ.value = document.all.price.value*document.all.amount.value;
			document.all.sweight.value = document.all.weight.value*document.all.amount.value;
			i1=i1+ parseInt(document.all.amount.value)
			i2=i2+ parseInt(document.all.summ.value)
			i3=i3+ parseInt(document.all.sweight.value)
	}
	document.all.iamount.value=i1;
	document.all.isumm.value=i2;
	if(	document.all.na_summu){
		document.all.na_summu.value=document.all.isumm.value + ' руб.';
	}
	document.all.iweight.value=i3;

	tmp_x=(document.all.iweight.value - (document.all.iweight.value % 3000))/3000;
	if((document.all.iweight.value % 3000)>0){
		tmp_x= tmp_x+1;
	}
	if(document.all.dsumm){
		if(tmp_x<3){
			if(tmp_x==1){
				document.all.dsumm.value = 200;
			}else{
				document.all.dsumm.value = 300;
			}
			
		}else{
			document.all.dsumm.value = 600;
		}
	}
	if(document.all.fsumm){
		document.all.fsumm.value = parseInt(document.all.dsumm.value) + parseInt(document.all.isumm.value)
	}
	//window.alert(tmp_x);
	if(document.all.td_err){
		document.all.td_err.style.display="none";
	}
	if(document.all.region)count_nal();
}

function del_one(it){
	
	var the_cookie="";
	for(var i=0; i<document.all.summ.length; i++){
		if(i!=it-1){
			the_cookie=the_cookie+document.all.id[i].value+"-"+document.all.amount[i].value+"*"+document.all.price[i].value+"=";
		}
	}
	the_cookie=the_cookie.substring(0,the_cookie.length-1);
	setCookie("cart", the_cookie,"","/");
	//submit_cart();
	document.location="index.php?carta="+the_cookie;

}

function step(num){

	if(num==1){
		var the_cookie="";
		if(document.all.summ.length){
			for(var i=0; i<document.all.summ.length; i++){
				the_cookie=the_cookie+document.all.id[i].value+"-"+document.all.amount[i].value+"*"+document.all.price[i].value+"=";
			}
		}else{
			the_cookie=the_cookie+document.all.id.value+"-"+document.all.amount.value+"*"+document.all.price.value+"=";
		}
		the_cookie=the_cookie.substring(0,the_cookie.length-1);
		setCookie("cart", the_cookie,"","/");

		document.all.carta.value=the_cookie;

		if(document.all.delivery[0].checked){
			document.all.deliv.value=document.all.delivery[0].value;
		}else{
			document.all.deliv.value=document.all.delivery[1].value;
		}
		document.all.dsum.value=document.all.dsumm.value;
		//loc=loc + "&kol=" + document.all.dsumm.value/100 + "&dsumm=" + document.all.dsumm.value
		//window.alert("NEN")//document.all.price[document.all.price.length-1].value);
		document.cart_form.submit();
	}
	
	if(num==2){
		var the_cookie="";
		if(document.all.id.length){
			for(var i=0; i<document.all.id.length; i++){
				if (document.all.id[i]){
					the_cookie=the_cookie+document.all.id[i].value+"-"+document.all.amount[i].value+"*"+document.all.price[i].value+"=";
				}
			}
		}else{
			if(document.all.amount.length){
				the_cookie=the_cookie+document.all.id.value+"-"+document.all.amount[0].value+"*"+document.all.price[0].value+"=";
			}else{
				the_cookie=the_cookie+document.all.id.value+"-"+document.all.amount.value+"*"+document.all.price.value+"=";
			}
		}
		the_cookie=the_cookie.substring(0,the_cookie.length-1);

		//var loc="";
		//loc=loc+"index.php?carta="+the_cookie+"&delivery=";
		//loc=loc+document.all.delivery.value;

		document.step2.carta.value=the_cookie
		//document.carta_form.carta.
		document.all.sity.disabled=false;
		if(document.all.nal){
			document.all.nal.disabled=false;
		}
		deleteCookie("cart");

		if(document.step2.dsum)document.step2.dsum.value=document.all.price[document.all.price.length-1].value;
		//if(document.step2.dsumm)document.step2.dsumm.value=document.all.price[document.all.price.length-1].value;
		//window.alert(document.all.price[document.all.price.length-1].value);
		//window.alert(document.all.price[document.all.price.length-1].value);
		//document.all.delivery.value
		document.all.step2.submit()
		
	}
}

function oformit(){
	disable_some(false); 
	document.all.step2.dsumm.value = document.all.price[document.all.price.length-1].value;
	document.all.step2.submit();
	//window.alert(document.all.price[document.all.price.length-1].value);
	//window.alert(document.all.step2.dsumm.value);
}

function block_some(){
	if(document.all.amount.length){
		for(var i=0; i<document.all.amount.length; i++){
			document.all.amount[i].disabled=true;
			document.all.amount[i].className="buttonflat";
			if(document.all.delbutton.length){
				if(document.all.delbutton[i]){
					document.all.delbutton[i].style.display="none";
				}
			}else{
				document.all.delbutton.style.display="none";
			}
		}
	
	}else{

		document.all.amount.disabled=true;
		document.all.amount.className="buttonflat";
		if(document.all.delbutton){
			document.all.delbutton.className="buttonflat";
			document.all.delbutton.disabled=true
		}
	}
}

function checkit(){
	var ok = true
	if (document.all.fname.value==""){
		document.all.td_fname.className="err";
		ok=false;
	}else{
		document.all.td_fname.className="text";
	}
	if (document.all.adres.value==""){
		document.all.td_adres.className="err";
		ok=false;
	}else{
		document.all.td_adres.className="text";
	}
	if (document.all.phone.value==""){
		document.all.td_phone.className="err";
		ok=false;
	}else{
		document.all.td_phone.className="text";
	}
	if(ok){
		step(2);
	}else{
		document.all.td_err.style.display="block";
	}
}

function disable_some(yes){

	for (var i=0;i<document.all.step2.elements.length; i++ )
	{
		document.all.step2.elements[i].disabled=yes;
	}
}

function count_nal(){
	// Подсчет наложенного платежа

	if (document.all.region.value=="")
	{
			document.all.nal.value="[нет]";
	}
	else{
		if(document.all["region"+document.all.region.value]){
			poias=document.all["region"+document.all.region.value].value;
		
			tmp_x=(document.all.iweight.value - (document.all.iweight.value % 500))/500;
			if((document.all.iweight.value % 500)>0){
				tmp_x= tmp_x+1;
			}
			var kolvo; //количество бандеролей
			kolvo = 1;
			var tmp_weight=0;
			

			if(document.all.weight.length){
				
				for(var i=0; i<document.all.weight.length; i++){
					if((tmp_weight+document.all.weight[i].value*document.all.amount[i].value)> 1900)
						{
						for (var k=0; k<document.all.amount[i].value ; k++ )
						{	
							//alert("Вес сейчас: " +(tmp_weight + parseInt(document.all.weight[i].value)));
							if(tmp_weight + parseInt(document.all.weight[i].value) < 1900){
								tmp_weight = tmp_weight+parseInt(document.all.weight[i].value);
							}else{
								//alert("Кол-во " + kolvo + " Вес:" + tmp_weight)
								kolvo++;
								tmp_weight = parseInt(document.all.weight[i].value);
							}
						}
						
					}else{
						tmp_weight = tmp_weight+document.all.weight[i].value*document.all.amount[i].value;
					}
				}
				//alert("Кол-во " + kolvo);
				
			}
			
			vel=parseFloat(document.all["poias"+poias].value)
			document.all.nal.value=((tmp_x*vel + 40)*1.15+document.all.isumm.value*0.15+10*kolvo);

			
			// округляем до рублей в большую сторону
			tmp_x=document.all.nal.value - (document.all.nal.value % 1);
			if((document.all.nal.value % 1)>0){
				tmp_x= tmp_x+1;
			}
			document.all.nal.value=tmp_x//tmp_str.substring(0, tmp_str.indexOf(".")+3)		
		}
	}

}


function checkit2(){
	// ???
	var ok = true
	if (document.all.fname.value==""){
		document.all.td_fname.className="err";
		ok=false;
	}else{
		document.all.td_fname.className="text";
	}
	if (document.all.indeks.value==""||document.all.indeks.value.length<6){
		document.all.td_indeks.className="err";
		ok=false;
	}else{
		document.all.td_indeks.className="text";
	}
	if (document.all.adres.value==""){
		document.all.td_adres.className="err";
		ok=false;
	}else{
		document.all.td_adres.className="text";
	}
	if (document.all.region.value==""){
		document.all.td_region.className="err";
		ok=false;
	}else{
		document.all.td_region.className="text";
	}


	if (document.all.sity.value==""){
		document.all.td_sity.className="err";
		ok=false;
	}else{
		document.all.td_sity.className="text";
	}
	if(ok){
		step(2);
	}else{
		document.all.td_err.style.display="block";
	}
}

function recall(){
	//проверка правильности положения во фрейме
	if(self.parent.document==self.document){
		document.location="..";
	}
}