/* JavaScript file */

//========================================================
// Функции от Дрынкина

function show(file, width, height)
{
	file="docs/" + file + ".html";
	//window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes\no ,resizable=no");
	window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes ,resizable=no");
}

function ShowGallery( )
{
	var page = '/cms/index.php?view=category&catid=1';
	window.open( page , "_blank", "toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,titlebar=no, width=1000, height=920, top=10, left=50, resizable=yes");
}


function show_noscroll(file, width, height)
{
	file="/docs/" + file + ".html";
	//window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes\no ,resizable=no");
	window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no ,resizable=no");
}

function show_img(file, width, height)
{
	//file="docs/" + file + ".html";
	//window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes\no ,resizable=no");
	window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes ,resizable=no");
}

function show_maket_big(file, width, height)
{
	//file="docs/" + file + ".html";
	//window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes\no ,resizable=no");
	window.open(file, "_blank", "width="+width+",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes ,resizable=no");
	return false;
}

function close_window()
{
	window.close();
}

function div_border(obj, new_border_value)
{
	if (obj.style)
	{
		obj.style.border = new_border_value;
	}
}

function div_bg(obj, new_bg_value)
{
	if (obj.style)
	{
		obj.style.background = new_bg_value;
	}
}


function print_window()
{
	if ( confirm("Распечатать данную страницу?") )
	{
		window.print();
	}
}


function ShowSide( sid )
{
	window.open("/index.php?op=sidedb&side_id="+ sid , null, "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,titlebar=no, width=785, height=816, resizable=yes");
}

