
var g_strPath = get_strPhysicalPath(false) + "ver2.0/iuser/"

function quickResvPopCalendar(p_objCaller, p_objForm, dayObj, monthYearObj) 
{

	var dateHtl = new Date()
		
	var monthHtl = dateHtl.getMonth()+1;
	var yearHtl = dateHtl.getFullYear();
	var yearTo = dateHtl.getFullYear()+2;
		
	PopCalendar(p_objCaller, '2', eval(p_objForm+'.'+dayObj), eval(p_objForm+'.'+monthYearObj), '', '', '', monthHtl, yearHtl, monthHtl, yearTo);
}

function showCalWithOpt(p_objCaller, p_objForm, p_option, p_intDefLOS) {
	with (eval("document."+p_objForm)) {
		var dateHtl = new Date()
		
		var monthHtl = dateHtl.getMonth()+1;
		var yearHtl = dateHtl.getFullYear();
		var yearTo = dateHtl.getFullYear()+2;
		var ArrDateInit;
		var monthYearObj;
		var dayObj;
		var monthInit;
		var yearInit;				
		var v_strReserv;
		var dayToObj = "";
		var monthYearToObj = "";
		
		switch (p_option) {
			case "REQUEST" : ArrDateInit	= slcCheckInMonthYearReq.value.split('/');
											 dayObj		  = "txtCheckOutDayReq";
											 monthYearObj = "slcCheckOutMonthYearReq";
											 v_strReserv  = "request";
											 break;
			case "REQUEST-CI" : ArrDateInit	= slcCheckInMonthYearReq.value.split('/');
											 dayObj		  = "txtCheckInDayReq";
											 monthYearObj = "slcCheckInMonthYearReq";
											 v_strReserv  = "request";
											 dayToObj	 = eval("txtCheckOutDayReq");
											 monthYearToObj = eval("slcCheckOutMonthYearReq");
											 break;
			case "ROOM" : ArrDateInit		= slcCheckInMonthYear.value.split('/');
										dayObj			= "txtCheckOutDay";
										monthYearObj	= "slcCheckOutMonthYear";
										v_strReserv  = "reserv";
										break;
			case "ROOM-CI" : ArrDateInit		= slcCheckInMonthYear.value.split('/');
										dayObj		 = "txtCheckInDay";
										monthYearObj = "slcCheckInMonthYear";
										v_strReserv  = "reserv";
										dayToObj	 = eval("txtCheckOutDay");
										monthYearToObj = eval("slcCheckOutMonthYear");
										break;
			case "FLIGHT-ARR" : ArrDateInit		= slcMonthYearArr.value.split('/');
													dayObj				= "txtDayArr";
													monthYearObj	= "slcMonthYearArr";
													break;
			case "FLIGHT-DEP" : ArrDateInit		= slcMonthYearDep.value.split('/');
													dayObj				= "txtDayDep";
													monthYearObj	= "slcMonthYearDep";
													break;
		}
		
		monthInit	= ArrDateInit[0];
		yearInit	= ArrDateInit[1];
		
		if (monthInit == "pick"){
			monthInit = '';
			yearInit  = '';
		}
		PopCalendar(p_objCaller, '2', eval(dayObj), eval(monthYearObj), '', monthInit, yearInit, monthHtl, yearHtl, monthHtl, yearTo, '', '', dayToObj, monthYearToObj, '', p_intDefLOS);
	}
}


function writeYearCombo() {
	var v_dteToday = new Date()
	var v_intMonth = v_dteToday.getMonth()
	var v_intYear = v_dteToday.getFullYear()
								
	var v_arrMonthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", 
											  "September", "October", "November", "December");
	
	for (var i=0;i<24;i++) {
		document.write("<option value='" + parseInt(v_intMonth+1) + "/" + v_intYear + "'>" + v_arrMonthArray[v_intMonth] + ", " + v_intYear + "</option>")
									
		v_intMonth += 1
									
		if(v_intMonth > 11) {
			v_intMonth = 0;
			v_intYear += 1
		}	
	}
}

