// AJAX
function ajx_send(c_block, url, param){
	$(c_block).block();
	
	$.getJSON(url+'&hash=' + Math.random(),
		param,
		function(data){
			$(c_block).unblock();
			$(data['info']['pref']).empty();
			for (i = 1; i < data['info']['cnt'] ; i++){
				if(data['values'][i]['vals']){
					str = '<optgroup label="'+data['values'][i]['title']+'">';
					$(str).appendTo(data['info']['pref']);
					for (j = 0; j < data['values'][i]['vals'].length ; j++){
						$("<option></option>").attr("value", data['values'][i]['vals'][j]['id']).html('    '+data['values'][i]['vals'][j]['title']).appendTo(data['info']['pref']);
					}
					$("</optgroup>").appendTo(data['info']['pref']);
				}else{
					$("<option></option>").attr("value", data['values'][i]['id']).html(data['values'][i]['title']).appendTo(data['info']['pref']);
				}
			}
			$("<option></option>").attr("value", data['values'][0]['id']).html(data['values'][0]['title']).appendTo(data['info']['pref']);
		}
	);
}
// это для главной
function ajx_send2(c_block, url, param, div){
	$(c_block).block();
	
	$.getJSON(url+'&hash=' + Math.random(),
		param,
		function(data){
			$(c_block).unblock();
			$(data['info']['pref']+'_list').empty();
			$(data['info']['pref']).attr('value', 0);
			$(data['info']['pref']+'_title').html('Выбрать');
			$("<li></li>")
				.html('<a href="javascript:void(0);" onClick="$(\''+data['info']['pref']+'\').attr(\'value\', 0); $(\''+data['info']['pref']+'_title'+'\').html(\'Выбрать\'); hideId(\''+div+'\');">Выбрать</a>')
				.appendTo(data['info']['pref']+'_list');
			for (i = 1; i < data['info']['cnt'] ; i++){
					$("<li></li>")
						.html('<a href="javascript:void(0);" onClick="$(\''+data['info']['pref']+'\').attr(\'value\', '+data['values'][i]['id']+'); $(\''+data['info']['pref']+'_title'+'\').html(\''+data['values'][i]['title']+'\'); hideId(\''+div+'\');">'+data['values'][i]['title']+'</a>')
						.appendTo(data['info']['pref']+'_list');
			}
			
		}
	);
}

function ajx_send3(c_block, url, param, div){
	$(c_block).block();
	
	$.getJSON(url+'&hash=' + Math.random(),
		param,
		function(data){
			$(c_block).unblock();
			$(data['info']['pref']+'_list').empty();
			$(data['info']['pref']).attr('value', 0);
			$(data['info']['pref']+'_title').html('Выбрать');
			$("<li></li>")
				.html('<a href="javascript:void(0);" onClick="$(\'#cs_zip_list\').empty(); $(\'#cs_zip\').attr(\'value\', 0); $(\'#cs_zip_title\').html(\'Выбрать\'); $(\''+data['info']['pref']+'\').attr(\'value\', 0); $(\''+data['info']['pref']+'_title'+'\').html(\'Выбрать\'); hideId(\''+div+'\');">Выбрать</a>')
				.appendTo(data['info']['pref']+'_list');
			for (i = 1; i < data['info']['cnt'] ; i++){
					$("<li></li>")
						.html('<a href="javascript:void(0);" onClick="$(\''+data['info']['pref']+'\').attr(\'value\', '+data['values'][i]['id']+'); $(\''+data['info']['pref']+'_title'+'\').html(\''+data['values'][i]['title']+'\'); hideId(\''+div+'\');  ajx_send2(\'div.r_p\',\'/?tp=cs_zip&val='+data['values'][i]['id']+'\',\'\',\'popStoCity\');">'+data['values'][i]['title']+'</a>')
						.appendTo(data['info']['pref']+'_list');
			}
			
		}
	);
}

function ajx_captcha(c_block, name, length, width, height, align){
	$(c_block).load('/scripts/captcha.php?refresh=1',
		{
			name: name,
			length: length,
			width:width,
			height:height,
			align:align
		}
	);
	
}
function ajx_send_mail(c_block,url, param){
	$(c_block).html('Идет отправка сообщения');
	$.post(url,
		param,
		function(data){
			$(c_block).html(data);
		}
	);
}
function SetNull(item, c_block, str){
	$(item).empty();
	$("<option></option>")
		.attr("value", 0)
		.html(str)
		.appendTo(item);
	if(c_block != false)
	$(c_block).block();
}

var get = function(id) { return document.getElementById(id); }

document.onkeydown = function cmsSwitch(event)
{
    if (!document.getElementById) 
      return;
	if (window.event) 
      event = window.event; 
	
	if (event.ctrlKey){ 
		var key = (event.keyCode ? event.keyCode : (event.which ? event.which : null) ); 
		if (!key) 
         return; 

		var link = null; 
		if (key == 37) 
			link = get('next_page'); 
		else if (key == 39) 
			link = get('previous_page'); 
		if (link && link.href) 
			document.location = link.href; 
	}else{
		switch (event.keyCode) {
			case 120: window.open('/'); break;
			case 123: document.location.href = admin_uri; break;
		}
	}
}
function zoom(src, w, h) {
	var z_window = window.open('about:blank', z_window, 'left=' + ((screen.width - w)/2) + ',top=' + ((screen.height - h)/2 - 50) + ',width=' + (w+20) + ',height=' + (h+20));
	z_window.document.write('<title>Zoom</title><body style="margin:0px;cursor:pointer;" onClick="window.close()">');
	z_window.document.write('<table width="100%" height="100%"><tr><td align="center" valign="center">');
	z_window.document.write('<img src="' + src + '"></td></tr></table></body>');
	z_window.focus();
}

