var httphrefShort = location.href;
httphrefShort = httphrefShort.replace("http://", "");
var arrhttphrefShort = httphrefShort.split("/");
httphrefShort = "http://" + arrhttphrefShort[0];

var pathwebcheckavailabilityShort = httphrefShort + '/smartreserve/indexShort.php';
var ckAvailStartTdyShort = new Date();
//remove time from Date
var dateAddNight = 0;
ckAvailStartTdyShort = new Date(ckAvailStartTdyShort.getFullYear(),  ckAvailStartTdyShort.getMonth(), ckAvailStartTdyShort.getDate());
var ckAvailStartDateShort = new Date(ckAvailStartTdyShort.getFullYear(),  ckAvailStartTdyShort.getMonth(), ckAvailStartTdyShort.getDate() + dateAddNight);

function sm_getForm() {
	var formname = (arguments[0]) ? arguments[0] : "checkAvailShort";
	return document.forms[formname];
}

function sm_getEl(tmpname) {
	var formuse = (arguments[1]) ? arguments[1] : sm_getForm();
	return formuse.elements[tmpname];
}

//->this section may be same javascript other file
function padZero(num) {
	return (num< 10) ? '0' + num : num ;
}

function IsIE() {
	return navigator.appVersion.match(/\bMSIE\b/);
}

function ChDatetoStr(cDate) {
	return padZero(cDate.getDate()) + '/' + padZero(cDate.getMonth() + 1) + '/' + cDate.getFullYear();
}

function ChStrtoDate(cDate) {
	var arrcDate = cDate.split("/");
	return new Date(arrcDate[2], Number(arrcDate[1])-1, Number(arrcDate[0]));
}

function setValue(arrElement, values) {
	var formuse = (arguments[2]) ? arguments[2] : sm_getForm();
	var i;
	var j = arrElement.length;
	var tmpInput;
	for(i=0;i<j;i++) {
		tmpInput = sm_getEl(arrElement[i], formuse);
		if(tmpInput.type.indexOf("select")==0) setElement(tmpInput, values);
		else tmpInput.value = values;
	}
}

function setElement(cElement,cValue) {
	for(var i=0;i<cElement.length;i++) {
		if(cElement.options[i].value==cValue) {
			cElement.selectedIndex = i;
			break;
		}
	}
}
//<-this section may be same javascript other file

function startapp(formuse, TypeSet) {
	//var tmp = obj.parentNode;
	//while(tmp.nodeName!="FORM") {tmp = tmp.parentNode;}
	//var formuse = tmp;

	var m;
	//create date option
	var days = new Array("checkHotel[sDay2]", "checkHotel[eDay2]");
	var tmpdate;
	for(m=0;m<2;m++) {
		obj = sm_getEl(days[m], formuse);
		obj.length = 0;
		for(i=0;i<31;i++) {
			tmpdate = padZero(i+1);
			obj.options[i]=new Option(tmpdate, tmpdate);
		}	
	}

	//create month option
	var months = new Array("checkHotel[sMonth2]", "checkHotel[eMonth2]");
	var tmpmonth;
	if(sm_getEl('checkHotel[LanguageCode]', formuse).value=="ja") 
		tmpmonth = new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
	else 
		tmpmonth = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

	for(m=0;m<2;m++) {
		obj = sm_getEl(months[m], formuse);
		obj.length = 0;
		for(i=0;i<12;i++) {
			obj.options[i]=new Option(tmpmonth[i], padZero(i+1));
		}
	}

	//create year option
	var years = new Array("checkHotel[sYear2]", "checkHotel[eYear2]");
	var m;
	var tmpyear;

	var tnow = new Date();
	var sMonth = tnow.getMonth() + 1;
	var sYear = tnow.getFullYear();
	var sYearOld = sYear;
	var i;
	for(i=0;i<=17;i++) {
		sMonth = sMonth + 1;
		if(sMonth>=13) {
			sMonth = 1;
			sYear = sYear + 1;
		}
	}
	prefixyearAddOn = sYear - sYearOld;

	for(m=0;m<2;m++) {
		obj = sm_getEl(years[m], formuse);
		obj.length = 0;
		tmpyear = ckAvailStartTdyShort.getFullYear();
		for(i=0;i<=prefixyearAddOn;i++) {
			obj.options[i]=new Option(tmpyear, tmpyear);
			tmpyear++;
		}
	}

	sm_setAllDate(ChDatetoStr(ckAvailStartTdyShort), "sDate");
	sm_setAllDate(ChDatetoStr(ckAvailStartDateShort), "eDate");
	sm_getEl('checkHotel[Nights]', formuse).value = dateAddNight;
	if(sm_getEl('checkHotel[pDestinationID]', formuse)) showshortDestination(TypeSet, formuse);
	else setSubmitButtonShort(true);
}

