/*****************************************************************************\
  FILE..........: hlpfuncs.js
  AUTHOR........: Dmitry Vinogradov
  COPYRIGHT.....: Copyright(c) 2006 - 2007, Kribla. All rights reserved.
  DESCRIPTION...:
  HISTORY.......: DATE       COMMENT
                  ---------- ------------------------------------------------
                      y  m  d
                  2006-12-15 Created by VDima.
\*****************************************************************************/

//----------------------------------------------------------------------------

var g_arrID_Elems = new Array();

function GetByID(strID)
{
  if ( g_arrID_Elems[strID] )
    return g_arrID_Elems[strID];

  g_arrID_Elems[strID] = document.getElementById(strID);

  return g_arrID_Elems[strID];
}

//----------------------------------------------------------------------------

function StartWith(str, strStart)
{
  return str ? str.substr(0, strStart.length) == strStart : false;
}


function StartWithNoCase(str, strStart)
{
  if ( str )
  {
    if ( strStart )
    {
      str      = str.toUpperCase();
      strStart = strStart.toUpperCase();

      return str ? str.substr(0, strStart.length) == strStart : false;
    }
    return true;
  }

  return false;
}


function HasSubStrNoCase(str, substr1)//substr2, substr3...
{
  var i;

  if ( str && 0 < str.length )
  {
    str = str.toUpperCase();

    for(i = 1; i < arguments.length; i++)
    {
      substr = arguments[i];

      if ( substr )
      {
        substr = substr.toUpperCase();

        if ( -1 != str.indexOf(substr) )
          return true;
      }
    }
  }

  return false;
}


function ReplaceTxt(strWhere, strSub, strWith)
{
  var oSub = new RegExp(strSub, "g");

  return strWhere.replace(oSub, strWith);
}


function CreateStr(str, iTimes)
{
  var strRet = new String();
  for(i = 0; i < iTimes; i++)
    strRet += str;

  return strRet;
}


function TrimSpaces(str)
{
  var i = 0;

  while(i < str.length && ' ' == str.charAt(i) )
    i++;

  if ( 0 < i )
    str = str.substr(i);

  i = str.length;
  while( 0 < i && ' ' == str.charAt(i) )
    i--;

  str = str.substr(0, i + 1);

  return str;
}


function Split(str, strSeporators)
{
  var i;
  var ch;
  var arrRet  = new Array();
  var strWord = "";

  for(i = 0; i < str.length; i++)
  {
    ch = str.charAt(i);

    if ( 0 <= strSeporators.indexOf(ch) )
    {
      if ( 0 < strWord.length )
      {
        arrRet[arrRet.length] = strWord;
        strWord               = "";
      }
    }
    else
      strWord += ch;
  }
  
  if ( 0 < strWord.length )
    arrRet[arrRet.length] = strWord;

  return arrRet;
}


function ResizeImg(oImg, w, h)
{
  if ( oImg )
  {
    var wSrc = oImg.width;
    var hSrc = oImg.height;

    if ( w < wSrc || h < hSrc )
    {
      if ( wSrc * h / hSrc <= w )
        oImg.height = h;
      else
        oImg.width  = w;
    }
  }
}



function ParseInt(strNum)//defaultNum, radix
{
  var n = 2 < arguments.length ? parseInt(strNum, arguments[2]) 
                               : parseInt(strNum);
  if ( isNaN(n) )
  {
    if ( 1 < arguments.length )
      n = arguments[1];
    else
      n = 0;
  }

  return n;
}


//----------------------------------------------------------------------------


function FindPos(o)
{
  var iLeft = new Number(0);
  var iTop  = new Number(0);

  if ( o )
  {
    iLeft = o.offsetLeft;
    iTop  = o.offsetTop;
    o     = o.offsetParent;

    while(o)
    {
      iLeft += o.offsetLeft;
      iTop  += o.offsetTop;
      o      = o.offsetParent;
    }
  }
  return [iLeft, iTop];
}


