function ravno() {
//var h = document.getElementById("global").offsetHeight;
//document.getElementById("globallin").style.height = h - 196 - 113 + 'px';
}

onload = run;
function run() {
	ravno();
}

function platformSubmenuOpen(platform_id) {
	var i = 1;
	while(document.getElementById('platform_submenu_'+ i)) {
		document.getElementById('platform_submenu_'+ i).style.display = "none";
		document.getElementById('platform_menu_'+ i).setAttribute("class", false);
		i++;
	}
	document.getElementById('platform_submenu_'+platform_id).style.display = "block";
	document.getElementById('platform_menu_'+ platform_id).setAttribute("class", "open");
	
	ravno();
}

var req;
var total_prod;

function makeRequest(url, backFunction) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = backFunction;
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = backFunction;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function sort(order_by, dir_id) {
}

function cart_up_quantity(prod_id) {
	prod_quantity = parseInt(document.getElementById('prod_quantity_' + prod_id).value);
	if(prod_quantity > 0) {
		prod_quantity++;
	} else {
		prod_quantity = 1;
	}
	document.getElementById('prod_quantity_' + prod_id).value = prod_quantity;
}

function cart_down_quantity(prod_id) {
	prod_quantity = parseInt(document.getElementById('prod_quantity_' + prod_id).value);
	if(prod_quantity > 1) {
		prod_quantity--;
	} else {
		prod_quantity = 1;
	}
	document.getElementById('prod_quantity_' + prod_id).value = prod_quantity;
}

function cart_change_quantity(prod_id, is_plus) {
	prod_quantity = parseInt(document.getElementById('prod_quantity_' + prod_id).value);
	if(prod_quantity > 0) {
		if(is_plus == 0 && prod_quantity > 1) {
			prod_quantity--;
		}
		if(is_plus == 1) {
			prod_quantity++;
		}
	} else {
		prod_quantity = 1;
	}
	document.getElementById('prod_quantity_' + prod_id).value = prod_quantity;
	makeRequest('/cart.php?functionName=changeProductQantity&prod_id=' + prod_id + '&quantity=' + prod_quantity, ajax_none);
}

function cart_check_prod_quantity(prod_id) {
	prod_quantity = parseInt(document.getElementById('prod_quantity_' + prod_id).value);
	if(prod_quantity > 0) {
	} else {
		prod_quantity = 1;
	}
	makeRequest('/cart.php?functionName=changeProductQantity&prod_id=' + prod_id + '&quantity=' + prod_quantity, ajax_none);
}

function ajax_none() {
}

function check_prod_quantity(prod_id) {
	prod_quantity = parseInt(document.getElementById('prod_quantity_' + prod_id).value);
	if(prod_quantity > 0) {
	} else {
		prod_quantity = 1;
	}
	document.getElementById('prod_quantity_' + prod_id).value = prod_quantity;
}

function addToCart(prod_id, quantity) {
	if(prod_id > 0 && quantity > 0) {
		document.getElementById('zag_ready_' + prod_id).style.display = "block";
		window.setTimeout(function() {document.getElementById('zag_ready_' + prod_id).style.display = 'none'}, 3000);
		makeRequest('/cart.php?functionName=addToCart&prod_id=' + prod_id + '&quantity=' + quantity, ajax_addToCart);
		total_prod += quantity;
	}
	return false;
}

function show_top_cart() {
	if(total_prod < 1) return;
	
	document.getElementById('cart_top_details').style.display='block';
}

function ajax_addToCart() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var the_object;
			the_object = eval("(" + req.responseText + ")");
			
