var geo_coords_set = false;
function showAddListForm()
{
	if ($('#selectUserLists').val() == 'new')
	{
		$('#divAddNewList').show();
		$('#createnew').val('1');
	}
	else
	{
		$('#divAddNewList').hide();
		$('#createnew').val('0');
	}
}

function submitListForm()
{
	/*if ($('createnew').value == "0" ||  $('txtAddNewList').value.trim() != "")
	{
		window.location = '/places/addtolist/?'+Form.serialize('form_addtolist');
	}
	*/
	new Ajax.Updater('divContainerAdd', '/places/addtolist/?'+Form.serialize('form_addtolist'), {
	  parameters: {  }
	});
}

function SetLocationLoadCities()
{
	$("#city").attr('disabled', true);

	$.get("/member/setlocationgetcities/" + escape($("#country").val())+"/?type=city", function(data){
		if(data.slice(0, 1) != "%")
			return;
		res = data.split("%_%");
		if (res[1] == "city")
		{
			$("#city").attr('disabled', false);
			$("#list-cities").html(res[2]);
			SetLocationLoadDistricts();
		}
	});
}

function SetLocationLoadCitiesStates(hideothers, location)
{
	$("#city").attr('disabled', true);
	var hideothers = (hideothers == null) ? false : eval(hideothers);
	var location = (location == null) ? 'home' : location;
	var str = hideothers ? '&hide=1' : '';
		str = str + '&location='+location;

	$.get("/member/setlocationgetcities/" + escape($('#country').val())+"/?type=stateorcity"+str, function(data){
		SetLocationLoadCitiesStatesComplete(data);
	});
}

function SetLocationLoadCitiesStatesComplete(data)
{
	if(data.slice(0, 1) != "%")
		return;

	res = data.split("%_%");
	if (res[1] == "state")
	{
		$("#divStatesList").show();
		$("#list-states").html(res[2]);
		SetLocationLoadCitiesByState(res[3], res[4]);
	}
	else if (res[1] == "city")
	{
		if (res[3] != "new")
		{
			$("#divStatesList").hide();
			$('#state').val('0');
			$("#list-states").html("");
			isHide = res[3];
		}
		else
			isHide = res[4];
		$("#list-cities").html(res[2]);
		SetLocationLoadDistricts(isHide);
	}
}

function SetLocationLoadCitiesByState(hideothers, location)
{
	var location = (location == null) ? 'home' : location;
	var hideothers = (hideothers == null) ? false : eval(hideothers);
	var str = hideothers ? '&hide=1' : '';
	str = str + '&location='+location;

	$("#city").attr('disabled', true);

	$.get("/member/setlocationgetcities/" + escape($("#state").val())+"/?type=state"+str, function(data){
		if(data.slice(0, 1) != "%")
			return;
		res = data.split("%_%");
		if (res[1] == "city")
		{
			$("#list-cities").html(res[2]);
			SetLocationLoadDistricts(res[3]);
		}
	});
}

function SetLocationLoadDistricts(hideothers)
{
	var hideothers = (hideothers == null) ? false : true;
	if (hideothers)
		var str = '?hide=1';
	$("#district").attr('disabled', true);

	$.get("/member/setlocationgetdistricts/" + escape($("#city").val())+ str, function(data){
		if(data.slice(0, 1) != "_")
			return;

		$("#list-districts").html(data.slice(1));
	});
}

function SetLocationOnChangeCity(hideothers)
{
	var hideothers = (hideothers == null) ? false : true;
	var str = hideothers ? '&hide=1' : '';
	if($("#city").val() == "0")
	{
		$.get("/member/setlocationgetcities/0/?type=city"+str, function(data){
			SetLocationLoadCitiesStatesComplete(data);
		});
	}
	else
		SetLocationLoadDistricts(hideothers);
}

function SetLocationOnChangeDistrict()
{
	if ($('#district').val() == 0)
		$('#list-districts').html('<input id="district" name="district" type="hidden" value="0" />' +
							'<input id="district_text" name="district_text" type="text" value="" class="width180" />' +
							' <small class="likehref" onclick="SetLocationOnChangeCity()">or select from the list...</small>');
}

function AddTagsToField(cbId, tag)
{
	if ($(cbId).checked)
	{
		if ($('txtReviewTags').value.trim() == '')
			$('txtReviewTags').value = tag;
		else
			$('txtReviewTags').value += ', '+tag;
	}
	else
	{
		$('txtReviewTags').value = $('txtReviewTags').value.replace(tag+', ', '');
		$('txtReviewTags').value = $('txtReviewTags').value.replace(tag, '');
	}
}

