function set_html(id,text)
{
	var buf = document.getElementById(id);
	if (buf) buf.innerHTML = text;
}

function set_src(id,src)
{
	var buf = document.getElementById(id);
	if (buf) buf.src = src;
}

function set_href(id,href)
{
	var buf = document.getElementById(id);
	if (buf) buf.href = href;
}

function set_value(id,value)
{
	var buf = document.getElementById(id);
	if (buf) buf.value = value;
}

function get_value(id)
{
	var buf = document.getElementById(id);
	if (buf) return buf.value;
	return false;
}

function ViewForgotDiv()
{
	el = document.getElementById('divforgot');
	if (el.style.display == 'none')
		el.style.display  = "block";
	else	
		el.style.display  = "none";
}

function ChangeLang()
{
	document.changeLang.submit();
}

function MakeOn(url)
{
	location.href=url;
}

function DopImg()
{
	targetId="dopimg";
	targetElement = document.getElementById(targetId);
	if (targetElement.style.display == "none")
	        targetElement.style.display= "";
	else targetElement.style.display = "none";
	location.href = "#dopimg";
}

function open_window(url)
{
	cwin = window.open(url,"attach","width=350,height=400,toolbar=no,resizable=yes");
}

function ViewResume(id)
{
	var targetId = "res"+id;
	var buf = document.getElementById(targetId);
	if (buf.style.visibility == "")
	{
		buf.style.visibility = "hidden";
		buf.style.display = "none";		
	}
	else 
	{
		buf.style.visibility = "";
		buf.style.display = "";		
	}
}

function set_visible(id,visible)
{
	var buf = document.getElementById(id);
	if (!buf) return;
	if (visible)
	{
		buf.style.visibility	= "";
		buf.style.display	= "";		
	}
	else 
	{
		buf.style.visibility	= "hidden";
		buf.style.display	= "none";		
	}
}

var done = 0;
function Upload(id, module)
{
	document.getElementById('bs').disabled = true;
	document.getElementById('br').disabled = true;
	document.getElementById('info_'+id).innerHTML = "Loading...";
	done = 1;
	var upl = document.getElementById(id);
	setTimeout('checkUpload(\''+id+'\')', 1000);
	JsHttpRequest.query('include/upload.php',{upl: upl, module: module}, 
	function(result, errors) {UploadDone(result, id);}, true);
	return false;
}

function UploadDone(result, id)
{
	done = 0;
	var temp = new Array();
	temp = result.split('#');
	var res = document.getElementById('info_'+id);
	if (temp[0] == "-2")
	{
		alert('Файл с таким именем уже существует на сервере! Будет подключен существующий');	
		res.innerHTML = 'Файл с таким именем уже существует на сервере! Будет подключен существующий';
		document.getElementById('hidden_'+id).value = temp[1];		
	}
	else if (temp[0] == "-1")
	{
		alert('При загрузке файла на сервер возникла ошибка! Попробуйте еще раз!');	
		res.innerHTML = 'При загрузке файла на сервер возникла ошибка! Попробуйте еще раз!';
	}
	else
	{	
		document.getElementById('hidden_'+id).value = temp[1];		
		res.innerHTML = 'Файл успешно загружен!';
	}
	document.getElementById('bs').disabled = false;
	document.getElementById('br').disabled = false;	
}

function checkUpload(id)
{
	JsHttpRequest.query('include/checkUpload.php',{}, 
	function(result, errors) {checkUploadDone(result, id);}, true);
	return false;
}

function checkUploadDone(result, id)
{
	if (done == 1)
	{
		document.getElementById('info_'+id).innerHTML = result;	
		setTimeout('checkUpload(\''+id+'\')', 2000);
	}
}

function clearFile()
{
	var f = document.getElementById('form1');
	for (var i=0;i<f.elements.length; i++) 
	{ 		
		if (f.elements[i].type == "file")
		{
			var temp = new Array();
			temp = f.elements[i].name.split('_');
			if (temp[0] == "file")
				f.elements[i].disabled = true;				
		}
	}
}

function ShowDiv(id)
{
	var buf = document.getElementById(id);
	if (buf.style.display == "none")
		buf.style.display = "";
	else buf.style.display = "none";
}

function update_list(result)
{
	if (result)
	{
		set_html('vkladka_'+result['sel'],result['vkladka']);
		set_html('list_'+result['sel'],result['data']);
	}
}

function set_list(pid,sel)
{
	set_html('div_'+pid,'<img src="/images/base/loading.gif">');
	JsHttpRequest.query(
		'/include/core/ajax_list.php',
		{pid:pid,sel:sel},
		function(result, errors) {update_list(result);},
		true);
}

function write_div(id,result)
{
	if (result) set_html(id,result);
}

function wait_div(id)
{
	write_div(id,'<select class="select" style="width:100%" disabled><option value=0>Идёт обновление</option></select>');
}

