function updateLogInfo( pURL ){
	makeGETRequest( pURL, 'divLogInfo' );
}
function setUserPref( pUserID, pPrefName, pPrefValue){
	vURL = "includes/user_pref_set.php?r="+getRandomString()+"&uid="+pUserID+"&n="+pPrefName+"&v="+pPrefValue;
	makeGETRequest(vURL);
}
function getUrlQueryItem( theArgName, theURL ) {
    vURL = (theURL=="") ? location.search : theURL;
    
    // Add By TAN --------------------------------
    sUrlEnd = vURL.indexOf('?');
    if(sUrlEnd>0){
      vTmp = vURL.slice(sUrlEnd,vURL.length);
      vURL = vTmp;
    }
    // -------------------------------------------
    sArgs = vURL.slice(1).split('&');
    r = '';
    for (var i = 0; i < sArgs.length; i++) {
        if (sArgs[i].slice(0,sArgs[i].indexOf('=')) == theArgName) {
            r = sArgs[i].slice(sArgs[i].indexOf('=')+1);
            break;
        }
    }
    return (r.length > 0 ? unescape(r).split(',') : '')
}
function getObjAttributeValue(obj,vAttribute){
  returnValue = obj.getAttribute(vAttribute);
  return returnValue;
}
function findPosX(obj){
  var curleft = 0;
  if(obj.offsetParent)
    while(1){
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  else if(obj.x)
    curleft += obj.x;
  return curleft;
}
function findPosY(obj){
  var curtop = 0;
  if(obj.offsetParent)
    while(1){
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
  else if(obj.y)
    curtop += obj.y;
  return curtop;
}
function hideOtherLayerExceptThis( pDontHideThisDiv ){
  switch (pDontHideThisDiv){
    case 'lightbox1':
      if (document.getElementById('divAbout')){
        if (document.getElementById('divAbout').style.display!='none'){
          hideItSimple("divAbout");
          hideItSimple("divDialog");
		  hideItSimple("divCommonImage");
        }
        if (document.getElementById('divStartMenu')){
          hideItSimple("divStartMenu");
        }
        if (document.getElementById('divColorProfile')){
          hideItSimple("divColorProfile");
        }
      }
      break;
    case 'divAbout':
      if (document.getElementById('lightbox1') ){
        if (document.getElementById('lightbox1').style.display!='none'){
          hideItSimple("lightbox1");
          hideItSimple("divDialog");
		  hideItSimple("divCommonImage");
        }
        if (document.getElementById('divStartMenu')){
          hideItSimple("divStartMenu");
        }
        if (document.getElementById('divColorProfile')){
          hideItSimple("divColorProfile");
        }
      }
	  break;
    case 'divCommonImage':
      if (document.getElementById('divCommonImage') ){
        if (document.getElementById('divCommonImage').style.display!='none'){
          hideItSimple("lightbox1");
          hideItSimple("divDialog");
		  hideItSimple("divAbout");
        }
        if (document.getElementById('divStartMenu')){
          hideItSimple("divStartMenu");
        }
        if (document.getElementById('divColorProfile')){
          hideItSimple("divColorProfile");
        }
      }	  
  }
}
function handleKey (field, event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  switch (keyCode){
    case 13: // enter
      if (field.name=='txt_user_name' || field.name=='txt_password'){
        webSave();
      }else if(field.name=='search_case'){
        if(document.search_case_list_form){
          document.search_case_list_form.submit();
        }
      }else if(field.name=='search'){
        if(recSearch()){
          recSearch();
        }
      }
      break;
    case 27: // escape
      if (document.getElementById('divAbout').style.display!='none'){
        hideAbout();
      }else if (document.getElementById('divDialog').style.display!='none'){
        hideItSimple("divDialog");
	  }else if (document.getElementById('divCommonImage').style.display!='none'){
		hideItSimple("divCommonImage");
		hideItSimple('overlay1');
      }else{
        displayLogoutLayer();
      }
      break;
  }
}
function documentHeight() {
  if (window.innerHeight && window.scrollMaxY) {// Firefox
	yWithScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  	yWithScroll = document.body.scrollHeight;
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
 	yWithScroll = document.body.offsetHeight;
  }
  return yWithScroll;
}
function getRandomString(){
  a=Math.random()+'a';
  b=a.split('.');
  vDate = new Date;
  return vDate.getTime()+b[1];
}
function logoutNow(){
  document.frmLogin.submit();
}
function setOverlay(){
  changecss(".overlay","width",screen.width);
  changecss(".overlay","height",documentHeight());
}
function hideCombobox( vItemToHideShow ){
	var vIsSelectHidden;
    vIsSelectHidden = (vIsSelectHidden === undefined)? false:true;

    visibility = (vIsSelectHidden) ? 'hidden' : 'none';
    display = (vIsSelectHidden) ? 'none' : 'block';
    selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		//selects[i].style.visibility = visibility;
		selects[i].style.display = display;
	}

  /*
  //hides the combobox, specify the name in variable vItemToHideShow separate by coma (,)
  vIsSelectHidden = (vIsSelectHidden)? false:true;
  var vDocItem = new Array();
  vDocItem = vItemToHideShow.split(",");
  for(p=0;p<vDocItem.length;p++){
    if(document.getElementById(vDocItem[p])){
      if(vIsSelectHidden){
        document.getElementById(vDocItem[p]).style.visibility = "hidden";
      }else{
        document.getElementById(vDocItem[p]).style.visibility = "visible";
      }
    }
  }
  */
}
function setDivToCenter(id){
	//Get object from id
	obj = document.getElementById(id);
	
	//Get the height and width of screen
	var swidth = screen.availWidth;
	var sheight = screen.availHeight;
		
	//Set the top-offset and width-offset
	var top = (sheight - obj.offsetHeight)/2 - 100 + "px";		
	if (obj.offsetWidth==0){
		// offset width (dont know why firefox wont return the correct offset, so, we divide to 1/4 of the scren width)
		var left = swidth/4 + "px";
	}else{
		var left = (swidth -obj.offsetWidth)/2 + "px";
	}
	
	//Set the attribute to the object's style
	obj.style.top = top;
	obj.style.left = left;
		
	v = 'screen width='+swidth+'\n';
	v +='screen height='+sheight+'\n';
	v +='\ntop='+top+'\n';
	v +='left='+left+'\n\n';
	v +='offset width='+obj.offsetWidth+'\n';
	v +='offset height='+obj.offsetHeight+'\n';
	//v +='client width='+obj.clientWidth+'\n'
	//alert(v)
	//alert(obj.style.width)
}
function hideAbout(){
  hideItSimple("divAbout");
  hideItSimple("overlay1");
}
function displayCommonImage( pURL ){
	document.getElementById('imgCommon').src = pURL;
	displaySystemDoc('image');	
	showItSimple('divCommonImage');
	setDivToCenter('divCommonImage');
	setOverlay();
	showItSimple("overlay1");	
	
}
function displayAbout(){ displaySystemDoc('about'); }
function displaySystemDoc(pDocType){
	switch (pDocType){
		case 'about':
			hideOtherLayerExceptThis('divAbout');
			if (document.getElementById('divAbout') ){
				vURL=vRootDir+'systemdoc/'+pDocType+'.php?r='+getRandomString();
				makeGETRequest(vURL,'divAbout');
				showItSimple('divAbout');
				setOverlay();
				showItSimple("overlay1");
			}
			break;
		case 'image':
			hideOtherLayerExceptThis('divCommonImage');			
			break;
		default:
			// do nothing
	}
}
function displayLogoutLayer(){
  if(document.getElementById('lightbox1') && document.getElementById('overlay1')){
      hideOtherLayerExceptThis('lightbox1');
      toggleDivHideShow("lightbox1");
      hideCombobox('');
      setOverlay();
      toggleDivHideShow("overlay1");
  }
}
function newSomething(){
  if (window.recNew){
    recNew();
  }
}
function readSomething(){
  if (window.recRead){
    recRead();
  }
}
function editSomething(){
  if (window.recEdit){
    recEdit();
  }
}
function saveSomething(){
  if (window.recSave){
    recSave();
  }
}
function deleteSomething(){
  if (window.recDeleteConfirm){
    recDeleteConfirm();
  }else{
    if (window.recDelete){
      recDelete();
    }
  }
}
function toggleDisplayQuickTheme(){
  toggleDivHideShow("divColorProfile");
}
//==================== Cookie Management =====================
function setCookieExp( c, v, d ){
	defaultValue = document.forms[0].test_field5.value;
	d = (d=="" ? defaultValue : d);
	v = (v=="" ? "" : v)
	if ( getCookie( c ) != "" ){
		setCookie( c, v, d )
		document.forms[0].test_field4.value = "expired=" + d;
	}else{
		document.forms[0].test_field4.value = "cookie " + c + " not found"
	}
}

function getCookie( strName ){
	var searchString = strName+"=";
	var returnValue = "";
	var offset = 0;
	var end = 0;

	if (document.cookie.length > 0 ){
		offset= document.cookie.indexOf( searchString );
		if ( offset != -1 ){
			offset += searchString.length;
			end = document.cookie.indexOf( ";" , offset );
			if (end == -1){
				end = document.cookie.length;
			}
			returnValue = unescape( document.cookie.substring( offset, end ) );
		}
	}
	return returnValue;
}

function setCookie( strName, strValue, dateExpired ){
	var nameValuePair = strName + "=" + escape(strValue);
	var expDate = ( dateExpired=="" ? "" : ("; expires="+dateExpired ) )
	var vpath = ";path=/"
	document.cookie = nameValuePair + expDate + vpath;
}

function changeCookieValue( strName, strValue){
	if ( getCookie( strName ) !=""  ){
		setCookie( strName, strValue, "" )
	}
}
function setQueryString(pQuery, pValue){
    /* "%3F"-"?" , "%3D"-"=", "%26"-"&" */
    vSign="&"
    vLocation=escape(window.location)
    vSplitValue=vLocation.split("%3F"+pQuery+"%3D")
    if (vSplitValue.length<2){
        vSplitValue=vLocation.split("%26"+pQuery+"%3D")
    }
    if (vSplitValue.length<2){
        vSplictQ=vLocation.split("%3F")
        if (vSplictQ.length<2){
            vSign="?" 
        }
    window.location=window.location+vSign+pQuery+"="+pValue
    }else{
        vFirstArray = vSplitValue[0]
        vSplictQ=vFirstArray.split("%3F")
        if (vSplictQ.length<2){
            vSign="?"
        }
        vFirstHTTP = (unescape(vSplitValue[0]))
        vSecondHTTP = ""
        vSecondArray = unescape(vSplitValue[1])
        vSplitAnd = vSecondArray.split("&")
        if (vSplitAnd.length>1){
            vSplitAnd[0]="";
            vSecondHTTP = vSplitAnd.join("&");
        }
    }
    window.location=vFirstHTTP+vSign+pQuery+"="+pValue+vSecondHTTP
}
function expandDiv(pDivDisplayName, pDivName){
  vTitle = pDivDisplayName;
  vURL = pDivName;
  vSetting = '';
  vWinType = 'div';
  openNewDhtmlWindow( vTitle, vURL, vSetting, vWinType );
}
//==================== GUI RELATED =====================
function onClickEffectForTextbox(pDefaultTxt,pTextboxID){
	obj = document.getElementById(pTextboxID);
	if ( obj.value==pDefaultTxt ){
		obj.value='';
		obj.style.color = '#000000';
	}
}
function onBlurEffectForTextbox(pDefaultTxt,pTextboxID){
	obj = document.getElementById(pTextboxID);
	if ( obj.value=='' ){
		obj.value=pDefaultTxt;
		//obj.style.color = '#808080';
		obj.style.color = '#808080';
	}
}
function initEffectForTextbox(pDefaultTxt,pTextboxID){
	onBlurEffectForTextbox(pDefaultTxt,pTextboxID)
}
