<!--
/*
  *** USAGE OF setQueryStringValuesInForm() function ***
  NOTE: the names 'myelement1' and 'myelement2' are EXAMPLE names of form elements. these names should not be used 
        (unless you explicitely require this).

  put the call to the function in the onLoad of the page, i.e.:
  <body onLoad="setQueryStringValuesInForm('myelement1', 'myelement2');">

  optional: you may also add the function to other events if desired, for instance a button:
  <button onClick="setQueryStringValuesInForm('myelement1', 'myelement2');">reset form</button>

  also link this file as a javascript by adding a <script>-block the usual way:
  <script language="javascript" type="text/javascript" src="/cms/includes/javascript/querystring_functions.js"></script>

  make sure that every element of the form has a name-attribute. 
  (the name must not be shared between elements of different types. in other words:
  do not use a checkbox group AND a radiobutton group with the same name or something similar. 
  this is very unlikely but should be taken into account)

  note that for this function to work method="GET" should be used. 

  The function setQueryStringValuesInForm takes a variable number of parameters. Each parameter specifies
  the name of a form element or group of form elements which should be set. (ONLY!) The specified elements will
  be set. They will be procecessed in the given order. In most cases the order is unimportant however in some cases it is
  important (i.e. when using the subselects script). 

*/












//---------------------------------------------------------
//---------------------------------------------------------





  /*
  ' function  : arrayOfQueryString
  ' overview  : returns an associative array of the current querystring
  ' arguments : [in] sQueryStringToUse, if empty the current querystring is used else this one is used
  ' returns   : associative array
  ' example   :
  '             myQS = arrayOfQueryString();
  '             alert('The current pageID is: ' + myQS['pageid']);
  */
  function arrayOfQueryString(sQueryStringToUse)
  {
    // retrieve the querystring
    var sQueryString;
    if ((sQueryStringToUse == '') || (!sQueryStringToUse))
    {
      sQueryString = location.search;
    }
    else
    {
      sQueryString = sQueryStringToUse;
    }

    // remove the question mark at the beginning of the querystring
    sQueryString = sQueryString.replace(/^\?/, '');

    // split the querystring in name/value pairs
    var asNameValuePairs = sQueryString.split('&');

    // create the associative querystring array
    var asQueryString = new Array();
    for (var iQueryStringName = 0; iQueryStringName < asNameValuePairs.length; iQueryStringName++)
    {
      // split the name/value pair into a seperate name and a value
      var sNameValuePair = asNameValuePairs[iQueryStringName];
      var sName = sNameValuePair.substring(0, sNameValuePair.indexOf('='));
      var sValue = sNameValuePair.substring(sNameValuePair.indexOf('=') + 1);
      if (decodeURIComponent) {
        sValue = decodeURIComponent(sValue.replace(/\+/g, '%20')); } else {
        sValue = unescape(sValue.replace(/\+/g, '%20')); }

      // add the querystring value to the array
      if (asQueryString[sName]) {
        if (sValue != '') {
          asQueryString[sName] += ', ' + sValue;
        }
      } else {
        asQueryString[sName] = sValue;
      }
    }

    return asQueryString;
  }



  /*
  ' function  : arrayToQueryString
  ' overview  : creates and returns a querystring from the array
  ' arguments : [in] saQueryStringArray
  ' returns   : querystring including leading question mark
  ' example   : var sHyperlink = 'page.asp' + arrayToQueryString(myQS)
  */
  function arrayToQueryString(saQueryStringArray)
  {
    var sLeadingSymbol = '?';
    var sQueryString = '';

    for (sName in saQueryStringArray)
    {
      if (sName != '')
      {
        sQueryString += sLeadingSymbol + sName + '=' + saQueryStringArray[sName];
        sLeadingSymbol = '&';
      }
    }

    return sQueryString;
  }



  /*
  ' function  : setQueryStringValuesInForm
  ' overview  : reads the values from the querystring and changes all relevant form elements so that they reflect the values from the querystring as far as possible. this way resending the form will restore the values from the current querystring for the supplied values.
  ' arguments : [in], ids of form elements to process first, these will be processed in the given order. this is might be required by some scripts. 
  ' return    :
  ' example   :
  ' note      : after changing each value the onChange event of the element with an id equal the the form element name will be invoked
  */
  function setQueryStringValuesInForm() {
    var saQS = arrayOfQueryString();

    // loop through the querystring
    for (var el_cnt = 0; el_cnt < arguments.length; el_cnt++) {
      var sQSattrib = arguments[el_cnt];
      var sQSattribValue = saQS[sQSattrib];
      var el = document.getElementById(sQSattrib);
      var els = document.getElementsByName(sQSattrib);
      if (els.length > 0) {
        // the type of the first element is considered to be the type of all elements with the same name
        var sTagType;
        if (els[0].tagName.toLowerCase() == 'select') {
          sTagType = 'select';
        } else {
          sTagType = els[0].type.toLowerCase();
        }
        switch (sTagType) {
          case 'select':
            var options = els[0].options;
            for (var iOption = 0; iOption < options.length; iOption++) {
              if ((options[iOption].value == sQSattribValue) || ((options[iOption].value == '') && (options[iOption].innerHTML == sQSattribValue))) {
                options[iOption].selected = true;
              }
            }
            break;
          case 'text':
            els[0].value = sQSattribValue;
            break;
          case 'password':
            els[0].value = sQSattribValue;
            break;
          case 'hidden':
            els[0].value = sQSattribValue;
            break;
          case 'checkbox':
            var els = document.getElementsByName(sQSattrib);
            for (var iChkBox = 0; iChkBox < els.length; iChkBox++) {
              els[iChkBox].checked = ((', ' + sQSattribValue + ', ').indexOf(', ' + els[iChkBox].value + ', ') != -1);
            }
            break;
          case 'radio':
            var rads = document.getElementsByName(sQSattrib);
            for (var iRadBtn = 0; iRadBtn < rads.length; iRadBtn++) {
              if (rads[iRadBtn].value == sQSattribValue) {
                rads[iRadBtn].checked = true;
              }
            }
            break;
        }

        if (el) {
          if (el.onchange) el.onchange();
        }
      } 
    }
  }


-->

