// Captures the enter key press when a textbox is selected
// Used for the Site Search stuff.
function doClick(buttonName,e) {
    var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

    if (key == 13) {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null) { 
            //If we find the button click it
            btn.click();
            event.keyCode = 0;
        }
    }
}

function linkTo(url) {
  openExternalLink(url);
}

function openExternalLink(url) {
	window.open(url);
	// document.location = url;
}

// IE only.
// Provides MaxLength functionality for a TextArea
// by preventing further keypresses being registered once 
// the max length is exceeded.
function HandleTextAreaKeyPress(object, maxLength)
{
	var charCode = event.keyCode;

	// (Pressing enter will insert two characters)
	if(event.keyCode == 13)
		maxLength -= 1;
		
	if(object.value.length >= maxLength)
	{
		event.keyCode = 0;
		event.returnValue = false;
	}
}

// IE only.
// Provides MaxLength functionality for a TextArea 
// by preventing the user pasting in text which 
// would exceed the MaxLength.
function HandleTextAreaPaste(object, maxLength)
{
	// Get length of text in textarea
	var iTextLength = object.value.length;
	
	// Get length of text being pasted
	var iPasteLength = window.clipboardData.getData("Text").length;
	
	// Check if there are any characters selected
	var textRange = document.selection.createRange();
	
	// Get length of text the selected text
	var textSelectedSize = textRange.text.length;

	// If the size of the characters being pasted in is greater than the selected characters
	if(iPasteLength > textSelectedSize)
	{
		// and the size of the text would be too big after the paste
		if(((iPasteLength - textSelectedSize) + iTextLength) > maxLength)
		{
			// then stop the paste and replace the selected text with the number of characters allowed.
			var strValue =  window.clipboardData.getData("Text");
			
			strValue = strValue.substring(0, textSelectedSize + (maxLength - iTextLength));
			textRange.text = strValue;
			event.returnValue = false;
		}
	}
}


// All browsers.
// Provides MaxLength functionality for a TextArea (but not very well :)
function calcCharLeft(object,maxLength) 
{
	if (object.value.length > maxLength)
	{
		object.value = object.value.substring(0,maxLength);
		charleft = 0;
	}
	else 
	{
		charleft = maxLength - object.value.length;
	}
}


// Function to substitute for getElementById that works on older browsers
function findElementById(strItemName, objObject) {
  	var lngPosition,lngCounter,objRetVal;
   
	if (document.getElementById) {
  		objRetVal = document.getElementById(strItemName);
  	}
  	else {   
		if(!objObject) { objObject=document; }
		//alert(objObject.layers[0].name);
		
		//DEBUG alert("attempting to loop thru DOM");
		
		if((lngPosition=strItemName.indexOf("?"))>0&&parent.frames.length) {
		objObject=parent.frames[strItemName.substring(lngPosition+1)].document;
		strItemName=strItemName.substring(0,lngPosition);
		}
		
		if(!(objRetVal=objObject[strItemName])&&objObject.all) { 
		objRetVal=objObject.all[strItemName]; 
		}
		
		for (lngCounter=0;!objRetVal&&lngCounter<objObject.forms.length;lngCounter++) {
		objRetVal=objObject.forms[lngCounter][strItemName];
		}
		
		for(lngCounter=0;!objRetVal&&objObject.layers&&lngCounter<objObject.layers.length;lngCounter++) {
		objRetVal=findElementById(strItemName,objObject.layers[lngCounter].document);
		}
  	}

	return objRetVal;
}

// Function to allow you to access style properties of elements,
// without worrying which browser you are using
function accessElementStyle(strElementId){      //access a CSS property
	if(document.getElementById){ // NS6+ & IE6+ etc
        	return document.getElementById(strElementId).style;
      	} else if(document.all){ // IE4+
        	return document.all[strElementId].style;
	} else if(document.layers){ // NS4+
		return findElementById(strElementId);
	}
}

//window popup function
function openwin(theURL,winName,features) {
	window.open(theURL,winName,features);
}
	
