/***************************************************************************
 ********************** Cross Browser getElementByID ***********************
 ***************************************************************************/

function get_object(id) {
   var object = null;
   if( document.layers ) {
    object = document.layers[id];
   } else if( document.all ) {
    object = document.all[id];
   } else if( document.getElementById ) {
    object = document.getElementById(id);
   }
   return object;
  }

/***************************************************************************
 ************************ Query String Utilities ***************************
 ***************************************************************************/
  
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	var result = false;
	for (i=0;i<gy.length;i++) {
		//Default return value is false. We can do a boolean comparison to check that the specified key has a value.
		
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			result = ft[1];
		}
	}
	
	return result;
}

function checkForNull(variable, defaultVal){
	if(defaultVal=="undefined"||defaultVal==null){
		defaultVal="";
	}
	var returnVal = defaultVal
	if(variable!=""||variable!="undefined"||variable!=null){
		returnVal = variable;
	}
	return variable
}

 /***************************************************************************
 ********************** Cross Browser getElementByTagName() ***********************
 ***************************************************************************/
  
      var xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xNN4,xUA=navigator.userAgent.toLowerCase();

      function xGetElementsByTagName(t,p){

          var list = null;

          t = t || '*';

          p = p || document;

          if (xIE4 || xIE5) {

              if (t == '*') {

                  list = p.all;

          } else {

                  list = p.all.tags(t);

              }

          } else if (p.getElementsByTagName) {

              list = p.getElementsByTagName(t);

          }

          return list || new Array();
 
      }
	  
 /***************************************************************************
 ********************** Cross Browser getFlashMovieObject *******************
 ****************************************************************************/
	  
function getFlashMovieObject(movieName)
{
	returnVal = false;
	if (window.document[movieName])
	{
		returnVal = window.document[movieName];
	}
	
	if (navigator.appName.indexOf("Microsoft Internet")==-1)
	{
		if (document.embeds && document.embeds[movieName])
		returnVal = document.embeds[movieName];
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		returnVal = document.getElementById(movieName);
	}
	return returnVal;
}

/***************************************************************************
 *************************** Array Utilities********************************
 ***************************************************************************/

/*Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i] && this[i]!="##sep##")
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}*/

/***************************************************************************
 *************************** String Utilities ******************************
 ***************************************************************************/


// Replaces all instances of the given substring.
	 String.prototype.replaceAll = function(
	 strTarget, // The substring you want to replace
	 strSubString // The string you want to replace in.
	 ){
	 var strText = this;
	 var intIndexOfMatch = strText.indexOf( strTarget );
	  
	 // Keep looping while an instance of the target string
	 // still exists in the string.
	 while (intIndexOfMatch != -1){
	 // Relace out the current instance.
	 strText = strText.replace( strTarget, strSubString )
	  
	 // Get the index of any next matching substring.
	 intIndexOfMatch = strText.indexOf( strTarget );
	 }
	  
	 // Return the updated string with ALL the target strings
	 // replaced out with the new substring.
	 return( strText );
	 }
	
	/*function php_urlencode (str) {
		str = escape(str);
		return str.replace(/[*+\/@]|%20/g, function (s) {
				switch (s) {
					case "*": s = "%2A"; break;
					case "+": s = "%2B"; break;
					case "/": s = "%2F"; break;
					case "@": s = "%40"; break;
					case "%20": s = "+"; break;
				}
				return s;
				}
			);
	}*/
	
function php_urlencode (str) {
    // URL-encodes string  
    // 
    // version: 910.813
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    var hexStr = function (dec) {
        return '%' + (dec < 16 ? '0' : '') + dec.toString(16).toUpperCase();
    };

    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters); https://developer.mozilla.org/index.php?title=en/Core_JavaScript_1.5_Reference/Global_Objects/String/charCodeAt
                ret += ((code - 0xD800) * 0x400) + (str.charCodeAt(i+1) - 0xDC00) + 0x10000;
                i++; // skip the next one as we just retrieved it as a low surrogate
            }
            // We never come across a low surrogate because we skip them, unless invalid
            // Reserved assumed to be in UTF-8, as in PHP
            else if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048) { // 3 bytes (code < 65536)
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}


/***************************************************************************
 *************************** Cookie Utilities*******************************
 ***************************************************************************

Routine to write a session cookie

    Parameters:
        cookieName        Cookie name
        cookieValue       Cookie Value

    Return value:
        true              Session cookie written successfullly
        false             Failed - persistent cookies are not enabled

   e.g. if (writeSessionCookie("pans","drizzle") then
           alert ("Session cookie written");
        else
           alert ("Sorry - Session cookies not enabled");
*/

function writeSessionCookie (cookieName, cookieValue) {
  if (testSessionCookie()) {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else return false;
}

/*==============================================================================

Routine to get the current value of a cookie

    Parameters:
        cookieName        Cookie name

    Return value:
        false             Failed - no such cookie
        value             Value of the retrieved cookie

   e.g. if (!getCookieValue("pans") then  {
           cookieValue = getCoookieValue ("pans2);
        }
*/

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

/*==============================================================================

Routine to see if session cookies are enabled

    Parameters:
        None

    Return value:
        true              Session cookies are enabled
        false             Session cookies are not enabled

   e.g. if (testSessionCookie())
           alert ("Session coookies are enabled");
        else
           alert ("Session coookies are not enabled");
*/

function testSessionCookie () {
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled")
    return true
  else
    return false;
}

/*==============================================================================

Routine to see of persistent cookies are allowed:

    Parameters:
        None

    Return value:
        true              Session cookies are enabled
        false             Session cookies are not enabled

   e.g. if (testPersistentCookie()) then
           alert ("Persistent coookies are enabled");
        else
           alert ("Persistent coookies are not enabled");
*/

function testPersistentCookie () {
  writePersistentCookie ("testPersistentCookie", "Enabled", "minutes", 1);
  if (getCookieValue ("testPersistentCookie")=="Enabled")
    return true
  else
    return false;
}

/*==============================================================================

Routine to write a persistent cookie

    Parameters:
        CookieName        Cookie name
        CookieValue       Cookie Value
        periodType        "years","months","days","hours", "minutes"
        offset            Number of units specified in periodType

    Return value:
        true              Persistent cookie written successfullly
        false             Failed - persistent cookies are not enabled

    e.g. writePersistentCookie ("Session", id, "years", 1);
*/

function writePersistentCookie (CookieName, CookieValue, periodType, offset) {

  var expireDate = new Date ();
  offset = offset / 1;

  var myPeriodType = periodType;
  switch (myPeriodType.toLowerCase()) {
    case "years":
     var year = expireDate.getYear();
     // Note some browsers give only the years since 1900, and some since 0.
     if (year < 1000) year = year + 1900;
     expireDate.setYear(year + offset);
     break;
    case "months":
      expireDate.setMonth(expireDate.getMonth() + offset);
      break;
    case "days":
      expireDate.setDate(expireDate.getDate() + offset);
      break;
    case "hours":
      expireDate.setHours(expireDate.getHours() + offset);
      break;
    case "minutes":
      expireDate.setMinutes(expireDate.getMinutes() + offset);
      break;
    default:
      alert ("Invalid periodType parameter for writePersistentCookie()");
      break;
  }

  document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}

/*==============================================================================

Routine to delete a persistent cookie

    Parameters:
        CookieName        Cookie name

    Return value:
        true              Persistent cookie marked for deletion

    e.g. deleteCookie ("Session");
*/

function deleteCookie (cookieName) {

  if (getCookieValue (cookieName)) writePersistentCookie (cookieName,"Pending delete","years", -1);
  return true;
}
