/**
*	Various JavaScript tools.
*
*	@author Giorgio Sintichakis
*	Copyright (c) 2007 HitGrab Inc. and Giorgio Sintichakis
*/

/* Zipor funcs start */
var timer = new Array();
var ziporHeight;
var ziporSpeed = 10; // in milliseconds
var ziporPause = 2.25; // in seconds

function ziporDown(id, msg)
{
	resetError(id);
	fillElementHTML(id, msg);
	var o = getObjectRef(id);
	ziporHeight = (empty(ziporHeight)) ? o.offsetHeight : ziporHeight;
	o.style.height = 0;
	o.style.visibility = "visible";
	addZiporTop(id, 0);
	timer[timer.length] = setTimeout("ziporUp('"+id+"')", ziporPause * 1000 + (ziporSpeed * ziporHeight));
}

function addZiporTop(id, index)
{
	var o = getObjectRef(id);
	if (++index<=ziporHeight)
	{
		o.style.height = index+"px";
		timer[timer.length] = setTimeout("addZiporTop('"+id+"', '"+index+"')", ziporSpeed);
	}
}

function ziporUp(id)
{
	var o = getObjectRef(id);
	removeZiporTop(id, ziporHeight);
	timer[timer.length] = setTimeout("resetError('"+id+"')", ziporHeight * ziporSpeed);
}

function removeZiporTop(id, index)
{
	var o = getObjectRef(id);
	if (--index>=0)
	{
		o.style.height = index+"px";
		timer[timer.length] = setTimeout("removeZiporTop('"+id+"', '"+index+"')", ziporSpeed);
	}
}

function resetError(id)
{
	var o = getObjectRef(id);
	o.style.visibility = "hidden";
	o.style.height = ziporHeight;
	stopTimers();
}

function stopTimers()
{
	for (i=0; i<timer.length; i++) { clearTimeout(timer[i]); }
	timer = new Array();
}
/* Zipor funcs end */


/* Fador funcs start */
var colorTimer = new Array();

function fadorColor(id, color, index)
{
	if (++index<15)
	{
		var currentColor = getObjectRef(id).style.backgroundColor;
		color = convertColorCode(color) - 15;
		color = color.toString(16);
		color = (color.length > 5) ? color : "0"+color;
		setBGColor(id, "#"+color);
		colorTimer[colorTimer.length] = setTimeout('fadorColor("'+id+'", "'+color+'", "'+index+'")', 25 + ((index/2) * 25));
	}
}

function convertColorCode(color)
{
	var offset = (color.indexOf("#")==-1) ? 0 : 1;
	return parseInt("0x"+color.substring(0+offset)); 
}

function getBGColor(id) { return getObjectRef(id).style.backgroundColor; }
function setBGColor(id, color) { getObjectRef(id).style.backgroundColor = color; }
function flickerBGColor(id, curColor, newColor) { setBGColor(id, newColor); setTimeout("setBGColor('"+id+"', '"+curColor+"')", 250); }

function getColor(id) { return getObjectRef(id).style.color; }
function setColor(id, color) { getObjectRef(id).style.color = color; }
function flickerColor(id, curColor, newColor) { setColor(id, newColor); setTimeout("setColor('"+id+"', '"+curColor+"')", 250); }

/* Fador funcs end */

function wrapSpan(text, color) {
	return "<span style='color: "+color+";'>" + text + "</span>";
}

function empty(text) { if (text != '' && text != null && text != 'undefined') return false; else return true; }
function checkEnter(e) { (checkKeyPress(e)==13 ? loginSubmit() : null); }
function checkKeyPress(e) { return (window.event) ? e.keyCode : e.which; }
function cancelEvent(e)
{
	if (!e) e = window.event;
	if (e.stopPropagation) e.stopPropagation();
	if (e.preventDefault) e.preventDefault();
	e.cancelBubble = true;
	e.cancel = true;
	e.returnValue = false;
	return false;
}

function getObjectRef(id) { return document.getElementById(id); }

function getOptionValue(id) { return getObjectRef(id).options[getObjectRef(id).selectedIndex].value; }
function getOptionHTML(id) { return getObjectRef(id).options[getObjectRef(id).selectedIndex].innerHTML; }
function getElementHTML(id) { return getObjectRef(id).innerHTML; }
function getElementValue(id) { return getObjectRef(id).value; }

