var QuoteConfig = {
	current: 0,
	direction: 'up',
	timeout: 5,
	speed: 100,
	quotes: new Array()
	};

var ajax = null;

/* ---------------------------------------------------------------- */
// Fetch an object element
//
// @param	string		object name
// @return	object		element
/* ---------------------------------------------------------------- */
function fetch_object(idname)
{
	if (document.getElementById)
	{
		return document.getElementById(idname);
	}
	else if (document.all)
	{
		return document.all[idname];
	}
	else if (document.layers)
	{
		return document.layers[idname];
	}
	else
	{
		return null;
	}
}

/* ---------------------------------------------------------------- */
// Fetch object alias
/* ---------------------------------------------------------------- */
function getObj(idname)
{
	return fetch_object(idname);
}

/* ---------------------------------------------------------------- */
// Redirect the user
//
// @param	string		location
// [@param	string		confirmation]
/* ---------------------------------------------------------------- */
function redirect(location, conf)
{
	if ( conf )
	{
		if ( !confirm(conf) )
		{
			return false;
		}
	}

	window.location = location;
	return false;
}

/* ---------------------------------------------------------------- */
// Toggle display style value
/* ---------------------------------------------------------------- */
function toggleDisplay(objToggle)
{
	if ( !getObj(objToggle) )
	{
		return false;
	}

	var displaystyle = getObj(objToggle).style.display;

	getObj(objToggle).style.display = ( displaystyle == '' ) ? 'none' : '';
	return false;
}

/* ---------------------------------------------------------------- */
// Follow URL selection
//
// @param	object		select object
/* ---------------------------------------------------------------- */
function urlselection(objsel)
{
	var url = objsel.options[objsel.selectedIndex].value;

	if ( url )
	{
		var urlsplit = url.split("_confirm:");

		if ( urlsplit[1] )
		{
			if ( !confirm([urlsplit[1]]) )
			{
				return false;
			}

			redirect(urlsplit[0]);
			return true;
		}		

		redirect(url);
		return true;
	}

	return false;
}

/* ---------------------------------------------------------------- */
// Initiate full row selection scan
// Basically, this function loops through all checkboxes
// and checks if the row needs to have the selected color.
// This function is needed because this the color is broken
// when the page refreshes.
/* ---------------------------------------------------------------- */
function rowsel_init()
{
	var arr = new Array();
	arr = document.documentElement.getElementsByTagName('*');

	for ( var i=0; i < arr.length; i++ )
	{
		var attr = document.documentElement.getElementsByTagName('*').item(i);

		if ( !attr )
		{
			continue;
		}

		// ---------------------------------------------------
		// Check onclick exists
		// ---------------------------------------------------
		if ( !attr.getAttribute('onclick') )
		{
			return false;
		}

		var onclickattr = attr.getAttribute('onclick').toString();

		if ( !onclickattr && !onclickattr.match(/rowsel\((.*?)\)/) )
		{
			return false;
		}

		// ---------------------------------------------------
		// Process...
		// ---------------------------------------------------
		lent_id = false;

		if ( !attr.getAttribute('id') )
		{
			lent_id = true;
			attr.setAttribute('id', 'CBtemp' + i);
		}

		var paramdata = '';
		var params = RegExp.$1.split(",");
		params[0] = 'document.getElementById(\'' + attr.getAttribute('id') + '\')';

		for ( x in params )
		{
			paramdata += (x!=0) ? ',' + params[x].trim() : params[x].trim();
		}

		eval('rowsel(' + paramdata + ')');

		if ( lent_id )
		{
			attr.setAttribute('id', '');
		}
	}
}

