var time = null;

function redirect(l, s)
{
	url = l;
	timer = setTimeout('window.location = url', s*1000);
}

function toggleDis(id)
{
	if (document.getElementById)
	{
		var objStyle = document.getElementById(id).style;
	}
	else
	{
		alert('Crucial operation not supported. This page may not function properly.');
	}

	var on = 'block';
	var off = 'none';
	var currD = objStyle.display;

	if (currD == off || currD == '')
	{
		objStyle.display = on;
	}
	else
	{
		objStyle.display = off;
	}
}

function makeVisible(id)
{
	if (document.getElementById)
	{
		var objStyle = document.getElementById(id).style;
	}
	else
	{
		alert('Crucial operation not supported. This page may not function properly.');
	}
	
	var currD = objStyle.display;
	if (currD == 'none' || currD == '')
	{ toggleDis(id); }
}

currLi = 'none';

function showLi(id)
{
	// check new li is not current
	if (currLi != id)
	{
		// make old one invisible
		if (currLi != 'none')
		{
			toggleDis(currLi);
		}
		// make new one visible
		// if it exists
		if (document.getElementById(id))
		{
			toggleDis(id);
			currLi = id;
		}
		else
		{
			currLi = 'none';
		}
	}
}

function bigSelect(form, id, length)
{ // select or deselect all buttons in sub-array
	var f = document[form];
	var val = f[id + 'check0'].checked;
	
	for (var i=1; i< length; i++)
	{ f[id + 'check' + i].checked = val; }
}

function countChar(text, c)
{
	var count = 0;
	for (var i = 0; i < text.length; i++)
	{
		if (text.charAt(i) == c) count++;
	}
	return count;
}

function checkEmail(e)
{
	if (countChar(e, '@') == 1)
	{
		var arr = e.split('@');
		if (arr.length == 2)
		{
			if (countChar(arr[1], '.') > 0)
			{
				return true;
			}
		}
	}
	return false;
}

function requiredField(t)
{
	debug('requiredField called...');
	if (t.length > 0) return true;
	return false;
}

//function noNumbers(t)
//{
	// check if the string contains numbers

var errors = new Array();

function error(obj, m)
{
	debug('error call');
	obj.style.background = '#FFD9D9';
	obj.style.borderColor = '#FF0000';

	// ensure no duplication in array
	var f = function(a) {
			return a[0];
		};
	var i = getIndexOf(obj.id, errors, f);
	if (i == null) 
	{
		i = errors.length;
		errors[i] = new Array();
		errors[i][0] = obj.id;
	}
	errors[i][1] = false;
	floatError(m, obj.id);
}

function correct(obj)
{
	debug('correct call');
	obj.style.background = '#ffffff';
	obj.style.borderColor = '';
	
	var f = function(a) {
			return a[0];
		};
	var i = getIndexOf(obj.id, errors, f);
	debug('index returned = '+i);
	if (i != null) errors[i][1] = true;	
	
	deleteObj('error_' + obj.id);
}

function deleteObj(id)
{
	debug('deleteObj');
	if (document.getElementById(id))
	{	var dying = document.getElementById(id); 
		dying.parentNode.removeChild(dying);
	}
}

function getIndexOf(id, array, func)
{
	debug('getIndexOf');
	for (var i = 0; i < array.length; i++)
	{ if (func(array[i]) == id) return i; }
	return null;
}

function floatError(m, id)
{
	debug('floatError call');
	// set inner text to message
	var holder = document.getElementById('innererror');

	// ensure no duplicates
	while (document.getElementById('error_' + id))
	{ deleteObj('error_' + id); }
	
	var padFront = '\n<span id="error_' + id + '">';
	var padBack = '<br /></span>';
	if (holder.innerHTML.length == 0) holder.innerHTML = padFront + m + padBack;
	else holder.innerHTML += padFront + m + padBack;
	// set position at top of page
	var pos = 0;
	
	var navHeight = document.getElementById('frontofhouse').clientHeight + 10;
	
	var myHeight = 0; // get window height
	var myWidth = 0; // get window width
    if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
    }
	
	var scrOfY = 0; // scroll in Y direction
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
    }
	
	pos = scrOfY - navHeight;
	if (pos < 0) pos = 0;
	
	var errorId = 'errormessage';
	var errorBox = document.getElementById(errorId);
	errorBox.style.position = 'absolute';
	errorBox.style.top = pos + 10 + 'px';
	if (myWidth > 1000) 
	{
		errorBox.style.right = '-100px';	 
		errorBox.style.left = '';
	}
	else
	{
		errorBox.style.left = 26 - errorBox.offsetWidth + 'px';
	}
	
	// set as visible
	makeVisible(errorId);
}

function validate()
{
	debug('validate');
	var form = document.quoteform;
	for (var i= 0; i<form.elements.length; i++)
	{ 
		debug('first level of for loop in validate. i = '+i);
		if (form.elements[i].attributes)
		{ 
			debug('form element at index '+i+' has attributes');
			var atts = form.elements[i].attributes;
			for (var j = 0; j < atts.length; j++)
			{
				debug('checking attributes... j = '+j);
				if (atts[j].name == 'onchange')
				{
					debug('fire event: '+form.elements[i].name);
					var elem = form.elements[i];
					if ( elem.fireEvent ) // IE 5.5(WIN)
					{
						elem.fireEvent("onChange");
					}
					else // Mozilla, Safari...
					{
						var evt = document.createEvent("HTMLEvents");
						evt.initEvent("change", true, true);
						elem.dispatchEvent( evt );
					}

				}
			}
		}
	}
	debug('out of for loop');

	var count = 0;
	for (var i = 0; i < errors.length; i++)
	{   if (errors[i][1] == false) 
		{   count++;		}
	}
	if (count > 0)
	{	
		var s = '';
		if (count > 1) s = 'issues';
		else s = 'issue';
		floatError('<b>Submit Failed. <br />Please address ' + s + ' above</b>', 'submit');
		makeVisible('errormessage');
		return false;
	}
	return true;
}
	
function debug(m)
{
	var debug = false;
	if (debug) alert(m);
}


		