// JavaScript Document

/*
	POPUPS
*/
	var popups = new Array();
	
	function closePopups(){
		for(var i = 0; i < popups.length; i++)	{
			try{
					popups[i].close();
			}catch(irrelavantException){};
		}
	}
/*
	// POPUPS
*/


/*
	CANCEL EVENT BUBBLING
*/
function cancelEventBubbling(e){
	if(!e) var e = window.event;
	
	if(e.stopPropagation){
		e.stopPropagation();
	}else{
		e.cancelBubble = true;
	}
}
/*
	// CENCEL EVENT BUBBLING
*/


/*
	PARSE INPUPTS
*/
	function parseRounded(input){
		var result = input;
		var decimals = 2;
		
		if(!isNaN(arguments[1])) decimals = arguments[1];
		
		decimals = parseInt(Math.pow(10, decimals));
		
		if(!isNaN(result)){
			result = Math.round(parseFloat(result) * decimals) / decimals;
		}
		
		result = result.toFixed(2);
	
		return result;
	}
	
	function parseCurrency(input){
		var result = input;
		
		if(!isNaN(result)){
			result = '€ ' + parseRounded(result);
		}
		
		return result;
	}
/*
	// PARSE INPUPTS
*/


/*
	CLEAR RESULTS
*/
	function clearResults(){
		document.getElementById('surface').value = '';
		
		var tableShape = document.getElementById('shape');
		var listInputShape = tableShape.getElementsByTagName('input');
		
		for(var i=0; i<listInputShape.length; i++){
			listInputShape[i].value = 0;
		}
	}
/*
	// CLEAR RESULTS
*/


function initShapeInputs(shape, lengthA, lengthB, lengthC, lengthD, lengthE ,lengthF){
	var formElements = document.getElementById(shape).getElementsByTagName('input');
	
	for(i = 0; i < formElements.length; i++){
		formElements[i].value = arguments[i+1];
	}
}

/*
	APPLICATION
*/
	function handleMaterialChange(){
		var material = document.getElementById('material');
		var materialID = parseInt(material.value);
		var meters = document.getElementById('meters');
		var metersAmount = parseInt(meters.value);
		var surface = parseInt(document.getElementById('surface').value);
		
		material.className = null;
		material.onmouseover = null;
		material.onmouseout = null;
		
		if(material.value == 'Other compound'){
			setError(text.otherCompound, 'material');
			
			return false;
		}
			
		clearTimeOut();
	}
	
	function handleMetersChange(){
		var material = document.getElementById('material');
		var materialID = parseInt(material.value);
		var meters = document.getElementById('meters');
		var metersAmount = parseInt(meters.value);
		var surface = parseInt(document.getElementById('surface').value);
		
		meters.className = null;
		meters.onmouseover = null;
		meters.onmouseout = null;
		
		metersAmount = Math.floor(metersAmount);
		
		if((isNaN(metersAmount)) || (metersAmount < 0) || (metersAmount > 500)){
			setError(text.metersErrorToLarge, 'meters');
			return false;
		}
			
		clearTimeOut();
	}
/*
	// APPLICATION
*/


/*
	APPTOOLBAR
*/
	// REMOVE DIV LAYER (WITH SAVED DESIGNS IN IT) AND CLEAR THE ERROR'S
	var divLayer;
	
	function clearDiv(){
		if(divLayer){
			divLayer.style.display = 'none';
			divLayer = '';
		}else{
			divLayer = document.getElementById('designNames');
		}
		
		clearError();
		
		if(warningWrapper) clearWarningWrapper();
	}
	
	function showDesignNames(){
		setTimeout("document.getElementById('designNames').style.display = 'block'", 1);
	}
	
	var savedDesigns = [];
	
	function showDesigns(){
		var designsWrapper = document.getElementById('designNames');
			
		this.createDesign = function(designGUID, designName){
			var listItem = document.createElement('li');
			var link = document.createElement('a');
			
			link.appendChild(document.createTextNode(designName));
			link.onclick = loadDesign;
			link.setAttribute('designGUID', designGUID);
			link.href = '#';
			
			listItem.appendChild(link);
			return listItem;
		}
		
		for(var i = 0; i < savedDesigns.length; i++){
			designsWrapper.appendChild(createDesign(savedDesigns[i][0], savedDesigns[i][1]));
		}
	}
/*
	// APPTOOLBAR
*/


/*
	HOVER:QUICKLINKS RESETCALCULATOR DELETEBUTTON OFFERREQUESTBUTTON
*/
	function showQuickLinks(){
		document.getElementById('material').style.visibility = 'hidden';
		document.getElementById('quickLinksDiv').style.display = 'block';
	}
	
	function hoverButton(){
		if(this.id == 'resetCalculator'){
			this.style.background = '#0064AE';
			this.childNodes[0].style.color = 'white';
		}else if(this.id == 'deleteButton' || this.id == 'offerRequestButton'){
			this.style.fontWeight = 'bold';
		}
		
		this.style.cursor = 'pointer';
	}
	
	function onMouseOut(){
		document.getElementById('material').style.visibility = 'visible';
		document.getElementById('quickLinksDiv').style.display = 'none';
	
		if(this.id == 'resetCalculator'){
			this.style.background = '#CCC';
			this.childNodes[0].style.color = 'black';
		}else{
			this.style.fontWeight = 'normal';
		}
	}
