// Generic get object by id
function getObject(elmID) {
	if (document.getElementById) { elmID = document.getElementById(elmID); }
	else if (document.all) { elmID = document.all[elmID]; }
	else if (document.layers) { elmID = this._getLayer(elmID); }
	else if (document.forms) {
		if(document.forms[elmID]) { elmID = document.forms[elmID]; }
		else {
			for(var i=0; i<document.forms.length; i++) {
				if(document.forms[i][elmID]) {
					elmID = document.forms[i][elmID];
					break;
				}
			}
		}
	} else { elmID = null; }
	return elmID;
}

// Managing multiple object event
function addEvent(obj, evType, fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else {
		var originalEvent = obj["on" + evType];
		if (typeof obj["on" + evType] != "function") {
			obj["on" + evType] = fn;
		} else {
			obj["on" + evType] = function() {
				originalEvent();
				fn();
			}
		}
	}
}

// Image rollover and rollout functions
function imgOut(image) {
	ext = image.src.substr(image.src.lastIndexOf('.') + 1);
	image.src = image.src.replace(new RegExp('ov.' + ext + '$'), 'on.' + ext);
	image.style.filter = image.style.filter.replace(new RegExp('ov.png'), 'on.png');
}

function imgOver(image) {
	ext = image.src.substr(image.src.lastIndexOf('.') + 1);
	image.src = image.src.replace(new RegExp('on.' + ext + '$'), 'ov.' + ext);
	image.style.filter = image.style.filter.replace(new RegExp('on.png'), 'ov.png');
}

// Image changer
function changeImages() {
	if (document.images) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = eval(changeImages.arguments[i+1]).src;
		}
	}
}

// Generic image popup window
function imagePopUp(imageURL,imageTitle){
	var defaultWidth  = 400;
	var defaultHeight = 500;
	var autoClose = false;
	var imgWin = openPopUpCenter("about:blank", defaultWidth, defaultHeight, "scrollbars=no");
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(400,500);');
		writeln('width=400-(document.body.clientWidth-document.images[0].width);');
		writeln('height=500-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);');
		writeln('window.moveTo(screen.width/2-width/2, screen.height/2-height/2);}');
		writeln('if (isNN){');       
		writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;');
		writeln('window.moveTo(screen.width/2-window.innerWidth/2, screen.height/2-window.innerHeight/2);}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');
		writeln('</sc'+'ript>');
		if (!autoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
		close();
	}
}

function openPopUpCenter(pageUrl, width, height, feature) {
	openPopUpCenter(pageUrl, "Popup", width, height, feature);
}

function openPopUpCenter(pageUrl, windowName, width, height, feature) {
	feature += ",width=" + width + ",innerWidth=" + width;
	feature += ",height=" + height + ",innerHeight=" + height;
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		feature += ",left=" + xc + ",screenX=" + xc;
		feature += ",top=" + yc + ",screenY=" + yc;
	}
	return window.open(pageUrl, windowName, feature);
}

function centerWindow() {
	var ns = (navigator.appName == "Netscape") ? true : false;
	iWidth = (ns) ? window.innerWidth : document.body.clientWidth;
	iHeight = (ns) ? window.innerHeight : document.body.clientHeight;
	window.moveTo(screen.width/2-iWidth/2, screen.height/2-iHeight/2);
	self.focus();
}

function getWindowSize() {
	var windowSize = new Object();
	windowSize.width = 0;
	windowSize.height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowSize.width = window.innerWidth;
		windowSize.height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		windowSize.width = document.documentElement.clientWidth;
		windowSize.height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		windowSize.width = document.body.clientWidth;
		windowSize.height = document.body.clientHeight;
	}
	return windowSize;
}

function getObjectPosition(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		while (1) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			if (!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
		curtop += obj.y;
	}
	return {x:curleft, y:curtop};
}

function getObjectDimension(obj) {
	return { width:obj.offsetWidth, height:obj.offsetHeight }
}

