// toggles

Ajax.Responders.register({
  onCreate: function() { showBusyLayer(); },
  onComplete: function() { hideBusyLayer(); }
});

function showBusyLayer() {
     var busyLayer = document.getElementById("busy_layer");
     if (window.innerHeight && window.scrollMaxY) {     
          yScroll = window.innerHeight + window.scrollMaxY;
          var deff = document.documentElement;
          var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
          yScroll -= (window.innerHeight - hff);
     } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){
          yScroll = document.body.scrollHeight;
     } else { 
          yScroll = document.body.offsetHeight;
       }
     if (busyLayer != null) {
          busyLayer.style.visibility = "visible";
          busyLayer.style.height = yScroll;
     }
     document.getElementById('AjaxLoader').style.display='';
}
function hideBusyLayer() {
          var busyLayer = document.getElementById("busy_layer");
          if (busyLayer != null) {
               busyLayer.style.visibility = "hidden";
               busyLayer.style.height = "0px";
          }
          document.getElementById('AjaxLoader').style.display='none';
}


function row_toggle(name)
{
    var showRow = (browser.IE) ? "block" : "table-row";

    for (var i = 0;  document.getElementById(name+'_'+i) != null;  i++)
    {
        if (document.getElementById(name+'_'+i).style.display == "none")
            document.getElementById(name+'_'+i).style.display = showRow;
        else
            document.getElementById(name+'_'+i).style.display = "none";
    }
}

function showhide_field(name, show, hide)
{
	var el = $(name);
    if (el.style.display == "none"  &&  (show == '' || show == undefined)  ||  show == 1)
    {
        el.show();
    }
    else
    {
        el.hide();
    }
	
	if (hide != '')
	{
		$(hide).hide();
	}
}


function ShowHide(id)
{
	if ($(id).style.display == "none")
	{
		$(id).show();
	} else {
		$(id).hide();
	}
} 

function Smilies(Smilie)
{
	$("Smile").value+=Smilie+" ";
	$("Smile").focus();
	$("SmileBox").hide();
}


function show_hide(old_id, new_id)
{
	$(old_id).hide();
	$(new_id).show();
}


function getCountry(virt_path, pr) {

	 region = $('region').value;
	 country = $('country').value;
	 $('country').innerHTML = '<option value="0">Select Country</option>';
	 $('country').disabled=true;
	 $('type').value = "region";
	 
	
	if (region != 0){
	
	 new Ajax.Request(virt_path + 'index.php?m=ajax_search&region=' + region + '&language=' + pr, {
	   method: 'get',
	   onSuccess: function (transport) {
	   get_response = '';
	   get_response += '<select style="width:200px;" onchange="getState(\'' + virt_path + '\', \'' + pr + '\');" name="country" id="country"><option value="0">Select Country</option>';
	   get_response += transport.responseText;
	   get_response += '</select>';
	   $('get_country').innerHTML = '';
	   $('get_country').innerHTML = get_response; 
   
	   }
	 });
	 } else {
	 $('country').innerHTML = '<option value="0">Select Country</option>';
	 $('country').disabled=true;
	 }

	 
		$('state').disabled=true;
		$('city').disabled=true;
	 
	 if(country == 0){
		$('type').value = "region";
	 }
 

}


function getState(virt_path, pr) {

	country = $('country').value;
	state = $('state').value;
	
	if (country != 0){
		$('type').value = "country";
	}
	
	$('state').innerHTML = '<option value="0">Select State/Province</option>';
	$('city').innerHTML = '<option value="0">Select City</option>';
	$('state').disabled=true;
	$('city').disabled=true;
	
	if(country != 0){
	
	new Ajax.Request(virt_path + 'index.php?m=ajax_search&country=' + country + '&language=' + pr, {
			method: 'get',
			onSuccess: function (transport) {
		if(transport.responseText.match('error')){
			$('state').innerHTML = '<option value="0">Select State/Province</option>';
			$('state').disabled=true;
			 getCity(virt_path , "false", pr);
	
		} else {
		   get_response = '';
		   get_response += '<select style="width:200px;" onchange="getCity(\'' + virt_path + '\', \'true\', \'' + pr + '\');" name="state" id="state"><option value="0">Select State/Province</option>';
		   get_response += transport.responseText;
		   get_response += '</select>';
		   $('get_state').innerHTML = '';
		   $('get_state').innerHTML = get_response; 
			 getCity(virt_path , "true", pr);
			}	
		}
	});
	
	}
	
	if(country == 0){
		$('state').innerHTML = '<option value="0">Select State/Province</option>';
		$('city').innerHTML = '<option value="0">Select City</option>';
		$('state').disabled=true;
		$('city').disabled=true;
		$('type').value = "region";
	}

}



