//<![CDATA[

function listManager(theForm,theSelects,theArrays,theFields) 
	{

	this.selects = theSelects;
	this.oForm = theForm;
	this.oFields = theFields;
	this.draw = lists_draw;
	this.arrays = theArrays;
	this.lists = new Array();
	this.draw();//draw lists from scratch
	this.drawresort=lists_drawresort;
	this.drawaccom=lists_drawaccom;
	this.aircountry=lists_drawaircoun;
	this.airresort=lists_drawairreso;
	this.drawMaxSleeps=lists_drawMaxSleeps;


    }
function lists_draw() 
	{

  objcountry = document.getElementById('countryList1')
  objresort = document.getElementById('resortList1')
  objaccomm = document.getElementById('accommodationList1')
  objairport = document.getElementById('originAirport1')				

		n = new selectList(this.selects[0],this,0);
		n.draw();//this function will  generate the country list for the country combo box.
		this.lists[this.lists.length] = n;
		n = null;
		//the addBasicElements function is  used to add the  'select' combo item to each combobox.
		n = new selectList(this.selects[1],this,1);
		n.addBasicElements();
		n = new selectList(this.selects[2],this,2);
		n.addBasicElements();
		n = new selectList(this.selects[3],this,0);
		n.addBasicElements();
			if(this.selects.length==5)
			{
			n=new selectList(this.selects[4],this,0);
			n.addBasicElements();
			}
		var field0 = this.oFields[0];
		var field1 = this.oFields[1];
		var field2 = this.oFields[2];
			if (((field0 != "" )&&(field1 == "")&&(field2 == "")))
			{
				//This loop will select the country as  the default country 							
								
				for(i=0;i<objcountry.options.length;i++)
				{
					if (objcountry.options[i].value == field0)
					{
					objcountry.options[i].selected = true;
					break;
					}
				}
			}
		//The following code will generate the resort array and will select the corresponding resort as a default one.
			if (((field0 != "" )&&(field1 != "")&&(field2 == "")))
 			{
				//This will generate the coutnry list 
					for(i=0;i<objcountry.options.length;i++)
				{
					if (objcountry.options[i].value == field0 )
					{
						objcountry.options[i].selected = true;
						//break;
					}
				}
				//Code which generates the resort list
				n = new selectList(this.selects[1],this,1);
				var theArrayToUse=new Array();		
				theArrayToUse = n.listManager.arrays[1];
				n.clearElements();
				n.addBasicElements();
				var inArray=new Array();
				for (j=0;j<theArrayToUse.length;j++) 
				{
					inArray = theArrayToUse[j].split(',');
					if(inArray[0]==this.oFields[0] )
					{
						inArray[2]=n.parseText(inArray[2]);
						g = new Option(inArray[2],inArray[1]);
						g.setAttribute('level',inArray[0]);
						n.list.options[n.list.options.length] = g;  
					}
						
				}
				this.lists[this.lists.length] = n;
				n = null;
				//The above code genrates the resort list and the following code selects one  of the resort as the default resort 	
				for(j=0;j<objresort.options.length;j++)
					{

							if (objresort.options[j].value == field1)
							{

							objresort.options[j].selected = true;
							break;
							}
					}	
					
		
			}//if close
			//this code is for the accommodation page. 
				if (((field0 != "" )&&(field1 != "")&&(field2 != "")))
				{
					for(i=0;i<objcountry.options.length;i++)
					{	
						if (objcountry.options[i].value == field0 )
						{
							objcountry.options[i].selected = true;
							break;	
						}
					}
					//This code will generate the resort list combo and select the resort as the default . 
					n = new selectList(this.selects[1],this,1);
					var theArrayToUse=new Array();		
					theArrayToUse = n.listManager.arrays[1];
					n.clearElements();
					n.addBasicElements();
					var inArray=new Array();
					for (j=0;j<theArrayToUse.length;j++) 
					{
					    inArray = theArrayToUse[j].split(',');
						if(inArray[0]== field0 )
						{
							inArray[2]=n.parseText(inArray[2]);
							g = new Option(inArray[2],inArray[1]);
							g.setAttribute('level',inArray[0]);
							n.list.options[n.list.options.length] = g;  
						}
						
					}
					this.lists[this.lists.length] = n;
					n = null;
					for(j=0;j<objresort.options.length;j++)
					{
							if (objresort.options[j].value == field1)
							{
								objresort.options[j].selected = true;
								break;
							}
					}	
					//Code to Generate the accommodation combo box and select the accommodation value as the default.
					//Code Which generates the accommodation combobox
					n = new selectList(this.selects[2],this,2);
					var theArrayToUse=new Array();		
					theArrayToUse = n.listManager.arrays[2];
					n.clearElements();
					n.addBasicElements();
					var inArray=new Array();
					for (j=0;j<theArrayToUse.length;j++) 
					{
						inArray = theArrayToUse[j].split(',');
						if(inArray[0]== field1 )
							{
									inArray[2]=n.parseText(inArray[2]);
									g = new Option(inArray[2],inArray[1]);
									g.setAttribute('level',inArray[0]);
									n.list.options[n.list.options.length] = g;  
							}
					}
					this.lists[this.lists.length] = n;
					n = null;


				    for(l=0;l<objaccomm.options.length;l++)
					{
						if (objaccomm.options[l].value == field2)
						{
							objaccomm.options[l].selected = true;
							break;
						}
					}
		

			
			}//if close

		
		}//function close

