
function getData(variabila,div,pag){
	if(variabila.value != "-"){

		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    div.innerHTML=xmlhttp.responseText;
		     
		   // alert(xmlhttp.responseText);
		    
		    }
		  }  
		xmlhttp.open("GET","actions/"+pag+".php?op="+variabila.value,true);
		xmlhttp.send();		
	}
}
function clear_l(){
	var loc = document.getElementById("localitate_dropdown");
	
	var nr_l = loc.options.length;
	for (var i=nr_l-1; i>=1; i--) {
		  loc.remove(i);
	}
	
}
function clear_c(){
	var cart = document.getElementById("cartier_dropdown");
	
	var nr_c = cart.options.length;
	for (var i=nr_c-1; i>=1; i--) {
		  cart.remove(i);
	}
	
}
function getLocalitati(){

	var judet = document.getElementById("judet_dropdown");
	var localitate = document.getElementById("localitate_div");
	
	getData(judet,localitate,"cauta_localitate");
	
}

function getCartiere(){
	
	var localitate1 = document.getElementById("localitate_dropdown");
	var cartier = document.getElementById('cartier_div');

	getData(localitate1, cartier,"cauta_cartier");
	
}

function setMin(){
	
	var min = document.getElementById('pret_min');
	var max = document.getElementById('pret_max');
	
	min.value="";
	if(max.value == ""){
		max.value="max";
	}
	
}

function setMax(){
	
	var min = document.getElementById('pret_min');
	var max = document.getElementById('pret_max');
	
	max.value="";
	if(min.value == ""){
		min.value="min";
	}
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function setMin1(){
	
	var min = document.getElementById('pret_min');

	if(!IsNumeric(min.value) || min.value == null){
		min.value="min";
	}
	
	
}
function setMax1(){
	
	var max = document.getElementById('pret_max');
	
	if(!IsNumeric(max.value) || max.value ==""){
		max.value="max";
	}
	
}

function getSearchData(rows){
	
	var judet = document.getElementById("judet_dropdown");
	var localitate = document.getElementById("localitate_dropdown");
	var cartier = document.getElementById("cartier_dropdown");
	var imobil = document.getElementById("tip_imobil");
	var cam_min = document.getElementById("min_camere");
	var cam_max = document.getElementById("max_camere");
	
	var min = document.getElementById('pret_min'); 
	var max = document.getElementById('pret_max');
	
	var result_div = document.getElementById("content_div");
	
	if(judet.value == "-" || localitate.value == "-" || imobil.value == "-"){

		alert("Pentru a efectua cautarea trebuie sa selectati o optiune valida pentru campurile: Judet, Localitate si Imobil!");
	}
	else{
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    result_div.innerHTML=xmlhttp.responseText;
		     
		   // alert(xmlhttp.responseText);
		    
		  }
		  }  
		xmlhttp.open("GET","actions/cauta.php?j="+judet.value+"&l="+localitate.value+"&c="+cartier.value+"&i="+imobil.value+"&cam_min="+cam_min.value+"&cam_max="+cam_max.value+"&p_min="+min.value+"&p_max="+max.value+"&pageno="+rows,true);
		xmlhttp.send();	
	}
}

function getSearchData1(rows){
	
	var id = document.getElementById("search_id");
	var result_div = document.getElementById("content_div");
	
	if(id.value == ""){

		alert("Va rugam sa introduceti un id de oferta!");
	}
	else{
		
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    result_div.innerHTML=xmlhttp.responseText;
		     
		    //alert(xmlhttp.responseText);
		    
		  }
		  }  
		xmlhttp.open("GET","actions/cauta_dupa_id.php?id="+id.value+"&pageno="+rows,true);
		xmlhttp.send();	
	}
	
}

function SetCookie(cookieName,cookieValue) {
	 var today = new Date();
	 var expire = new Date();
	 nDays=0.50;
	 expire.setTime(today.getTime() + 3600000*24*nDays);
	 document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
	  if (x==c_name)
	  {
	    return unescape(y);
	  }
	}
}