//dates is string date = 31/01/2007
function sm_setAllDate(dates, caseset) {
	var arrtmpdate = dates.split("/");

	if(caseset=="sDate") {
		var a = new Array("checkHotel[sDay]", "checkHotel[sDay2]");
		var b = new Array("checkHotel[sMonth]", "checkHotel[sMonth2]");
		var c = new Array("checkHotel[sYear]", "checkHotel[sYear2]");
		var d = new Array("txtCheck_InShort");
	} else

	if(caseset=="eDate") {
		var a = new Array("checkHotel[eDay]", "checkHotel[eDay2]");
		var b = new Array("checkHotel[eMonth]", "checkHotel[eMonth2]");
		var c = new Array("checkHotel[eYear]", "checkHotel[eYear2]");
		var d = new Array("txtCheck_OutShort");
	}

	setValue(a, arrtmpdate[0]);
	setValue(b, arrtmpdate[1]);
	setValue(c, arrtmpdate[2]);
	setValue(d, dates);
}

function showshortDestination(fCountry, formuse) {
	var a = sm_getEl("pDestinationID", formuse).value;
	var b = sm_getEl("DestinationID", formuse).value;
	en_disListShort(true, formuse);
	var lang = sm_getEl('checkHotel[LanguageCode]', formuse).value;
	var x = pathwebcheckavailabilityShort + '?action=dest&deF='+a+'&deF2='+b+'&lang='+lang;
	if(fCountry!="") x = x + "&TypeSet=" + fCountry;
	runXmlReturnJavascript(x);
}

function changelistShort(aarr, barr, obj, indexSelected) {
	var i;
	obj = sm_getEl(obj);
	obj.length = 0;
	for(i=0;i<aarr.length;i++) {
		obj.options[i]=new Option(aarr[i],barr[i]);
	}
	obj.selectedIndex = indexSelected;
	en_disListShort(false);
}

function sm_search(formuse) {
	if(sm_getEl("checkHotel[pDestinationID]", formuse)) {
		if(sm_getEl("checkHotel[pDestinationID]", formuse).value=="0") {
			alert("Please select a country");
			return false;
		}
		
		sm_getEl("pDestinationID", formuse).value = sm_getEl("checkHotel[pDestinationID]", formuse).value;
		
		if(sm_getEl("checkHotel[DestinationID]", formuse).options[0].selected) {
			sm_getEl("checkHotel[DestinationID]", formuse).options[0].value = sm_getEl("pDestinationID", formuse).value;
		}
		sm_getEl("DestinationID", formuse).value = sm_getEl("checkHotel[DestinationID]", formuse).value;
	}

	formuse.submit();
}

function changeDestinationShort() {
	var obj = sm_getEl('checkHotel[pDestinationID]');
	if(obj.value=="0") {
		var a = sm_getEl("checkHotel[DestinationID]");
		a.length = 0;
		a.options[0]=new Option("Select a city/area", 0);
		return false;
	}

	en_disListShort(true);
	var b = sm_getEl("pDestinationID").value;
	var lang = sm_getEl('checkHotel[LanguageCode]').value;
	var x = pathwebcheckavailabilityShort + '?action=hotel&hotel='+obj.value+'&deF='+b+'&lang='+lang;
	runXmlReturnJavascript(x);
}

