var domain_link = 'https://www.reservationpage.com';
var mychainid = 'C00227';
var myhotelid;     

/*****************************************************************/

function validate() {

	document.quickbook.arrival.value = document.quickbook.txt_outBound.value
	document.quickbook.departure.value = document.quickbook.txt_inBound.value
	
	//Check-in
	if (document.quickbook.arrival.value =="mm/dd/yyyy") { 
		alert('Missing Check-in date')
		return false; 
	};

	//Check-out
	if (document.quickbook.departure.value =="mm/dd/yyyy") { 
		alert('Missing Check-out date')
		return false; 
	};
	
	//Adults
	if (document.quickbook.adults.value =="") { 
		alert('Missing Number of Adults')
		return false; 
	};
	
	//Children
	if (document.quickbook.children.value =="") { 
		alert('Missing Number of Children')
		return false; 
	};
	
	date1 = new Date(document.quickbook.arrival.value);
	date2 = new Date(document.quickbook.departure.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.quickbook.checkcapacity!=undefined){
		if (checkDateCapacity(date1,date2,nights) == false)
		{
		return false;
		}
	}
	
	var lg;
	
	for (j=Number(document.quickbook.children.value)+1;j<=document.quickbook.maxnumofchildren.value;j++) {
		//alert(j);
		hideRow('childAge'+j);
		document.quickbook.childages[j-1].value = '';
	};
	if (document.quickbook.children.value>0){
		for (j=1;j<=document.quickbook.children.value;j++) {
			//alert(j);
			showRow('childAge'+j);
		};
		vcuiDisplayChildAgesContainer(document.getElementById('childAgesContaineriframe'));
		document.getElementById('childAgesContainer').style.marginTop = Number(document.quickbook.children.value)*(-24); 
		vcuiDisplayChildAgesContainer(document.getElementById('childAgesContainer'));
	}else{
		document.getElementById('childAgesContaineriframe').style.display = 'none';
		document.getElementById('childAgesContainer').style.display = 'none';
		//document.quickbook.submit()
		callFreeRide();
	};
	
	return false;
}

function vcuiSubmitVCUI(){
	var intNumberOfChildren = Number(document.quickbook.children.value);
	if (document.quickbook.children.value>0){
		for (j=1;j<=document.quickbook.children.value;j++) {
			document.quickbook.childages[j-1].value = document.getElementById('age'+j).value;
		};
	};
	//document.quickbook.submit()
	callFreeRide();
}

function callFreeRide(){
	
	//msg = "http://www.breckenridge.com/Plan-a-Trip/Lodging/Lodging-Explorer.aspx?Search="
	
	msg = "http://www.vail.com/plan-your-trip/lodging/Lodging-Explorer.aspx?Search="
	
	msg = msg + document.quickbook.arrival.value + ","
	
	msg = msg + document.quickbook.departure.value + ","

	msg = msg + document.quickbook.adults.value + ","
	
	msg = msg + document.quickbook.children.value
	
	if (document.quickbook.children.value>0){
		msg = msg + ","
		for (j=1;j<=document.quickbook.children.value;j++) {
			msg = msg + document.quickbook.childages[j-1].value;
			if (j<document.quickbook.children.value){
				msg = msg + "|";
			}
		};
	}
	
	window.open(msg);
}

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)

}

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();
}

