function lCat(type, desc, parent, resultId){
	lCommon('lcat', type, desc, parent, resultId);
}

function lLoc(type, parent, resultId){
	lCommon('lloc', type, 1, parent, resultId);
}

function lLocND(type, parent, resultId){
	lCommon('lloc', type, 0, parent, resultId);
}

function lCommon(act, type, desc, parent, resultId){
 if(parent != null && parent != '' ){
 	var id = parent.options[parent.selectedIndex].value;
 } else {
 	var id = 0;
 }
	var resultObj = document.getElementById(resultId);
	var resultSelect = resultObj.getElementsByTagName('SELECT');
	resultSelect[0].innerHTML = '<option>Loading...</option>';
	resultSelect[0].disabled = true;
	var link = '/ajax.php?action=' + act + '&type=' + type + '&id='+ id + '&desc='+ desc + '&rand=' + Math.round(Math.random()*1000);
	new Ajax.Request(link,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText || 'no response text';
			if('' != response){
				resultObj.innerHTML = response;
			}
		},
	  onFailure: function(){alert('Something went wrong...')}
	});
}

function lDet(obj, act, branch, id, style){
	obj.className = "in-sel"+style;
	var resultObj = document.getElementById("dt" + id);
	var labelObj = document.getElementById("sh" + id);
	if(obj.opened != undefined){
		if(obj.opened){
			obj.className = "in-vis"+style;
			resultObj.style.display = "none";
			labelObj.innerHTML = 'подробности';
			obj.opened = false;
			return;
		} else {
			obj.className = "in-sel"+style;
			resultObj.style.display = "block";
			labelObj.innerHTML = 'закрыть';
			obj.opened = true;
			return;
		}
	}
	obj.opened = true;
	labelObj.innerHTML = 'закрыть';
	var link = '/ajax.php?action=' + act + '&branch=' + branch + '&id='+ id + '&rand=' + Math.round(Math.random()*1000);
	resultObj.className = "more";
	new Ajax.Request(link,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText || 'no response text';
			if('' != response){
				resultObj.innerHTML = response;
			}
		},
	  onFailure: function(){alert('Something went wrong...')}
	});
}

function maxlength(text_el, count_el, maxvalue) {
	var el = document.getElementById(text_el);
  	var count = parseInt(el.value.length);
	if(count > maxvalue) {
		el.value = el.value.substr(0, maxvalue);
	}
	document.getElementById(count_el).innerHTML = el.value.length;
}

function addToFavorites(title, url) { 
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,'');
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);}
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}			
}

function lExtFields(obj, act, type){
	obj.className = "";
	var resultObj = document.getElementById("div_extended_filtr");
	//var labelObj = document.getElementById("extended_button");
	if(obj.opened != undefined){
		if(obj.opened){
			obj.className = "in-vis";
			resultObj.style.display = "none";
			//labelObj.innerHTML = 'подробности';
			obj.opened = false;
			return;
		} else {
			obj.className = "in-sel";
			resultObj.style.display = "block";
			//labelObj.innerHTML = 'закрыть';
			obj.opened = true;
			return;
		}
	}
	obj.opened = true;
	//labelObj.innerHTML = 'закрыть';
	var link = '/ajax.php?action=' + act + '&type=' + type + '&rand=' + Math.round(Math.random()*1000);
	resultObj.style.display = "block";
	new Ajax.Request(link,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText || 'no response text';
			if('' != response){
				resultObj.innerHTML = response;
			}
		},
	  onFailure: function(){alert('Something went wrong...')}
	});
}

function moveUpItem(branch,item){
  actionWithItem(branch, item, 'move_up');
}

function promoteItem(branch,item){
  actionWithItem(branch, item, 'promote_item');
}

function hideItem(branch,item){
	if($('hiden_'+branch+'_'+item)){
	  actionWithItem(branch, item, 'unhide');
	  var obj = $('hiden_'+branch+'_'+item);
  	  obj.getElementsByTagName('img')[0].src='/images/unhide.png';
	  obj.getElementsByTagName('img')[0].title = obj.getElementsByTagName('img')[0].alt = 'Hide item';
	  obj.id='unhiden_'+branch+'_'+item;
  }else if($('unhiden_'+branch+'_'+item)){
	  actionWithItem(branch, item, 'hide');
	  var obj = $('unhiden_'+branch+'_'+item);
	  obj.getElementsByTagName('img')[0].src='/images/hide.png';
	  obj.getElementsByTagName('img')[0].title = obj.getElementsByTagName('img')[0].alt = 'Unhide item';
	  obj.id='hiden_'+branch+'_'+item;
  }
}