function setSubmitButtonShort(flag) {
	var formuse = (arguments[1]) ? arguments[1] : sm_getForm();
	var cElement = formuse.getElementsByTagName('input')
	var i;
	var j = cElement.length;
	var k = null;
	for(i=0;i<j;i++) {
		if(cElement[i].name=="Submit") k = cElement[i];
	}
	if(k!=null) k.style.display = (flag) ? "" : "none";
}

function en_disListShort(flag) {
	var cForm = (arguments[1]) ? arguments[1] : sm_getForm();
	var cElement = cForm.elements['checkHotel[pDestinationID]'];
	cElement.disabled=flag;
	var cElement = cForm.elements['checkHotel[DestinationID]'];
	cElement.disabled=flag;
	setSubmitButtonShort(false, cForm);
}

function ValidateDate(tmpdate) {
	var defRtn = (arguments[1]) ? arguments[1] : 99;
	var chkdate = ChDatetoStr(ChStrtoDate(tmpdate));
	if(defRtn==99) return (chkdate==tmpdate);
	else return (chkdate==tmpdate) ? tmpdate : defRtn;
}

function updateCheckAvailAllShort(column) {
	var obj = (arguments[1]) ? arguments[1] : null;
	var defDate;
	var dateSel;
	var tmpselmonth;
	if(column=="sDate") {
		defDate = ChDatetoStr(ckAvailStartTdyShort);
		tmpselmonth = sm_getEl("checkHotel[sMonth2]").value;
		dateSel = sm_getEl("checkHotel[sDay2]").value + '/' + tmpselmonth + '/' + sm_getEl("checkHotel[sYear2]").value;
	} else 
	
	if(column=="eDate") {
		defDate = ChDatetoStr(ckAvailStartDateShort);
		tmpselmonth = sm_getEl("checkHotel[eMonth2]").value;
		dateSel = sm_getEl("checkHotel[eDay2]").value + '/' + tmpselmonth + '/' + sm_getEl("checkHotel[eYear2]").value;
	}

	dateSel = ValidateDate(dateSel, defDate);
	var tmpdateSel = ChStrtoDate(dateSel);
	if(
		(tmpdateSel.getTime()<ckAvailStartTdyShort.getTime()) &&
		(tmpselmonth-1==ckAvailStartTdyShort.getMonth())
		) {
		var tmpnewmonth = new Date(tmpdateSel.getFullYear(),  Number(tmpselmonth), tmpdateSel.getDate());
		dateSel = ChDatetoStr(tmpnewmonth);
	}

	sm_setAllDate(dateSel, column);

	var ckIn = ChStrtoDate(sm_getEl("txtCheck_InShort").value);
	var ckOut = ChStrtoDate(sm_getEl("txtCheck_OutShort").value);

	if(ckIn.getTime()<ckAvailStartTdyShort.getTime()) {
		sm_setAllDate(ChDatetoStr(ckAvailStartTdyShort), "sDate");
	}

	if(ckOut.getTime()<=ckIn.getTime()) {
		var tmpaddday = ChStrtoDate(sm_getEl("txtCheck_InShort").value);
		tmpaddday = new Date(tmpaddday.getFullYear(), tmpaddday.getMonth(), tmpaddday.getDate() + 0);
		sm_setAllDate(ChDatetoStr(tmpaddday), "eDate");
	}

	try {
		var ckIn = ChStrtoDate(sm_getEl("txtCheck_InShort").value);
		var ckOut = ChStrtoDate(sm_getEl("txtCheck_OutShort").value);
		var cTime  = ckOut.getTime() - ckIn.getTime();
		var nDate  = new Date(cTime);
		var cValue = Number(nDate.getDate()) - 1;
		cValue = Math.floor(cTime / (1000 * 60 * 60 * 24));
		sm_getEl("checkHotel[Nights]").value = cValue;		
	} catch(e) 	{}
}

function showChangeDateShort(values) {
	if(values=='in') {
		sm_setAllDate(sm_getEl('txtCheck_InShort').value, "sDate");
		updateCheckAvailAllShort('sDate');
	} else 

	if(values=='out') {
		sm_setAllDate(sm_getEl('txtCheck_OutShort').value, "eDate");
		updateCheckAvailAllShort('eDate');
	}
}