/*
	// HOVER:QUICKLINKS RESETCALCULATOR DELETEBUTTON OFFERREQUESTBUTTON
*/


var timeout;
function setTimeOut(){
	// make an serverreguest after 1 hour to keep the user loged in 
	time = setTimeout("setTimeOut(); handleHTTPRequest('', 'reset');", 3600000);
}

function clearTimeOut(){
	clearTimeout(timeout);
	setTimeOut();
}

initLogin = function(){
	var loginButton = document.getElementById('loginButton');
		
	toggleButtons();
	
	if( !rubberProfileCalculator.userIsLoggedIn ) {
		loginButton.onclick = openLoginForm;
	}
}

toggleButtons = function(){
	var deleteButton = document.getElementById('deleteButton');
	var loginButton = document.getElementById('loginButton');
	var designNameSelect = document.getElementById('designNameSelect');
	var offerRequestButton = document.getElementById('offerRequestButton');
	
	var designName = document.getElementById('designName');

	if( !rubberProfileCalculator.userIsLoggedIn ){
		deleteButton.style.display = 'none';
		designNameSelect.style.display = 'none';
		//designName.style.display = 'block';
		loginButton.style.display = 'inline';
	}else{
		loginButton.style.display = 'none';
		deleteButton.style.display = 'inline';
		designNameSelect.style.display = 'inline';
		designName.style.display = 'block';
	}		
}


/*
	WINDOW ONLOAD
*/
	var shape;
	window.onload = function(){
		setTimeOut();
		initLogin();
		if( !rubberProfileCalculator.userIsLoggedIn ) automaticLoginDetection();
		
		document.getElementsByTagName('body')[0].onclick = clearDiv;
		
		window.flashapp = document.forms[0].flashapp;
		
		document.getElementById('application').style.display = 'block';
		document.getElementById('material').style.visibility = 'visible';
		document.getElementById('material').onchange = handleMaterialChange;
		document.getElementById('meters').onchange = handleMetersChange;
		document.getElementById('resetCalculator').onclick = resetCalculator;
		
		initLengths();
		
		shape = new shapeEntity();
		shape.setShape('free');
	
		setTimeout(clearPainting, '1');
		
		var links = document.getElementById('shapeButtons').getElementsByTagName('a');
		
		for(var i=0; i<links.length; i++){
			
			links[i].onfocus = function(){ this.blur(); }
			
			links[i].onclick = function(){
				var shapeChange = true;
				
				clearTimeOut();
	
				if(document.getElementById('surface').value != '' || shape.getLengthA() != '' || shape.getLengthB() != '' || shape.getLengthC() != '' || shape.getLengthD() != '' || shape.getLengthE() != '' || shape.getLengthF() != ''){ 
					shapeChange = confirm(text.shapeChange);
					
					shape.setLengthA(0);
					shape.setLengthB(0);
					shape.setLengthC(0);
					shape.setLengthD(0);
					shape.setLengthE(0);
					shape.setLengthF(0);
					
					var inputs = document.getElementById('shape').getElementsByTagName('input');
					
					for(var j=0; j<inputs.length; j++){
						inputs[j].className = null;
						inputs[j].onmouseover = null;
						inputs[j].onmouseout = null;
					}
				}
				
				document.getElementById('brushType').setAttribute('class', 'squareBrushButton');
				document.getElementById('profileDimension').setAttribute('class', 'buttonProfile1');
				
				if(shapeChange){
					var className = this.parentNode.className;
					
					if(this.className == 'free'){
						setTimeout("setFlashDimensions('flashapp', (" + x/y + ")*200, 200);", 100);
						setTimeout("getMovieName('flashapp').setX(" + x + ");", 200);
						setTimeout("getMovieName('flashapp').setY(" + y + ");", 300);
						setTimeout("clearPainting();", 400);
					}
					
					document.getElementById('selector').className = className;
					
					shape.setShape(className);
					
					clearPainting();
					clearResults();
				}
				return false;
			}
		}
			
		/* in ie6 a "hover" is made with javascript */
		if(navigator.appName == 'Microsoft Internet Explorer'){
			document.getElementById('quickLinks').onmouseover = showQuickLinks;
			document.getElementById('quickLinks').onmouseout = onMouseOut;
		
			document.getElementById('resetCalculator').onmouseover = hoverButton;
			document.getElementById('resetCalculator').onmouseout = onMouseOut;
			
			document.getElementById('deleteButton').onmouseover = hoverButton;
			document.getElementById('deleteButton').onmouseout = onMouseOut;
			
			document.getElementById('offerRequestButton').onmouseover = hoverButton;
			document.getElementById('offerRequestButton').onmouseout = onMouseOut;
			
			var freeButtons = document.getElementById('free').getElementsByTagName('button');
			
			for(var ii=0; ii<freeButtons.length; ii++){
				freeButtons[ii].onmouseover = hoverButton;
			}
		}
	}
/*
	// WINDOW ONLOAD
*/


/*
	WINDOW ONUNLOAD
*/
	var userIsLoggingOut = false;
	window.onunload = function(){
		closePopups();
		
		if(!userIsLoggingOut) resetCalculator();
	}
/*
	// WINDOW ONUNLOAD
*/


