/* script provided for free by http://www.howtocreate.co.uk */

function getFormString( formRef ) {
  
  for( var x = 0, oStr = '', y = false; formRef.elements[x]; x++ ) {
    if( formRef.elements[x].type ) {
      var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
      if( oT == 'text' || oT == 'textarea' || ( oT == 'password' && oAndPass ) ) {
        oStr += ( y ? ',' : '' ) + oE.value.replace(/%/g,'%p').replace(/,/g,'%c');
        y = true;
      } else if( oT == 'radio' || oT == 'checkbox' ) {
        oStr += ( y ? ',' : '' ) + ( oE.checked ? '1' : '' );
        y = true;
      } else if( oT == 'select-one' ) {
        oStr += ( y ? ',' : '' ) + oE.selectedIndex;
        y = true;
      }
    }
  }
  return oStr;
}

function recoverInputs( formRef, oStr, oAndPass, oTypes, oNames ) {
  if( oStr ) {
    oStr = oStr.split( ',' );
    for( var x = 0, y = 0; formRef.elements[x]; x++ ) {
      if( formRef.elements[x].type ) {
        var oE = formRef.elements[x]; var oT = oE.type.toLowerCase();
        if( oT == 'text' || oT == 'textarea' || ( oT == 'password' && oAndPass ) ) {
          oE.value = oStr[y].replace(/%c/g,',').replace(/%p/g,'%');
          y++;
        } else if( oT == 'radio' || oT == 'checkbox' ) {
          oE.checked = oStr[y] ? "checked" : "";
          y++;

        } else if( oT == 'select-one' ) {
          if (parseInt(oStr[y]) < oE.options.length) oE.selectedIndex = parseInt( oStr[y] );
          y++;
        }
      }
    }
  }
}

function retrieveCookie( cookieName ) {
  /* retrieved in the format
  cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
  only cookies for this domain and path will be retrieved */
  var cookieJar = document.cookie.split( "; " );
  for( var x = 0; x < cookieJar.length; x++ ) {
    var oneCookie = cookieJar[x].split( "=" );
    if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
  }
  return null;
}

function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
  if( !cookieName ) { return false; }
  if( lifeTime == "delete" ) { lifeTime = -10; } //this is in the past. Expires immediately.
  /* This next line sets the cookie but does not overwrite other cookies.
  syntax: cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]]
  Because of the way that document.cookie behaves, writing this here is equivalent to writing
  document.cookie = whatIAmWritingNow + "; " + document.cookie; */
  document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
    ( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
    ( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
    ( isSecure ? ";secure" : "");
  //check if the cookie has been set/deleted as required
  if( lifeTime < 0 ) { if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return false; } return true; }
  if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return true; } return false;
}