function submitPublishReview()
{
	$('#errReviewRating').hide();
	$('#errNoReviewName').hide();
	$('#errNoReviewBody').hide();

	if ($('#txtReviewName').val().trim() == '')
	{
		$('#errNoReviewName').show();
		return false;
	}
	if ($('#hdnReviewRating').val() == '0')
	{
		$('#errReviewRating').show();
		return false;
	}
	if ($('#txtReviewBody').val().trim() == '')
	{
		$('#errNoReviewBody').show();
		return false;
	}
	$('#formToReviewStep4').submit();
}

function bigStarClicked(id)
{
	$('#hdnReviewRating').val(id);
	$(".big-rating .star-rating .current").width(15 * id);
}

function unselectall(obj)
{
	var form = $(obj);
	for(i=0;i<form.elements.length;i++){
		if(form.elements[i].checked == true) form.elements[i].checked = false;
	}

}

function SubmitAddPlace()
{
	$('#divChooseGeo').hide();
	$('#imgNext').hide();
	$('#errAddPlaceName').hide();
	$('#errAddPlaceCity').hide();
	$('#errAddPlaceAddress').hide();
	$('#errAddPlacePostcode').hide();

	if ($('#txtAddPlaceName').val() == '')
	{
		$('#errAddPlaceName').show();
		$('#imgNext').show();
		return false;
	}

	if ($('#city').length == 0 || $("#city").get(0).options == null)
	{
		if ($('#city_text').val() == '')
		{
			$('#errAddPlaceCity').show();
			$('#imgNext').show();
			return false;
		}
	}

	if ($('#selectSubcategory option:selected').val() <= 0)
	{
		$('#errAddPlaceSubcategory').show();
		$('#imgNext').show();
		return false;
	}

	if($('#txtAddPlacePostcode').val() == '')
	{
		$('#errAddPlacePostcode').show();
		$('#imgNext').show();
		return false;
	}

	$('#imgNext').attr('disabled', true);

	$('#formAddPlace').submit();
}


function SwitchFileUploadType(uplType)
{
	if (uplType == 'file')
	{
		$('#divVideoCode').hide();
		$('#divVideoFile').show();
		$('#hdnVideoType').val('file');
	}
	else if (uplType == 'code')
	{
		$('#divVideoFile').hide();
		$('#divVideoCode').show();
		$('#hdnVideoType').val('embed');
	}
}

function changeVideosInProfile(divid)
{
	$('#divShowVideo').html( $('#divHiddenVid_'+divid).html() );
}

function TogglePeoplePageHeader()
{
	$('#people_header_1').toggle();
	$('#people_header_2').toggle();
}

function SubmitIf(formid, quest)
{
	if (window.confirm(quest))
		$('#' + formid).submit();
}

