var lVisible = false;
var lShow = false; 

function getLanguage( selectedlang, selectedaction, selectedtype )
	{
	  document.search_select.action = "index.php";
	  document.search_select.langtype.value = selectedlang;
	  document.search_select.submit();
	}
	
	var request = false;
    try {
      request = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try {
          request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          request = false;
        }  
      }
    }

    if (!request)
      alert("Error initializing XMLHttpRequest!");
	
	function search_submit( type, act )
	{
	  document.search_select.actiontype.value = type;
	  document.search_select.action = act;
	  document.search_select.submit();
	}
	
	function location_submit( type, act )
	{
	  var location = document.getElementById("search-location");
	  location.options[location.selectedIndex].value = type;
	  document.search_select.actiontype.value = 'search';
	  document.search_select.action = act;
	  document.search_select.submit();
	}
	
	function getSelect( index ) {
	  if(index == '1'){
	    var type = document.getElementById("search-type");
        var type_id = type.options[type.selectedIndex].value;  
	    var location_id = '';
	  }
	  if(index == '2'){
        var location = document.getElementById("search-location");
        var location_id = location.options[location.selectedIndex].value;
        var type_id = '';
      }
	  if((location_id != '')||(type_id != '')){		
	    var url = "selector.php?location_id=" + escape(location_id) + "&test=1&type_id=" + escape(type_id);
	    request.open("GET", url, true);
	    request.onreadystatechange = handleHttpResponse;
	    request.send(null);
      }
	}

    function handleHttpResponse() {	
	  if (request.readyState == 4) {
	     if(request.status ==200){
			var result=request.responseXML;
			var li = result.getElementsByTagName("name");
			var lo = result.getElementsByTagName("locationID");
			var where = document.getElementById("search-location").options[0].innerHTML;
			var what = document.getElementById("search-type").options[0].innerHTML;
			var de = result.getElementsByTagName("deal");
			var deal = document.getElementById("search-deal").options[0].innerHTML;
			if(lo.length != 0){
			  var value = "";
			  var text = where;	
			  document.getElementById("search-location").options.length = 0;
			  document.getElementById("search-location").options[0] = new Option(text, value);
			  for (var j = 0; j < lo.length; j++)
			  {
			    var locationID = result.getElementsByTagName("locationID")[j];
			    var loID = locationID.childNodes[0].data;
				if(loID != '-1'){
				  var locationName = result.getElementsByTagName("location")[j];
				  var loName = locationName.childNodes[0].data;
				  document.getElementById("search-location").options[j+1] = new Option(loName, loID);
			    }
			  }
			} 
			var ty = result.getElementsByTagName("typeID");
			if(ty.length != 0){
			  var value = "";
			  var text = what;	
			  document.getElementById("search-type").options.length = 0;
			  document.getElementById("search-type").options[0] = new Option(text, value);
			  for (var j = 0; j < ty.length; j++)
			  {
			    var typeID = result.getElementsByTagName("typeID")[j];
			    var tyID = typeID.childNodes[0].data;
				if(tyID != '-1'){
				  var typeName = result.getElementsByTagName("type")[j];
				  var tyName = typeName.childNodes[0].data;
				  document.getElementById("search-type").options[j + 1] = new Option(tyName, tyID);
			    }
			  }
			}	
			  if(de.length != 0){
				var value = "";
			  	var text = deal;	
				var buy = document.getElementById("search-deal").options[1].innerHTML;
			  	var rent = document.getElementById("search-deal").options[2].innerHTML;
				document.getElementById("search-deal").options.length = 0;
			  	document.getElementById("search-deal").options[0] = new Option(text, value);
				for (var j = 0; j < de.length; j++)
			    {
			    	var dealTag = result.getElementsByTagName("deal")[j];
			    	var d_id = dealTag.childNodes[0].data;
					if(d_id != '-1'){
				  		if(d_id == '0'){
							document.getElementById("search-deal").options[j + 1] = new Option(buy, d_id);
						}else{
							document.getElementById("search-deal").options[j + 1] = new Option(rent, d_id);
						}
			    	}
			  	}
			  } 
		    }	
	      }
		}
		
		function validate( selectedaction )
		{
		  if((document.newsletter.email.value.length != 0)||(document.newsletter.email.value != '')){
		    document.newsletter.action = selectedaction;
			document.newsletter.submit();
		  }else{
		    alert("You didn't enter email your email address");
			document.newsletter.email.focus();
		  }
		}
