/*
return element object by Id
@param string, object Id
@return element object
*/
function elId(id){return document.getElementById(id);}

/*
return element object by name
@param string, object name
@return element object
*/
function elName(name){return document.getElementsByName(name);}

/**
 * trim value
 * @param string strToTrim. value to be trim.
 * @return string
 */
function trim(strToTrim)
{
	return strToTrim.replace(/^\s+|\s+$/g,"");
}
function changeBox(divTip, divField, txtField) 
{
  setObjVisibility(divTip, 'false');//elId(divTip).style.display='none';
  setObjVisibility(divField, 'true');//elId(divField).style.display='';
  focusCtrl(elId(txtField));
}
function focusCtrl(ctrl)
{
    //ctrl.focus();
    try {
        // focus_form_field() is located at Static/common/core/common.js
        if(ctrl.id) {focus_form_field(ctrl.id);} else {focus_form_field(ctrl.name);}
    } catch (e) {
        //do nothing
    }
}
function restoreBox(divTip, divField, txtField) 
{
	if(elId(txtField).value=='')
	{
	  setObjVisibility(divTip, 'true');//elId(divTip).style.display='';
	  setObjVisibility(divField, 'false');//elId(divField).style.display='none';
	} else {
		changeBox(divTip, divField, txtField);
	}
}
/**
 * set object visibility
 * @param string strObjName. object name.
 * @param boolean blnShow. true=visible | false=collapse
 */
function setObjVisibility(strObjName, blnShow)
{	
	var obj = elId(strObjName);
	if(obj == null) return;

	var strVisibleValue ;
//	if(blnShow == 'true') strVisibleValue = 'visible';
//	else strVisibleValue = 'collapse';
//	obj.style.visibility = strVisibleValue;
	 
	if(blnShow == 'true')
	{
		obj.style.display = '';
		obj.style.visibility = 'visible';		
	}
	else
	{
		obj.style.display = 'none';
	}
}

function setDivClass(sObj, sCssClass)
{
	var obj = elId(sObj);
	if(obj == null) return;
	obj.className=sCssClass;
}
/**
 * set object Div msg
 * @param string strObjName. regex.
 * @param string strMsg. msg
 * @param string strClassName. css class name
 */
function setDivMsg(strObjName, strMsg, strClassName)
{
	var obj = elId(strObjName);
	if(obj == null) return;
	if(typeof(strClassName) != 'undefined' && strClassName.length > 0) 
	{
		strMsg = "<span class='" + strClassName +"'>" + strMsg + "</span>";
	}
	obj.innerHTML  = strMsg;
}

/*
* Do submit
*/
function doSubmit(objCtrl)
{
	objCtrl.form.submit();
}

/*
* Do Client site language translation
*/
function lang(strKey)
{
	var translation = eval(strKey);
	if(translation == '') return strKey;
	return translation;
}

function escapeHTML (str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}

/*
* Include a js file in a js file. same as includeJs2 
*/
function includeJs(strUrl)
{
	var str = '<script type="text/javascript" src="' + strUrl + '"></script>';
	document.write(str); 
}

/*
* Include a js file in a js file. same as includeJs 
*/
function includeJs2(strUrl)
{
	var body = document.getElementsByTagName('body').item(0);
	script = document.createElement('script');
	script.src = strUrl;
	script.type = 'text/javascript';
	body.appendChild(script);
}

function popWin(url, h, w) 
{
	if(typeof(h) == 'undefined') h = 500;
	if(typeof(w) == 'undefined') w = 700;
	w=window.open(url, "W958b6e9ea6bed1f9c5288b15959dad77", "left=150,top=120,menubar=no,height="+h+",width="+w+",scrollbars=yes,resizable=yes");
	w.focus();
} 

function popup_win( loc, wd, hg ) {
   var remote = null;
   var xpos = screen.availWidth/2 - wd/2; 
   var ypos = screen.availHeight/2 - hg/2; 
   remote = window.open('','','width=' + wd + ',height=' + hg + ',resizable=1,scrollbars=1,screenX=0,screenY=0,top='+ypos+',left='+xpos);
   if (remote != null) {
      if (remote.opener == null) {
         remote.opener = self;
      }
      remote.location.href = loc;
      remote.focus();
   } 
   else { 
      self.close(); 
   }
}

//popup window with popup blocker check
function popup_win7( loc, wd, hg, msg) {
    var remote = null;
    var xpos = screen.availWidth/2 - wd/2; 
    var ypos = screen.availHeight/2 - hg/2; 
    remote = window.open(loc,'','width=' + wd + ',height=' + hg + ',resizable=0,scrollbars=1,screenX=0,screenY=0,top='+ypos+',left='+xpos);
	try{
		remote.focus();
	} 
	catch(e){ 
		//alert('The page you clicked could not be opened!\nDo you have any pop-up blocker installed?\n\nIf so, please disable the pop-up blocker or \nadd this site to the "allow pop-up" list.');
		alert(msg);
	}
}

function setFixedPosition(id,x,y){
    var sty=elId(id).style;
    sty.left=x + 'px';
    sty.top=y + 'px';
} 

function centerDiv(E){
  var A = elId(E);
  if (IE) {
    C = document.documentElement.clientWidth;
    D = document.documentElement.clientHeight;
  } else {
    C = window.innerWidth;
    D = window.innerHeight;
  }
  //must display block first to have clientWidth and clientHeight
  A.style.display = "block";
  var elementWidth = A.clientWidth;
  var elementHeight = A.clientHeight;
  A.style.top = String(Math.round((D-elementHeight)/2))+"px";
  A.style.left = String(Math.round((C-elementWidth)/2))+"px";
}