function allowNumericOnly(p_objText){
	var v_regExp = /^\d+$/;
	var v_strTemp;
	
	with (p_objText) {
		if (value.length<2) {
			v_strTemp= value;
			if ( !v_regExp.test(v_strTemp) ) value= "";
		}
		else {
			v_strTemp = value.substring(0,1);
			if ( !v_regExp.test(v_strTemp) ) value = value.substring(1,value.length);
			
			v_strTemp= value.substring(value.length-1);
			if ( !v_regExp.test(v_strTemp) ) value = value.substring(0,value.length-1);
			
			if ( !v_regExp.test(value) ) {

				v_strTemp = "";
				for (var v_intCounter=0; v_intCounter<value.length; v_intCounter++) {
					if ( v_regExp.test( value.substring(v_intCounter, v_intCounter+1) ) ) v_strTemp += value.substring(v_intCounter, v_intCounter+1);
				}
				value = v_strTemp;
			}
		}
	}
	return true;
}

function isDate(nday,nmonth,nyear){
	if(nyear<1) return false;
	nmonth-=1;
	var dtemp=new Date(nyear,nmonth,nday)
	var dyear=dtemp.getFullYear()
	if((dyear==nyear) && (dtemp.getMonth()==nmonth) && (dtemp.getDate()==nday))
		return true
	else
		return false
}

function getCheckOutDay(p_frmName){
	var strSelectedDay,strSelectedMonth,strSelectedYear;	
	var strNewMonth,strNewMonthYear;
	var strNewCOMonthYear,arrCIMonthYear;	
	var strTxtCIDay;
	var strSlcCIMonthYear;
	var strTxtCODay;
	var strSlcCOMonthYear;
	

	with(eval("document."+p_frmName)){				
		strTxtCIDay = eval("txtCheckInDay");
		strSlcCIMonthYear = eval("slcCheckInMonthYear");
		strTxtCODay = eval("txtCheckOutDay");
		strSlcCOMonthYear = eval("slcCheckOutMonthYear");			
		
		
		if((strSlcCIMonthYear.value != "pick") && (strTxtCIDay.value != "")) {	
			strCurrDay = strTxtCIDay.value;
			arrCIMonthYear = strSlcCIMonthYear.value.split("/");			
			if(isDate(strCurrDay, arrCIMonthYear[0], arrCIMonthYear[1])){
				strSelectedDay = parseInt(strCurrDay)+1;
				strSelectedMonth = parseInt(arrCIMonthYear[0],10)-1;
				strSelectedYear = arrCIMonthYear[1];				
				strNewCOMonthYear = new Date(strSelectedYear,strSelectedMonth,strSelectedDay);
				
				//set new value to check out day
				strTxtCODay.value = strNewCOMonthYear.getDate() ;
				//set new value to check out month year
				strNewMonth = parseInt(strNewCOMonthYear.getMonth()) + 1;
				//if (parseInt(strNewMonth,10) < 10){strNewMonth = "0" + strNewMonth;}
				strNewMonthYear = strNewMonth +'/'+strNewCOMonthYear.getFullYear();
				
				for(var i=0; i < strSlcCOMonthYear.length; i++){
					if(strNewMonthYear == strSlcCOMonthYear[i].value){
						strSlcCOMonthYear[i].selected = true;				
					}
				}
			}else{
				strTxtCODay.value = "";
				strSlcCOMonthYear[0].selected = true;
			}
		}else{
			strTxtCODay.value = "";
			strSlcCOMonthYear[0].selected = true;
		}
	}
}