function viewTopic(index, selectedaction){
  document.view_topic.topic_id_hid.value = index;
  document.view_topic.action = selectedaction;
  document.view_topic.submit();
}

function viewProperty(property_id, page, sortby, selectedaction){
	document.getElementById("property_id").value = property_id;
	document.getElementById("page").value = page;
	document.getElementById("sortby").value = sortby;
	document.search_criteria.action = selectedaction;
	document.search_criteria.submit();
}

function viewPropertyLatest(property_id, selectedaction){
	document.getElementById("property_id").value = property_id;
	document.search_criteria.action = selectedaction;
	document.search_criteria.submit();
}

function viewPropertyFirm(property_id, selectedaction, companyId){
	document.getElementById("property_id").value = property_id;
	document.getElementById("company_flag").value = "1";
	document.getElementById("company_id").value = companyId;
	document.search_criteria.action = selectedaction;
	document.search_criteria.submit();
}

function listResult( list ){
	document.search_select.list_flag.value = list;
	document.search_select.action = "properties";
	document.search_select.submit();
}

function getMail(){
	var url = "mailing.php";
	request.open("GET", url, true);
	request.onreadystatechange = handleMailing;
	request.send(null);
}

function handleMailing(){
	if (request.readyState == 4) {
	     if(request.status ==200){
		 	var result = request.responseText;
			document.getElementById("mailing").innerHTML = result;
		 }
	}
}

function showDate( selected ){
	document.search_criteria.date.value = selected;
	document.search_criteria.date_flag.value = "1";
	document.search_criteria.page.value = "0";
	document.search_criteria.action = "properties";
	document.search_criteria.submit();
}

function viewCompany( selected, username ){
	document.search_criteria.company_id.value = selected;
	document.search_criteria.action = "companies/"+username;
	document.search_criteria.submit();
}

function viewAllFirm( type, act, name )
{
	document.search_criteria.actiontype.value = type;
	document.search_criteria.company_name.value = name;
	document.search_criteria.action = act;
	document.search_criteria.submit();
}

function viewHistory(){
	document.getElementById("page").value = "0";
	document.search_criteria.action = "properties";
	document.search_criteria.history_flag.value = "1";
	document.search_criteria.date_flag.value = "";
	document.search_criteria.list_flag.value = "";
	document.search_criteria.search_flag.value = "";
	document.search_criteria.submit();
}

function updateNews(text, text_sub, text_unsub){
	document.search_criteria.action = "index.php";
	/*document.search_criteria.submit();
	var user = document.getElementById("user-id").value;
	var email = document.getElementById("user-email").value;
	var text_new = document.getElementById("news-text-field").value;
	if(user != ''){
		document.getElementById("news-box").innerHTML = "<input type=\"hidden\" name=\"email\" value=\""+email+"\"/><p>"+text_new+"</p><div class=\"submit br\"><span class=\"l\"><img src=\"img/ico4.gif\" />&nbsp;<a onmouseover=\"this.style.cursor='pointer'\" onClick=\"javascript:validate( 'subscribe.php' )\">"+text_sub+"</a></span></div>";
	}else{
		document.getElementById("news-box").innerHTML = "<p>"+text_new+"</p><fieldset><label for=\"YourEmail\">"+text+":&nbsp;&nbsp;</label><input type=\"text\" id=\"YourEmail\" name=\"email\"/></fieldset><div class=\"submit br\"><span class=\"l\"><img src=\"img/ico4.gif\" />&nbsp;<a onmouseover=\"this.style.cursor='pointer'\" onClick=\"javascript:validate( 'subscribe.php' )\">"+text_sub+"</a></span><span class=\"r\"><img src=\"img/ico4.gif\" />&nbsp;<a onmouseover=\"this.style.cursor='pointer'\" onClick=\"javascript:validate( 'index.php?unsub=1' )\">"+text_unsub+"</a></span></div>";
	}*/
}

function updateTimeOut( text, text_sub, text_unsub){
	setTimeout("updateNews('"+text+"', '"+text_sub+"', '"+text_unsub+"')", 5000);
}

function getDeal(){
	var location = document.getElementById("search-location");
	var type = document.geteelementById("search-type");
	var type_id = type.options[type.selectedIndex].value;
	var loc_id = location.options[location.selectedIndex].value;
	var url = "selector.php?test=0&l_id="+loc_id+"&t_id="+type_id;
	request.open("GET", url, true);
	request.onreadystatechange = dealResponse;
	request.send(null);
}

