// JavaScript Document

//Removes all content from list box
function RemoveAll(optionListName)
{
	var optionList = eval('document.getElementById("'+optionListName + '");' );
	if (optionList == null) 
		return;
		
	if (optionList.removeChild) 
	{ // DOM to remove optgroups as well as options

		while(optionList.hasChildNodes()) 
		{
			optionList.removeChild(optionList.firstChild);
		}
	}

}



// arrays for holding urls for each list
var alevel1;
var alevel2;
var alevel3;
var alevel4;
var cookieName = 'hpQS';

function GetSelectOptions(regionID,regionTYPE,optionListName) 
{
	
	var objXMLHTTP = false;

	
	try {
		objXMLHTTP = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			objXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					objXMLHTTP = false;
				}
			}
	}
	
	

	if(objXMLHTTP) {
	
		// build a string

		var szRequest = "<RequestRegion regionID='";
		szRequest += regionID;
		szRequest += "' regionTYPE='";
		szRequest += regionTYPE;
		szRequest += "' targetLIST='";
		szRequest += optionListName;
		szRequest += "'></RequestRegion>";
		
		var szURL = "http://www.ownerdirect.com/search/quicksearch/regionInfo.asp";
		var szHttpMethod = "POST";
		
		objXMLHTTP.open(szHttpMethod, szURL, false);
		objXMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		objXMLHTTP.send(szRequest);
		
        if (objXMLHTTP.status == 200) 
		{
            // ...processing statements go here...
			DisplaySelectOptions(objXMLHTTP.responseText);
        } 
		else 
		{
            alert("There was a problem retrieving the data:\n" + objXMLHTTP.statusText);
        }
	} else {
		alert("Error initializing XMLHttpRequest!");	
	}
}


//Display info in 'RegionList' listbox for the given selection
function DisplaySelectOptions(szReply)
{

	var myObject = eval('(' + szReply + ')');

	// grab the object list name
	var optionListName;

	for (var i in myObject) {
		optionListName = i;
	}

	// clean out the top level the first time thru
	if (optionListName == 'level1')
		RemoveAll(optionListName);

	var optionList = eval('document.getElementById("' + optionListName + '")' );

	if (optionList == null)
		return;

	optionList.selectedIndex = -1;

	// erase the dropdown data
	for ( var x = 4; x > 1; x-- ) 
	{

		if (optionList.name == 'level'+ x ) {
			RemoveAll(optionList.name);
			break;
		} 
		else 
		{
			RemoveAll('level'+ x );
			addTopOption(eval('document.getElementById("level'+ x +'");'));
		}
	}

	addTopOption(optionList);	
	
	// build a place to hold the links
	eval('a' + optionListName + '= new Array();');
	
	// create a reference to our object
	var objList = eval('myObject.' + optionListName );

	if (optionList.name == 'level4' && objList.length>0 )
	{
		var build_rg = false;
		var build_resort = false;
		var optRGroup;
		var optResorts;
		
		for (var i = 0; i < objList.length; i++)
		{
			// we need to track changes in rg status
			if (objList[i].fld_type == 'G') 
			{
				// we have a resort group
				// do some checks
				//are we currently building a rg optionlist

				if (! build_rg)
				{
					// add option since ths should be first time thru
					optRGroup = document.createElement ( "OPTGROUP" );
					optRGroup.label = "Regions";
					build_rg = true;
				}

				var optionText = objList[i].fld_name + '  (' + objList[i].fld_count + ')';
					
				// create a new option
				var newOption = document.createElement( "OPTION" );
				newOption.innerHTML = optionText;
				newOption.value = objList[i].fld_id;

				// add the option to the optRGroup
				optRGroup.appendChild( newOption );

			}
			else
			{

				// build a resort option group
				if (! build_resort && build_rg )
				{
					// append the resort groups
					optionList.appendChild( optRGroup );
					
					// add option since ths should be first time thru
					optResorts = document.createElement ( "OPTGROUP" );
					optResorts.label = "Specific Destinations";
					build_resort = true;
				}

				var optionText = objList[i].fld_name + '  (' + objList[i].fld_count + ')';
				// create a new option
				var newOption = document.createElement ( "OPTION" );
				newOption.innerHTML = optionText;
				newOption.value = objList[i].fld_id;
				
				if (! build_rg )
				{
					optionList.appendChild( newOption );
				}
				else
				{
					// add the option to the optRGroup
					optResorts.appendChild( newOption );
				}
			
			}
				// add url to array for later
				eval('a' + optionList.name + '[' + i + ']=\'' + objList[i].fld_link + '\';');
		}
		
		if (build_resort)
		{
			optionList.appendChild( optResorts );
		}
	}
	else
	{
		// build the regular way
	
		for (var i = 0; i < objList.length; i++)
		{
			var optionText = objList[i].fld_name;

			if (objList[i].fld_count != '0' && objList[i].fld_count != 'X')
					optionText+= '  (' + objList[i].fld_count + ')';
			

			var newOption = document.createElement ( "OPTION" );
			newOption.innerHTML = optionText;
			newOption.value = objList[i].fld_id;
			
			optionList.appendChild( newOption );

			//if (objList[i].fld_count == '0' )
			//	optionList.options[i+1].className = 'highlightOption';

			// add url to array for later
			eval('a' + optionList.name + '[' + i + ']=\'' + objList[i].fld_link + '\';');
		}
		
	}

	
	// if we build a zone list then decide to display it
	if (optionListName == 'level3') 
	{
		var o = document.getElementById('zone');
		if ( objList.length>0 ) {
			try {
					o.style.display='table-row';
				} catch(e) {
					o.style.display = 'block';
				}
		} else {
			o.style.display = 'none';
		}
	}
	
}

