$(document).ready(function(){
	
	//----------------------------------------------------------------------------------------
	// Set initial tab
	//----------------------------------------------------------------------------------------
	var address = $("#propertyAddress").val();
	var postcode = $("#propertyPostcode").val();
	var showtab = $("#showtab").val();
	$( "#search_div" ).tabs({selected: 0});
	if(showtab == "advancedsearch") $( "#search_div" ).tabs('option','selected',2);
	else if(showtab == "search") $( "#search_div" ).tabs('option','selected',1);
	else $( "#search_div" ).tabs('option','selected',0);
	$("#search_div").bind('tabsselect', clearTabs);
	
	
	//----------------------------------------------------
	//	Define all the info tips
	//----------------------------------------------------
		
		//postcode info tooltip
		$('#postcodeinfo a[href]').qtip({
			content: 'please enter the first part of the postcode you want to search, e.g. <b>&quot;NW6&quot; or &quot;WV13&quot;</b>',
			style: 'dark'
		});
		
		//address info tooltip
		$('#addressinfo a[href]').qtip({
			content: 'Please enter either the road name or town that you want to search, e.g. <b>&quot;West End Lane&quot;</b> or <b>&quot;West Hampstead&quot;</b>',
			style: 'dark'
		});
		
		
		//-------------------------------------------
		//	go through all the checked ones 
		//	and add them to the selected items div
		//-------------------------------------------
			var countAreaDialog = $(".area-dialog").size();	
			for(var i=1; i<=countAreaDialog; i++){
				var elementDiv = "dialog-form-"+i;
				getSelectedCounties(elementDiv);
				if (countSelectedCounties(elementDiv) > 0) showCountyDialog(elementDiv);
			}
});

function showCountyDialog(id)
{
	var intIndex = id.match(/[0-9]+/);
	var dialogId = "#dialog-form-"+intIndex;
	var linkId = "#areadialog_"+intIndex;
	var selectId = "#selected-counties-"+intIndex;
	var action = $(linkId).text();
	if (action == "[more]") {
		$(linkId).text("[hide]");
//		$(linkId).css("display", "none");
		$(selectId).css('display', 'block');
	} else {
		$(linkId).text("[more]");
		$(selectId).css('display', 'none');
	}
	
	return false;
}

function disableSearch()
{
	var address = $("#propertyAddress").val();
	var postcode = $("#propertyPostcode").val();
	
	//---------------------------------
	//	Validate postcode field
	//---------------------------------
	var pcLength = postcode.length;
	if((pcLength < 2 && pcLength != 0) || pcLength > 4){
		$("#postcodeError").html("Incorrect Postcode");
		$("#postcodeError").css('color', 'red');
		$("#searchform :input").attr('disabled', true);
	}else{
		$("#postcodeError").html("");
		$("#searchform #search_btn").attr('disabled', false);
	}
}

function disableQuickSearch(obj)
{
	var cboxes = $(":checkbox:checked");
}

function filterResBy(url)
{
	if(url != "")
	{
		document.location.href = url;
	}
}

function countSelectedCounties(elementsDiv)
{
	var counter = 0;
	var divNum = $("#"+elementsDiv).attr('id').match(/[0-9]+/);
	$("#"+elementsDiv+" :checkbox").each(function()
	{
		if ($(this).attr('checked')) counter++;
	});
	return counter;
}

function countAllCounties(elementsDiv)
{
	var counter = 0;
	var divNum = $("#"+elementsDiv).attr('id').match(/[0-9]+/);
	$("#"+elementsDiv+" :checkbox").each(function()
	{
		counter++;
	});
	return counter;
}

