var checklist = false;

function save_listing (link, id) {

	link.firstChild.nodeValue = addLoading; //'Adding...';
	
	$.post (
		'/AJAXUtil.aspx',
		{ 
		action: "SaveProperty",
		propertyID: id },
		function (resp) {
			link.firstChild.nodeValue = msgAddToFav;
			link.href = '/MojeNepremicnine.aspx';
			link.onclick = null;
			link.className += link.className ? ' added' : 'added';
		}
	);

	return false;

}

function save_listing_old (link, id) {

	link.firstChild.nodeValue = 'Adding...';

	$.post (
		'/account/save-listing',
		{ listing_id: id },
		function (resp) {
			link.firstChild.nodeValue = 'Added to My Property! Click to view.';
			link.href = '/account/my-property';
			link.onclick = null;
			link.className += link.className ? ' added' : 'added';
		}
	);

	return false;

}


function save_open_home (link, id) {

	link.firstChild.nodeValue = 'Adding...';

	$.post (
		'/account/save-open-home',
		{ open_home_id: id },
		function (resp) {
			link.firstChild.nodeValue = 'Added to My Open Homes List! Click to view.';
			link.href = '/account/my-property';
			link.onclick = null;
			link.className += link.className ? ' added' : 'added';
		}
	);

	return false;

}

function save_notes (link, id) {

	link.firstChild.nodeValue = 'Saving...';

	var notes = document.getElementById('notes-'+id).value;

	$.post (
		'/account/saved-listings?edit=' + id,
		{ notes: notes },
		function (resp) {
			link.firstChild.nodeValue = 'Notes saved.';
		}
	);

	return false;

}

function redirect (url) {

	location.href = url;

	return false;

}

function confirm_redirect (url, message) {

    if (confirm (message))
		redirect (url);

    //if (confirm (message))
	//    return  (true);
	    
	return false;

}

function confirm_redirect_postback (url, message) {

	if (confirm (message))
	{
	    return true;
	}
		//redirect (url);

    //if (confirm (message))
	//    return  (true);
	    
	return false;

}


var isImage = true;
function image_switcher () {
//alert("init should switch" + this);

   	$("div.imgThumbnails ul li a.noUnderline").click( function () {
	//document.getElementById('primary-image')
	
	    
		
		//document.getElementById('imgMain').innerHTML = "";
		    //////document.getElementById('imgMain').innerHTML = "<a class=\"noUnderline\" id=\"primary-image\" href=\"#/images#image1\" rel=\"nofollow\"><img height=\"316\" alt=\"\" src=\'photos\no-image.jpg\' width=\"515\"></a>";
		
		//document.getElementById('imgMain').innerHTML = "<p class=\"noUnderline\"  id=\"primary-image\"><img height=\"316\" alt=\"\" src=\'photos\no-image.jpg\' width=\"515\"></p>";
		
		document.getElementById('primary-image').innerHTML = "<img  alt=\"\" src=\'/photos/no-image.jpg\' >";
		var pri = document.getElementById('primary-image');
        //alert(pri);
        
        //alert(this.firstChild.src);
        
		pri.href = this.href;
		
		//alert(this.firstChild.name);
		//alert(this.firstChild.id);
		//alert(this.firstChild.src);
		//alert(this.firstChild.value);
		
		//alert(this.id);
		
		pri.firstChild.src = this.firstChild.src.replace('photos2','photos');
		pri.firstChild.alt = this.firstChild.alt;

		return false;
	});


$("div.imgThumbnails ul li a.noUnderlineVideo").click( function () {

	
	    //alert("calling...");
		var temp = this.firstChild.src.replace('http://','');
	    temp = temp.replace('https://','');
		temp = temp.split('/');
		temp = temp[temp.length-1];
		//s3.addParam("wmode","transparent");
	    s3.addVariable("image","/videos/Thumbs/" + temp);
	    
	    temp = temp = temp.split('.');
		temp = temp[0]+ ".flv";
		s3.addVariable("file","/videos/FLV/" + temp);
	    //s3.addVariable('autostart','false');
	    s3.write("primary-image");
		return false;
	});



}

function image_switcher_old_floatRight () {
    //alert("init should switch" + this.firstChild);
	$("div.imgThumbnails ul li a").click( function () {
	//document.getElementById('primary-image')
	
	    //alert("good ho gaya hai");

		var pri = document.getElementById('primary-image');

        //alert(pri);
        
		pri.href = this.href;
		
		//alert(this.firstChild.name);
		//alert(this.firstChild.id);
		//alert(this.firstChild.src);
		//alert(this.firstChild.value);
		
		//alert(this.id);
		
		pri.firstChild.src = this.firstChild.src.replace('photos2','photos');
		pri.firstChild.alt = this.firstChild.alt;

		return false;
	});

}



