var domain_link = 'https://www.reservationpage.com';
var mychainid = 'C00227';
var myhotelid;     

/*****************************************************************/

function validate() {

	//Check-in
	if (document.enquiry.txt_outBound.value =="mm/dd/yyyy") { 
		alert('Missing Check-in date')
		return false; 
	};

	//Check-out
	if (document.enquiry.txt_inBound.value =="mm/dd/yyyy") { 
		alert('Missing Check-out date')
		return false; 
	};
	
	//Adults
	if (document.enquiry.adults.value =="") { 
		alert('Missing Number of Adults')
		return false; 
	};
	
	//Children
	if (document.enquiry.children.value =="") { 
		alert('Missing Number of Children')
		return false; 
	};
	
	date1 = new Date(document.enquiry.txt_outBound.value);
	date2 = new Date(document.enquiry.txt_inBound.value);
	monthyear1 = (date1.getMonth()+1) + " " + date1.getFullYear();
	day1 = date1.getDate();
	nights = days_between(date1, date2);
	
	if(date1 > date2 || nights == 0){
		alert('Invalid Dates')
		return false; 
	};
	
	if (document.enquiry.checkcapacity!=undefined){
		if (checkDateCapacity(date1,date2,nights) == false)
		{
		return false;
		}
	}
	
	var lg;
	myhotelid = document.enquiry.promoMainCatIDResort.value;
	
	if (document.enquiry.bucketLodgeCode!=undefined){
		myhotelid = document.enquiry.bucketLodgeCode.value
	}
	
	if (document.enquiry.pagename.value!='rocksitepackage'){
		if (document.enquiry.promoCode.value=='') {
			if (document.enquiry.iid!=undefined){
				if (myhotelid == '' && document.enquiry.promoMainCatIDInterest.value == '') {
					alert('Please select a Resort or an Interest');return false; 
				};
			}else{
				if (myhotelid == '') {
					alert('Please select a Resort');return false; 
				};
			};
		}
	}

	if (document.enquiry.golfform != null){
		totgolfdays = 0
		for (i = 0;  i < document.enquiry.golfday.length;  i++){
			
			if (document.enquiry.golfday[i].checked){
				totgolfdays = totgolfdays + 1
				}
			}
		if (totgolfdays<1){
			alert("You must select at least one day of golfing with this package.");
			return false;
			}
		if (totgolfdays>document.enquiry.maxgolfdays.value){
			golfdaysmsg = "You may only select a maximum of "+document.enquiry.maxgolfdays.value+" day(s) of golfing with your current lenght of stay.\n\n"
			golfdaysmsg = golfdaysmsg + "You have selected "+ totgolfdays +" days of golfing.\n\n"
			golfdaysmsg = golfdaysmsg + "Please remove at lease "+ Number(totgolfdays - Number(document.enquiry.maxgolfdays.value)) +" day(s) of golfing."
			alert(golfdaysmsg)
			return false;
			}
		
		}

	for (j=Number(document.enquiry.children.value)+1;j<=document.enquiry.maxnumofchildren.value;j++) {
		//alert(j);
		hideRow('childAge'+j);
		document.enquiry.childages[j-1].value = '';
	};
	if (document.enquiry.children.value>0){
		for (j=1;j<=document.enquiry.children.value;j++) {
			//alert(j);
			showRow('childAge'+j);
		};
		vcuiDisplayChildAgesContainer(document.getElementById('childAgesContaineriframe'));
		document.getElementById('childAgesContainer').style.marginTop = Number(document.enquiry.children.value)*(-24); 
		vcuiDisplayChildAgesContainer(document.getElementById('childAgesContainer'));
	}else{
		document.getElementById('childAgesContaineriframe').style.display = 'none';
		document.getElementById('childAgesContainer').style.display = 'none';
		document.enquiry.submit()
	};
	
	return false;
}