function getCity(virt_path , status, pr){
	
	country = $('country').value;
	prov_id = $('state').value;
	city = $('city').value;
	
	$('city').innerHTML = '<option value="0">Select City</option>';
	$('city').disabled=true;
	
	if (status == "true"){
	
	if (prov_id == 0){
			$('city').innerHTML = '<option value="0">Select City</option>';
			$('city').disabled=true;
	} else {
	
		new Ajax.Request(virt_path + 'index.php?m=ajax_search&prov_id=' + prov_id + '&language=' + pr, {
			method: 'get',
			onSuccess: function (transport) {
			
		if(transport.responseText.match('error')){
			$('city').innerHTML = '<option value="0">Select City</option>';
			$('city').disabled=true;

		} else {
		   get_response = '';
		   get_response += ' <select style="width:200px;" name="city" id="city" onchange="getType();"><option value="0">Select City</option>';
		   get_response += transport.responseText;
		   get_response += '</select>';
		   $('get_city').innerHTML = '';
		   $('get_city').innerHTML = get_response; 
			}	
		}
	});
	}
	
	} else if (status == "false"){
	
			new Ajax.Request(virt_path + 'index.php?m=ajax_search&get_city=' + country + '&language=' + pr, {
			method: 'get',
			onSuccess: function (transport) {
			
		if(transport.responseText.match('error')){
			$('city').innerHTML = '<option value="0">Select City</option>';
			$('city').disabled=true;

		} else {
		   get_response = '';
		   get_response += ' <select style="width:200px;" name="city" id="city" onchange="getType();"><option value="0">Select City</option>';
		   get_response += transport.responseText;
		   get_response += '</select>';
		   $('get_city').innerHTML = '';
		   $('get_city').innerHTML = get_response; 
			}	
		}
	});
	
	} else {
	alert("Ajax response error.");
	}
	
	if(prov_id != 0){
		$('type').value = "state";
	}
	else
	{
		$('type').value = "country";
	}
}

// polls
function submit_poll(poll_id, answer_id, virpath)
{
	new Ajax.Request(virpath+'index.php?m=vote&poll_id='+poll_id+'&answer_id='+answer_id, {
		method: 'get',
		onSuccess: function (response) {
			response = response.responseText;
			var response = response.split("\n");
			if ( response.length == 3 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('poll_votes_' + poll_id).innerHTML = response[1];
					$('show_progres').show();
					$('hide_progres').hide();
					var votes = response[2].split(",");
					for ( var i = 0, totalvotes = votes.length; i < totalvotes; i++ ) {
						var counters = votes[i].split("=");
						if ( counters.length == 2 )
						{
							$('progress_'+counters[0]).innerHTML = counters[1] + '%';
							$('progress_size_'+counters[0]).style.width = counters[1] + '%';
						}
					}
					return true;
				}
			}
		}
	});
}

function submit_poll_before(poll_id, answer_id, virpath)
{
	$j("div[id^=poll_answer_]").hide();
	$j("input[id^=answer_txt_]").attr('disabled', 'disabled');
	$j("#poll_answer_"+answer_id).show();
	$j("#answer_txt_"+answer_id).removeAttr('disabled');
}


function getType()
{
	city = $('city').value;
	
	if(city != 0){
		$('type').value = "city";
	}
	else if ($('state').value == 0)
	{
		$('type').value = "country";
	}
	else
	{
		$('type').value = "state";
	}

}