/* ---------------------------------------------------------------- */
// Highlight a selected row
//
// @param	object		input area
// @param	string		hex color selected
// @param	string		hex color unselected 
/* ---------------------------------------------------------------- */
function rowsel(objCheck, colorOn, colorOff)
{
	var lastObj = objCheck;

	if ( !colorOn )
	{
		colorOn = '#fff6f6';
	}

	if ( !colorOff )
	{
		colorOff = '';
	}

	while ( lastObj.parentNode.tagName != 'TR' )
	{
		if ( lastObj.parentNode.tagName == 'BODY' )
		{
			alert('DOM Error: Could not find selected row');
			return false;
		}

		lastObj = lastObj.parentNode;
	}

	lastObj = lastObj.parentNode;

	if ( objCheck.checked )
	{
		lastObj.style.backgroundColor = colorOn;
	}
	else
	{
		lastObj.style.backgroundColor = colorOff;
	}
}

/* ---------------------------------------------------------------- */
// Alters input size based on text inside
//
// @param	object		input area
/* ---------------------------------------------------------------- */
function alterInputSize(obj, min, max)
{
	var strText = obj.value;

	if ( obj.type == 'textarea' )
	{
		var arrLines = strText.split('\n');		

		if ( arrLines.length >= obj.rows && arrLines.length < max - 3 )
		{
			obj.rows = arrLines.length + 3;
		}
		else if ( arrLines.length < obj.rows - 3 && obj.rows > min )
		{
			obj.rows = arrLines.length + 3;
		}

		if ( obj.rows < min )
		{
			obj.rows = min;
		}
	}
}

/* ---------------------------------------------------------------- */
// Validate form
//
// @param	object		form object
/* ---------------------------------------------------------------- */
function validateForm(objform)
{
	var obj;

	for ( var i=0; i<=objform.elements.length; i++ )
	{
		obj = objform.elements[i];

		if ( obj.tagName == 'INPUT' || obj.tagName == 'TEXTAREA' )
		{
			if ( obj.getAttribute('required') == 'true' )
			{
				if ( obj.value == '' || !obj.value )
				{
					alert('You have not filled in all the required fields.');
					return false;
				}
			}
		}
	}

	return true;
}

/* ---------------------------------------------------------------- */
// Highlight required form fields
//
// @param	object		form object
/* ---------------------------------------------------------------- */
function lightRequired(objform)
{
	var obj;

	for ( var i=0; i<objform.elements.length; i++ )
	{
		obj = objform.elements[i];

		if ( obj.tagName == 'INPUT' || obj.tagName == 'TEXTAREA' )
		{
			if ( obj.getAttribute('required') == 'true' )
			{
				obj.style.border = '1px solid #CCCCCC';
				obj.style.background = '#FFFCCC';
			}
		}
	}

	return true;
}


/* ---------------------------------------------------------------- */
// Check at least one checkbox is checked
//
// @param	object		checkbox object
/* ---------------------------------------------------------------- */
function atLeastOneChecked(objCB)
{
	if ( !objCB )
	{
		return false;
	}

	if ( objCB.length )
	{
     	   	for ( var i=0; i<objCB.length; i++ )
		{
                	if ( objCB[i].checked )
                	{
                      	  	return true;
                	}
        	}
	}
	else
	{
		if ( objCB.checked )
		{
			return true;
		}
	}

	return false;
}