function lists_drawresort() //This function generates the resorts in resorts combo box.
		{
			objcountry = document.getElementById('countryList1') 
			for(i=0;i<objcountry.options.length;i++)
			{
				if(objcountry.options[i].selected== true)
				{
					var keyval=objcountry.options[i].value;
					break;
				}
			}
			n = new selectList(this.selects[1],this,1);
			var theArrayToUse=new Array();		
			theArrayToUse = n.listManager.arrays[1];
			n.clearElements();
			n.addBasicElements(); 
			var inArray=new Array();
			for (j=0;j<theArrayToUse.length;j++) 
			{
			     inArray = theArrayToUse[j].split(',');
				if(inArray[0]==keyval )
				{
					inArray[2]=n.parseText(inArray[2]);
					g = new Option(inArray[2],inArray[1]);
					g.setAttribute('level',inArray[0]);
					n.list.options[n.list.options.length] = g;  
				}
			}

			this.lists[this.lists.length] = n;
			n = null;
		//To  clear the accommodation list combobox.
			n = new selectList(this.selects[2],this,2);
			n.clearElements();
			n.addBasicElements();
			//this.drawaccom();
		}

function lists_drawMaxSleeps()
	{
		objresort = document.getElementById('resortList1')
		for(i=0;i<objresort.options.length;i++)
		{
			if(objresort.options[i].selected== true)
				{
					var keyval=objresort.options[i].value;
					break;
				}
		}
		n = new selectList(this.selects[4],this,4);
		var theArrayToUse=new Array();		
		theArrayToUse = n.listManager.arrays[5];
		n.clearElements();
		//n.addBasicElements();
		n.addTextOption('Select...',0,'')
		var inArray=new Array();
		for (j=0;j<theArrayToUse.length;j++) 
		{
		    inArray = theArrayToUse[j].split(',');
			if(inArray[0]==keyval )
			{
				inArray[2]=n.parseText(inArray[2]);
				g = new Option(inArray[2],inArray[1]);
				g.setAttribute('level',inArray[0]);
				n.list.options[n.list.options.length] = g;  
			}
		}
		this.lists[this.lists.length] = n;
		n = null;
}
function lists_drawaccom() 
	{
		objresort = document.getElementById('resortList1')
		for(i=0;i<objresort.options.length;i++)
		{
		if(objresort.options[i].selected== true)
			{
					var keyval=objresort.options[i].value;
					break;
			}
		}
		n = new selectList(this.selects[2],this,2);
		var theArrayToUse=new Array();		
		theArrayToUse = n.listManager.arrays[2];
		n.clearElements();
		n.addBasicElements();
		var inArray=new Array();
		for (j=0;j<theArrayToUse.length;j++) 
		{
		    inArray = theArrayToUse[j].split(',');
			if(inArray[0]==keyval )
			{
				inArray[2]=n.parseText(inArray[2]);
				g = new Option(inArray[2],inArray[1]);
				g.setAttribute('level',inArray[0]);
				n.list.options[n.list.options.length] = g;  
			}
		}
		this.lists[this.lists.length] = n;
		n = null;
}
//This function will generates the airport combo based on the courty 
function lists_drawaircoun()
	{
	    objcountry = document.getElementById('countryList1')
	    for(i=0;i<objcountry.options.length;i++)
		{
			if(objcountry.options[i].selected== true)
			{
					var keyval=objcountry.options[i].value;
					break;
			}
		}
		p = new selectList(this.selects[3],this,3);
		var selectedAirport = p.list.value;
		p.clearElements();
		p.addTextOption('Select...',0,'');
		var theArrayToUse=new Array();		
		theArrayToUse = p.listManager.arrays[3];
		var inArray=new Array();
		for (j=0;j<theArrayToUse.length;j++) 
		{
		    inArray = theArrayToUse[j].split(',');
			if(inArray[0]==keyval )
			{
				for(xyz=0;xyz< (aList.length);xyz=xyz+2) 
					{
						if(aList[xyz] == inArray[1]) 
						{
							inArray[2] = aList[xyz+1];
							break;
						}
					}	
				g = new Option(inArray[2],inArray[1]);
				g.setAttribute('level',inArray[0]);
				p.list.options[p.list.options.length] = g;  
			}
		}
		p.setSelection(selectedAirport);
		this.lists[this.lists.length] = p;
		p = null;
}

