// --------------------------------------------------------
//  Browser help functions
//  ver 2.0.7
//
//  Copyright (c) 2000-2003 by KIEV2000.com Production
//  All Rights Reserved
// --------------------------------------------------------

var DlgParam = "RESIZABLE=NO,SCROLLBARS=NO,MENUBAR=NO,STATUS=NO,DIRECTORIES=NO,LOCATION=NO,TOOLBAR=NO"

function MakeDlgParam (w, h, Param)
{
  return "WIDTH=" + w + "px,HEIGHT=" + h + "px,LEFT=" + ((window.screen.width - w) / 2) + "px" + "px,TOP=" + ((window.screen.height - h) / 2) + (Param ? Param : DlgParam)
}

function isspace (ch)
{
  return ((ch == " ") || (ch == "\r") || (ch == "\n") || (ch == "\t") || (ch == "\x00") || (ch == '\xA0'))
}

function isalpha (ch)
{
  return (("A" <= ch) && (ch <= "Z")) || (("a" <= ch) && (ch <= "z"))
}

function isnum (ch)
{
  return ("0" <= ch) && (ch <= "9")
}

function isalnum (ch)
{
  return isalpha (ch) || isnum (ch)
}

function IsNumStr (str)
{
  for (var i = 0 ; i < str.length ; i++)
    if (!isnum (str.charAt (i)))
      return false
  return true
}

function IsURLPart (str)
{
  var AllowCh = "!#$&+-=_^~" // "%'*/?`{|}"
  var i, pos_pt, ch

  for (i = 0, pos_pt = -1 ; i < str.length ; i++)
  {
    ch = str.charAt (i)
    if (ch == ".")
    {
      if (i == pos_pt + 1)
        return false
      pos_pt = i
    }
    else if (!isalnum (ch) && (AllowCh.indexOf (ch) == -1))
      return false
  }
  if (i == pos_pt + 1)
    return false
  return true
}

function IsEMail (str) // ÏÅÐÅÄÅËÀÒÜ !!!
{
  var i
  if ((str.indexOf ("@", 0) != -1) && (str.indexOf (".", 0) != -1))
  {
    i = str.length - str.lastIndexOf (".") - 1
    if (i && (i <= 3) && (str.indexOf ("@", 0) != 0) && ((str.indexOf ("@", 0) + 1) < str.lastIndexOf (".")))
      return true
  }
  return false
}

function TruncSpace (str)
{
  str = String (str)
  var i, j
  for (i = 0 ; (i < str.length) && isspace (str.charAt (i)) ; i++) ;
  for (j = str.length - 1 ; (j > i) && isspace (str.charAt (j)) ; j--) ;
  return str.substring (i, j + 1)
}

function TagCheck (str)
{
  var i, j
  for (i = str.indexOf ("<", 0) ; i != -1 ; i = str.indexOf ("<",0))
  {
    j = str.indexOf (">", i)
    if (j == -1)
      j = str.length
    else 
      j++
    str = str.substring (0, i) + str.substring (j, str.length)
  }
//  for (i = 0 ; i < str.length ; i++)
//    if (str.charAt (i) == "'")
//      str = str.substring (0,i) + "\"" + str.substring (i + 1, str.length)
  return str
}

function CheckStr (str)
{
  return TagCheck (TruncSpace (str))
}

function EditTruncSpace (edit)
{
  edit.value = TruncSpace (edit.value)
}

function EditTagCheck (edit)
{
  edit.value = TagCheck (edit.value)
}

function EditCheckStr (edit)
{
  edit.value = CheckStr (edit.value)
}

function EditCheckNumKey (Flags)
{
  var ret = false
  var edit

  if ((event.keyCode >= 48) && (event.keyCode <= 57))
    ret = true

  if (Flags && (event && event.srcElement && event.srcElement.tagName == "INPUT"))
  {
    edit = event.srcElement

    if ((Flags & 0x01) && event.keyCode == 46)
      ret = edit.value.indexOf ('.') == -1 ? true : false

    if ((Flags & 0x02) && event.keyCode == 45)
    {
      if (edit.value.charAt (0) == '-')
        edit.value = edit.value.substr (1)
      else
        edit.value = "-" + edit.value
    }
  }

  event.returnValue = ret
}

function FormArray (str, delim)
{
  str = String (str)
  var ret = new Array ()
  var i, j
  for (i = 0, j = str.indexOf (delim) ; j != -1 ; i = j + delim.length , j = str.indexOf (delim, i))
    ret[ret.length] = str.substring (i, j)
  if (i < str.length)
    ret[ret.length] = str.substr (i)
  return ret
}

function Random (max)
{
  return parseInt (Math.random () * (max + 1))
//  return Math.ceil (Math.random () * (max + 1)) - 1
}

function GenPassword (l)
{
  var ret = ""
  if (isNaN (l = parseInt (l))) 
    l = 8
  for (var i = 0 ; i < l ; i++)
  {
    if (Random (2))
      ret += String.fromCharCode ((Random (1) ? 65 : 97) + Random (25))
    else
      ret += Random (9)
  }
  return ret
}

function GetBRStr (str)
{
  var ret = ""
  var i, j, ch, b
  for (i = 0, j = 0 ; i < str.length ; i++)
    if (((b = ((ch = str.charAt (i)) == "\r")) && (str.charAt (i + 1) == "\n")) || (ch == "\n"))
    {
      ret += str.substring (j, i) + "<BR>"
      i++
      if (b)
        j = i + 1
      else
        j = i
    }
  return ret + str.substring (j, i)
}

function SetCookie (Name, Value, ExpireMonth)
{
  dt = new Date ()
  dt.setMonth (dt.getMonth () + (ExpireMonth ? ExpireMonth : 3))
  document.cookie = Name + "=" + escape (Value) + "; expires=" + dt.toGMTString ()
}

function GetCookie (Name)
{
  var Cookie = document.cookie.split ("; ")
  for (var i = 0 ; i < Cookie.length ; i++)
  {
    var Crumb = Cookie[i].split ("=")
    if (Name == Crumb[0]) 
      return unescape (Crumb[1])
  }
  return null
}

function DelCookie (Name)
{
  document.cookie = Name + "=;expires=Fri, 31 Dec 1999 23:59:59 GMT"
}

function SearchParentByTag (obj, tag)
{
  for (obj = obj.parentElement ; obj && (obj.tagName != tag) ; obj = obj.parentElement) ;
  return obj
}
