// Global vars:
var lastChecked = null;

var first_load = true;
var checkboxHtml = "";
var selectedTab = "";

var map_area = null; // polygon on the map
var map_area_dragger = null; // draggable marker to move the polygon

// Default map zooms for each distance:
var zooms = {'0.5': 14, '1.0': 13, '2.0': 12, '3.0': 12, '4.0': 11, '5.0': 11};

// Checkboxlist object:
// var myCheckboxList = new checkboxList("myCheckboxList", "districts_field", "scrollingList");

//var regionName = gettext("Singapore");
var regionName = "Singapore";
var areaVisible = false;
if (REGION == 'MY') {
	//regionName = gettext("Malaysia");
	regionName = "Malaysia";
	areaVisible = true;
}

// Document ready:
$(document).ready(function() {
		
	$('div.searchsection').bgiframe();
	$('div.searchsection a.searchsection_close').click(function() {
		$(document).click(); return false;
	});
	$('#toggle_advanced').click(function() {
		if ($('#advanced_options').is(':visible')) {
			$('#toggle_advanced').removeClass('toggle_advanced_closed').addClass('toggle_advanced_open');
			$('#advanced_options').hide();
		}
		else {
			$('#toggle_advanced').removeClass('toggle_advanced_open').addClass('toggle_advanced_closed');
			$('#advanced_options').show();
		}
		return false;
	});
	
	$('#searchbox div.searchsection').click(function(event) { 
		event.cancelBubble = true; // MSIE
		event.stopPropagation();
	});
	$('#searchbox li.searchrow_expandable span.selections').click(function() {
		var searchrow = $(this).parents('li.searchrow');
		$('#searchbox li.searchrow_expandable').not(searchrow).removeClass('searchrow_expanded').removeClass('subrow_expanded');
		searchrow.toggleClass('searchrow_expanded');
		$(document).one('click', function() {
			searchrow.removeClass('searchrow_expanded');
		});
		if (searchrow.attr('id') == 'location_row' && searchrow.hasClass('searchrow_expanded')) {
			if ($('#map_search').is(':visible')) {
				if (typeof(map) == "undefined") init_map();
				else map.checkResize();
			}
			else {
				// this fix is required by IE to avoid "phantom" checkboxes
				$('#district_search').hide().show();
			}
		}
		return false;
	});
	$('#searchbox li.searchrow input.formcheck').click(function() {
		var parentrow = $(this).parent('label').parent('li');
		var formcheckboxes = parentrow.find('input.formcheckbox');
		if (formcheckboxes.length && formcheckboxes.filter(':checked').length <= 0) {
			formcheckboxes.attr('checked', $(this).attr('checked'));
		}
		if ($(this).attr('type') == 'radio') {
			parentrow.siblings().find('div.subsearchsection')
								.stop(true, true)
								.slideUp('fast')
								.find('input.formcheckbox')
								.attr('checked', '');
		}
		updateSelections($(this).parents('li.searchrow'));
		if (formcheckboxes.length > 1) {
			parentrow.find('div.subsearchsection').stop(true, true).slideToggle('fast');
		}
	});
	$('#searchbox li.searchrow input.formcheckbox').click(function() {
		updateSelections($(this).parents('li.searchrow'));
	});
	$('#distance').change(function() {
		if ($('#mrt').val() || $('#school').val() || $('#longitude').val()) {
			// Redraw square:
			if (map_area) removeArea(map_area);
			map_area = drawArea($('#latitude').val(), $('#longitude').val(), $('#distance').val());
			
			// Adjust map zoom and center:
			map.setCenter(new GLatLng($('#latitude').val(), $('#longitude').val()), zooms[$('#distance').val()]);
			
			updateAddress();
		}
	});
	$('#address_submit').click(function() {
		address_change($('#address').val());
		return false;
	});
	$('#property_submit').click(function() {
		$('#property_name').trigger('keydown').trigger('focus').trigger('click');
		return false;
	});

	// Tab change detection:
	$('#location_search a').click(function() {
		if ($(this).hasClass('selected')) return false;
		else {
			$(this).addClass('selected');
			$(this).parent('li').siblings('li').children('a').removeClass('selected');
		}
		
		$('#location_section div.search_tab').hide();
		$('#' + $(this).attr('name')).show();
		if ($(this).attr('name') == "district_search") {
		
		}
		else if ($(this).attr('name') == 'map_search') {
			
			// Reinitialize map (avoids render problems):
			if (typeof(map) == "undefined") init_map();
			else map.checkResize();
			
			$('#school_row, #mrt_row, #address_row, #property_row, #distance_row').hide();
			if ($(this).attr('id') == 'school_tab') {
				$('#school_row, #distance_row').css('display', 'inline');
			}
			else if ($(this).attr('id') == 'mrt_tab') {
				$('#mrt_row, #distance_row').css('display', 'inline');
			}
			else if ($(this).attr('id') == 'address_tab') {
				$('#address_row, #distance_row').css('display', 'inline');
			}
			else if ($(this).attr('id') == 'property_tab') {
				$('#property_row').css('display', 'inline');
			}
		}
		//updateAddress();
		return false;
	});
	
	$('#region_code').change(function() {
		if ($(this).val() && $(this).val() != 'MY99') $('#location_row').show();
		else $('#location_row').hide();
		$('#district_list ul.district_list').hide().find('input.formcheck').attr('checked', '');
		$('#location_row select.district_list').hide().find('option:first').attr('selected', 'selected');
		$('#region_' + $(this).val()).show();
		resizeLocation();
		updateAddress();
		if (typeof coords != 'undefined') {
			if ($(this).val() && $(this).val() != 'MY99') {
				map.setCenter(new GLatLng(coords[$(this).val()]['lat'], coords[$(this).val()]['lng']), parseInt(coords[$(this).val()]['zoom']));
			}
		}
	});
	
	initLocation();
	updateAddress();
	updateSelections($('#property_type_row'));
	updateSelections($('#hdb_type_row'));
	updateSelections($('#tenure_row'));
	updateSelections($('#furnishing_row'));
	updateSelections($('#lease_row'));
	
	if ($("input[name='listing_type']:checked").val() == 'sale') {
		$('#minbed option[value="-1"]').remove();
		$('#maxbed option[value="-1"]').remove();
	}
	
	if ($('#map_search').is(':visible')) {
		if (typeof(map) == "undefined") init_map();
		else map.checkResize();
	}
	
	$('#mrt, #school').change(function() {
		interest_change($(this).attr('id'), $(this).val());
	});
	
	$('select[name=sort]').change(function() {
		$('select[name=order]').val($(this).find('option:selected').attr('name') || '');
	});
	
	$('#minprice, #minbed, #minsize, #minsize_land, #mintop').change(function() {
		var min = $(this).find('option:selected').index();
		if (min == 0) return true;
		var max = $(this).siblings('select').find('option:selected').index();
		if (max > 0 && max < min) {
			$(this).siblings('select').val($(this).val());
		}
		return true;
	});
	
	$('#maxprice, #maxbed, #maxsize, #maxsize_land, #maxtop').change(function() {
		var max = $(this).find('option:selected').index();
		if (max == 0) return true;
		var min = $(this).siblings('select').find('option:selected').index();
		if (min > max) {
			$(this).siblings('select').val($(this).val());
		}
		return true;
	});
	
	$('#location_reset').click(function() {
		$('#longitude').val('');
		$('#latitude').val('');
		$('#location_row').find('input.formcheckbox, input.formcheck').attr('checked', '');
		$('#location_row div.subsearchsection').hide();
		$('#mrt').val(''); 
		$('#school').val('');
		$('#interest').val('');
		$('#address').val('');
		$('#property_name, #property_id').val('');
		$('#location_row span.selections span').text("All locations in " + regionName);
		$('#district_tab').click();
		$(document).click();
		return false;
	});
	$('#location_close').click(function() {
		$(document).click();
		return false;
	});
		
	$('#freetext').keypress(function(e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$('#searchsubmit').click();
			return false;
		}
		return true;
	});
	
	$('input.validate_numeric').keyup(function() {
		$(this).val($(this).val().replace(/[^0-9.]/g, ''));
	});
	
	// Districts map button:
	$("#district_btn").click(function() {
		loadDialog(620, 520, regionName + ' District Map',
			'/controller.php?pageid=ps_dialog_districtmap');
	});
	
	// Listing type change:
	$("input[name='listing_type']").click(function() {
		var arr = null;
		
		if ($("input[name='listing_type']:checked").val() == 'rent') {
			$("#furnishing_row").show();
			$("#lease_row").show();
			
			$("#tenure_row").hide();
			$("#tenure").val('');
			$("input[name='tenure\[\]']").attr('checked', '');
			$("#tenure_row span.selections span").text("All tenure options");
			if (!$('#minbed option[value="-1"]').length) {
				$('#minbed option:first').after($('<option/>').attr('label', 'Room Rental').attr('value', '-1').text('Room Rental'));
				$('#maxbed option:first').after($('<option/>').attr('label', 'Room Rental').attr('value', '-1').text('Room Rental'));
			}
			$('#price_row').show();
			arr = minmax_rent;
		} else if ($("input[name='listing_type']:checked").val() == 'sale') {
			$("#furnishing_row").hide();
			$("#furnishing").val('');
			$("input[name='furnishing\[\]']").attr('checked', '');
			$("#furnishing_row span.selections span").text('All furnishing options');
			
			$("#lease_row").hide();
			$("#lease_term").val('');
			$("input[name='lease_term\[\]']").attr('checked', '');
			$("#lease_row span.selections span").text('All lease term options');
			
			if ($("input[name='property_type']:checked").val() != 'H')	$("#tenure_row").show();
			$('#price_row').show();
			$('#minbed option[value="-1"]').remove();
			$('#maxbed option[value="-1"]').remove();
			arr = minmax_sale;
		} else {
			$("#furnishing_row").hide();
			$("#furnishing").val('');
			$("input[name='furnishing\[\]']").attr('checked', '');
			$("#furnishing_row span.selections span").text('All furnishing options');
			
			$("#lease_row").hide();
			$("#lease_term").val('');
			$("input[name='lease_term\[\]']").attr('checked', '');
			$("#lease_row span.selections span").text('All lease term options');
			
			$("#tenure_row").hide();
			$("#tenure").val('');
			$("input[name='tenure\[\]']").attr('checked', '');
			$("#tenure_row span.selections span").text("All tenure options");
			
			$('#price_row').hide();
			$('#minprice option:first').attr('selected', 'selected');
			$('#maxprice option:first').attr('selected', 'selected');
		}

		html = '';
		for(x in arr) {
			html += '<option value=\"'+x+'\">'+arr[x]+'</option>';
		}
		$('#minprice').html('<option value=\"\" selected=\"selected\">Min</option>' + html);
		$('#maxprice').html('<option value=\"\" selected=\"selected\">Max</option>' + html);
		
		var selectFirst = function() {
			$("option:first", "select#minprice").attr("selected","selected");
			$("option:first", "select#maxprice").attr("selected","selected");
		};
		$.browser.msie ? setTimeout(selectFirst,1) : selectFirst();
		
		return true;
	});
	
	// Property type change:
	$("input[name='property_type']").change(function() {
		// HDB fields:
		if ($("input[name='property_type']:checked").val() == 'H') {
			
			$("#district_tab span").text("By HDB Estate");
			$("input[name='districts\[\]']").attr('checked', '');
			$("#district_list").hide();
			$("#hdb_estate_row").show();
			
			$('#tracker').val('');
			
			$("#hdb_type_row").show();
			$("input[name='tenure\[\]']").attr('checked', '');
			$("#tenure_row span.selections span").text("All tenure options");
			$("#tenure_row").hide();
		}
		else {
			$("#district_tab span").text("By District");
			//myCheckboxList.updateValue(true); // updates the districts tracker
			$("input[name='hdb_estate\[\]']").attr('checked', '');
			$("#hdb_estate_row").hide();
			$("#district_list").show();
			$("#hdb_estate").val('');
			
			$("#hdb_type_row").hide();
			$("#hdb_type_row span.selections span").text('All HDB types');
			$("input[name='hdb_type\[\]']").attr('checked', '');
			$('#hdb_type_group_all').attr('checked', 'checked');
			if ($("input[name='listing_type']:checked").val() == 'sale') $("#tenure_row").show();
		}
		
		// Landed fields:
		if ($("input[name='property_type']:checked").val()=='L' ||
			$("input[name='property_type']:checked").val()=='B' ||
			$("input[name='property_type']:checked").val()=='S' ||
			$("input[name='property_type']:checked").val()=='T' ||
			$("input[name='property_type']:checked").val()=='D') {
			$("#size_land_row").show();
		}
		else {
			$("#size_land_row").hide();
			$("#minsize_land").val('');
			$("#maxsize_land").val('');
		}
		updateAddress();
	});
	
	// Location manually changed (dragging):
	$('#latitude').change(function() {
		$('#location_row').find('input.formcheckbox, input.formcheck').attr('checked', '');
		$('#mrt').val(''); 
		$('#school').val('');
		$('#interest').val('');
		$('#address').val('');
		$('#property_name, #property_id').val('');
		$('#location_row span.selections span').text('Address');
		if (!$('#address_row').is(':visible')) {
			$('#address_tab').click();
		}
		// Redraw square:
		if (map_area) removeArea(map_area);
		map_area = drawArea($('#latitude').val(), $('#longitude').val(), $('#distance').val());
	});
	
	// Submit button click:
	$('#searchsubmit').click(function() {
		// if advanced options is closed, remove all values
		if (!$('#advanced_options').is(':visible')) {
			$('#minpsf').val(''); 
			$('#maxpsf').val(''); 
			$("input[name='lease_term\[\]']").attr('checked', '');
			$('#listing_posted').find('option:first').attr('selected', 'selected');
			$('#cobroke').find('option:first').attr('selected', 'selected');
			$('#mintop').find('option:first').attr('selected', 'selected');
			$('#maxtop').find('option:first').attr('selected', 'selected');
			$('#with_photos').attr('checked', '');
			$('#with_stream').attr('checked', '');
			$('#event_id').attr('checked', '');
			$('#floorplan_id').attr('checked', '');
		}
		// if all multiselection options checked, remove them
		checkFullSelection($('#hdb_estate_row'));
		checkFullSelection($('#district_list'));
		checkFullSelection($('#tenure_row'));
		checkFullSelection($('#furnishing_row'));
		checkFullSelection($('#lease_row'));
		
		// determine search_type
		checkSearchType();
		
		$('#searchform').submit();
	});
	$('#district_list input.formcheck, #hdb_estate_row input.formcheck').click(function(event) {
		event.cancelBubble = true; // MSIE
		event.stopPropagation();
	});
	$('#district_list label, #hdb_estate_row label').click(function(event) {
		if (event.shiftKey) {
			var labels = $(this).parents('#district_list, #hdb_estate_row').find('label');
			if (!lastChecked) lastChecked = labels.filter(':first');
			var index1 = labels.index(lastChecked);
			var index2 = labels.index($(this));
			if (index1 == index2) return true;
			var checked = $(this).find('input.formcheck').attr('checked') ? '' : 'checked';
			labels.slice(Math.min(index1, index2), Math.max(index1, index2) + 1).each(function() {
				$(this).find('input.formcheck').attr('checked', checked);
			});
			lastChecked = $(this);
			updateAddress();
			return false;
		}
		lastChecked = $(this);
		return true;
	});
	
});