function checkPeriodCICO(p_dayCi, p_arrCheckIn, p_dayCo, p_arrCheckOut) {
	with (document.frmQuickCICO) {
		var v_strMsg = "";
		
		if (!isDate(p_dayCi, p_arrCheckIn[0], p_arrCheckIn[1])) {
			if (v_strMsg == "") { txtCheckInDay.focus(); }
			v_strMsg += "Check In Date input is incorrect.\n";
		}
		
		if (!isDate(p_dayCo, p_arrCheckOut[0], p_arrCheckOut[1])) {
			if (v_strMsg == "") { txtCheckOutDay.focus(); }
			v_strMsg += "Check Out Date input is incorrect.\n";
		}
		
		var v_dteCheckIn = new Date(parseInt(p_arrCheckIn[1]), parseInt(p_arrCheckIn[0])-1, parseInt(p_dayCi));
		var v_dteCheckOut = new Date(parseInt(p_arrCheckOut[1]), parseInt(p_arrCheckOut[0])-1, parseInt(p_dayCo));
	
		if (v_strMsg == "") {
			if (Date.UTC(p_arrCheckOut[1], parseInt(p_arrCheckOut[0])-1, parseInt(p_dayCo)) == Date.UTC(p_arrCheckIn[1], parseInt(p_arrCheckIn[0])-1, parseInt(p_dayCi))) {
				if (v_strMsg == "") { slcCheckOutMonthYear.focus(); }
				v_strMsg += "Check-Out Date must be later than Check-In Date\n";
			}
				
			if (Date.UTC(p_arrCheckOut[1], parseInt(p_arrCheckOut[0])-1, parseInt(p_dayCo)) < Date.UTC(p_arrCheckIn[1], parseInt(p_arrCheckIn[0])-1, parseInt(p_dayCi))) {
				if (v_strMsg == "") { slcCheckOutMonthYear.focus(); }
				v_strMsg += "Check-Out Date cannot be earlier than Check-In Date\n";
			}
		}	
		

		if(v_strMsg == ""){
			var v_intNumOfAdult = txtNumOfAdult.value;
			var v_intNumOfRoom = txtNumOfRoom.value;
			var v_dblResult = (v_intNumOfAdult/v_intNumOfRoom);
			
			if(txtNumOfRoom.value == "" && txtNumOfAdult.value != "") {
				if(v_strMsg == ""){txtNumOfRoom.focus();}
				v_strMsg += "Please enter number of room to accomodate the number of adult entered\n";
			} else {
				if(v_dblResult < 1 && txtNumOfAdult.value != ""){
					if(v_strMsg == ""){txtNumOfAdult.focus();}
					v_strMsg += "Minimum number of adult should be "+ v_intNumOfRoom +" person(s) or more\n";
				}
				if(v_dblResult > 4){
					if(v_strMsg == ""){txtNumOfAdult.focus();}
					v_strMsg += "Number of room requested is not enough to accommodate the number of adult\n";
				}
			}	
		}
		
		if (v_strMsg == "") { 
			return true;
		} else {
			alert(v_strMsg);
			return false;
		}	
	}	
}

