var arAllElements=new Array('house_number', 'house', 'permise', 'ground', 'complex', 'rooms_plan', 'wall',	 'bathroom', 
							'balcony', 'rooms_number', 'floor_total', 'floor','area_total',
							'area_live','area_kitchen', 'area_ground', 'price',
							'additional', 'phone', 'furniture', 'refrigerator', 'tv', 'water', 'sewerage', 'gas', 'newbuilding'
							);
var arAllOperationElements=new Array('interval','rooms_number_range', 'floor_range', 'floor_total_range',
									 'area_total_range','area_live_before','area_kitchen_before','area_ground_before',
									  'price_before' 	
									);
function is_visible(element) 
	{
	if( eval("document.getElementById('div_"+element+"').style.display")=='none' ) {return false;} else {return true;} 
	}

									
function is_present(element) 
	{
	if( eval("document.getElementById('div_"+element+"')")==null ) {return false;} else {return true;} 
	}

									
function display(mode, elements)
	{
	for(i=0;i<elements.length;i++) 
		{
		if(is_present(elements[i]))
			{
			eval("document.getElementById('div_"+elements[i]+"').style.display='"+mode+"'");
			}
		}
	}

function display_one(mode, element)
	{
	eval("document.getElementById('div_"+element+"').style.display='"+mode+"'");
	}


function category_onchange()
	{
	display('', arAllElements);

	switch(forma.subcategory.value)
		{
		
		case "cat1": case "1": arElements=new Array('house','permise','ground','complex','area_ground','water', 'sewerage', 'gas');break;
		case "2": case "3": case "4": case "cat2": arElements=new Array('permise','ground','complex','floor','rooms_plan','rooms_number','bathroom', 'balcony', 'furniture', 'refrigerator', 'tv');break;
		case "5": case "6": case "7": case "8": case "9": case "10": case "11": case "cat3":  arElements=new Array('house','ground','complex','rooms_plan','rooms_number','bathroom', 'balcony',  'area_live', 'area_kitchen', 'area_ground', 'furniture', 'refrigerator', 'tv', 'water', 'sewerage', 'gas');break;		
		case "12": case "cat4": arElements=new Array('house_number','permise','house','complex','wall','floor','floor_total','rooms_plan','rooms_number','bathroom', 'balcony', 'area_total', 'area_live', 'area_kitchen', 'additional', 'newbuilding');break;		
		case "13": case "cat5": arElements=new Array('permise','house','ground','wall','floor','floor_total','rooms_plan','rooms_number','bathroom', 'balcony',  'area_live', 'area_kitchen', 'phone', 'furniture', 'refrigerator', 'tv');break;				
		case "14": case "cat6": arElements=new Array('permise','house','ground','complex','wall','floor','floor_total','rooms_plan','rooms_number','bathroom', 'balcony',  'area_live', 'area_kitchen', 'phone', 'furniture', 'refrigerator', 'tv');break;		
		
		}
		display('none', arElements);
		
	}



function operation_onchange()
	{
	display('', arAllOperationElements);
	switch(forma.operation.value)
		{

		case "1": arElements=new Array('interval','rooms_number_range','floor_range','floor_total_range','area_total_range','area_live_before','area_kitchen_before','area_ground_before', 'price_before'); break;
		case "2": arElements=new Array('interval'); break;
		case "3": arElements=new Array('rooms_number_range','floor_range','floor_total_range','area_total_range','area_live_before','area_kitchen_before','area_ground_before', 'price_before'); break;
		case "4": arElements=new Array(); break;
		}
		display('none', arElements);
	}



function is_email(value)
	{
	if (!/(^[A-Za-z][A-Za-z0-9_\-]*[\.]?[A-Za-z0-9_\-]+\@(((([A-Za-z]+([\-]?[A-Za-z0-9])+|[A-Za-z])[\.])+[A-Za-z]{2,4})|((([01]?[0-9]{1,2})|(2(([0-4][0-9])|(5[0-5]))))[\.]){3}(([01]?[0-9]{1,2})|(2(([0-4][0-9])|(5[0-5])))))$)/i.test(value))  
		{return false;}
	return true;	
	}
	
function is_image(value)
	{
	if ( !/[^.]+.jpg$/i.test(value) && !/[^.]+.jpeg$/i.test(value) && !/[^.]+.gif$/i.test(value) && !/[^.]+.png$/i.test(value) )	
		{return false;}	
		return true;	
	}

function is_media(value)
	{
	if ( !is_image(value) &&  !/[^.]+.swf$/i.test(value)  )	
		{return false;}	
		return true;	
	}



function is_natural(value)
	{
	if ( !/^[1-9]+[0-9]*$/i.test(value) )	
		{return false;}	
		return true;	
	}


function select_cb_delete_all()
   {
	 for (i = 0; i < forma.elements.length; i++)
		{
		   var item = forma.elements[i];
		   
		   if (item.name == "cb_delete[]")
				{ 
				item.checked = forma.elements.cb_delete_all.checked;
				};
		 }   
   }	 

//	Add Item to ListBox

	function listAddItem(itemsList, itemName, itemValue, bTop) {
		oOption = document.createElement("OPTION");

		if (bTop) {
			itemsList.options.add(oOption, 0);
		} else {
			itemsList.options.add(oOption);
		}

		oOption.text = itemName;
		oOption.value = itemValue;

		return oOption;
	}

	var loop=0;
	function process() {
		var num=$("ul.s > li").get().length;		
		$("ul.s > li").each(function(i) {			
			if(i==loop) $(this).show();
			else $(this).hide();
		});		
		if(loop==num-1) loop=0
		else loop++;		
	}	
	
	function pr() {
		var num=$("ul.s > li").get().length;
		if(num>0) {
			$("ul.s > li").hide();
			$("ul.s > li:first-child").show();
			setInterval('process()', 5000); 
		}
	}