function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
					((expires) ? "; expires=" + expires.toGMTString() : "") +
					((path) ? "; path=" + path : "") +
					((domain) ? "; domain=" + domain : "") +
					((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function SetPromptText(ID, Text)
// sets the element with the specified ID to have the specified prompt text;
// better than simply hardcoding the prompt text with a value="" attribute on
// that element, so that the text only appears in JavaScript-enabled browsers
// (where it magically disappears) and doesn't get in the way of those without
// JavaScript
{

      var Textbox = document.getElementById(ID);
      // Only set the prompt text if the box is currently empty; reloading the page
      // or moving back to it may mean there's already content in there, in which
      // case it should be left alone:
      if (Textbox.value == '')
        Textbox.value = Text;

      // If it currently contains a value that isn't the default text then just
      // leave it alone:
      else if (Textbox.value != Text)
        return;

      // Either the default text has just been set, or the page has been reloaded
      // with the default text in it carried over from last time.  In either case
      // set up an event handler that will clear the text as soon as the box is
      // focussed:
      Textbox.onfocus = function() { ClearPromptText(Textbox) };
}

function ClearPromptText(Textbox)
// clears the text from the specified textbox -- assumed to have been invoked
// from its onfocus event handler -- and disables the event handler so that
// this doesn't get invoked again and clear any text the user types
{
  Textbox.value = '';
  Textbox.onfocus = null;
}

/* addEvent function found at http://www.scottandrew.com/weblog/articles/cbs-events */
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn); 
		return r;
	} else {
		return false;
	}
}

/*
createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

function insertTop(obj) {
	// Create the two div elements needed for the top of the box
	d=createElement("div");
	d.className="bt"; // The outer div needs a class name
    d2=createElement("div");
    d.appendChild(d2);
	d2.innerHTML = ".";
	obj.insertBefore(d,obj.firstChild);
}

function insertBottom(obj) {
	// Create the two div elements needed for the bottom of the box
	d=createElement("div");
	d.className="bb"; // The outer div needs a class name
    d2=createElement("div");
	d2.innerHTML = ".";
    d.appendChild(d2);
	obj.appendChild(d);
}

function initCB()
{
	// Find all div elements
	var divs = document.getElementsByTagName('div');
	var arClass;
	var cbDivs = [];
	for (var i = 0; i < divs.length; i++) {
	// Allow for multiple class names
		arClass = divs[i].className.split(' ');
		for (var iCl = 0; iCl < arClass.length; iCl++) {
	// Find all div elements with cbb in their class attribute
			if (arClass[iCl] == 'divSPSearch')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divTheShopsMenu')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divList')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divInfoPanel')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divError')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divSPViewMap')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divSPVisitDate')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divSearchAgain')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divNextPage')
				cbDivs[cbDivs.length] = divs[i];
			if (arClass[iCl] == 'divASBox')
				cbDivs[cbDivs.length] = divs[i];
		}
	}
	// Loop through the found div elements
	for (var i = 0; i < cbDivs.length; i++) {
	// Save the original outer div for later
		var thediv = cbDivs[i];
	// 	Create a new div, give it the original div's class attribute, and replace 'cbb' with 'cb'
		var outer = createElement('div');
		outer.className = thediv.className;
		outer.className = thediv.className.replace('cbb', 'cb');
	// Change the original div's class name and replace it with the new div
		thediv.className = 'i3';
		thediv.parentNode.replaceChild(outer, thediv);
	// Create two new div elements and insert them into the outermost div
		var i1 = createElement('div');
		i1.className = 'i1';
		outer.appendChild(i1);
		var i2 = createElement('div');
		i2.className = 'i2';
		i1.appendChild(i2);
	// Insert the original div
		i2.appendChild(thediv);
	// Insert the top and bottom divs
		insertTop(outer);
		insertBottom(outer);
	}
}

if(document.getElementById && document.createTextNode)
{
	addEvent(window, 'load', initCB);
}