function ShowSideNoCart( sid )
{
	window.open("/index.php?op=sidedb&side_id="+ sid + "&no_cart=1" ,"Информация", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,titlebar=no, width=785, height=816, resizable=yes");
}

function ShowSideNoCartNoPrice( sid )
{
	window.open("/index.php?op=sidedb&side_id="+ sid + "&no_cart=1&price=2" ,"Информация", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,titlebar=no, width=785, height=816, resizable=yes");
}

function ShowSidesOnMap(sides)
{

	window.open("index.php?op=show_sides_on_map&sides=" + sides, 'Карта', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,titlebar=no, width=760,height=875,top=20,left=50,resizable=no');
}

function ShowSpec( spec_id )
{
	window.open("index.php?op=spec_programm&spec_id="+ spec_id , "Спецпредложение", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,titlebar=no, width=1000, height=920,top=10,left=50, resizable=yes");
}

function update_server_year_and_month(year, month)
{
	var cart_frame_obj = document.getElementById('cartFrame');
	
	if (cart_frame_obj)
	{
		cart_frame_obj.src = 'update_cart.php?s_period_year=' + year + '&s_period_month=' + month;
	}
	return false;
}



function side_to_cart(side_id, price, year, month, is_free)
{
	var cart_img_obj = document.getElementById('cart_' + side_id);
	var cart_frame_obj = document.getElementById('cartFrame');
	
	if (cart_img_obj)
	{
		//alert(cart_img_obj.src);
		if ( in_cart(side_id, year, month, price) )
		{
			del_from_cart(side_id, year, month, price);
			cart_img_obj.src = 'images/img/basket.gif';
			update_cart_div(-1, -1 * price, is_free);
			cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&price=' + price + '&action=del_side';

		}
		else
		{
			add_to_cart(side_id, year, month, price);
			cart_img_obj.src = 'images/img/basket2.gif';
			update_cart_div(1, price, is_free);
			cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&price=' + price + '&action=add_side';
		}
		
	}
	return false;
}

function side_to_cart_no_change_img(side_id, price, year, month, is_free)
{
	//var cart_img_obj = document.getElementById('cart_' + side_id);
	var cart_frame_obj = document.getElementById('cartFrame');
	//alert(cart_frame_obj);
	if (cart_frame_obj)
	{
		//alert(cart_img_obj.src);
		if ( in_cart(side_id, year, month, price) )
		{
			del_from_cart(side_id, year, month, price);
			
			update_cart_div(-1, -1 * price, is_free);
			cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&price=' + price + '&action=del_side';

		}
		else
		{
			add_to_cart(side_id, year, month, price);
			update_cart_div(1, price, is_free);
			cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&price=' + price + '&action=add_side';
		}
		
	}
	return false;
}


function ks_side_to_cart(side_id, price, year, month, is_free)
{
	var cart_img_obj = document.getElementById('cart_' + side_id);
	
	var fw = window.top;

	try {
		while ( fw.opener )
		{
			fw = fw.opener;
		}
	}
	catch (e) 
	{
		
	}

	//alert(fw);
	
	if (cart_img_obj)
	{
		//alert(cart_img_obj.src);
		if ( fw.in_cart(side_id, year, month, price) )
		{
			//del_from_cart(side_id, year, month, price);
			cart_img_obj.src = 'images/img/basket.gif';
			//update_cart_div(-1, -1 * price);
			/*
			if (cart_frame_obj)
			{
				cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&price=' + price + '&action=del_side';
			}
			*/

		}
		else
		{
			//add_to_cart(side_id, year, month, price);
			cart_img_obj.src = 'images/img/basket2.gif';
			//update_cart_div(1, price);
			/*
			if (cart_frame_obj)
			{
				cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&price=' + price + '&action=add_side';
			}
			*/
		}
		fw.side_to_cart_no_change_img(side_id, price, year, month, is_free);
	}
	return false;
}

function spec_to_cart(spec_id)
{
	var cart_img_obj = document.getElementById('cart_' + spec_id);
	var cart_frame_obj = document.getElementById('cartFrame');
	
	if (cart_img_obj)
	{
		//alert(cart_img_obj.src);
		if ( in_spec_cart(spec_id) )
		{
			del_from_spec_cart(spec_id);
			cart_img_obj.src = 'images/img/basket.gif';
			update_spec_cart_div();
			cart_frame_obj.src = 'update_cart.php?spec_id=' + spec_id + '&action=del_spec';

		}
		else
		{
			add_to_spec_cart(spec_id);
			cart_img_obj.src = 'images/img/basket2.gif';
			update_spec_cart_div();
			cart_frame_obj.src = 'update_cart.php?spec_id=' + spec_id + '&action=add_spec';
		}
		
	}
	return false;
}


function add_remove_service(side_id, service_key, year, month)
{
	//alert(side_id);
	var cart_frame_obj = document.getElementById('cartFrame');
	//alert(cart_frame_obj);
	
	if ( in_cart_service(side_id, year, month, service_key) )
	{
		del_service_from_cart(side_id, year, month, service_key);
		cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&service_key=' + service_key + '&action=del_service';
	}
	else
	{
		add_service_to_cart(side_id, year, month, service_key);
		cart_frame_obj.src = 'update_cart.php?side_id=' + side_id + '&year=' + year + '&month=' + month + '&service_key=' + service_key + '&action=add_service';
	}
	
	return true;
}


function copyObj_my(dst, src)
{
	for (var i in src)
	if (typeof src[i] == 'object')
	{
		dst[i] = new Object;
		copyObj_my(dst[i], src[i]);
	}
	else
		dst[i] = src[i];
}

function d(str)
{
	var debug_div_obj = document.getElementById('debug');
	if ( debug_div_obj )
	{
		debug_div_obj.innerHTML += str + '<br>';
	}
}

//========================================================
// Для работы онлайн заказа

function change_online_city(obj)
{
	if (obj)
	{
		var type_param = '';
		if ( obj.options[obj.selectedIndex].value == '2' )
		{
			type_param = '&s_type=*';
		}
		document.location = 'index.php?op=online&s_cityid=' +  obj.options[obj.selectedIndex].value + type_param;
	}
	return false;
}


function change_online_type(obj)
{
	if (obj)
	{
		var s_cityid_param = '';
		
		var s_cityid_obj = document.getElementById('s_cityid');
		
		//alert(s_cityid_obj);
		
		//alert(s_cityid_obj.selectedIndex);
		
		if (s_cityid_obj && s_cityid_obj.options)
		{
			s_cityid_param = '&s_cityid=' + s_cityid_obj.options[s_cityid_obj.selectedIndex].value;
		}
		else if ( document.all && document.forms.right_online_form )
		{
			s_cityid_obj = document.forms.right_online_form.s_cityid;
			if (s_cityid_obj && s_cityid_obj.options)
			{
				s_cityid_param = '&s_cityid=' + s_cityid_obj.options[s_cityid_obj.selectedIndex].value;
			}
		}
		
		document.location = 'index.php?op=online&s_type=' +  obj.options[obj.selectedIndex].value + s_cityid_param;
	}
	return false;
}

//========================================================
// Для работы с картой

function change_map_city(obj)
{
	if (obj)
	{
		var s_type_param = '';
		if ( obj.options[obj.selectedIndex].value == '2')
		{
			s_type_param = '&s_type=*';
		}
		document.location = 'index.php?op=map&s_cityid=' +  obj.options[obj.selectedIndex].value + s_type_param;
	}
	return false;
}

//========================================================
// Для работы голосования

function show_poll_pic(file)
{
	var width = 700;
	var height = 650;
	window.open( file, null, "width=" + width + ",height="+height+",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no ,resizable=no");
}

function confirm_vote()
{
	return window.confirm('Проголосовать второй раз будет невозможно!!! Вы уверены, что правильно выбрали оценки?');
}

//========================================================




//========================================================
// Фунции от ADV
var status;

function opens_el (el_id)
{
  el = document.getElementById(el_id);
  if (el)
    el.style.display = "block";
}
function close_el (el_id)
{
  el = document.getElementById(el_id);
  if (el)
    el.style.display = "none";
}
    


function showhide(obj)
{
	if (status == 1)
	{
		hideObject(obj);
	}
	else 
	{
		showObject(obj);
	}
}

function showObject(obj) 
{
    obj.className = "visible";
	status = 1;
}
function hideObject(obj) 
{
    obj.className = "hidden";
	status = 0;
}



function check_input(form_id)
{	
	var f_obj = document.getElementById(form_id);
	
	if (f_obj.name_new.value == "")
	{
		f_obj.name_new.style.border = "2px solid red";
		alert("Введите имя");
		return false;
	}
	
	if ( (f_obj.email_new.value.length < 4 ) )
	{
		//f_obj.phone_new.style.border = "2px solid red";
		f_obj.email_new.style.border = "2px solid red";
		alert("Введите координаты для связи!");
		return false;
	}
	
	return true;
}

function form_submit(form_id)
{	
	var f_obj = document.getElementById(form_id);
	
	if (f_obj)
	{
		return f_obj.submit();
	}
	
	return false;
}



//========================================================
//


function checkcontained(e)
{
	var iscontained=0;
	cur = ns6 ? e.target : event.srcElement;
	i=0;
	if ( cur.id == "foldheader" )
	{
		iscontained=1
	}
	else
	{
		while ( ns6 && cur.parentNode || (ie4 && cur.parentElement) )
		{
			if ( cur.id == "foldheader" || cur.id == "foldinglist" )
			{
				iscontained = (cur.id == "foldheader") ? 1 : 0;
				break;
			}
			
			cur = ns6 ? cur.parentNode : cur.parentElement;
		}
	}	
	
	if (iscontained)
	{
		var foldercontent=ns6? cur.nextSibling.nextSibling : cur.all.tags("UL")[0];
		if (foldercontent.style.display=="none")
		{
			foldercontent.style.display=""
		}
		else
		{
			foldercontent.style.display="none"
		}

	}
	
}


/*
var head="display:''";

var ns6 = document.getElementById && !document.all;
var ie4 = document.all && (navigator.userAgent.indexOf("Opera")) == -1;


if (ie4||ns6)
{
	document.onclick = checkcontained;
}
*/



/* calc */

function update_calc()
{
	var type = $('#calc_input input:radio:checked').val();
	var num = $('#calc_num').val();
	
	num = parseInt(num);
	
	var result_m = '';
	var result_v = '';
	var result = '';
	
	
	
	if (type == 1) // Бумага
	{
		result_m = 2.35 * num;
		result_v = 0.05 * Math.ceil(num / 10); /* размер паралипипеда  из 10 плакатов 1,25х0,20х0,20 (объем) = 0,05 м3 */
		result_v = result_v.toFixed(3);
	}
	else if(type == 2) // Винил
	{
		result_m = 10.35 * num;
		result_v = 0.0235 * num; /* объем 1 шт 0,5х0,47х 0,1м = 0,0235 м3 */
		result_v = result_v.toFixed(4);
	}
	
	result_m = result_m.toFixed(2);

	
	if ( result_m > 0 )
	{
		result += 'Масса: <b>' + result_m + ' кг</b>';
		result += '<br>Объем: <b>' + result_v + ' м<sup>3</sup> не менее чем</b>';
	}
	
	$('#calc_result').html(result);
	
}



function search_radius_sides(bid)
{
	var radius = $('#radius').val();
	radius = parseInt(radius);
	
	if (radius > 0 && radius < 10000)
	{
		$('#radius_search_result_loading').css('display', 'block');
		$('#radius_search_result').css('display', 'none');
		
		$.get('/get_radius_sides.php?bid='+bid+'&r='+radius, function(data){ 
			
			$('#radius_search_result').html(data);
			$('#radius_search_result').css('display', 'block');
			
			$('#radius_search_result_loading').css('display', 'none');
		});
		
	
	}
}


function update_preview_paper_border()
{
	var type = $('.preview_result input:radio:checked').val();
	
	if (type == 1) // Бумага
	{
		$('.paper_border').css('display', 'block');
	}
	else if(type == 2) // Винил
	{
		$('.paper_border').css('display', 'none');
	}
	
}

