/**
 * @name: 		global.js
 * @author: 	Albert Harounian
 * @copyright: 	inManage LTD
 * @since:		August 1, 2007
 * @desc:		Global JS functions
 */

var dlgWin = null;

function obj(id,type,obj){
	switch(type){
		case "tag":
			if(obj.getElementsByTagName(id)){
				return obj.getElementsByTagName(id);
			}
			break;
		case "name":
			if(document.getElementsByName(id)){
				return document.getElementsByName(id)[0];
			}
			break;
		case "id":
		default:
			if(document.getElementById(id)){
				return document.getElementById(id);
			}
	}
}

function show(id){
	if(typeof id=='object'){
		id.style.display = 'block';
	}else{
		if(obj(id)){
			obj(id).style.display = 'block';
		}
	}
}

function hide(id){
	if(typeof id=='object'){
		id.style.display = 'none';
	}else{
		if(obj(id)){
			obj(id).style.display = 'none';
		}
	}
}

function switchDisplay(id){
	if(typeof id=='object'){
		id.style.display = id.style.display == '' ? 'none' : '';
	}else{
		obj(id).style.display = obj(id).style.display == '' ? 'none' : '';
		if ($('username') && obj(id).style.display == ''){
			$('username').focus();
		}
	}
}

function innerData(id, data, isAdding){
	if(typeof id=='object'){
		if(typeof(isAdding)=='undefined' || !isAdding){
			(id).innerHTML = data;
		}else if(isAdding){
			(id).innerHTML += data;
		}
	}else{
		if(typeof(isAdding)=='undefined' || !isAdding){
			obj(id).innerHTML = data;
		}else if(isAdding){
			obj(id).innerHTML += data;
		}
	}
}

function gotoURL(url){
	document.location.href = url;
}

function newWindow(mypage,myname,w,h,scroll,pos){
	if (typeof pos == 'undefined') pos='center';
	if (typeof scroll == 'undefined') scroll='auto';
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	if(dlgWin){dlgWin.close();}
	dlgWin=window.open(mypage,myname,settings);
}

function invertValue(inpObj, Event, txt){
	switch(Event){
		case "focus":
			if(inpObj.value==txt){
				inpObj.value='';
			}
			break;
		case "blur":
			if(inpObj.value==''){
				inpObj.value=txt;
			}
			break;
	}
}

function initTxtInputs(){
	var inps = obj("input","tag",document);
	for(i=0;i<inps.length;i++){
		if(inps[i].getAttribute("type")=='text'){
			if(inps[i].getAttribute("invertValue")=='yes'){
				inps[i].onfocus = function (){
					invertValue(this,"focus",this.defaultValue);
				}
				inps[i].onblur = function (){
					invertValue(this,"blur",this.defaultValue);
				}
			}
		}
	}
	var inps = obj("textarea","tag",document);
	for(i=0;i<inps.length;i++){
		if(inps[i].getAttribute("invertValue")=='yes'){
			inps[i].onfocus = function (){
				invertValue(this,"focus",this.defaultValue);
			}
			inps[i].onblur = function (){
				invertValue(this,"blur",this.defaultValue);
			}
		}
	}
}

function setObjOpacity(object,opacity){
	if(ie){
		if(typeof object=='object'){
			object.style.filter = "alpha(opacity="+opacity+")";
		}else{
			obj(object).style.filter = "alpha(opacity="+opacity+")";
		}
	}else{
		if(typeof object=='object'){
			object.style.opacity = opacity/100;
		}else{
			obj(object).style.opacity = opacity/100;
		}
	}
}

