// get browser
var isNS = 0;
var isIE = 0;
if (navigator.appName.indexOf('Netscape') != -1){
	isNS = 1;
}
else if (navigator.appName.indexOf('Microsoft Internet Explorer') != -1){
	isIE = 1;
}

if(!Array.prototype.push) {
	Array.prototype.push = function(i) {
		this[this.length] = i;
	}
}

// MENU MOUSE OVER 
function menuOver(itemName) {
 clearTimeout(timeOn)
 menuActive = 1
}// MENU MOUSE OUT 
function menuOut(itemName) {
 if(document.layers) {
 	menuActive = 0 
 	timeOn = setTimeout("hideAllMenus()", 400)
  }
}
 // SET BACKGROUND COLOR 
function getImage(name) {
  if (document.layers) {
    return findImage(name, document);
  }
  return null;
}

function findImage(name, doc) {
  var i, img;
  for (i = 0; i < doc.images.length; i++)
    if (doc.images[i].name == name)
      return doc.images[i];
  for (i = 0; i < doc.layers.length; i++)
    if ((img = findImage(name, doc.layers[i].document)) != null) {
      img.container = doc.layers[i];
      return img;
    }
  return null;
}

function getImagePageLeft(img) {
  var x, obj;
  if (document.layers) {
    if (img.container != null)
      return img.container.pageX + img.x;
    else
      return img.x;
  }
  return -1;
}

function getImagePageTop(img) {
  var y, obj;
  if (document.layers) {
    if (img.container != null)
      return img.container.pageY + img.y;
    else
      return img.y;
  }
  return -1;
}

//document.write('<style> .menu{position: absolute;}</style>');
var timeOn = null
numMenus = 6;
document.onmouseover = hideAllMenus;
document.onclick = hideAllMenus;
window.onerror = null;

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	//we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility


function showMenu(menuNumber, eventObj, labelID) {
    hideAllMenus();
	if(document.layers) {
	img = getImage("img" + menuNumber);
 	x = getImagePageLeft(img);
 	y = getImagePageTop(img);
 	menuTop = y + 10; // LAYER TOP POSITION
	eval('document.layers["menu'+menuNumber+'"].top="'+menuTop+'"');
 	eval('document.layers["menu'+menuNumber+'"].left="'+x+'"');
	}
	eventObj.cancelBubble = true;
    var menuId = 'menu' + menuNumber;
    if(changeObjectVisibility(menuId, 'visible')) {
	return true;
    } else {
	return false;
    }
}

function hideAllMenus() {
    for(counter = 1; counter <= numMenus; counter++) {
	changeObjectVisibility('menu' + counter, 'hidden');
    }
}

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject



// ***********************
// hacks and workarounds *
// ***********************

// setup an event handler to hide popups for generic clicks on the document
function initializeHacks() {
	// this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}



function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
	window.event = false;
    }
} // createFakeEventObj



function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}

function mClk(src){ 
	if(event.srcElement.tagName=='TD')
		src.children.tags('A')[0].click();
}

//'a' is an object with property names that match the $variable name 
//to be replaced in the text.  The variable will be replaced with the 
//value of the object's property. Note: variables must have a "$"
function interpolatefunc(text, a) {
	var re = /{?\$([a-zA-Z]\w*)}?/;
	if(re.exec(text)){
		var propName = RegExp.$1;
		text = RegExp.leftContext + a[propName] + interpolatefunc(RegExp.rightContext, a);
	}
	return text;
}

function interpolate(text, a) {
	return interpolatefunc(text, a);
}
String.prototype.interpolate = function(obj) { return interpolatefunc(this, obj); }
//Object.prototype.interpolate = function(text) { return interpolatefunc(text, this); }

//trims whitespace from the left and right ends of the text
function trim(text) {
	if(text == undefined) return '';
	return text.replace(/^\s+/, '').replace(/\s+$/, '');
}

// preload dynamic images - only load appplicable images - create arrays to hold applicable images
function preloadImages(_images) {
	if (document.images){
		for(var i = 0; i < _images.length; i++) {
			var menu_image = new Image();
			menu_image.src = "img/" + _images[i];
		}	
	}
}

// toggle images on and off
function swapImage(_obj){
	var ua = navigator.userAgent;
	if(ua.indexOf('Netscape6') == -1){
		_obj = (typeof _obj == 'string') ? document.getElementById(_obj) : _obj;
		var state;
		var fn = _obj.src;
		if(fn.match(/(.*)_(\w+)(\.\w+)$/)) {
			var path = RegExp.$1;
			var cur_state = RegExp.$2;
			var ext = RegExp.$3;
			var new_state = (cur_state == 'OFF') ? 'ON' : 'OFF';

			_obj.src = path + '_' + new_state + ext;
		}
	}
}

function showPopupBlockerMsg() {
	var html = "<table width='100%' align='center' style='border: 3px double #999999;'><tr>\
<td width='10%' style='padding: 8px 8px 8px 8px' align='center'><img src='img/caution.gif' alt='System Message'></td>\
<td width='90%' style='padding: 3px 3px 3px 3px; color: #333333;'>A pop-up blocker has prevented a window from opening. Please temporary disable the pop-up blocking software or add LangNet to the list of web sites where pop-up windows are always allowed.\
<div align='right' style='margin-top: 4px'><input type='button' value=' Close ' onclick='hidePopupBlockerMsg()'></div>\
</td></tr>\
</table>";
	var frameHeight = window.innerHeight ? window.innerHeight : document.body.clientHeight;
	var frameWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;

	var div = document.getElementById('popup_block_msg');
	if(!div) {
		div = document.createElement('DIV');
		div.id = 'popup_block_msg';
		div.style.position = 'absolute';
		div.style.backgroundColor = '#FFFFFF';
		div.style.top = (frameHeight - 200) / 2 + document.body.scrollTop;
		div.style.left = (frameWidth - 450) / 2;
		div.style.width = 450;
		div.innerHTML = html;
		document.body.appendChild(div);
	}
	else {
		div.style.top = (frameHeight - 200) / 2 + document.body.scrollTop;
	}
}