function getRegState(virt_path)
{
	country = $('country').value;

		new Ajax.Request(virt_path + 'index.php?m=ajax_get_state&country='+country, {
			method: 'get',
			onSuccess: function (transport) 
			{
				if (transport.responseText.match('error')){
					$('get_state').innerHTML = '';
					$('get_state').innerHTML = '<input type="text" name="s_p" style="width: 200px" size="40" class="inputText" />';
				} else {
					response = '';
					response += '<select name="s_p" id="s_p" style="width:205px;" class="inputCombo">';
					response +=  transport.responseText;
					response += '</select>';
					$('get_state').innerHTML = '';
					$('get_state').innerHTML = response;
				}	
			}
	});
}	

//------------------------------------------------
// Misc
//------------------------------------------------
function confirmLink(question, url)
{
    var is_confirmed = confirm(question);

    if (is_confirmed && url != '')
	{
		window.location = url;
	}
	
    return is_confirmed;
}

function confirmForm(question, form)
{
    var is_confirmed = confirm(question);

    if (is_confirmed && form != '')
        eval('document.' + form + '.submit()');

    return is_confirmed;
}

function toggleMessages(form, check)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        if (form.elements[i].type == 'checkbox')
        {
            form.elements[i].checked = check;
        }
    }
}

function get_date(ResponseText, date_s, id, cp_path)
{
	new Ajax.Request(cp_path + 'index.php?m=calendar_request&p=show&id=' + id + '&date=' + date_s, {
			method: 'get',
			onSuccess: function (res) {
				$(ResponseText).innerHTML = res.responseText;
			}
	});
}

function get_calendar_edit(id, month, year, image, cp_path)
{
	$('Show_ResponseText').innerHTML = '<img src="' + image + '" style="margin: 20px 100px 20px 100px" />';
	new Ajax.Request(cp_path + 'index.php?m=property&p=show_calendar&id=' + id + '&year=' + year + '&month=' + month, {
		method: 'get',
		onSuccess: function (res) {
			$("Show_ResponseText").innerHTML = res.responseText;
		}
	});
}


function get_calendar_details(id, month, year, image, path, cels)
{
	$('Show_ResponseText').innerHTML = '<img src="' + image + '" style="margin: 20px 100px 20px 100px" />';
	new Ajax.Request(path + 'index.php?m=details&p=show_calendar&id=' + id + '&year=' + year + '&month=' + month + '&cels='+cels, {
		method: 'get',
		onSuccess: function (res) {
			$("Show_ResponseText").innerHTML = res.responseText;
		}
	});
}


function show_date(date_s, DivID, ResponseText, id, e, image, cp_path)
{
	var xdoc = $(DivID);
	xdoc.show();
	$(ResponseText).innerHTML = '<img src="' + image + '" style="margin: 20px 100px 20px 100px" />';
	var IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	var tempX = 0;
	var tempY = 0;
	if (IE) { // grab the x-y pos.s if browser is IE
	tempX = event.clientX + document.body.scrollLeft;
	tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
	tempX = e.pageX;
	tempY = e.pageY;
	}
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	xdoc.style.left = tempX -140 + "px";
	xdoc.style.top = tempY - 120 + "px";
	
	get_date(ResponseText, date_s, id, cp_path);
	return true;
}

function hide_date(DivID) {
	$(DivID).hide();
}


function moveTo(element){
	new Effect.ScrollTo(element,{offset:-140});
}

function stylizeBooleanBox(prefix, boxid)
{
	var options = document.getElementById(prefix + boxid);
	for ( var i = 0, length = options.length; i < length; i++ )
	{
		options[i].style.background = ( options[i].value == 1 ) ? '#DEFAE0' : '#FBDDDD';
		if ( options[i].value == 1 && options[i].selected )
			document.getElementById(prefix + boxid).style.background = '#DEFAE0';
		else if ( options[i].value == 0 && options[i].selected )
			document.getElementById(prefix + boxid).style.background = '#FBDDDD';
	}
}


