//Destination Lighting scripts

/* 
Browser Detection Scripts
*****************************
	* Firefox returns "Netscape"
	* IEcompat is for general compatibility with IE
	* NScompa is for general compatibility with Netscape
*/
function getBrowser() {
	browser = new Object();
	browser.isIE5 = false;
	browser.isIE6 = false;
	browser.isNetscape = false;
	browser.isOpera = false;
	browser.isFirefox = false;	
	browser.IEcompat = false;
	browser.NScompat = false;
	browser.versionFound = 0;
	
	//IE
	version=0;
	if (navigator.appVersion.indexOf("MSIE")!=-1) {
		browser.versionFound++;
		temp=navigator.appVersion.split("MSIE");
		version = (parseFloat(temp[1]));
	}
	
	//NON IE browser will return 0
	if ( (version >= 5.5) && (version < 6)) {
		browser.versionFound++;
		browser.isIE5 = true;
		browser.IEcompat = true;
	} else if (version >= 6) {	
		browser.versionFound++;
		browser.isIE6 = true;
		browser.IEcompat = true;
	}
	
	//Firefox
	if (navigator.userAgent.indexOf("Firefox")!=-1) {
		var versionindex= (navigator.userAgent.indexOf("Firefox")+8);
		if ( parseInt(navigator.userAgent.charAt(versionindex))>=1 ) {
			browser.versionFound++;
			browser.isFirefox = true;
			browser.NScompat = true;
		}
	}	
	
	//Netscape
	if (navigator.userAgent.indexOf("Netscape")!=-1) {
		browser.isNetscape = true;
		var versionindex= (navigator.userAgent.indexOf("Firefox")+8);
		if ( parseInt(navigator.userAgent.charAt(versionindex))>=1 ) {
			browser.versionFound++;
			browser.NScompat = true;
			
		}
	}	
	
	//Opera
	if(navigator.userAgent.indexOf("Opera")!=-1){
		var versionindex=navigator.userAgent.indexOf("Opera")+6;
		if (parseInt(navigator.userAgent.charAt(versionindex))>=8) {
			browser.isOpera = true;
			browser.NScompat = false;
			browser.IEcompat = true;
			browser.isIE5 = false;
			browser.isIE6 = false;
			browser.versionFound++;
		}
	}
	
	return browser;
}

/*
popWin()
*****************************
Calls the popup window popup.htm.  At this point, it is just a placeholder.
Parameters: 
	(none)
*/
function popWin(target,title,width,height) {
	var w = window.open(target, "testwin", "width=" + width + ",height=" + height + ",status=yes,resizable=yes");
}

function popWin1(target,title,width,height) {
	var w = window.open(target, "testwin", "width=" + width + ",height=" + height + ",status=yes,resizable=yes");
}
		
//Loads the zoomify window
function popZoom(prodCode) {
	var w = window.open("/zoomwindow.jhtml?code=" + prodCode,"zoom", "width=565,height=485,status=yes,resizable=yes");
}


/*
swapMenu functions
*****************************
*/
function swapMenuImgOver(imgObj,imgState,imgRoot,urlRoot) {
	if (imgState == "rest") {
		if(imgObj!=null && urlRoot!=null && imgRoot!=null){
			imgObj.src = urlRoot + "/template_imgs/_framework/menu_" + imgRoot + "_over.gif";
		}
	} 
}


function swapMenuImgOut(imgObj,imgState,imgRoot,urlRoot) {
	if (imgState == "rest") {
		if(imgObj!=null && urlRoot!=null && imgRoot!=null){
			imgObj.src = urlRoot + "/template_imgs/_framework/menu_" + imgRoot + "_rest.gif";
		}
	} 
}

/**
 * swap source of an image. Try first by ID, then if not, by name (for IE compatibility)
 * this function was originally selecting the object by name. 
 * This method was left for backward compatibility 
 * @author Mike Smith
 * @param {String} imgName
 * @param {String} target
 */
function swapImg(imgName,target) {
	var imgObj = eval("document." + imgName); // legacy method to select object
	if (imgObj == null) 
		imgObj = document.getElementById(imgName);
	imgObj.src = target;
}

/**
 * 
 * @param {String} objId ID selector for the HTML element
 * @param {String} background CSS background command
 */
function swapBackground(objId, background) {
//	alert("swapBackground: " + objId + " with " + background);
	
	var obj = document.getElementById(objId);
	alert(obj);
	obj.style.background = background;
}

/**
 * checks to see if an object is of a specific class (allows multiple classes)
 * @param {Object} objId 
 * @param {Object} className
 * @author Mike Smith
 */