// Interest change:
function interest_change(type, key) {
	// Check if there's any value selected:
	if (!key) return;
	
	// Set default latitude and longitude:
	$('#latitude').val(interests[key][0]);
	$('#longitude').val(interests[key][1]);
	
	// Save in the hidden field:
	$('#interest').val(key);
	
	// Remove the other location values:
	$('#location_row').find('input.formcheckbox, input.formcheck').attr('checked', '');
	if (type == 'mrt') $('#school').val('');
	else if (type == 'school') $('#mrt').val('');
	$('#property_name, #property_id').val('');
	
	// Draw the square:
	if (map_area) removeArea(map_area);
	map_area = drawArea($('#latitude').val(), $('#longitude').val(), $('#distance').val());
	
	// Draw the dragger:
	if (map_area_dragger) map.removeOverlay(map_area_dragger);
	map_area_dragger = addAreaDragger();
	
	// Adjust map zoom and center:
	map.setCenter(new GLatLng($('#latitude').val(), $('#longitude').val()), zooms[ $('#distance').val()]);
	updateAddress();
}

//Locate address:
function address_change(query) {
	if (query != '') {
		geocoder.getLocations(query + ", " + regionName, function(response) {
			if (!response || response.Status.code != G_GEO_SUCCESS) {
				alert('Location not found');
				return false;
			}
			var place = response.Placemark[0];
			var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			// Remove other locations
			$('#location_row').find('input.formcheckbox, input.formcheck').attr('checked', '');
			$('#school').val('');
			$('#mrt').val('');
			$('#interest').val('');
			$('#property_name, #property_id').val('');
			
			$('#latitude').val(place.Point.coordinates[1]);
			$('#longitude').val(place.Point.coordinates[0]);
			
			update_map();
		});
	}
}