function SetMaxWidth(arrElems)//, MinWidth
{
  if ( arrElems && 0 < arrElems.length )
  {
    var i, w = 0;

    if ( 1 < arguments.length )
      w = parseInt(arguments[1]);

    for(i = 0; i < arrElems.length; i++)
    {
      if ( arrElems[i] && arrElems[i].offsetWidth )
      {
        if ( w < arrElems[i].offsetWidth )
          w = arrElems[i].offsetWidth;
      }
    }

    w = w + "px";

    for(i = 0; i < arrElems.length; i++)
    {
      if ( arrElems[i] && arrElems[i].style )
        arrElems[i].style.width = w;
    }
  }
}


function ColWidth(oTable, nCol)
{
  if ( oTable && 0 <= nCol )
  {
    var o = oTable.rows[0];

    if ( o && o.cells && 0 < o.cells.length && nCol < o.cells.length )
    {
      o = o.cells[nCol];
      if ( o )
        return o.offsetWidth;
    }
  }

  return 0;
}


function GetMEElement(oEvent)
{
  var oRet = null;
  if ( window.event )
    oEvent = window.event;

  if ( oEvent.target )
    oRet = oEvent.target;
  else
  {
    if ( oEvent.srcElement )
      oRet = oEvent.srcElement;
  }

  if ( oRet.nodeType == 3 )//Safari bug
    oRet = oRet.parentNode;

  return oRet;
}


function IsHasParent(o, strID)
{
  while(o)
  {
    if ( strID == o.id )
      return true;

    o = o.parentNode;
  }

  return false;
}


//----------------------------------------------------------------------------

function OnEnterDo(oEvent, fDoOnEnter)
{
  if ( oEvent && 13 == oEvent.keyCode && fDoOnEnter ) //Enter
    fDoOnEnter();
}


function SetValue(strID, strValue)
{
  var obj = document.getElementById(strID);
  if ( obj )
    obj.value = strValue;
}


function GetValue(strID)
{
  var obj = document.getElementById(strID);
  if ( obj )
    return obj.value;

  if ( 2 <= arguments.length )
    return arguments[1];

  return null;
}


function GetCmbValue(strID)
{
  var strRet = new String();
  var oCmb   = document.getElementById(strID);

  if ( oCmb )
  {
    var t = new Number(oCmb.selectedIndex);
    if ( t < 0 )
      t = 0;

    strRet = oCmb.options[t].value;
  }

  return strRet;
}


function GetChecked(strID)
{
  var obj = document.getElementById(strID);
  if ( obj && obj.checked )
  {
    var b = new Boolean(obj.checked);
    return b;
  }

  return false;
}


function SetChecked(strID, bSet)
{
  var obj = document.getElementById(strID);
  var b   = new Boolean(bSet);

  if ( obj )
    obj.checked = (false != b);
}


function SwitchChk(strID)
{
  SetChecked(strID, false == GetChecked(strID));
}


function GetCmbSelID(strCmbID)
{
  var strRet = new String();
  var oCmb   = document.getElementById(strCmbID);

  if ( oCmb )
  {
    var t = new Number(oCmb.selectedIndex);
    if ( t < 0 )
      t = 0;

    strRet = oCmb.options[t].id;
  }

  return strRet;
}


function SetCmbSelID(strCmbID, strOptID)
{
  var oCmb = document.getElementById(strCmbID);

  if ( oCmb )
  {
    for(i = 0; i < oCmb.length; i++)
    {
      if ( strOptID == oCmb.options[i].id )
      {
        oCmb.options[i].selected = true;
        return true;
      }
    }
  }

  return false;
}


function SetCmbSel(strID, strValue)
{
  var oCmb = document.getElementById(strID);

  if ( oCmb )
  {
    for(i = 0; i < oCmb.length; i++)
    {
      if ( strValue == oCmb.options[i].value )
      {
        oCmb.options[i].selected = true;
        return true;
      }
    }
  }

  return false;
}


function SetCmbSelTxt(strID, strTxt)
{
  var oCmb = document.getElementById(strID);

  if ( oCmb )
  {
    for(i = 0; i < oCmb.length; i++)
    {
      if ( strTxt == oCmb.options[i].text )
      {
        oCmb.options[i].selected = true;
        return true;
      }
    }
  }

  return false;
}