function set_marka(marka)
{
	wait_div('div_models');
	wait_div('div_years');
	wait_div('div_volumes');
	JsHttpRequest.query(
		'/include/core/ajax_auto.php',
		{action:'model',marka:marka},
		function(result, errors)
		{
			write_div('div_models',result[1]);
			write_div('div_years',result[2]);
			write_div('div_volumes',result[3]);
		},
		true);
}

function set_model(model)
{
	wait_div('div_years');
	wait_div('div_volumes');
	marka	= get_value('auto_marka');
	JsHttpRequest.query(
		'/include/core/ajax_auto.php',
		{action:'year',marka:marka,model:model},
		function(result, errors)
		{
			write_div('div_years',result[2]);
			write_div('div_volumes',result[3]);
		},
		true);
}

function set_year(year)
{
	wait_div('div_volumes');
	marka	= get_value('auto_marka');
	model	= get_value('auto_model');
	JsHttpRequest.query(
		'/include/core/ajax_auto.php',
		{action:'volume',marka:marka,model:model,year:year},
		function(result, errors)
		{
			write_div('div_volumes',result[3]);
		},
		true);
}

function set_seson(seson)
{
	document.results.shina_seson.value = seson;
	document.results.submit();
}

function set_page(page)
{
	document.results.page.value = page;
	document.results.submit();
}

function set_cpp(cpp)
{
	document.results.op.value = 'set_cpp';
	document.results.cpp.value = cpp;
	document.results.submit();
}

function set_orderby(orderby)
{
	document.results.op.value = 'set_orderby';
	document.results.orderby.value = orderby;
	document.results.submit();
}

function set_order(orderby)
{
	document.results.orderby.value = orderby;
	document.results.submit();
}

function show_shina(size,seson)
{
	document.search_shina.shina_seson.value = seson;
	document.search_shina.shina_size.value = size;
	document.search_shina.submit();
}

function update_shop(result)
{
	if (result)
	{
		set_html('basket',result['data']);
		alert('Товар добавлен в корзину!\nДля офомления покупки перейдите в корзину!\n\nВ вашей корзине уже '+result['count']+' единиц(ы) этого товара');
	}
}

function add_shina(id)
{
	var buff	= document.getElementById("count_"+id);
	var count	= buff ? buff.value : 1;
	JsHttpRequest.query(
		'/include/core/ajax_shop.php',
		{id:id,count:count,action:'insert_shina'},
		function(result, errors) {update_shop(result);},
		true);		
}

function add_diski(id)
{
	var buff	= document.getElementById("count_"+id);
	var count	= buff ? buff.value : 1;
	JsHttpRequest.query(
		'/include/core/ajax_shop.php',
		{id:id,count:count,action:'insert_disk'},
		function(result, errors) {update_shop(result);},
		true);		
}

function comment_page(module,id,page)
{
	set_html('wait_page','&nbsp;Пододжите, идёт обновление');
	JsHttpRequest.query(
		'/include/core/ajax_comment.php',
		{id:id,module:module,page:page},
		function(result, errors) {if (result) set_html('comment',result)},
		true);		
}

function comment_cpp(module,id,cpp)
{
	set_html('wait_cpp','Идёт обновление');
	return;
	JsHttpRequest.query(
		'/include/core/ajax_comment.php',
		{id:id,module:module,cpp:cpp},
		function(result, errors) {if (result) set_html('comment',result)},
		true);
}

function add_comment(module,id)
{
	var comm	= document.add_comm;
	if (!comm)	return;
	var comm_name  = comm.comm_name.value;
	var comm_email = comm.comm_email.value;
	var comm_text  = comm.comm_text.value;
	var comm_code  = comm.code.value;
	set_visible('wait',1);
	JsHttpRequest.query(
		'/include/core/ajax_comment.php?id='+id+'&module='+module,
		{action:'comment',comm_email:comm_email,comm_name:comm_name,comm_text:comm_text,comm_code:comm_code},
		function(result, errors) {if (result) set_html('add_comm',result);},
		true);
}

function update_type(result,mode)
{
	set_html('select_brands',result['sh_brand']);
	set_html('select_sizes',result['select_sizes']);
	set_html('select_types',result['select_types']);
	set_html('select_brands',result['select_brands']);
	if (mode)
	{
		set_visible('select_seson',0);
		set_html('mode','<a class="style1" href="javascript:select_type(0)">АВТО</a> / МОТО');
	}
	else
	{
		set_visible('select_seson',1);
		set_html('mode','АВТО / <a class="style1" href="javascript:select_type(1)">МОТО</a>');
	}
}

function select_type(mode)
{
	set_html('mode','<img src="/images/base/loading.gif" align="absmiddle" title="Подождите">');
	document.search_shina.shina_mode.value = mode;
	JsHttpRequest.query(
		'/include/core/ajax_select.php',
		{mode:mode},
		function(result, errors) {if (result) update_type(result,mode)},
		true);

}