function addTopOption(optionList) 
{
	
		var t;

		// make each list slightly different
		switch (optionList.name) 
		{ 
			case 'level4':
				t = 'Specific Destinations ';	
				break;
			case 'level3':
				t = 'Select Zone [Optional]';	
				break;
			case 'level2':
				t = 'Select Region ';	
				break;
			case 'level1':
				t = 'Select Country or Click on Map ';
				break;
				
		} 

		optionList.options[optionList.options.length] = new Option(t, '0');

}


function DisplayZoneResort (regionID,regionTYPE,targetLIST,parentID) 
{

	//re build the resort list for the parentID
	// T => Subregion
	// U => Zone

	if (regionID==0) 
	{
		// we changed the subregion so show a list of resorts for subregion
		GetSelectOptions(parentID,'T',targetLIST);
	} 
	else 
	{
		// we changed the zone so show list of resorts for zone
		GetSelectOptions(regionID,'U',targetLIST);
	}
	
}


function jumpSearchPage(f) 
{
	
	// we have to amke sure certain things are happening
	// we can go to intermediate forms as required

	// first do we have a resort selected
	if (f.level4.selectedIndex < 1) 
	{
		// no resort selected so see if zone
		if (f.level3.selectedIndex<1) 
		{
			// no zone so see if sr
			if (f.level2.selectedIndex <1) 
			{
				// sr so only 1 more
				if (f.level1.selectedIndex<1) 
				{
					alert('Please select a destination.');
					return false;
				} 
				else 
				{
					// we have only country selection
					// shoot to country list
					window.location = alevel1[f.level1.selectedIndex-1];
					return false;
				}
			} 
			else 
			{
				// we have only subregion selection
				// shoot to sr list
				window.location = alevel2[f.level2.selectedIndex-1];
				return false;
			}
		} 
		else 
		{
			// we have only zone selection
			// shoot to zn list
			window.location = alevel3[f.level3.selectedIndex-1];
			return false;
		}
	} 
	else 
	{
		// we have a resort selection
		// shoot to resort list
		window.location = alevel4[f.level4.selectedIndex-1];
		return false;
	}
	return false;
}