//This function generates the airport combo box based on the resort selected.
function lists_drawairreso()
	{
		    objresort = document.getElementById('resortList1')
			for(i=0;i<objresort.options.length;i++)
			{
				if(objresort.options[i].selected== true)
					{
					var keyval=objresort.options[i].value;
					break;
					}
			}
			p = new selectList(this.selects[3],this,4);
			var selectedAirport = p.list.value;
			p.clearElements();
			p.addTextOption('Select...',0,'');
			var theArrayToUse=new Array();		
			theArrayToUse = p.listManager.arrays[4];
			var inArray=new Array();
			for (j=0;j<theArrayToUse.length;j++) 
			{
				inArray = theArrayToUse[j].split(',');
				if(inArray[0]==keyval )
				{
					for(xyz=0;xyz< (aList.length);xyz=xyz+2) 
					{
						if(aList[xyz] == inArray[1]) 
						{
							inArray[2] = aList[xyz+1];
							break;
						}
					}	
					g = new Option(inArray[2],inArray[1]);
					g.setAttribute('level',inArray[0]);
					p.list.options[p.list.options.length] = g;  
				}
			}
			p.setSelection(selectedAirport);
			this.lists[this.lists.length] = p;
			p = null;
	}



/*function lists_concatenate() {

	//look at last list, read backwards...

	valueField = document.forms[this.oForm].elements(this.oField);
	valueField.value = "";
	for (s=this.lists.length-1;s>=0;s--) {
		if (this.lists[s].selections.length) {
			for (t=0;t<this.lists[s].selections.length;t++) {
				//alert(this.lists[s].selections[t].outerHTML);
				valueField.value += this.lists[s].selections[t].value + '|';
			}
		break;
		}
	}
	valueField.value = valueField.value.substring(0,(valueField.value.length-1));
}*/


function selectList(theList,theListManager,theIndex) 
	{
		this.listManager = theListManager; //the group of lists to which it belongs
		//For the first time the list elements are null --elements of the countryList.
		this.list = document.forms[this.listManager.oForm].elements[theList]; //the list itself
		this.index = theIndex; //the position it occupies in the array of lists
		this.selections; // an array of selected elements;            
		this.draw = list_draw; //method to draw this list
		this.clearElements = list_clearElements; //clear elements in this list
		this.addOption = list_addOption;
		this.addTextOption = list_addTextOption;
		this.addBasicElements = list_addBasicElements;
		//this.checkSelected = list_checkSelected;
		//this.checkDependencies = list_checkDependencies;
		//this.reDraw = list_redraw;
		//this.restoreHighlighted = list_restoreHighlighted;
		this.setSelection = list_setSelection; //pre-selecting dropdown values 
		this.parseText=list_parseText;
	}

function list_parseText(txt)
	{
       
		var re = /&amp;/;
        var text1 =  txt.replace(re, "&");
		var re1 = / : /;
        text1 =  text1.replace(re1, ",");
		
		var regex = /&#([0-9]{2,4});/;
		var match;
		
		while (match=regex.exec(text1)) 
			{
			replacement = String.fromCharCode(match[1]);
			text1 = text1.replace(match[0], replacement);
			}

		return text1
	}


function list_setSelection(sSelection) 
	{
	//This function will return filtered combobox values .
    	for (z=0;z<this.list.options.length;z++) 
		{
    
                if (this.list.options[z].value == sSelection)
				{
                         this.list.options[z].selected=true;
                }
    
     	}
    
        this.selections = new Array();    
        for (x=0;x<this.list.options.length;x++)
		{
   
                if (this.list.options[x].selected == true)
				{
                       this.selections[this.selections.length] = this.list.options[x];
                }
    
        }
    
    }



