// Declaration and Initialization========================================================
var DueDate;
var cst_num_enhanced = 0;				// changed 4 to 0 by Andrew, 02-21-2006

// InitSelect ===========================================================================
function InitSelect (CityCode) {
	
	var i;
	var j;
	var k;
	var CountryCode;
	var idxCountry;
	var idxCity;
	
	prtCountrySelectForm ();
	
	if (CityCode != "") 
	{
		for (i=1; i<arrCity.length; i++)
		{
			if (arrCity[i] != null) 
			{
				for (j=1; j<arrCity[i].length; j++)
				{
					// added 2 lines by Andrew, 02-20-2006
					if (arrCity[i][j] == undefined)
						continue;

					if (arrCity[i][j].value == CityCode)
					{
						CountryCode = i;
						idxCity = j;
						
						for (k=1; k<arrCountry.length; k++)
						{
							if (arrCountry[k].value == CountryCode)
							{
								idxCountry = k;
								Redirect(CountryCode);
								// added 2 line, commented 2 line by Andrew, 02-21-2006
								select_input('country', CountryCode);
								select_input('city', CityCode);
								//document.TourFinder.country.options[idxCountry+cst_num_enhanced].selected = true;
								//document.TourFinder.city.options[idxCity].selected = true;
								return;
							}
						}
					}
				}
			}
		}
	}
	else
	{
		Redirect (0);
	}
}
// prtCountrySelectForm =================================================================
function prtCountrySelectForm () {

	var count;
	var index;
	
	index = 1+cst_num_enhanced;
	
	// commented 4 lines by Andrew, 02-21-2006
//	document.TourFinder.country.options[1] = new Option ('Canada','2');
//	document.TourFinder.country.options[2] = new Option ('Mexico','3');
//	document.TourFinder.country.options[3] = new Option ('United States','1');
//	document.TourFinder.country.options[4] = new Option ('----------------','');
	
	for (count=1; count<arrCountry.length; count++)
	{
		if (arrCountry[count] != null)
		{
			document.TourFinder.country.options[index] = arrCountry[count];
			index = index + 1;
		}
	}

	// added 1 line by Andrew, 02-21-2006
	sboxSort(document.forms['TourFinder'].elements['country']);
}
// chkTourFinder ======================================================================
function chkTourFinder () {
	
	var objForm = document.forms['TourFinder'];			// Form Object
	var objCountry = objForm.elements['country'];		// Country Select Object
	var objCity = objForm.elements['city'];				// City Select Object

	// Declaration of variable
	var retval = true;

	// Country
	if (objCountry.options[objCountry.selectedIndex].value == "") {
		alert("³ª¶ó¸¦ ¼±ÅÃÇÏ¿© ÁÖ½Ê½Ã¿À.");
		objCountry.focus();
		return false;
	}
		
	// City
	if (objCity.options[objCity.selectedIndex].value == "") {
		alert("µµ½Ã¸¦ ¼±ÅÃÇÏ¿© ÁÖ½Ê½Ã¿À.");
		objCity.focus();
		return false;
	}
	
	//Arrival Date
	tmpMonthYear = objForm.elements['ArrvMonth'].value; //ex) 12/2004
	arrMonthYear = tmpMonthYear.split("/");
	ArrvMonth = arrMonthYear[0];
	ArrvYear = arrMonthYear[1];
	ArrvDay = objForm.elements['ArrvDay'].value;

	// Declaration of variable
	var SelectedDate
	
	SelectedDate = new Date(ArrvYear,ArrvMonth-1,ArrvDay);
	
	if (SelectedDate < new Date())
	{
		alert("You have selected an invalid date!!");
		objForm.elements['ArrvMonth'].focus();
		return false;
	}
	
	return true;
}
// chkMainTourFinder ======================================================================
function chkMainTourFinder () {
	
	// Search city or Hostel Name
	if (document.MainTourFinder.city[15].checked == true) {
		if (document.MainTourFinder.searchStr.value == "") {
			alert("µµ½Ã¸í ¶Ç´Â È£½ºÅÚ¸íÀ» °Ë»ö¶õ¿¡ ÀÔ·ÂÇÏ¿© ÁÖ½Ê½Ã¿À.");
			document.MainTourFinder.searchStr.focus();
			return false;
		}
		else if (chkSearchString(document.MainTourFinder.searchStr.value) == false)
		{
			alert("¼¼±ÛÀÚ ÀÌ»óÀÇ µµ½Ã¸í ¶Ç´Â È£½ºÅÚ¸íÀ» °Ë»ö¶õ¿¡ ÀÔ·ÂÇÏ¿© ÁÖ½Ê½Ã¿À.");
			document.MainTourFinder.searchStr.focus();
			return false;
		}
	}
	//Arrival Date
	tmpMonthYear = document.MainTourFinder.ArrvMonth.value; //ex) 12/2004
	arrMonthYear = tmpMonthYear.split("/");
	ArrvMonth = arrMonthYear[0];
	ArrvYear = arrMonthYear[1];
	ArrvDay = document.MainTourFinder.ArrvDay.value;
	
	// Declaration of variable
	var SelectedDate
	
	SelectedDate = new Date(ArrvYear,ArrvMonth-1,ArrvDay);
	
	if (SelectedDate < new Date())
	{
		alert("You have selected an invalid date!!");
		document.MainTourFinder.ArrvMonth.focus();
		return false;
	}
	
	return true;
}

