var d = document;

function checkCat(what) {
	var mf = d.forms["search"];
	for (i = 0; i < mf.length; i++) {
		bum = mf[i].id.split("_");
		if (bum[0]+"_"+bum[1] == 'cat_'+what) {
			if (typeof(checked) == "undefined")
				var checked = !(mf[i].checked);
			mf[i].checked = checked;
		}
	}
}

function clearAll(what) {
	for (i = 0; i < d.getElementsByName(what).length; i++)
		d.getElementsByName(what)[i].checked = false;
}
function addAll(what) {
	for (i = 0; i < d.getElementsByName(what).length; i++)
		d.getElementsByName(what)[i].checked = true;
}

function Clear_Elements() {
//and city[1] and order[15] no need to clear
window.document.getElementById("extras").innerHTML=""; //0
window.document.f.ad_type_id.value=""; //0
window.document.f.nomer.value=""; //13
window.document.f.keyword.value="" //12
window.document.f.surface_to.value=""; //8
window.document.f.surface_from.value=""; //7
window.document.f.price_to.value=""; //4
window.document.f.price_from.value=""; //3
for (var i = 0; i<document.f.elements['floor_from'].length; i++) { //17
		document.f.elements['floor_from'].options[i].selected = false;
	}
for (var i = 0; i<document.f.elements['floor_to'].length; i++) { //16
		document.f.elements['floor_to'].options[i].selected = false;
	}

for (var i = 0; i<document.f.elements['currency_kv_id'].length; i++) { //5
		document.f.elements['currency_kv_id'].options[i].selected = false;
	}

for (var i = 0; i<document.f.elements['currency_id'].length; i++) { //6
		document.f.elements['currency_id'].options[i].selected = false;
	}

for (var i = 4; i < 27; i++) {
     b = document.getElementById("cat_5_"+i);
     if (b) { b.checked = false;  }
     }

for (var i = 0; i < 15; i++) {
     b = document.getElementById("bt"+i);
     if (b) { b.checked = false;  }
     }

}


// getvane na konkretno cookie i zarejdane na formata
function Get_Cookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
    Clear_Elements();
	var stre=unescape( document.cookie.substring( len, end ) );
	var strexp=stre.split("|||");
	for (var i = 0; i < document.f.city_id.length; i++) {
		if (document.f.city_id.options[i].value==strexp[1]) 	window.document.f.city_id.options[i].selected="true"; // city_id
	}

	var btype=strexp[2].split(","); //building types ids
	for (var k = 0; k < btype.length-1; k++) {
		window.document.getElementById("cat_5_" + btype[k]).checked="true";
	}

	var btype=strexp[20].split(","); //building types (stroitelstvo)
	for (var k = 0; k < btype.length-1; k++) {
		window.document.getElementById("bt" + btype[k]).checked="true";
	}

	LoadExtras('/extras.php?type=extras&save_search=' + strexp[10],'extras');
	LoadAreas_Ajax('/search_fillcombos.php?type=kvartal1&save_search=' + strexp[19],'kvartal1',strexp[1]);
    LoadAreas_AjaxS('/search_fillcombos.php?type=kvartal5&save_search=' + strexp[19],'kvartal2',strexp[1]);

	if (strexp[17]>0) { window.document.f.floor_from.options[strexp[17]].selected="true"; } //floor_from
	if (strexp[16]>0) { window.document.f.floor_to.options[strexp[16]-1].selected="true"; } // floor_to
	if (strexp[15]>0) { window.document.f.order[strexp[15]-1].checked="true"; } //order
	window.document.f.price_from.value=strexp[3]; // price_from
	window.document.f.price_to.value=strexp[4]; // price_to
	for (var i = 0; i < document.f.currency_id.length; i++) {
		if (document.f.currency_id.options[i].value==strexp[5]) 	window.document.f.currency_id.options[i].selected="true"; // currency_id
	}
	for (var i = 0; i < document.f.currency_kv_id.length; i++) {
		if (document.f.currency_kv_id.options[i].value==strexp[6]) 	window.document.f.currency_kv_id.options[i].selected="true"; // currency_kv_id
	}
	window.document.f.surface_from.value=strexp[7]; // surface_from
	window.document.f.surface_to.value=strexp[8]; // surface_to
	 // keyword - ve4e stava s ajax
	window.document.f.nomer.value=strexp[13]; // nomer
	window.document.f.ad_type_id.value=strexp[0]; // nomer
	if (strexp[22]=="on") window.document.f.withpics.checked="true"; //withpics

	if (document.getElementById("new_building_yes") != null) {
	if (strexp[23]=="on") window.document.f.new_building_yes.checked="true"; else  window.document.f.new_building_yes.checked="";//new_building_yes
	}

	if (strexp[24]) {
		for (var i = 0; i < document.f.new_building_month.length; i++) {
			if (document.f.new_building_month.options[i].value==strexp[24]) window.document.f.new_building_month.options[i].selected="true"; // new_building_month
		}
	}

}


