// CREATING THE REQUEST

function createRequestObject() {
	try {
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
		alert('Sorry, but your browser doesn\'t support XMLHttpRequest.');
	}
	return xmlhttp;
}

var http = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg() {
	var url = 'kontakt/image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback) {
	sess.open('POST', 'kontakt/newsession.php', true);
	sess.send(null);
	http.open('POST', url, true);
	http.onreadystatechange = displayimg;
	http.send(null);
}

function displayimg()
{
	if(http.readyState == 4)
	{
		var showimage = http.responseText;
		document.getElementById('captchaimage').innerHTML = showimage;
	}
}

// SUBMISSION

function check()
{
	var submission = document.getElementById('captcha').value;
//	alert(submission);
	var url = 'kontakt/process.php?captcha=' + submission;
//	alert(url);
	docheck(url, displaycheck);
}

function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

//SUBMIT THE FORM, IF THE CAPTCHA IS CORRECT
function submitform(){
    var name = document.getElementById("name").value;
    var subject = document.getElementById("subject").value;
    var email = document.getElementById("email").value;
    var msg = document.getElementById("msg").value;
    
    document.getElementById('loading').style.display = 'block';
    document.getElementById('elem').style.display = 'none';

//document.captchaform.submit.disabled = 'true'; //DISABLE THE SUBMIT BUTTON
    http.open('GET', 'kontakt/mailer.php?name=' +name+'&subject=' +subject+'&email=' +email+ '&msg='+escape(msg)); 
	http.onreadystatechange = printit;
	http.send(null);

}

//PRINT THE RESPONSE FROM PHP
function printit()
{
	if(http.readyState == 4)
	{
	
	document.getElementById('loading').style.display = 'none';
	var response = http.responseText;
	var mainerr = '0';
//	alert(response);
	
	if (response.search(/err_name.+/) != -1) {
	    document.getElementById('name').style.border = '1px solid #c24949';
		document.getElementById('name').style.background = '#ffbcbc';
		document.getElementById('nameerror').innerHTML = '<img src="pix/error.gif">';
        mainerr = '1';
    } else {
		document.getElementById('name').style.border = '1px solid #49c24f';
		document.getElementById('name').style.background = '#bcffbf';
		document.getElementById('nameerror').innerHTML = '';
    }
	if (response.search(/err_email_empty.+/) != -1) {
        document.getElementById('email').style.border = '1px solid #c24949';
		document.getElementById('email').style.background = '#ffbcbc';
		document.getElementById('emailerror').innerHTML = '<img src="pix/error.gif">';
        mainerr = '1';
    } else {
		document.getElementById('email').style.border = '1px solid #49c24f';
		document.getElementById('email').style.background = '#bcffbf';
		document.getElementById('emailerror').innerHTML = '';
    }
	if (response.search(/err_email_invalid.+/) != -1) {
        document.getElementById('email').style.border = '1px solid #c24949';
		document.getElementById('email').style.background = '#ffbcbc';
		document.getElementById('emailerror').innerHTML = '<img src="pix/error.gif">';
        mainerr = '1';
    } else {
		document.getElementById('email').style.border = '1px solid #49c24f';
		document.getElementById('email').style.background = '#bcffbf';
		document.getElementById('emailerror').innerHTML = '';
    }
	if (response.search(/err_msg.+/) != -1) {
        document.getElementById('msg').style.border = '1px solid #c24949';
		document.getElementById('msg').style.background = '#ffbcbc';
		document.getElementById('msgerror').innerHTML = '<img src="pix/error.gif">';
        mainerr = '1';
        } else {
		document.getElementById('msg').style.border = '1px solid #49c24f';
		document.getElementById('msg').style.background = '#bcffbf';
		document.getElementById('msgerror').innerHTML = '';
    }

    if (mainerr == '0') {
    	document.getElementById('elem').style.display = 'none';
        document.getElementById('results').innerHTML = http.responseText; //PRINT THE PHP'S RESPONSE IN THE RESULTS DIV
    } else {
        document.getElementById('elem').style.display = 'block';
    }
	
//    check();

	}
}

	
function displaycheck()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1') //CAPTCHA IS CORRECT
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
			document.getElementById('captchaerror').innerHTML = '';
	
			submitform(); //SUBMIT THE FORM
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';
			document.captchaform.captcha.value = ''; //RESET THE CAPTCHA INPUT'S VALUE
			document.captchaform.captcha.focus(); //CHANGE THE FOCUS TO CAPTCHA INPUT
			document.getElementById('captchaerror').innerHTML = '<img src="pix/error.gif">';
		}
	}
}

