//##########################################
//																				//
//	This function sends the form via POST	//
//	if is not to use AJAX, and call and		// 
//	sendByAJAX if is to use AJAX.			 		//
//																				//
//##########################################

function sendEmail(useAJAX, form_to_send){
	if($('mail_ok').style.display == "block" && $('name_ok').style.display == "block" && $('subject_ok').style.display == "block" && $('message_ok').style.display == "block"){
		if(useAJAX == true){
			alert("Send E-Mail by AJAX.");
		} else {
			if(form_to_send != ""){
				$(form_to_send).submit();
			} else {
				alert("Ocorreu um erro! Por favor tente mais tarde.\nPedimos desculpa pelo incómodo.");
			}
		}
	} else {
		alert("O formulário contém erros! Por favor corrija os campos vazios ou a vermelho e volte a tentar.");
	}
}

//##########################################
//																				//
//	This function clears the form 				//
//	including	the BG colors and symbols		//
//																				//
//##########################################

function clearForm(){
	a = confirm("O fomulário irá ser apagado!\nPretende continuar?");
	if(a){
		document.mail_form.reset();
		$('mail_error').style.display = "none";
		$('mail_ok').style.display = "none";
		$('mail').style.backgroundColor = "#FFF";
		$('name_error').style.display = "none";
		$('name_ok').style.display = "none";
		$('name').style.backgroundColor = "#FFF";
		$('subject_error').style.display = "none";
		$('subject_ok').style.display = "none";
		$('subject').style.backgroundColor = "#FFF";
		$('message_error').style.display = "none";
		$('message_ok').style.display = "none";
		$('message').style.backgroundColor = "#FFF";
	}
}

//##########################################
//																				//
//	This function checks the content of		//
//	a string, and validates e-mail 				//
//	addresses.														//
//																				//
//##########################################

function checkChars(string){
	var legal_chars = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_", "-", ".");
	string_arr = string.split("");
	for(i=0; i<string_arr.length; i++){
		for(x=0; x<legal_chars.length; x++){
			if(string_arr[i] == legal_chars[x]){
				founded = true;
				break;
			} else {
				founded = false;
			}
		}
		if(founded == false){
			return false;
		}
	}
	return true;
}

//##########################################
//																				//
//	This function checks if the fields 		//
//	are typed and change the background 	//
//	color of fields and show/hide the 		//
//	symbols.															//
//																				//
//##########################################

function checkInfo(element, type){
	switch(type){
		case "email":
		error = false;
			if(element.value != ""){
				//try to split email by @
				email_arr = element.value.split("@");
				//check if array have two elements and they are not empty
				if(email_arr.length != 2 || (email_arr[0] == "" || email_arr[1] == "")){
					$('mail_error').style.display = "block";
					$('mail_ok').style.display = "none";
					$('mail').style.backgroundColor = "#FFC6C6";
					error = true;
					break;
				}
				//check if chars before @ are legal
				if(!checkChars(email_arr[0])){
					$('mail_error').style.display = "block";
					$('mail_ok').style.display = "none";
					$('mail').style.backgroundColor = "#FFC6C6";
					error = true;
					break;
				}
				//split domain by .
				domain_arr = email_arr[1].split(".");
				//check if domain prefix haves two or three letters
				if(domain_arr[domain_arr.length-1].length != 2 && domain_arr[domain_arr.length-1].length != 3){
					$('mail_error').style.display = "block";
					$('mail_ok').style.display = "none";
					$('mail').style.backgroundColor = "#FFC6C6";
					error = true;
					break;
				}
				//check if domain elements have ilegal chars
				for(z=0; z<domain_arr.length; z++){
					if(!checkChars(domain_arr[z])){
						$('mail_error').style.display = "block";
						$('mail_ok').style.display = "none";
						$('mail').style.backgroundColor = "#FFC6C6";
						error = true;
						break;
					}
					//check if there are any empty element, this means that the user had typed ..
					if(domain_arr[z] == ""){
						$('mail_error').style.display = "block";
						$('mail_ok').style.display = "none";
						$('mail').style.backgroundColor = "#FFC6C6";
						error = true;
						break;
					}
				}
			} else {
				$('mail_error').style.display = "block";
				$('mail_ok').style.display = "none";
				$('mail').style.backgroundColor = "#FFC6C6";
				break;
			}
			//if no errors and the field isn't empty show OK signal and change the BG of the INPUT
			$('mail_error').style.display = "none";
			$('mail_ok').style.display = "block";
			$('mail').style.backgroundColor = "#B7FFB7";
		break;
		case "name":
			if(element.value != ""){
				$('name_error').style.display = "none";
				$('name_ok').style.display = "block";
				$('name').style.backgroundColor = "#B7FFB7";
			} else {
				$('name_error').style.display = "block";
				$('name_ok').style.display = "none";
				$('name').style.backgroundColor = "#FFC6C6";
			}
		break;
		case "subject":
			if(element.value != ""){
				$('subject_error').style.display = "none";
				$('subject_ok').style.display = "block";
				$('subject').style.backgroundColor = "#B7FFB7";
			} else {
				$('subject_error').style.display = "block";
				$('subject_ok').style.display = "none";
				$('subject').style.backgroundColor = "#FFC6C6";
			}
		break;
		case "message":
			if(element.value != ""){
				$('message_error').style.display = "none";
				$('message_ok').style.display = "block";
				$('message').style.backgroundColor = "#B7FFB7";
			} else {
				$('message_error').style.display = "block";
				$('message_ok').style.display = "none";
				$('message').style.backgroundColor = "#FFC6C6";
			}
		break;
	}
}