function Form_Handler() {
	this.name = "";
	this.formElements = new Array();
	this.formElements[0] = new Object();
	
	// Variable für PHP-Script
	this.php = "";
	
	// Standard Texte
	this.formValues = new Array();
	this.formValues[0] = "Bitte geben Sie Ihren Namen ein...";
	this.formValues[1] = "Bitte geben Sie Ihre E-Mail Addresse ein...";
	this.formValues[2] = "Bitte geben Sie Ihre Telefonnummer ein...";
	this.formValues[3] = "Bitte hinterlassen Sie mir eine Nachricht...";
}


// Formularprüfung
Form_Handler.prototype.checkForm = function() {
	var allCorrect = true;
	
	for (var element in this.formElements[0]) {
		//alert(element + "  " + this.formElements[0][element]);
		switch(element) {
		case "name":
			if (this.formElements[0][element] < 3 || this.formElements[0][element] == "Erforderlich" || this.formElements[0][element] == this.formValues[0]) {
				document.getElementById(element).setAttribute("style", "border: solid 3px #ff0000; text-align: center; color: red;");
				window.document.getElementById(element).value="Erforderlich";
				allCorrect = false;
			}
			break;
		case "email":
			if (this.formElements[0][element] < 3 || this.formElements[0][element] == "Erforderlich" || this.formElements[0][element].match(/\w*@\w.*\.\w\w*/)==null || this.formElements[0][element] == this.formValues[1]) {
				document.getElementById(element).setAttribute("style", "border: solid 3px #ff0000; text-align: center; color: red;");
				window.document.getElementById(element).value="Erforderlich";
				allCorrect = false;
			}
			break;
		case "telefon":
			if (this.formElements[0][element] < 3 || this.formElements[0][element] == "Erforderlich" || this.formElements[0][element] == this.formValues[2]) {
				document.getElementById(element).setAttribute("style", "border: solid 3px #ff0000; text-align: center; color: red;");
				window.document.getElementById(element).value="Erforderlich";
				allCorrect = false;
			}
			break;
		case "nachricht":
			break;
		default:
			break;
		}
	}
	return allCorrect;
};


Form_Handler.prototype.clearForm = function(formElement) {
	for (var z=0; z<this.formValues.length; z++) {
		if(document.getElementById(formElement).value == "Erforderlich" || this.formValues[z]) {
			document.getElementById(formElement).setAttribute("style", "color: black; font-style: normal;");
			document.getElementById(formElement).value="";
			break;
	    }
	}
};

// Formular absenden
Form_Handler.prototype.sendForm = function(formName) {
	this.name = formName;
	this.form2array();
	if (this.checkForm()) {
		this.send2Php();
	}
	return false;
};

// Formular an php Handler senden
Form_Handler.prototype.send2Php = function() {
	
	var aString = "";
	for (var element in this.formElements[0]) {
		aString += element+"="+this.formElements[0][element]+"&";
	}
	this.php += aString.substr(0, aString.length-1);
	
	//alert(this.php);
	
	var req = null;
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		   req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (req != null) {
		req.open("POST", "fileadmin/berlin/form_handler.php", true);
		req.onreadystatechange = formdata;
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(this.php);
	}

	function formdata() {
		//alert(req.readyState + " " + req.status + " ");
		if (req.readyState == 4) {
			document.getElementById("contact_form").innerHTML = req.responseText;
		}
	}
};

// Formular in Array speichern
Form_Handler.prototype.form2array = function() {	
	for (var i=0; i<document.forms[this.name].elements.length; i++) {
		if (document.forms[this.name].elements[i].name != undefined) {
			this.formElements[0][document.forms[this.name].elements[i].name] = document.forms[this.name].elements[i].value;
		}
	}
	
};

myFormhandler = new Form_Handler();

/* GoogleMaps */
function initialize(lat, long, markerText) {
	if (GBrowserIsCompatible()) {
		
		var map = new GMap2(document.getElementById("map_canvas"));
		var point = new GLatLng(lat, long);
		
		map.setMapType(G_SATELLITE_MAP);
		map.setCenter(point, 15);
		map.addOverlay(new GMarker(point));
		map.openInfoWindow(map.getCenter(), document.createTextNode(markerText));
		map.setUIToDefault();
	}
}

/* mouseover & mouseout for booking form */

function b_mouseover(id) {
	document.getElementById(id).style.backgroundColor = "#ff7777";
	//document.getElementById(id).style.background = "url(fileadmin/mds/apartment_display/bg_b2.png)";
}

function b_mouseout(id) {
	document.getElementById(id).style.backgroundColor = "red";
	//document.getElementById(id).style.background = "url(fileadmin/mds/apartment_display/bg_b.png)";
}