function list_addBasicElements() 
	{
          switch (this.index) 
		  {
            case 0:
            	this.addTextOption('Select...',0,'');
            	break;
            case 1 :
				if(chkIsBusyHour())
				{
					this.addTextOption('Select...',0,'');
				}
				else
				{
					this.addTextOption('Any Resort',0,'');
				}
            	break;
            case 2 :
            	this.addTextOption('Any Accommodation',0,'');
            	break;
            }
	}

	function list_draw()
	{
		theArrayToUse = this.listManager.arrays[this.index];
		var skiAreaCount=1;
		n.clearElements();
		n.addBasicElements();  
		//condition added to not to display ski areas for the customer review functionality
		if((!(this.listManager.oForm=="customerReviewForm") && !(this.listManager.oForm=="showReviewsForm")))
		{
		this.addTextOption('--- Countries ---',1,'');
		for (j=0;j<theArrayToUse.length;j++) 
		{
			theArrayToUse1 = n.parseText(theArrayToUse[j]);
			if(!(theArrayToUse1.search('SkiArea')))
			{
				if(skiAreaCount==1)
				{
					this.addTextOption('--- Ski Areas ---',j+2,'');
					skiAreaCount++;
				}
			}
			this.addOption(theArrayToUse1);
		}
		}
		else
		{
				for (j=0;j<theArrayToUse.length;j++) 
		{
			theArrayToUse1 = n.parseText(theArrayToUse[j]);
			if(theArrayToUse1.search('SkiArea'))
			{
				this.addOption(theArrayToUse1);
			}
		
		}
		}
	}


function list_clearElements() 
	{
        this.list.options.length = 0;
	}


function list_addOption(inArray)
	{
            
			inArray = inArray.split(',');
			var inArrayText=inArray[1];
			countryName=inArray[2];
			g = new Option(countryName,inArray[1]);
            g.setAttribute('level',inArray[0]);
            this.list.options[this.list.options.length] = g;           
	}

function list_addTextOption(txt,pos,val) {
	var regex = /&#([0-9]{2,4});/;
	var match;
			
	if (this.list.name == 'originAirport' && pos > 0)
		{
		for(xyz=0;xyz< (aList.length);xyz=xyz+2) 
			{
			if(aList[xyz] == txt) 
				{
				txt = aList[xyz+1];
				break;
				}
			}	
		}	
	else 
		{
		while (match=regex.exec(txt)) 
			{
			
			replacement = String.fromCharCode(match[1]);
			txt = txt.replace(match[0], replacement);
			alert("txt after replacement is "+txt);
			}	
		}	
	this.list.options[pos] = new Option(txt,val);
}
function findResort(accomid)
		{
		var accomArray=new Array();
		for(i=0;i<accList.length;i++)
			{
			accomArray=accList[i].split(",");
			if(accomArray[1]==accomid)
			{
				return accomArray[0];
			}
			}
		}

	
		function findCountry(resortid)
		{
		var resortArray=new Array();
		for(i=0;i<rList.length;i++)
			{
			resortArray=rList[i].split(",");
			if(resortArray[1]==resortid)
			{
				return resortArray[0];
			}
			}
		}
		function isSkiArea(countryid)
		{
			var skiAreaArray = new Array();
			for(i=0;i<skiAreaList.length;i++)
				{
					skiAreaArray=skiAreaList[i].split(",");
					if(skiAreaArray[0]==countryid)
					{
					  return (true);
					}
				}
		}
		 function   openPopUp(url)
					{
					 var width           =   480;
					 var height          =   380;
					 var top              =   (screen.availHeight - height) / 2;
					 var left             =    (screen.availWidth  - width)  / 2;
 					var features = "directories=No,location=No,menubar=no,status=YES,titlebar=no,toolbar=no,scrollbars=yes,width="+width+",height="+height+", top="+top+", left="+left+",resizable=no";
 					var newWin = open(url,"newWin",features);
					} 
/*function listElement(theForm,theSelect,theArray) 
	{

//single list element object

	this.oForm = theForm;
	this.select = document.forms[theForm].elements[theSelect]
	this.oArray = theArray;	
	this.draw = listEl_draw;
	this.addTextOption = listEl_addTextOption;
	this.addOption = listEl_addOption;
	this.draw(); //draw list from scratch

	}


function listEl_addTextOption(txt,pos,val) {
    	this.select.options[pos] = new Option(txt,val);
}

function listEl_addOption(txt,val) {
	g = new Option(txt,val);
	this.select.options[this.select.options.length] = g;           
}

function listEl_draw() {
  theArrayToUse = this.oArray;
  this.select.options.length = 0;
  this.addTextOption('Select...',0,'');
	for (j=0;j<theArrayToUse.length;j=j+2) {
		this.addOption(theArrayToUse[j+1],theArrayToUse[j]);
	}
}*/

//]]>
