var isIE = document.all?true:false;
var site_location="http://localhost/vreaucasa";
if (location.href.match(/localhost\/vreaucasa/)==null || location.href.match(/localhost/)=="")
	site_location="http://www.vandlocuinta.ro";

function showTab(obj){
	document.getElementById("orase").style.display="none";
	document.getElementById("toateorasele").style.display="none";
	
	document.getElementById("tab1").className="tabheader";
	document.getElementById("tab2").className="tabheader";
	
	document.getElementById(obj.rel).style.display="block";
	obj.className="tabheader selected";
}

function showTab2(obj){
	document.getElementById("orase2").style.display="none";
	document.getElementById("toateorasele2").style.display="none";
	
	document.getElementById("tab3").className="tabheader";
	document.getElementById("tab4").className="tabheader";
	
	document.getElementById(obj.rel).style.display="block";
	obj.className="tabheader selected";
}

function checkField(fieldid,min,number) {
	var value=document.getElementById(fieldid).value;	
	if (value.length<min)
		return fieldid+" trebuie completat\r\n";
	if (number==true && checkNumberValue(value)==false) {
		return fieldid+" nu este valid\r\n";
	}
	return "";
}

function checkNumberValue(checkStr) {
	var checkOK = "0123456789";
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
		allValid = false;
		break;
		}
	}
	return allValid;
}

function checkForm(obj) {
	if (obj.id=="form_adauga") 
		return checkAdaugaForm();
	if (obj.id=="form_edit_ap") 
		return checkEditApForm();
	if (obj.id=="form_edit_casa") 
		return checkEditCasaForm();

}

function checkAdaugaForm() {
	var retstring="";
	
	retstring+=checkField("pret",2,true);
	retstring+=checkField("an",4,true);
	retstring+=checkField("camere",1,true);
	retstring+=checkField("suprafata",2,true);
	retstring+=checkField("oras",2,false);
	retstring+=checkField("cartier",2,false);
	retstring+=checkField("strada",2,false);
	retstring+=checkField("nume_proprietar",4,false);
	retstring+=checkField("telefon",6,true);
	retstring+=checkField("descriere_zona",5,false);
	retstring+=checkField("descriere_apartament",5,false);
	
	if (document.getElementById("tip_imobil").value=="1") {
		retstring+=checkField("etaj",1);
		retstring+=checkField("etajmaxim",1);
	}
	if (document.getElementById("tip_imobil").value=="2") {
		retstring+=checkField("nivele",1);
		retstring+=checkField("amprenta",2,true);
	}	
	

	if (retstring.length>0) {
		alert(retstring);
		return false;
	}
	else
		return true;
}

function checkEditApForm() {
	var retstring="";
	
	retstring+=checkField("pret",2,true);
	retstring+=checkField("an",4,true);
	retstring+=checkField("camere",1,true);
	retstring+=checkField("suprafata",2,true);
	retstring+=checkField("oras",2,false);
	retstring+=checkField("cartier",2,false);
	retstring+=checkField("strada",2,false);
	retstring+=checkField("telefon",6,true);
	retstring+=checkField("descriere_zona",5,false);
	retstring+=checkField("descriere_apartament",5,false);
	
	retstring+=checkField("etaj",1);
	retstring+=checkField("etajmaxim",1);
	

	if (retstring.length>0) {
		alert(retstring);
		return false;
	}
	else
		return true;
}

function checkEditCasaForm() {
	var retstring="";
	
	retstring+=checkField("pret",2,true);
	retstring+=checkField("an",4,true);
	retstring+=checkField("camere",1,true);
	retstring+=checkField("suprafata",2,true);
	retstring+=checkField("oras",2,false);
	retstring+=checkField("cartier",2,false);
	retstring+=checkField("strada",2,false);
	retstring+=checkField("telefon",6,true);
	retstring+=checkField("descriere_zona",5,false);
	retstring+=checkField("descriere_apartament",5,false);
	
	retstring+=checkField("nivele",1);
	retstring+=checkField("amprenta",2,true);
	

	if (retstring.length>0) {
		alert(retstring);
		return false;
	}
	else
		return true;
}


function showMyAddLogin(){
	document.getElementById("myaddlogin").style.display="block";
}
function hideMyAddLogin(){
	document.getElementById("myaddlogin").style.display="none";
}

function applyFilter(value) {
	document.getElementById("hiddenfilterid").value=value;
	document.getElementById("filterform").submit();
}

function removeFilter(value) {
	document.getElementById("hiddenremovefilterid").value=value;
	document.getElementById("removefilterform").submit();
}

function doFillZone() {
	city=document.getElementById("oras").value;
	if (city=="")
		return;
	goAjax(site_location+"/getzones.php?oras="+city,doHandleZone);
}

function doFillZoneSearch(oras1,oras2) {
	city=oras1;
	tip=document.getElementById("tip_anunt").value;
	if (tip==2 || tip=="2")
		city=oras2;
	goAjax(site_location+"/getzones.php?oras="+city,doHandleZone);
}

function doHandleZone(xmlhttp) {
	//If readyState = 4, the server response is complete
	if (xmlhttp.readyState === 4) {
		//We can evaluate the status property to see what HTTP response	
		//Status’ in the 200’s are okay; “200” is the best
		var statusCode = xmlhttp.status;
		if (statusCode == 200  || statusCode ==0){		
			//Process the server response
			document.getElementById("zone").innerHTML=xmlhttp.responseText;
		}else{
		 //Some error handling
		 processError(xmlhttp);
		}
	}
}

function goAjax(url,method) {
	//Get a reference to the XMLHttpRequest object
	var xmlRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
	//If the browser doesn’t support Ajax, exit now
	if (xmlRequest == null)
		return;
	//Initiate the XMLHttpRequest object
	xmlRequest.open("GET", url, true);
	//Since this is a POST, set the request Content-Type header
	//xmlRequest.setRequestHeader("Content-Type", "application/x-www-formurlencoded");
	//Setup the callback function
	xmlRequest.onreadystatechange = function () { method(xmlRequest) };
	//Send the Ajax request to the server with the POST data
	xmlRequest.send(null);	
}

function processError(xmlhttp) {
	alert("Error occured");
}

function setZona(divobj) {
	document.getElementById("cartier").value=divobj.innerHTML;	
}