function stopAgent(branch, item){
	actionWithItem(branch, item, 'stop_agent');
	if($('stopped_'+branch+'_'+item)){
		var obj = $('stopped_'+branch+'_'+item);
		obj.getElementsByTagName('img')[0].src='/images/btn-agent-pause.png';
		obj.getElementsByTagName('img')[0].title = obj.getElementsByTagName('img')[0].alt='Stop agent';
		obj.id = 'started_'+branch+'_'+item;
	}else if($('started_'+branch+'_'+item)){
		var obj = $('started_'+branch+'_'+item);
		obj.getElementsByTagName('img')[0].src='/images/btn-agent-play.png';
		obj.getElementsByTagName('img')[0].title = obj.getElementsByTagName('img')[0].alt='Start agent';
		obj.id = 'stopped_'+branch+'_'+item;
	}
}

function deleteAgent(branch, item){
	if(confirm('Are you sure?')){
		actionWithItem(branch, item, 'delete_agent');
		$('link_'+branch+'_'+item).remove();
		if($('stopped_'+branch+'_'+item)){
			$('stopped_'+branch+'_'+item).remove();
		}else if($('started_'+branch+'_'+item)){
			$('started_'+branch+'_'+item).remove();
		}
		$('linkdelete_'+branch+'_'+item).remove();
	}
}

function actionWithItem(branch,item,action){
  var resultObj = document.getElementById('action_'+branch+'_'+item);
  var link='/ajax.php?action=item&type=' + action + '&branch=' + branch + '&item=' + item + '&rand=' + Math.round(Math.random()*1000);
  new Ajax.Request(link,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText;
			if('' != response){
				resultObj.innerHTML = response;
			}
		},
	  onFailure: function(){alert('Something went wrong...')}
	});
}

function saveFavorites (){
  Lightview.show({
    href: '/ajax.php?action=favorites&type=save',
    rel: 'ajax',
    options: {
      menubar: false,
	  topclose: true,
	  autosize: true,
      ajax: {
        parameters: Form.serialize('favorite_form') // the parameters from the form
      }
    }
  });
}

function showTestimonials (title){
	Lightview.show({
		href: '/ajax.php?action=testimonials',
		rel: 'ajax',
		title: title,
		options: {
			autosize: true,
			topclose: true,
			ajax: {
			}
		}
	});
}

function saveTestimonials (){
  Lightview.show({
    href: '/ajax.php?action=testimonials',
    rel: 'ajax',
    options: {
      menubar: false,
	  topclose: true,
	  autosize: true,
      ajax: {
        parameters: Form.serialize('testimonial_form') // the parameters from the form
      }
    }
  });
}

function chnageFavoriteStatus(current,item,branch){
  var resultObj = document.getElementById('fav_pic_'+item);
  var link='/ajax.php?action=favorites&type=change&fav_status_id=' + current + '&branch=' + branch + '&item=' + item + '&rand=' + Math.round(Math.random()*1000);
  new Ajax.Request(link,
	{
		method:'get',
		onSuccess: function(transport){
			var response = transport.responseText;
			if('' != response){
				resultObj.innerHTML = response;
			}

		},
	  onFailure: function(){alert('Something went wrong...')}
	});
	return false;
}

function changeRE(){
  var property_id = $('form_property_type_id').options[$('form_property_type_id').selectedIndex].value;
  var action_id = $('form_action_type_id').options[$('form_action_type_id').selectedIndex].value;
//  var item_id = $('form_id').value;
  document.location='/b12.php?property_id='+property_id+'&action_id='+action_id;
}

function CheckSourses(branch){
  var resultObj = document.getElementById('source_text_check_result');
  var url = document.getElementById('form_source_url').value;
  var text = document.getElementById('form_source_text').value;
  var link_url='/ajax.php?action=source_check&type=url&branch=' + branch + '&url=' + url +'&rand=' + Math.round(Math.random()*1000);
  var link_text='/ajax.php?action=source_check&type=text&branch=' + branch + '&text=' + text +'&rand=' + Math.round(Math.random()*1000);
  new Ajax.Updater('source_url_check_result', link_url, {method:'get'});
  new Ajax.Updater('source_text_check_result', link_text, {method:'get'});
//
//  new Ajax.Request(link,
//	{
//		method:'get',
//		onSuccess: function(transport){
//			var response = transport.responseText;
//			if('' != response){
//				resultObj.innerHTML = response;
//			}
//
//		},
//	  onFailure: function(){alert('Something went wrong...')}
//	});
	return false;
}

function ChangeSalaryTitle(select){
	var selected = select.options[select.selectedIndex].value
    var link='/ajax.php?action=salary_name&selected=' + selected +'&rand=' + Math.round(Math.random()*1000);
	new Ajax.Updater('salary_div', link, {method:'get'});
}
