function createObject() {
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_type = new XMLHttpRequest();
	}
	return request_type;
}

var http = createObject();

var nocache = 0;
function sendmail() {
	// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('response').innerHTML = "Enviando..."
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var name = encodeURI(document.getElementById('name').value); 
	var email = encodeURI(document.getElementById('email').value);
	var telefono = encodeURI(document.getElementById('telefono').value);
	var empresa = encodeURI(document.getElementById('empresa').value);
	var comment = encodeURI(document.getElementById('comment').value);
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('post', 'http://www.news.korazzaejecutivos.com/korazza/contact_form/send.php?name='+name+'&email='+email+'&telefono='+telefono+'&empresa='+empresa+'&comment='+comment+'&nocache = '+nocache);
	http.onreadystatechange = emailReply;
	http.send(null);
}

function emailReply() {
	if(http.readyState == 4){ 
		var response = http.responseText;
		//if(response == 0){
			// if login fails
			//document.getElementById('response').innerHTML = '<span class="alert">Los datos introducidos son incorrectos. Intente nuevamente.</span>';
			// else if login is ok show a message: "Welcome + the user name".
		//} else {
			document.getElementById('response').innerHTML = '<strong>Muchas gracias, su mensaje ha sido recibido.</strong><br />';
			document.getElementById('form_email').style.display = 'none';
		//}
	}
}

$(function() {

	// load the modal window
	$('a.modal').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#name').val('Su nombre');
		$('#email').val('Su direccion de e-mail');
		$('#telefono').val('Su telefono');
		$('#empresa').val('Su empresa');
		$('#comment').val('Introduzca su comentario o consulta...');

		//show the mask and contact divs
		//$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});

	$('#contactForm input').focus(function() {
		$(this).val(' ');
	});
	
	$('#contactForm textarea').focus(function() {
        $(this).val('');
    });

	// when the Submit button is clicked...
	/*$('input#submit').click(function() {
	$('.error').hide().remove();
		//Inputed Strings
		var username = $('#name').val(),
			email = $('#email').val(),
			telefono = $('#telefono').val(),
			empresa = $('#empresa').val(),
			comment = $('#comment').val();

		//Error Count
		var error_count;

		//Regex Strings
		var username_regex = /^[a-z0-9_-]{3,16}$/,
			email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
		http.open('get', 'contact_form/send.php?name='+username+'&email='+email+'&telefono='+telefono+'&empresa='+empresa +'&comment='+comment);
		http.send(null);
		$('form#contactForm').fadeOut('slow');
				
			//Test Username
			if(!username_regex.test(name)) {
				$('#contact_header').after('<p class=error>Escriba su nombre correctamente</p>');
				error_count += 1;
			}
			
			//Test Email
			if(!email_regex.test(email)) {
				$('#contact_header').after('<p class=error>Escriba su e-mail correctamente</p>');
				error_count += 1;
			}
			*/
			//Blank Comment?
			/*if(comment == '') {
				$('#contact_header').after('<p class=error>Escriba su consulta</p>');
				error_count += 1;
			}
			
			//No Errors?
			if(error_count === 0) {
				$.ajax({
					type: "post",
					url: "send.php",
					data: "name=" + name + "&email=" + email + "&telefono=" + telefono + "&empresa=" + empresa + "&comment=" + comment,
					error: function() {
						$('.error').hide();
						$('#sendError').slideDown('slow');
					},
					success: function () {
						$('.error').hide();
						$('.success').slideDown('slow');
						$('form#contactForm').fadeOut('slow');
					}				
				});	
				
			}
			
			else {
                $('.error').show();
            }
			
		return false;
	});*/
	
});
