// UTIL FUNCTIONS

//check if obj is empty
function empty(obj) {
	
	if (obj == "undefined" || obj == null || obj == "")
		return true;
		
	return false;	
}

//function to change page with ajax request
function gotopage(page, elemId, effect) {
	
	if (!empty(elemId) && !empty(effect) && effect == true) {
		Effect.tagifyText(elemId);
		new Effect.multiple($(elemId).childNodes, Effect.Highlight, {speed:0.01, duration: 0.15, afterFinishInternal:function(){}});
	}

	new Ajax.Updater('content', page, { method:'post', evalScripts: true,
	  onSuccess: function(){
		 //sucess goes here
	  }
		  ,
	  onFailure: function(){
		  //failure goes here
		  alert('Erro!!');
		}
	  });
	

}

//function to open a modal window
function openModalAlert(page, wSize) {

	Dialog.alert({url: page, options: {method: 'post'}}, 
				 {width:wSize, className: "alphacube", okLabel: "Fechar", showEffectOptions: {duration: 0.0},
				 hideEffectOptions: {duration: 0.0},
				 ok:function(win) {return true;}
				 });
	
}


function openSignGuestbookDialog(page, wSize, hSize) {

	Dialog.confirm({url: page, options: {method: 'post'}}, 
				 {width:wSize, maxHeight:hSize, className: "alphacube", okLabel: "OK", cancelLabel: "Cancelar", 		showEffectOptions: {duration: 0.0},
				 hideEffectOptions: {duration: 0.0},
				 ok:function(win) {
					
					var queryStr = "";										
														
					if (!validateFormValues('guestbookform')) {						
						$('error_msg').innerHTML='Os campos devem ser todos preenchidos!'; 
						$('error_msg').show(); 
						Windows.focusedWindow.updateHeight(); 
						new Effect.Shake(Windows.focusedWindow.getId()); 
						return false;							
					}
																
					if (document.getElementById("message").value.length > 1000) {						
						$('error_msg').innerHTML='A mensagem &eacute; demasiado longa! S&oacute; pode conter no m&aacute;ximo 1000 caracteres.'; 
						$('error_msg').show(); 
						Windows.focusedWindow.updateHeight(); 
						new Effect.Shake(Windows.focusedWindow.getId()); 
						return false;							
					}
						
					 queryStr = 'fielddata=' + getFormValues('guestbookform');								
					 gotopage_post('dbguestbookchanges.php', queryStr, 'content');					 					 
					 
					 return true;
					 },
					 
				 cancel:function(win) {return true;}
				 });	
}


function gotopage_post(page, querystr, elemId) {
		
	new Ajax.Updater(elemId, page, { method:'post', parameters: querystr, evalScripts: true,
	  onSuccess: function(){
		 //sucess goes here
	  }
		  ,
	  onFailure: function(){
		  //failure goes here
		  alert('Erro!!');
		}
	  });
}


function validateFormValues(formname) {	

	ff = document.getElementById(formname);

	for ( i = 0; i < ff.elements.length; i++ ) 
		if (ff.elements[i].value == "" && ff.elements[i].type != "hidden")		
			return false;	
	 
	return true; 
}

function getFormValues(formname) {
	var str = "";

	ff = document.getElementById(formname);

	for ( i = 0; i < ff.elements.length; i++ ) {
				
		str = str + ff.elements[i].name +"|||||"+ encodeURIComponent(ff.elements[i].value);											
		
		if (i < ff.elements.length - 1)
			str = str + ')()(';
	 }	
	 
	return str;
}