function vcuiSubmitVCUI(){
	var intNumberOfChildren = Number(document.enquiry.children.value);
	if (document.enquiry.children.value>0){
		for (j=1;j<=document.enquiry.children.value;j++) {
			document.enquiry.childages[j-1].value = document.getElementById('age'+j).value;
		};
	};
	document.enquiry.submit()
}

function vcuiDisplayChildAgesContainer(elem)
{
	var winWidth = 0, winHeight = 0;
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}
	// Give elem height/width but invisible
	elem.style.left = null;
	elem.style.visibility = 'hidden';
	elem.style.display = 'block';
		
	var tooltipWidth = elem.offsetWidth;
	var tooltipLeftPos = elem.offsetLeft;
	var tooltipLeftPosAdjusted = winWidth - tooltipWidth;
	if (tooltipLeftPosAdjusted < tooltipLeftPos)
	{
		elem.style.left = tooltipLeftPosAdjusted-60 + 'px';
	}

	//alert(tooltipLeftPos);
	elem.style.visibility = 'visible';
	

} // vcuiDisplayTooltip

strRowDisplay = 'table-row'

if (navigator.appName.indexOf("Microsoft")>-1){;
	strRowDisplay = 'block'
	}

function showRow(strRow){
	document.getElementById(strRow).style.display=strRowDisplay
}

function hideRow(strRow){
	document.getElementById(strRow).style.display='none'

}

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

//Begin Legendary Lodging additions
function bookSynXis(date1, date2, adults, child, hotelid, chainid) {

	document.resSynXisForm.Hotel.value = hotelid;
	document.resSynXisForm.Chain.value = chainid;
	
	document.resSynXisForm.arrive.value = date1;
	document.resSynXisForm.depart.value = date2;
	document.resSynXisForm.adult.value = adults;
	document.resSynXisForm.child.value = child;
	
	document.resSynXisForm.submit();
	
	return false;
	
}

function bookMarriott(date1, date2, adults, child, nights, rooms) {

	document.resMarriott.fromDate.value = date1;
	document.resMarriott.toDate.value = date2;
	document.resMarriott.numberOfGuests.value = Number(adults) + Number(child);
	document.resMarriott.numberOfNights.value = nights;
	document.resMarriott.numberOfRooms.value = rooms;

	document.resMarriott.submit();
	
	return false;

}

function bookVCUI(date1, date2, adults, child, propertyName) {

	var agesTable = document.getElementById("childAgesForm");

	document.quickbook.arrival.value = date1;
	document.quickbook.departure.value = date2;
	document.quickbook.adults.value = adults;
	document.quickbook.children.value = child;
	document.quickbook.propertyName.value = propertyName;
	
	//alert(document.quickbook.children.value);
	
	if (document.quickbook.children.value>0){
		
		if (child > 0) {
			agesTable.style.display = "block";
		} else {
			agesTable.style.display = "none";
		}
		
		for (j=1;j<=6;j++) {
			if (j <= child) {
				document.getElementById("childrow" + j).style.display = "block";
			} else {
				document.getElementById("childrow" + j).style.display = "none";
			}
		}

		return false;
	}
	
	bookVCUICheckChildren(child);
	
	return false;
	
}

function bookVCUICheckChildren(child){
	
	for (j=0;j<6;j++) {
		document.quickbook.childages[j].value='';
		if (j <= child-1) {
			document.quickbook.childages[j].value=document.getChildAgesForm.childAgesSelect[j].options[document.getChildAgesForm.childAgesSelect[j].selectedIndex].value;
		}
	}
	document.getElementById("childAgesForm").style.display = "none";
	document.quickbook.submit();
}

function showRoomsOption(strHotelID){

	if (strHotelID == 'Marriott') {
		document.getElementById('room1Container').style.display='block';
		document.getElementById('room2Container').style.display='block';
	}
	else {
		document.getElementById('room1Container').style.display='none';
		document.getElementById('room2Container').style.display='none';
	}

}

