// JavaScript Document
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function findWidth(obj){
	xPos = obj.offsetWidth;
	return xPos;
}

//Start of drop down script
var subMnus="";
function showMenu(idx,img){
	try{
		hideAllMenu();
		var dv=document.getElementById("sub"+idx);
		if(idx==5)
			dv.style.left=(findPosX(document.getElementById(img))+findWidth(document.getElementById(img)))-findWidth(dv)+"px";
		else
			dv.style.left=findPosX(document.getElementById(img))+"px";
		dv.style.top="48px";
		dv.style.display="inline";
	}
	catch(e){}
}

//This function is to hide the dropdown menu
function hideAllMenu(){
	try{
		var ar=subMnus.split(',');
		for(i=0;i<ar.length;i++){
			var dv=document.getElementById("sub"+ar[i]);
			dv.style.display="none";
		}
	}
	catch(e){}
}
//End of drop down script

// This function checks to see if a selection was made in a checkbox group
function cbxSelectionMade(cbx){
	var isGood=false;
	if(cbx.checked){
		isGood=true;
	}
	for(i=0;i<cbx.length && !isGood;i++){
		if(cbx[i].checked){
			isGood=true;
		}
	}
	return isGood;
}

// This function checks to see if a selection was made in a dropdown box
function cboSelectionMade(cbo){
	var isGood=false;
	
	if(cbo.options[cbo.selectedIndex].value!="")
		isGood=true;
		
	return isGood;
}

// This function is used to validate email entries
function isValidEmail(thisEmail){
	var emailexp = /.*\@.*\..*/;
	if (!emailexp.test(thisEmail)) {
		alert("The email you entered is not valid");
		return false;
	} 
	return true;
}

