function checkSearch(userControlName) { 
	//check that user has checked recieve updates
	var msgResult =true;
	//alert($F(userControlName + '_cboLocation'));
	if ($(userControlName + '_chkSendEmails').checked != true) {
		msgResult = confirm('Are you sure you don\'t wish to receive updates on new properties added to the Joyce website? Click cancel and select the "Recieve Updates" checkbox to enable updates.');
		if (msgResult != true){
			new Effect.Highlight('tdcheck', {duration: 5.0});
			}
		}
	else {
		//if so confirm that is what they wan't
		if (checkSettingsDefault(userControlName))
			msgResult = confirm('Are you sure you wish to recieve updates on ALL properties added to the website? If you wish to receive more relevant results by refining your search to a certain area and/or price range, click Cancel and amend your criteria. ');
		if (msgResult != true)
			highlightControls(userControlName);
	}
	return msgResult;
}

function checkSettingsDefault(userControlName){
	//check that user hasn't selected all for ddls
	var allSet = true;
	if ($F(userControlName + '_cboLocation') != 'All Areas')
		allSet = false;
		
	if ($F(userControlName + '_cboPostCode') != 'Any')
		allSet = false;
		
	if ($F(userControlName + '_cboPropertyType') != 'All')
		allSet = false;
		
	if ($F(userControlName + '_cboMinPrice') != 'Any')
		allSet = false;
		
	if ($F(userControlName + '_cboMaxPrice') != 'Any')
		allSet = false;
	
	if ($F(userControlName + '_cboNumBedrooms') != '0')
		allSet = false;
		
	//if ($F(userControlName + '_cboForSaleOnly') != 'Any')
	//	allSet = false;
	
	return allSet;
}

function highlightControls(userControlName){
	new Effect.Highlight(userControlName + '_cboLocation', {duration: 5.0});
	new Effect.Highlight(userControlName + '_cboPostCode', {duration: 5.0});
	new Effect.Highlight(userControlName + '_cboPropertyType', {duration: 5.0});
	new Effect.Highlight(userControlName + '_cboMinPrice', {duration: 5.0});
	new Effect.Highlight(userControlName + '_cboMaxPrice', {duration: 5.0});
	new Effect.Highlight(userControlName + '_cboNumBedrooms', {duration: 5.0});
	new Effect.Highlight(userControlName + '_cboForSaleOnly', {duration: 5.0});
}

function describeSearch(userControlName){
	//generate search description for page in real time
	var category ='';
	var bedrooms = '';
    var priceBand = '';
    var locationDesc = '';
    var keywordDesc = '';
    var houseType = '';
    var forSaleOnly ='';
    
    //set ddls based on category before generating search
    disableDDLsCategory(userControlName);
    
    if ($F(userControlName + '_cboPropertyType') == 'All')
		houseType = ' all ' +  getTextForCurrentValue(userControlName + '_cboCategory') + ' properties ';
    else 
        houseType = $F(userControlName + '_cboPropertyType') + ' ' + getTextForCurrentValue(userControlName + '_cboCategory') + ' properties ';
        
    category = 'Your search will return ' + houseType;
    
    if ($F(userControlName + '_cboNumBedrooms') != '0')
		bedrooms = 'with at least ' + $F(userControlName + '_cboNumBedrooms') + ' bedrooms ';
		
    if ($F(userControlName + '_cboMinPrice') != 'Any') {
		if ($F(userControlName + '_cboMaxPrice')  != 'Any') 		
			//between
			//alert(getTextForCurrentValue(userControlName + '_cboMinPrice'));
			priceBand = 'costing between ' + getTextForCurrentValue(userControlName + '_cboMinPrice') + ' and ' + getTextForCurrentValue(userControlName + '_cboMaxPrice')  + ' ';
		
		else
			//min only
			priceBand = 'costing more than ' + getTextForCurrentValue(userControlName + '_cboMinPrice') + ' ';
    }
    else {
		if ($F(userControlName + '_cboMaxPrice') != 'Any')
			//Max only
			priceBand = 'costing up to ' + getTextForCurrentValue(userControlName + '_cboMaxPrice') + ' ';
    }
    
    if ($F(userControlName + '_cboPostCode') != 'Any')
		locationDesc = 'in the ' + getTextForCurrentValue(userControlName + '_cboPostCode') + ' area '
    else	{
		if ($F(userControlName + '_cboLocation') != 'All Areas') 
			locationDesc = 'in the ' + getTextForCurrentValue(userControlName + '_cboLocation') + ' area '
	}

	//sale status
	if ($F(userControlName + '_cboForSaleOnly') != 'Any')
		//for sale only
		forSaleOnly = 'that are currently For Sale only';
	// str = str.replace(/^\s*|\s*$/g,"");
    $('searchDescription').innerHTML = (category + bedrooms + priceBand + locationDesc + forSaleOnly).replace(/^\s*|\s*$/g,"") + '.';
    
    new Effect.Highlight('searchDescription');
}

