function eid(ename){
	var e_name = document.getElementById(ename);
	return e_name;
}

function aid(ename){
	var e_name = document.getElementById(ename).value;
	return e_name;
}

function addtocart(idnum){
	function newCls(tl){
		if(tl.responseText >= "1"){
			//alert(tl.responseText);
			eid('cart_size').innerHTML = tl.responseText;
		}else{
			//alert(tl.responseText);
			eid('cart_size').innerHTML = "0";
		}
	}

	ajax = new Ajax.Request('/session.php?id='+idnum, {
		method:'get',
		onComplete: newCls
	});
}



/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Mr J | http://www.huntingground.net/ */

function product_image(i,ii,a,aa) {
	document.getElementById(ii).src = i;
	//document.getElementById(a).href = aa;
}

scrollStep=2;
timerLeft="";
timerRight="";

function toLeft(id){
	document.getElementById(id).scrollLeft=0;
}

function scrollDivLeft(id){
	clearTimeout(timerRight);
	document.getElementById(id).scrollLeft+=scrollStep;
	timerRight=setTimeout("scrollDivLeft('"+id+"')",10);
}

function scrollDivRight(id){
	clearTimeout(timerLeft);
	document.getElementById(id).scrollLeft-=scrollStep;
	timerLeft=setTimeout("scrollDivRight('"+id+"')",10);
}

function toRight(id){
	document.getElementById(id).scrollLeft=document.getElementById(id).scrollWidth;
}

function stopMe(){
	clearTimeout(timerRight);
	clearTimeout(timerLeft);
}

function recalcTotal(){
	var i = 0;
	var totalsum = 0;
	var sum = 0;
	var quantity = 0;
	while(eid('price_'+i) != null){
		sum = eid("price_" + i).value;
		sum = Number(sum);
		quantity = eid('quantity_' + i).value;
		quantity_sum = Number(sum * quantity);
		totalsum = Number(totalsum + quantity_sum);
		eid("total_sum").innerHTML = totalsum.toFixed(2);
		//document.getElementById("sum_total").value = totalsum.toFixed(2);
		i++;
	}
}

function deleteOrderItem(id){
	function ajaxResult(tl){
		window.location = "/porychka";
	}

	new Ajax.Request('/order.php', { 
		method: 'post',
		postBody: 'action=delete&id=' + id,
		onSuccess: ajaxResult } );

}

function validateOrder(){
	errmsg = new Array();

	if(aid('p_name') == ""){
		errmsg.push("Моля попълнете Име и Фамилия");
	}
	if(aid('p_city') == ""){
		errmsg.push("Моля попълнете Град");
	}
	if(aid('p_phone') == ""){
		errmsg.push("Моля попълнете Телефон за връзка");
	}
	if(aid('p_oblast') == ""){
		errmsg.push("Моля попълнете Област");
	}
	if(aid('p_adres') == ""){
		errmsg.push("Моля попълнете Адрес");
	}
	if(aid('p_code') == ""){
		errmsg.push("Моля попълнете Пощенски код");
	}
	if(aid('p_email') == ""){
		errmsg.push("Моля попълнете Е-мейл");
	}else{
		var emailRE = /^([\w\.]+)\@([-a-z0-9\.]+)\.([\w\.]{2,6})$/i;
		if(emailRE.test(aid('p_email'))){
		}else{
			errmsg.push("Моля попълнете валиден Е-мейл адрес");
		}
	}

	/*
	if(aid('p_comment') == ""){
		errmsg.push("Моля попълнете ");
	}*/

	if(eid('lbl_2').checked == true){
		if(aid('c_name') == ""){
			errmsg.push("Моля попълнете име на фирмата");
		}
		if(aid('c_in') == ""){
			errmsg.push("Моля попълнете ИН по ЗДДС");
		}else{
			var inzdds = /^(BG)[0-9]{9,10}$/i;
			if(inzdds.test(aid('c_in'))){
			}else{
				errmsg.push("Моля попълнете валиден ИН по ЗДДС");
			}
		}

		if(aid('c_addr') == ""){
			errmsg.push("Моля попълнете Адрес на регистрация");
		}
		if(aid('c_mol') == ""){
			errmsg.push("Моля попълнете Материално Отговорно Лице на фирмата");
		}
	}

	if(errmsg != ""){
		eid('order_error_message').innerHTML = errmsg.join("<br/>\n");
		errmsg = new Array();
	}else{
		if(confirm("С натискането на бутона \"Ok\" Вие потвръждавате, че въведената от Вас информация е вярна.")){
			document.submitorder.submit();
		}
	}
}

function checkpricebox(){
	var regtest = /^[\d]+$/i;
	if(eid('price_filter_do').value != "" && eid('price_filter_ot').value != ""){
		if( regtest.test(eid("price_filter_do").value) && regtest.test(eid("price_filter_ot").value) ){
			if(eid('price_filter_do').value > 0){
				var price_raw = eid('priceraw').value;
				var pricea = eid("price_filter_ot").value;
				var priceb = eid("price_filter_do").value;
				var price = price_raw.replace("price0-0","price" + pricea + "-" + priceb);
				eid('pricefilter').href = price;
				$('price_filter_box').appear({ duration: 0.3 });
			}else{
				$('price_filter_box').fade({ duration: 0.3 });
			}
		}else{
			eid("price_filter_ot").value = "";
			eid("price_filter_do").value = "";
			alert("Моля използвайте само цифри");
		}
	}else{
		$('price_filter_box').fade({ duration: 0.3 });
	}
}
