Array.prototype.end = function () {
	return (this.length-1);
}

Array.prototype.last = function () {
	return this[this.end()];
}

Array.prototype.isSet = function (fData) {
	for (var i = 0, l = this.length; i < l; i++) {
		if (this[i] == fData) return true;
	}
	return false;
}

Array.prototype.insert = function (iPosition, iData) {
	if (iPosition === 0)
		this.unshift(iData);
	else if (iPosition == this.length)
		this.push(iData);
	else {
		for (var i = this.length; i > iPosition; i--)
			this[i] = this[(i-1)];
		this[iPosition] = iData
	}
}

Array.prototype.append = function (iData) {
	if (typeof(iData) != 'object')
		this.unshift(iData);
	else {
		for (var i = 0, l = iData.length; i < l; i++)
			this.unshift(iData[i]);
	}
}

Array.prototype.removeIt = function (fData, all) {
	var iPosition = this.end();
	for (var i = iPosition; i >= 0; i--) {
		if (this[i] === fData) {
			if (all == true) return this.splice(i,1);
			else var removed = this.splice(i,1);
		}
	}
	if (removed !== null) return removed;
}

String.prototype.trim = function () {
	var reg = /^\s{1,}|\s(?!\S)/g;
	if (this != '') return this.replace(reg, '');
	else return '';
}

function in_array (needle, haystack, strict) {
    var found = false, key, strict = !!strict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}

/****************************/
/********** leamix **********/
/****** x8p@leamix.com ******/
/****************************/
var X = {	
	Browser: 
	{
		Opera: (navigator.appName == 'Opera'),
		IE: (navigator.appName.indexOf('Microsoft') >= 0),
		NS: (navigator.appName == 'Netscape')
	},

	Support: 
	{
		DOM: (document.getElementById ? true : false),
		Dall: (document.all ? true : false)
	},

	$: function (objId)	
	{
		if (X.Support.DOM)
			return document.getElementById(objId) || false;
		else if (X.Support.Dall)
			return document.all[objId] || false;
		else return false;
	},

	$Tags: function (tagsType, theParent) 
	{
		return (typeof(theParent) == 'string' ? 
				X.$(theParent) : 
				(theParent ? theParent : document)).getElementsByTagName(tagsType);
	},
	
	$Obj: function (theObject)
	{
		return (typeof(theObject) == 'string' ? X.$(theObject) : theObject);
	},

	Temp: new Array()
}

var Blocks = 
{
	show: function ()
	{
		for (var i = 0, l = Blocks.show.arguments.length; i < l; i += 2)
		{
			var theObject = X.$Obj(Blocks.show.arguments[i]);
			if (theObject.style.display == "none")
				theObject.style.display = Blocks.show.arguments[(i+1)];
		}
	},

	hide: function ()
	{
		for (var i = 0, l = Blocks.hide.arguments.length; i < l; i++)
		{
			var theObject = X.$Obj(Blocks.hide.arguments[i]);
			if (theObject.style.display != "none")
				theObject.style.display = "none";
		}
	},

	isHidden: function (theObject)
	{
		return (X.$Obj(theObject).style.display == "none");
	},

	setClass: function (theObject, newClassName)
	{
		theObject = X.$Obj(theObject);
		if (!theObject.originalClassName)
			theObject.originalClassName = theObject.className;
			
		if (theObject.className !== newClassName && theObject.originalClassName !== newClassName)
			theObject.className = (newClassName || "");
	},

	returnClass: function (theObject)
	{
		theObject = X.$Obj(theObject);
		theObject.className = (theObject.originalClassName || "");
	}
}

function trySubmit (tryForm)
{
	if (!tryForm.onsubmit || tryForm.onsubmit())
		tryForm.submit();
}

function showLength (vTxtArea, cMax, cPlace, clr)
{
	var d = cMax - vTxtArea.value.length;
	if (d < 0 && clr)
	{
		vTxtArea.value = vTxtArea.value.substr(0, cMax);
		X.$Obj(cPlace).innerHTML = 0;
	}
	else
		X.$Obj(cPlace).innerHTML = d;
}

function watchForm (theObject)
{
	this.F = (typeof(theObject) == 'string' ? document.forms[theObject] : theObject);
	if (document.watchingForms == undefined)
		document.watchingForms = new Array();
	
	this.F.holded = new Array();
	this.F.doSubmit = new Array();
	this.setSubmit = false;
		
	document.watchingForms[this.F.name] = this;
}

watchForm.prototype.holdValue = function (params)
{
	if (typeof(params.field) == 'string')
	{
		var f = this.F[params.field];		
		var f_t = f.type;

		if (!in_array(f_t, Array('password', 'text', 'textarea')))
			return;
	
		if (params.value)
		{
			f.value = params.value;
			f.clearValue = params.value;
		}
		else
			f.clearValue = f.defaultValue;
			
		if (params.unactive && f.value == f.clearValue)
			Blocks.setClass(f, params.unactive);
		
		this.F.holded.push(f);
		
		f.onfocus = function ()
		{
			if (this.value == this.clearValue && params.clear) 
				this.value = '';
			if (params.unactive)
				Blocks.returnClass(this);
		}
		f.onblur = function ()
		{
			if (this.value == '')
				this.value = this.clearValue;
			if (this.value == this.clearValue && params.unactive)
				Blocks.setClass(this, params.unactive);
		}
	}
	else for (i = 0, l = params.field.length; i < l; i++)
	{
		var f_v = params.value ? (typeof(params.value) != 'string' ? params.value[i] : params.value) : undefined;
		var f_u = params.unactive ? (typeof(params.unactive) != 'string' ? params.unactive[i] : params.unactive) : undefined;
		var f_c = params.clear ? (typeof(params.clear) != 'boolean' ? params.clear[i] : params.clear) : false; 
		this.holdValue({field: params.field[i], values: f_v, clear: f_c, unactive: f_u});
	}
}