function dealResponse(){
	if(request.readyState == 4) {
	     if(request.status ==200){
		 	var result = request.responseXML;
			var de = result.getElementsByTagName("deal");
			var deal = document.getElementById("search-deal").options[0].innerHTML;
		 	if(de.length != 0){
				var value = "";
			  	var text = deal;	
				var buy = document.getElementById("search-deal").options[1].innerHTML;
			  	var rent = document.getElementById("search-deal").options[2].innerHTML;
				document.getElementById("search-deal").options.length = 0;
			  	document.getElementById("search-deal").options[0] = new Option(text, value);
				for (var j = 0; j < de.length; j++)
			    {
			    	var dealTag = result.getElementsByTagName("deal")[j];
			    	var d_id = dealTag.childNodes[0].data;
					if(d_id != '-1'){
				  		if(d_id == '0'){
							document.getElementById("search-deal").options[j + 1] = new Option(buy, d_id);
						}else{
							document.getElementById("search-deal").options[j + 1] = new Option(rent, d_id);
						}
			    	}
			  	}
			  } 
		 }
	}
}

function catchEnterNews(e){
	var key=e.keyCode || e.which;
  	if(key == 13){
		validate('subscribe.php');
    }
}

function type_submit(id, actiontype){
	document.search_select.search_type.value = id;
	document.search_select.actiontype.value = 'search';
	document.search_select.action = actiontype;
	document.search_select.submit();
}

function catchEnterReference(e){
	var key=e.keyCode || e.which;
  	if(key == 13){
		search_submit('reference', 'properties');
    }
}

function showList( list ){
  	document.getElementById("page").value = "0";
	document.search_criteria.action = "properties";
	document.search_criteria.history_flag.value = "";
	document.search_criteria.date_flag.value = "";
	document.search_criteria.list_flag.value = list;
	document.search_criteria.search_flag.value = "";
	document.search_criteria.submit();
}

function showForm(id){
	if(document.getElementById(id) != null){
		if(lVisible == false){
			if(id == 'news-box') document.getElementById(id).className = "property_notification"; 
			document.getElementById(id).style.display = "block";
			lVisible = true;
		}else{
			hideForm(id);
		}
	}
}

function hideForm(id){
	lVisible = false;
	if(document.getElementById(id) != null){
		if(id == 'news-box') document.getElementById(id).className = "property_notification_float"; 
		document.getElementById(id).style.display = "none";
	}	
}

function inviteFriend(id){
	var lValues = true;
	if(document.getElementById("your-name") != null){
		var yname = document.getElementById("your-name").value;
		if(yname == '') lValues = false;		
	}
	if(document.getElementById("your-email") != null){
		var yemail = document.getElementById("your-email").value;
		if(yemail == '') lValues = false;		
	}
	var femail = document.getElementById("friend-email").value;
	if(femail == '') lValues = false;
	var fname = document.getElementById("friend-name").value;
	if(fname == '') lValues = false;
	if(lValues != false){
		var url = "invite_friend.php?yname="+yname+"&yemail="+yemail+"&femail="+femail+"&fname="+fname;
		request.open("GET", url, true);
		request.onreadystatechange = function(){ inviteResponse(id); }
		request.send(null);
	}else
		alert("Please, enter all fields!");
}

function inviteResponse(id){
	if(request.readyState == 4) {
	     if(request.status ==200){
		 	alert(request.responseText);
		 }
	}
	hideForm(id);
}

function visibleForm(id){
	if(document.getElementById(id) != null){
		if(lShow == false){
			document.getElementById(id).style.visibility = "visible";
			lShow = true;
		}else{
			hiddenForm(id);
		}
	}
}

function hiddenForm(id){
	lShow = false;
	if(document.getElementById(id) != null)
		document.getElementById(id).style.visibility = "hidden";
}

function makeGeneralRequest(){
	var lBool = true;
	var r1 = Number(document.req_frm.result.value);
	var r2 = Number(document.req_frm.result_hid.value);
	if(r1 != r2) lBool = false;
	if(document.req_frm.user_flag.value == "0"){
		if(document.req_frm.name.value == '' || document.req_frm.email.value == '') lBool = false;
	}	
	if(lBool == true) document.req_frm.submit();
		else alert("You did not enter all field or valid result. Please, check it!");
}
