// JavaScript Document
var cpEmail = new cpaint();
cpEmail.set_transfer_mode('post');
cpEmail.set_response_type('XML');
cpEmail.set_persistent_connection(false);
//cpEmail.set_debug(1);

function validateContactForm(e) {
	if(document.getElementById('blnActive').value == 1) {
		alert('Please wait email is still being sent!');
	} else {
		var Vaild = true;
		if(document.getElementById('txtContactFormName').value == '') {
			alert('Please enter your name!');
			document.getElementById('txtContactFormName').select();
			document.getElementById('txtContactFormName').focus();
			Vaild = false;
		}
		if(Vaild) {
			if(document.getElementById('txtContactFormEmail').value == '') {
				alert('Please enter your e-mail address!');
				document.getElementById('txtContactFormEmail').select();
				document.getElementById('txtContactFormEmail').focus();
				Vaild = false;
			}
		}
		if(Vaild) {
			if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('txtContactFormEmail').value)) {
				alert('Invalid e-mail address! Please re-enter.');
				document.getElementById('txtContactFormEmail').select();
				document.getElementById('txtContactFormEmail').focus();
				Vaild = false;
			}
		}
		if(Vaild) {
			if(document.getElementById('txtContactFormComment').value == '') {
				alert('Please enter your comment');
				document.getElementById('txtContactFormComment').select();
				document.getElementById('txtContactFormComment').focus();
				Vaild = false;
			}
		}
		
		var formName = document.getElementById('txtContactFormName');
		var formEmail = document.getElementById('txtContactFormEmail');
		var formComment = document.getElementById('txtContactFormComment');
		
		if(Vaild) {
			document.getElementById('emailMsgInit').innerHTML = 'Sending Email Please Wait!';
			document.getElementById('emailMsgInit').style.background = "#CEE0EC";
			document.body.style.cursor = "wait";
			document.getElementById('blnActive').value = 1;
			cpEmail.call('lib/php/lib_mail.php', 'Email', handleEmail, formName.value, formEmail.value, formComment.value);
		}
	}
	
}

function handleEmail(result) {
	
	var Err = result.getElementsByTagName('err').item(0).firstChild.nodeValue;
	
	if(Err == 0) {
		document.getElementById('emailMsg').style.display = "block";
	} else {
		document.getElementById('emailMsgErr').style.display = "block";
	}
	document.getElementById('blnActive').value = 0;
	document.getElementById('emailMsgInit').style.display = "none";
	document.getElementById('txtContactFormName').value = "";
	document.getElementById('txtContactFormEmail').value = "";
	document.getElementById('txtContactFormComment').value=  "";
	document.body.style.cursor = "default";
	
}