//to display a popup that does not pop off the edge of the screen if the link is toward the edge, make sure cross-browser compatible
function setPosition(id,x,y){
    var sty=elId(id).style;
    
    //to get browser's page view screen size
    getScreenAvailHeight = screen.availHeight;
    getScreenAvailWidth = screen.availWidth;
   
    //to get Pop-up window size
    getPopupHeight = elId(id).offsetHeight;
    getPopupWidth = elId(id).offsetWidth;
	
	//Microsoft's DOM (IE, Opera and Konqeuror)
 	if (IE) {
        getScrollTop = document.documentElement.scrollTop;
 		if ((tempAvailY + y + getPopupHeight) > getScreenAvailHeight) {
            topY = tempY - getPopupHeight;
		  	if (topY < getScrollTop) { 
		  	   topY = topY - getScrollTop - 30;
		  	   //alert("tempAvailY (mouse) + y + getPopupHeight > getScreenAvailHeight"+"<br/>" + tempAvailY + "+" + y + "+" + getPopupHeight + ">" + tempY);
            } 	
	  	} 	else {
	  		topY = tempY + y;
	  		//alert("tempAvailY (mouse) + y + getPopupHeight > getScreenAvailHeight"+"<br/>" + tempAvailY + "+" + y + "+" + getPopupHeight + ">" + getScreenAvailHeight);
	  	}
        	  	
	} 
	//W3C DOM  (Mozilla, Netscape and Epiphany)
  	else {
        getScrollTop = window.pageYOffset;
  		if 	(tempAvailY + 110 + y + getPopupHeight > tempY)	{
	  		topY = tempY - getPopupHeight;
	  		if (topY < getScrollTop) {	
	  			topY = topY - getScrollTop - 30; 
	  			//alert("tempAvailY (mouse) + y + getPopupHeight > getScreenAvailHeight"+"<br/>" + tempAvailY + "+" + y + "+" + getPopupHeight + ">" + getScreenAvailHeight);
            }
	  	}   else {
	  		topY = tempY + y;
	  		//alert("tempAvailY (mouse) + y + getPopupHeight > getScreenAvailHeight"+"<br/>" + tempAvailY + "+" + y + "+" + getPopupHeight + ">" + getScreenAvailHeight);
	  	}
	}
  	
  	if (topY < 0) {
		topY = 0 + getScrollTop; 
  	}
  	
  	sty.top = topY + 'px';
  	sty.left = tempX + x + 'px';
}

function getMouseXY(e) 
{	
	if (window.event) 
	{		
		//mouse position within the web page you would simply add the scroll position of the page within the browser window
		tempX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		tempY = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		
		tempAvailX = window.event.screenX;
		tempAvailY = window.event.screenY;		
		
    	getScrollTop = document.documentElement.scrollTop;
	} 
	else 
	{
		//event.pageX and event.pageY, using these fields you do not have to add the scroll position of the page
		tempX = e.pageX;
		tempY = e.pageY;
		
		tempAvailX = e.clientX;
		tempAvailY = e.clientY;
		
		getScrollTop = window.pageYOffset;		
	}
	
	if (tempX < 0) { tempX = 0;	}
	if (tempAvailX < 0) { tempAvailX = 0;	}
	if (tempY < 0) { tempY = 0;	}
	if (tempAvailY < 0) { tempAvailY = 0;	}
		
	return true;
}

function closeWin() {
	window.open('','_parent','');
	window.close();
}

function Trim(str) {
	var res = /^\s+/ig;
	var ree = /\s+$/ig;
	var out = str.replace(res,"").replace(ree,"");
    return out;
}

function isIE6OrAbove() 
{ 
	var bver = navigator.appVersion.indexOf("MSIE") 
	if(bver!=-1) { 
		bver=bver+5 
		var vernum = navigator.appVersion.substr(bver,3) 
		if(vernum>6) {
			return true;
		} else {
			return false;
		} 
	} else {
		return true;
	}
 } 

function isBrowser(b,v) {
  /*
  ** Check if the current browser is compatible
  **  b  browser name
  **  v  version number (if 0 don't check version)
  ** returns true if browser equals and version equals or greater
  */
  browserOk = false;
  versionOk = false;

  browserOk = (navigator.appName.indexOf(b) != -1);
  if (v == 0) versionOk = true;
  else  versionOk = (v <= parseInt(navigator.appVersion));
  return browserOk && versionOk;
  }


/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function focus_form_field(id)
{
	var obj;
	if (elId(id)) {obj =elId(id); } else { obj = elName(id);obj=obj[0];}	
	if(obj != null && obj.type != 'hidden' && obj.disabled == false) { obj.focus(); return true; }
	return false;
}

function CountText(ctl, indicator) {
    indicator.value = ctl.value.length;
}

if (self.parent.frames.length != 0) {
	self.parent.location = window.document.URL;
}
var IE = document.all?true:false;
if (!IE) {
	if (window.event) document.captureEvents(Event.MOUSEMOVE);
}
//mouse cursor position relative to the top left corner of the web page
var tempX=0;
var tempY=0;
//mouse distance from the left and top of the browser window respectively
var tempAvailX=0;
var tempAvailY=0;

var getScrollTop=0;

function showTip(ele, left, top)
{
	var tipbox = document.getElementById(ele);
	tipbox.style.left = tempX + left + "px";
	tipbox.style.top = tempY + top + "px";
	tipbox.style.visibility = 'visible';
}

function hideTip(ele)
{
	document.getElementById(ele).style.visibility='hidden';
}