
YAHOO.namespace( 'AutoHub' );

YAHOO.AutoHub = function( )
{
  function AutoHubData( x )
  {
    this.nUserId    =  x.userid ;
    this.sUriState  =  x.uri_state ;
  }

  return {
    Data: null,
    Init: function(x) { this.Data = new AutoHubData( x ); },
    Log: function(x) { }
  };

}();


YAHOO.namespace( 'AutoHub.Util' );

YAHOO.AutoHub.Util = function( )
{
  function AutoHubUtil( ) { }

  AutoHubUtil.prototype.AddStateToArgs = function( sArgs )
  {
    // If there is no uri state, return args as-is
    if( ! YAHOO.AutoHub.Data.sUriState ) return sArgs ;
    // Append uri state to args
    return( ( sArgs ? sArgs+"&" : "" ) + YAHOO.AutoHub.Data.sUriState );
  }

  AutoHubUtil.prototype.UriWithState = function( sUri )
  {
    // If there is no uri state, return sUri as-is
    if( ! YAHOO.AutoHub.Data.sUriState ) return sUri ;
    // Replace "?" with "?state&" in sUri
    var sUri2 = sUri.replace( /\?/, "?"+YAHOO.AutoHub.Data.sUriState+"&" );
    if( sUri2 != sUri ) return sUri2 ; // return result if there was a change
    // Append "?state" to sUri which has no args
    return sUri + "?" + YAHOO.AutoHub.Data.sUriState ;
  }

  AutoHubUtil.prototype.XhtmlEncode = function( s )
  {
    if( typeof(s) != 'string' ) return "" ;
    return s.replace( /\&/g, "&amp;" )
            .replace( /</g, "&lt;" ).replace( />/g, "&gt;" )
            .replace( /"/g, "&quot;" ).replace( /'/g, "&#8217;" );
  }

  AutoHubUtil.prototype.ParseQueryArgs = function( sArgs )
  {
    hArgs = { };
    aArgs = sArgs.split( /&/g );

    for( var i=0; i<aArgs.length; i++ )
    {
      aArg = aArgs[i].split( /\=/, 2 );
      if( aArg[0] !== '' )
      {
        if( aArg[1] )
          hArgs[unescape(aArg[0])] = YAHOO.AutoHub.Util.utf8_decode( unescape( aArg[1] ) );
        else
          hArgs[unescape(aArg[0])] = true;
      }
    }

    return hArgs ;
  }

  AutoHubUtil.prototype.getElementsByClass = function( sClass, sTag )
  {
    sTag       = sTag || '*';
    var aFound = [];
    var aElts  = document.getElementsByTagName( sTag );
    var oRE    = new RegExp( '(^|\\s)'+sClass+'(\\s|$)' );

    for( var i = 0; i < aElts.length; i++ )
      if( oRE.test( aElts[i].className ) )
        aFound.push( aElts[i] );

    return aFound;
  }
  
  AutoHubUtil.prototype.NumberFormat = function(nNumber, nDecimals, sDecimalPoint, sThousandsSeparator)
  {
	  // get the desired decimals
	  nNumber = Math.round(nNumber * Math.pow(10, nDecimals)) / Math.pow(10, nDecimals);
	  
	  // convert number to string
	  sNumber = nNumber + '';
	  aNum = sNumber.split('.');
	  if (!aNum[0]) 
	  {
		aNum[0] = '0';
	  }
	  if (!aNum[1]) 
	  {
	    aNum[1] = '';
	  }
	  if (aNum[1].length < nDecimals) 
	  {
	    sDec = aNum[1];
	    for (i=aNum[1].length + 1; i <= nDecimals; i++) 
	    {
	      sDec += '0';
	    }
	    aNum[1] = sDec;
	  }
	  if(sThousandsSeparator != '' && aNum[0].length > 3) 
	  {
	    sNum = aNum[0];
	    aNum[0] = '';
	    for(j = 3; j < sNum.length; j+=3) 
	    {
	      i = sNum.slice(sNum.length - j, sNum.length - j + 3);
	      aNum[0] = sThousandsSeparator + i +  aNum[0] + '';
	    }
	    j = sNum.substr(0, (sNum.length % 3 == 0) ? 3 : (sNum.length % 3));
	    aNum[0] = j + aNum[0];
	  }
	  sDecimalPoint = (nDecimals <= 0) ? '' : sDecimalPoint;
	  
	  return aNum[0] + sDecimalPoint + aNum[1];

  }

  // public method for url encoding
  AutoHubUtil.prototype.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;
  };

  // public method for url decoding
  AutoHubUtil.prototype.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;
  };

  
  AutoHubUtil.prototype.base64_encode = function(data) 
  {
    // Encodes string using MIME base64 algorithm  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/base64_encode
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Bayron Guevara
    // +   improved by: Thunder.m
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_encode
    // *     example 1: base64_encode('Kevin van Zonneveld');
    // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['atob'] == 'function') {
    //    return atob(data);
    //}
        
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];

    if (!data) {
        return data;
    }

    data = YAHOO.AutoHub.Util.utf8_encode(data+'');
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch (data.length % 3) {
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
  }

  AutoHubUtil.prototype.base64_decode = function(data) 
  {
    // Decodes string using MIME base64 algorithm  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/base64_decode
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['btoa'] == 'function') {
    //    return btoa(data);
    //}

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, dec = "", tmp_arr = [];

    if (!data) {
        return data;
    }

    data += '';

    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);

    dec = tmp_arr.join('');
    dec = YAHOO.AutoHub.Util.utf8_decode(dec);

    return dec;
  }
  
  AutoHubUtil.prototype.PopulateDropDown = function( sSelectElemId, hOptions, sSelectedValue )
  {
    var selectElem = document.getElementById(sSelectElemId);
    // remove existing options
    while (selectElem.length > 0)
    {
      selectElem.remove(0);
    }

    
    for (var value in hOptions)
    {
      var opt = document.createElement('option');
      opt.value = value;
      opt.text = hOptions[value];
      if (value == sSelectedValue)
        opt.selected=true;
      try
      {
        selectElem.add(opt, null); // standards compliant; doesn't work in IE
      }
      catch(ex)
      {
        selectElem.add(opt);   // IE only  
      }
    }
  }

  
  return new AutoHubUtil( );

}();