//get all cookies and print links
function Get_All_Cookies() {
	var exist="1";
	alert('start');
	for(var i=0;i<5;i++) {
		exist=1;
		var name="c_save_search["+i+"]";
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
		{
		}
		if ( start == -1 ) exist="0";
		if (exist>0) {
			var end = document.cookie.indexOf( ";", len );
			var stre=unescape( document.cookie.substring( len, end ) );
			var strexp=stre.split("|||");
			document.write("<li> <a href=\"javascript: Get_Cookie('"+name+"');\">" + strexp[18] + " </a></li>");
		}
	}
	}

function Redirect_Cookie(url,param) {
document.location = "http://imoti.net/nd/obqvi/"+ url+"?cookie_value="+param;
}


function DecodeUtf8(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

    
function Print(url,type,txt,e) {
	
	if ( e.keyCode == 13 ) {
		if ( txt == '' ) {
			document.getElementById('city_id').value = '';
			document.getElementById('region').value='';
		}
		return;
	}
 document.getElementById('gaden').value = 0;
 
 req = false;
 var city="?city=";
 pole_input=txt;
 pole=type;
 txt=document.getElementById(txt).value;
 
 
if ( txt == '' ) {
	document.getElementById('city_div').style.display='none';	
	return;
}


 //for(i=0;i< txt.length; i++) {
 //city=city + txt.charCodeAt(i) + "-";
 
 //}

  url = url+city+txt;
  
  if ( txt == '') {
  	//document.getElementById('city_id').style.display='none';
  	//return;
  }

 if (window.XMLHttpRequest) {
	try {
		req= new XMLHttpRequest();
	}
	catch (e) {
		req = false;
	}
 }
 else if(window.ActiveXObject) {
	 try {
		req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				req = false;
			}
		}
 }
 
 if (req) {
 req.onreadystatechange=processReqChange2;
 req.open("GET", WEBPATH+""+url, true);
 req.send(null);
 }
 

}

function processReqChange2()
{

	if (req.readyState == 4)
	{
		if (req.status == 200)
		{

 
			window.document.getElementById(pole).innerHTML = req.responseText;
			window.document.getElementById(pole).style.display = 'block';
		}
		else
		{
			alert("Данните не могат да бъдат заредени");
		}
	}
}


function div_mouseout(div_id) {
	window.document.getElementById(div_id).style.display = 'none';
//	window.document.getElementById("building_type_id").style.visibility = 'visible';
}


function SetCity(city_id,city,area_id,sub_area_id) {
	 
 window.document.getElementById("region_id").value = area_id;
 window.document.getElementById("city_id").value = city_id;
 window.document.getElementById("des").value = city;
 if (area_id) {
	 window.document.getElementById("region_id").value = area_id;
 }
  if (sub_area_id) {
	 window.document.getElementById("sub_area_id").value = sub_area_id;
 }
  
}

