// JavaScript Document
function initLengths(){
	var formElements = document.getElementById('shape').getElementsByTagName('input');

	for(var i = 0; i < formElements.length; i++){
		if(formElements[i].getAttribute('name').substring(0, 6) == 'length'){
			formElements[i].onchange = handleShapeLengthChange;
		}
	}
}

/*
	EVENTS
*/
function handleShapeLengthChange(){
	var name = this.getAttribute('name');
	var label = this.parentNode.getElementsByTagName('span')[0].innerHTML;
	var result = false;
	var surface = false;
	var materialID = parseFloat(document.getElementById('material').value);
	var meters = parseFloat(document.getElementById('meters').value);
	var inputs = document.getElementById('shape').getElementsByTagName('input');
	
	for(var i = 0; i < inputs.length; i++){
			inputs[i].removeAttribute('error');
			inputs[i].className = null;
			inputs[i].onmouseover = null;
			inputs[i].onmouseout = null;
	}
	
	this.value = this.value.replace(',', '.');
	
	function getProperName(name){
		return (name.substring(0, 1).toUpperCase()) + name.substring(1, name.length);
	}
	
	eval('result = shape.set' + getProperName(name) + '("' + this.value + '")');
	
	surface = shape.getSurface();
	
	if(surface){
		document.getElementById('surface').value = surface;
		
		clearTimeOut();
	}
}


	/*
		INIT
	*/
	function shapeEntity(){
		this.init();
	}
	
	shapeEntity.prototype.init = function(){
		this.shape = '';
		this.lengthA = 0;
		this.lengthB = 0;
		this.lengthC = 0;
		this.lengthD = 0;
		this.lengthE = 0;
		this.lengthF = 0;
		this.surface = 0;
	}

	
	/*
		GETTERS
	*/
	shapeEntity.prototype.getShape = function(){
		return this.shape;
	}
	
	shapeEntity.prototype.getSurface = function(){
		
		switch(this.shape){
			case 'round':
				this.surface = this.getRoundSurface(this.lengthA);
				break;
			case 'ellipse':
				this.surface = this.getEllipseSurface(this.lengthA, this.lengthB);
				break;
			case 'hose':
				this.surface = this.getHoseSurface(this.lengthA, this.lengthB);
				break;
			case 'square':
				this.surface = this.getSquareSurface(this.lengthA);
				break;
			case 'rectangle':
				this.surface = this.getRectangleSurface(this.lengthA, this.lengthB);
				break;
			case 'trapezium':
				this.surface = this.getTrapeziumSurface(this.lengthA, this.lengthB, this.lengthC);
				break;
			case 'pSection':
				this.surface = this.getPSectionSurface(this.lengthA, this.lengthB, this.lengthC, this.lengthD);
				break;
			case 'straightU':
				this.surface = this.getStraightUSurface(this.lengthA, this.lengthB, this.lengthC, this.lengthD);
				break;
			case 'roundU':
				this.surface = this.getRoundUSurface(this.lengthA, this.lengthB, this.lengthC, this.lengthD);
				break;
			case 'tProfile':
				this.surface = this.getTProfileSurface(this.lengthA, this.lengthB, this.lengthC, this.lengthD);
				break;
			case 'lProfile':
				this.surface = this.getLProfileSurface(this.lengthA, this.lengthB, this.lengthC, this.lengthD);
				break;
			case 'dProfile':
				this.surface = this.getDProfileSurface(this.lengthA, this.lengthB);
				break;
			case 'dProfile2':
				this.surface = this.getDProfileSurface2(this.lengthA, this.lengthB);
				break;
			case 'openDProfile':
				this.surface = this.getOpenDProfileSurface(this.lengthA, this.lengthB, this.lengthC);
				break;
			case 'openDProfile2':
				this.surface = this.getOpenDProfileSurface2 (this.lengthA, this.lengthB, this.lengthC);
				break;
			case 'eProfile':
				this.surface = this.getEProfileSurface(this.lengthA, this.lengthB, this.lengthC, this.lengthD, this.lengthE, this.lengthF);
				break;
			case 'hProfile':
				this.surface = this.getHProfileSurface(this.lengthA, this.lengthB, this.lengthC, this.lengthD);
				break;
		}
		
		return this.surface;
	}
	
	shapeEntity.prototype.getRoundSurface = function(A){
		var result;
			
		A = parseInt(A);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'roundLengthA', ''); return; }
		
		// basic circle surface formula:  pi * radius * radius
		result = Math.PI * ((A/2) * (A/2));
		return result;
	}

	shapeEntity.prototype.getHoseSurface = function(A, B){
		var result;
		
		
		A = parseInt(A);
		B = parseInt(B);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'hoseLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'hoseLengthB', ''); }
		
		if(A < 0 || B < 0){ return false; }
		
		if(A != '' && B != ''){
			if(B >= A){
				setError(text.hoseError, 'hoseLengthB', 'hoseLengthA');
				return;
			}
		
			// the surface of a circle with radius OD minus the surface of a circle with radius ID
			result = ((Math.PI* ((A/2)*(A/2))) - (Math.PI* ((B/2)*(B/2))) );
			return result;
		}
	}
	
	shapeEntity.prototype.getEllipseSurface = function(A, B){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'ellipseLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'ellipseLengthB', ''); }
		
		if(A < 0 || B < 0){ return; }			
		
		// basic ellips surface formula: pi * minor radius * major radius
		result = Math.PI * (A/2) * (B/2);
		return result;
	}
	
	shapeEntity.prototype.getSquareSurface = function(A){
		var result;
			
		A = parseInt(A);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'squareLengthA', ''); return; }
		
		// basic formula of the surface of a square
		result = A * A;
		return result;                 
	}
	
	shapeEntity.prototype.getRectangleSurface = function(A,B){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'rectangleLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'rectangleLengthB', ''); }
		
		if(A < 0 || B < 0){ return; }			
		
		// basic formula of the surface of a rectangle
		result = A * B;
		return result;
	}
	
	shapeEntity.prototype.getTrapeziumSurface = function(A, B, C){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'trapeziumLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'trapeziumLengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'trapeziumLengthC', ''); }
		
		if(A < 0 || B < 0 || C < 0){ return; }			
		
		if(A != '' && B != ''){
			if (A >= B){
				setError(text.trapeziumError, 'trapeziumLengthB', 'trapeziumLengthA');
				return;
			}
			
			if(C != ''){
				if (C==0) return 0;   
			
				// divides the trapezium in a rectangular area in the middle and two triangles
				// which combined form a rectangular area of size (B-A)/2 by H 
				result = ((A + B) / 2) * C;

				return result;
			}
		}
	}
	
	shapeEntity.prototype.getStraightUSurface = function(A, B, C, D){
		var result;
		var thickness;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		D = parseInt(D);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'straightULengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'straightULengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'straightULengthC', ''); }
		if(isNaN(D) || D < 0){ setError(text.numericError, 'straightULengthD', ''); }
		
		if(A < 0 || B < 0 || C < 0 || D < 0){ return; }
			
		if(A != '' && C != ''){
			if(C >= A){
				setError(text.straightUError, 'straightULengthC', 'straightULengthA');
				return;
			}
		}
			
		if(B != '' && D != ''){
			if(D >= B){
				setError(text.straightUError, 'straightULengthD', 'straightULengthB');
				return;
			}
		}
			
		if(A != '' && B != '' && C != '' && D != ''){
			// adds the surface of two colums of height B to a connecting rectangle
			thickness = (A-C)/2;
			result =  2*B*thickness +  C*D;
			return result;
		}
	}
	
	shapeEntity.prototype.getRoundUSurface = function(A, B, C, D){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		D = parseInt(D);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'roundULengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'roundULengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'roundULengthC', ''); }
		if(isNaN(D) || D < 0){ setError(text.numericError, 'roundULengthD', ''); }
		
		if(A < 0 || B < 0 || C < 0 || D < 0){ return; }
			
		if(A != '' && C != ''){
			if(C >= A){
				setError(text.roundUError, 'roundULengthC', 'roundULengthA');
				return;
			}
		}
			
		if(B != '' && D != ''){
			if(D >= B){
				setError(text.roundUError, 'roundULengthD', 'roundULengthB');
				return;
			}
		}
			
		if(A != '' && B != '' && C != '' && D != ''){
			// adds the surface of two colums of height B to a connecting rectangle
			thickness = (A-C)/2;
			result =  2*B*thickness +  C*D;
			return result;
		}
	}
	
	shapeEntity.prototype.getTProfileSurface = function(A, B, C, D){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		D = parseInt(D);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'tProfileLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'tProfileLengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'tProfileLengthC', ''); }
		if(isNaN(D) || D < 0){ setError(text.numericError, 'tProfileLengthD', ''); }
		
		if(A < 0 || B < 0 || C < 0 || D < 0){ return; }			
			
		if(A != '' && D != ''){
			if (D >= A) {
				setError(text.tProfileDError, 'tProfileLengthD', 'tProfileLengthA');
				return;
			}
		}
			
		if(C != '' && B != ''){
			if (C <= B) {
				setError(text.tProfileCError, 'tProfileLengthC', 'tProfileLengthB');
				return;
			}
			
			// add top horizontal rectangle to bottom vertical rectangle
			result = (A * B + D * (C - B));
			return result;
		}
	}
	
	shapeEntity.prototype.getLProfileSurface = function(A, B, C, D){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		D = parseInt(D);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'lProfileLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'lProfileLengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'lProfileLengthC', ''); }
		if(isNaN(D) || D < 0){ setError(text.numericError, 'lProfileLengthD', ''); }
		
		if(A < 0 || B < 0 || C < 0 || D < 0){ return; }			
			
		if(A != '' && D != ''){
			if(A >= D){
				setError(text.lProfileAError, 'lProfileLengthA', 'lProfileLengthD');
				return;
			}
		}

		if(B != '' & C != ''){
			if(B >= C){
				setError(text.lProfileBError, 'lProfileLengthB', 'lProfileLengthC');
				return;
			}
			
			// add left to right rectangle
			result = (A * C + (B * (D - A)));
			//result = (C*D + B*(A-C));
			return result;
		}
	}
	
	shapeEntity.prototype.getDProfileSurface = function(A, B){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'dProfileLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'dProfileLengthB', ''); }
		
		if(A < 0 || B < 0){ return; }
		
		// just a half ellipse
		if(A != '' && B != ''){
			result  = (A * (B - (A / 2))) + ((Math.PI * (A / 2) * (A / 2)) / 2); 
			return result;
		}
	}
	
	shapeEntity.prototype.getOpenDProfileSurface = function(A, B, C){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'openDProfileLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'openDProfileLengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'openDProfileLengthC', ''); }
		
		if(A < 0 || B < 0 || C < 0){ return; }
						
		if(C != '' && B != ''){
			if (2 * C >= B) {
				setError(text.openDProfileBError, 'openDProfileLengthC', 'openDProfileLengthB');
				return;
			}
		}
	
		if(A != '' && C != ''){
			if (2 * C >= A) {
				setError(text.openDProfileCError, 'openDProfileLengthC', 'openDProfileLengthA');
				return;
			}

			// first calculates the surface of a half ellipse minus a smaller half ellipse and then adds the line at the bottom  
			result = (A * (B - (A / 2))) + ((Math.PI * (A / 2) * (A / 2)) / 2);
			
			result = result - ((A - (C * 2)) * ((B - (C * 2)) - ((A - (C * 2)) / 2))) + ((Math.PI * ((A - (C * 2)) / 2) * ((A - (C * 2)) / 2)) / 2);
	
			return result;
		}
	}
	
		// D profile page 40
	shapeEntity.prototype.getDProfileSurface2 = function(A, B){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'dProfile2LengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'dProfile2LengthB', ''); }
		
		if(A < 0 || B < 0){ return; }
			
		if(A != '' && B != ''){
			// Rectangular area plus the half of an circle
			// Note: A/2 equals the radius of the circle
			result = ((Math.PI * (A / 2) * (B / 2)) / 2);
			return result;
		}
	}
	
	shapeEntity.prototype.getOpenDProfileSurface2 = function(A, B, C){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'openDProfileLength2A', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'openDProfileLength2B', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'openDProfileLength2C', ''); }
		
		if(A < 0 || B < 0 || C < 0){ return; }
			
		if(C != '' && B != ''){
			if (2 * C >= B) {
				setError(text.openDProfile2BError, 'openDProfileLength2C', 'openDProfileLength2B');
				return;
			}
		}
		
		if(A != '' && C != ''){
			if (2 * C >= A) {
				setError(text.openDProfile2CError, 'openDProfileLength2C', 'openDProfileLength2A');
				return;
			}
			
			// Subtract a small circle from a larger circle
			// add the area underneath the curved area
			result = ((Math.PI * (A / 2) * (B / 2)) / 2) - ((Math.PI * ((A - (C * 2)) / 2) * ((B - (C * 2)) / 2)) / 2);
			return result;
		}
	}
	
	shapeEntity.prototype.getPSectionSurface = function(A, B, C, D){
		var result;
		var alpha;
		var temp;
		
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		D = parseInt(D);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'pSectionLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'pSectionLengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'pSectionLengthC', ''); }
		if(isNaN(D) || D < 0){ setError(text.numericError, 'pSectionLengthD', ''); }
		
		if(A < 0 || B < 0 || C < 0 || D < 0){ return; }
			
		if(B != '' && C != ''){
			if(C >= B){
				setError(text.pSectionCError, 'pSectionLengthC', 'pSectionLengthB');
				return;
			}
		}
		
		if(B != '' && D != ''){
			if(D > (B / 2)){
				setError(text.pSectionDError, 'pSectionLengthD', 'pSectionLengthB');
				return;
			}
		}

		if(A != '' && B != ''){
			if(B > A){
				setError(text.pSectionBError, 'pSectionLengthB', 'pSectionLengthA');
				return;
			}
			
			// first the basic shapes are calculate with an overlap and then the overlap is
			// subtracted from the result
			
			// first add the big circle to the square (overlap will be removed later)
			result = (Math.PI*(B/2)*(B/2)) + (D * (A-(B/2)));
			
			// subtract the hole
			result = result - (Math.PI*(C/2)*(C/2));
			
			// calculate overlap part I (calculate alpha and area of pie-piece)
			alpha = Math.acos((B/2-D)/(B/2));
			temp = (alpha/(2*Math.PI)) * (Math.PI*(B/2)*(B/2));
			// calculate overlap part II (subtract triangle)
			temp = temp - ((1/2) * (B/2-D) * (Math.sin(alpha)*(B/2)));
			
			// subtract overlap from result         
			result = result - temp;
			return result;
		}
	}
	
	shapeEntity.prototype.getHProfileSurface = function(A, B, C, D){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		D = parseInt(D);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'hProfileLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'hProfileLengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'hProfileLengthC', ''); }
		if(isNaN(D) || D < 0){ setError(text.numericError, 'hProfileLengthD', ''); }
		
		if(A < 0 || B < 0 || C < 0 || D < 0){ return; }
						
		if(A != '' && D != ''){
			if(D >= A){
				setError(text.hProfileBError, 'hProfileLengthB', 'hProfileLengthA');
				return;
			}
		}
			
		if(A != '' && B != '' && C != '' && D != ''){
			// the connecting horizontal rectangle + 2 times the vertical rectangles
			result = (C*D + 2*B*A);
			return result;
		}
	}
	
	shapeEntity.prototype.getEProfileSurface = function(A, B, C, D, E, F){
		var result;
			
		A = parseInt(A);
		B = parseInt(B);
		C = parseInt(C);
		D = parseInt(D);
		F = parseInt(F);
		
		if(isNaN(A) || A < 0){ setError(text.numericError, 'eProfileLengthA', ''); }
		if(isNaN(B) || B < 0){ setError(text.numericError, 'eProfileLengthB', ''); }
		if(isNaN(C) || C < 0){ setError(text.numericError, 'eProfileLengthC', ''); }
		if(isNaN(D) || D < 0){ setError(text.numericError, 'eProfileLengthD', ''); }
		if(isNaN(E) || E < 0){ setError(text.numericError, 'eProfileLengthE', ''); }
		if(isNaN(F) || F < 0){ setError(text.numericError, 'eProfileLengthF', ''); }
		
		if(A < 0 || B < 0 || C < 0 || D < 0 || E < 0 || F < 0){ return; }			
			
		if(A != '' && D != ''){
			if(A <= D){
				setError(text.eProfileAError, 'eProfileLengthA', 'eProfileLengthD');
				return;
			}
		}
			
		if(B != '' && C != '' & F != ''){
			if (F <= B+C) {
				setError(text.eProfileFError, 'eProfileLengthF', 'eProfileLengthB;eProfileLengthC');
				return;
			}
		}
					
		if(A != '' && B != '' && C != '' && D != '' && E != '' && F != ''){
			// combines one of the formulas for the D profile with two added rectangular areas below that
			result = ((Math.PI * (E / 2) * ((F - ( C + B)) / 2)) / 2);
			result = result - (Math.PI * ((E - (D * 2)) / 2) * (((F - (C + B) - (D * 2) / 2)) / 2));
			result = result + (C * D) + (B * A);
	
			return result;
		}
	}
	
	
	/*
		GETTERS
	*/
	shapeEntity.prototype.getLengthA = function(){
		return this.lengthA;
	}
	
	shapeEntity.prototype.getLengthB = function(){
		return this.lengthB;
	}
	
	shapeEntity.prototype.getLengthC = function(){
		return this.lengthC;
	}
	
	shapeEntity.prototype.getLengthD = function(){
		return this.lengthD;
	}
	
	shapeEntity.prototype.getLengthE = function(){
		return this.lengthE;
	}
	
	shapeEntity.prototype.getLengthF = function(){
		return this.lengthF;
	}
	
	
	/*
		SETTERS
	*/
	
	shapeEntity.prototype.setShape = function(shape){
		if(shape == ''){
			return false;
		}else{
			this.shape = shape;
			return true;
		}	
	}
	
	shapeEntity.prototype.setLengthA = function(lengthA){
		this.lengthA = lengthA;
	}
	
	shapeEntity.prototype.setLengthB = function(lengthB){
		this.lengthB = lengthB;
	}
	
	shapeEntity.prototype.setLengthC = function(lengthC){
		this.lengthC = lengthC;
	}
	
	shapeEntity.prototype.setLengthD = function(lengthD){
		this.lengthD = lengthD;
	}
	
	shapeEntity.prototype.setLengthE = function(lengthE){
		this.lengthE = lengthE;
	}
	
	shapeEntity.prototype.setLengthF = function(lengthF){
		this.lengthF = lengthF;
	}


