<!--
function Get_Cookie(name) {
//  retrieves cookie (userProfile), returns null if it doesn't exist
//  called by global variable

    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
//  sets the cookie according to the values passed by hitting the submit button on the form
//  and the values set in the global variables
//  called by global variable

    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
history.go(-2);
}

function setupForm() {
//  called by the inline command within the BODY tag.  HEAD loads first, then BODY
//  sets off the chain that passes either the default or stored values to the form values

    if (userProfile) getValues(userProfile);
}

function getValues(string) {
//  depending on the elementType, sends the relevant info to getValue()
//  called by setupForm()

    getValue(string,"user",   document.profileForm.user,   "text");
    getValue(string,"backColour",  document.profileForm.backColour,  "select");    
    getValue(string,"textColour",document.profileForm.textColour,"select");    
    getValue(string,"world",    document.profileForm.world,    "select");
    getValue(string,"age",    document.profileForm.age,    "select");
    getValue(string,"sex",    document.profileForm.sex,    "radio");

    for (var i=0;i<7+1;i++)
        getValue(string,"i"+i, eval("document.profileForm.i"+i), "checkbox");
}

function replace(string,text,by) {
//  replaces plus with space in string
//  called by global variable

    var i = string.indexOf(text);
    var newstr = '';
    if ((!i) || (i == -1)) return string;
    newstr += string.substring(0,i) + by;

    if (i+text.length < string.length)
        newstr += replace(string.substring(i+text.length,string.length),text,by);
    
    return newstr;
}

function onCheck(string) {
//  used for checkboxes, determines true if on, vice-versa
//  called by getValue()

	 if (string == "on") return true; return false;
 }

function getValue(string,elementName,object,elementType) {
//  gets value of elementName from string and populates object of elementType
//  called by getValues()

    var startPos = string.indexOf(elementName + "=")
    
    if (startPos > -1) {
        startPos = startPos + elementName.length + 1;
        var endPos = string.indexOf("&",startPos);
        if (endPos == -1) endPos = string.length;

        var elementValue = unescape(string.substring(startPos,endPos));
        
        if (elementType == "text")     object.value = elementValue;
        if (elementType == "password") object.value = elementValue;
        if (elementType == "select")   object.selectedIndex = elementValue;
        if (elementType == "checkbox") object.checked = onCheck(elementValue);
        if (elementType == "radio")    object[elementValue].checked = true;
    }
}

// global variables below:

var today = new Date();
var expires = new Date(today.getTime() + (2 * 365 * 86400000));
// expires in TWO years

var searchString = replace(self.location.search.substring(1),"+"," ");
//  above, checks the URL to when the page loads to see if there's a string after URL
//  when form submit is pressed the page reloads with the content after the URL

if (searchString.length > 0) Set_Cookie("userProfile",searchString,expires);

//  if string, sets the cookie

var userProfile = Get_Cookie("userProfile");

//-->