//			document.getElementById('zag_ready_' + the_object.prod_id).style.display = "none";
//			updateTopCartCounter(the_object.TotalCartProducts);
			
			document.getElementById('cart_top_quantity').innerHTML = the_object.quantity;
			document.getElementById('cart_top_price').innerHTML = the_object.price;
			
			if(the_object.is_new == 1) {
				var model = document.createElement('div');
				model.setAttribute((document.all ? 'className' : 'class'), 'model'); 
				model.innerHTML = the_object.cut_model;
				if((the_object.model).length > 50) model.innerHTML += '...';
				var prod_price = document.createElement('div');
				prod_price.setAttribute('id', 'cart_top_product_list_price_' + the_object.prod_id); 
				prod_price.setAttribute((document.all ? 'className' : 'class'), 'price'); 
				prod_price.innerHTML = the_object.prod_price;
				var prod_quantity = document.createElement('div');
				prod_quantity.setAttribute('id', 'cart_top_product_list_quantity_' + the_object.prod_id); 
				prod_quantity.setAttribute((document.all ? 'className' : 'class'), 'quantity'); 
				prod_quantity.innerHTML = the_object.prod_quantity;
				document.getElementById('cart_top_product_list').appendChild(model);
				document.getElementById('cart_top_product_list').appendChild(prod_price);
				document.getElementById('cart_top_product_list').appendChild(prod_quantity);
			} else {
				if(the_object.prod_id > 0) {
					document.getElementById('cart_top_product_list_price_' + the_object.prod_id).innerHTML = the_object.prod_price;
					document.getElementById('cart_top_product_list_quantity_' + the_object.prod_id).innerHTML = the_object.prod_quantity;
				}
			}
			
			if(the_object.quantity > 0) {
				document.getElementById('cart_top').setAttribute('onMouseover', 'javascript:document.getElementById(\'cart_top_details\').style.display=\'block\'');
			}
		} else {
//          alert('There was a problem with the request.');
		}
	}
}

function removeFromCart(prod_id) {
	if(prod_id > 0) {
		makeRequest('/cart.php?functionName=removeFromCart&prod_id=' + prod_id, ajax_removeFromCart);
	}
	return false;
}

function ajax_removeFromCart() {
	if (req.readyState == 4) {
		if (req.status == 200) {	
			location.href = "/cart.php";
		}
	}
}

function makeOrder() {
	var error = document.getElementById('error');
	
	var fio = document.getElementById('fio');
	var address = document.getElementById('address');
	var phone1 = document.getElementById('phone1');
	var phone2 = document.getElementById('phone2');
	var email = document.getElementById('email');
	var additional = document.getElementById('additional');
	fio.value = trim(fio.value, ' ');
	address.value = trim(address.value, ' ');
	phone1.value = trim(phone1.value, ' ');
	phone2.value = trim(phone2.value, ' ');
	email.value = trim(email.value, ' ');
	additional.value = trim(additional.value, ' ');
	if((fio.value).length <= 2) {
		error.innerHTML = "Заполните поле Ф.И.О.";
		fio.focus();
		return false;
	}
	if((address.value).length <= 2) {
		error.innerHTML = "Заполните поле Адрес доставки";
		address.focus();
		return false;
	}
	if((phone1.value).length <= 2) {
		error.innerHTML = "Заполните поле Контактный телефон 1";
		phone1.focus();
		return false;
	}
	makeRequest('/cart.php?functionName=makeOrder&fio=' + encodeURIComponent(fio.value) + '&address=' + encodeURIComponent(address.value) + '&phone1=' + encodeURIComponent(phone1.value) + '&phone2=' + encodeURIComponent(phone2.value) + '&email=' + encodeURIComponent(email.value) + '&additional=' + encodeURIComponent(additional.value), ajax_makeOrder);
}

