// Declaration and Initialization========================================================
var DueDate 
var cst_num_enhanced = 0;
// 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.HostelFinder.country.options[idxCountry+cst_num_enhanced].selected = true;
								//document.HostelFinder.city.options[idxCity].selected = true;
								return;
							}
						}
					}
				}
			}
		}
	}
	else
	{
		Redirect (0);
	}
}
// prtCountrySelectForm =================================================================
function prtCountrySelectForm () {

	var count;
	var index;
	
	index = 1+cst_num_enhanced;
	
	//document.HostelFinder.country.options[1] = new Option ('Canada','2');
	//document.HostelFinder.country.options[2] = new Option ('Mexico','3');
	//document.HostelFinder.country.options[3] = new Option ('United States','1');
	//document.HostelFinder.country.options[4] = new Option ('----------------','');
	for (count=1; count<arrCountry.length; count++)
	{
		if (arrCountry[count] != null)
		{
			document.HostelFinder.country.options[index] = arrCountry[count];
			index = index + 1;
		}
	}

	// added 1 line by Andrew, 02-21-2006
	sboxSort(document.forms['HostelFinder'].elements['country']);
}
// chkHostelFinder ======================================================================
function chkHostelFinder () {
	
	// Declaration of variable
	var retval = true;

	// Country
	if (document.HostelFinder.country.options[document.HostelFinder.country.selectedIndex].value == "") {
		alert("国を選択してください。");
		document.HostelFinder.country.focus();
		return false;
	}
		
	// City
	if (document.HostelFinder.city.options[document.HostelFinder.city.selectedIndex].value == "") {
		alert("都市を選択してください。");
		document.HostelFinder.city.focus();
		return false;
	}
	
	//Arrival Date
	tmpMonthYear = document.HostelFinder.ArrvMonth.value; //ex) 12/2004
	arrMonthYear = tmpMonthYear.split("/");
	ArrvMonth = arrMonthYear[0];
	ArrvYear = arrMonthYear[1];
	ArrvDay = document.HostelFinder.ArrvDay.value;

	if (isValidateDate(ArrvYear,ArrvMonth,ArrvDay) == false)
	{
		alert("You have selected an invalid date!!");
		document.HostelFinder.ArrvMonth.focus();
		return false;
	}
	
	return true;
}
// chkMainHostelFinder ======================================================================
function chkMainHostelFinder () {
	
	// Search city or Hostel Name
	if (document.MainHostelFinder.city[15].checked == true) {
		if (document.MainHostelFinder.searchStr.value == "") {
			alert("都市名又はホステル名を検索欄に入力してください。");
			document.MainHostelFinder.searchStr.focus();
			return false;
		}
		else if (chkSearchString(document.MainHostelFinder.searchStr.value) == false)
		{
			alert("都市名又はホステル名を3文字以上のロ??字で検索欄に入力してください。");
			document.MainHostelFinder.searchStr.focus();
			return false;
		}
	}
	//Arrival Date
	tmpMonthYear = document.MainHostelFinder.ArrvMonth.value; //ex) 12/2004
	arrMonthYear = tmpMonthYear.split("/");
	ArrvMonth = arrMonthYear[0];
	ArrvYear = arrMonthYear[1];
	ArrvDay = document.MainHostelFinder.ArrvDay.value;

	if (isValidateDate(ArrvYear,ArrvMonth,ArrvDay) == false)
	{
		alert("You have selected an invalid date!!");
		document.MainHostelFinder.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.HostelFinder.city.options.length-1; i>=0; i--)
	{
		document.HostelFinder.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.HostelFinder.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.HostelFinder.city.options[nOptionIDX] = new Option (arrCity[country][j].text,arrCity[country][j].value);
						nOptionIDX = nOptionIDX + 1;
					}
					//document.HostelFinder.city.options[j] = new Option (arrCity[country][j].text,arrCity[country][j].value);
				}
			}
		}
		else
		{
			// changed option's text(original : there are no city)
			// by Andrew, 02-20-2006
			document.HostelFinder.city.options[0] = new Option ('choose a country first','');
		}
	}
	else
	{
		document.HostelFinder.country.options[0].selected = true;
		// Added by Andrew : 2005-11-01, 2 Lines.
		document.HostelFinder.city.options[0] = new Option ('choose a country first','');
		document.HostelFinder.city.options[0].selected = true;
		/* Commented by Andrew : 2005-11-01
		document.HostelFinder.city.options[0] = new Option ('','');
		document.HostelFinder.city.options[1] = new Option ('choose a country first','');
		*/
	}
}
// GotoCalendar =========================================================================
function GotoCalendar (FormName) {

	window.open ("/Jp/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; 
    //number of option elements in select that should come first without being sorted.
    var numOfNonSortedCountries = 15;
    
    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 - numOfNonSortedCountries); 
    oArr = new Array; 
		//tempArr = new Array(obj.options.length); 

    for (idx = numOfNonSortedCountries; idx < obj.options.length; idx++) 
    { 
        if (isValuesort) 
        	sArr[idx - numOfNonSortedCountries] = obj.options[idx].value; 
        else
					sArr[idx - numOfNonSortedCountries] = obj.options[idx].text.toUpperCase(); 
					
        oArr[sArr[idx - numOfNonSortedCountries]] = obj.options[idx]; 
    } 
    sArr.sort(); 

    //for (idx in sArr) obj.appendChild(oArr[sArr[idx]]); 
		var i;
    for (i = 0; i < sArr.length; i++)
		{
			//document.write("idx : " + i + " - ");
			//document.write("sArr : " + sArr[i] + " <BR /> ");
			obj.appendChild(oArr[sArr[i]]); 
		}
}