function SetCmbSelLabel(strID, strLabel)
{
  var oCmb = document.getElementById(strID);

  if ( oCmb && oCmb.options )
  {
    if ( oCmb.selectedIndex && oCmb.options[oCmb.selectedIndex].label )
    {
      if ( strLabel == oCmb.options[oCmb.selectedIndex].label )
        return true;
    }

    for(i = 0; i < oCmb.length; i++)
    {
      if ( strLabel == oCmb.options[i].label )
      {
        oCmb.options[i].selected = true;
        return true;
      }
    }
  }

  return false;
}


function SetFocus(strID)
{
  var o = document.getElementById(strID);

  if ( o )
    o.focus();
}


function SetOnFocusHlp(o, strName, strTip)
{
  if( o )
  {
    if ( StartWith(o.id, strName) && o.value == strTip )
      o.value = "";
  }
}


function AddEvent(o, strEvent, oFunction)
{
  if ( o ) 
  {
    if ( o.addEventListener )
    {
      o.addEventListener(strEvent, oFunction, false);
    }
    else
    {
      if ( window.attachEvent )
        o.attachEvent(("on" + strEvent), oFunction);
    }
  }
}

function RemoveEvent(o, strEvent, oFunction)
{
  if ( o )
  {
    if ( o.removeEventListener )
    {
      o.removeEventListener(strEvent, oFunction, false);
    }
    else
    {
      if ( window.detachEvent )
        o.detachEvent(("on" + strEvent), oFunction)
    }
  }
}


//----------------------------------------------------------------------base64

//g_Base64.encode(str);
//g_Base64.decode(str);
var g_Base64 =
{
  // private property
  _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

  // public method for encoding
  encode : function (input)
  {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;

    input = g_Base64._utf8_encode(input);

    while (i < input.length)
    {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if ( isNaN(chr2) )
        enc3 = enc4 = 64;
      else
      {
        if ( isNaN(chr3) )
          enc4 = 64;
      }

      output = output + this._keyStr.charAt(enc1)
                      + this._keyStr.charAt(enc2)
                      + this._keyStr.charAt(enc3)
                      + this._keyStr.charAt(enc4);
    }

    return output;
  },

  // public method for decoding
  decode : function (input)
  {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length)
    {
      enc1 = this._keyStr.indexOf(input.charAt(i++));
      enc2 = this._keyStr.indexOf(input.charAt(i++));
      enc3 = this._keyStr.indexOf(input.charAt(i++));
      enc4 = this._keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64)
        output = output + String.fromCharCode(chr2);

      if (enc4 != 64)
        output = output + String.fromCharCode(chr3);
    }

    output = g_Base64._utf8_decode(output);

    return output;
  },

  // private method for UTF-8 encoding
  _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;
  },

  // private method for UTF-8 decoding
  _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;
  }
}

//----------------------------------------------------------------------------

function writeHistory(message)
{
  var oLog = document.getElementById("js_log");
  if ( oLog )
  {
    var d = new Date();
    oLog.appendChild(document.createTextNode(d + "-> " + message));
    oLog.appendChild(document.createElement('BR'));
    oLog.scrollTop += 50;
  }
}


function email_check(str)
{
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
     alert("Invalid E-mail ID")
     return false
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
  }

  if (str.indexOf(at,(lat+1))!=-1){
      return false
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
  }

  if (str.indexOf(dot,(lat+2))==-1){
      return false
  }

  if (str.indexOf(" ")!=-1){
     return false
  }

  return true
}


function urlDecode(str) {
    str=str.replace(new RegExp('\\+','g'),' ');
    return unescape(str);
}

function urlEncode(str){
    str=escape(str);
    str=str.replace(new RegExp('\\+','g'),'%2B');
    return str.replace(new RegExp('%20','g'),'+');
}


//----------------------------------------------------------------------------

var constUnQuote_quot  = new RegExp("\\&\\#34;" , 'g');
var constUnQuote_quot2 = new RegExp("\\&quot;"  , 'g');
var constUnQuote_gt    = new RegExp("\\&\\#62;" , 'g');
var constUnQuote_gt2   = new RegExp("\\&gt;"    , 'g');
var constUnQuote_lt    = new RegExp("\\&\\#60;" , 'g');
var constUnQuote_lt2   = new RegExp("\\&lt;"    , 'g');
var constUnQuote_apos  = new RegExp("\\&\\#39;" , 'g');
var constUnQuote_amp   = new RegExp("(\\&\\#38;|\\&amp;)", 'g');

