// Javascript to implement saving and loading a form to and from a cookie
// Copyright 2003 Doug Gale. All rights reserved.
// Return a GMT date string offset from today by the specified number of days

function MakeRelativeDate(nDayOfs)

{
	var d = new Date();
	d = new Date(d.getYear(), d.getMonth(), d.getDate() + nDayOfs,
			d.getHours(), d.getMinutes(), d.getSeconds());
	return d.toGMTString();
}

// Expires the specified cookie
function ExpireCookie(cName)
{
	document.cookie = cName + "=NULL; " +
			"expire=" + MakeRelativeDate(-365) + "; " +
			"path=/;";
}

// Sift through the cookie and return the cookie with the specified name
// The value is unescaped before returning
// Returns undefined if the cookie is not found
function GetCookieByName(cName)
{
	var aCookieItems, aCookieSplit, i;
	aCookieItems = document.cookie.split(/; ?/);
	for (i = 0; i < aCookieItems.length; i++) {
		aCookieSplit = aCookieItems[i].split(/=/, 2);
		if (aCookieSplit[0] == cName) {
			return aCookieSplit[1].length > 0 ?
					unescape(aCookieSplit[1]) : "";
		}
	}
	return undefined;
}

// Initialize form from cookie
function SetFormFromCookie(oForm, cCookieName)
{
	var cFormIni;
	var aItems, aSplit, i, n;

	cFormInit = GetCookieByName(cCookieName);
	if (cFormInit == undefined)
		return 0;

	aItems = cFormInit.split(/&/);
    n = aItems.length;
    if (n == undefined || n < 1)
	    return 0;

	for (i = 0; i < n; i++) {
		aSplit = aItems[i].split(/=/, 2);
		if (oForm[aSplit[0]] == undefined) {
			continue;
		}
		if (oForm[aSplit[0]].type.match(/checkbox|radio/)) {
			oForm[aSplit[0]].checked = aSplit[1]!=0;
		} else if (oForm[aSplit[0]].type.match(/text(area)?|password/)) {
			oForm[aSplit[0]].value = aSplit[1] ? unescape(aSplit[1]) : "";
		}
	}

	return 1;
}

// Recreate cookie from form
function SetCookieFromForm(oForm, cCookieName)
{
	var i, n, f, cName, c;
	n = oForm.length;
	c = "";
	f = false;
	for (i = 0; i < n; i++) {
		if (oForm[i].noSave != undefined) {
			// Skip input items with noSave property
		} else if (oForm[i].type.match(/checkbox|radio/)) {
			c += f ? "&" : "";
			c += oForm[i].name + "="
			c += oForm[i].checked ? "1" : "0";
			f = true;
		} else if (oForm[i].type.match(/text(area)?|password/)) {
			c += f ? "&" : "";
			c += oForm[i].name + "="
			c += escape(oForm[i].value);
			f = true;
		}
	}
	c = cCookieName + "=" + escape(c) +
			"; expires=" + MakeRelativeDate(365) + ';'
	document.cookie = c;
}

function CleanTextPhone(o)
{
	var cVal;
	cVal = o.value.replace(/[^0-9]/g, "");
	switch (cVal.length) {
	case 7:
		cVal = "(905) " + cVal.substr(0,3) + "-" + cVal.substr(3,4);
		break;
	case 10:
		cVal = "(" + cVal.substr(0,3) + ") " +
				cVal.substr(3,3) + "-" +
				cVal.substr(6,4);
		break;
	case 0:
		break;
	default:
		// Unrecognized format, don't change
		break;
	}
	o.value = cVal;
}

function CleanTextNumber(o)
{
	o.value = o.value.replace(/[^0-9]/, '');
}

function InitForm(oForm, cCookieName)
{
	var i;

	// Initialize form from cookie
	if (cCookieName != undefined) {
		SetFormFromCookie(oForm, cCookieName);
	}

	// Scan through validation data
	for (i = 0; i < oForm.length; i++) {
		switch (oForm[i].validateType) {
		case "phone":
			oForm[i].onBlur = new Function("CleanTextPhone(this)");
			CleanTextPhone(oForm[i]);
			break;
		case "password":
			break;
		case "minlength":
			break;
		case "email":
			break;
		case "number":
			oForm[i].onBlur = new Function("CleanTextNumber(this)");
			break;
		}
	}
}

function ValidateForm(oForm, cCookieName)
{
	var o, i, x, x2;

	// Create a regular expression for "this" replacements
	var reThisSubst;

	reThisSubst = /\bthis\b/;

//	x = x.replace(reThisSubst, "o");

	// Save form to cookie
	if (cCookieName != undefined) {
		SetCookieFromForm(oForm, cCookieName);
	}

	// Scan through validation data
	for (i = 0; i < oForm.length; i++) {
		o = oForm[i];
		switch (o.validateType) {
		case undefined:
			break;
		case "password":
			x = o.validateParam.replace(reThisSubst, "o");
			x = eval(x);
			if (!ComparePassword(o, x)) {
				if (o.validateAutoclear) {
					x2 = o.validateAutoclear.replace(reThisSubst, "o");
					x2 = eval(x2);
					x2.value = "";
					o.value = "";
				}
				return false;
			}
			break;
		case "minlength":
			x = o.validateParam;
			if (o.value.length < x) {
				if (o.validateAutoclear) {
					x2 = o.validateAutoclear.replace(reThisSubst, "o");
					x2 = eval(x2);
					x2.value = "";
					o.value = "";
				}
				alert(o.validateMsg);
				o.focus();
				return false;
			}
			break;
		case "email":
			break;
		case "number":
			CleanTextNumber(o);
			break;
		}
	}

	return true;
}

function ComparePassword(oPword, oVerify)
{
	if (oPword.value != oVerify.value) {
		alert("You have made a mistake. Please enter a password " +
				"twice for verification");
		oPword.value = '';
		oVerify.value = '';
		oVerify.focus();
		return false;
	}
	return true;
}

function ResetFormWarn(oForm)
{
	if (confirm("Wipe all form values and start over?")) {
		oForm.reset();
		oForm[0].focus();
	}
}