/* ---------------------------------------------------------------- */
// Create automatic default value in input field
//
// @param	object		field object
// @param	string		blur or focus
// [@param	string		text color]
/* ---------------------------------------------------------------- */
function autoval(objField, type, color)
{
	if ( type == 'blur' )
	{
		if ( objField.value == '' )
		{
			objField.style.color = (color) ? color : '#b6b6b6';
			objField.value = objField.getAttribute('autovalue');
		}
		else
		{
			return false;
		}
	}
	else if ( type == 'focus' )
	{
		if ( objField.value == objField.getAttribute('autovalue') )
		{
			objField.style.color = (color) ? color : '#000000';
			objField.value = '';
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

/* -------------------------------------------------------------------- */
// Set Attribute [Compatibility Fix]
//
// @param	object		element to add to
// @param	string		action property
// @param	string		method 
/* -------------------------------------------------------------------- */
function setProp(element, actionProp, useMethod)
{
	try {
		element.setAttribute(actionProp, useMethod);
	}
	catch(e)
	{
		element[actionProp] = useMethod;
	}
}

/* ---------------------------------------------------------------- */
// Trim functions
//
// @param	string		string to trim
/* ---------------------------------------------------------------- */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/* ---------------------------------------------------------------- */
// Set opacity of an object
//
// @param	object		object to change
// @param	integer		opacity level
/* ---------------------------------------------------------------- */
function setOpacity(obj, opacityval)
{
	obj.style.opacity = opacityval / 10;
	obj.style.filter = 'alpha(opacity=' + opacityval * 10 + ')';
}


/* ---------------------------------------------------------------- */
// Load a quote to the homepage
//
// Called in order:
// 1: loadQuote
// 2: hideQuote
// 3: getAllQuotes
// 4: getAllQuotes_complete
// 5: showQuote
/* ---------------------------------------------------------------- */
function loadQuote(strDirection, intTimeout)
{
	if ( typeof(intTimeout) != 'undefined' ) QuoteConfig.timeout = intTimeout;
	if ( typeof(strDirection) != 'undefined' ) QuoteConfig.direction = strDirection;

	if ( !QuoteConfig.quotes.length )
	{
		getAllQuotes();
	}
}

function getAllQuotes()
{
	ajax = new ajax_request(true);
	ajax.onreadystatechange(getAllQuotes_complete);
	ajax.send('', 'GET', 'index.php?act=loadquotes');
}

function getAllQuotes_complete()
{
	var data;
	var quote;

	if ( ajax.ready_and_okay() )
	{
		QuoteConfig.quotes = ajax.responseText.split('<<<_QUOTESPLIT_>>>');
		hideQuote(10, true);
	}
}

function hideQuote(i, loadNewQuote)
{
	if ( typeof(i) == 'undefined' ) i = 10;

	if ( i > 0 )
	{
		loadNewQuote = (loadNewQuote) ? 'true' : 'false';
		setOpacity(getObj('quote_field'), --i)
		setTimeout('hideQuote('+i+', '+loadNewQuote+')', QuoteConfig.speed);
	}
	else
	{
		if ( loadNewQuote )
		{
			getQuote();
		}
	}
}

function getQuote()
{
	if ( typeof(QuoteConfig.quotes) == 'undefined' )
	{
		getObj('quote_field').innerHTML = 'No quotes available';
	}

	if ( QuoteConfig.direction == 'up' )
	{
		if ( QuoteConfig.current < QuoteConfig.quotes.length )
		{
			++QuoteConfig.current;
		}
		else
		{
			QuoteConfig.current = 0;
		}
	}
	else
	{
		if ( QuoteConfig.current > 0 )
		{
			--QuoteConfig.current;
		}
		else
		{
			QuoteConfig.current = QuoteConfig.quotes.length - 1;
		}
	}

	if ( typeof(QuoteConfig.quotes[QuoteConfig.current]) == 'undefined' )
	{
		getQuote();
		return false;
	}

	getObj('quote_field').innerHTML = QuoteConfig.quotes[QuoteConfig.current];
	showQuote(0, true);
}

function showQuote(i, setQuoteTimeout)
{
	if ( typeof(i) == 'undefined' ) i = 0;

	if ( i < 10 )
	{
		setQuoteTimeout = (setQuoteTimeout) ? 'true' : 'false';
		setOpacity(getObj('quote_field'), ++i)
		setTimeout('showQuote('+i+', '+setQuoteTimeout+')', QuoteConfig.speed);
	}
	else
	{
		// Automatically callout for the next quote
		if ( QuoteConfig.timeout && setQuoteTimeout )
		{
			setTimeout('hideQuote(10, true)', QuoteConfig.timeout * 1000);
		}
	}
}

/* ---------------------------------------------------------------- */
// Homepoint hovering
/* ---------------------------------------------------------------- */
function homepoint(hp, switchon)
{
	hp.className = (switchon) ? 'homepoint_hover' : 'homePoint';
}

function hometick(hp, switchon)
{
	hp.className = (switchon) ? 'hometick_hover' : 'homeTick';
}
