//<!--

/******************************************************************************
 File      : FORMPARS.JS, form parsing routines with cookie functions
 Version   : 1.02; mar 19, 2002
 Created by: Serge Settels, Global Home Views, Breda, Netherlands
 Copyright : (c) 1999-2002 Global Home Views BV

 History   : 
   1.02; mar 19, 2002
         ParseVarsIntoForm: Added support for select-multiple
   1.01; feb 21, 2002
 ******************************************************************************/

//if (top.location == self.location)
//{top.location.replace('/go.htm?'+self.location.pathname+self.location.search);}

function debug(s)
//generic function to put output to a debug window
{
  outputwindow = open("","output");
  outputwindow.document.write(s+"<br>");
}

function IntToStr(value)
//creates an string with thousand-separators
{
  result = new String;
  str    = new String(value);

  for (var i = str.length-1; i >= 0; i--)
  {
    if ((str.length-i-2)%3==2)
      result = str.substring(i,i+1) + '.' + result;
    else
      result = str.substring(i,i+1) + result;
  }
  return result;
}

function trim(s)
//removes trailing and leading spaces. Need as a hack because cookie.split creates spaces.
{
  while ((s.charAt(0) == " ")          && (s != "")) s = s.substring(1,s.length);
  while ((s.charAt(s.length-1) == " ") && (s != "")) s = s.substring(0,s.length-2);
  return s
}

function indexOfName(name, a)
//finds first instance of name in name=value in array a
{
  for(var i=0;i<a.length;++i)
  {
    if (a[i]||"" != "")
     if (trim(a[i].split("=")[0].toLowerCase()) == trim(name).toLowerCase())
      return i
  }
  return -1
}

function indexOf(value, a)
//finds first instance of value in array a
{
  for(var i=0;i<a.length;++i)
  {
    if (a[i]==value)
      return i
  }
  return -1
}

function setVar(vars, name, value)
//appends or creates name=value in the vars array
{
  var i = indexOfName(name, vars);

  if (i >= 0)
    vars[i] = vars[i] + ',' + value
  else
    vars[vars.length] = name + "=" + value;
}

function getVar(vars, name)
//retrieves value from name=value in the vars array
{
  var i = indexOfName(name, vars);

  if (i >= 0)
  {
    s = vars[i].split("=")[1];

    if (s)
    {
    s = unescape(s);
    }

    return(s);
  }
}

function isValueSelected(vars, name, value)
//retrieves value from name=value in the vars array
{
  var i = indexOfName(name, vars);

  if (i>= 0)
  {
    var s = vars[i].split("=")[1].split(",");

    for (var x=0;x<=s.length;++x)
      if (s[x] == value)
        return true
  }
  else
    return false
}

//BEGIN OF FORM PARSING ROUTINES

function parseVarsIntoFormExcludeHidden(vars, f)
//parse all form fields and set their values from the vars array
{
  for(var i=0;i<f.elements.length;++i)
  {
    e = f.elements[i];

    //is there an option linked
    var j = indexOfName(e.name.toLowerCase(), vars)

    if (j > -1)
    {
      var s = vars[j].split("=")[1];

      s = unescape(s);

      if ((e.type == "text"))
      {
        e.value = s;
      } else

      if (e.type == "select-one")
      {
        s = s.toLowerCase();
        for (var k=0;k < e.options.length;k++)
        if (e.options[k].value.toLowerCase() == s)
          e.selectedIndex = k;
      } else

      if (e.type == "select-multiple")
      {
        for (var k=0;k < e.options.length;k++)
        if (indexOf(e.name.toLowerCase()+'='+e.options(k).value.toLowerCase(), vars) != -1)
          e.options(k).selected = true;
      } else

      if ((e.type == "radio") || (e.type == "checkbox"))
      {
       if (e.value != "")
       {
        //is it checked (same code as isValueSelected)
        var s2 = s.split(",");
        var s3 = e.value.split(",");
        for (var x=0;x<s2.length;++x)
          if (s2[x].toLowerCase() == s3[0].toLowerCase())
          {
            e.checked = true

            //HACK: manually call the onclick-event to update the pricelists
            if (e.onclick)
              e.onclick();
          }
        }
      }
    }
  }
}

function parseVarsIntoForm(vars, f)
//parse all form fields and set their values from the vars array
{
  for(var i=0;i<f.elements.length;++i)
  {
    e = f.elements[i];

    //is there an option linked
    var j = indexOfName(e.name.toLowerCase(), vars)

    if (j > -1)
    {
      var s = vars[j].split("=")[1];

      s = unescape(s);

      if ((e.type == "text") || ((e.type.toLowerCase() == "hidden") && (e.name.substring(0,1).toUpperCase() == "Q")))
      {
        e.value = s;
      } else

      if (e.type == "select-one")
      {
        s = s.toLowerCase();
        for (var k=0;k < e.options.length;k++)
        if (e.options[k].value.toLowerCase() == s)
          e.selectedIndex = k;
      } else

      if (e.type == "select-multiple")
      {
        for (var k=0;k < e.options.length;k++)
        if (indexOf(e.name.toLowerCase()+'='+e.options(k).value.toLowerCase(), vars) != -1)
          e.options(k).selected = true;
      } else

      if ((e.type == "radio") || (e.type == "checkbox"))
      {
       if (e.value != "")
       {
        //is it checked (same code as isValueSelected)
        var s2 = s.split(",");
        var s3 = e.value.split(",");
        for (var x=0;x<s2.length;++x)
          if (s2[x].toLowerCase() == s3[0].toLowerCase())
          {
            e.checked = true

            //HACK: manually call the onclick-event to update the pricelists
            if (e.onclick)
              e.onclick();
          }
        }
      }
    }
  }
}

function parseFormIntoVars(f, vars)
//parse all form elements and read their values into the vars array
{
  for(var i=0;i<f.elements.length;++i)
  {
    var e = f.elements[i];

    if ((e.type == "text") || (e.type == "hidden"))
      {
      if (e.value.length > 0)
        setVar(vars, e.name, e.value)
      }
    else

    if (e.type == "select-one")
      {
      if (e.selectedIndex > 0)
        setVar(vars, e.name, e.options[e.selectedIndex].value);
      }
    else

    if ((e.type == "radio") || (e.type == "checkbox"))
      {
      if (e.checked)
        setVar(vars, e.name, e.value)
      }
  }
}

function createQueryString(vars)
{
  //do not return an empty string, must know the query-parameter is passed 
  if (vars.length == 0) return "all";

  var s = "";
  for(var i=0; i < vars.length; ++i)
    s = s + vars[i].split("=")[1] + "+";
  return s.substring(0,s.length-1)
}

function PrepareGoBack()
//generic function to return to last query page
{
  setCookie("lastQpage",location.href,'','/');
}

function dogoBack()
//generic function to return to last query page
{
//  location.href=getCookie("lastQpage");
  history.go('q.exe');

}



// -->