function updateSelections(src) {
	var selected = '';
	var inputs = src.find('input.formcheck');
	if (!inputs.filter(':checked').length || inputs.length == inputs.filter(':checked').length) {
		selected = formatSelections(src);
		var subinputs = src.find('input.formcheckbox');
		if (subinputs.length && subinputs.filter(':checked').length && (subinputs.filter(':checked').length < subinputs.length)) {
			selected = selected + ' +';
		}
	}
	else {
		inputs.filter(':checked').each(function() {
			var subselected = '';
			subselected = formatSelections(this);
			var subinputs = $(this).parent().parent().find('input.formcheckbox');
			if (subinputs.length && subinputs.filter(':checked').length && (subinputs.filter(':checked').length < subinputs.length)) {
				subselected = subselected + ' +';
			}
			selected = selected ? selected + ', ' + subselected : subselected;
		});
	}
	
	if (selected.length > 32) selected = selected.substr(0, 28) + '...';
	src.find('span.selections span').text(selected);
	
	if (src.attr('id') == 'location_row') {
		// clear other locations
		$('#latitude').val('');
		$('#longitude').val('');
		$('#mrt').val('');
		$('#school').val('');
		$('#interest').val('');
		$('#distance').val('');
		$('#address').val('');
		$('#property_name, #property_id').val('');
	}
}
function updateAddress() {
	var selected = '';
	
	if ($('#location_row input.formcheck:checked').length) return updateSelections($('#location_row'));
	else if ($('#mrt').val()) selected = $('#distance option:selected').text() + ' from ' + $('#mrt').find('option:selected').text();
	else if ($('#school').val()) selected = $('#distance option:selected').text() + ' from ' + $('#school').find('option:selected').text();
	else if ($('#property_id').val()) selected = $('#property_name').val() || 'Property';
	else if ($('#latitude').val()) selected = $('#address').val() || 'Address';
	else selected = (areaVisible ? "All areas in " + ($('#region_code').val() ? $('#region_code option:selected').text() : 'selected state') : "All locations in " + regionName);
	
	if (selected.length > 32) selected = selected.substr(0, 28) + '...';
	
	$('#location_row').find('span.selections span').text(selected);
}
function formatSelections(src) {
	var formatted = {
		'location_row': (areaVisible ? "All areas in " + ($('#region_code').val() ? $('#region_code option:selected').text() : 'selected state') : "All locations in " + regionName),
		'tenure_row': "All tenure options",
		'tenure_L30': "30 years",
		'tenure_L60': "60 years",
		'tenure_L99': "99/103 years",
		'tenure_L103': "103 years",
		'tenure_L999': "999 years",
		'tenure_L999|F': "999 years or Freehold",
		'furnishing_row': "All furnishing options",
		'furnishing_UNFUR': "None",
		'furnishing_PART': "Partial",
		'furnishing_FULL': "Full",
		'lease_row': "All lease term options",
		'lease_term_1YR': '1 year',
		'lease_term_2YR': '2 years',
		'lease_term_3YR': '3+ years',
		'lease_term_comm_1YR': '1 year',
		'lease_term_comm_2YR': '2 years',
		'lease_term_comm_3YR': '3 years',
		'lease_term_comm_4YR': '4 years',
		'lease_term_comm_5YRS': '5+ years'
	};
	var id = $(src).attr('id');
	if (formatted[id]) return formatted[id];
	else {
		if ($(src).hasClass('formselect')) return $(src).find('option:selected').text();
		else return $(src).parent().text();
	}
}