function setOptionValue(id, value)
{ 
	for (i=0; i<getObjectRef(id).options.length; i++)
	{
		if (getObjectRef(id).options[i].value == value)
		{
			getObjectRef(id).options.selectedIndex = i;
			break;
		}
	}
}
function getOptionValue(id) { return getObjectRef(id).options[getObjectRef(id).options.selectedIndex].value; }
function setOptionIndex(id, index) { getObjectRef(id).options.selectedIndex = index; }
function getOptionIndex(id) { return getObjectRef(id).options.selectedIndex; }

function setCheckbox(id, value) { getObjectRef(id).checked = value; }
function getCheckbox(id) { return getObjectRef(id).checked; }
function toggleCheckbox(id) { getObjectRef(id).checked = !getObjectRef(id).checked; }
function toggleCheckboxDisabled(id) { getObjectRef(id).disabled = !getObjectRef(id).disabled; }

function fillElementHTML(id, text) { getObjectRef(id).innerHTML = (text==null?'':text); }
function fillElementValue(id, text) { getObjectRef(id).value = (text==null?'':text); }
function addElementHTML(id, text) { getObjectRef(id).innerHTML += text; }
function addElementValue(id, text) { getObjectRef(id).value += text; }
function clearHTML(id) { getObjectRef(id).innerHTML = ''; }
function clearValue(id) { getObjectRef(id).value = ''; }
function clearAll(id) { clearHTML(id); clearValue(id); }

function setErrorValue(id, msg, curColor, newColor) { fillElementValue(id, msg); (newColor!=null ? flickerColor(id, curColor, newColor) : 0); }
function setErrorHTML(id, msg, curColor, newColor) { fillElementHTML(id, msg); (newColor!=null ? flickerColor(id, curColor, newColor) : 0); }

function checkBox(id) { getObjectRef(id).checked = true; }
function uncheckBox(id) { getObjectRef(id).checked = false; }
function recheckBox(id) { getObjectRef(id).checked = !getObjectRef(id).checked; }

function checkIEHeightFix(id) { if (navigator.appName == "Microsoft Internet Explorer") getObjectRef(id).style.height = "auto"; }
function changeImageSrc(id, src) { getObjectRef(id).src = src; }
function changeBGImageSrc(id, src) { getObjectRef(id).style.backgroundImage = "url("+src+")"; }
function changeOverflow(id, type) { getObjectRef(id).style.overflow = type; }
function focusElement(id) { getObjectRef(id).focus(); }

function makeInvisible(id) { getObjectRef(id).style.visibility = 'hidden'; }
function makeVisible(id) { getObjectRef(id).style.visibility = 'visible'; }
function displayNone(id) { getObjectRef(id).style.display = 'none'; }
function displayBlock(id) { getObjectRef(id).style.display = 'block'; }

function doDisable(id) { getObjectRef(id).disabled = true; }
function doEnable(id) { getObjectRef(id).disabled = false; }

Array.prototype.find = function(string) { for (i=0; i<this.length; i++) { if (string == this[i]) return true; } return false; };
Array.prototype.remove = function(string) { for (i=0; i<this.length; i++) if (string == this[i]) { this.splice(i, 1); break; } };
Array.prototype.removeAll = function(string) { for (i=0; i<this.length; i++) if (string == this[i]) this.splice(i, 1); };

function trim(s) { return s.replace(/^\s+|\s+$/g,""); }
function ltrim(s) { return s.replace(/^\s+/,""); }
function rtrim(s) { return s.replace(/\s+$/,""); }

function clearFields(fields)
{
	fields = fields.split(",");
	for (i=0; i<fields.length; i++) clearAll(fields[i]);
}
function checkFields(fields)
{
	fields = fields.split(",");
	for (i=0; i<fields.length; i++) if (empty(getElementValue(fields[i])) && empty(getElementHTML(fields[i]))) return false;
	return true;
}
function pushVars(form) { getObjectRef(form).submit(); }

function ued_encode(arr, current_index) 
{
	var query = "";
	if (current_index=='undefined') current_index = '';
	if (typeof(arr) == 'object') 
	{
		var params = new Array();
		for (key in arr) {
			var data = arr[key];
			var key_value = key;
			if (current_index) key_value = current_index+"["+key+"]";

			if (typeof(data) == 'object')
			{
				if(data.length) for(var i=0;i<data.length; i++) params.push(key_value+"[]="+ued_encode(data[i],key_value)); //:RECURSION:
				else params.push(ued_encode(data,key_value)); //:RECURSION:
			} 
			else params.push(key_value+"="+encodeURIComponent(data));
		}
		query = params.join("&");
	} 
	else query = encodeURIComponent(arr);
	return query;
}

function verifyEmail(email)
{
	var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return emailRegex.test(email);
}