/* -------------------------------------------------------------
THE AJAX GUESTBOOK
Written by Sean Fannan

If you would like to use this guestbook, please
leave these comments here.  If you need to contact me
for any reason, my email address is sfannan@gmail.com.  Enjoy
---------------------------------------------------------------*/

/* CONSTANTS */
var phpFileName = 'http://www.lustau.es/gst/functions_guestbook.php';

// Key function to send Ajax packet to php file.
function sendPacket(fileName, strQuery, functionName) {
		var myConn = new XHConn();
		if (!myConn) 
			alert("XMLHTTP not available. Try a newer/better browser.");
		myConn.connect(fileName, "POST", strQuery, functionName);
}
// toggles the message form from visible to hidden
function showPost(linkElement, swap) {
	var formElement = document.getElementById('postMessage');
	if (swap) {
	if(location.href=="http://www.lustau.es/index_08.php?pageid=70" || location.href=="http://www.lustau.es/index_08.php?pageid=70#" || location.href=="http://www.lustau.es/es/lustau_70.html"){
		linkElement.innerHTML = 'Cerrar';
		}
		if(location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en#" || location.href=="http://www.lustau.es/en/lustau_70.html"){
		linkElement.innerHTML = 'Close';
		}
		linkElement.onclick = function () { showPost(this, 0);$('.description').jScrollPane(); };
		formElement.style.display = 'block';
	}
	else {
	if(location.href=="http://www.lustau.es/index_08.php?pageid=70" || location.href=="http://www.lustau.es/index_08.php?pageid=70#" || location.href=="http://www.lustau.es/es/lustau_70.html"){
		linkElement.innerHTML = 'Escribir';
		}
		if(location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en#" || location.href=="http://www.lustau.es/en/lustau_70.html"){
		linkElement.innerHTML = 'Post Message';
		}
		linkElement.onclick = function () { showPost(this, 1);$('.description').jScrollPane(); };
		formElement.style.display = 'none';
	}
	
	
}
/* Verifies the form has no missing fields */
function verify_new() {
var message = document.getElementById('message');
var guest = document.getElementById('swad');
var email = document.getElementById('email');
var agreed = document.getElementById('cagree');



if (message.value == '' || guest.value == '' || email.value == ''){
		if(location.href=="http://www.lustau.es/index_08.php?pageid=70" || location.href=="http://www.lustau.es/index_08.php?pageid=70#" || location.href=="http://www.lustau.es/es/lustau_70.html" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang="){
	alert("Debe rellenar todos los campos");
	}
	if(location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en#" || location.href=="http://www.lustau.es/en/lustau_70.html"){
	alert("There are empty fields");
	}
return false;
}

if(email.value != ''){
if(email.value.indexOf("@")<3){
if(location.href=="http://www.lustau.es/index_08.php?pageid=70" || location.href=="http://www.lustau.es/index_08.php?pageid=70#" || location.href=="http://www.lustau.es/es/lustau_70.html" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang="){
alert("la direccion de email parece incorrecta"
+"!");
}
if(location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en#" || location.href=="http://www.lustau.es/en/lustau_70.html"){
alert("Wrong email format"
+"!");
}
return false;
}
if (email.value.charAt(email.value.length-4)!="." && email.value.charAt(email.value.length-3)!="."){ 

if(location.href=="http://www.lustau.es/index_08.php?pageid=70" || location.href=="http://www.lustau.es/index_08.php?pageid=70#" || location.href=="http://www.lustau.es/es/lustau_70.html" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang="){
alert("la direccion de email parece incorrecta"
+"!!!");
}
if(location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en#" || location.href=="http://www.lustau.es/en/lustau_70.html"){
alert("Wrong email format"
+"!!!");
}


return false;
}


//accept terms and conditions
if (agreed.checked == false){
		if(location.href=="http://www.lustau.es/index_08.php?pageid=70" || location.href=="http://www.lustau.es/index_08.php?pageid=70#" || location.href=="http://www.lustau.es/es/lustau_70.html" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang="){
	alert("Debe de aceptar las condiciones generales");
	}
	if(location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en#" || location.href=="http://www.lustau.es/en/lustau_70.html"){
	alert("You need to accept the terms and conditions");
	}
return false;
}



else
	prepare_new(message, guest, email);
	//alert(message.value);
	//alert(email.value);
	//alert(guest.value);

}


}
// Clears the form
function clear_new() {
var message = document.getElementById('message');
var guest = document.getElementById('swad');
var email = document.getElementById('email');
message.value = '';
guest.value = '';
email.value = '';
}

/* PREPARE FUNCTIONS */
// sends the request for all the entries on a given page
function prepare_entries() {
	document.getElementById('currentEntries').innerHTML = "loading . . .";
	var page = document.getElementById('page');
	strQuery = 'type=1&page='+page.value;
	var functionName = function(oXML) { process_entries(oXML); };
	sendPacket(phpFileName, strQuery, functionName);
}
// sends a request for inserting an entry into the database
function prepare_new(message,guest,email) {
	strQuery = 'type=2&message='+message.value+"&swad="+guest.value+"&email="+email.value;
	var functionName = function(oXML) { process_new(oXML); };
	sendPacket(phpFileName, strQuery, functionName);
	
	showPost(document.getElementById('postLink'), 0);
clear_new();
}
/* PROCESS FUNCTIONS */
// processes the incoming entries from a given page
function process_entries(oXML) {
	var response = oXML.responseText.split("$$");
	var entries = new Array();
	var count = response[0];
	var pageHTML = response[1];
	var strPageInfo = pageHTML;
	if (count > 0)
	if(location.href=="http://www.lustau.es/index_08.php?pageid=70" || location.href=="http://www.lustau.es/index_08.php?pageid=70#"){
	strPageInfo += ' | ' + count;
	}
	if(location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en" || location.href=="http://www.lustau.es/index_08.php?pageid=70&clang=en#"){
	strPageInfo += ' | ' + count;
	}
	document.getElementById('pageInfo').innerHTML = strPageInfo;
	var htmlText = '<br>';//'<br>Total messages: '+count+'<br><br>';
	var currentEntries = document.getElementById('currentEntries');
	
	htmlText += '<div class=\"guestbook_entry_message\">';
	for (i = 2; i < response.length-2; i++) {
		entries = response[i].split("$$");
		
		
		if (entries[0] != '') {
			
			htmlText += entries[0];
			
		}
		htmlText += '<br>';
	}
	htmlText +='</div>';
	currentEntries.innerHTML = htmlText;
	
	
	$(function(){		
$('.description').jScrollPane();
});


}
// processes a new entry, and eventually calls prepare_entries()
function process_new(oXML) {
	var response = oXML.responseText;
	if (response=='1')
		prepare_entries();
}
// retrieve entries for a new page
function process_page(page) {
	document.getElementById('page').value = page;
	prepare_entries();
}