function ajax_makeOrder() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var the_object;
			the_object = eval("(" + req.responseText + ")");
			order_id = the_object.order_id;
			
			if(order_id > 0) {
				document.getElementById('right_col').innerHTML = '<h1>Корзина</h1><a name="makeOrderComplite"></a><div style="margin: 50px; padding: 30px; border: 1px solid #D8D8D8; font-weight: bold; color: #323232;"><span style="color:#2F9A00;">Ваш заказ успешно оформлен!</span><br />Номер вашего заказа ' + order_id + '<br />Наши операторы свяжутся с Вами в ближайшее время.<br />Спасибо за то, что Вы обратились к нам!<br /></div></div><div class="clr"></div>';
				document.getElementById('cart_top_quantity').innerHTML = "0&nbsp;шт.";
				document.getElementById('cart_top_price').innerHTML = "0&nbsp;руб.";
			} else {
				document.getElementById('right_col').innerHTML = '<h1>Корзина</h1><a name="makeOrderComplite"></a><div style="margin: 50px; padding: 30px; border: 1px solid #D8D8D8; font-weight: bold; color: #323232;"><span style="color: red;">Ошибка при оформлении заказа!</span><br />Вернитесь назад и попробуйте еще раз или позвоните нашим операторам.<br /></div></div><div class="clr"></div>';
			}
			location.href="#makeOrderComplite";
		} else {
//          alert('There was a problem with the request.');
		}
	}
}

function curtainOff() {
	document.getElementById('window').style.display = "none";
}

function change_scr(scr_changer, ScreenID, width, height) {
	if(scr_changer != 1) {
		document.getElementById('screen_prev').onclick = document.getElementById("scr_changer_" + (scr_changer - 1)).onclick;
		document.getElementById('screen_prev').style.border = "1px solid #CDCBCC";
		document.getElementById('screen_prev').style.cursor = "pointer";
	} else {
		document.getElementById('screen_prev').onclick = 'return false;';
		document.getElementById('screen_prev').style.border = "1px solid #FFF";
		document.getElementById('screen_prev').style.cursor = "auto";
	}
	i = 1;
	while(document.getElementById('scr_changer_' + i)) {
		document.getElementById('scr_changer_' + i).style.fontSize = '12px';
		i++;
	}
	document.getElementById('scr_changer_' + scr_changer).style.fontSize = '14px';

	if((scr_changer + 1) != i) {
		document.getElementById('screen_next').onclick = document.getElementById("scr_changer_" + (scr_changer + 1)).onclick;
		document.getElementById('screen_next').style.border = "1px solid #CDCBCC";
		document.getElementById('screen_next').style.cursor = "pointer";
	} else {
		document.getElementById('screen_next').onclick = 'return false;';
		document.getElementById('screen_next').style.border = "1px solid #FFF";
		document.getElementById('screen_next').style.cursor = "auto";
	}
	
	document.getElementById('window').style.display = "block";
	
	if(width > 470) {
		height = height * 470 / width;
		width = 470;
	}
	
	document.getElementById('big_src').src = "/prod_screen.php?screenID=" + ScreenID + "&newwidth=470";
	document.getElementById('big_src').width = width;
	document.getElementById('big_src').height = height;
	document.getElementById('window').style.width = parseInt(width + 30) + 'px';
	document.getElementById('window').style.height = parseInt(height + 35) + 'px';
	return false;
}

function sendContact() {
	var fio = document.getElementById('fio');
	var email = document.getElementById('email');
	var address = document.getElementById('address');
	var phone = document.getElementById('phone');
	var message = document.getElementById('message');
	
	var error = document.getElementById('error');

	if((fio.value).length <= 2) {
		error.innerHTML = "Заполните поле Имя";
		error.style.display = "block";
		fio.focus();
		return false;
	}
	if((message.value).length <= 2) {
		error.innerHTML = "Заполните поле Сообщение";
		error.style.display = "block";
		message.focus();
		return false;
	}
	
	makeRequest('/contact.php?functionName=sendContact'
					+ '&fio=' + encodeURIComponent(fio.value) 
					+ '&email='+ encodeURIComponent(email.value) 
					+ '&address='+ encodeURIComponent(address.value) 
					+ '&phone='+ encodeURIComponent(phone.value) 
					+ '&message='+ encodeURIComponent(message.value)	
				, ajax_sendContact);
	
	document.getElementById('sendForm').style.display = 'none';
	document.getElementById('sendFormThanks').style.display = 'block';
}

function ajax_sendContact() {
	
}