function isInClass(objId, className)
{
//	alert("checking if "+className+" is set");
	var obj = document.getElementById(objId);
	// IE fix: IE uses 'className' instead of 'class'
	var attribName = (document.all ? 'className' : 'class');
	var attribClass = obj.getAttribute(attribName);
//	alert(attribClass);
	var exists = false;
	if(attribClass != null)
	{
		var classes = attribClass.split(' ');
		for (var i = 0; i<classes.length; i++)
		{
			if(classes[i] == className)
				exists = true;
		}
	}
	return exists;
}

/**
 * helper function to dynamically change the appearance of an object 
 * using the class attribute (typically for 'mouseover' event)
 * IE issue for className (vs. class) is handled
 * @param {Object} objId HTML id of the desired object
 * @param {Object} className name of CSS class
 * @author Mike Smith
 */
function addClass (objId, className)
{
//	alert("objId=" + objId + ", className=" + className);
	var obj = document.getElementById(objId);
	// IE fix: IE uses 'className' instead of 'class'
	var attribName = (document.all ? 'className' : 'class');
//	alert(attribName);
	var attribClass = obj.getAttribute(attribName);
	var classes = [];
	// class attribute not defined?
	if(attribClass == null)
	{
//		alert('before: empty');
		obj.setAttribute(attribName, className);
//		alert("after: " + obj.getAttribute(attribName));
	}
	// already has defined class(es)
	else
	{
//		alert('before: ' + attribClass);
		classes = attribClass.split(' ');
		var exists = false;
		for (var i = 0; i<classes.length; i++)
		{
			if(classes[i] == className)
				exists = true;
		}
		if (!exists)
		{
			obj.setAttribute(attribName, obj.getAttribute(attribName) + ' ' + className);
//			alert("after: " + obj.getAttribute(attribName));
		}
	}
}

/**
 * helper function to dynamically change the appearance of an object 
 * using the class attribute (typically for 'mouseout' event)
 * IE issue for className (vs. class) is handled
 * @param {Object} objId
 * @param {Object} className
 * @author Mike Smith
 */
function removeClass (objId, className)
{
//	alert("objId=" + objId + ", className=" + className);
	var obj = document.getElementById(objId);
	// IE fix: IE uses 'className' instead of 'class'
	var attribName = (document.all ? 'className' : 'class');
	var attribClass = obj.getAttribute(attribName);
	var classes = [];
	// class attribute not defined?
	if(attribClass != null)
	{
//		alert("before: " + obj.getAttribute(attribName));
		classes = attribClass.split(' ');
		attribClass = '';
		for (var i = 0; i<classes.length; i++)
		{
			if(classes[i] != className)
				attribClass = ' ' + classes[i];
		}
		attribClass = attribClass.substr(1, attribClass.length - 1);
		obj.setAttribute(attribName, attribClass);
//		alert("after: " + obj.getAttribute(attribName));
	}
}


/*
swapIfContents()
*****************************
	- params
		newTarg: link to new HTML file
*/
function swapIfContents(newTarg) {
	var targFrame = window.frames[0];
	if ( targFrame ) {
	    targFrame.location = newTarg;   
	}
}

function Print()
{
	document.body.offsetHeight;
	window.print();
}

/*
checkSubmit()
******************************


*/

/*
* automatically submit a form when the enter key is pressed
* place this snippet of code in any HTML input tag where you want to submit with enter key
* onkeypress="keySubmit(event.which, 'loginform');
* note: 'loginform' happens to be an example name of the form
*/
function getKeyPressed (the_event)
{
	var the_key = the_event.which;
	if ( ! the_key )
	{
		the_key = the_event.keyCode;
	}
	return the_key;	
}
function keySubmit(the_event, the_form) 
{ 
	var the_key = getKeyPressed(the_event);
	if (the_key == 13)
		document.forms[the_form].submit();
//	String.fromCharCode ( the_key )	can be used to show the actual char (if not whitespace)
}

/*
 * toggle between expanding or contracting a div element (show/hide)
 */
function expand(Id, focusId) 
{
	styleObj = document.getElementById(Id).style;
	if (styleObj.display == 'none') 
	{
		styleObj.display='';
		if (focusId != null)
		{
			document.getElementById(focusId).focus();
		}
	}
	else 
	{
		styleObj.display='none';
	}
}

/*
 * integer validation
 */
function isDigit(c)
{
	return ((c >= "0") && (c <= "9"));
}
function isInteger(s)
{
	var i, c;
	var returnVal = true;
	
	if (isEmpty(i))
	{
		returnVal = false;
	}
	else
	{
		for (i=0; i<s.length; i++)
		{
			// check each character
			c = s.charAt(i);
			if (! isDigit(c))
				returnVal = false;
		}
	}
	return returnVal;
}
/**
 * event management
 * @param {Object} obj object in which the function will be hooked
 * @param {Object} evType trigger event (without the "on" prefix)
 * @param {Object} fn what to do when the event occurs
 * @param {Object} useCapture set to false
 * @author http://www.scottandrew.com/weblog/articles/cbs-events
 */
