function getBrowser(){
	d=document; 
	w=window; 
	if ((d.all) && (w.offscreenBuffering))
		return "ie";
	if ((d.captureEvents) && (!d.getElementById))
		return "ns";
	if ((d.getElementById) && (!d.all) && (d.documentElement))
		return "mz";
	if ((d.getElementById) && (navigator.userAgent.indexOf('Opera') != -1))
		return "op";
}
var browserStr=getBrowser();

//-----------------------------------------------------------------------------
function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

//-----------------------------------------------------------------------------
function setCookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + "=" + escape(value) +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "") + //expires.toGMTString()
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ((secure) ? ";secure" : "");
}

//-----------------------------------------------------------------------------
function deleteCookie(name, path, domain) {
    if (getCookie(name)) document.cookie = name + "=" +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//-----------------------------------------------------------------------------
function trim(str) {return str.replace(/^[ ]+|[ ]+$/ig,"");}

//-----------------------------------------------------------------------------
function retNull(e){
	if(browserStr != "mz"){
		window.event.returnValue=null;
		window.event.cancelBubble=true;
	}
	else{
		e.preventDefault();
		e.stopPropagation();
	}
	return false;
}
//-----------------------------------------------------------------------------
function getEventSource(e){
	if(browserStr != "mz")
		return e.srcElement; 
	else
		return e.target; 
}
//-----------------------------------------------------------------------------
function hideCtrl(ctrlId){
	ctrl=$get(ctrlId);
	if(ctrl!=null){
		ctrl.style.visibility='hidden';
	}
}
//-----------------------------------------------------------------------------
function showCtrl(ctrlId){
	ctrl=$get(ctrlId);
	if(ctrl!=null){
		ctrl.style.visibility='visible';
	}
}
//-----------------------------------------------------------------------------
function clickCtrl(e, ctrlId){
	ctrl=$get(ctrlId)
	if(ctrl != null){
		if(browserStr =="mz"){
			location.href=ctrl;
		}
		else{
			ctrl.click();
		}
	}
	
	return retNull(e);
}
//-----------------------------------------------------------------------------
function sbm(e, itemId){ //submit by enter
	var key;

	if (!e) var e=window.event; 
	key=e.keyCode || e.which;

	if(key == 13){
		var targetLink=$get(itemId); 
		if(targetLink.click){ // check function exists 
			targetLink.click();
			return retNull(e);
		} 
		else { 
			// if no, replace location 
			self.location=targetLink.getAttribute("href"); 
		} 
	} 
	return true;
}
//-----------------------------------------------------------------------------
function nm(e, itemId){ //Allow entering number only && submit by enter
	var key;
	if (!e) var e=window.event; 

	var srcElem=getEventSource(e);

	key=e.keyCode || e.which;
	if (key == 13){
		if (itemId && itemId != ""){
			if (srcElem != null && srcElem.value != null && srcElem.value != "0"){
				sbm(e, itemId);
				return retNull(e);
			}
			else{
				return retNull(e);
			}
		}
		else{
			return retNull(e);
		}
	}
	else{
		if (itemId && itemId != ""){
			if ((key >= 48 && key <= 57) || (key == 8 || key == 37 || key == 39)){
				return true;
			}
			else{
				return retNull(e);
			}
		}
		else{
			if ((key >= 48 && key <= 57) || (key == 8 || key == 37 || key == 39)){
				return true;
			}
			else{
				return retNull(e);
			}
		}
	}
}

//-----------------------------------------------------------------------------
function shM(ctrlID, isClose, timeVal){
	showCtrl(ctrlID);
	window.setTimeout("hideCtrl('" +  ctrlID + "')", timeVal);
	if(isClose == 1)
		window.setTimeout("window.close();", timeVal + 1000);
}


//-----------------------------------------------------------------------------
function goBack(e){
	if(history.length)
		history.back();
	retNull(e);
}
//-----------------------------------------------------------------------------
function wn(url){
	width=800;
	height=600;
	return openW(url, width, height);
}

//-----------------------------------------------------------------------------
function wn1(url){
	width=800;
	height=600;
	return openW(url, width, height);
}

//-----------------------------------------------------------------------------
function fb(url){
	width=600;
	height=580;
	return openW(url, width, height);
}

//-----------------------------------------------------------------------------
function openW(url, width, height) {
	var newWin=window.open(url, "", "menubar=no, status=no, resizable=no, width="+width+", height="+height+", scrollbars=auto");
	if (!newWin) return null;
	if (window.parent.frames.frmMain) newWin.opener=window.parent.frames.frmMain;
    
	newWin.moveTo((screen.availWidth - width) / 2, (screen.availHeight - height) / 2);

    retNull;
}
//-----------------------------------------------------------------------------
function setPopupPosition(ctrl, xVal, yVal){
	widthVal = parseInt(ctrl.clientWidth)
	heightVal = parseInt(ctrl.clientHeight);
	
	bottomVal = yVal + heightVal;
	rightVal =  xVal + widthVal;
		
	scrollX = document.documentElement.scrollLeft;
	scrollY = document.documentElement.scrollTop;
	windowWidth = document.documentElement.clientWidth;
	windowHeight = document.documentElement.clientHeight;
		
	ctrl.style.top = Math.min((windowHeight + scrollY - heightVal), (yVal + scrollY)) - 10 + "px";
	ctrl.style.left = Math.min((windowWidth + scrollX - widthVal), (xVal + scrollX)) - 5 + "px";
}