function goLink(p_strOption) {
	with (document.frmQuickCICO) {
		var v_strHotelcd
		var v_strPath = g_strPath
		
		if(HID_PopUpWindow.value == 'Y'){
			target = "_blank";
		}else{
			if(HID_BackPage.value == ''){
				alert('HID_BackPage is empty')
			}
		}
		
		
		switch(p_strOption){
			case "CANCEL" :			
				var dayCI = txtCancelDay.value;
				var checkIn = slcCancelMonthYear.options[slcCancelMonthYear.selectedIndex].value.split("/");
				HID_Mode.value = 'CANCELLATION';
				action = v_strPath + "BOtherRsv.asp"
				HID_CheckIn.value = checkIn[0] +"/"+ dayCI +"/"+ checkIn[1];
				HID_EmailAddress.value = txtCancelEmail.value;				
				submit();
				break;
			case "AMEND" :
				var dayCI = txtAmendDay.value;
				var checkIn = slcAmendMonthYear.options[slcAmendMonthYear.selectedIndex].value.split("/");
				HID_Mode.value = 'AMEND';
				action = v_strPath + "BOtherRsv.asp"
				HID_CheckIn.value = checkIn[0] +"/"+ dayCI +"/"+ checkIn[1];
				HID_EmailAddress.value = txtAmendEmail.value;				
				submit();
				break;
			case "RESV" :
				var dayCI = txtCheckInDay.value;
				var dayCO = txtCheckOutDay.value;
				var checkIn = slcCheckInMonthYear.options[slcCheckInMonthYear.selectedIndex].value.split("/");
				var checkOut = slcCheckOutMonthYear.options[slcCheckOutMonthYear.selectedIndex].value.split("/");
				var v_intnumOfChild;
				var v_bolResult = false;
				
				
				
		
				//set variable to hidden
				if (checkPeriodCICO(dayCI, checkIn, dayCO, checkOut)) {					
					if (!compareBetweenTwoDate(slcCheckInMonthYear.options[slcCheckInMonthYear.selectedIndex].value, txtCheckInDay.value, slcCheckOutMonthYear.options[slcCheckOutMonthYear.selectedIndex].value, txtCheckOutDay.value ,"COMPARE30"))
					{
						v_bolResult = window.confirm("Are you sure that the period of stay is more than 30 days?\n")						
					}else {
						v_bolResult = true;					
					}	
				}
				
				var v_strErr = ""
				v_intnumOfChild = "";				
				if(typeof(slcChild) != 'undefined'){
					v_intnumOfChild= slcChild.options[slcChild.selectedIndex].value;
					if(v_intnumOfChild > 0){
						for(i=1; i <= v_intnumOfChild; i++){
							if(eval("txtChildAge" + i).value == ""){
								v_bolResult = false;
								v_strErr += "Please enter Children age \n"
								break;
							}
						}								
					}
				}
				
				if(v_bolResult){
					var v_strChildAge = "";
					
					if(v_intnumOfChild > 0){
						for(i=1; i <= v_intnumOfChild; i++){
							if(typeof(eval("txtChildAge" + i)) == "object"){
								v_strChildAge += eval("txtChildAge" + i).value + "|#|"
							}						
						}
					}				
				
					//Add by andryanus 07-06-2007
					if(typeof(txtPromoCode) != 'undefined')
					{
						HID_PromoCode.value = getTrim(txtPromoCode.value)
					}
				
					HID_CheckIn.value = checkIn[0] +"/"+ dayCI +"/"+ checkIn[1];
					HID_CheckOut.value = checkOut[0] +"/"+ dayCO +"/"+ checkOut[1];				
					HID_Mode.value = 'RESV';
					HID_NumOfRoom.value = txtNumOfRoom.value;
					HID_NumOfAdult.value = txtNumOfAdult.value;
					HID_NumOfChild.value = v_intnumOfChild
					HID_ChildAge.value = v_strChildAge
					
					action = v_strPath + "BOtherRsv.asp"					
					submit();					
				}else{
					if(v_strErr != ''){
						alert(v_strErr)
					}
					return false;
				}
				
				break;
			}
	}
}

function compareBetweenTwoDate(p_dtMonthYear1, p_dtDay1, p_dtMonthYear2, p_dtDay2, p_option) {
	var v_arrMonthYear1 = p_dtMonthYear1.split('/');
	var v_intMonth1			= parseInt(v_arrMonthYear1[0],10)-1;
	var v_intYear1			= parseInt(v_arrMonthYear1[1],10);
	var v_intDay1				= parseInt(p_dtDay1,10);
	
	var v_arrMonthYear2	= p_dtMonthYear2.split('/');
	var v_intMonth2			= parseInt(v_arrMonthYear2[0],10)-1;
	var v_intYear2			= parseInt(v_arrMonthYear2[1],10);
	var v_intDay2				= parseInt(p_dtDay2,10);
	
	var v_date1 = new Date(v_intYear1, v_intMonth1, v_intDay1);
	var v_date2 = new Date(v_intYear2, v_intMonth2, v_intDay2);
	
	if (p_option == "COMPARE30") {
		if (dateDiff("d", v_date1, v_date2) > 30) {
			return false;
		} else {
			return true;
		}
	}
}

function dateDiff(p_strType, p_dtFrom, p_dtTo){
	var v_dblMicroSecond;

	v_dblMicroSecond = Date.UTC( p_dtTo.getFullYear(), p_dtTo.getMonth(), p_dtTo.getDate() ) -
	Date.UTC( p_dtFrom.getFullYear(), p_dtFrom.getMonth(), p_dtFrom.getDate() );
	
	switch(p_strType){
		case "d":
			return v_dblMicroSecond/1000/60/60/24;
			break;
		case "m":
			return v_dblMicroSecond/1000/60/60;
			break;
		case "n":
			return v_dblMicroSecond/1000/60;
			break;
		case "s":
			return v_dblMicroSecond/1000;
			break;
	}
	return true;
}