function SetCookie(cookieName,cookieValue,nDays)
{
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

var params = null;

function StartParentSearch()
{
	new Ajax.Updater('divSearchResults', '/system/multi/find_parent/?'+Form.serialize('formParentFind'), {
	  parameters: {  }
	});
}

function SetNewParent(id, text, obj)
{
	$('hdnParentID').value = id;
	$('lblParent').innerHTML = text;
	Dialog.close();
}


function updateSubcategoriesList(_selectCategory, _selectSubCategory, _divSubCat, hideSelect)
{
	var selectCategory = (_selectCategory == null) ? "selectCategory" : _selectCategory;
	var selectSubCategory = (_selectSubCategory == null) ? "selectSubcategory" : _selectSubCategory;
	var divSubCat = (_divSubCat == null) ? "subcategory-div" : _divSubCat;
	var hideSelectStr = (hideSelect == null) ? "" : "/hide";
	$('#' + selectSubCategory).attr('disabled', true);

	if ($('#' + selectCategory + ' option:selected').val() != '0')
	{
		$.get("/places/getsubcategories/" + escape($('#' + selectCategory + ' option:selected').val()) + hideSelectStr, function(data){
			res =  data.split('%_%');
			if(res[0].trim() != "ok")
				return;

			if (hideSelectStr)
				$('#' + selectSubCategory).html(res[1]);
			else
				$('#' + divSubCat).html(res[1]);
			$('#' + selectSubCategory).attr('disabled', false);
		});
	}
	else
	{
		$('#' + divSubCat).html('');
		$('#' + selectSubCategory).attr('disabled', false);
	}
}

function SetLocationOnChangeCityEditPlace()
{
	if($("city").value == "0")
	{
		new Ajax.Request(
			"/member/setlocationgetcities/0",
			{
				method: 'get',
				parameters: '',
				onComplete: SetLocationLoadCitiesCompleteEditPlace
			});
	}
	else
	{
		new Ajax.Request(
		"/member/setlocationgetdistricts/" + escape($("city").value)+"?editplace=1",
		{
			method: 'get',
			parameters: '',
			onComplete: SetLocationLoadDistrictsCompleteEditPlace
		});
	}
}

function SetLocationLoadCitiesCompleteEditPlace(req)
{
	if(req.responseText.slice(0, 1) != "_")
		return;

	$("list-cities").innerHTML = req.responseText.slice(1);
	SetLocationLoadDistrictsEditPlace();
}

function SetLocationLoadDistrictsCompleteEditPlace(req)
{
		$("list-districts").innerHTML = req.responseText.slice(1);
}

function OnSelectDistrictEditPlace(num)
{
	if ($("district_"+num).options[$("district_"+num).selectedIndex].value == "-1")
		$('pDistrictContainer_'+num).innerHTML = '<input id="district_'+num+'" name="district_'+num+'" type="hidden" value="0" ><input id="district_text_'+num+'" name="district_text_'+num+'" type="text" value="" class="width180" >';
}

function SaveWiki()
{
	$.post('/places/areawiki/?' + $('#form_areawiki').serialize(), function(data){
		$('#divWiki').html(data);
	});
}


function AjaxUpdater(objId, url, formId)
{
	var str = url;
	if (formId != null)
		str += '/?' + Form.serialize(formId);
	new Ajax.Updater(objId, str, {
	  parameters: {  }
	});
}

function ShowAdZone(zone_id)
{
   	$('#adzone_' + zone_id).show();
}

function HideAdZone(zone_id)
{
   	$('#adzone_' + zone_id).hide();
}

function GetLocationStr(country_id, city_id, citytext_id, address_id, postcode_id)
{
	var location;
	var country = $('#'+country_id).get(0).options[$('#'+country_id).get(0).selectedIndex].innerHTML;
	location = $('#'+address_id).val();
	if (country == "United Kingdom" && $('#'+postcode_id).val() != "")
	{
		if(location != "")
			location += ", ";
		location += $('#'+postcode_id).val();
//		return location + $('#'+postcode_id).val() + ', '+country;
	}

	if($('#'+city_id).length != 0 && $('#'+city_id).get(0).options != null && $('#'+city_id).val() != -1)
	{
		if(location != "")
			location += ", ";
		location +=  $('#'+city_id).get(0).options[$('#'+city_id).get(0).selectedIndex].innerHTML;
	}
	else
	{
		if($('#'+citytext_id).val() != "")
		{
			if(location != "")
				location += ", ";
			location += $('#'+citytext_id).val();
		}
	}
	if(location != "")
		location += ", ";
	if ("Russian Federation" == country)
		country = "Russia";

	location += country;

	return location;
}
var global_longitude_id = '';
var global_latitude_id = '';
var global_to_eval = '';
function SetLocationValues(location, longitude_id, latitude_id, to_eval)
{
	global_longitude_id = longitude_id;
	global_latitude_id  = latitude_id;
	global_to_eval = to_eval;
	var geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			location,
			function(point){
			   	if(point){
					$('#'+global_longitude_id).val(point.x);
					$('#'+global_latitude_id).val(point.y);
					eval(global_to_eval);
				}
			}
		);
}


function LoggerAddRecord(data)
{
	return; // DISABLE LOGGER

	var uniqid = ((new Date()).getTime() + '' + Math.floor(Math.random() * 1000000)).substr(0, 10);
	var params = '?_u=' + uniqid + '&ajax=1' + '&u=' + encodeURIComponent(window.location.href);
	for(var property in data)
		params += '&' + property + '=' + encodeURIComponent(data[property]);

	$.get('/s2' + params);
}

function SwitchAreas(path, alpha, currTab)
{
	$('#divAllTabs a').removeClass('currentpage');
	$('#' + currTab).addClass('currentpage');

	$('#ulMoreAreas').load('/places/moreareas/?alpha=' + alpha + '&browse=' + path + '&dialog');

	return false;
}