// The following javascript functions support the MultiSelectList Control
var Index       = new Object;

function InsertIntoTextAndRepopulateTowns(txtId,selectId)
{
	InsertIntoText (txtId,selectId);	
	RepopulateTowns(txtId);
}

function CompleteTextFromListAndRepopulateTowns(txtId,selectId)
{
	CompleteTextFromList (txtId,selectId);
	RepopulateTowns(txtId);
} 
function RepopulateTowns(regionTxtId)
{

	if (!document.getElementById) return false;
	var text   = document.getElementById("ctl00$PlaceHolderMain$SearchResults$AdvancedSearch$FindLocation$TextBoxControl");
	if (!text)
	{
		text   = document.getElementById("ctl00$PlaceHolderMain$NewSearch$FindLocation$TextBoxControl");
	}
	if (!text)
	{
		text   = document.getElementById("ctl00_PlaceHolderMain_SearchResults_AdvancedSearch_FindLocation_TextBoxControl");
	}
	if (!text)
	{
		text   = document.getElementById("ctl00_PlaceHolderMain_NewSearch_FindLocation_TextBoxControl");
	}
	var select = document.getElementById("ctl00$PlaceHolderMain$SearchResults$AdvancedSearch$FindLocation$ListBoxControl");
	if (!select)
	{
		select= document.getElementById("ctl00$PlaceHolderMain$NewSearch$FindLocation$ListBoxControl");
	}
	if (!select)
	{
		select= document.getElementById("ctl00_PlaceHolderMain_SearchResults_AdvancedSearch_FindLocation_ListBoxControl");
	}
	if (!select)
	{
		select= document.getElementById("ctl00_PlaceHolderMain_NewSearch_FindLocation_ListBoxControl");
	}
	var secretTownList = document.getElementById("ctl00$PlaceHolderMain$SearchResults$AdvancedSearch$SecretTownList");
	if (!secretTownList)
	{
		secretTownList = document.getElementById("ctl00_PlaceHolderMain_SearchResults_AdvancedSearch_SecretTownList");	
	}	
	if (!secretTownList)
	{
		secretTownList = document.getElementById("ctl00$PlaceHolderMain$NewSearch$SecretTownList");	
	}	
	if (!secretTownList)
	{
		secretTownList = document.getElementById("ctl00_PlaceHolderMain_NewSearch_SecretTownList");	
	}	

	var regionText = document.getElementById(regionTxtId);
	
	// clear the town textbox and town select
		text.value = '';
		select.options.length=0;
		var j = 0;

	var textRegions = regionText.value.split(/\s*,\s*/);
		if (!textRegions) return false;
		
//	if(textRegions.length > 0)
//	{    
		
		// Now Fill the town select
		
		select.options.length++;
		select.options[j].value = "ALL";
	    select.options[j].text = "ALL";
		j++;
		
		for (var k = 0; k < secretTownList.options.length; k++) 
            {
            	for (var i = 0; i < textRegions.length; i++) 
				 {
				 
				 	var thisRegion = textRegions[i].replace(' ','').replace(' ','').replace(' ','').replace(' ','')
					 switch(thisRegion)
					{
						case "ExperiencePerth":
						if (secretTownList.options[k].text == "Experience Perth")
		                     {
		                        select.options.length++;
		 	                    select.options[j].value = secretTownList.options[k].value;
		 	                    select.options[j].text = secretTownList.options[k].value;	                        
					            j++;
					         }
					    break;				         
					    case "Australia'sNorthWest":
   						if (secretTownList.options[k].text == "Australia's North West")
		                     {
		                        select.options.length++;
		 	                    select.options[j].value = secretTownList.options[k].value;
		 	                    select.options[j].text = secretTownList.options[k].value;	                        
					            j++;
					         }
						break;
						case "Australia'sSouthWest":
						if (secretTownList.options[k].text == "Australia's South West")
		                     {
		                        select.options.length++;
		 	                    select.options[j].value = secretTownList.options[k].value;
		 	                    select.options[j].text = secretTownList.options[k].value;	                        
					            j++;
					         }
						break;
						case "Australia'sGoldenOutback":
						if (secretTownList.options[k].text == "Australia's Golden Outback")
		                     {
		                        select.options.length++;
		 	                    select.options[j].value = secretTownList.options[k].value;
		 	                    select.options[j].text = secretTownList.options[k].value;	                        
					            j++;
					         }
						break;
						case "Australia'sCoralCoast":
						if (secretTownList.options[k].text == "Australia's Coral Coast")
		                     {
		                        select.options.length++;
		 	                    select.options[j].value = secretTownList.options[k].value;
		 	                    select.options[j].text = secretTownList.options[k].value;	                        
					            j++;
					         }
						break;
					}

            	 }
            }
		 
		 // For NO regions, display ALL towns
		 if(textRegions.length == 1 && textRegions[0] == '')
		 {		 
		 var n = 0
		 select.options.length++;
		 select.options[n].value = "ALL";
	     select.options[n].text = "ALL";
		 n++;

		 for (var m = 0; m < secretTownList.options.length; m++)  
		                 {
		                        select.options.length++;
		 	                    select.options[n].value = secretTownList.options[m].value;
		 	                    select.options[n].text = secretTownList.options[m].value;	                        
					            n++;
					      }	
		  }

	return true;
}

 function CompleteTextFromList (txtId,selectId) 
 {
	 if (!document.getElementById) return;
	 var text   = document.getElementById(txtId);
	 var select = document.getElementById(selectId);
	 if (!Index.populated) BuildIndex(selectId);
	 var suburb = text.value.match(/,*([^,]+)$/);

	 if (suburb) 
	 {
	 	var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
	 	for (var i = Index[name.charAt(0)]; i < select.options.length; i++) 
	 	{
			 if (select.options[i].text.toUpperCase().indexOf(name) == 0) 
			 {
				 select.selectedIndex = i;
				 break;
			 }
			 else 
			 {
			 	 select.selectedIndex = -1;
			 }
		 }
	 }
 }

 function BuildIndex (selectId) 
 {
	 if (!document.getElementById) return;
	 var select = document.getElementById(selectId);
	 for (var i = select.options.length; i--;) 
	 {
	 	Index[select.options[i].text.toUpperCase().charAt(0)] = i;
	 }
	 Index.populated = true;
 }
 
 /// ****************
 /// This is the function used by the SingleSelectList control. 
 /// ****************
 function InsertIntoTextSingle(txtBox, listBox) 
 {     
	 if (!document.getElementById) return false;
	 var text    = document.getElementById(txtBox);
	 var select  = document.getElementById(listBox); 
	 
	 // An IE bug means we need another event before select.selectedIndex
	 // will update from -1. Since we need to swap the focus anyway we'll
	 // do it before we look at what was clicked in the select box.
	 text.focus();	 
	 var suburb = select.options[select.selectedIndex];
	 
	 // short circuit on 'show all suburbs'
	 if (select.selectedIndex == 0) 
	 {
		 text.value = '';
		 return true;
	 }
	 
	 var newvalue =  suburb.text;
	 text.value = newvalue;
	 return false;
 }

