// Form Helpers

function selected_value( ele )
{
	return ele.options[ ele.selectedIndex ].value;
}


// Debug function - Render out the values of the form into an alert IF the user has requested so on the query string (?debug=xxx)
function check_for_debug_info( frm )
{
	if ( location.search.length == 0 || location.search.indexOf( "debug=" ) == -1 )
	{
		return;
	}

	frm = frm || ( document.forms.length > 0 ? document.forms[0] : null );
	
	// If user has passed in the debug flag on the query string then write some goodies out
	if ( frm )
	{
		eleArray = new Array();

		for( var i=0; i < frm.elements.length; i++ )
		{
			var ele = frm.elements[i];
			eleArray.push( ele.name + ": \t" + ele.value );
		}

		alert( "Debug information for form: " + frm.id + ":\n\n" + eleArray.join( "\n\t" ) );
	}
}

	
// TODO: Phase these out
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_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_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];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function display(spanblock,condition,thisvalue){
	if (condition == "" || condition == "&nbsp;" || condition == thisvalue)
	{ MM_findObj(spanblock).className="hidden" }
}


function printformaton(){
	document.body.className = "brandPRINT";
}

function printformatoff(){
	document.body.className = "";
}


function setStyle(obj, style, value) {
	if (typeof obj == "string") { obj=MM_findObj(obj); }
	if (obj != null) {
		 obj.style[style] = value;
	}
}

function updateheader() {
	// Depreciated
}


// Adds the ref=someidentifier name value pair to the end of all hrefs for links, ignores javascript links
// ref_tag : the identifier to credit the link with
function addReferrerTag( ref_value, ref_tag )
{
	if ( !document.getElementsByTagName )
	{
		return;
	}
	
	ref_tag = ref_tag ? ref_tag : "ref";

	// Get all the links on the page and add the ?ref=[ref_tag] to the end of them
	links = document.getElementsByTagName( "a" );

	var link_total = links.length;

	for ( var i=0; i < link_total; i++ )
	{
		var link = links[i];
		if ( link.href.indexOf( ref_tag + "=" ) == -1 && link.href.indexOf( "javascript" ) == -1 && link.href.indexOf( "#" ) < 1 )
		{
			if ( link.href.indexOf( "?" ) > -1 )
			{
				link.href += "&" + ref_tag + "=" + ref_value;
			}
			else
			{
				link.href += "?" + ref_tag + "=" + ref_value;
			}
		}
	}
}


//////////////////////////////////////////////////////////////////////////////
// Generic cookie functions
//////////////////////////////////////////////////////////////////////////////
function setCookie(name, value, expires, path) {

	var thisCookie = new String(escape(name) + '=' + escape(value)),
		days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

	if (expires) {
		expires = new Date(expires);
		thisCookie += ';EXPIRES=' +
						days[expires.getUTCDay()] + ', ' +
						(expires.getUTCDate() < 10 ? '0' : '') + expires.getUTCDate() + '-' +
						months[expires.getUTCMonth()] + '-' +
						String(expires.getUTCFullYear()).substring(1) + ' ' +
						(expires.getUTCHours() < 10 ? '0' : '') + expires.getUTCHours() + ':' +
						(expires.getUTCMinutes() < 10 ? '0' : '') + expires.getUTCMinutes() + ':' +
						(expires.getUTCSeconds() < 10 ? '0' : '') + expires.getUTCSeconds() +
						' GMT';
	}

	if (path) {
		thisCookie += ';PATH=' + path;
	}

	document.cookie = thisCookie;
}


function getCookie(name) {

	var c = new String(document.cookie).split(/; /),
		p;

	for (var i = 0; i < c.length; i++) {
		p = c[i].split('=');
		if (p[0] == escape(name)) return unescape(String(p[1]).replace(/\+/g, ' '));
	}
	return '';
}


function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


// This shouldn't be used anywhere - these values don't need to be remembered any more
// It's left in because Anite have done some stuff that seems to require it, and it's causing JS errors
function storeformvalues() {

	/*
	formdefaults = getCookie("formdefaults");
	if (typeof formdefaults == "string"){
		defaultarray = formdefaults.split("|");

		if(defaultarray.length !=6){
			defaultarray = new Array("","","","","","");
		}
	}

	formdefaults = 	"";
	if (frm.AdultPax) { formdefaults += frm.AdultPax.selectedIndex + "|" } else {formdefaults += defaultarray[0]+"|"}
	if (frm.ChildPax) { formdefaults += frm.ChildPax.selectedIndex + "|" } else {formdefaults += defaultarray[1]+"|"}
	if (frm.InfantPax) { formdefaults += frm.InfantPax.selectedIndex + "|" } else {formdefaults += defaultarray[2]+"|"}
	if (frm.Day) { formdefaults += frm.Day.selectedIndex + "|" } else {formdefaults += defaultarray[3]+"|"}
	if (frm.Month) { formdefaults += frm.Month.selectedIndex + "|" } else {formdefaults += defaultarray[4]+"|"}
	if (frm.Duration) { formdefaults += frm.Duration.selectedIndex   } else {formdefaults += defaultarray[5]}

	setCookie("formdefaults",formdefaults,false,"/");
	*/
}