// JavaScript Document

/*
	ERROR HANDLING
*/
function setError(message, input){
	var value = input;

	input.className = 'errorMessage';
	input.setAttribute('error', message);

	input.onmouseover = displayError;
	input.onmouseout = clearError;
}

function findPos(obj){
	var curleft = curtop = 0;

	if (obj.offsetParent){
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;

		while (obj = obj.offsetParent){
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	
	return [curleft,curtop];
}

function displayError(message, input){
	var application = document.getElementById('application');
	
	var id = input.id + 'ID';

	input.className = '';
	input.className = 'errorMessage';
	input.onmouseover = showError;
	
	var messageBox = document.createElement('div');
	messageBox.className = 'messageBox';
	messageBox.id = id;
	var messageBoxHeader = document.createElement('div');
	messageBoxHeader.className = 'messageBoxHeader';	
	var messageBoxContent = document.createElement('div');
	messageBoxContent.className = 'messageBoxContent';
	
	messageBox.appendChild(messageBoxHeader);
	messageBox.appendChild(messageBoxContent);
	application.appendChild(messageBox);
	
	id = "'" + id + "'";

	messageBoxHeader.innerHTML = '<a href="#" onclick="clearError('+id+');"><img src="img/closeWarningBox.gif" /></a>';
	messageBoxContent.innerHTML = message;
	messageBox.style.display = 'block';
	
	var height = messageBoxContent.offsetHeight;
	
	var object = document.getElementById(input.id);

	var pos = findPos(object);
	
	var y = pos[1] - 39 - height;
	var x = pos[0] + 13;
	
	if(document.getElementsByTagName('body')[0].offsetWidth <= 1075){
		if(x > 850){
			y = pos[1] - 29 - height;
			x = pos[0] - 50;
		}
	}
	
	if(navigator.appName == 'Microsoft Internet Explorer'){
		var navVersion = navigator.appVersion.split(";");
		
		y = y + 1;
		x = pos[0] + 15;
		
		if(navVersion[1].replace('MSIE ', '') == 6.0){
			y = y - 5;
			x = x;
		}
	}
	
	messageBox.style.top = y+ 'px';
	messageBox.style.left = x + 'px';
}

/* SHOW AN ERROR */
function showError(){
	var id = this.id + 'ID';
	
	document.getElementById(id).style.display = 'block';
	
	return false;
}

/* HIDE AN ERROR */
function clearError(id){
	document.getElementById(id).style.display = 'none';
	 return false;
}

/* REMOVE AN ERROR FROM THE APPLICATION */
function removeError(input){
	id = input.id + 'ID';

	try{
		var obj = document.getElementById(id);
		document.getElementById('application').removeChild(obj);
	}catch(e){}
}

/* SET THE ATTRIBUTES CLASSNAME, ONMOUSEOVER AND ONMOUSEOUT TO NULL */
function handleClearAttributes(input){
	input.className = null;
	input.onmouseover = null;
	input.onmouseout = null;
	
	removeError(input);
}


