/***************
 *             *
 *   JS menu   *
 *             *
 ***************/
var menu_li = document.getElementById('menu').getElementsByTagName('li');
var menu_length = menu_li.length;
for (i = 0; i < menu_length; i++) {
    if (menu_li[i].className.indexOf('active') != -1) {
        if (i) {
            menu_li[i - 1].style.backgroundImage = 'none';
        }
    }
}
if(readCookie('fio') != null){
	$('fio').value = readCookie('fio');
}
if(readCookie('email') != null){
	$('email').value = readCookie('email');
}
if(readCookie('phone') != null){
	$('phone').value = readCookie('phone');
}
if(readCookie('address') != null){
	$('address').value = readCookie('address');
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+escape(value)+expires+"; path=/";
}
function readCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
function checkForm(){
	var error = false;
	if($F('qty') == '' || !$F('qty').match(/^\d+$/)){
		$('qty').value = '1';
		error = true;
	}
	if($F('fio').length < 2){
		error = true;
	}
	if($F('email').strip().length < 2){
		error = true;
	}
	if($F('phone').strip().length < 2){
		error = true;
	}
	if($F('address').strip().length < 2){
		error = true;
	}
	if($F('gname').strip() == ''){
		error = true;
	}
	if(error){
		$('message').setStyle({
			color: '#f00',
			fontWeight: 'bold'
		});
	}else{
		createCookie('fio', $F('fio'), 7);
		createCookie('email', $F('email'), 7);
		createCookie('phone', $F('phone'), 7);
		createCookie('address', $F('address'), 7);
		new Ajax.Request('/lib/send_order.php?fio=' + $F('fio') + '&email=' + $F('email') + '&phone=' + $F('phone') + '&address=' + $F('address') + '&gid=' + $F('gname') + '&qty=' + $F('qty'), {
						 method: 'get',
						 onLoading: function(){
							 $('loader').show();
						 },
						 onSuccess: function(transport){
							 if(transport.responseText == 'ok'){
								 $('order_form').hide();
								 $('loader').hide();
								 $('result').update('Ваша заявка отправлена!').show();
								 
							 } else {
								 $('result').update('Возникала ошибка. Пожалуйста, попробуйте еще раз!').show();
							 }
							 setTimeout('hideOrderForm()', 1500);
						 }
		});
	}
}
function showOrderForm(gid){
	$('gray_layer').show();
	$('popup_container').show();
	$('loader').show();
	new Ajax.Request('/lib/show_good.php?gid=' + gid, {
		method: 'get',
		onSuccess: function(transport){
			if(transport.responseText != 'error'){
				var good = eval(transport.responseText);
				$('good_title').update(good.good_name);
				$('good_description').update(good.good_description);
				$('price').update(good.good_price + ' руб.');
				$('gname').value = good.good_id;
				$('loader').hide();
				$('order_form').show();
			} else {
					hideOrderForm();
			}
		}
	});
}
function hideOrderForm(){
	$('message').setStyle({
		fontWeight: 'normal',
		color: '#000'
	});
	$('order_form').hide();
	$('loader').hide();
	$('result').hide();
	$('gray_layer').hide();
	$('popup_container').hide();
	$('good_title').update('');
	$('good_description').update('');
	$('price').update('');
	$('gname').value = '';
	$('qty').value = '1';
}
function showImage (url){
	$('gray_layer').show();
	$('image_popup').setStyle({
		top: document.viewport.getScrollOffsets().top + 50 + 'px'
	});
	$('image_area').update('<a href="#" onclick="hideImage(); return false;"><img src="' + url + '" /></a><p style="margin-bottom: 0px; "><a href="#" onclick="hideImage(); return false;">Закрыть</a></p>');
	$('image_wrapper').show();
}
function hideImage (){
	$('gray_layer').hide();
	$('image_wrapper').hide();
	$('image_area').update('');
}
