var http_request;
var div_request;
function mshow(node) { node.style.display="block";}
function mhide(node) { node.style.display="none"; }
function popUp(URL)  { window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=735,height=465'); }
function openwindow(imgURL) { myImg=new Image(); myImg.src=imgURL; w=myImg.width; h=myImg.height;	window.open(imgURL,"mywindow","menubar=no,resizable=yes,width="+w+",height="+h);}
function noCache(uri) { return uri.concat(/\?/.test(uri)?"&":"?","noCache=",(new Date).getTime(),".",Math.random()*1234567);}

function getInfo(div, fileName, parameters) {
      http_request=false;
      if (window.XMLHttpRequest) { // Firefox, Mozilla, Safari,...
         http_request=new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request=new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request=new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      div_request=div;
      http_request.onreadystatechange=stateChanged;
      http_request.open('GET', noCache(fileName + parameters), true);
      http_request.setRequestHeader('Content-Type', 'text/plain; charset=utf8');
      http_request.send(null);
}

function stateChanged () {
  switch (http_request.readyState) {
    case 0:
		if (document.getElementById("loading")!=null) document.getElementById("loading").style.visibility="visible";
		return document.getElementById(div_request).innerHTML="";
		break;
    case 1:
		if (document.getElementById("loading")!=null) document.getElementById("loading").style.visibility="visible";    break;
    case 2:
		if (document.getElementById("loading")!=null) document.getElementById("loading").style.visibility="visible";    break;
    case 3:
		if (document.getElementById("loading")!=null) document.getElementById("loading").style.visibility="visible";    break;
    case 4:
		if (document.getElementById("loading")!=null) document.getElementById("loading").style.visibility="hidden";
		return document.getElementById(div_request).innerHTML=http_request.responseText;
		break;
    default:
		if (document.getElementById("loading")!=null) ocument.getElementById("loading").style.visibility="hidden";
  }
}

function getForm(obj, target, filePath, level, params) {
  var getstr='';
  if (obj.hasChildNodes()) {
    for (var i=0; i<obj.childNodes.length; i++) {
      if (obj.childNodes[i].tagName=="INPUT") {
        if (obj.childNodes[i].type=="text") {
          getstr +=obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + "&";
        }
        if (obj.childNodes[i].type=="password") {
          getstr +=obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + "&";
        }
        if (obj.childNodes[i].type=="hidden") {
          getstr +=obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + "&";
        }
        if (obj.childNodes[i].type=="checkbox") {
          if (obj.childNodes[i].checked) {
            getstr +=obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
          } else {
            getstr +=obj.childNodes[i].name + "=&";
          }
        }
        if (obj.childNodes[i].type=="radio") {
          if (obj.childNodes[i].checked) {
            getstr +=obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
          }
        }
        if (obj.childNodes[i].type=="file") {
          getstr +=obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
        }
      }
      if (obj.childNodes[i].tagName=="SELECT") {
        var sel=obj.childNodes[i];
        getstr +=sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
      }
      if (obj.childNodes[i].tagName=="TEXTAREA") {
        var sel=obj.childNodes[i];
        getstr +=obj.childNodes[i].name + "=" + urlencode(obj.childNodes[i].value) + "&";
      }
            if (obj.childNodes[i].hasChildNodes() && obj.childNodes[i].tagName !="SELECT") {
              getstr +=getForm(obj.childNodes[i], level+1);
          }

        }
  }
  if (level==0) {
	getInfo(target, filePath+'?'+params+'&', getstr);
  } else {
    return getstr;
  }
}

function urlencode( str ) {                             
    var hash_map={}, unicodeStr='', hexEscStr='';
    var ret=(str+'').toString();
    
    var replacer=function(search, replace, str) {
        var tmp_arr=[];
        tmp_arr=str.split(search);
        return tmp_arr.join(replace);
    };

    // The hash_map is identical to the one in urldecode.
    hash_map["'"]='%27';hash_map['(']='%28';hash_map[')']='%29';hash_map['*']='%2A';hash_map['~']='%7E';hash_map['!']='%21';hash_map['%20']='+';
    hash_map['\u00DC']='%DC';hash_map['\u00FC']='%FC';hash_map['\u00C4']='%D4';hash_map['\u00E4']='%E4';hash_map['\u00D6']='%D6';hash_map['\u00F6']='%F6';hash_map['\u00DF']='%DF';hash_map['\u20AC']='%80';
    hash_map['\u0081']='%81';hash_map['\u201A']='%82';hash_map['\u0192']='%83';hash_map['\u201E']='%84';hash_map['\u2026']='%85';hash_map['\u2020']='%86';hash_map['\u2021']='%87';hash_map['\u02C6']='%88';
    hash_map['\u2030']='%89';hash_map['\u0160']='%8A';hash_map['\u2039']='%8B';hash_map['\u0152']='%8C';hash_map['\u008D']='%8D';hash_map['\u017D']='%8E';hash_map['\u008F']='%8F';hash_map['\u0090']='%90';
    hash_map['\u2018']='%91';hash_map['\u2019']='%92';hash_map['\u201C']='%93';hash_map['\u201D']='%94';hash_map['\u2022']='%95';hash_map['\u2013']='%96';hash_map['\u2014']='%97';hash_map['\u02DC']='%98';
    hash_map['\u2122']='%99';hash_map['\u0161']='%9A';hash_map['\u203A']='%9B';hash_map['\u0153']='%9C';hash_map['\u009D']='%9D';hash_map['\u017E']='%9E';hash_map['\u0178']='%9F';    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret=encodeURIComponent(ret);
 
    for (unicodeStr in hash_map) {
        hexEscStr=hash_map[unicodeStr];
        ret=replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}

function include(script_filename) {document.write('<' + 'script');document.write(' language="javascript"');document.write(' type="text/javascript"');document.write(' src="' + script_filename + '">');document.write('</' + 'script' + '>');}
function include_str(script) {document.write('<' + 'script');document.write(' language="javascript">');document.write(script);document.write('</' + 'script' + '>');}
function load_script(scriptpath){
      http_request=false;
      if (window.XMLHttpRequest) {
         http_request=new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { 
   	     try {
            http_request=new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request=new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.open('GET', scriptpath, false);
      http_request.setRequestHeader('Content-Type', 'text/plain; charset=utf8');
      http_request.send('');
	  return http_request.responseText;
}

function WaitForLogon(path) { 
	if( http_request.readyState!=4) {
		setTimeout("WaitForLogon('"+path+"')",100); 
	} else {
		if (document.getElementById('user_login')==null) document.location=path;
	} 
};

function Set_Cookie( name, value )
{
	var today = new Date();
	today.setTime( today.getTime());
	var expires_date = new Date( today.getTime() + (1000 * 60 * 0.3) );
	document.cookie = name + "=" +escape( value ) +	( ";expires=" + expires_date.toGMTString() + ";" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return '';
	}
}

function Delete_Cookie( name ) {
	document.cookie = name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