function initLocation() {
	checkFullSelection($('#region_' + $('#region_code').val()));
	if (REGION != 'SG') {
		resizeLocation();
	}
}

function resizeLocation() {
	var cols = $('#region_' + $('#region_code').val()).find('ul').length;
	if (cols > 1) {
		$('ul.district_list').css({'width': (220 * cols) + 'px', '_width': (220 * cols + 20) + 'px'});
		$('#location_section a.searchsection_close').show();
	}
	else {
		$('ul.district_list').css({'width': '165px', '_width': '153px'});
		$('#location_section a.searchsection_close').hide();
	}
}

function checkFullSelection(src) {
	var inputs = src.find('input.formcheck');
	if (inputs.length == inputs.filter(':checked').length) {
		inputs.attr('checked', '');
	}
}

function checkSearchType() {
	var tab_id = $('#location_search a.selected').attr('id');
	if (tab_id == 'district_tab') {
		if ($("input[name='property_type']:checked").val() == 'H') $('#search_type').val('hdb');
		else $('#search_type').val('district');
	}
	else if (tab_id == 'school_tab') $('#search_type').val('school');
	else if (tab_id == 'mrt_tab') $('#search_type').val('mrt');
	else if (tab_id == 'address_tab') $('#search_type').val('location');
	else if (tab_id == 'property_tab') $('#search_type').val('property');
}