/*
function addEvent(obj, evType, fn, useCapture)
{ 	
	if (obj == null) 
		alert ("Object null in addEvent()");
	if (obj.addEventListener)
	{
alert('addingEvent: '+ evType);
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} 
	else 
	{
alert('attachingEvent: ' + evType);		
alert('function: '+ fn);
		if (obj.attachEvent)
		{
			var r = obj.attachEvent("on"+evType, fn);
alert(r);			
			return r;
		} 
		else 
		{
			alert("Handler could not be attached in addEvent()");
		}
	}		
}
*/

/**
 * event management
 * http://www.scottandrew.com/weblog/articles/cbs-events
 * Free usage permitted as long as this credit notice remains intact.
 * AddEvent Manager (c) 2005-2006 Angus Turnbull http://www.twinhelix.com
 * @param {Object} o object in which the function will be hooked
 * @param {Object} t trigger event (without the "on" prefix)
 * @param {Object} f function...what to do when the event occurs
 * @param {Object} c use capture (set to false)
 * @author Angus Turnbull
 * @author Mike Smith (light editing)
 */
if (typeof addEvent != 'function')
{
	var addEvent = function(o, t, f, l)
	{
		if (o == null) 
			alert ("Object null in addEvent()");
		var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
		if (o[d] && !l) return o[d](t, f, false);
		if (!o._evts) o._evts = {};
		if (!o._evts[t])
		{
			o._evts[t] = o[n] ? { b: o[n] } : {};
			o[n] = new Function(
				'e',
				'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
				'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
				'} return r'
			);
			if (t != 'unload') 
				addEvent(window, 'unload', function() {	removeEvent(rO, rT, rF, rL); });
		}
		if (!f._i) f._i = addEvent._i++;
		o._evts[t][f._i] = f;
	};
	addEvent._i = 1;
	var removeEvent = function(o, t, f, l)
	{
		var d = 'removeEventListener';
		if (o[d] && !l) return o[d](t, f, false);
		if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
	};
}

/**
* unbtrusive rollover events management for multiple tags within a container
* designed for CSS rollover effects using attributes color, background, etc.
* can also selectively choose tags that match a certain class (future feature)
* uses reflection to find objects
* automatically adds unique IDs if not already specified in markup
* @param {String} elementId
* @param {String} tagName
* @param {String} className (future feature, not implemented yet)
* @author Mike Smith
*/
function setRollovers(elementId, tagName, className) 
{ 
	// get a collection of all the [tagName] tags in the navigation element
	var navs = document.getElementById(elementId).getElementsByTagName(tagName);
	var attribName = (document.all ? 'className' : 'class'); // IE fix
	var body;
	var ac = [];
	var rc = [];
	for (var i=0; i<navs.length; i++)
	{
//alert(i);
// future functionality to allow undefined class or matching class
//		if((className == undefined) || (navs[i].eval(attribName) == className)) alert("match found on element " + i);

		// setup unique ids for all elements
		if (navs[i].id == "")
			navs[i].id = elementId + '_' + i;

		// setup event handlers
		body = "addClass('" + navs[i].id + "','over');";
		ac[i] = new Function(body);
//alert(ac[i]);
//alert(navs[i]);
		addEvent(navs[i], 'mouseover', ac[i], false);
//alert(navs[i]);		
		body = "removeClass('" + navs[i].id + "','over');";
//alert(body);
		rc[i] = new Function(body);
//alert(rc[i]);
		addEvent(navs[i], 'mouseout', rc[i], false);
	}
}

/**
 * Attaches a swapImg function on an src-compatible tag to mouseover and mouseout events on a trigger object
 * The trigger object and the src-compatible object can be the same (i.e. triggerElementId == swapImgId)
 * @param {String} triggerElementId object that causes the {object}.src to change by mouse over/out events
 * @param {String} swapImgId object whose src will change
 * @param {String} swapImgUrl URL of src for resting state
 * @param {String} swapImgUrlOver URL of src for mouse over state
 * @author Mike Smith
 */
function setImgRollover(triggerElementId, swapImgId, swapImgUrl, swapImgUrlOver)
{
	var obj = document.getElementById(triggerElementId);
	if (obj != null) {
		addEvent(obj, 'mouseover', function(){
			var test = isInClass(swapImgId, 'over');
			//			alert("over: "+this.id+", isInClass="+test); 
			if (!test) {
				//				alert('not over...swap and set');
				swapImg(swapImgId, swapImgUrlOver);
				addClass(swapImgId, 'over');
			}
		}, false);
		addEvent(obj, 'mouseout', function(){
			if (isInClass(swapImgId, 'over')) {
				swapImg(swapImgId, swapImgUrl);
				removeClass(swapImgId, 'over');
			}
		}, false);
	}
}