// chkSearchString ======================================================================
function chkSearchString (SearchStr) {
	
	if (SearchStr.length <=2) {
		return false;
	}
	
	return true;
}
// Redirect =============================================================================
function Redirect (country) {
	
	// Variable declaration
	var i;
	var j;
	var nOptionIDX = 0;					// Select Option Index
	
	for (i=document.TourFinder.city.options.length-1; i>=0; i--)
	{
		document.TourFinder.city.options[i] = null;
	}
	
	if (country != 0)
	{	
		if (arrCity[country] != null) 
		{
			for (j=0; j<arrCity[country].length; j++)
			{
				// commented 3 lines by Andrew, 02-20-2006
				//if (j == 0)
				//	document.TourFinder.city.options[0] = new Option ('choose a city','');
				//else
					// added 5 lines, commented 1 line by Andrew, 02-20-2006
					if (arrCity[country][j] != undefined)
					{
						document.TourFinder.city.options[nOptionIDX] = new Option (arrCity[country][j].text,arrCity[country][j].value);
						nOptionIDX = nOptionIDX + 1;
					}
					//document.TourFinder.city.options[j] = new Option (arrCity[country][j].text,arrCity[country][j].value);
			}
		}
		else
		{
			document.TourFinder.city.options[0] = new Option ('there is no city','');
		}
	}
	else
	{
		document.TourFinder.country.options[0].selected = true
		// Added 2 Lines, Commented 2 Lines by Andrew, 02-21-2006
		document.TourFinder.city.options[0] = new Option ('choose a country first','');
		document.TourFinder.city.options[0].selected = true;
		//document.TourFinder.city.options[0] = new Option ('','');
		//document.TourFinder.city.options[1] = new Option ('choose a country first','');
	}
}
// GotoCalendar =========================================================================
function GotoCalendar (FormName) {

	window.open ("/Kr/Calendar.asp?formname="+FormName+"&Position=1","Calendar","menubar=no,toolbar=no,scrollbars=no,resizable=no,width=250,height=150,left=0,top=0");
}
// isValidateDate ============================================================================
function isValidateDate(year,month,day)
{
	// Declaration of variable
	var SelectedDate
	
	SelectedDate = new Date(year,month-1,day);
	
	if (!(parseInt(SelectedDate.getFullYear()) == year && parseInt(SelectedDate.getMonth()+1) == month && parseInt(SelectedDate.getDate()) == day))
	{
		return false;
	}
	else if ((DueDate / (1000*60*60*24)  - SelectedDate / (1000*60*60*24) ) > 0)
	{
		return false;
	}
	
	return true;
}

// Added By Andrew, 02-21-2006
// Form(select, radio, checkbox) Initialize by value
function select_input(input_name,input_value) {
    for ( i = 0 ; i < document.forms.length; i ++ ) {
        for ( j = 0 ; j < document.forms[i].elements.length; j++) {
            with(document.forms[i].elements[j]) {
                if(name == input_name) {
                    if( value == input_value) { checked=true; }
                        else if(type!='radio' && type != 'checkbox') { value=input_value; }
                }
            }
        }
    }
}

// Added By Andrew, 02-21-2006
// Sorting Select Box
// boxIdObj : select box object, isValuesort : sort by value(default is false, sort by text)
function sboxSort(boxIdObj, isValuesort) 
{ 
    var obj, sArr, oArr, idx, op; 
    
    if (typeof boxIdObj == 'string') obj = document.getElementById(boxIdObj); 
    else obj = boxIdObj; 

    if (obj.tagName.toLowerCase() != 'select') return false; 
    if (typeof isValuesort == 'undefined') isValuesort = false; 

    sArr = new Array(obj.options.length); 
    oArr = new Array; 
	tempArr = new Array(obj.options.length); 

    for (idx = 1; idx < obj.options.length; idx++) 
    { 
        if (isValuesort) sArr[idx] = obj.options[idx].value; 
        else
		{
			sArr[idx] = obj.options[idx].text.toUpperCase(); 
		}
        oArr[sArr[idx]] = obj.options[idx]; 
    } 
    sArr.sort(); 

    //for (idx in sArr) obj.appendChild(oArr[sArr[idx]]); 
	var i;
    for (i = 0; i < sArr.length - 1; i++)
	{
//		document.write("idx : " + i + " - ");
//		document.write("sArr : " + sArr[i] + " <BR /> ");
		obj.appendChild(oArr[sArr[i]]); 
	}
}
