///////////////////////////////////////////////////////////
//	Created By:		Simon Dingley / www.creativenrg.co.uk
//	Created On:		25th January 2005
//	Version:		1.1
///////////////////////////////////////////////////////////
addEvent(window, 'load', PopulateForm);;
///////////////////////////////////////////////////////////
// Adjust these variables as required
///////////////////////////////////////////////////////////
var daysToExpiry = 7;
var cookieName = "contactDetails"; 
///////////////////////////////////////////////////////////

var today = new Date();
var expires = new Date(today.getTime() + (daysToExpiry * 86400000));

var formResults="";
var myCookie = LoadCookie(cookieName);

function SetValues() {
	
	var string = myCookie;
	
    GetValue(MM_findObj("txtName"), "text");
    GetValue(MM_findObj("txtCompany"), "text");
	GetValue(MM_findObj("txtDepartment"),   "text");
    GetValue(MM_findObj("txtTelephone"), "text");
    GetValue(MM_findObj("txtEmail"), "text");
	GetValue(MM_findObj("txtAddress"), "text");
}

function ScrapeForm()
{
    if(MM_findObj("chkRemember").checked)
	{
		SetValue(MM_findObj("txtName"), "text");
		SetValue(MM_findObj("txtCompany"), "text");
		SetValue(MM_findObj("txtDepartment"),   "text");
		SetValue(MM_findObj("txtTelephone"),   "text");
		SetValue(MM_findObj("txtEmail"), "text");
		SetValue(MM_findObj("txtAddress"), "text");
		
		if (formResults.substring(formResults.length -1) == "&")
		{
			formResults = formResults.substring(0, formResults.length -1);
		}
		
		formResults = replace(formResults,"undefined","")
	
		// Save the cookie to the users machine
		SaveCookie(cookieName,formResults,expires)
	}
}

///////////////////////////////////////////////////////////
/// Populates the form with values stored in the cookie
///////////////////////////////////////////////////////////
function PopulateForm() {

	try
	{
		// If the cookie exists
		if (myCookie) 
		{	// Set the form values
			SetValues();
		}
	}
	catch(e) {//Hide any errors
	} 
}

///////////////////////////////////////////////////////////
/// Returns a string containing the cookie data
///////////////////////////////////////////////////////////
function LoadCookie(name) {
try
{
    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));
}
catch(e)
{
	//Hide any errors
}
}

///////////////////////////////////////////////////////////
/// Saves the data in a cookie on the user machine
///////////////////////////////////////////////////////////
function SaveCookie(name,value,expires,path,domain) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "")
}

///////////////////////////////////////////////////////////
/// Replaces characters/string within a string 
///////////////////////////////////////////////////////////
function replace(string,text,replaceWith) {
// Replaces text with by in string
    var i = string.indexOf(text);
    var newstr = '';
    if ((!i) || (i == -1)) return string;
    newstr += string.substring(0,i) + replaceWith;

    if (i+text.length < string.length)
        newstr += replace(string.substring(i+text.length,string.length),text,replaceWith);
    
    return newstr;
}

///////////////////////////////////////////////////////////
/// Finds a named object in the page
///////////////////////////////////////////////////////////
function MM_findObj(n, d) { //v4.01 by Macromedia Inc.
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

///////////////////////////////////////////////////////////
/// Populates a form field with it's value from the cookie
///////////////////////////////////////////////////////////
function GetValue(object, elementType) {
    var string = myCookie;
   	var elementName = object.name; 
    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.value = elementValue;
	}
}

///////////////////////////////////////////////////////////
/// Builds a string to represent form data
///////////////////////////////////////////////////////////
function SetValue(object,elementType) {
	var value;
	
	value = escape(object.name) + "=";
	        
    if (elementType == "text")     value += object.value;
    if (elementType == "password") value += object.value;
    if (elementType == "select")   value += object.selectedIndex;
    if (elementType == "checkbox") value += object.checked;
    if (elementType == "radio")    value += object.value;
	//Add a delimeter to the name/value pair;
	value += "&";
		
	formResults += value;
}

///////////////////////////////////////////////////////////
/// Helper Function to map radio button values
///////////////////////////////////////////////////////////
function onCheck(string) { 
	if (string == "YES") return true; return false; 
}

// Add event handlers to existing ones 
// by Scott Andrew
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, true); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
