/*******************************************************************
 * File: activexObject.js                                          *
 *                                                                 *
 * Autor: Christian Schwarze                                       *
 * Copyright: Christian Schwarze                                   *
 * Stand: 09.11.2007                                               *
 *                                                                 *
 * Liefert die eigentliche Funktion zum Senden und Empfangen       *
 * von Daten im Hintergrund einer Webseite						   *
 *******************************************************************/
var counter = 0;
function saveHeadData(){
	properties['action'] 		= 'loadBackground';
	properties['uri'] 			= '/res/head.php';
	properties['addrSonst'] 	= document.getElementById('addrSonst').value;
	properties['objectName'] 	= document.getElementById('objectname').value;
	properties['zip'] 			= document.getElementById('zip_code').value;
	properties['street'] 		= document.getElementById('street').value;
	properties['city'] 			= document.getElementById('city').value;

	// Show AnimImage
	var waiting = document.getElementById("waitState");
	waiting.style.visibility="visible";
	window.setTimeout("actionNow(properties, 'head', true)", 500);
	return false;
}

function calculation(){
	counter++;
	if(counter > 2){
		properties['action'] 		= 'loadBackground';
		properties['uri'] 			= '/res/calculator_Fl.php';
		properties['flaeche'] 		= document.getElementById('flaeche').value!='' ? parseFloat(document.getElementById('flaeche').value+'.'+document.getElementById('flaeche_1').value) : 0;
		properties['schichtdicke'] 	= document.getElementById('schicht').value!='' ? parseFloat(document.getElementById('schicht').value) : 0;
		properties['nivellierung'] 	= document.getElementById('nivellierung_1').checked==true	?	'nein'	:	'ja';
		properties['format_bp'] = '925_2';

		var waiting = document.getElementById("waitState");
		waiting.style.visibility="visible";
		
		window.setTimeout("actionNow(properties, 'calculator', true)", 500);
		return false;

	}else{
		var error = document.getElementById('error');
	 	error.style.border = "none";
		error.innerHTML = '';
		error.style.display = 'none';
		return false;
	}
}

function actionNow(arr, callBackFunction, showWaitingState){
	requestEngine(arr, callBackFunction, showWaitingState);
}

function requestEngine(properties, ausgabeFunktion, showWaitingState){
	// try/catch Block um Fehler abzufangen
	try{
		var waiting = document.getElementById("waitState");
		// Variablen deklarieren
		var data = '';
		// Post Variablen definieren, diese sollte als Parameter 
		// an die Funktion übergeben werden !
		for(property in properties){
			data += '&' + property + '=' + encodeURIComponent(properties[property]);
		}
		// Basis ist das Verzeichnis der Datei welche 
		// diese Funktion includiert hat und aufruft
		var xhttp = false;
		
		// Überprüfen welches ActiveX bzw. welcher Browser vorliegt!
		xhttp = getActiveX();
		
		// xhttp - Objekt initialisiert ist dann Anfrage senden
		if( xhttp ){
			xhttp.open('POST', properties['uri'], true);        
			xhttp.setRequestHeader('Method', 'POST' + properties['uri'] + ' HTTP/1.1');
			xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xhttp.onreadystatechange = function(){
				if (xhttp.readyState != 4){
					// Solange uri nichts zurückliefert, warten
					return;
				}
				else{
					var result=xhttp.responseText;
					
					switch(ausgabeFunktion){
						case "head":
						break;
						case "calculator":
						 var output = document.getElementById('ergebnis');
						 var error = document.getElementById('error');
						 error.style.display = 'block';
						 msg = result.split('][');
						 output.innerHTML = '';
						 if(msg[2]=='0'){
						 	output.innerHTML = msg[0];
						 }
						 error.innerHTML = '';
						 error.innerHTML = msg[1];
						break;						
						default:
						break;
					}
					
					// Hide AnimImage
					if(showWaitingState==true){
						waiting.style.visibility="hidden";
					}
				}
			}
		}
		else{
			// return Fehler
			return "error";
		}
		xhttp.send( data );
		delete xhttp;
	}
	catch(e){
		alert("Fehler in Anwendung" + e);
	}
	// Ende try/catch Block
}

function getActiveX(){
	try{
		xhttp = new ActiveXObject('Msxml2.XMLHTTP');
	} 
	catch (e){
		try { xhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (oc)  { xhttp = null;	}
	}
	
	if(!xhttp && typeof XMLHttpRequest != 'undefined'){
		xhttp = new XMLHttpRequest();
	}
	
	return xhttp;
}

/*
 * Sonstige Funktionalitäten:
 * IwView
 */
function checkFields(obj){
	calculation();
}

function showAuswahl(state){
	var auswahl = document.getElementById('bapladi');
	if(state=='show'){
		auswahl.style.display="";
		
	}else{
		auswahl.style.display="none";	
	}
	calculation();
}

function checkSchichtdicke(state){
	var fieldToShow = document.getElementById('schichtdicke');
	if(state=='show'){
		fieldToShow.style.visibility="visible";
	}else{
		fieldToShow.style.visibility="hidden";	
	}
	calculation();
}

function clearErg(){
	document.getElementById('ergebnis').innerHTML='';
}

flag = 0;
flashObj = '';
function highlight(id, flash){
	if(flag == 0){
		if(flash != ''){
			var span = document.getElementById(flash+'_span');
			flashObj = span.innerHTML;
			span.innerHTML = '<img class="menupics" alt="" src="/res/images/bodensystem220x178.png"/>';
		}
	var obj = document.getElementById(id);
	var obj_nav = document.getElementById(id+'_links');
	obj.style.backgroundColor="#01AEF0";
	obj_nav.style.display="block";
	obj_nav.style.visibility="visible";
	obj_nav.style.backgroundColor="";
	flag = 1;
	}
}

function downlight(id, flash){
	if(flash != ''){
		var span = document.getElementById(flash+'_span');
		span.innerHTML = flashObj;
		flashObj = '';
	}
	var obj = document.getElementById(id);
	var obj_nav = document.getElementById(id+'_links');
	obj.style.backgroundColor="#0434B2";
	obj_nav.style.display="none";
	obj_nav.style.visibility="hidden";
	obj_nav.style.backgroundColor="";
	flag = 0;
}

