/*
	Marquee
*/
function Marquee(container, amount, speed, delay)
{
	this.advance = function()
	{
		this.count++;
		if (this.count>this.last)
		{
			this.count = 0;
			this.container.scrollTop = 0;
		}
		this.scroll();
		var d = this.delay;
		if (d == null)
		{
			d = this.container.children(this.count).innerText.length
			if (d)
				d = d*60 + 4000;
			else
				d = 0;
		}
		setTimeout(this.id + ".advance();", d);
	}
	this.scroll = function()
	{
		var item = this.container.children(this.count);
		var a = item.offsetTop;
		var b = this.container.scrollTop;	
		if (b >= a)
			return;
		if ((b+this.amount)>a)
			this.container.scrollTop = a;
		else
			this.container.scrollTop = this.container.scrollTop + this.amount;
		setTimeout(this.id + ".scroll();", this.speed);
	}
	this.id = "m" + (new Date()).valueOf();
	eval(this.id + "=this;");
	this.container = container;
	this.last = container.children.length-1;
	this.count = -1;
	this.speed = typeof(speed)=="undefined" ? 70 : speed;
	this.amount = typeof(amount)=="undefined" ? 2 : amount;
	this.delay = typeof(delay)=="undefined" ? null : delay;
	this.advance();
}

/*
	Validate a form's elements according to varius attributes
*/
function validateForm(form, title, submit)
{
	var i, j, u, sum;
	var input;
	var valid;
	var sErrorMsg = "";

	if (!title)
		title = "Please correct the following:";

	for (i=0; i<form.elements.length; i++)
	{
		input = form.elements[i];
		// skip input when it's not rendered (ie. parent display:none)
		if (input.offsetHeight == 0) continue;
		valid = true;

		// Validate value according to element type and validation type
		switch (input.type.toLowerCase())
		{
		case "text":
		case "password":
		case "textarea":
		case "file":
			if (!input.getAttribute("validation")) continue;
			if (input.getAttribute("mandatory"))
			{
				if (input.getAttribute("mandatory").toLowerCase() == "false" && input.value.length == 0) continue;
			}
			else
				continue;
			switch (input.getAttribute("validation").toLowerCase())
			{
			case "string":
				if (input.value.length == 0)
					valid = false;
				break;
			case "password":
				if (input.value.length == 0)
					valid = false;
				break;
			case "integer":
				if (!/\d+/.test(input.value))
					valid = false;
				break;
			case "email":
				if (!/^[\w\.\-]+@[\w\-]+(\.\w+)+$/.test(input.value))
					valid = false;
				break;
			case "phone":
				if (!/^\+?\d+(-\d+)*$/.test(input.value))
					valid = false;
				break;
			case "id":
				j = input.value.toString();
				input.value = j.replace(/\D/g, "");
				if (/\d+/.test(input.value))
				{
					sum = 0;
					for (j=0; j<input.value.length; j++)
					{
						u = (j % 2 ? 2 : 1) * parseInt(input.value.charAt(input.value.length - j - 1));
						sum += u > 9 ? Math.floor(u / 10) + u % 10 : u;
					}
					if (sum % 10) valid = false;
				}
				else
					valid = false;
				break;
			case "compare":
				// Check the compareInput attribute
				if (input.getAttribute("compareInput"))
					if (input.value != form.elements[input.getAttribute("compareInput")].value)
						valid = false;
			}

			// Validate max and min according to validation type
			if (valid == true)
			{
				switch (input.getAttribute("validation").toLowerCase())
				{
				case "integer":
					if (input.getAttribute("validmax"))
					{
						if (parseInt(input.value) > parseInt(input.getAttribute("validmax")))
							valid = false;
					}
					if (input.getAttribute("validmin"))
					{
						if (parseInt(input.value) < parseInt(input.getAttribute("validmin")))
							valid = false;
					}
					break;
				default:
					if (input.getAttribute("validmax"))
					{
						if (input.value.length > input.getAttribute("validmax")) valid = false;
					}
					if (input.getAttribute("validmin"))
					{
						if (input.value.length < input.getAttribute("validmin")) valid = false;
					}
					break;
				}
			}
			break;
		case "select-one":
			if (input.getAttribute("mandatory"))
				if (input.selectedIndex == 0)
					valid = false;
			break;
		case "select-multiple":
			if (input.getAttribute("mandatory"))
			{
				sum = 0;
				for (j=0; j<input.options.length; j++)
				{
					if (input.options[j].selected) sum++;
				}
				if ((!input.getAttribute("validmax")) && (!input.getAttribute("validmin")))
				{
					if (sum == 0) valid = false;
				}
				else
				{
					if (input.getAttribute("validmax"))
					{
						if (sum > input.getAttribute("validmax")) valid = false;
					}
					if (input.getAttribute("validmin"))
					{
						if (sum < input.getAttribute("validmin")) valid = false;
					}
				}
			}
			break;
		case "checkbox":
			if (input.getAttribute("mandatory"))
				if (!input.checked)
					valid = false;
			break;
		}

		if (!valid)
		{
			if (input.getAttribute("validationError"))
				sErrorMsg += input.getAttribute("validationError") + "\n";
			else
				sErrorMsg += "Error in field " + input.name + "\n";
		}
	}
	if (sErrorMsg.length)
	{
		alert(title + "\n" + sErrorMsg);
		return false;
	}
	else
	{
		if (submit)
			form.submit();
		return true;
	}
}
