var custom_onload_func_exists = false;

function loadMain() {
  if( custom_onload_func_exists )
  {
      custom_onload_func();
  }
} // loadMain()

function swap( img, isrc )
{
  if( !document.images ) return;
  document.images[img].src = isrc;
}

function popWindow( uri, width, height )
{
    var wnd = window.open( uri,
			   '_blank', 
			   'height=' + height + ',width=' + width + ',location=no,menubar=no,scrollbars=no,resizable=no,toolbar=no'
			   );
    wnd.focus();
} // popWindow

function popPrinterFriendly( uri, width, height )
{
    var wnd = window.open( uri,
			   '_blank', 
			   'height=' + height + ',width=' + width + ',location=no,menubar=yes,scrollbars=yes,resizable=no,toolbar=yes'
			   );
    wnd.focus();
} // popWindow


// this function shows/hides the text hints in form fields
function textHint( $frm, $element, $event, $text, $hint )
{
    $obj = document.forms[$frm].elements[$element];

    if( $obj )
    {
	if( $event == 'focus' )
	{
	    if( $obj.value == $text )
	    {
		$obj.value = '';
	    }
	    
	    if( $hint )
	    {
		window.status = $hint;
	    }
	}
	else if( $event == 'blur' )
	{
	    if( $obj.value == '' )
	    {
	    	$obj.value = $text;
	    }

	    if( $hint )
	    {
		window.status = '';
	    }
	}
    }
}

function updateStateDropdown( frm, strCountryList, strStateList )
{
    var divUS, divUK, divCA, divOther;
    var lstCountry, strCountryVal;
    
    // get the Country listbox object
    lstCountry = document.forms[frm].elements[strCountryList];

    // get the various DIVs to show/hide
    divUS = document.getElementById( 'contact_state_us' );
    divUK = document.getElementById( 'contact_state_uk' );
    divCA = document.getElementById( 'contact_state_ca' );
    divOther = document.getElementById( 'contact_state_other' );

    if( lstCountry )
    {
	// get the string value of the selected country
	strCountryVal = lstCountry.options[lstCountry.selectedIndex].value;

	// show/hide the appropriate DIVs
	if( strCountryVal == "USA" )
	{
	    divUS.style.display = 'block';
	    divUK.style.display = 'none';
	    divCA.style.display =  'none';
	    divOther.style.display = 'none';

	    //alert( 'Please choose your state from the State/Province list.' );
	}
	else if( strCountryVal == "IRELAND" || strCountryVal == "UNITED KINGDOM" )
	{
	    divUS.style.display = 'none';
	    divUK.style.display = 'block';
	    divCA.style.display = 'none';
	    divOther.style.display = 'none';

	    //alert( 'Please choose your county from the State/Province list.' );
	}
	else if( strCountryVal == "CANADA" )
	{
	    divUS.style.display = 'none';
	    divUK.style.display = 'none';
	    divCA.style.display = 'block';
	    divOther.style.display = 'none';

	    //alert( 'Please choose your province from the State/Province list.' );
	}
	else
	{
	    divUS.style.display = 'none';
	    divUK.style.display = 'none';
	    divCA.style.display = 'none';
	    divOther.style.display = 'block';

	    //alert( 'Please enter your state/province in the State/Province field.' );
	}
    }
} // updateStateDropdown()

function enlargeText()
{
    var activeStyleSheet, currentStyleSheet, newStyleSheet;

    activeStyleSheet = getActiveStyleSheet();
    if( !activeStyleSheet || activeStyleSheet.toLowerCase() == "default" )
    {
	newStyleSheet = "Large";
    }
    else if( activeStyleSheet == "Large" )
    {
	newStyleSheet = "Larger";
    }
    else if( activeStyleSheet == "Larger" )
    {
	newStyleSheet = "Largest";
    }
    else
    {
	newStyleSheet = "Largest";
    }
    
    if( newStyleSheet != activeStyleSheet )
    {
 	createCookie("style", newStyleSheet );
	// set the active stylesheet
	setActiveStyleSheet( newStyleSheet );
    }
} // enlargeText()

function reduceText()
{
    var activeStyleSheet, currentStyleSheet, newStyleSheet;

    activeStyleSheet = getActiveStyleSheet();
    if( activeStyleSheet == "Largest" )
    {
	newStyleSheet = "Larger";
    }
    else if( activeStyleSheet == "Larger" )
    {
	newStyleSheet = "Large";
    }
    else
    {
	newStyleSheet = "default";
    }
    
    if( newStyleSheet != activeStyleSheet )
    {
 	createCookie("style", newStyleSheet );
	// set the active stylesheet
	setActiveStyleSheet( newStyleSheet );
    }
} // reduceText()

// ********************************************************
// BEGIN: Enlarge/Reduce Text Functions
// Originally written by Paul Sowden and published at:
//	http://www.alistapart.com/stories/alternate/
//
// Updated and tweaked by Rubenstein Technology Group, more info at:
//	http://www.rubensteintech.com/

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title );
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

// END: Enlarge/Reduce Text Functions
// ********************************************************

function wm_goto( nRegion )
{
    var currUrl, lang;
    
    // determine the language
    currUrl = location.href;
    if( currUrl.indexOf( '/ar/' ) >= 0 )
    {
	lang = 'ar';
    }
    else if( currUrl.indexOf( '/fr/' ) >= 0 )
    {
	lang = 'fr';
    }
    else if( currUrl.indexOf( '/es/' ) >= 0 )
    {
	lang = 'es';
    }
    else // catch-all is English
    {
	lang = 'en';
    }

    // redirect to the region page in the appropriate language
    location.href = '/' + lang + '/where/region' + nRegion + '.html';
} // wm_goto()

function wm_newImage( arg )
{
    if( document.images )
    {
	rslt = new Image();
	rslt.src = arg;
	return rslt;
    }
} // wm_newImage

// changeImages (renamed to save download space)
function wm_cI()
{
    if( document.images && (wm_preloadFlag == true) )
    {
	for( var i = 0; i < wm_cI.arguments.length; i+=2 )
	{
	    document[wm_cI.arguments[i]].src = wm_cI.arguments[i+1];
	}
    }
} // wm_Ci()

var wm_preloadFlag = false;
function wm_preloadImages()
{
    if( document.images )
    {
	wm_r1_over = wm_newImage("/images/wm/r1_over.gif");
	wm_r2_over = wm_newImage("/images/wm/r2_over.gif");
	wm_r3_over = wm_newImage("/images/wm/r3_over.gif");
	wm_r4_over = wm_newImage("/images/wm/r4_over.gif");
	wm_r5_over = wm_newImage("/images/wm/r5_over.gif");

	wm_preloadFlag = true;
    }
} // wm_preloadImages()

// preload world map images
wm_preloadImages();
