/* Varibalen */
var errors = 0;


/* Funktionen */
function openContact() {
	document.getElementById('light').style.display	='block';
	document.getElementById('fade').style.display	='block';
	
	hideTagCloud();
}

function closeContact() {
	document.getElementById('light').style.display	='none';
	document.getElementById('fade').style.display	='none';
		
	showTagCloud();
	hideErrors();
	clearInputs();
	
	errors = 0;
}

function hideTagCloud(){
	if (document.getElementById('wpcumuluswidgetcontent') != null){
		document.getElementById('wpcumuluswidgetcontent').style.visibility='hidden';
	}
}

function showTagCloud(){
	if (document.getElementById('wpcumuluswidgetcontent') != null){
		document.getElementById('wpcumuluswidgetcontent').style.visibility='visible';
	}
}

function hideErrors(){
	document.getElementById('first_name_error').style.visibility 	= "hidden";
	document.getElementById('last_name_error').style.visibility 	= "hidden";
	document.getElementById('phone_error').style.visibility 	= "hidden";
	document.getElementById('email_error').style.visibility 	= "hidden";
	document.getElementById('message_error').style.visibility 	= "hidden";
}

function clearInputs(){
	document.getElementById('first_name').value	= "";
	document.getElementById('last_name').value	= "";
	document.getElementById('phone').value		= "";
	document.getElementById('email').value		= "";
	document.getElementById('message').value	= "";
}

function checkReadyToSend(){
	
}

function AJAX_sendMail(first_name, last_name, phone, email, message){
	var xmlHttp = null;
	
	// Mozilla, Opera, Safari sowie Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
	if (!xmlHttp) {
		// Internet Explorer 6 und älter
		try {
			xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlHttp  = null;
			}
		}
	}
	if (xmlHttp) {
		xmlHttp.open('GET', 'http://www.innopact.de/php_test/contact.php?first_name='+first_name+'&last_name='+last_name+'&phone='+phone+'&email='+email+'&message='+message, true);
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {				
				document.getElementById('contact_form_message').innerHTML = xmlHttp.responseText;
			}
		};
		xmlHttp.send(null);
	}
}

function sendMail(first_name, last_name, phone, email, message){
	if(errors == 0){		
		if(document.getElementById('message').value != ""){		
			AJAX_sendMail(document.getElementById(first_name).value, document.getElementById(last_name).value, document.getElementById(phone).value, document.getElementById(email).value, document.getElementById(message).value);
		}
		else{
			alert('Bitte füllen Sie alle Felder korrekt aus.');
		}
	}
	else{
		alert('Bitte füllen Sie alle Felder korrekt aus.');
	}
}


function AJAX_checkValue(checkElement, checkType, errorElement){
	var xmlHttp = null;
	
	// Mozilla, Opera, Safari sowie Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
	if (!xmlHttp) {
		// Internet Explorer 6 und älter
		try {
			xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlHttp  = null;
			}
		}
	}
	if (xmlHttp) {
		xmlHttp.open('GET', 'http://www.innopact.de/php_test/check_form.php?checkType='+checkType+'&checkElement='+checkElement, true);
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {			
				if (xmlHttp.responseText == "false"){
					document.getElementById(errorElement).style.visibility = "visible";
					errors = errors + 1;
				}
				else{
					document.getElementById(errorElement).style.visibility = "hidden";
					if(errors > 0){
						errors = errors - 1;
					}
				}				
			}
		};
		xmlHttp.send(null);
	}
}


function checkValue(checkElement, checkType, errorElement){	
	AJAX_checkValue(checkElement, checkType, errorElement);	
}

function AJAX_checkReadyToSend(checkElement, checkType){
	var xmlHttp = null;
	
	// Mozilla, Opera, Safari sowie Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
	if (!xmlHttp) {
		// Internet Explorer 6 und älter
		try {
			xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlHttp  = null;
			}
		}
	}
	if (xmlHttp) {
		xmlHttp.open('GET', 'http://www.innopact.de/php_test/check_form.php?checkType='+checkType+'&checkElement='+checkElement, true);
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {			
				if (xmlHttp.responseText == "false"){
					errors = errors + 1;
				}				
			}
		};
		xmlHttp.send(null);
	}
}