function zoom(url,width,height) 
{
	if ( (width <= window.screen.availWidth) && (height <= window.screen.availHeight) )
		{
		w=width;h=height;			
		}
		else if(width>height)
			{
			w=window.screen.availWidth-10;h=Math.ceil(w*height/width);			
			if(h>window.screen.availHeight-35) {h=window.screen.availHeight-35;}
			}
			else
				{
				h=(window.screen.availHeight-35);w=Math.ceil(width*h/height);						
				}

						
		wnd=window.open(url+'&width='+w+'&height='+h,'popupWindow','left=0,top=0,menubar=no,resizable=no,status=no,toolbar=no,location=no,scrollbars=no,width=' + w +',height=' + h);
		wnd.focus();
				
}

			function imgCalendar_onclick(divPosition,inpDate,boolUpdate) 
				{
				needUpdate=boolUpdate;
				controlDate=inpDate;
				popUpCalendar(divPosition,'', 'yyyy-mm-dd');
				}


			function closeCalendar() 
				{
				monthSelected=monthSelected+1;
				if(dateSelected<10){dateSelected="0"+dateSelected;}
				if(monthSelected<10){monthSelected="0"+monthSelected;}
				controlDate.value=dateSelected+"."+monthSelected+"."+yearSelected;
				hideCalendar();
				}



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();
  }
}


function sel_state_onchange(sel1,sel2,ar1,ar2,need_zero) 
{

	while (sel2.length) 	{  sel2.options[0] = null; }
	if(need_zero) {listAddItem(sel2,'-- не определен --' , 0, 0);}

	for(i=0;i<ar2.length;i++)
		{
		if(ar2[i][2]==sel1.value )		{listAddItem(sel2, ar2[i][1], ar2[i][0], 0);}
		}
	
}


function sel_city_onchange(sel1,sel2,ar1,ar2) 
{

	while (sel2.length) 	{  sel2.options[0] = null; }
	listAddItem(sel2,'-- не определен --' , 0, 0);	
	for(i=0;i<ar2.length;i++)
		{
		if(ar2[i][2]==sel1.value )		{listAddItem(sel2, ar2[i][1], ar2[i][0], 0);}
		}
	
}



function r(name)
{
return document.getElementById(name);
}





function location_load()
{
	if(r('d_location_new').style.display=='none')
	{
		r('d_location_new').style.display='';	
		r('d_location_new').innerHTML='<fieldset><legend>Выбор населенного пункта:</legend><div class="loading"> Подождите, идет загрузка ...</div><img src="images/progressbar.gif"></fieldset>';	
	
		JsHttpRequest.query
		(
			'http://'+location.host+'/locations.php', 
			{type: 'location_load'}, 
			function(responseJS, responseText) 
			{
			document.getElementById('d_location_new').innerHTML = responseJS.locations;						
			document.getElementById('debug').innerHTML = responseText;			
			},
			true 
		);
	}
	else 	r('d_location_new').style.display='none';	
}

function city_load(state_id)
{
		JsHttpRequest.query
		(
			'http://'+location.host+'/locations.php', 
			{type: 'city_load', state_id: state_id}, 
			function(responseJS, responseText) 
			{
			document.getElementById('d_city_new').innerHTML = responseJS.locations;						
			document.getElementById('debug').innerHTML = responseText;			
			},
			true 
		);
}



function location_cancel()
{
r('d_location_new').style.display='none';
}

function city_add_save()
{
state_id=r('state_new').value;
city_id=r('city_new').value;
cityname=r('cityname').value;

if(state_id<1) {alert('Выберите область.'); return; }
if(city_id<1 && cityname=='') {alert('Выберите город.'); return; }

state_name=r('state_new').options[r('state_new').selectedIndex].text;
city_name=r('city_new').options[r('city_new').selectedIndex].text;

prefix=r('prefix').value;

	JsHttpRequest.query
	(
		'http://'+location.host+'/locations.php', 
		{type: 'city_add_save', state_id: state_id, state_name: state_name, city_id: city_id, city_name: city_name, prefix: prefix, cityname: cityname}, 
		function(responseJS, responseText) 
		{		
			document.getElementById('debug').innerHTML = responseText;			
			if(responseJS.error!=null) r('location_error').innerHTML=responseJS.error;
			else 
				{
				document.getElementById('d_city').innerHTML = responseJS.locations;										
				r('d_location_new').style.display='none';
				}
		},
		true 
	);
	
}


function wait(id, flag){
	var content;
	if(flag==1) content='<img src="http://www.realtors.com.ua/images/icon/wait.gif" width="16" height="16">';
	else content='';
	document.getElementById(id).innerHTML=content;
}

function q_city_load(state_id) {
	wait('q_wait',1);
	JsHttpRequest.query (
		'http://'+location.host+'/qsearch_load.php', 
		{type: 'city_load', state_id: state_id}, 
		function(responseJS, responseText) {
			document.getElementById('q_city').innerHTML = responseJS.locations;						
			//document.getElementById('debug').innerHTML = responseText;			
			wait('q_wait',0);
		},
		true 
	);
}

function processTab(num) {
	for(i=1; i<=2; i++ ) {
		if(i==num) {
			$("#tab"+i).animate({ opacity: "show" }, "slow");
			$("#tr"+i).show();
		} else {
			$("#tab"+i).hide();
			$("#tr"+i).hide();
		}	
	}
	return false;
}
