// submission validation 
	fieldconverter=new Array();
	fieldconverter[0]='postcode';
	fieldconverter[1]='address';
	fieldconverter[2]='type of property';
	fieldconverter[3]='style of property';
	fieldconverter[4]='no of bedrooms';
	fieldconverter[5]='no of receptions';
	fieldconverter[6]='I am';
	
	window.addEvent("domready",function(){
        //onchange event for property type
        $('displayPropTypeId').addEvent('change', function()
        {
        	addPropertyStyleIds($('displayPropTypeId').get('value'));
        });
        
        //This needed so that when drop down is selected colour changes to black so behaviour is the same as non dynamically populated ones
        //Seems to be a problem with the styler sheet and moooselecta
        $('displayPropStyleId').addEvent('change', function()
        {
        	$('displayPropStyleId_trigger').set({
        			styles: {color: '#000000'}
        	});
        });
        
        //This needed so that when drop down is selected colour changes to black so behaviour is the same as non dynamically populated ones
        //Seems to be a problem with the styler sheet and moooselecta
        $('addressPointTOID').addEvent('change', function()
        {
        	$('addressPointTOID_trigger').set({
        			styles: {color: '#000000'}
        	});
        });
    });
	
	function valForm(){
		inputsChk=validateForm('postcode<s>,addressPointTOID<s>,displayPropTypeId<s>,displayPropStyleId<s>,bedrooms<s>,receptions<s>,purchaseReasonID<s>','propertyDetailsForm');
			
		if(inputsChk.length != 0){
			showAlert(inputsChk);
			return false;
		}
		return true;
	}
	
	
// Populate address
	function postcodeChange(postcodeEntered,selectedAddressPointTOID,clearAlert){
		document.propertyDetailsForm.selectedAddressPointTOID.value = selectedAddressPointTOID; 
		
		if(document.propertyDetailsForm.addressPointTOIDSelectedPostcode.value != document.propertyDetailsForm.postcode.value || document.propertyDetailsForm.addressPointTOID.options[0].text == 'Enter a postcode above'){
			if(clearAlert){
				showAlert("");
			}
			if(postcodeEntered.length != 0){
				document.propertyDetailsForm.addressPointTOID.options.length = 2;
				document.propertyDetailsForm.addressPointTOID.options[0].text = 'Loading...';
				
				
				callServer(myself  + "address.getAddresses&postcode=" + postcodeEntered, "displayData", false);
				
			}
			document.propertyDetailsForm.postcode.value=postcodeEntered;
			document.propertyDetailsForm.addressPointTOIDSelectedPostcode.value=postcodeEntered;
		}
		
		return true;
	}
	
	function displayData(returnedData){	
		//eval(returnedData);
		var addressTxt
		var selectPosition = 0;
		var errorMessage = "";
		
		count = returnedData.getRowCount();		
	
		if(count==0){
			document.propertyDetailsForm.addressPointTOID.options.length = 1;
			errorMessage = '<ul>';
			
			errorMessage = errorMessage + '<li class="alertList">The postcode you entered ' + "'" + document.propertyDetailsForm.postcode.value + "'" + " was either incomplete, incorrect, may have recently changed or is not covered by us at present. </li>";
			errorMessage = errorMessage + '<li class="alertList">Please re-enter your postcode.</li>';
			errorMessage = errorMessage + '</ul>';
			showAlert(errorMessage);
			
			document.propertyDetailsForm.addressPointTOID.options[0].text = 'Enter a postcode above';
			
		}
		else{
			document.propertyDetailsForm.addressPointTOID.options.length = count;
			clearAlert();
			
			for (x=0; x < count; x++)
			{
				selectPosition = x; 
				addressTxt="";
				document.propertyDetailsForm.addressPointTOID.options[selectPosition].value = returnedData["addresspointtoid"][x];
	
				if(returnedData["thoroughfare"][x].length > 47){
					addressTxt=returnedData["thoroughfare"][x].substring(0,47)+"...";
				}
				else{
					addressTxt=returnedData["thoroughfare"][x];
				}
				document.propertyDetailsForm.addressPointTOID.options[selectPosition].text = addressTxt;	
			}
		}
		
		selectAddressPointTOID(document.propertyDetailsForm.selectedAddressPointTOID.value);
		
		//reload the the select in mooselecta plugin
		window.selecta.replaceSelect(document.propertyDetailsForm.addressPointTOID);
		
		$('addressPointTOID_trigger').set({
			styles: {color: '#000000'}
		});
		
		return true;
	}
	
	function selectAddressPointTOID(selectedAddressPointTOID){	
		if(selectedAddressPointTOID != ""){
			with(document.propertyDetailsForm.addressPointTOID){
				for (x=0; x < options.length; x++){				
					if(options[x].value==selectedAddressPointTOID){
						options[x].selected=true;
						break;
					}
				}
			}	
		}
	}	
	
	function postcodeChangeOnload(){
		if(document.propertyDetailsForm.postcode.value != ""){
			postcodeChange(document.propertyDetailsForm.postcode.value);
		}
	}	
	
//	populate Property Style
	function addPropertyStyleIds(selectedproptypeid,setfocus){
		count=(selectedproptypeid !== "" & selectedproptypeid !== " ")? proptypeid[selectedproptypeid].length : 0 ;
		with(document.propertyDetailsForm.displayPropStyleId){
			options.length=(count+1);
			if(selectedproptypeid != ""){
				for (x=0; x < count; x++){
					values=proptypeid[selectedproptypeid][x].split("|");
					options[x+1].text=values[1];
					options[x+1].value=values[0];
					if(values[0] == setfocus){
						setfocus=x+1;
						break;
					}
				}
				selectedIndex=setfocus;
			}
		}				
		
		if(document.propertyDetailsForm.displayPropStyleId.length==1){
			document.propertyDetailsForm.displayPropStyleId.options[0].values="";
			document.propertyDetailsForm.displayPropStyleId.options[0].text="Select an option above";
		} else {
			document.propertyDetailsForm.displayPropStyleId.options[0].values="";
			document.propertyDetailsForm.displayPropStyleId.options[0].text="Select an option";
		}
		
		//reload the select in mooselecta plugin
		window.selecta.replaceSelect(document.propertyDetailsForm.displayPropStyleId);
	}
	
	
	function populatePropStyle(selectedDisplayPropStyleId){
		with(document.propertyDetailsForm.displayPropStyleId){
			options.length=1;
			options[0].values="";
			options[0].text="Select an option";
			addPropertyStyleIds(document.propertyDetailsForm.displayPropTypeId[document.propertyDetailsForm.displayPropTypeId.selectedIndex].value);
			selectPropStyle(selectedDisplayPropStyleId);
		}
	}	
	
	function selectPropStyle(selectedPropStyle){
		with(document.propertyDetailsForm.displayPropStyleId){
			for (x=0; x < options.length; x++){
				if(options[x].value==selectedPropStyle){
					options[x].selected=true;
					break;
				}
			}	
		}	
	}
	

	

	 