function InsertIntoText (txtBox, listBox) 
 {
	 if (!document.getElementById) return false;
	 var text    = document.getElementById(txtBox);
	 var select  = document.getElementById(listBox);
	 // An IE bug means we need another event before select.selectedIndex
	 // will update from -1. Since we need to swap the focus anyway we'll
	 // do it before we look at what was clicked in the select box.
	 text.focus();
	 var suburb = select.options[select.selectedIndex];
	 
	 // short circuit on 'show all suburbs'
	 if (select.selectedIndex == 0) 
	 {
		 text.value = '';
		 return true;
	 }
	  
	 // see if the selected suburb is in the list already
	 var textSuburbs = text.value.split(/\s*,\s*/);
	 if (!textSuburbs) return false;
	 var pattern = new RegExp('\s*' + suburb.text + '\s*$');
	 for (var i = 0; i < textSuburbs.length; i++) 
	 {
		 // if it's in the list don't do anything
		 if (pattern.exec(textSuburbs[i])) return false;
	 }
	 // its not in the list so lets add it replacing any incomplete words
	 var newvalue = text.value.replace(
	 /(^|,)([^,]*)$/,
	 "$1 " + suburb.text + ', '
	 );
	 text.value = newvalue;
	 return false;
 }
 
/* used to turn page content on and off */
function toggleMulti(el,iconShow,iconHide,text){
 var state = document.getElementById(el).style.display;
 if(state=="" || state=="none"){
	 document.getElementById(el).style.display = "block";
	 document.getElementById(iconShow).style.display = "none";
 	 document.getElementById(iconHide).style.display = "inline";
	 if(document.getElementById(text))
	 {
	    document.getElementById(text).innerHTML = "Hide Details";
	 }
 }else{
	 document.getElementById(el).style.display = "none";
	 document.getElementById(iconShow).style.display = "inline";
 	 document.getElementById(iconHide).style.display = "none";
 	 if(document.getElementById(text))
	 {
	    document.getElementById(text).innerHTML = "Show More";
	 }
 }
}

function doSearch()
{
    var keyWords = document.getElementById("siteSearch");
    if( keyWords != null && keyWords.value != "" )
    {
       window.location = "/en/search/Pages/Search.aspx?k=" + encodeHtml(keyWords.value) ;                                                                              
    }                            
}

function checkKey(event)
{
    
    oEvent = event;        
    iKeyCode = oEvent.keyCode;
    
    //in PC enter's keycode =13; in Mac is 3
    if( iKeyCode == 13 || iKeyCode == 3 )
    {       
       document.getElementById("searchBtn").click();
       return false;
    }            
}

//set title suffixed 
// This is now handled in the TitleTag control under TeM.Utilities document.title = document.title + " - Tourism Western Australia";