function hidePopupBlockerMsg() {
	var div = document.getElementById('popup_block_msg');
	if(div) {
		document.body.removeChild(div);
	}
}

// open a popup window
function openwin(url, name, width, height){
	var win = window.open(url, name, 'scrollbars=yes,width='+width+',height='+height+',resizable=yes');
	if(!win) {
		showPopupBlockerMsg();
	}
}

//REMOVE LOCATION BAR AFTER TESTING IS FINISHED
function openLOwin(url, name, width, height){
	var win = window.open(url, name, 'scrollbars=yes,status=yes,location=yes,menubar=yes,width='+width+',height='+height+',resizable=yes');
	if(!win) {
		showPopupBlockerMsg();
	}
}

// toggle visibility of an element
function toggleDisplay(id, obj) {
	var el = document.getElementById(id);
	var new_state = (el.style.display != 'block') ? 'block' : 'none';	
	el.style.display = new_state;
	
	if(arguments.length > 1){			
		var suffix = (obj.src.indexOf("_ON") == -1) ? "_ON" : "_OFF";
		obj.src = "img/" + ((obj.name)?obj.name:obj.id) + suffix + ".gif";		
	}
}

// change background of buttons
function changeBackgrnd(_obj, _action){		
	if(_action == 'on'){				
		_obj.style.backgroundColor = "#ffcc66";
		if(window.navigator.userAgent.indexOf("MSIE") >= 0){
			_obj.style.cursor = "hand";
		}
		else{
			_obj.style.cursor = "pointer";
		}
	}
	else{
		_obj.style.backgroundColor = "#eeeeee";
	}
}

function SaveFormData(form_name, cookie_name) {
	var form = document.getElementById(form_name);
	var inputs = form.getElementsByTagName('INPUT');
	var fields = Array();
	var checked_items = Array();
	for(var i = 0; i < inputs.length; i++) {
		var input = inputs[i];
		if(input.type != 'checkbox' || input.checked) {
			fields.push(input.name + '=' + encodeURIComponent(input.value));
		}
	}
	var selects = form.getElementsByTagName('SELECT');
	for(var i = 0; i < selects.length; i++) {
		var select = selects[i];
		fields.push(select.name + '=' + encodeURIComponent(select.value));
	}
	document.cookie = cookie_name + '=' + fields.join('|');
}

function RestoreFormData(form_name, cookie_name) {
	var s = document.cookie;
	if(typeof(s) == 'string') {
		if(s.match(new RegExp( cookie_name + '=([^;]*)' ))) {
			var d = RegExp.$1;
			var fields = d.split('|');
			var values = Array();
			for(var i = 0; i < fields.length; i++) {
				if(fields[i].match(/(\w+)=(.+)/)) {
					values[RegExp.$1] = decodeURIComponent(RegExp.$2);
				}		
				else if(fields[i].match(/(\w+\[\])=(.+)/)) {
					if(!values[RegExp.$1]) {
						values[RegExp.$1] = Array();
					}
					values[RegExp.$1][decodeURIComponent(RegExp.$2)] = true;
				}
			}

			var form = document.getElementById(form_name);
			var inputs = form.getElementsByTagName('INPUT');
			var fields = Array();
			for(var i = 0; i < inputs.length; i++) {
				var input = inputs[i];
				var value = values[input.name];
				if(value) {
					if(input.type == 'checkbox') {
						if(value[input.value]) {
							input.checked = true;
						}
					}
					else {
						if(input.value == '') {
							input.value = value;
						}
					}
				}
			}
			var selects = form.getElementsByTagName('SELECT');
			for(var i = 0; i < selects.length; i++) {
				var select = selects[i];
				var value = values[select.name];
				if(value) {
					select.value = value;
				}
			}			
		}
	}
}

function inArray( _needle , _haystack ){
	for(var j=0;j<_haystack.length;j++){
		if(_haystack[j]==_needle)return true;
	}
	return false;
}

function SetElementTextDir(arg, text) {
	var element = (typeof(arg) == 'object') ? arg : document.getElementById(arg);
	if(typeof(text) != 'string') text = text + '';
	var dir = getTextDir(text);
	if(dir) {
		var align = (dir == 'rtl') ? 'right' : 'left';
		element.style.direction = dir;
		element.style.textAlign = align;
	}
}

window.addEvent('domready', function() {
	if (!window.console && window.loadFirebugConsole) {
		window.loadFirebugConsole();
	}/*debugger;*/
		if (window.console && !console.dir) {
			window.console.dir = objToString;
/*			window.Console.implement({
				dir: function(obj) {
					this.log(objToString(obj));
				}
			});*/
			//window.console = new window.Console();
		}
});
function objToString(obj, prefix) {
	if (!prefix) prefix = '';
	var str = '';
	for (var i in obj) {
		if (typeof(obj[i]) == 'function') continue;
		if (typeof(obj[i]) == 'object') {
			if (prefix.length < 3) {
				str += prefix + i + ": {\n" + objToString(obj[i], prefix+'	') + prefix + "}\n";
				continue;
			}
		}
		str += prefix + i + ':  ' + obj[i] + ",\n";
	}
	if (prefix == '') console.log(str);
	return str;
}