var contactBox = null;
var contactBoxIframe = null;

function openContactForm() {
	if (contactBox != null)
		closeContactBox();
	getAjaxPage('index.php?action=show&mod=contact&ajax=1', openContactBoxHandler);
}

function openContactBoxHandler() {
	if (httpreq.readyState == 4) {
		contactBoxIframe = document.createElement('iframe');
		contactBoxIframe.id = 'contactBoxIframe';
		document.body.appendChild(contactBoxIframe);
		var response = httpreq.responseText;
		contactBox = getContactBox(response);
		document.body.appendChild(contactBox);
	}
}

function getContactBox(content) {
	var imageFolder = "";
        var box = document.createElement('div');
        box.id = 'contactBox';
        var d = document.createElement('div');
        d.id = 'contactBoxContent';

	d.innerHTML = content;

        box.appendChild(d);

        var btnBar = document.createElement('div');
        btnBar.id = 'btnBar';

        btnClose = document.createElement('input');
        btnClose.id = 'btnClose';
        btnClose.className = 'contactBoxButton';
	btnClose.type = 'button';
	btnClose.value = 'Abort';
        btnClose.onclick = closeContactBox;
        btnBar.appendChild(btnClose);

        // box.appendChild(btnBar);

        return box;
}

function submitContactBox() {
	if (checkContactForm()) {
		params = 'ok=1';
		params += '&yemail='+document.forms['contactForm'].yemail.value;
		params += '&yname='+document.forms['contactForm'].yname.value;
		params += '&ymessage='+document.forms['contactForm'].ymessage.value;
		params += '&subject='+document.forms['contactForm'].subject.value;
		params += '&subject_other='+document.forms['contactForm'].subject_other.value;
		postAjaxPage('index.php?ajax=1&action=show&mod=contact', params, submitContactBoxHandler);
	}
}

function submitContactBoxHandler() {
	if (httpreq.readyState == 4) {
		var response = httpreq.responseText;
		// show confirmation notice
		var el = document.getElementById('contactBoxContent');
        	var d = document.createElement('div');
		d.innerHTML = response;
		clearChildren(el);
		el.appendChild(d);
        	var d = document.createElement('div');
		d.className = 'closeButtonDiv';
		btnClose = document.createElement('input');
		btnClose.id = 'btnClose';
		btnClose.className = 'contactBoxButton';
		btnClose.type = 'button';
		btnClose.value = 'Close';
		btnClose.onclick = closeContactBox;
		d.appendChild(btnClose);
		el.appendChild(d);
	}
}

function clearChildren(cell) {
	if ( cell.hasChildNodes() ) {
    		while ( cell.childNodes.length >= 1 ) {
		        cell.removeChild( cell.firstChild );       
		} 
	}

}

function closeContactBox() {
	contactBox.parentNode.removeChild(contactBox);
	contactBox = null;
	contactBoxIframe.parentNode.removeChild(contactBoxIframe);
	contactBoxIframe = null;
}

function isDE() {
    	var de = /\.de$/;
	if (de.test(window.location.host))
		return true;
	else
		return false;
}

function checkContactForm() {
	var error = '';
	/* if (validateEmpty(document.getElementById('c_subject'))) {
		if (isDE())
			error+= "Der Betreff fehlt\n";
		else
			error+= "The subject is missing\n";
	} */
	if (validateEmpty(document.getElementById('c_other_subject'))) {
		if (isDE())
			error+= "Der Betreff fehlt\n";
		else
			error+= "The subject is missing\n";
	}
	if (validateEmpty(document.getElementById('c_yourname'))) {
		if (isDE())
			error+= "Ihr Name fehlt\n";
		else
			error+= "Your name is missing\n";
	}
	if (validateEmpty(document.getElementById('c_your_email'))) {
		if (isDE())
			error+= "Ihre Emailadresse fehlt\n";
		else
			error+= "Your email address is missing\n";
	}
	if (validateEmpty(document.getElementById('c_your_message'))) {
		if (isDE())
			error+= "Ihre Nachricht fehlt\n";
		else
			error+= "Your message is missing\n";
	}
	if (error.length > 0) {
		if (isDE())
			alert("Es wurden Fehler in Ihrer Eingabe gefunden:\n\n"+error);
		else
			alert("There are errors in your input:\n\n"+error);
		return false;
	}
	return true;
}

