

function openWindow(tourcode,el){

	var path = "";
	
	if(el.value == "N"){	
	path = "http://www.thaiair.com/AIP_ROH/SelectTourType?TourCode="+tourcode.value;
	}else{
	path = "http://www.thaiair.com/AIP_ROH/SelectTourType?TourCode="+tourcode.value+"&EL="+el.value;
	}         

    window.open(path,'_blank','top=100, left=100, height=768, width=1024, status=no, menubar=no, resizable=yes, scrollbars=yes, toolbar=no, location=no, directories=no');
	return true;
}



function RohContent(param){
	var xmlPath = gHost + "xml";
	
	this.createInterestList = createInterestList;
	this.createDestList = createDestList;
	this.addInterestChange = addInterestChange;
	
	var cSeperator = ',';
	
	/*--------------------------------------------------
		Create Interest List
	*/
	function createInterestList(){
		if(param['listObj'].indexOf('http') < 0){	
			var listFileName = xmlPath + param['listObj'].toLowerCase() + ".xml";
		}
		else {	
			var listFileName = param['listObj'].toLowerCase() + ".xml";
		}
		
		//find select value
		var selectInterestValue = "";
		var selectDestValue = "";
		var Loc = window.location;
		var myLoc = Loc.toString().split('#');
		if(myLoc.length > 1){
			var myParam = myLoc[1].split('_');
			selectInterestValue = myParam[0];
			if(myParam.length > 1){
				selectDestValue = myParam[1] + '|' + myParam[2];
			}
		}
		
		//empty object
		$('#' + param['objName']).empty();
				
		$.ajax({
                 type: "GET",
                 url: listFileName,
                 dataType: "xml",
                 success: function(xmlDoc) {
					 createInterest(xmlDoc,param['objName'],param['title'],param['lang'],selectInterestValue);
					 
					 if(selectDestValue != ''){
						 createDestList(selectInterestValue,selectDestValue);
					 }
                 },
				 error: function(){
				 	//alert('error on ' + param['objName']);
				 }
        }); //close $.ajax
		
	}
	
	function createInterest(xmlDoc,objName,title,lang,selectValue){
		var myOptions = []; var i=0;
		$(xmlDoc).find('item').each(function(){
			myOptions[i] = { optText:$(this).find('name').text(), optValue:$(this).find('code').text() };
			i++;
		});
		
		var mySort = new Sorting();
		var sortOptions = mySort.sortSelect(myOptions, true,lang);
		
		$('#' + objName).append('<option value="">' + title + '</option>');
		for(var i=0; i<sortOptions.length; i++){
			var str = '<option value="' + sortOptions[i].optValue + '"';
			if(selectValue == sortOptions[i].optValue){
				str += ' selected';
			}
			str += '>' + sortOptions[i].optText + '</option>';
			
			$('#' + objName).append(str);
		}
	}
	
	function addInterestChange(objName){
		//add event on change
		$('#' + objName).change(function(){
			$('#' + param['divObj']).empty();
			
			if(this.options[this.selectedIndex].value == '') {
				//disable link go to interest
				$('#' + param['linkGoInt']).hide();
				
				//display not found destination
				$('#' + param['objName2']).html(param['ErrMsg']);
			}
			else{
				myRoh.createDestList(this.options[this.selectedIndex].value,'');
			}
		});
		
	}
	
	/*--------------------------------------------------
		Create Destination List
	*/
	function createDestList(selectInterestValue,selectDestValue){
		if(param['itemObj2'].indexOf('http') < 0)	{	
			var listFileName = xmlPath + param['itemObj2'].toLowerCase() + selectInterestValue.toLowerCase() + ".xml";	
		}
		else{
			var listFileName = param['itemObj2'].toLowerCase() + selectInterestValue.toLowerCase() + ".xml";
		}
		
		//empty value
		$('#' + param['objName2']).empty();
		
		//load xml
		$.ajax({
                 type: "GET",
                 url: listFileName,
                 dataType: "xml",
                 success: function(xmlDoc) {					 
					 //init list
					 var newList = $('<select></select>').attr({id: param['listName2'], size: '1'});
					 $(newList).append('<option value="">' + param['title2'] + '</option>');
					 
					 var mapCountry = createGroupCountry(xmlDoc,param['lang']);
					 //put data to list
					 for(var i=0; i<mapCountry.length; i++){
						 //fill country
						 var str = '<option value="' + mapCountry[i].optValue + '"';
						 if(selectDestValue == mapCountry[i].optValue){
							 str += ' selected';
						 }
						 str += '>' + mapCountry[i].optText + '</option>';
						newList.append(str);
						
						for(var j=0; j<mapCountry[i].cities.length; j++){
							var str = '<option value="' + mapCountry[i].cities[j].optValue + '"';
							if(selectDestValue == mapCountry[i].cities[j].optValue){
							 str += ' selected';
						 	}
							str += '>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ' + mapCountry[i].cities[j].optText + '</option>';
							newList.append(str);
						}
					 }
					 
					 if(newList.find('option').length > 1){						 
						$('#' + param['objName2']).append($('<h3></h3>').attr({className: 'red'}).text('Select destination'));
					 	$('#' + param['objName2']).append(newList);
						
						//enable link go to interest
					 	$('#' + param['linkGoInt']).show();
					 }
					 else{
						//disable link go to interest
					 	$('#' + param['linkGoInt']).hide();
						
						//display not found destination
					 	$('#' + param['objName2']).html(param['ErrMsg']);
					 }
					
				 
                 },
				 error: function(){
					 //disable link go to interest
					 $('#' + param['linkGoInt']).hide();
					 
					 //display not found destination
					 $('#' + param['objName2']).html(param['ErrMsg']);
				 }
        }); //close $.ajax
	}
	
	function createGroupCountry(xmlDoc,lang){
		var myCountry = []; var i=0;
		
		$(xmlDoc).find("item").each(function(){
			var country = trim($(this).find('country').text());
			var city = trim($(this).find('city').text());
			
			//if none country
			if(country == '') return;
			
			var no = getDupCountry(myCountry,country);
			var pos = no;
			//create new country
			if(no == -1){
					myCountry[i] = { 
										optText:country,
										optValue: 'country|' + country.toLowerCase(),
										cities: [] };
					pos = i;
					i++;
			}
			
			//put city in country
			var cityArr = createGroupCity(myCountry[pos].cities,city);
			var k = myCountry[pos].cities.length;
			for(var j=0; j<cityArr.length; j++){
				myCountry[pos].cities[k] = cityArr[j];
				k++;
			}
		});
		
		//sort country
		var mySort = new Sorting();
		var sortCountry = mySort.sortSelect(myCountry,true,lang);
		//map cities to sort country
		var mapCountry = [];
		for(var k=0; k<sortCountry.length; k++){
			var mapCities = mapCountryCities(myCountry, sortCountry[k].optText);
						
			mapCountry[k] = {	optText: sortCountry[k].optText,
								optValue: sortCountry[k].optValue,
								cities :mySort.sortSelect(mapCities, true,lang)
							}
		}
		
		return mapCountry;
	}
	
	function mapCountryCities(countryList, country){		
		for(var i=0; i<countryList.length; i++){
			if(country == countryList[i].optText){
				return countryList[i].cities;
			}
		}
		
		return [];
	}
	
	function createGroupCity(myCities,txtCity){
		var cityArr = [];
		var cities = txtCity.split(cSeperator);
		var i=0;
			
		for(var j=0; j<cities.length; j++){
			var cityText = trim(cities[j]);
			var cityValue = 'city|' + cityText.toLowerCase();
			
			if(!isDupCity(myCities,cityText)){
				cityArr[i] = { optText:cityText,
							optValue: cityValue };
				i++;
			}
		}
		
		return cityArr;
	}
	
	function getDupCountry(myCountry,value){
		for(var j=0; j<myCountry.length; j++){
			if(value == myCountry[j].optText){
				return j;
			}
		}
		return -1;
	}
	
	function isDupCity(myCities,value){
		if(value == '') return true;
		
		for(var j=0; j<myCities.length; j++){
			if(value == myCities[j].optText){
				return true;
			}
		}
		return false;
	}
	
	function trim(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
}
