// JavaScript Document
var warningWrapper;
function showWarning(warning){
	if(!warningWrapper){
		warningWrapper = document.createElement('div');
		warningWrapper.className = 'warning';			
		
		document.getElementsByTagName('body')[0].appendChild(warningWrapper);
	}
	
	warningWrapper.innerHTML = '<h2><em>' + warning + '</em></h2>';
}

function clearWarningWrapper(){
	warningWrapper.innerHTML = '';
}


/*
	ERROR HANDLING
*/
function setError(message, input, secondInput){
	var value = input;
	var input = document.getElementById(input);

	input.className = 'errorMessage';
	input.setAttribute('error', message);
	
	input.setAttribute('secondInput', secondInput);
	
	if(value == 'material' || value == 'meters'){
		input.onmouseover = displayInputError;
	}else{
		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];
}


/* display error for all input fields, except material and meters */
function displayError(){
	var messageBox = document.getElementById('messageBox');
	var messageBoxContent = document.getElementById('messageBoxContent');
	var secondInput;
	
	messageBoxContent.innerHTML = this.getAttribute('error');
	messageBox.style.display = 'block';
	
	var height = messageBoxContent.offsetHeight;
	
	var pos = findPos(this);
	
	var y = pos[1] - 39 - height;
	var x = pos[0];
	
	messageBox.style.top = y+ 'px';
	messageBox.style.left = x + 'px';
	
	try{
		secondInput = this.getAttribute('secondInput');
		secondInput = secondInput.split(';');
		
		for(var i = 0; i < secondInput.length; i++){
			document.getElementById(secondInput[i]).className = 'secondErrorMessage';	
		}
	}catch(e){}
}


/* display error for material and meters only */
function displayInputError(){
	var messageBox = document.getElementById('messageBox');
	var messageBoxContent = document.getElementById('messageBoxContent');

	messageBoxContent.innerHTML = this.getAttribute('error');
	messageBox.style.display = 'block';
	
	var height = messageBoxContent.offsetHeight;
	
	var pos = findPos(this);

	var y = pos[1] - 34 - height;
	
	// check if the browser is IE
	if(navigator.appName == 'Microsoft Internet Explorer'){
		y = pos[1] - 39 - height;
	}
	
	var x = pos[0] + 13;
	
	if(this.getAttribute('name') == 'material'){
		x = pos[0] + 80;
		
		if(navigator.appName == 'Microsoft Internet Explorer'){
			x = pos[0] + 62;
		}
	}else{
		if(navigator.appName == 'Microsoft Internet Explorer'){
			x = pos[0] + 4;
		}
	}
	
	messageBox.style.top = y+ 'px';
	messageBox.style.left = x + 'px';	
}


/* CLEARERROR */
function clearError(){
	var secondInput;
	
	document.getElementById('messageBox').style.display = 'none';
	
	try{
		secondInput = this.getAttribute('secondInput');
		secondInput = secondInput.split(';');
		
		for(var i = 0; i < secondInput.length; i++){
			document.getElementById(secondInput[i]).className = null;	
		}
	}catch(e){}
}