// Redraw map according to #latitude and #longitude values
function update_map(hide_area) {
	if ($('#distance').length) {
		hide_area = hide_area || false;
		var distance = hide_area ? 0 : $('#distance').val();
		var zoom = hide_area ? zooms[0.5] : zooms[ $('#distance').val()];
		// Draw the square:
		if (map_area) removeArea(map_area);
		map_area = drawArea($('#latitude').val(), $('#longitude').val(), distance);
		
		// Draw the dragger:
		if (map_area_dragger) map.removeOverlay(map_area_dragger);
		map_area_dragger = addAreaDragger();
	}
	else var zoom = zooms[0.5];
	
	// Adjust map zoom and center:
	//alert($('#latitude').val());
	map.setCenter(new GLatLng($('#latitude').val(), $('#longitude').val()), zoom);
	updateAddress();
}

// Map initialization:
function init_map() {
	if(typeof delay_loading !== 'undefined')
	if(delay_loading)
	{
		var script = document.createElement("script");
		script.type = "text/javascript";     
		script.src = "http://maps.google.com/maps?file=api&v=2.225&key="+GMAP_KEY+"&sensor=false&async=2&callback=init_map";
		document.body.appendChild(script);
		delay_loading = false;
		return false;
	}
	init();
	if (!$('#latitude').val()) {
		var latitude = centerLatitude;
		var longitude = centerLongitude;
	}
	else {
		var latitude = $('#latitude').val();
		var longitude = $('#longitude').val();
	}
	map.setCenter(new GLatLng(latitude, longitude));
	
	var hide_area = false;
	if ($('#property_id').val()) hide_area = true;
	var distance = hide_area ? 0 : $('#distance').val();
	var zoom = hide_area ? zooms[0.5] : zooms[ $('#distance').val()];
	
	// Draw the square:
	if (map_area) removeArea(map_area);
	map_area = drawArea(latitude, longitude, distance);
	
	// Draw the dragger:
	if (map_area_dragger) map.removeOverlay(map_area_dragger);
	map_area_dragger = addAreaDragger(map_area);
	
	// If a location is already set, we adjust the zoom according to the selected distance:
	if (latitude != centerLatitude ||  longitude != centerLongitude) {
		map.setZoom(zoom);
	}
	geocoder = new GClientGeocoder();
	if (REGION) geocoder.setBaseCountryCode(REGION.toLowerCase());
	else geocoder.setBaseCountryCode("sg");
	return true;
}

function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null) {
		return "";
	}
	else {
		return results[1];
	}
}

function buildDistricts(checklist) {
	for(var id in checklist) {
		//alert(locations[id].code);
		if (checklist[id].type_code=='L') {
			myCheckboxList.add(checklist[id].code,checklist[id].desc, checklist[id].checked);
			//createCheckbox(checklist[id].code,checklist[id].desc, false);
		} else {
			myCheckboxList.add(checklist[id].code,checklist[id].desc, checklist[id].checked, checklist[id].parent);
			//createCheckbox(checklist[id].code,checklist[id].desc, false, checklist[id].parent);
		}
	}
	myCheckboxList.render();
	//var checkList = document.getElementById("scrollingList");
	//checkList.innerHTML=checkboxHtml;
	//checkList.appendChild(checkboxHtml);
	
}

function createCheckbox(code, desc, checked, parent) {
	if(!parent){
		checkboxHtml += '<span class="node nodeParent"><input type="checkbox" id="' + code + '">' + desc + '</span></span>';
	}else{
		checkboxHtml += '<span class="nodeChild" id="' + parent +'"><span class="node"><input type="checkbox" id="' + code + '" name="' + code +'" > <span>' + desc + '</span></span>';
	}
}