// Hint popup
function prepareHints() {
	var windowSize = getWindowSize();
	var hasHint = false;
	
	var triggerList = [
		{tagName:"input", displayEvent:"focus", hideEvent:"blur"},
		{tagName:"select", displayEvent:"focus", hideEvent:"blur"},
		{tagName:"textarea", displayEvent:"focus", hideEvent:"blur"},
		{tagName:"img", displayEvent:"mouseover", hideEvent:"mouseout"},
		{tagName:"a", displayEvent:"mouseover", hideEvent:"mouseout"}
	];
	
	for(var i = 0; i < triggerList.length; i++) {
		var trigger = triggerList[i];
		var triggerElements = document.getElementsByTagName(trigger.tagName);
		for (var j = 0; j < triggerElements.length; j++){
			var triggerObj = triggerElements[j];
			var hintElements = triggerObj.parentNode.getElementsByTagName("span");
			for (var k = 0; k < hintElements.length; k++) {
				if (hintElements[k].className == "hint") {
					var hintObj = hintElements[k];
					// Add pointer
					var pointer = document.createElement("span");
					pointer.className = "hint_pointer";
					pointer.innerHTML = " ";
					hintObj.appendChild(pointer);
					
					// Check the position of the hint, make sure it's not over the window size, otherwise set to show the hint on the top
					hintObj.style.display = "inline";
					var hintPosition = getObjectPosition(hintObj);
					var hintDimension = getObjectDimension(hintObj);
					hintObj.style.display = "none";
					if (hintPosition.x + hintDimension.width > windowSize.width) {
						hintObj.className = "hint_offset";
						hintObj.style.marginTop = -(hintDimension.height + 4) + "px";
						hintObj.style.marginLeft = -(hintDimension.width + 35) + "px";
						pointer.style.top = hintDimension.height - 2 + "px";
						pointer.style.left = hintDimension.width + "px";
					}
	
					// Add the event
					triggerObj.hintObj = hintObj;
					addEvent(triggerObj, trigger.displayEvent, function(eventObj) {
						var thisObj = eventObj["srcElement"] ? eventObj["srcElement"] : eventObj["target"];
						thisObj.hintObj.style.display = "inline";
						
						// Fix for hover overlapping in IE 6 using i-frame
						var iframe = getObject("hint_mask");
						iframe.style.display = "block";
						iframe.style.width = thisObj.hintObj.offsetWidth;
						iframe.style.height = thisObj.hintObj.offsetHeight;
						iframe.style.left = thisObj.hintObj.offsetLeft;
						iframe.style.top = thisObj.hintObj.offsetTop;

					}, false);
					addEvent(triggerObj, trigger.hideEvent, function(eventObj) {
						var thisObj = eventObj["srcElement"] ? eventObj["srcElement"] : eventObj["target"];
						thisObj.hintObj.style.display = "none";
						
						var iframe = getObject("hint_mask");
						iframe.style.display = "none";
						
					}, false);
					
					hasHint = true;
				}
			}
		}
	}
	
	if (hasHint) {
		// Fix for hover overlapping in IE 6 using i-frame
		var hintMask = document.createElement("iframe");
		hintMask.id = "hint_mask";
		hintMask.scrolling = "no";
		hintMask.frameborder = "0";
		hintMask.width = "0";
		hintMask.height = "0";
		document.body.appendChild(hintMask);
	}
}
//addEvent(window, "load", prepareHints, false);

function toggleDisplay(showids, hideids, sender) {
	if (showids != null) {
		showids = showids.split(",");
		for (var i = 0; i < showids.length; i++)
		{
			id = showids[i];
			element = document.getElementById(id);
			if (element != null) {
				if (element.style.display != "block") {
					element.style.display = "block";
				} else {
					element.style.display = "none";
				}
			}
		}
	}
	if (hideids != null) {
		hideids = hideids.split(",");
		for (var i = 0; i < showids.length; i++)
		{
			id = hideids[i];
			element = document.getElementById(id);
			if (element != null) {
				element.style.display = "none";
			}
		}
	}
	if (sender != null) {
		if (endWith(sender.className, "_on")) {
			sender.className = sender.className.replace(/_on$/, "_off");
		} else if (endWith(sender.className, "_off")) {
			sender.className = sender.className.replace(/_off$/, "_on");
		}
	}
}

function startWith(strCheck, pattern) {
	return strCheck.indexOf(pattern) === 0;
}

function endWith(strCheck, pattern) {
	var d = strCheck.length - pattern.length;
	return d >= 0 && strCheck.lastIndexOf(pattern) === d;
}

function fade(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = getObject(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
	if (opacity >= 100) {
		object.filter = "";
	}
}

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

/*Auto Tab*/
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}
