function getID(formElement, x)  {
  var retID = String(x);
  if (formElement.id) {
	  retID = String(formElement.id);
  }
  else if (formElement.name)  {
	  retID = String(formElement.name);
  }
  return retID;
}

function sessionFormToString(theForm,SaveAs)  {
  var theFormElements = theForm.elements;
  var theSessionString = "";
  var theSessionSeparator = "|?";
  for (var x=0; x<theFormElements.length; x++)  {
    if (theFormElements[x].tagName=="TEXTAREA")  {
      if (theSessionString!="")  {
	    theSessionString += theSessionSeparator;
	  }
	  theSessionString += getID(theFormElements[x],x) + "=" + encodeHex(theFormElements[x].value.replace(/=/g, " "));
    }
	else if (theFormElements[x].tagName=="SELECT")  {
      if (theSessionString != "")  {
	    theSessionString += theSessionSeparator;
	  }
	  for (var y=0; y<theFormElements[x].options.length; y++)  {
	    if (theFormElements[x].options[y].selected) {
	      theSessionString += getID(theFormElements[x],x) + "=" + theFormElements[x].options[y].value;
		}
	  }
    }
else if(theFormElements[x].tagName=="EMBED")
    {

    }
	else  {
	  theType = "text";
	  if (theFormElements[x].type) {
	    theType = theFormElements[x].type.toLowerCase();
	  }
	  if (theType == "text")  {
        if (theSessionString!="")  {
	      theSessionString += theSessionSeparator;
	    }
	    theSessionString += getID(theFormElements[x],x) + "=" + escape(theFormElements[x].value.replace(/=/g, " "));
	  }
	  if (theType == "hidden")  {
        if (theSessionString!="")  {
	      theSessionString += theSessionSeparator;
	    }
	    theSessionString += getID(theFormElements[x],x) + "=" + theFormElements[x].value;
	  }
	  if (theType == "checkbox")  {
	    if (theFormElements[x].checked)  {
          if (theSessionString!="")  {
	        theSessionString += theSessionSeparator;
	      }
	      theSessionString += getID(theFormElements[x],x) + "=" + theFormElements[x].value;
		}
	  }
	  if (theType == "radio")  {
	    if (theFormElements[x].checked)  {
          if (theSessionString!="")  {
	        theSessionString += theSessionSeparator;
	      }
	      theSessionString += getID(theFormElements[x],x) + "=" + theFormElements[x].value;
		}
	  }
    }	
	}
		getSessionNameVal(SaveAs,theSessionString);
}

function findValInString(theString,theVal,theSep)  {
  var retVal = "";
  var searchStr = theSep + theString + theSep;
  var findStr = theSep + theVal + "=";
  if (searchStr.indexOf(findStr) >= 0)  {
    retVal = searchStr.substring(searchStr.indexOf(findStr)+findStr.length);
	retVal = retVal.substring(0,retVal.indexOf(theSep));
  }
  return retVal;
}

function findMatchInString(theString,theMatch,theSep)  {
  var searchStr = theSep + theString + theSep;
  var findStr = theSep + theMatch + theSep;
  if (searchStr.indexOf(findStr) >= 0)  {
    return true;
  }
  return false;
}


function sessionFormFromString(sessVal,theForm,SaveAs)  {  //alert("hi");return false;

  var sessionVal = sessVal;
  var theFormElements = theForm.elements;
  var theSessionSeparator = "|?";
  for (var x=0; x<theFormElements.length; x++)  {
    if (theFormElements[x].tagName=="TEXTAREA")  {
	  var findID = getID(theFormElements[x], x);
	  var findVal = findValInString(sessionVal,findID,theSessionSeparator);
	  if (findVal != "")  {
                                 
	    theFormElements[x].value = decodeHex(findVal);
	  }
    }
	else if (theFormElements[x].tagName=="SELECT")  {
	  var findID = getID(theFormElements[x], x);
	  for (var y=0; y<theFormElements[x].options.length; y++)  {
        if (findMatchInString(sessionVal,findID+"="+theFormElements[x].options[y].value,theSessionSeparator))  {
		  theFormElements[x].options[y].selected = true;
		}
	  }
    }
	else  {
	  theType = "text";
	  if (theFormElements[x].type) {
	    theType = theFormElements[x].type.toLowerCase();
	  }
	  if (theType == "text")  {
        var findID = getID(theFormElements[x], x);
	    var findVal = findValInString(sessionVal,findID,theSessionSeparator);
	    if (findVal != "")  {
	      theFormElements[x].value = unescape(findVal);
	    }
	  }
	  if (theType == "hidden")  {
        var findID = getID(theFormElements[x], x);
	    var findVal = findValInString(sessionVal,findID,theSessionSeparator);
	    if (findVal != "")  {
	      theFormElements[x].value = findVal;
	    }
	  }
	  if (theType == "checkbox")  {
	    var findID = getID(theFormElements[x], x);
        if (findMatchInString(sessionVal,findID+"="+theFormElements[x].value,theSessionSeparator))  {
		  theFormElements[x].checked = true;
		}
	  }
	  if (theType == "radio")  {
        var findID = getID(theFormElements[x], x);
        if (findMatchInString(sessionVal,findID+"="+theFormElements[x].value,theSessionSeparator))  {
		  theFormElements[x].checked = true;
		}
	  }
    }
  }
}
function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

