
var check_timeout = 1;

$(document).ready(function(){
	
	$('span#focus_pokus').css({'color':'#0066FF','text-decoration':'underline','cursor':'hand'});
	$('span#focus_pokus').bind('click',function(){
		$('input#login_top').focus();										
	});
	
	
	
	$('input.recount').bind('keyup',function(){
		bind_recount_action($(this).attr('lang'), $(this).val());
	})
	
						   
});

function bind_recount_action(add_id, prod_count){
		recount();
		JsHttpRequest.query(
            'hr_gate.php?test=500&r='+Math.random(),
            {
                'sp': "prod_in_basket",
				'add_id': add_id,
				'prod_count': prod_count
            },
           function(result, errors) {
                //alert('d');
				if (result) {
					if (result.count_prod) update_count_basket(result.count_prod, result.total_sum);
//					alert(result.total_sum);
					
                }
				if (errors) alert(errors);
				
            },
            true  //disable caching
      	);
		check_timeout = 1;
}



function recount(){
	var total_price = 0;
	var total_weight = 0;
	$('input.recount').each(function(){
		var id = $(this).attr('id').substring(4);
		var price = parseFloat($('label#price_'+id).text());
		var reg=/(\D*)/gi;
		var count = Math.abs($(this).val().replace(reg, ""));
		var weight = parseFloat($('label#weight_'+id).text());
		if (count>1000) count = 1000;
		$(this).val(Math.abs(count));
		var tw = parseInt(count*weight*1000)/1000
		var cost = parseInt(count*price*100)/100;
		$('label#cost_'+id).html(cost);	
		$('label#tw_'+id).html(tw);	
		total_price += cost;
		total_weight += tw;
	})
	//alert(parseInt(total_weight*1000)/1000);
	$('label#total_weight').text(parseInt(total_weight*1000)/1000);
	$('label#total').text(parseInt(total_price*100)/100);

	if (check_timeout == 0) return;
		check_timeout = 0;
	//alert('k');
	
}


function test_timeout(){
	
	
}

function update_count_basket(count, sum){
	
	if (count && $('span#cart_count')) {
		$('span#cart_count').html((count>0)?count+' øò.':'ïóñòî');
		$('span#cart_sum').html(sum);

		/*$('span#cart_count').attr('class','cart_top_title_hl');*/
	}
	if (count && window.opener && !window.opener.closed){
		//alert('opener  ['+count+' øò.]');
		window.opener.$('span#cart_count').html((count>0)?count+' øò.':'ïóñòî');
		window.opener.$('span#cart_sum').html(sum);
		/*window.opener.$('td.cart_top_title').attr('class','cart_top_title_hl');*/
	}
}






var d = new Array(0,0,0); // cumul 

var d_sum = new Array(500,2500,5000);

var act_d = 0;

var act_low_limit = 500;



// JavaScript Document

function recount_(form)

 {

	 var i=0;

	 var is_object;

	 var sum=0, cost, cumul_d = 0;

	 if (typeof(form)== 'undefined') return false;

	 var tmp_str, tmp_el, el_name;

	 var handler_mask = "qty_";

	 

		for (var i = 0; i < form.elements.length; i++) {

			if (form.elements[i].type == "text") {

				el_name = form.elements[i].name;

				index = el_name.indexOf(handler_mask);

				if (index>-1) {

					id_item = el_name.substr(index+handler_mask.length);

					tmp_el = getElement('price_'+id_item);

//					 alert(typeof(tmp_el));

	 				if (typeof(tmp_el)!= 'undefined') {

						 var price = tmp_el.innerHTML;

//						 alert(price);

//						 var oQty = form["qty_"+i];

						 var qty = form.elements[i].value;

						 cost = Math.floor(price*100)*qty/100;

//						 if (form["get_item_"+i].checked) 

						 sum = sum + cost;

						 tmp_el = getElement('cost_'+id_item);

	 					 if (typeof(tmp_el)!= 'undefined') tmp_el.innerHTML = cost;

						 

//						 window["cost_"+i].innerText = cost;

					}

				}

			}

		}	 

	/* do

	  {

		 i++;

		 is_object=true;

//		 qty = document.f1["qty_".i];

		 var oPrice = window["price_"+i];

		 if (oPrice) 

		 	{

				 var price = oPrice.innerText;

				 var oQty = form["qty_"+i];

				 var qty = oQty.value;

				 cost = Math.floor(price*100)*qty/100;

				 if (form["get_item_"+i].checked) sum = sum + cost;

 				 window["cost_"+i].innerText = cost;

			}

		 	else{is_object=false;}

		 if (i>100) is_object=false;

		   

	  }

	 while(is_object);

	 */

	if (sum > 0) {

		for (var j = 0; j < d.length; j++) 

			if (sum < d_sum[j]) {

				break;

			} else {cumul_d = d[j];}

	}else{sum=0;cumul_d=0;}



	 tmp_el = getElement('discount');

	 if (typeof(tmp_el)!= 'undefined') tmp_el.innerHTML =  cumul_d;



	 tmp_el = getElement('total');

	if (sum > act_low_limit) {

		tmp_el.innerHTML = Math.floor(sum*(1-(cumul_d+act_d)/100));

	} else {

		tmp_el.innerHTML = Math.floor(sum*(1-(cumul_d)/100));

	}

//	alert(getElement('total').innerHTML);

//	 var oQty = form["qty_"+i];

	return true;

	 

 }

 

function pt_correction(obj,k)

 {

	 var form = obj.form;

	 var elements = new Array("lastName","organization","legalAddress","VATpn","TIN");

	 if (!k) k=2;

//	 if (obj.value=='1') window["calc_total"].innerText = form.total*k;

	 if (obj.value=='1') 

	 	{

			getElement('calc_total').innerHTML = Math.floor(form.total.value*k);

			getElement('cl_req').style.display = 'block';

//			window["cl_req"].style.visibility = 'visible';

			getElement('cl_star').style.visibility = 'visible';

//			form.lastName.id = "lastName_";

//			form.organization.id = form.organization.name+"_";

			for (i = 0; i < elements.length; i++) {

				form[elements[i]].id = form[elements[i]].name+"_";

			}



			

		}

	   else

	    {

			getElement('calc_total').innerHTML = form.total.value;

//			window["cl_req"].style.visibility = 'hidden';

			getElement('cl_star').style.visibility = 'hidden';

//			form.lastName.id = "lastName";

			getElement('cl_req').style.display = 'none';

//			form.organization.id = form.organization.name;

			for (i = 0; i < elements.length; i++) {

				form[elements[i]].id = form[elements[i]].name;

			}

		}

 }


