function getUrlQueryItem( theArgName, theURL ) {
    vURL = (theURL=="") ? location.search : theURL;
	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");
        }
        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");
        }
        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();
      }
      break;
    case 27: // escape
      if (document.getElementById('divAbout').style.display!='none'){
        hideAbout();
      }else if (document.getElementById('divDialog').style.display!='none'){
        hideItSimple("divDialog");
      }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(){
  vDate = new Date;
  return vDate.getTime();
}
function logoutNow(){
  document.frmLogin.submit();
}
function setOverlay(){
  changecss(".overlay","width",screen.width);
  changecss(".overlay","height",documentHeight());
}
function hideCombobox( vItemToHideShow ){
  //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 hideAbout(){
  hideItSimple("divAbout");
  hideItSimple("overlay1");
}
function displayAbout(){ displaySystemDoc('about'); }
function displaySystemDoc(pDocType){
  hideOtherLayerExceptThis('divAbout');
  if (document.getElementById('divAbout') ){
      vURL=vRootDir+'systemdoc/'+pDocType+'.php?r='+getRandomString();
      makeGETRequest(vURL,'divAbout');
      showItSimple('divAbout');
      setOverlay();
      showItSimple("overlay1");
  }
}
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 );
}
