// JavaScript Document
// Created 7/22/06
// by Ismael L. Ruiz

// Variables
var http = AJAX(); // http object
var email_regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var name_regex = /^[a-zA-Z \.]+$/
var message_regex = /^[a-zA-Z0-9 _\.\-\n\r\?\!\$\,\;\:\"\']+$/ 
var code_regex = /^[0-9A-Za-z]{6}$/
var loading = '<div style="margin:15px; text-align:center;"><img src="images/loader.gif" /></div>';
//var myForm = document.forms.email_form
// /^[0-9]$/


// XMLHttpRequest function to initiate request object
function AJAX(){ 
	var http = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		http = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			http = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			http = false;
		}
	}
	@end @*/
	if (!http && typeof XMLHttpRequest!='undefined') {
		http = new XMLHttpRequest();
	}
	return http;
}

/* Send email */
function sendEmail(){
	// declare variable to hold form object
	var myForm = document.forms.email_form;		
	// Hide message 
	showMessage(false);
	// disable button
	myForm.send.disabled = true;
	// Check form
	if(checkWholeForm(myForm)){
		// hide div
		//document.getElementById("content").style.display = "none";
		
		// Show loading
		feedBack(loading);
		
		// replace new line character
		var message = myForm.message.value;
		message = message.replace(/\n/g, "<br/>");
		// Send message
		http.open('get', 'send_email.php?name=' + myForm.name.value + '&email=' + myForm.email.value + '&message=' + message + '&code=' + myForm.code.value);
		http.onreadystatechange = handleProducts;
		http.send(null);
		
	}else{
		//feedBack('Please fill required fields!');
		myForm.send.disabled = false; // enable button
	}
}

// Check if the form has been filled
function checkWholeForm(theForm){
	var str = "";
	// check name
	if(isEmpty(theForm.name.value)){
		str += '<li>Name is required!</li>';
		theForm.name.className = 'error'; // highlight textbox
	}else if (name_regex.test(theForm.name.value) == false){
		str += '<li>Name contains invalid characters!</li>';
		theForm.name.className = 'error'; // highlight textbox
	}
	// check email
	if(isEmpty(theForm.email.value)){
		str += '<li>E-mail is required!</li>';
		theForm.email.className = 'error'; // highlight textbox
	}else if(email_regex.test(theForm.email.value) == false){
		str += '<li>E-mail is invalid!</li>';
		theForm.email.className = 'error'; // highlight textbox
	}
	// check message
	if(isEmpty(theForm.message.value)){
		str += '<li>Message is required!</li>';
		theForm.message.className = 'error'; // highlight textbox
	}else if (message_regex.test(theForm.message.value) == false) {
		str += '<li>Message contains invalid characters!</li>';
		theForm.message.className = 'error'; // highlight textbox
	}
	// check code
	if(isEmpty(theForm.code.value)){
		str += '<li>Verification Code is required!</li>';
		theForm.code.className = 'error'; // highlight textbox
	}else if(code_regex.test(theForm.code.value) == false){
		str += '<li>Code is not valid!</li>';
		theForm.code.className = 'error'; // highlight textbox
	}
	// check concatinated string
	if (str != "") {
	   feedBack(str); // show error message
	   return false; // form is not valid
	   // theForm.btn_send.disabled = false; // enable button
	}return true; // form is valid
}

// Make sure fields are not empty
function isEmpty(str){
	if(str.length == 0){
		return true; // string is empty
	}return false; // string is not empty
}

// Restore button
function restoreButton(){
	document.getElementById("content").style.display = "block";
	document.forms.email_form.send.disabled = false;
	document.forms.email_form.code.focus()
	document.forms.email_form.code.select()
}

// show message
function showMessage(show){
	if(show){document.getElementById('msg').style.display="block";}
	else{document.getElementById('msg').style.display="none";}
}

// Send message to the user
function feedBack(str){
	// Show message div
	showMessage(true);
	// create message
	document.getElementById('msg').innerHTML = str;
}

// Remove invalid characters
function removeInvalid(val, ex) {
	var strPass = val.value;
	var strLength = strPass.length;
	var lchar = val.value.charAt((strLength) - 1);
	if(lchar.search(ex) == -1) {
		var tst = val.value.substring(0, (strLength) - 1);
		val.value = tst;
	}
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		//document.getElementById('content').innerHTML = response;
		feedBack(response);
	}
}