watchForm.prototype.holdSubmit = function (params)
{
	if (params.notEmpty != undefined)
		for (i = 0, l = params.notEmpty.length; i < l; i++)
			this.F[params.notEmpty[i]].flag_ne = true;
	
	if (params.anyOf != undefined)
		for (i = 0, l = params.anyOf.length; i < l; i++)
			this.F[params.anyOf[i]].flag_a = true;
	
	if (!this.setSubmit) this.F.onsubmit = function ()
	{
		this.doSubmit['main'] = true;
		for (k in this.doSubmit)
		{
			if (k != 'main' && !this.doSubmit[k])
			{
				this.doSubmit['main'] = false;
				break;
			}
		}
			
		if (this.doSubmit['main']) for (i = 0, l = this.length, cnt_a = 0, cnt_e = 0; i < l; i++)
		{
			var f = this.elements[i];				
			var f_t = f.type;
			var cflag = true;
			
			if (!f.flag_a && !f.flag_ne)
				continue;
								
			if (f.flag_a)
				cnt_a++;
			
			switch (f_t)
			{
				case 'password':
				case 'text':
				case 'textarea':
					if (f.value == '')
					{
						if (f.flag_e) continue;
						else cflag = !cflag;
					}
					else if (f.clearValue !== undefined && f.value == f.clearValue || f.value == f.defaultValue)
					{
						if (f.flag_d) continue;
						else cflag = !cflag;
					}
				break;					
				case 'file':
					if (f.flag_d || f.flag_e) continue;
					if (f.value == '' || f.files.length == 0) 
						cflag = !cflag;
				break;					
				case 'select-one':
					if (f.flag_d || f.flag_e) continue;
					if (f.options[f.selectedIndex].defaultSelected == true)
						cflag = !cflag;
				break;
				case 'radio':
					if (f.flag_d || f.flag_e) continue;
					var numradio = f.length;
					for (r = 0; r < numradio; r++) if (f[r].checked == f[r].defaultChecked)
					{
						cflag = !cflag;
						break;
					}
				break;
			}
			
			if (cflag == false)
			{
				if (f.flag_ne)
				{
					this.doSubmit['main'] = false;
					break;
				}
				if (f.flag_a)
				{
					cnt_e++;
				}
			}
		}
		if (this.doSubmit['main'] && cnt_a)
			this.doSubmit['main'] = (cnt_e < cnt_a);
		
		if (this.doSubmit['main'] && this.holded.length) for (i = 0, l = this.holded.length; i < l; i++)
		{
			var f = this.holded[i];
			if (f.value == f.clearValue) 
				f.value = '';
		}
		return this.doSubmit['main'];
	}
	
	this.setSubmit = true;
}

watchForm.prototype.allowDefault = function (fields)
{
	if (typeof(fields) == 'string')
		fields = new Array(fields);
		
	for (i = 0, l = fields.length; i < l; i++)
		this.F[fields[i]].flag_d = true;
}

watchForm.prototype.allowEmpty = function (fields)
{
	if (typeof(fields) == 'string')
		fields = new Array(fields);
		
	for (i = 0, l = fields.length; i < l; i++)
		this.F[fields[i]].flag_e = true;
}

watchForm.prototype.watchFile = function (params)
{
	var f = this.F[params.field];
	if (f.type != 'file')
		return;
	
	if (params.types) switch (params.types)
	{
		case 'userpic': case 'img': params.types = ['jpg', 'jpeg', 'gif', 'png']; break;
		case 'attach': params.types = ['jpg', 'jpeg', 'gif', 'png', 'zip', 'rar']; break;
		//'tif', 'txt', 'html', 'htm', 'doc', 'rtf', 'xls', 'pdf'
	}
		
	f.onchange = function ()
	{
		var f_file = f.value.toLowerCase();
		if (f_file.length && f_file.length < 5)
		{
			this.form.doSubmit['file'] = false;
		}
		
		var typeOk = false;
		for (i = 0, l = params.types.length; i < l; i++)
		{
			var ext1 = '.' + params.types[i];
			var ext2 = f_file.substr(f_file.length - ext1.length, ext1.length);
			if (ext1 == ext2)
				typeOk = true;
		}
		
		if (!typeOk)
		{
			this.form.doSubmit['file'] = false;
			if (params.highlight)
				Blocks.setClass(params.highlight, 'highlight');
		}
		else
		{
			this.form.doSubmit['file'] = true;
			if (params.highlight)
				Blocks.returnClass(params.highlight);
			if (params.submitOnChange)
				trySubmit(this.form);
		}
	}
}