
var psSuccess = false;
var lastResult = null;

function searchResultBox(result) {
	var html = "Search resulted into multiple matches. Please select one:<br/><br/><form name=\"searchresultform\"><ul id=\"searchlist\" class=\"checklist cl1\">";

	for (var x = 1; x <= result.length; x++) {
		var item = result[x - 1];

		if (x % 2 == 0) {
	        html = html + "<li class=\"alt\"><label for=\"sr" + x + "\"><input id=\"sr" + x + "\" " +
	        	"name=\"s_result\" type=\"radio\" value=\"" + item[0] + "\"/>  " + item[1] + "</label></li>";
		} else {
	        html = html + "<li><label for=\"sr" + x + "\"><input id=\"sr" + x + "\" " +
	        	"name=\"s_result\" type=\"radio\" value=\"" + item[0] + "\"/>  " + item[1] + "</label></li>";
		}
	}
	html = html + "</ul></form>";

	openDialog(400, 400, "SEARCH RESULT", html,
		new Array(getButton('btn_cancel', 'cancel', 'closeDialog();', 60),
				getButton('btn_select', 'select', 'selectResult();', 60)));
	
}

function selectResult() {
	var selectedId = getRadioValue('searchresultform', 's_result');
	
	if (selectedId != null) {
		for (var i = 0; i < lastResult.length; i++) {
			var item = lastResult[i];
			if (item.id == selectedId) {
				AJS.showElement(AJS.$('propertydetails'));
				setPropertyDetails(lastResult[i]);
				AJS.showElement(AJS.$('section2'));
				closeDialog();
				AJS.$f('qvform', 'floorsize').focus();
				break;
			}
		}
	}
}

function doPropertySearch() {	
	AJS.hideElement(AJS.$('propertydetails'));
	AJS.hideElement(AJS.$('section2'));

	busyDialog('SEARCH IN PROGRESS');

	var searchstring = AJS.$f('qvform', 'searchstring').value;
	
	var d = AJS.loadJSONDoc("/controller.php?pageid=pv_xmlhttp_propertysearch&searchstring="+searchstring)

	d.addCallback(
		function(o) {
			lastResult = o;
			if (!isArray(o) || o[0] == 'ERROR') { 
				errorDialog(o[1]);
			} else {				
				if (o.length == 0) {
					// empty
					closeDialog();
					openMessageBox('NO RESULTS FOUND', 'No results found with the search criteria.<br/>Please check it or try a different one.');
					setPropertyDetails(null);
					
				} else if (o.length == 1) {				
					// one hit
					AJS.showElement(AJS.$('propertydetails'));
					setPropertyDetails(o[0]);
					AJS.showElement(AJS.$('section2'));
					closeDialog();
					AJS.$f('qvform', 'floorsize').focus();

				} else {
					// multiple
					var result = new Array();
					for (var i = 0; i < o.length; i++) {
						result[i] = new Array(o[i].id, o[i].name);
					}
					closeDialog();					
					searchResultBox(result);
				}
			}
		});
	
	d.addErrback(
		function(o) { 
			errorDialog("System error - please try again");
		});
	
	d.sendReq();
}

function setPropertyDetails(property) {
	if (property == null) {
		// clear values
		AJS.$f('qvform', 'propertyid').value = '';
		AJS.$('pd_name').innerHTML = '';
		AJS.$f('qvform', 'p_name').value = '';
		AJS.$('pd_type').innerHTML = '';
		AJS.$f('qvform', 'p_type').value = '';
		AJS.$('pd_address').innerHTML = '';
		AJS.$f('qvform', 'p_address').value = '';
		AJS.$('pd_developer').innerHTML = '';
		AJS.$f('qvform', 'p_developer').value = '';
		AJS.$('pd_top').innerHTML = '';
		AJS.$f('qvform', 'p_top').value = '';
		AJS.$('pd_tenure').innerHTML = '';
		AJS.$f('qvform', 'p_tenure').value = '';

	} else {
		// set values
		AJS.$f('qvform', 'propertyid').value = property.id;
		AJS.$('pd_name').innerHTML = property.name;
		AJS.$f('qvform', 'searchstring').value = property.name;
		AJS.$f('qvform', 'p_name').value = property.name;
		AJS.$('pd_type').innerHTML = property_typenames[property.typeCode];
		AJS.$f('qvform', 'p_type').value = property_typenames[property.typeCode];
		AJS.$('pd_developer').innerHTML = property.developer;
		AJS.$f('qvform', 'p_developer').value = property.developer;
		AJS.$('pd_top').innerHTML = property.topYear;
		AJS.$f('qvform', 'p_top').value = property.topYear;
		AJS.$('pd_tenure').innerHTML = property.tenure;
		AJS.$f('qvform', 'p_tenure').value = property.tenure;
		
		var addr = '';
		if (property.streetnumber != '0') {
			addr = addr + property.streetnumber + ' '
		}
		addr = addr + property.streetname + ', Singapore ';
		if (property.postcode.substring(2, 6) != '0000') {
			addr = addr + property.postcode;
		}
		
		AJS.$('pd_address').innerHTML = addr;
		AJS.$f('qvform', 'p_address').value = addr;
	}
}

function catchEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13) {
          doPropertySearch();
          return false;
     }
     else
          return true;
}

$(document).ready(function() {
		if (AJS.$f('qvform', 'searchstring').value == '') {AJS.$f('qvform', 'searchstring').focus();}
		else {AJS.$f('qvform', 'floorsize').focus()}
});


