function GetCookieValue(cname)
{
  var cookieValue = "";
  var searchStr = cname + "=";
  if(document.cookie.length > 0)
  {
    offset = document.cookie.indexOf(searchStr);
    if(offset != -1)
    {
      offset += searchStr.length;
      end = document.cookie.indexOf(";", offset);
      if(end == -1)
        end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end));
    }
  }
  return cookieValue;
}

function SetCookieValue(cname, cvalue)
{
  document.cookie = cname + "=" + escape(cvalue) + ";";
}

function InitWindow()
{
  if(window.menubar)
  {//if netscape
    window.menubar.visible = false;
    window.toolbar.visible=false;
    window.locationbar.visible=false;
    window.personalbar.visible=false;
    window.statusbar.visible=false;
  }
  else
  {
    SetCookieValue("loaded", "1", 24*3600); //24 hours cookie

    if(!window.opener)
    {
      window.open(window.location, "pb3", "scrollbars=yes,resizable=yes");
    }
    else
    {
      try
      {
        var op = window.opener;
        op.opener = self;
        op.close();
      }
      catch(er)
      {}
    }
  }
}

function ValidateTextAdv(obj, allowed_chars)
{
  var txt = obj.value;
  if(txt == "")
    return false;
  var newtxt = "";
  for(var i = 0; i < txt.length; i++)
  {
    var found = false;
    for(var j = 0; j < allowed_chars.length; j++)
    {
      if(txt.charAt(i) == allowed_chars.charAt(j))
      {
        found = true;
        break;
      }
    }
    if(found)
      newtxt += txt.charAt(i);
  }

  if(txt != newtxt)
  {
    return false;
  }
  else
  {
    return true;
  }
}//end of ValidateText

function CanSubmit(obj, allowed_chars, msg)
{
  if(allowed_chars == '')
    return true;

  if(ValidateTextAdv(obj, allowed_chars))
    return true;
  else
  {
    alert(msg);
    return false;
  }
}

function ValidateText(obj, allowed_chars)
{
  var txt = obj.value;
  var newtxt = "";
  for(var i = 0; i < txt.length; i++)
  {
    var found = false;
    for(var j = 0; j < allowed_chars.length; j++)
    {
      if(txt.charAt(i) == allowed_chars.charAt(j))
      {
        found = true;
        break;
      }
    }
    if(found)
      newtxt += txt.charAt(i);
  }

  if(txt != newtxt)
    obj.value = newtxt;
}//end of ValidateText

function GoToURL(trgt, loc)
{
  eval(trgt + ".location='" + loc + "'");
}//end of GoToURL

function GoToUrlConfirm(message, trgt, loc)
{
  if(confirm(message))
  {
    GoToURL(trgt, loc);
  }
}//end of GoToUrlConfirm

function InputBox(msg, def_val, retvalname, destination, additional_vars)
{
  var rez = prompt(msg, def_val);
  if(rez)
  {
    GoToURL('this', destination + "?" + retvalname + "=" + rez + "&" + additional_vars);
  }
}//End of InputBox

function CheckTxt(txt, allowed_chars)
{
  var newtxt = "";
  for(var i = 0; i < txt.length; i++)
  {
    var found = false;
    for(var j = 0; j < allowed_chars.length; j++)
    {
      if(txt.charAt(i) == allowed_chars.charAt(j))
      {
        found = true;
        break;
      }
    }
    if(found)
      newtxt += txt.charAt(i);
  }

  if(txt != newtxt)
    return false;
  return true;
}

function InputBoxAdv(msg, def_val, retvalname, destination, additional_vars, allowed_chars)
{
  var txt = prompt(msg, def_val);

  var b = CheckTxt(txt, allowed_chars);
  if(!b)
  {
    alert("An illeagal charakters were found. Please change text.\nCharakters allowed: '" + allowed_chars + "'");
  }
  else
  {
    var dst = destination + "?" + retvalname + "=" + txt + "&" + additional_vars;
    GoToURL('parent', dst);
  }
}//End of InputBox

function OpenPopupWindow(theUrl, winName, features)
{
  if(window.menubar)
    window.open(theUrl, winName, features);
  else
  {
		var i;
		var nWndName = "";
		for(i = 0; i < winName.length; i++)
		{
			var c = winName.charAt(i);
			if((c >= '0' && c <= '9') ||
		     (c >= 'a' && c <= 'z') ||
				 (c >= 'A' && c <= 'Z'))
			{
				nWndName += c;
			}
		}

		//alert("f='" + features + "'");
		
    var wnd = window.open(theUrl, nWndName, features);
		if(wnd)
			wnd.focus();
  }
}

