window.onerror = reportError;

function loadURL(connection, destinationURL, arguments, triggerFunction) 
{
	connection.onreadystatechange = triggerFunction;

	//Browsers will cache text documents even if they've changed so we append a timestamp to get the latest file
	if (arguments == '')
	{
		stamp = '?' + Math.round(new Date().getTime()/1000.0);
	}
	else
	{
		stamp = '&' + Math.round(new Date().getTime()/1000.0);
	}

	connection.open("GET", destinationURL + arguments + stamp);
	connection.send(null);
}

function reportError(errMsg, errScript, errLine)
{
	errorConnection = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	loadURL(errorConnection, 'javascriptError.php', '?errMsg=' + errMsg + '&errScript=' + errScript + '&errLine=' + errLine, errorLog);
	return true;
}

function errorLog()
{
	return true;
}

function openWindow(url, width, height)
{
	ref = window.open(url, 'window',  'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height+',left=400,top=200');
}

function callDelete(url, name)
{
	if (confirm('Are you sure you want to delete "' + name + '"?'))
	{
		document.location.href = url;
	}
	else
	{
		return false;
	}
}

function validateForm(formArray)
{
	returnValue = true;

	for (i=0; i<formArray.length; i++)
	{
		if (returnValue)
		{
			element = document.getElementById(formArray[i][0]);

			switch (formArray[i][1])
			{
				case 'text':
				case 'password':	
				case 'textarea':
				case 'hidden':

					if ((formArray[i][2] == 'STRING' && element.value != '') || (formArray[i][2] == 'NUMBER' && isNumeric(element.value)))
					{
						highlightElement(formArray[i][0], false);
					}
					else
					{
						alert(formArray[i][3]);
						highlightElement(formArray[i][0], true);
						focusElement(element);
						returnValue = false;
					}

					break;

				case 'radio':

					var element = eval('document.forms[0].' + formArray[i][0]);
					foundItem = false;

					for (j=0; j<element.length; j++) 
					{
						if (element[j].checked) 
						{
							foundItem = true;
							break;
						}
					}

					if (!foundItem)
					{
						alert(formArray[i][3]);
						returnValue = false;
					}

					break;

				case 'check':
					
					if (element.checked)
					{
						highlightElement(formArray[i][0] + 'Border', false);
					}
					else
					{
						alert(formArray[i][3]);
						highlightElement(formArray[i][0] + 'Border', true);
						focusElement(element);
						returnValue = false;
					}

					break;

				case 'select':

					if (element.selectedIndex != 0)
					{
						highlightElement(formArray[i][0] + 'Border', false);
					}
					else
					{
						alert(formArray[i][3]);
						highlightElement(formArray[i][0] + 'Border', true);
						focusElement(element);
						returnValue = false;
					}

					break;

				case 'date':

					alert("You'll have to write something custom for this, as it does basic date formatting itself");

					break;
			}
		}		
	}

	return returnValue;
}

function isNumeric(value)
{
	var validChars = "0123456789.-";
	var singleChar;
	var result = true;

	if (value.length == 0) 
	{
		return false;
	}

	for (j=0; j<value.length && result == true; j++)
	{
		singleChar = value.charAt(j);
	
		if (validChars.indexOf(singleChar) == -1)
		{
			result = false;
		}
	}

	return result;
}

function highlightElement(id, status)
{
	element = document.getElementById(id);

	if (status)
	{
		element.style.borderColor = 'red';
		element.style.borderWidth = '2px';
		element.style.borderStyle = 'solid';
	}
	else
	{
		element.style.borderColor = '';
		element.style.borderWidth = '';
		element.style.borderStyle = '';
	}
}

function highlightRow(id, status)
{
	element = document.getElementById(id);

	if (status)
	{
		element.style.backgroundColor = '#F86B6B';
	}
	else
	{
		element.style.backgroundColor = '';
	}
}

function focusElement(element)
{
	element.focus();
}

function elementLock(elementID, status)
{
	element = document.getElementById(elementID);

	if (status)
	{
		element.selectedIndex = 0;
	}
	
	element.disabled = status;
}

function debug(message)
{
	element = document.getElementById('debug');
	element.value += message + "\n";
}

function setHover(name, number)
{
	for (i=1; i<=5; i++)
	{
		element = document.getElementById(name + i);

		if (i <= number)
		{
			element.src = imagePath + 'starGold.gif';
		}
		else
		{
			element.src = imagePath + 'starGrey.gif';
		}
	}
}

function lockStars(name, number)
{
	document.getElementById(name + 'Score').value = number;
	document.getElementById(name + 'ImageScore').src = imagePath + 'score' + number + '.gif';
}

function clearStars(name)
{
	score = document.getElementById(name + 'Score').value;

	for (i=1; i<=5; i++)
	{
		element = document.getElementById(name + i);

		if (i <= score)
		{
			element.src = imagePath + 'starGold.gif';
		}
		else
		{
			element.src = imagePath + 'starGrey.gif';
		}
	}

}