function addWarningMessage(userControlName){
	//add warning to desc if we are receiving emails and user has selected a wide search
    if($F(userControlName + '_chkSendEmails') == 'on'){
		if  (checkSettingsDefault(userControlName) && (getTextForCurrentValue(userControlName + '_cboCategory')=='Residential'))
			$('searchDescription').innerHTML += ' &nbsp<b>Warning</b> : If you do not narrow your search by Area, Property Type or Price you will be informed of most properties added to the Joyce website, which may result in a large number of emails.';
	}
}


function getTextForCurrentValue(ddlName){
	 var ddl = $(ddlName);
	 var ddlValue = $F(ddlName);
	 return getTextForValue(ddl, ddlValue);
}

function getTextForValue(ddList, searchValue) {
	//return text from ddl
	var nodeList = ddList.getElementsByTagName('option');
	var nodes = $A(nodeList);
	var returnVal = 'Not Found';
	nodes.each(function(node){
		if (node.value == searchValue)
			returnVal = node.innerHTML;
	});
	return returnVal;	
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function disableDDLsCategory(userControlName){
	//default and disable dropdowns based on category
	
	//rentals have no price or status
	if ($F(userControlName + '_cboCategory') == '3') {
		//sel value
		setIndexToValue($(userControlName + '_cboMinPrice') , 'Any');
		setIndexToValue($(userControlName + '_cboMaxPrice') , 'Any');
		setIndexToValue($(userControlName + '_cboForSaleOnly') , 'Any');
		//disable
		$(userControlName + '_cboMinPrice').disabled=true;
		$(userControlName + '_cboMaxPrice').disabled=true;		
		$(userControlName + '_cboForSaleOnly').disabled=true;
	}
	else{
		//enable
		$(userControlName + '_cboMinPrice').disabled = false;
		$(userControlName + '_cboMaxPrice').disabled = false;		
		$(userControlName + '_cboForSaleOnly').disabled = false;
	}
	
	
	//2,4,5,6 have no type, numbeds or forsale
	if ($F(userControlName + '_cboCategory') == '2' || $F(userControlName + '_cboCategory') == '4' || $F(userControlName + '_cboCategory') == '5' || $F(userControlName + '_cboCategory') == '6') {
		setIndexToValue($(userControlName + '_cboPropertyType'),'All');
		setIndexToValue($(userControlName + '_cboNumBedrooms'),'0');
		//disable
		$(userControlName + '_cboPropertyType').disabled = true;
		$(userControlName + '_cboNumBedrooms').disabled = true;		
	}	
	else{
		$(userControlName + '_cboPropertyType').disabled = false;
		$(userControlName + '_cboNumBedrooms').disabled = false;	
	}
	
}

function setIndexToValue(ddList, searchValue) {
	//set index of a ddl to searchValue
	var nodeList = ddList.getElementsByTagName('option');
	var nodes = $A(nodeList);
	var newIndex = -1;
	nodes.each(function(node){
		if (node.value == searchValue){
			//alert(searchValue + ' ' + node.value + ' ' + node.index);
			newIndex = node.index;
		}
	});
	//set index if found
	if (newIndex != -1){
		ddList.selectedIndex = newIndex;
	}
}