function fSearchAmendCancel(p_strMode) {
	with(document.frmQuickCICO) {

		var v_strErrMessage = "";
		var v_strFocus = "";
		var v_arrDateTimeHotel;
		var v_dteDateTimeHotel;		
		var v_arrTemp;
		var v_objEmail
		var v_objDay
		var v_objSelectMonthYear
		
		if(p_strMode == "AMEND"){
			v_objEmail = txtAmendEmail
			v_objDay = txtAmendDay
			v_objSelectMonthYear = slcAmendMonthYear
			
		}else{
			v_objEmail = txtCancelEmail
			v_objDay = txtCancelDay
			v_objSelectMonthYear = slcCancelMonthYear
		}
		
		
		//start validasi
		if (getTrim(v_objEmail.value, " ") == "") {
			v_strErrMessage += 'Please enter Main Guest Email Address' + '\n';
			if (v_strFocus == "") {	
				v_strFocus = v_objEmail;
			}	
		} else {
			if (!isEmail(getTrim(v_objEmail.value))) {
				v_strErrMessage += 'Email Address is invalid. Please verify' + '\n';
				if (v_strFocus == "") {	
					v_strFocus = v_objEmail;
				}	
			}
		}
		
		if (getTrim(v_objDay.value, "") == "" || v_objSelectMonthYear.selectedIndex == 0) {
			v_strErrMessage += 'Please enter Check-In Date' + '\n';
			if (v_strFocus == "") {	
				v_strFocus = v_objDay;
			}
		} else {
			v_arrTemp = v_objSelectMonthYear.options[v_objSelectMonthYear.selectedIndex].value.split("/");
			if (!isDate(v_objDay.value,v_arrTemp[0],v_arrTemp[1])) {
				v_strErrMessage += 'Check-In Date input is incorrect. Please verify.' + '\n';
				if (v_strFocus == "") {	
					v_strFocus = v_objDay;
				}						
			} else {						
			}		
		
		}
		
		if (v_strErrMessage != "") {
			alert(v_strErrMessage);
			v_strFocus.focus();
		} else {
			v_arrTemp = v_objSelectMonthYear.options[v_objSelectMonthYear.selectedIndex].value.split("/");
			//set data to hidden
			goLink(p_strMode)
		}				
		
		
		
	}
}

function showChildAge(){
	with(document.frmQuickCICO){
		var v_intnumOfChild = slcChild.options[slcChild.selectedIndex].value;
		var v_strRowChildAge = "";
		var i;
		
		//if(v_intnumOfChild != "0"){
			v_strRowChildAge =	"<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
			for(i=1; i <= v_intnumOfChild; i++){
				v_strRowChildAge +=	"<tr class='mainfont'>" +
									"	<td width='32%'>Child Age "+ i +"</td> " +
									"	<td>" +
									"		<input class='mainfont' name='txtChildAge"+ i +"' size='1' maxlength='2' onkeyup='allowNumericOnly(this)' onkeydown='allowNumericOnly(this)' onblur='allowNumericOnly(this)' class='font11px' value=''>" +
									"	</td> " +
									" </tr>";
			}
			
			v_strRowChildAge += "</table>"
			
			divChildAge.innerHTML = v_strRowChildAge
			divChildAge.style.display = "block"; 
		//}
	}
}

function doModifyCancel(p_strMode){
	with(document.frmQuickCICO)
	{
		if(HID_PopUpWindow.value == 'Y'){
			target = "_blank";
		}else{
			if(HID_BackPage.value == ''){
				alert('HID_BackPage is empty')
			}
		}
		
		if(p_strMode == "MODIFY"){
			action = g_strPath + "Index.asp?hotelcd=" + HID_HotelCd.value + "&mode=AMEND"
		}else if(p_strMode == "CANCEL"){
			action = g_strPath + "Index.asp?hotelcd=" + HID_HotelCd.value + "&mode=CANCEL"
		}
		
		submit();
	}
}