// function checks a date value to make sure its valid
function isValidDate(aDate) {
	var dateexp = /^(\d{1,2}\/\d{1,2}\/\d{4})$/;
    if (!dateexp.test(aDate)) { return false; } 	
	var	temp = new String(aDate), m = 0, d = 0;
	
	for (i=0; i < temp.length; i++){ 
		var str = temp.charAt(i); 
		if (str == "/") { if (m == 0) { m = i; } else { d = i; break; }}			
	}
	
	var month = parseInt(temp.substring(0, m), 10);
	var day = parseInt(temp.substring(m + 1, d), 10); 
	var year = parseInt(temp.substring(d + 1, temp.length), 10);								
					
	switch (month) {
		case 1:	case 3:	case 5:	case 7:	case 8:	case 10: case 12:					
			if ((day < 1) || (day > 31)) {  return false;	} break;
		case 2:		
			if ((year % 400 == 0) || (year % 4 == 0)) {	if ((day < 1) || (day > 29)) {  return false;	} break}
			if ((day < 1) || (day > 28)) {  return false; } break;
		case 4:	case 6:	case 9: case 11:
			if ((day < 1) || (day > 30)) {	 return false; } break;
		default: return false;			
	}		
	return true;		
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//This function is to validate feedback form
function validateFeedback(thisForm){
	if(!isValidEmail(thisForm.userEmailAddress.value)){
		alert("Please provide a valid email address.");	
		thisForm.userEmailAddress.focus()
		return false;
	}
	
	if(thisForm.userComments.value==""){
		alert("Please fill out your comments.");	
		thisForm.userComments.focus()
		return false;
	}
}

// This function is used to pop the passed calc
function popCalc(c){
	window.open(c,'','width=425,height=705,scrollbars=1');
}

// This function is used to pop up windows
function popUpa(c,w,h){
	window.open(c,'','width='+w+',height='+h+',scrollbars=1');
}


function validateNewFeed(){
	if(document.feedbackFORM.fbData.value == ""){
		alert("Please enter your comments");
		return false;
	}else{
		if(document.feedbackFORM.imageVerify.value == confirmtxt){
		return true;
		}else{
		alert("The number you have entered does not match the number in the image");
		return false;
		}
	}
}

function setupPBoverlay(){
	if(typeof getDocHeight == "function"){
		document.getElementById('divBg').style.width=getDocWidth()+'px';
		document.getElementById('divBg').style.height=getDocHeight()+'px';
		document.getElementById('divImgPlayer').style.width=getDocWidth()+'px';
		document.getElementById('divImgPlayer').style.height=getDocHeight()+'px';
	}
}


function writeHomeBuyingSwf(dvSwf)
{
	var str='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="940" height="356" id="mainImg" align="middle">';
	str+='<param name="allowScriptAccess" value="sameDomain" />';
	str+='<param name="menu" value="false" />';
	str+='<param name="wmode" value="transparent" />';
	str+='<param name="allowFullScreen" value="false" />';
	str+='<param name="movie" value="flash/homeBuying.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />';
	str+='<embed src="flash/homeBuying.swf" quality="high" bgcolor="#ffffff" width="940" height="356" name="mainImg" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" menu="false" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/>';
	str+='	</object>';
	document.getElementById(dvSwf).innerHTML=str;
}


function popWindow(wName){
	features = 'width=675, height=590, toolbar=no, location=no, directories=no, menubar=no, scrollbars=no, copyhistory=no, resizable=yes';
	pop = window.open('',wName,features);
	if(pop.focus){ pop.focus(); }
	return true;
}

function resetdrops(){
	var blnk = null;
	for(var idx = 1; idx <= 14; idx++){
		blnk = document.getElementById("hide"+idx);
		blnk.style.display = "inline"; 
	 }
	 var dv = document.getElementById('arrivalpop');
	dv.style.display = 'none';
	dv = document.getElementById('departpop');
	dv.style.display = 'none';
	dv = document.getElementById('arrivalpop2');
	dv.style.display = 'none';
	dv = document.getElementById('departpop2');
	dv.style.display = 'none';
}

function bleadgendiv(dvIdx){
	for(var i=1;i<3;i++){
		var dv=document.getElementById("dvLeadGen"+i);
		var tab=document.getElementById("dvTab"+i);
		if(dvIdx==i)
			dv.style.display='inline';
		else
			dv.style.display='none';
		if(dvIdx==i)
			tab.className='reserveTab';
		else
			tab.className='reserveTabOff';
	}
	resetdrops();
}

function bleadgendivi(dvIdx){
	for(var i=1;i<3;i++){
		var dv=document.getElementById("dvLeadGen"+i);
		var tab=document.getElementById("dvTab"+i);
		if(dvIdx==i)
			dv.style.display='inline';
		else
			dv.style.display='none';
		if(dvIdx==i)
			tab.className='reserveTabi';
		else
			tab.className='reserveTabOffi';
	}
	resetdrops();
}

function wsleadgendiv(dvIdx){
	for(var i=3;i<5;i++){
		var dv=document.getElementById("dvLeadGen"+i);
		var tab=document.getElementById("dvTab"+i);
		if(dvIdx==i)
			dv.style.display='inline';
		else
			dv.style.display='none';
		if(dvIdx==i)
			tab.className='reserveTab';
		else
			tab.className='reserveTabOff';
	}
	resetdrops();
}

function wsleadgendivi(dvIdx){
	for(var i=3;i<5;i++){
		var dv=document.getElementById("dvLeadGen"+i);
		var tab=document.getElementById("dvTab"+i);
		if(dvIdx==i)
			dv.style.display='inline';
		else
			dv.style.display='none';
		if(dvIdx==i)
			tab.className='reserveTabi';
		else
			tab.className='reserveTabOffi';
	}
	resetdrops();
}

function navleadgendiv(dvIdx){
	for(var i=10;i<12;i++){
		var dv=document.getElementById("dvLeadGen"+i);
		var tab=document.getElementById("dvTab"+i);
		var con=document.getElementById("reserveContainer");
		if(dvIdx==i)
			dv.style.display='inline';
		else
			dv.style.display='none';
		if(dvIdx==i)
			tab.className='reserveTab';
		else
			tab.className='reserveTabOff';
	}
	if(dvIdx==10){
		con.style.backgroundColor="#d3caba";
	}	
	else if(dvIdx==11){
		con.style.backgroundColor="#e2e2e2";
	}
	resetdrops();
}

function navleadgendivi(dvIdx){
	for(var i=10;i<12;i++){
		var dv=document.getElementById("dvLeadGen"+i);
		var tab=document.getElementById("dvTab"+i);
		var con=document.getElementById("reserveContainer");
		if(dvIdx==i)
			dv.style.display='inline';
		else
			dv.style.display='none';
		if(dvIdx==i)
			tab.className='reserveTabi';
		else
			tab.className='reserveTabOffi';
	}
	if(dvIdx==10){
		con.style.backgroundColor="#d3caba";
	}	
	else if(dvIdx==11){
		con.style.backgroundColor="#e2e2e2";
	}
	resetdrops();
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


function validateSuitesRequest(thisForm, wName){
	var d=new Date();
	//************************************************************
	//** Start check for arrival date being set to before today **
	//************************************************************
	if(parseInt(thisForm.arrivalYear.value)<parseInt(d.getFullYear())){
		alert("You must select at least the current year for your arrival.");
		thisForm.arrivalYear.focus();
		return false;
	}
	if(parseInt(thisForm.arrivalYear.value)==parseInt(d.getFullYear())){
		if(parseInt(thisForm.arrivalMonth.value)<parseInt(d.getMonth()+1)){
			alert("You must select at least the current month for your arrival.");
			thisForm.arrivalMonth.focus();
			return false;
		}
		if(parseInt(thisForm.arrivalMonth.value)==parseInt(d.getMonth()+1)){
			if(parseInt(thisForm.arrivalDate.value)<parseInt(d.getDate())){
				alert("You must select at least the current day for your arrival.");
				thisForm.arrivalDate.focus();
				return false;
			}
		}
	}
	//**********************************************************
	//** End check for arrival date being set to before today **
	//**********************************************************
	
	//*********************************************************************
	//** Start check for departure date being set to before arrival date **
	//*********************************************************************
	if(parseInt(thisForm.departureYear.value)<parseInt(thisForm.arrivalYear.value)){
		alert("You must select at least the same year as your arrival for your departure.");
		thisForm.departureYear.focus();
		return false;
	}
	if(parseInt(thisForm.departureYear.value)==parseInt(thisForm.arrivalYear.value)){
		if(parseInt(thisForm.departureMonth.value)<parseInt(thisForm.arrivalMonth.value)){
			alert("You must select at least the same month as your arrival for your departure.");
			thisForm.departureMonth.focus();
			return false;
		}
		if(parseInt(thisForm.departureMonth.value)==parseInt(thisForm.arrivalMonth.value)){
			if(parseInt(thisForm.departureDate.value)<parseInt(thisForm.arrivalDate.value)){
				alert("You must select at least the same day as your arrival for your departure.");
				thisForm.departureDate.focus();
				return false;
			}
		}
	}
	//*******************************************************************
	//** End check for departure date being set to before arrival date **
	//*******************************************************************
	
	return popWindow(wName);
}

function validateBayRequest(thisForm, wName){
	var d=new Date();
	//************************************************************
	//** Start check for arrival date being set to before today **
	//************************************************************
	if(parseInt(thisForm.arrivalYear.value)<parseInt(d.getFullYear())){
		alert("You must select at least the current year for your arrival.");
		thisForm.arrivalYear.focus();
		return false;
	}
	if(parseInt(thisForm.arrivalYear.value)==parseInt(d.getFullYear())){
		if(parseInt(thisForm.arrivalMonth.value)<parseInt(d.getMonth()+1)){
			alert("You must select at least the current month for your arrival.");
			thisForm.arrivalMonth.focus();
			return false;
		}
		if(parseInt(thisForm.arrivalMonth.value)==parseInt(d.getMonth()+1)){
			if(parseInt(thisForm.arrivalDate.value)<parseInt(d.getDate())){
				alert("You must select at least the current day for your arrival.");
				thisForm.arrivalDate.focus();
				return false;
			}
		}
	}
	if(parseInt(thisForm.arrivalMonth.value)<parseInt(4)){
		alert("You can select at the earliest April 29 for your arrival.");
		thisForm.arrivalMonth.focus();
		return false;
	}
	if(parseInt(thisForm.arrivalMonth.value)==parseInt(4)){
		if(parseInt(thisForm.arrivalDate.value)<parseInt(29)){
			alert("You can select at the earliest April 29 for your arrival.");
			thisForm.arrivalDate.focus();
			return false;
		}
	}
	//**********************************************************
	//** End check for arrival date being set to before today **
	//**********************************************************
	
	//*********************************************************************
	//** Start check for departure date being set to before arrival date **
	//*********************************************************************
	if(parseInt(thisForm.departureYear.value)<parseInt(thisForm.arrivalYear.value)){
		alert("You must select at least the same year as your arrival for your departure.");
		thisForm.departureYear.focus();
		return false;
	}
	if(parseInt(thisForm.departureYear.value)==parseInt(thisForm.arrivalYear.value)){
		if(parseInt(thisForm.departureMonth.value)<parseInt(thisForm.arrivalMonth.value)){
			alert("You must select at least the same month as your arrival for your departure.");
			thisForm.departureMonth.focus();
			return false;
		}
		if(parseInt(thisForm.departureMonth.value)==parseInt(thisForm.arrivalMonth.value)){
			if(parseInt(thisForm.departureDate.value)<parseInt(thisForm.arrivalDate.value)){
				alert("You must select at least the same day as your arrival for your departure.");
				thisForm.departureDate.focus();
				return false;
			}
		}
	}
	if(parseInt(thisForm.departureMonth.value)>parseInt(10)){
		alert("You can select at the latest October 11 for your departure.");
		thisForm.departureMonth.focus();
		return false;
	}
	if(parseInt(thisForm.departureMonth.value)==parseInt(10)){
		if(parseInt(thisForm.departureDate.value)>parseInt(11)){
			alert("You can select at the latest October 11 for your departure.");
			thisForm.departureDate.focus();
			return false;
		}
	}
	//*******************************************************************
	//** End check for departure date being set to before arrival date **
	//*******************************************************************
	
	return popWindow(wName);
}

function setBackground() {
	document.body.style.background = '#182012 url(images/BG.jpg) no-repeat top center fixed';
}



function resformloaded()
{
	$('.date-pick').datePicker();
	$('#arrivalDate').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#departureDate').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#departureDate').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#arrivalDate').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);
}

function loadBayshoreReq(){
	$('#btnResBayshore').attr('src','images/btnResBayshore_ON.gif');
	$('#btnResGlacier').attr('src','images/btnResGlacier.gif');
	$('#resrequest').load('tmpReqBayshore.html?rand=' + Math.random()*99999,function(){
		resformloaded();}
	);
}


function loadGlacierReq(){
	$('#btnResBayshore').attr('src','images/btnResBayshore.gif');
	$('#btnResGlacier').attr('src','images/btnResGlacier_ON.gif');
	$('#resrequest').load('tmpReqGlacier.html?rand='+ Math.random()*99999,function(){
		resformloaded();}
	);
}
