//***********************************************************************
// The IText object was developed by Sierra Tel Internet and is
// Copyright 2009, All Rights Reserved

// Cookie support for IText
function setCookie(name, value, expiresAfter)
{
	// Set the value of the named cookie. ExpiresAfter is in days
	var	expires = new Date();
	expires.setDate(expires.getDate() + expiresAfter);
	document.cookie = name+"="+value + ";expires=" + expires.toGMTString();
}

function getCookie(name)
{
	// get the value of the named cookie
	var	value = "";
	var	cookies = document.cookie.split(/\s*;\s*/);
	for (var i = 0; i < cookies.length; i++) {
		var	nv = cookies[i].split("=");
		// alert("x" + nv[0] + "x");
		if (nv[0] == name) {
			if (nv.length > 1)
				value = nv[1];
			break;
		}
	}
	return value;
}

// Text box with instructional default contents
// name - The name assigned to the input element. Also used as the cookie name if requested
// value - Current value for the input element. Will be overridden by cookie
// def - A default instructional value that is displayed in the field until it has focus
// size - character width of input element
// maxLength - maximum length of the input data
// extra - any additional HTML that you want to emit inside the input tag
// useCookie - Set to true will use the name as a cookie to save and load the last valid input
// isPassword - Pass true to make this a password field.

function IText(name, value, def, size, maxlength, extra, useCookie, isPassword) {
	this.name = name;				// name of HTML input element
	this.def = def;					// default value to display if no value provided
	this.size = size;				// size of text
	this.maxlength = maxlength;		// maximum character in text
	this.extra = extra;				// HTML to write in the <input tag>
	this.cookie = IText.arguments.length > 6? useCookie : false;
	this.value = this.cookie? getCookie(name) : value;
	this.isPassword = IText.arguments.length > 7? isPassword : false;
		
	this.write = function() {
		// alert(this.value);
		// emit a text element with a focus/blur handler that
		// will hide/show a default contents when the field is empty
		var me = 'document.forms[' + (document.forms.length - 1) + '].' + name + '.value';
		var myName = 'document.forms[' + (document.forms.length - 1) + '].' + name + '.name';

		// when we get the focus we replace default with Nil
		// WIP we'd like to do nothing if we are not set to default
		var focusCmd = 'if (' + me + ' == \'' + this.def + '\')' + me + '=\'\'';

		// when we blur and we have NO contents we re-instate default
		var	blurToDef = me + '=('+ me + '.length != 0? ' + me + ': \'' + this.def + '\');';
		var	blurToNil = me + '=('+ me + '.length != 0? ' + me + ': \'\');';
		// if this.cookie is true, we use element name as a cookie
		var blurSetCookie = this.cookie == true? 'setCookie(' + myName + ' , ' + me + ', 365);' : '';

		var itype = this.isPassword? "password" : "text";
		document.write('<input type=' + itype + ' name="' + this.name + '" ' +
			'value="' + (this.value.length > 0? this.value : this.def) + '" ' +
			'size="' + this.size + '" maxlength="' + this.maxlength + '" ' +
			'onfocus="' + focusCmd + ';" ' +
			'onblur="' + blurToNil + blurSetCookie + blurToDef + '" ' +
			this.extra +
			'>'	);
	}
}
//***********************************************************************
