var speed
var t
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function Add(tov) {	clearTimeout(t);	var goods;
    goods = getCookie("tovar");
    if (goods==null){
	  setCookie("tovar",tov, "", "/");
	  speed=4000;
	}
	else{		setCookie("tovar",goods+":"+tov, "", "/");
		speed=1500;
	}
	showNotice();
    	//window.location.href = "/perform.php";
}
function showNotice() {	var hs = document.documentElement.scrollTop;
	var h = window.screen.height;
	var w = (window.screen.width /2) - 247;
	var block = document.getElementById('note');
	block.style.visibility="visible";
	block.style.left = w+"px";
	var heig = hs+(h/3);
	block.style.top = heig +"px";
	t = setTimeout("hideNotice()", speed);
}
function hideNotice() {
	var block = document.getElementById('note');
	block.style.visibility="hidden";
}
function summa2(current, total)
{
	var total_sum = 0;
	if (current>0){
	  var num = document.getElementById('num'+current).value;
	  var price = document.getElementById('price'+current).value;
	  document.getElementById('sum'+current).innerHTML = num*price;
	}
	else{      for (var i = 1; i < total; i++) {
        var num = document.getElementById('num'+i).value;
	    var price = document.getElementById('price'+i).value;
	    document.getElementById('sum'+i).innerHTML = num*price;
	  }
	}
	for (var i = 1; i < total; i++) {	  num = document.getElementById('num'+i).value;
	  price = document.getElementById('price'+i).value;      total_sum = total_sum + num*price;
	}
	document.getElementById('total').innerHTML = total_sum;
	document.main.total_summa.value = total_sum;
}

$(document).ready(function(){

	$(".tov_num").bind('keyup', function(){
		summa(0);
	});

	$(".pred").bind('click', function(){
		summa(0);
	});

	// тех. помощь

	var loadImg = new Image(32,32);
    loadImg.src = "pic/loader.gif";

	$("#call_me").click(function(){    	if ( ($("#name_client").val().length<3) || ($("#name_client").val()=='Введите имя!') ){
        	$("#lbl_name").addClass('red');
        	$("#name_client").addClass('grey');
        	$("#name_client").val('Введите имя!');
        	return;
    	}
    	if ( $("#tel_client").val().length<7 || $("#tel_client").val()=='Введите телефон!'){
        	$("#lbl_tel").addClass('red');
        	$("#tel_client").addClass('grey');
        	$("#tel_client").val('Введите телефон!');
        	return;
    	}

    	$("#call_fields").fadeOut(200);
        $("#call_bar").append('<div id="call_loader" class="loader">&nbsp;&nbsp;Отправка...</div>');
    	$.ajax({
    		type: "POST",
			url: '_phpscript/aj/call_me.php',
			data: 'name='+$("#name_client").val()+'&phone='+$("#tel_client").val(),
			beforeSend: function(){			    $("#call_me").attr('disabled','disabled');
			},
			success: function(){
				$("#call_loader").hide();
				$("#call_bar").append('<div style="padding-top:20px">Ваша заявка успешно принята.<br />Ждите звонка!</div>')
			},
			error: function(){				$("#call_me").removeAttr('disabled');			    alert('К сожалению, прием звонков не возможен');
			}
		});
	})

	$("#name_client").click(function(){
	    if ($("#name_client").val()=='Введите имя!'){
	    	$("#name_client").val('');
	    	$("#name_client").removeClass('grey');
	    	$("#lbl_name").removeClass('red');
	    }
	})

	$("#tel_client").click(function(){
	    if ($("#tel_client").val()=='Введите телефон!'){
	    	$("#tel_client").val('');
	    	$("#tel_client").removeClass('grey');
	    	$("#lbl_tel").removeClass('red');
	    }
	})

});

function summa(current)
{

	var total=document.order.count.value;

    var radioSkidka = document.order.predoplata;
	for (var j=0; j < radioSkidka.length; j++){
    	if (radioSkidka[j].checked) break;
    }
    var skidka = arSkidka[j];

    var total_num = 0;
    var total_sum = 0;
	var total_skidka = 0;


	for (var i = 1; i <= total; i++) {

	  num = document.forms['order'].elements['num'+i].value;
	  price = document.forms['order'].elements['price'+i].value;

	  total_skidka=total_skidka+num*skidka;
      total_sum = total_sum + (num*price)-(num*skidka);
      total_num = total_num + num*1;

	}
	document.getElementById('total').innerHTML = total_sum;
	document.order.total_summa.value = total_sum;


	if (skidka>0) {
		$('#discount_val').html(total_skidka);
		$('#discount').show();
	}
	else{		$('#discount').hide();
	}


}