function PopUp(url) {
	var w = 800;
	var h = 700;
	var z_window = window.open(url, z_window, 'left=' + ((screen.width - w)/2) + ',top=' + ((screen.height - h)/2 - 50) + ',width=' + (w+20) + ',height=' + (h+80));
	z_window.focus();
}

function addBookmark(url, title)
{
  if (!url) url = location.href;
  if (!title) title = document.title;
  
  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement)
  {
    var a = document.createElement('A');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
  }
  else return false;
  
  return true;
}

//----------------

function fixPngImage(p) {
	var imgsrc;
	var trans = "/templates/images/spacer.gif";   
	if(typeof p.tested != undefined && !p.tested &&  typeof p.runtimeStyle != 'undefined') {
	   // retain image src
	   imgsrc = p.src;

	   // make sure the image is a PING
	   if ( /\.png$/.test( imgsrc.toLowerCase() ) ) {
	   	// If it is set it to a transparent image
		p.src = trans;
		// Set the runtime style to load the alpha image in the background
		p.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgsrc + "',sizingMethod='scale')";
		p.tested = true;
	   }
   }
}

function newchbox(chbox)
{
	if (chbox.className != "newcheckbox checked")
	{
		chbox.className+=" checked";
		var el = chbox.getElementsByTagName("INPUT");
		el[0].value ="checked";
	}
	else
	{
		chbox.className = "newcheckbox";
		var el = chbox.getElementsByTagName("INPUT");
		el[0].value ="";
	}
	return false;
}

function chbox(chbox)
{
	if (chbox.className != "checkbox checked")
	{
		chbox.className+=" checked";
		var el = chbox.getElementsByTagName("INPUT");
		el[0].value ="checked";
	}
	else
	{
		chbox.className = "checkbox";
		var el = chbox.getElementsByTagName("INPUT");
		el[0].value ="";
	}
	return false;
}

function formOpen()
{
	$("#autoriz").css({"display": 'block'}).animate({top: "0px"},600);
}

function formClose ()
{
	$("#autoriz").animate(
		{top: "-130px"},
		600,
		function(){
			$(this).css({"display": 'none'});
		});
}

function hintshow(par)
{
	var el = par.getElementsByTagName('div');
	el[0].style.display = 'block';
}
function hinthide(par)
{
	var el = par.getElementsByTagName('div');
	el[0].style.display = 'none';
}

///////////////////////

// асинхронный вызов функций //
function createIFrame() {
  var id = 'f' + Math.floor(Math.random() * 99999);
  var div = document.createElement('div');
  div.innerHTML = '<iframe style="display:none" src="about:blank" id="'+id+'" name="'+id+'" onload="sendComplete(\''+id+'\')"></iframe>';
  document.body.appendChild(div);
  return document.getElementById(id);
}

function sendForm(form, url, func, arg) {
  if (!document.createElement) return; // not supported
  if (typeof(form)=="string") form=document.getElementById(form);
  var frame=createIFrame();
  frame.onSendComplete = function() { func(arg, getIFrameXML(frame)); };
  var form_target = form.target;
  var form_action = form.action;
  
  
  form.setAttribute('target', frame.id);
  form.setAttribute('action', url);
  form.submit();
  
  form.setAttribute('target', form_target);
  form.setAttribute('action', form_action);
  
}

function sendComplete(id) {
  var iframe=document.getElementById(id);
  if (iframe.onSendComplete && typeof(iframe.onSendComplete) == 'function') iframe.onSendComplete();
}

function getIFrameXML(iframe) {
  var doc=iframe.contentDocument;
  if (!doc && iframe.contentWindow) doc=iframe.contentWindow.document;
  if (!doc) doc=window.frames[iframe.id].document;
  if (!doc) return null;
  if (doc.location=="about:blank") return null;
  if (doc.XMLDocument) doc=doc.XMLDocument;
  return doc;
}

var cnt=0;

function uploadComplete(element, doc) {
  if (!doc) return;
  if (typeof(element)=="string") element=get(element);
  element.innerHTML=doc.documentElement.firstChild.nodeValue;
  //alert(doc.documentElement.firstChild.nodeValue);
  
}
function uploadComplete2(element, doc) {
  if (!doc) return;
  if (typeof(element) == "string") element=get(element);
  element.innerHTML = doc.documentElement.firstChild.nodeValue;
  get('result_cc2').innerHTML = doc.documentElement.firstChild.nodeValue;
  //alert(doc.documentElement.firstChild.nodeValue);
  
}

function requestHitsForUrl(url) { 
	var params = $('#search_cc').serialize();
	$.get(url+'?hash=' + Math.random()+'&'+params,
		{param: 'hits'},
		function(data){
			get('result_cc2').innerHTML = data;
			get('result_cc1').innerHTML = data;
			if(data == '0'){
				$('#div_result_cc1').block();
				$('#div_result_cc2').block();
			}else{
				$('#div_result_cc1').unblock();
				$('#div_result_cc2').unblock();
			}
		}
	);
}



