var vpArrivalRE = /(\d+)[-\/](\d+)[-\/](\d+)/;

function disclaimerPage(page) {
  var winWidth = 450;
  var winHeight = 50;
  OpenWin = this.open(page, "photoWindow", "top=10,left=10,width="+winWidth+",height="+winHeight+",toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no");
  // Add the following to the <body> in every page that appears in the pop up window
  // onload="self.focus();"
} // end function disclaimerPage

function mapPage(page) {
  var winWidth = 600;
  var winHeight = 480;
  OpenWin = this.open(page, "mapWindow", "top=10,left=10,width="+winWidth+",height="+winHeight+",toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,status=no");
  // Add the following to the <body> in every page that appears in the pop up window
  // onload="self.focus();"
} // end function mapPage

function photoPage(page) {
  var winWidth = 630;		// Start with size for large images,
  var winHeight = 520;		// and it will be resized if a small gallery opens.
  OpenWin = this.open(page, "photoWindow", "top=10,left=10,width="+winWidth+",height="+winHeight+",toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,status=no");
  // Add the following to the <body> in every page that appears in the pop up window
  // onload="self.focus();"
} // end function photoPage

function actionTag( action ) {
	var qidx = action.indexOf( '?' );
	var path = action;
	if ( qidx >= 0 ) {
		path = action.substr( 0, qidx );
	}
	var tag = path.split( '/' ).pop();
	if ( path.lastIndexOf( '/' ) == path.length - 1 ) {
		tag = path.substr( 0, path.lastIndexOf( '/' ) ).split( '/' ).pop();
	}
	tag = tag.replace( /[^a-zA-Z0-9_]+/g, '_' );

	//alert( 'actionTag(): action = "' + action + '"; tag = "' + tag + '"' );

	return tag;
}

function popUp( action ) {
	var tag = actionTag( action );
	var height = 584;
	var width = 720;

	// Centre the pop-up within the parent window.
	var browser_top = window.screenY;
	var browser_left = window.screenX;
	var browser_height = window.innerHeight;
	var browser_width = window.innerWidth;
	var top = browser_top + Math.floor( ( browser_height - height ) / 2 );
	var left = browser_left + Math.floor( ( browser_width - width ) / 2 );

	var today = new Date;
	var popupName = "popUpAction_" + tag + today.getTime();

	var newWindow = window.open( action, popupName, "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width + ",toolbar=yes,menubar=yes,location=yes,status=yes,scrollbars=yes,resizable=yes" );
}

function adjustSideBar() {
	var spacerElem = document.getElementById( 'sideplaceholderspacer' );
	var sideBarElem = document.getElementById( 'sidecontent' );
	var styleStr = "line-height: " + sideBarElem.clientHeight + "px;";

	// This is how I want to do it:
//	spacerElem.setAttribute( "style", styleStr );
	// But stupid IE makes me do this (fortunately, Firefox and Konqueror both accept it):
	spacerElem.style.cssText = styleStr;
}

function gotoPage( pagename ) {
	document.location.href = '/' + pagename;
}

function gotoURL( url ) {
	document.location.href = url;
}

function galCheckOpener( redirURI ) {
	var redirect = true;
	if ( window.opener )
	{
		// Check path component of opener's location URL for "/.../b".
		var openerLocationPath = window.opener.location.pathname;
		if ( openerLocationPath.match( /\/[A-Za-z0-9_\-,]+\/b/ ) )
		{
			// Got a match--we're OK.
			redirect = false;
		}
	}

	// If there is no opener or it doesn't look like a property page, redirect to the property page.
	if ( redirect )
	{
		window.location.href = redirURI;
		return false;
	}
	else
	{
		return true;
	}
}

function vpSubmit() {
	// Get all the chunks of parameters (except 'p') into a single string.
	var frm = document.getElementById( 'vacationplanner' );
	var param_chunks = new Array();

	// This should be a for loop!  But the stupid language won't allow it.
	param_chunks[ param_chunks.length ] = 'a' + frm.elements['a'].value;
	param_chunks[ param_chunks.length ] = 'c' + frm.elements['c'].value;
	param_chunks[ param_chunks.length ] = 'd' + frm.elements['d'].value;
	param_chunks[ param_chunks.length ] = 'k' + frm.elements['k'].value;
	param_chunks[ param_chunks.length ] = 'l' + frm.elements['l'].value;
	param_chunks[ param_chunks.length ] = 'm' + frm.elements['m'].value;
	param_chunks[ param_chunks.length ] = 'n' + frm.elements['n'].value;
	param_chunks[ param_chunks.length ] = 'r' + frm.elements['r'].value;
	param_chunks[ param_chunks.length ] = 's' + frm.elements['s'].value;
	param_chunks[ param_chunks.length ] = 'y' + frm.elements['y'].value;
	param_chunks[ param_chunks.length ] = 't' + frm.elements['t'].value;

	var params_string = param_chunks.join( '-' );

	var page = frm.elements['p'].value ? frm.elements['p'].value : '1';
	var uri = '/' + params_string + '/p' + page + '_.html';
	window.location.href = uri;
}

function vpResortsChange( frm ) {
	frm.l.value='';
	frm.p.value='';
}

function noItIsNotOctalThankYouVeryMuch( str ) {
	return str.replace( /^0+/, '' );
}

function genericArrivalChange(  datestr, inputYear, inputMonth, inputDay ) {
	var matches = datestr.match( vpArrivalRE );
	inputMonth.value = matches[1];
	inputDay.value = matches[2];
	inputYear.value = matches[3];

	var intYear = parseInt( noItIsNotOctalThankYouVeryMuch( inputYear.value ) );
	if ( intYear < 100 )
	{
		// ...because people still like their 2-digit years.
		// See you again in 2100. ;-)
		intYear += 2000;
		inputYear.value = intYear.toString();
	}
}

function vpArrivalChange( datestr ) {
	var frm = document.getElementById( 'vacationplanner' );
	genericArrivalChange( datestr, frm.y, frm.m, frm.d );
}

// Set current page in the vacation planner, and submit.
function vpSetPage( iPage ) {
	var vp_form = document.getElementById( 'vacationplanner' );
	vp_form.p.value = iPage.toString();
	vpSubmit();
}

function update_sortreverse( ctlReverse ) {
	var vp_form = document.getElementById( 'vacationplanner' );
	var split_re = /./g;
	var splt = vp_form.s.value.match( split_re );
	if ( ctlReverse.checked ) {
		splt[0] = 'D';
	} else {
		splt[0] = 'A';
	}

	vp_form.s.value = splt.join( '' );
	vpSubmit();
}

function update_location( ctlLocation ) {
	var vp_form = document.getElementById( 'vacationplanner' );
	vp_form.p.value = '1';
	vp_form.l.value = ctlLocation.value;
	vpSubmit();
}