function image_switcher_dataList(temp)
{
	return false;

}

function Node (id, label) {

	this.id = id;
	this.label = label;
	this.children = new Array ();

}

function createElement (type, name, checked) {

	var element = null;

	try {
		element = document.createElement('<input runat="server" type="'+type+'" id="'+name+'" name="'+name+'"'+(checked ? ' checked="checked"' : '')+'>');
		//alert(element.name);
	} catch (e) {}

	if (!element || element.nodeName != 'INPUT') {
		element = document.createElement('input');
		element.type = type;
		element.name = name;
		element.checked = checked;
	}

	return element;

}

function nodeChecklist (nodes, element_id, checked_id) {


	var element = document.getElementById(element_id);
    //alert(nodes.length +":" + element_id +":"+ checked_id );
	if (nodes.length > 0) {

		var ul = $.UL({id: element_id, className: 'checklist'});

		for (var i = 0; i < nodes.length; i++) {

			var label = document.createElement ('label');
			label.setAttribute ('for', element_id + '_' + nodes[i].id);
			
			

			var input = createElement ('checkbox', element_id+'[]', false);
			
			//alert(element_id);
			
			input.id = element_id + '_' + nodes[i].id;
			input.value = nodes[i].id;
			input.checked = (checked_id && nodes[i].id == checked_id);

			label.appendChild (input);

			label.appendChild (
				document.createTextNode (nodes[i].label)
			);

			label.onmouseover = function() { this.className = 'hover' }
			label.onmouseout = function() { this.className = '' }
			
			//alert("input.id " +input.id +":"+input.value);

			ul.appendChild ($.LI( {}, label ));
			//alert(ul.innerHTML);
			//document.write(ul.innerHTML);
			//debug.write(ul.innerHTML);

		}

		element.parentNode.replaceChild (ul, element);
		

	} else {

		var p = $.P( {id: element_id}, varPropertyTypeSubscription );

		element.parentNode.replaceChild (p, element);

	}

	return true;

}

function nodeSelectBox (nodes, element_id, selected_id, all_text, disabled) {

	//alert("nodeSelectBox" + nodes.length + " for " + element_id);
	var element = document.getElementById(element_id);
	var select = $.SELECT({id: element_id, name: element_id});
	var child_nodes = new Array ();

	select.appendChild ($.OPTION({
		text: all_text,
		innerText: all_text,
		value: '',
		selected: !selected_id
	}));

	for (var i = 0; i < nodes.length; i++) {

		var selected = (selected_id && nodes[i].id == selected_id);

		if (selected)
			child_nodes = nodes[i].children;

		select.appendChild ($.OPTION({
			text: nodes[i].label,
			innerText: nodes[i].label,
			value: nodes[i].id,
			selected: selected
		}));

    //alert("value: " + nodes[i].id);
	}

	if (disabled)
		select.disabled = true;

	element.parentNode.replaceChild (select, element);

	return child_nodes;
}

function changeRegion () {

	var districts = new Array ();
	var region_id = this.options[this.selectedIndex].value;
	
	document.getElementById("ctl00_hidSearchRegion").value = document.getElementById("region_id").value;
	
	document.getElementById("ctl00_hidSearchDistrict").value = "false";
	document.getElementById("ctl00_hidSearchSuburb").value = "false";
	
	//alert(document.getElementById("ctl00_hidSearchRegion").value);

	for (var i = 0; i < regions.length; i++) {

		if (regions[i].id == region_id)
			districts = regions[i].children;

	}

	nodeSelectBox (districts, 'district_id', false, allDistricts, !region_id);

	if (is_property) {

		document.getElementById('district_id').onchange = changeDistrict;

		if (checklist)
			nodeChecklist (new Array (), 'suburb_id');
		else
			nodeSelectBox (new Array (), 'suburb_id', false, allSuburbs, true);

	}

}