function selecteazaOferta(id_oferta){
	
	var res_div = document.getElementById("oferte_selectate");
	var c = getCookie("oferta");
	var oferte = "";
	var exista = 0;
	var limit = 0;
	if(c == null || c == ""){
		//alert("NU exista");
		SetCookie("oferta", id_oferta);
	}
	else{
		//alert("exista");
		//check if the cockie has this id
		var c1 = getCookie("oferta").split("_");
		var l = c1.length;
		if(l < 10){
			var i = 0;
			
			while(i<l){
				if(c1[i] == id_oferta){
					exista = 1;
					//alert("oferta exista deja");
				}
				i++;
			}
			
			if(exista == 0){
				oferte = getCookie("oferta")+"_"+id_oferta;
				SetCookie("oferta", oferte);
			}
			else{
				alert("Ati selectat deja aceasta oferta");
			}	
		}
		else{
			alert("Nu puteti selecta mai mult de 10 oferte!");
		}
	}
	
	var toate_ofertele = getCookie("oferta");
	
	//alert(toate_ofertele);
	
	if(exista != 1){
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    res_div.innerHTML=xmlhttp.responseText;
		     
		    //alert(xmlhttp.responseText);
		    
		  }
		  }  
		xmlhttp.open("GET","actions/add_oferta_selectata.php?id="+toate_ofertele,true);
		xmlhttp.send();
	}
}

function stergeOfertaSelectata(id_of){
	//alert(id_of);
	var res_div = document.getElementById("oferte_selectate");
	var c = getCookie("oferta");
	//eliminam oferta, trimitem restul la sterge_oferta_selectata.php care reafiseaza ofertele dupa stergere
	
	var c2 = getCookie("oferta").split("_");
	var l1 = c2.length;
	var j = 0;
	var rez = "";
	
	while(j<l1){
		
		if(c2[j] != id_of){
			if(rez == ""){
				rez = c2[j];
			}
			else{
				rez = rez + "_" + c2[j];
			}
		}
		
		j++;
	}
	
	SetCookie("oferta", rez);

	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    res_div.innerHTML=xmlhttp.responseText;
	     
	    //alert(xmlhttp.responseText);
	    
	  }
	  }  
	xmlhttp.open("GET","actions/del_oferta_selectata.php?oferte="+rez,true);
	xmlhttp.send();
	
	
}

function deleteCookieAfterMailSend(){
	//alert("OK");
	SetCookie("oferta", "");
	//stergeOfertaSelectata("");
}

var ascuns = 1;
function ascundeInfoUtil(){
	
	var prima_casa = document.getElementById("prima_casa");

	var articole = document.getElementById("articole");

	var stiri = document.getElementById("stiri_imobiliare");

	var certif = document.getElementById("cert_energetic");

	if(ascuns == 1){
		prima_casa.style.display = "block";
		articole.style.display = "block";
		stiri.style.display = "block";
		certif.style.display = "block";
		ascuns = 0;
		SetCookie("hide_menu", ascuns);
	}
	else{
		prima_casa.style.display = "none";
		articole.style.display = "none";
		stiri.style.display = "none";
		certif.style.display = "none";
		ascuns = 1;
		SetCookie("hide_menu", ascuns);
	}
}
function addslashes(str) {  
	return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");  
}  
function Ordoneaza(){
	
	var drop_down = document.getElementById("select_ordoneaza");
	
	if(drop_down.value != "-"){
		var link = addslashes(window.location); 
		
		if(link.search("sortare") != '-1'){
			var link_sort = link.split("&sortare");
			window.top.location = link_sort[0]+"&sortare=" + drop_down.value;
		}
		else{
			window.top.location = link + "&sortare="+drop_down.value;
		}
	}
}

function OrdoneazaCereri(){
	
	var drop_down = document.getElementById("select_cereri");
	
	
		var link = addslashes(window.location); 
		//alert(drop_down.value);
		if(link.search("judet") != '-1'){
			var link_sort = link.split("?judet=");
			if(drop_down.value == "-"){
				window.top.location = link_sort[0]; 
			}
			else{
				window.top.location = link_sort[0]+"?judet=" + drop_down.value;
			}
		}
		else{
			window.top.location = link+"?judet="+drop_down.value;
		}
	

}


if (window.addEventListener){
	window.addEventListener("load", getLocalitati, false);
	window.addEventListener("load", getCartiere, false);
}