function getSelectedCounties(elementsDiv)
{
	var countylist = new Array();
	var counties = "";
	var divNum = $("#"+elementsDiv).attr('id').match(/[0-9]+/);
	var linkId = "#areadialog_"+divNum;
	$("#"+elementsDiv+" :checkbox").each(function()
	{
		//alert("ok");
		var selId = $(this).attr('name');
		var selDivId = "selected-counties-"+selId.match(/[0-9]+/);
		var county = $(this).next('label').text();
		var checked = String($(this).attr('checked')).substring(0,1);
		var countyId = $(this).attr('id');
		var cd = "#"+selDivId+"|"+countyId+"|"+checked+county;
		countylist.push(cd);
	});
	if (countSelectedCounties(elementsDiv) == countAllCounties(elementsDiv)) counties = "<div id='toggle-county-"+divNum+"' onclick='toggleAllCounties(this.id)' class='show_county_selected'>"+"Unselect all of "+$("#"+elementsDiv).attr("title")+"</div>";
	else counties = "<div id='toggle-county-"+divNum+"' onclick='toggleAllCounties(this.id)' class='show_county'>"+"Search all of "+$("#"+elementsDiv).attr("title")+"</div>";
	if (countSelectedCounties(elementsDiv) == 0) $(linkId).css('display', 'inline');
	else $(linkId).css('display', 'none');

	//build the selected counties list
	for(i=0;i<countylist.length;i++){
		var selvalues = countylist[i].split("|");
		var countyDiv = selvalues[0];
		var countyId = selvalues[1];
		var checked = String(selvalues[2]).substring(0,1);
		var county = String(selvalues[2]).substring(1);
		if(countyDiv != "" && checked == "t"){
			var countyName = "<div id='show_"+countyId+"' onclick='setCheckBox(this.id)' class='show_county_selected'>"+county+"</div>";
			counties += countyName;
			$(countyDiv).html(counties);
		}
		if(countyDiv != "" && checked != "t"){
			var countyName = "<div id='show_"+countyId+"' onclick='setCheckBox(this.id)' class='show_county'>"+county+"</div>";
			counties += countyName;
			$(countyDiv).html(counties);
		}
	}
	
	var checkedCount = $("#"+elementsDiv+" :checked").length;
	var selCountiesDiv = "selected-counties-"+elementsDiv.match(/[0-9]+/);
	if(checkedCount == 0){
//		$("#"+selCountiesDiv).html("");
	}
	
	//Select all the counties checked and assign it to a hidden field as an array.
	var areaCounties = new Array();
	$("#selectedcounties").val('');
	$(".area-dialog :checked").each(function()
	{
		var county = $(this).val();
		areaCounties.push(county);
		$("#selectedcounties").val(areaCounties);
	});
	
	$(".area-dialog").dialog('close');
}

function toggleAllCounties(id) 
{
	var intIndex = id.match(/[0-9]+/);
	var dialog = 'dialog-form-' + intIndex;
	if ($('#'+id).attr('class') == 'show_county') {
		selectAllCounties(dialog);
		$('#'+id).attr('class', 'show_county_selected');
	}
	else {
		revertAllCounties(dialog);
		$('#'+id).attr('class', 'show_county');
	}

}

function selectAllCounties(dialogId)
{
	$("#"+dialogId+" :checkbox").each(function(){
		$(this).attr('checked', true);
	});
	getSelectedCounties(dialogId);
}

function revertAllCounties(dialogId)
{
	$("#"+dialogId+" :checkbox").each(function(){
		$(this).attr('checked', false);
	});
	getSelectedCounties(dialogId);
}

function clearTabs(event, ui) 
{
	$("#search_div :checkbox:checked").attr('checked', false);
	$("#search_div input[type='text']").val('');
	$("#minprice").val('');
	$("#maxprice").val('');
	$("#selectedcounties").val('');
	$(".area-dialog ").each(function()
	{
		var id = $(this).attr('id');
		// Remove dialog-form- from id
		id = String(id).substring(12);
//		$("#selected-counties-"+id).html('');
		$("#selected-counties-"+id).css('display', 'none');
		$("#areadialog_"+id).text("[more]");
		$("#areadialog_"+id).css('display', 'inline');
	});
	
	$("select[name='tenureType']").attr('selectedIndex', '0').children("option:selected").removeAttr("selected");
	$("select[name='priceRange']").attr('selectedIndex', '0').children("option:selected").removeAttr("selected");
	$("select[name='sizeRange']").attr('selectedIndex', '0').children("option:selected").removeAttr("selected");

	var countAreaDialog = $(".area-dialog").size();	
	for(var i=1; i<=countAreaDialog; i++){
		var elementDiv = "dialog-form-"+i;
		getSelectedCounties(elementDiv);
	}
	$('input[name="orderBy"]').remove();
}

function setCheckBox(id)
{
	var intIndex = id.match(/[0-9]+/);
	var countyId = '#county_'+intIndex;
	var showCountyId = '#show_county_'+intIndex;
	if ($(countyId).attr('checked') == true) {
		$(countyId).attr('checked', false);
		$(showCountyId).attr('class', 'show_county');
	} else {
		$(countyId).attr('checked', true);
		$(showCountyId).attr('class', 'show_county_selected');
	}

	var countiesDiv = $('#'+id).parent().first().attr('id');
	var countiesIndex = countiesDiv.match(/[0-9]+/);
	var linkId = "#areadialog_"+countiesIndex;
	var elementDiv = "dialog-form-"+countiesIndex;
	var toggleId = "#toggle-county-"+countiesIndex;
	if (countSelectedCounties(elementDiv) == 0) {
		$(toggleId).text("Search all of "+$("#"+elementDiv).attr("title"));
		$(toggleId).attr('class', 'show_county');
		$(linkId).css('display', 'inline');
	}
	else $(linkId).css('display', 'none');

	var areaCounties = new Array();
	$("#selectedcounties").val('');
	$(".area-dialog :checked").each(function()
	{
		var county = $(this).val();
		areaCounties.push(county);
		$("#selectedcounties").val(areaCounties);
	});
	return false;
}