function changePropertyRegion () 
{
    //alert("ch-reg");
	var districts = new Array ();
	var region_id = this.options[this.selectedIndex].value;

	for (var i = 0; i < regions.length; i++) {

		if (regions[i].id == region_id)
			districts = regions[i].children;

	}

	nodeSelectBox (districts, 'ctl00_ContentPlaceHolder1_txtPropertySubRegionId', false, selectDistrict, !region_id);
	document.getElementById("ctl00_ContentPlaceHolder1_hidRegion").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyRegionId").value;
	
	

	
//if (is_property) {
	
	   	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubRegionId').onchange = changePropertyDistrict;

		if (checklistA)
			nodeChecklist (new Array (), 'ctl00_ContentPlaceHolder1_txtPropertySubrubId');
		else
			nodeSelectBox (new Array (), 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', false, selectSuburbs, true);

//	}

}
function changeRegionAdmin () 
{
   	var districts = new Array ();
	var region_id = this.options[this.selectedIndex].value;

	for (var i = 0; i < regions.length; i++) {

		if (regions[i].id == region_id)
			districts = regions[i].children;
	}

	nodeSelectBox (districts, 'district_id', false, selectDistrict, !region_id);
	//document.getElementById("ctl00_ContentPlaceHolder1_hidRegion").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyRegionId").value;
	
	document.getElementById("ctl00_hidSearchRegion").value = document.getElementById("region_id").value;
	//alert(document.getElementById("ctl00_hidSearchRegion").value);
	
	document.getElementById("ctl00_hidSearchDistrict").value = "false";
	document.getElementById("ctl00_hidSearchSuburb").value = "false";
	
	
	//alert("propertyRegionCHANGESs" + document.getElementById("ctl00_ContentPlaceHolder1_hidRegion").value);


	
//if (is_property) {
	
	    //alert("chtest" + checklistA);
		document.getElementById('district_id').onchange = changeDistrictAdmin;

		if (checklistAdmin)
			nodeChecklist (new Array (), 'suburb_id');
		else
			nodeSelectBox (new Array (), 'suburb_id', false, selectSuburbs, true);

//	}

}


function changeDistrict () {

	var suburbs = new Array ();
	var district_id = this.options[this.selectedIndex].value;
	
	document.getElementById("ctl00_hidSearchDistrict").value = district_id; //document.getElementById("region_id").value;
	
	document.getElementById("ctl00_hidSearchSuburb").value = "false";
	//alert(document.getElementById("ctl00_hidSearchDistrict").value)
	
	var region = document.getElementById('region_id');
	var region_id = region.options[region.selectedIndex].value;

	for (var i = 0; i < regions.length; i++) {

		if (regions[i].id == region_id) {

			for (var j = 0; j < regions[i].children.length; j++) {

				if (regions[i].children[j].id == district_id)
					suburbs = regions[i].children[j].children;

			}

		}

	}

	if (checklist)
	{
		nodeChecklist (suburbs, 'suburb_id');
		document.getElementById('suburb_id').onchange = changeSearchSuburb;
		//alert(document.getElementById('suburb_id').onchange + "3");
	}
	else
	{
		nodeSelectBox (suburbs, 'suburb_id', false, allSuburbs, !district_id);
		document.getElementById('suburb_id').onchange = changeSearchSuburb;
		//alert(document.getElementById('suburb_id').onchange + "4");
	}

}




function changePropertyDistrict () {


    //alert("propertyDistrinctCHANGESs");
	var suburbs = new Array ();
	var district_id = this.options[this.selectedIndex].value;
	var region = document.getElementById('ctl00_ContentPlaceHolder1_txtPropertyRegionId');
	var region_id = region.options[region.selectedIndex].value;

	for (var i = 0; i < regions.length; i++) {

		if (regions[i].id == region_id) {

			for (var j = 0; j < regions[i].children.length; j++) {

				if (regions[i].children[j].id == district_id)
					suburbs = regions[i].children[j].children;

			}

		}

	}

	if (checklistA)
	{
		nodeChecklist (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId');
		//document.getElementById("ctl00_ContentPlaceHolder1_hidSuburb").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubrubId").value;
	}
	else
	{
		nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', false, selectSuburbs, !district_id);
		document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubrubId').onchange = changeSuburbAdmin;
		
		
	}
		
		document.getElementById("ctl00_ContentPlaceHolder1_hidSubRegion").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubRegionId").value;

}

function changeDistrictAdmin () {


    var suburbs = new Array ();
	var district_id = this.options[this.selectedIndex].value;
	var region = document.getElementById('region_id');
	var region_id = region.options[region.selectedIndex].value;

	for (var i = 0; i < regions.length; i++) {

		if (regions[i].id == region_id) {

			for (var j = 0; j < regions[i].children.length; j++) {

				if (regions[i].children[j].id == district_id)
					suburbs = regions[i].children[j].children;

			}

		}

	}

	if (checklistAdmin)
	{
		nodeChecklist (suburbs, 'suburb_id');
		//document.getElementById("ctl00_ContentPlaceHolder1_hidSuburb").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubrubId").value;
	}
	else
	{
		nodeSelectBox (suburbs, 'suburb_id', false, selectSuburbs, !district_id);
		document.getElementById('suburb_id').onchange = changeSuburbAdmin;
		
		
	}
		
		document.getElementById("ctl00_hidSearchDistrict").value = document.getElementById("district_id").value;
		

}

function changeSuburb()
{
    
    //alert("change suburb");
    //document.getElementById("ctl00_ContentPlaceHolder1_hidSuburb").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubrubId").value;
    document.getElementById("ctl00_hidSearchSuburb").value = document.getElementById("suburb_id").value;
}

function changeSuburbAdmin()
{
    document.getElementById("ctl00_ContentPlaceHolder1_hidSuburb").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubrubId").value;
}


function changeSearchSuburb()
{
    document.getElementById("ctl00_hidSearchSuburb").value = document.getElementById("suburb_id").value;
}

function search_options_admin (category_id, listing_sub_type_id, region_id, district_id, suburb_id) 
{

	//is_property = (listing_type_id != 4);
	//is_lifestyle = (listing_sub_type_id == 5);
	is_property = true;
	is_lifestyle = (listing_sub_type_id == 5);
	
	var districts = nodeSelectBox (regions, 'region_id', region_id, allRegions, false);
	document.getElementById('region_id').onchange = changeRegionAdmin;
	var suburbs = nodeSelectBox (districts, 'district_id', district_id, allDistricts, !region_id);
	document.getElementById('district_id').onchange = changeDistrictAdmin;
    
    if (checklistAdmin)
	{
		nodeChecklist (suburbs, 'suburb_id', suburb_id);
		document.getElementById('suburb_id').onchange = changeSuburbAdmin;
	}
	else
	{
		nodeSelectBox (suburbs, 'suburb_id', suburb_id, allSuburbs, (!region_id || !district_id));
		document.getElementById('suburb_id').onchange = changeSuburbAdmin;
	}

	var listing_sub_types = new Array ();
	for (var i = 0; i < listing_types.length; i++) 
	{
		if (listing_types[i].id == listing_type_id)
			listing_sub_types = listing_types[i].children;
	}
	var category = nodeSelectBox (listing_types, 'category_id', category_id, selectCategory, false);
	document.getElementById('category_id').onchange = changeCategoryAdmin;
	
	//document.getElementById("ctl00_ContentPlaceHolder1_hidCategory").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyCategoryId").value;
	
	if (is_property) {

		nodeSelectBox (listing_sub_types, 'listing_sub_type_id', listing_sub_type_id, 'Any property type', !category_id);

	} else {

		nodeSelectBox (listing_sub_types, 'listing_sub_type_id', listing_sub_type_id, 'Any business type', !category_id);

	}
	document.getElementById('listing_sub_type_id').onchange = changeSearchSubCategory;

}

function search_options (purpose_id, category_id,listing_sub_type_id, region_id, district_id, suburb_id) {

	//is_property = (listing_type_id != 4);
	//is_lifestyle = (listing_sub_type_id == 5);
	
	is_property = true;
	is_lifestyle = (listing_sub_type_id == 5);
	
    nodeSelectBox (purpose, 'purpose_id', purpose_id, allPurpose, false);
    document.getElementById('purpose_id').onchange = changePurpose;
	
	var districts = nodeSelectBox (regions, 'region_id', region_id, allRegions, false);
	document.getElementById('region_id').onchange = changeRegion;

	var suburbs = nodeSelectBox (districts, 'district_id', district_id, allDistricts, !region_id);
	document.getElementById('district_id').onchange = changeDistrict;

    
    
    var category = nodeSelectBox (listing_types, 'search_category_id', category_id, anyPropertyMainType, false);
	document.getElementById('search_category_id').onchange = changeCategorySearch;
	

	if (is_property) {

		if (checklist)
		{
			nodeChecklist (suburbs, 'suburb_id', suburb_id);
			document.getElementById('suburb_id').onchange = changeSearchSuburb;
			//alert(document.getElementById('suburb_id').onchange + "1");
		}
		else
		{
			nodeSelectBox (suburbs, 'suburb_id', suburb_id, allSuburbs, (!region_id || !district_id));
			document.getElementById('suburb_id').onchange = changeSearchSuburb;
			//alert(document.getElementById('suburb_id').onchange + "2");
		}

	}
	



	var listing_sub_types = new Array ();
	for (var i = 0; i < listing_types.length; i++) {

		if (listing_types[i].id == listing_type_id)
			listing_sub_types = listing_types[i].children;

	}
	
	

	if (is_property) {

		nodeSelectBox (listing_sub_types, 'listing_sub_type_id', listing_sub_type_id, anyPropertyType, !category_id);

	} else {

		nodeSelectBox (listing_sub_types, 'listing_sub_type_id', listing_sub_type_id, 'Any business type', !category_id);

	}
	
	document.getElementById('listing_sub_type_id').onchange = changeSearchSubCategory;

}

function changeSearchSubCategory()
{
    
    document.getElementById("ctl00_hidSubCategory").value = document.getElementById("listing_sub_type_id").value;
    //alert(document.getElementById("ctl00_hidSubCategory").value);
}

function changePurpose()
{
    
    document.getElementById("ctl00_hidPurpose").value = document.getElementById("purpose_id").value;
    //alert(document.getElementById("ctl00_hidSubCategory").value);
}

function test(listing_sub_type_id, region_id, district_id, suburb_id) {

	is_property = (listing_type_id != 4);
	is_lifestyle = (listing_sub_type_id == 5);

	//var districts = nodeSelectBox (regions, 'propertyRegionId', region_id, selectRegion, false);
	var districts = nodeSelectBox (regions, 'ctl00_ContentPlaceHolder1_txtPropertyRegionId', region_id, selectRegion, false);
	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertyRegionId').onchange = changePropertyRegion;

	var suburbs = nodeSelectBox (districts, 'ctl00_ContentPlaceHolder1_txtPropertySubRegionId', district_id, selectDistrict, !region_id);
	document.getElementById('district_id').onchange = changeDistrict;

	
	//if (is_property) {

		if (checklistA)
			nodeChecklist (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id);
		else
			nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id, selectSuburbs, (!region_id || !district_id));

	//}

	var cateory = nodeSelectBox (listing_types, 'ctl00_ContentPlaceHolder1_txtPropertyCategoryId', listing_types, selectCategory, false);
	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertyCategoryId').onchange = changeCategory;
	
	
	    if (checklistA)
			nodeChecklist (new Array (), 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id);
		else
		{
			nodeSelectBox (new Array (), 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', false, selectSubCategory, true);
		
		}
		
		//document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange = changeSubCategory;

	
	/*if (!checklistA)
	{
	    nodeChecklist (category, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id);
	}
	else
	{
		//nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id, 'All suburbs', (!region_id || !district_id));
    	nodeSelectBox (new Array(), 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id, 'Select Sub-category', true);
	}
	*/
	
	
	
	//nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', suburb_id, 'Select Sub-category', true);

}
function fillCategories(category_id)
{
    var category = nodeSelectBox (listing_types, 'ctl00_ContentPlaceHolder1_ddlCategory', category_id, 'All', false);
}

function testSelect(category_id, listing_sub_type_id, region_id, district_id, suburb_id) {

	is_property = (listing_type_id != 4);
	is_lifestyle = (listing_sub_type_id == 5);
    //var districts = nodeSelectBox (regions, 'propertyRegionId', region_id, selectRegion, false);
	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertyRegionId').onchange = changePropertyRegion;
	var districts = nodeSelectBox (regions, 'ctl00_ContentPlaceHolder1_txtPropertyRegionId', region_id, selectRegion, false);
	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertyRegionId').onchange = changePropertyRegion;
	
	document.getElementById("ctl00_ContentPlaceHolder1_hidRegion").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyRegionId").value;
	
	//alert("test-select" + document.getElementById("ctl00_ContentPlaceHolder1_hidRegion").value);

	var suburbs = nodeSelectBox (districts, 'ctl00_ContentPlaceHolder1_txtPropertySubRegionId', district_id, selectDistrict, !region_id);
	
	//document.getElementById('district_id').onchange = changeDistrict;
	
	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubRegionId').onchange = changePropertyDistrict;
	
	
	document.getElementById("ctl00_ContentPlaceHolder1_hidSubRegion").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubRegionId").value;
    
    
    	if (checklistA)
		{
		    nodeChecklist (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId');
			//nodeChecklist (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id);
		}
		else
		{
			//nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id, selectSuburbs, (!region_id || !district_id));
			//document.getElementById("ctl00_ContentPlaceHolder1_hidSuburb").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubrubId").value;
			//nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', false, selectSuburbs, !district_id);
			nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id, selectSuburbs, !district_id);
		    document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubrubId').onchange = changeSuburbAdmin;
		    //document.getElementById("ctl00_ContentPlaceHolder1_hidSuburb").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubrubId").value;
		    document.getElementById("ctl00_ContentPlaceHolder1_hidSuburb").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubrubId").value;
		}

	//}

	
	var category = nodeSelectBox (listing_types, 'ctl00_ContentPlaceHolder1_txtPropertyCategoryId', category_id, selectCategory, false);
	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertyCategoryId').onchange = changeCategory;
	
	document.getElementById("ctl00_ContentPlaceHolder1_hidCategory").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyCategoryId").value;
	
	
	var subCat = nodeSelectBox (category, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id, selectSubCategory, !category_id);
	document.getElementById("ctl00_ContentPlaceHolder1_hidSubCategory").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubCategoryId").value;
	
	document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange = changeSubCategoryAdmin;
	
	
	    /*if (checklistA)
			nodeChecklist (new Array (), 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id);
		else
		{
			nodeSelectBox (new Array (), 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', false, 'Select Sub-category', !category_id);
		
		}
		
		//document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange = changeSubCategory;

	
	/*if (!checklistA)
	{
	    nodeChecklist (category, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id);
	}
	else
	{
		//nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubrubId', suburb_id, 'All suburbs', (!region_id || !district_id));
    	nodeSelectBox (new Array(), 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id, 'Select Sub-category', true);
	}
	*/
	
	
	
	//nodeSelectBox (suburbs, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', suburb_id, 'Select Sub-category', true);
	//changeCategory();

}




    function changeSubCategory()
    {
        document.getElementById("ctl00_hidSubCategory").value = document.getElementById("listing_sub_type_id").value
    }
    
    function changeSubCategoryAdmin()
    {
       document.getElementById("ctl00_ContentPlaceHolder1_hidSubCategory").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertySubCategoryId").value
        //document.getElementById("ctl00_hidSubCategory").value = document.getElementById("listing_sub_type_id").value
    }

function changeCategory()
{
    //alert("chaing cate");
    
	var districts = new Array ();
	var region_id = this.options[this.selectedIndex].value;

	//alert(region_id + listing_types.length);

	for (var i = 0; i < listing_types.length; i++) {


		if (listing_types[i].id == region_id)
			districts = listing_types[i].children;

	}
	//alert(districts.length);
	//nodeSelectBox (districts, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', false, 'Select Sub-category', !region_id);
	document.getElementById("ctl00_ContentPlaceHolder1_hidCategory").value = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyCategoryId").value;
	
	
	//document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange = changeSubCategory;
	
	//alert(document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange);
	
	
	//alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyCategoryId").value);
	var category = document.getElementById("ctl00_ContentPlaceHolder1_txtPropertyCategoryId").value;
	
	//var subCategory = 
	
	if(!checklistA)
	{
	    setFormOnCategoryChange(category);
    }
	
	
	//alert(document.getElementById('location').className);
	//document.getElementById('location').style.display= false;

//	if (is_property) {

		//document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubRegionId').onchange = changePropertyDistrict;

		if (checklistA)
			nodeChecklist (districts, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId');
		else
		{
			nodeSelectBox (districts, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', false, selectSubCategory, false);
			document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange = changeSubCategoryAdmin;//changeSubCategory;
		}

	//}

    //alert(document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange);

/*
	
	var listing_sub_types = new Array ();
	for (var i = 0; i < listing_types.length; i++) {

		if (listing_types[i].id == listing_type_id)
			listing_sub_types = listing_types[i].children;

	}

	if (is_property) {

		nodeSelectBox (listing_sub_types, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', ctl00_ContentPlaceHolder1_txtPropertySubCategoryId, 'Any property type', false);

	} else {

		nodeSelectBox (listing_sub_types, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id, 'Any business type', false);

	}
*/


	
}
function setFormOnCategoryChange(category)
{
    if(category == "1")//Flat
	    {
	        document.getElementById("divFloorArea").style.display = "block";
	        document.getElementById("divGroundArea").style.display = "none";
	        document.getElementById("divPlacement").style.display = "block";
	        document.getElementById("divYearOfBuild").style.display = "block";
	        ValidatorEnable($("#ctl00_ContentPlaceHolder1_reqYearofBuild")[0], true);
	        //alert("setting inline");
	    }
	    else if (category == "2")//House
	    {
	        document.getElementById("divFloorArea").style.display = "block";
	        document.getElementById("divGroundArea").style.display = "block";
	        document.getElementById("divPlacement").style.display = "none";
	        document.getElementById("divYearOfBuild").style.display = "block";
	        ValidatorEnable($("#ctl00_ContentPlaceHolder1_reqYearofBuild")[0], true);
	        //alert("hiding");
	    }
    	
	    else if (category == "3")//vikend
	    {
	        document.getElementById("divFloorArea").style.display = "block";
	    
	        document.getElementById("divGroundArea").style.display = "block";
	        document.getElementById("divPlacement").style.display = "none";
	        document.getElementById("divYearOfBuild").style.display = "block";
	        ValidatorEnable($("#ctl00_ContentPlaceHolder1_reqYearofBuild")[0], true);
	    }
	    else if (category == "4") //Ground
	    {
	        document.getElementById("divFloorArea").style.display = "none";
	        document.getElementById("divGroundArea").style.display = "block";
	        document.getElementById("divPlacement").style.display = "none";
	        
	        document.getElementById("divYearOfBuild").style.display = "none";
	        ValidatorEnable($("#ctl00_ContentPlaceHolder1_reqYearofBuild")[0], false);
	        
	    }
	    else if (category == "5")//Business
	    {
	        document.getElementById("divFloorArea").style.display = "block";
	        document.getElementById("divGroundArea").style.display = "none";
	        document.getElementById("divPlacement").style.display = "none";
	        
	        document.getElementById("divYearOfBuild").style.display = "block";
	        ValidatorEnable($("#ctl00_ContentPlaceHolder1_reqYearofBuild")[0], true);
	        
	    }
	    else if(category == "6") //Garage
	    {
	        document.getElementById("divFloorArea").style.display = "block";
	        document.getElementById("divGroundArea").style.display = "block";
	        document.getElementById("divPlacement").style.display = "block";
	        document.getElementById("divYearOfBuild").style.display = "block";
	        ValidatorEnable($("#ctl00_ContentPlaceHolder1_reqYearofBuild")[0], true);
	    }
    	
	    else if(category == "7")//Holiday Appartment
	    {
	        document.getElementById("divFloorArea").style.display = "block";
	        document.getElementById("divGroundArea").style.display = "block";
	        document.getElementById("divPlacement").style.display = "none";
	        document.getElementById("divYearOfBuild").style.display = "block";
	        ValidatorEnable($("#ctl00_ContentPlaceHolder1_reqYearofBuild")[0], true);
	    }
}


function changeCategoryAdmin()
{
    var districts = new Array ();
	var region_id = this.options[this.selectedIndex].value;

	//alert(region_id + listing_types.length);

	for (var i = 0; i < listing_types.length; i++) {


		if (listing_types[i].id == region_id)
			districts = listing_types[i].children;

	}
	
	document.getElementById("ctl00_hidCategory").value = document.getElementById("category_id").value;
	var category = document.getElementById("category_id").value;
	
	document.getElementById("ctl00_hidSubCategory").value = false;
	//var subCategory = 
	
	if(!search_options_admin)
	{
	
    }
	
	
	//alert(document.getElementById('location').className);
	//document.getElementById('location').style.display= false;

//	if (is_property) {

		//document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubRegionId').onchange = changePropertyDistrict;

		if (checklistAdmin)
			nodeChecklist (districts, 'listing_sub_type_id');
		else
		{
			nodeSelectBox (districts, 'listing_sub_type_id', false, selectSubCategory, !region_id);
			document.getElementById('listing_sub_type_id').onchange = changeSubCategoryAdmin;
		}

	//}

    //alert(document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange);

/*
	
	var listing_sub_types = new Array ();
	for (var i = 0; i < listing_types.length; i++) {

		if (listing_types[i].id == listing_type_id)
			listing_sub_types = listing_types[i].children;

	}

	if (is_property) {

		nodeSelectBox (listing_sub_types, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', ctl00_ContentPlaceHolder1_txtPropertySubCategoryId, 'Any property type', false);

	} else {

		nodeSelectBox (listing_sub_types, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id, 'Any business type', false);

	}
*/


	
}


function changeCategorySearch()
{
    
    var districts = new Array ();
	var region_id = this.options[this.selectedIndex].value;

	for (var i = 0; i < listing_types.length; i++) {


		if (listing_types[i].id == region_id)
			districts = listing_types[i].children;

	}
	
	//document.getElementById("ctl00_hidSearchCategory").value = document.getElementById("category_id").value;
	document.getElementById("ctl00_hidSearchCategory").value = document.getElementById("search_category_id").value;
	
	
	var category = document.getElementById("search_category_id").value;
	
	document.getElementById("ctl00_hidSubCategory").value = false;
	
//	if (is_property) {

		//document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubRegionId').onchange = changePropertyDistrict;

		if (checklist)
			nodeChecklist (districts, 'listing_sub_type_id');
		else
		{
			nodeSelectBox (districts, 'listing_sub_type_id', false, anyPropertyType, !region_id);
			document.getElementById('listing_sub_type_id').onchange = changeSubCategory;
		}

	//}

    //alert(document.getElementById('ctl00_ContentPlaceHolder1_txtPropertySubCategoryId').onchange);

/*
	
	var listing_sub_types = new Array ();
	for (var i = 0; i < listing_types.length; i++) {

		if (listing_types[i].id == listing_type_id)
			listing_sub_types = listing_types[i].children;

	}

	if (is_property) {

		nodeSelectBox (listing_sub_types, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', ctl00_ContentPlaceHolder1_txtPropertySubCategoryId, 'Any property type', false);

	} else {

		nodeSelectBox (listing_sub_types, 'ctl00_ContentPlaceHolder1_txtPropertySubCategoryId', listing_sub_type_id, 'Any business type', false);

	}
*/
}
function setMapLocation (region_id, district_id) {

	var districts = nodeSelectBox (regions, 'region_id', region_id, allRegions, false);
	document.getElementById('region_id').onchange = changeRegion;

	var suburbs = nodeSelectBox (districts, 'district_id', district_id, allDistricts, false);
	document.getElementById('district_id').onchange = changeDistrict;

	if (is_property)
		nodeSelectBox (suburbs, 'suburb_id', false, allSuburbs, (!region_id || !district_id));

	var listing_sub_type = document.getElementById('listing_sub_type_id');
	var listing_sub_type_id = listing_sub_type.options[listing_sub_type.selectedIndex].value;


	var ajaxListings = document.getElementById('ajaxListings');

	if (region_id) {

		ajaxListings.innerHTML = '<p>Loading listings...<img src="/img/template/throbber.gif" class="throbber" /></p>';

		var listings_url = "/ajax/listings?listing_type_id="+listing_type_id+"&listing_sub_type_id="+listing_sub_type_id+"&region_id="+region_id+"&district_id="+district_id;

		$.get( listings_url, function (data) {
			ajaxListings.innerHTML = data;
		});

	} else {

		ajaxListings.innerHTML = '<p>Select a region or district from the map to load listings.</p>';

	}


	var ajaxSuburbs = document.getElementById('ajaxSuburbs');

	if (district_id) {

		ajaxSuburbs.innerHTML = '<p>Loading suburbs...<img src="/img/template/throbber.gif" class="throbber" /></p>';

		var suburbs_url = "/ajax/suburbs?listing_type_id="+listing_type_id+"&listing_sub_type_id="+listing_sub_type_id+"&region_id="+region_id+"&district_id="+district_id;

		$.get( suburbs_url, function (data) {
			ajaxSuburbs.innerHTML = data;
		});

	} else {

		ajaxSuburbs.innerHTML = '';

	}

}

function appendStylesheet (href, media) {

	var link = document.createElement ('link');
	link.setAttribute ('rel', 'stylesheet');
	link.setAttribute ('type', 'text/css');
	link.setAttribute ('href', href);
	link.setAttribute ('media', media);

	document.getElementsByTagName('head')[0].appendChild(link);

}

function addLoadEvent (func) {

	var oldonload = window.onload;

	if (typeof window.onload != 'function') {

		window.onload = func;

	} else {

		window.onload = function() {

			if (oldonload)
				oldonload ();

			func ();

		}

	}

}

function deleteChildNodes (parent) {

	if (parent == undefined || parent == null)
		return;

	while (parent.firstChild)
		parent.removeChild (parent.firstChild);

}

$(document).ready(function()
{
	//Popup windows, just add popup class to any link (replaces the old target=_blank)
	$('a.popup').bind('click',function(){
		window.open(this.href);
		return false;
	});
});

function goBack()
    {
        history.back();
    }
    
function setFirstImage()
{
   var thumbImg = document.getElementById("firstImageDisplay");
   
   //document.getElementById('primary-image').innerHTML = "<img  alt=\"\" src=\'photos\no-image.jpg\' >";
   
   //thumbImg.click();
   if(thumbImg != null)
   {
       document.getElementById("gallery").style.display = "inline";
       if(thumbImg.className == "noUnderline")
       {
     	    document.getElementById('primary-image').innerHTML = "<img  alt=\"\" src=\'/photos/no-image.jpg\' >";
	        var pri = document.getElementById('primary-image');
    	    pri.href = this.href;
		    pri.firstChild.src = thumbImg.firstChild.src.replace('photos2','photos');
		    pri.firstChild.alt = thumbImg.firstChild.alt;
		    return false;
       }
       else if(thumbImg.className == "noUnderlineVideo")
       {
       
            var temp = thumbImg.firstChild.src.replace('http://','');
	        temp = temp.replace('https://','');
		    temp = temp.split('/');
		    temp = temp[temp.length-1];
		    //s3.addParam("wmode","transparent");
	        s3.addVariable("image","/videos/Thumbs/" + temp);
    	    
	        temp = temp = temp.split('.');
		    temp = temp[0]+ ".flv";
		    s3.addVariable("file","/videos/FLV/" + temp);
	        s3.addVariable('autostart','true');
    	    
	        s3.write("primary-image");
		    return false;

       }
       
   }
   else
   {
        document.getElementById("gallery").style.display = "none";
        //alert(document.getElementById("gallery").style.display);
   }
}
function SetUniqueRadioButton(nameregex, current, agentID)
 {
	re = new RegExp(nameregex);  
  	for(i = 0; i < document.forms[0].elements.length; i++) 
	{

		elm = document.forms[0].elements[i]

		if (elm.type == 'radio') 
		{
			if (re.test(elm.name)) 
			{
				elm.checked = false;
				
			}
		}
	}
	//alert(agentID);
			
	current.checked = true;
	document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedAgent").value = agentID;
    //alert(document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedAgent").value);
}

function SetAgentIDInHiddenField(nameregex, current)
 {
	re = new RegExp(nameregex);  
    for(i = 0; i < document.forms[0].elements.length; i++) 
	{

		elm = document.forms[0].elements[i]




		if (elm.type == 'radio') 
		{
			if (re.test(elm.name)) 
			{
				if (elm.checked)

                    {
                    //alert("selected...");

                    }
			}
		}
	}
			
	current.checked = true;
}