function SetParentControlText(obj, txt)
{
  if(obj)
    obj.value = txt;
  else
    alert('no object');
}

//////////////////////// INFUNCS //////////////////////////
var pubvars = Array();
function SetVar(vname, vvalue)
{
  pubvars[vname] = vvalue;
}

function GetVar(vname)
{
  return pubvars[vname];
}
///////////////////////////////////////////////////////////////

function GenerateRandomPwd(objname, available_chars, pwd_len)
{
  document.getElementById(objname).value = "";
  var len = available_chars.length;
  var i = 0;
  for(i = 0; i < pwd_len; i++)
  {
    var rnum = Math.round(Math.random() * len);
    var txt = document.getElementById(objname).value;
    txt += available_chars.charAt(rnum);
    document.getElementById(objname).value = txt;
  }
}

function GetRadioItemValue(radioGrp)
{
  if(radioGrp.length > 0)
  {
    for(var i = 0; i < radioGrp.length; i++)
    {
      if(radioGrp[i].checked)
      {
        return radioGrp[i].value;
      }
    }
  }
  else if(radioGrp.checked)
  {
    return radioGrp.value;
  }
  return "";
}

function Trim(trim_str)
{
  if(trim_str.length < 1)
    return"";

  trim_str = RTrim(trim_str);
  trim_str = LTrim(trim_str);

  if(trim_str=="")
    return "";
  return trim_str;
}

function RTrim(trim_str)
{
  var chr_space = " ";
  var chr_tab = "\t";
  var chr_lf = "\n";
  var chr_cr = "\r";

  var s_length = trim_str.length;
  var strTemp = "";
  if(s_length < 0)
    return "";

  var i = s_length - 1;

  while(i > -1)
  {
    if(trim_str.charAt(i) != chr_space &&
        trim_str.charAt(i) != chr_tab &&
        trim_str.charAt(i) != chr_lf &&
        trim_str.charAt(i) != chr_cr)
    {
      strTemp = trim_str.substring(0, i + 1);
      break;
    }
    i--;
  } //End While
  return strTemp;
}

function LTrim(trim_str)
{
  var chr_space = " ";
  var chr_tab = "\t";
  var chr_lf = "\n";
  var chr_cr = "\r";

  var s_length = trim_str.length;

  if(s_length < 1)
    return "";

  var strTemp = "";

  var i = 0;

  while(i < s_length)
  {
    if(trim_str.charAt(i) != chr_space &&
      trim_str.charAt(i) != chr_tab &&
      trim_str.charAt(i) != chr_lf &&
      trim_str.charAt(i) != chr_cr)
    {
      strTemp = trim_str.substring(i, s_length);
      break;
    }
    i++;
  } //End While

  return strTemp;
}

function Replace(search_str, replacement_str, expression)
{
  expression = expression.replace(search_str, replacement_str);
  return expression;
}

function RegReplace(regexpr, replacement_txt, expression)
{
  var re = new RegExp(regexpr, "gi");
  expression = expression.replace(re, replacement_txt);
  return expression;
}

function ParseExpression(val, expression)
{
  expression = Trim(expression);
  expression = RegReplace("[ \r\n\t]", "", expression);
  expression = RegReplace("value", val, expression);
  expression = RegReplace("&lt;", "<", expression);
  expression = RegReplace("&gt;", ">", expression);
  expression = RegReplace("&eq;", "=", expression);

  var rezult = eval(expression);
  return rezult;
}