//========================================================================================
//= Name: insert_selCurrency
//= Desc: Insert Currency List to select box
//= Parameter: objForm = form object
//=========================================================================================
function insert_selCurrency(objForm)
{ 
	var i;
	var nCountIdx;
	var objCurrencyList = objForm.elements["curr"];			// Select Box Name

	var nCountTotalCurrency;								// number of total Currency

	nCountTotalCurrency = arrCurrency.length - 1;
	
	nCountIdx = 0;
	for (i = 0; i < nCountTotalCurrency; i++) 
	{ 
			objCurrencyList.options[nCountIdx] = new Option(arrCurrency[i + 1].text,arrCurrency[i + 1].value) //arrCurrency[i + 1]; 
			nCountIdx = nCountIdx + 1;
	}

	// User Rank : Currency's Value List that should be top ranked.
	var arrCurrencyRank = new Array;
	arrCurrencyRank[0] = 'United States Dollars'
	arrCurrencyRank[1] = 'Euro';
	arrCurrencyRank[2] = 'Japan Yen';
	arrCurrencyRank[3] = 'South Korea Won';
	arrCurrencyRank[4] = 'China Yuan Renminbi';
	arrCurrencyRank[5] = 'United Kingdom Pounds';
	arrCurrencyRank[6] = 'Australia Dollars';
	arrCurrencyRank[7] = 'Canada Dollars';

	SelBoxSortByUserRank(objCurrencyList, arrCurrencyRank);
}
//========================================================================================
//= Name: SelBoxSortByUserRank
//= Desc: Sorting Select Box, By User Rank(value)
//= Parameter: boxIdObj = select box Object(select box), isValuesort = sort by value(default is false, sort by text)
//=========================================================================================

function SelBoxSortByUserRank(boxIdObj, arrUserRank) 
{ 
    var obj, sArr, oArr, idx, op;
	var i;
	var nUserRankCount;						// User Rank's Number(Array Length)
	var nSortedTextLength;
	var strSeparatorChar = '-----------------------------';

	nUserRankCount = arrUserRank.length;

    if (typeof boxIdObj == 'string') obj = document.getElementById(boxIdObj); 
    else obj = boxIdObj; 

    if (obj.tagName.toLowerCase() != 'select') return false; 

    sArr = new Array(obj.options.length); 
	oArr = new Array; 

	// Sort By Text Value(Alphabetic)
    for (idx = 0; idx < obj.options.length; idx++) 
    { 
		sArr[idx] = obj.options[idx].text.toUpperCase(); 
        oArr[sArr[idx]] = obj.options[idx];
    } 
	oArr[strSeparatorChar] = new Option(strSeparatorChar, '');
    sArr.sort(); 

	// Sort By User Rank
	var arrNewRank = new Array;
	var IsRank = false;

	for (i = 0; i < nUserRankCount; i++)
	{
		for (j = 0; j < sArr.length; j++)
		{
			if (arrUserRank[i].toUpperCase() == sArr[j])
			{
				arrNewRank.push(sArr[j]);
			}
		}
	}

	arrNewRank.push(strSeparatorChar);

	for (i = 0; i < sArr.length; i++)
	{
		for (j = 0; j < nUserRankCount; j++)
		{
			if (sArr[i] == arrNewRank[j])
			{
				IsRank = true;
				break;
			}
		}

		if (IsRank != true)
		{
			arrNewRank.push(sArr[i]);
		} else
		{
			IsRank = false;
		}
	}

    for (i = 0; i < arrNewRank.length; i++)
	{
		obj.appendChild(oArr[arrNewRank[i]]); 
	}

	obj.options[0].selected = true;
}