/* -----------------------------------------------------------------------
	Title:		Shared scripts
	Author:		Tavis Tucker, ttucker@esitemarketing.com
	Copyright:	E-site Marketing, LLC, http://www.esitemarketing.com
	Created:	03 May 2006
------------------------------------------------------------------------- */

/* Source : http://www.easy-designs.net/code/jsUtilities/ and http://www.embimedia.com/resources/labs/js-inarray.html */
Array.prototype.inArray = function(needle) {
	for (i = 0, j = this.length; i < j; i++) {
		if (this[i] === needle) {
			return i;
		}
	}
	return false;
};

/* Source : http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/ */
function getElementsByClassName(strClass, strTag, objContElm) {
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = (strTag == '*' && document.all) ? document.all : objContElm.getElementsByTagName(strTag);
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (i = 0, j = objColl.length; i < j; i++) {
	var arrObjClass = objColl[i].className.split(' ');
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:
		for (k = 0, l = arrObjClass.length; k < l; k++) {
			for (m = 0, n = arrClass.length; m < n; m++) {
				if (arrClass[m] == arrObjClass[k]) c++;
				if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}

// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value) {
  this[this.length] = value;
}

/* Source : http://prototype.conio.net/ */
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}

/* Source : "DOM Scripting: Web Design with Javascript and the Document Object Model" Copyright 2005 by Jeremy Keith */
function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	}
	else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

function previousSibling( elem ) {
	while( elem.previousSibling.nodeType != 1 ) {
		elem = elem.previousSibling;
	}
	return elem.previousSibling;
}

function createPopupLinks(linkClass, container) {
	container = $(container) || document;
	linkClass = linkClass || "link-popup";
	arrLinks = getElementsByClassName(linkClass, "a", container);
	for ( i = 0, j = arrLinks.length; i < j; i++ ) {
		arrLinks[i].onclick = function () {
			arrParams = this.getAttribute("rel").split(",");
			strOptions = "width="+arrParams[1]+",height="+arrParams[2];
			if ( arrParams.length > 3 ) {
				for ( k = 3, l = arrParams.length; k < l; k++ ) {
					strOptions += "," + arrParams[k];
				}
			}
			window.open(this.getAttribute("href"),arrParams[0],strOptions);
			return false;
		}
	}
}

function createLinks(linkClass, container, altText, target) {
	container = $(container) || document;
	altText = altText || "";
	target = target || "_self";
	arrLinks = getElementsByClassName(linkClass, "a", container);
	imageName = linkClass.split("-");
	imageName = "images/icon-" + imageName[1] + ".gif";
	for (i = 0, j = arrLinks.length; i < j; i++) {
		wrapper = document.createElement("span");
		wrapper.style.whiteSpace = "nowrap";
		newLink = arrLinks[i].cloneNode(true);
		newLink.setAttribute("target",target);
		icon = document.createElement("img");
		icon.setAttribute("src",imageName);
		icon.setAttribute("alt",altText);
		icon.style.marginLeft = "3px";
		dummy = document.createElement("span");
		dummy.style.display = "inline-block";
		wrapper.appendChild(newLink);
		insertAfter(icon,newLink);
		insertAfter(dummy,icon);
		arrLinks[i].parentNode.replaceChild(wrapper,arrLinks[i]);
	}
}

/* Source : http://simon.incutio.com/archive/2004/05/26/addLoadEvent */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if ( typeof window.onload != "function" ) {
		window.onload = func;
	} else {
		window.onload = function() {
		oldonload();
		func();
		}
	}
}

/* Source : http://www.brothercake.com/site/resources/scripts/domready/ and http://www.openjsan.org/doc/a/au/autarch/DOM/Ready/0.14/lib/DOM/Ready.html */
function onDomReady(func) {
	var t = setInterval( function() {
		if ( typeof document.getElementsByTagName != "undefined" && typeof document.getElementById != "undefined" && ( document.getElementsByTagName( "body" )[0] != null || document.body != null ) ) {
			if ( typeof func == "function" ) {
				func();
				clearInterval(t);
			}
		}
	}, 250 );
}

/* Source : http://sixtwothree.org/blog/archives/2006/05/20/activateactivex-11/ */
function activateActiveX() {
	if ( !document.getElementsByTagName || !document.body.outerHTML || !document.compatMode ) return false;
	var elems = new Array( "object", "applet" );
	for ( i = 0, j = elems.length; i < j; i++ ) {
		var objects = document.getElementsByTagName(elems[i]);
		for ( k = 0, l = objects.length; k < l; k++ ) {
			var params = "";
			for ( m = 0, n = objects[k].childNodes.length; m < n; m++ ) {
				params += objects[k].childNodes[m].outerHTML;
			}
			objects[k].outerHTML = objects[k].outerHTML.replace( "</" + elems[i].toUpperCase() + ">", params + "</" + elems[i].toUpperCase() + ">" );
		}
	}
}

function showHideDiv(show,hide) {

	document.getElementById(show).style.display="block";
	document.getElementById(hide).style.display="none";
	
	if(show == 'hotel_av_services') {
		document.getElementById('rfp-hotel-form').style.className="validate";
		document.getElementById('rfp-planner-form').style.className="";
	}
	
	if(show == 'meeting_planner_services') {
		document.getElementById('rfp-hotel-form').style.className="";
		document.getElementById('rfp-planner-form').style.className="validate";
	}
}

function init() {
	activateActiveX();
	createPopupLinks("link-popup","content");
	createLinks("link-pdf","content","PDF File - Requires the Adobe Acrobat Reader","_blank");
	createLinks("link-external","content","External link - will open in a new window","_blank");
	//createLinks("link-email","content","Send an email");
}
onDomReady(init);