//var opt_texts = new Array("text1", "text2");
//var err_text = "Bad values input in \n%s. \nYou must type correct values in order to continue!";
//var check_elements = new Array("key1", "key2")
//var conditions = new Array("var1", "var2")
function CheckExpressions(check_elements, conditions, opt_texts, err_text, check_questions)
{
  var bad_elements = "";
  for(var i = 0; i < check_elements.length; i++)
  {
    var element_id = check_elements[i];
    var condition = conditions[i];
    var element_value = document.getElementById(element_id).value;

    if(element_value != "")
    {
      var condition_type = condition.substring(0, 1);
      if(condition_type == '(') //words
      {
        //przygotowac do postaci reg!!!
        var str_expression = Replace("(", "", condition);
        str_expression = Replace(")", "", str_expression);
        str_expression = ConvertTextUnicode(str_expression);

        var fnd = -1;

        fnd = str_expression.indexOf('U002c');
        while(fnd != -1)
        {
          str_expression = Replace("U002c", "\\b|\\b", str_expression);
          fnd = str_expression.indexOf('U002c');
        }
        str_expression = "\\b" + str_expression + "\\b";

        var element_value_arr = element_value.split(' ');
        for(var e = 0; e < element_value_arr.length; e++)
          element_value_arr[e] = Trim(element_value_arr[e]);

        //ok.. we have an array of words, so we check if they are ok
        for(var j = 0; j < element_value_arr.length; j++)
        {
          element_value_arr[j] = ConvertTextUnicode(element_value_arr[j]);
          str_chk = RegReplace(str_expression, "", element_value_arr[j]);

          if(str_chk != "")
          {
             bad_elements = bad_elements + check_questions[i];
		if(opt_texts[i].length > 1)
		{
			bad_elements = bad_elements + " - '" + opt_texts[i] + "'";
		}
		bad_elements = bad_elements + "\n";
            break;
          }
        }
        //ugly eagle shit horse cow
      }
      else if(condition_type == '[') //range
      {
        var str_chk = RegReplace(condition, "", element_value);
        if(str_chk != "")
        {
          bad_elements = bad_elements + check_questions[i];
          if(opt_texts[i].length > 1)
          {
          	bad_elements = bad_elements + " - '" + opt_texts[i] + "'";
	  }
          bad_elements = bad_elements + "\n";
        }
      }
      else if(condition_type == '{') //numeric expression
      {
        var str_expression = RegReplace("{", "", condition);
        str_expression = RegReplace("}", "", str_expression);
        str_expression = RegReplace("&gt;", ">", str_expression);
        str_expression = RegReplace("&lt;", "<", str_expression);
        str_expression = RegReplace("&eq;", "=", str_expression);

        try
        {
          if(!ParseExpression(element_value, str_expression))
          {
             bad_elements = bad_elements + check_questions[i];
		if(opt_texts[i].length > 1)
		{
			bad_elements = bad_elements + " - '" + opt_texts[i] + "'";
		}
		bad_elements = bad_elements + "\n";
          }
        }
        catch(err)
        {
          alert(err.description);
          return false;
        }
      }
    }
  }

  if(bad_elements != "")
  {
    err_text = Replace("%s", bad_elements, err_text);
    alert(err_text);
    return false;
  }

  //return false;
  return true;
}

function ConvertTextUnicode(source)
{
  result = '';
  for (i=0; i<source.length; i++)
  {
    result += source.charCodeAt(i).toString(16) + '~';
  }

  var slashes=result.split("~");
  var buildU = "";
  var finalU = "";

  for(i=0; i < slashes.length-1; i++)
  {
  //alert(slashes[i].length)
    switch(slashes[i].length)
    {
      case 1:
        buildU="U000"+slashes[i]
        break
      case 2:
        buildU="U00"+slashes[i]
        break
      case 3:
        buildU="U0"+slashes[i]
        break
      case 4:
        buildU="U"+slashes[i]
        break
    }
    finalU += buildU;
  }
  return finalU;
}

function CheckFileName(id, msg)
{
  var el = document.getElementById(id);
  var str = el.value;
  if(str.length == 0)
  {
    alert(msg);
    return false;
  }

  var allowed_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_./\\1234567890:- ";
  for(var i = 0; i < str.length; i++)
  {
    var is_ok = false;
    for(var j = 0; j < allowed_chars.length; j++)
    {
      if(allowed_chars.charAt(j) == str.charAt(i))
      {
        is_ok = true;
        break;
      }
    }

    if(is_ok == false)
    {
      alert(msg);
      return false;
    }
  }

  return true;
}

function AdjustPopImgSize(obj)
{
  var docwidth = self.innerWidth ? self.innerWidth : document.body.offsetWidth;

  if(obj.width > docwidth)
  {
    sizeRate = docwidth / obj.width;
    obj.width = obj.width*sizeRate-20;
    if(obj.hight > docwidth)
      obj.hight = docwidth;
  }
}

