var constSpace = "\n \t",
    constDigit = "0123456789",
    constEnglishUp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    constEnglishLow = "abcdefghijklmnopqrstuvwxyz";

function ltrim(xxx) {
  if(constSpace) {
    while((constSpace.indexOf(xxx.charAt(0)) > -1) && (xxx.length > 0)) {
      xxx = xxx.substring(1);
    }
  }
  return xxx;
}

function rtrim(xxx) {
  if(constSpace) {
    while((constSpace.indexOf(xxx.charAt(xxx.length - 1)) > -1) && (xxx.length > 0)) {
      xxx = xxx.substring(0, xxx.length - 1);
    }
  }
  return xxx;
}

function trim(xxx) {
  return rtrim(ltrim(xxx));
}

function isDigit(sSource) {
  var bOk = false;
  if(sSource) {
    var sBuf = new String(sSource);
    bOk = check_in(sSource, constDigit);
  }
  return bOk;
}

function isSymbol(sSource) {
  var bOk = false;
  if(sSource) {
    var sBuf = new String(sSource);
    bOk = check_in(sSource, constEnglishUp + constEnglishLow);
  }
  return bOk;
}

function check_in(sSource, sShablon) {
  var bOk = true;
  if(sSource && sShablon) {
    for(i = 0; i < sSource.length; i++) {
      if(sShablon.indexOf(sSource.charAt(i)) == -1) {
        bOk = false;
        break;
      }
    }
  }
  return bOk;
}

function getIndex(list, value) {
  var result = -1;
  if(list) {
    if(list.options) {
      for(var i = 0; i < list.options.length; i++) {
        if(list.options[i].value == value) return i;
      }
    } else {
      for(var i = 0; i < list.length; i++) {
        if(list[i] == value) return i;
      }
    }
  }
  return result;
}

function str_replace(source, from, to) {
  var buf = new String(source);
  while(buf.indexOf(from) != -1) {
    buf = buf.replace(from, to);
  }
  return buf;
}

function getChecked(list) {
  var ik = 0;
  if(list) {
    if(list.length) {
      for(var i = 0; i < list.length; i++) {
        if(list[i].checked) ik++;
      }
    } else {
      if(list.checked) return 1;
    }
  }
  return ik;
}

function getByValue(list, val) {
  var res = null;
  if(list) {
    if(list.length) {
      for(var i = 0; i < list.length; i++) {
        if(list[i].value == val) {
          res = list[i]
          break;
        }
      }
    } else {
      if(list.value == val) return 1;
    }
  }
  return res;
}

function checkAll(list, value) {
  try {
    if(list) {
      if(list.length) {
        for(var i = 0; i < list.length; i++)
          list[i].checked = (value) ? true : false;
      } else {
        list.checked = (value) ? true : false;
      }
    }
  } catch (err) {
    alert("Error in 'checkAll'\n" + err);
  }
}


function getElementsById(param, obj, prefix) {
  var list = new Array();
  var listAll;
  var j = 0;
  var buf = new String();
  if(document.all) {
    listAll = (obj) ? obj.all : document.all;
  } else {
    listAll = (obj) ? obj.getElementsByTagName("*") : document.getElementsByTagName("*");
  }
  if(!obj) return null;
  for(var i = 0; i < listAll.length; i++) {
    if(listAll[i].getAttribute("id")) {
      buf = '' + listAll[i].getAttribute("id");
      if(prefix) {
        if(buf.indexOf(param) == 0)  list[j++] = listAll[i];
      } else {
        if(buf == param) list[j++] = listAll[i];
      }
    }
  }
  return list;
}

function isUpChar(ch) {
  return (constEnglishUp.indexOf(ch) != -1);
}

function isLowChar(ch) {
  return (constEnglishLow.indexOf(ch) != -1);
}

function emailCheck(emailStr) {
  var emailPat = /^(.+)@(.+)$/;
  var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  var validChars = "\[^\\s" + specialChars + "\]";
  var quotedUser = "(\"[^\"]*\")";
  var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom = validChars + '+';
  var word = "(" + atom + "|" + quotedUser + ")";
  var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
  var matchArray = emailStr.match(emailPat);
  if(matchArray == null) return false;
  var user = matchArray[1];
  var domain = matchArray[2];

  if(user.match(userPat) == null)
    return false;
  var IPArray = domain.match(ipDomainPat);
  if(IPArray != null) {
    for(var i = 1; i <= 4; i++)
      if(IPArray[i] > 255) return false;
    return true;
  }
  var domainArray = domain.match(domainPat);
  if(domainArray == null)
    return false;
  var atomPat = new RegExp(atom, "g");
  var domArr = domain.match(atomPat);
  var len = domArr.length;
  if(domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 3)
    return false;
  if(len < 2)
    return false;
  return true;
}

function setGlobalOnLoad(f) {
  var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null
  if(root) {
    if(root.addEventListener) root.addEventListener("load", f, false)
    else if(root.attachEvent) root.attachEvent("onload", f)
  } else {
    if(typeof window.onload == 'function') {
      var existing = window.onload
      window.onload = function() {
        existing()
        f()
      }
    } else {
      window.onload = f
    }
  }
}

function Get_Cookie(name) {
  var start = document.cookie.indexOf(name + "=");
  var len = start + name.length + 1;
  if(( !start ) && ( name != document.cookie.substring(0, name.length))) {
    return null;
  }
  if(start == -1) return null;
  var end = document.cookie.indexOf(";", len);
  if(end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len, end));
}
/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie(name, value, expires, path, domain, secure) {
  var today = new Date();
  today.setTime(today.getTime());
  // if the expires variable is set, make the correct expires time, the
  // current script below will set it for x number of days, to make it
  // for hours, delete * 24, for minutes, delete * 60 * 24
  if(expires) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  //alert( 'today ' + today.toGMTString() );// this is for testing purpose only
  var expires_date = new Date(today.getTime() + (expires));
  //alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

  document.cookie = name + "=" + escape(value) +
                    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
                    ( ( path ) ? ";path=" + path : "" ) +
                    ( ( domain ) ? ";domain=" + domain : "" ) +
                    ( ( secure ) ? ";secure" : "" );
}
function Delete_Cookie(name, path, domain) {
  if(Get_Cookie(name))
    document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +
                      ( ( domain ) ? ";domain=" + domain : "" ) +
                      ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function Check_Cookie() {
  Set_Cookie('test', 'none', '', '/', '', '');
  if(Get_Cookie('test')) {
    Delete_Cookie('test', '/', '');
    return true;
  }
  return false;
}