var constQuote_quot = new RegExp("\\\"", 'g');
var constQuote_gt   = new RegExp("\\>" , 'g');
var constQuote_lt   = new RegExp("\\<" , 'g');
var constQuote_apos = new RegExp("\\'" , 'g');
var constQuote_amp  = new RegExp("\\&" , 'g');

function UnQuote_smart(str)
{
  if ( str )
  {
    str = str.replace(constUnQuote_quot , '"');
    str = str.replace(constUnQuote_quot2, '"');
    str = str.replace(constUnQuote_gt   , '>');
    str = str.replace(constUnQuote_gt2  , '>');
    str = str.replace(constUnQuote_lt   , '<');
    str = str.replace(constUnQuote_lt2  , '<');
    str = str.replace(constUnQuote_apos , "'");
    str = str.replace(constUnQuote_amp  , '&');
  }
  else
    str = "";//null not returns !

  return str;
}


function Quote_smart(str)
{
  if ( str )
  {
    str = str.replace(constQuote_amp , "&#38;");
    str = str.replace(constQuote_apos, "&#39;");
    str = str.replace(constQuote_lt  , "&#60;");
    str = str.replace(constQuote_gt  , "&#62;");
    str = str.replace(constQuote_quot, "&#34;");
  }
  else
    str = "";//null not returns !

  return str;
}


function HTMLasText(str)
{
  if ( str )
  {
    str = str.replace(constQuote_amp , "&amp;" );
    str = str.replace(constQuote_apos, "&#39;" );
    str = str.replace(constQuote_lt  , "&lt;"  );
    str = str.replace(constQuote_gt  , "&gt;"  );
    str = str.replace(constQuote_quot, "&quot;");
  }

  return str;
}


//----------------------------------------------------------------------------

function IntR(strHex) { return parseInt(strHex.substr(0, 2), 16); }
function IntG(strHex) { return parseInt(strHex.substr(2, 2), 16); }
function IntB(strHex) { return parseInt(strHex.substr(4, 2), 16); }

function To2Hex(value)
{
  var v = parseInt(value);

  if ( true == isNaN(v) )
    v = 0;

  var hexes = "0123456789ABCDEF";

  return hexes.charAt(Math.floor(v / 16) % 16)
         +
         hexes.charAt(v % 16);
}


// clrFilter - hex string like "C0C0C0"
// clrBkg    - hex string like "C0C0C0"
// 0 <= alpha <= 1
//
// return hex string like "C0C0C0"
//
function BlendClrs(clrFilter, clrBkg, alpha)
{
  var alpha_1 = 1 - alpha;

  return   To2Hex(alpha * IntR(clrFilter) + alpha_1 * IntR(clrBkg))
         + To2Hex(alpha * IntG(clrFilter) + alpha_1 * IntG(clrBkg))
         + To2Hex(alpha * IntB(clrFilter) + alpha_1 * IntB(clrBkg));
}


//clr... is hex string like "C0C0C0"
//
//return (clrFrom + (clrFrom - clrTo) * nStep / NSteps)
//
function GotoClr(clrFrom, clrTo, nStep, NSteps)
{
  var n = new Number(nStep);
  var N = new Number(NSteps);

  if ( isNaN(n) || isNaN(N) || 0 == N )
    return clrFrom;

  return   To2Hex(IntR(clrFrom) + (IntR(clrTo) - IntR(clrFrom)) * n / N)
         + To2Hex(IntG(clrFrom) + (IntG(clrTo) - IntG(clrFrom)) * n / N)
         + To2Hex(IntB(clrFrom) + (IntB(clrTo) - IntB(clrFrom)) * n / N); 
}

//----------------------------------------------------------------------------

function AskAndGoToURL(strAsk, strGoURL)
{
  var answer = confirm(strAsk);

  if (answer)
    window.location = strGoURL;
}


function AskAndGoToURL64(strAsk, strGoURL)
{
  strAsk = g_Base64.decode(strAsk);

  AskAndGoToURL(strAsk, strGoURL);
}

//** (END OF FILE  : hlpfuncs.js) ********************************************

