Event.observe(window, 'load', function() {
 	if($('pubman_send_button')!=null){
 		$('pubman_send_button').observe('click', enviaFormulario);
	}
});
//	alert($('pubman_send_button'));
//});


function showError(msg){
	Effect.Appear('pubman_ajax_info', { duration: 0.5 });
	$('pubman_ajax_info').addClassName("error");
	var console=$('pubman_ajax_info').down();
	console.insert(msg+"<br/>");	
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function validateEmail( strValue) {
	var objRegExp  =new RegExp("^.+\\\@(\\\[?)[a-zA-Z0-9\\\-\\\.]+\\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\\]?)$");
	
	  //check for valid email
	  return objRegExp.test(trim(strValue));
}

function enviaFormulario(){
	
	var id=$F('pubman_idpub');
	var nombre=$F('pubman_nombre');
	var ciudad=$F('pubman_ciudad');
	var email=$F('pubman_email');
	var comments=$F('pubman_comments');
	var valcaptcha=$F('txtCaptcha');
	var action=$F('pubman_action');
	var console=$('pubman_ajax_info').down();
	console.update("");
		
	var errores=0;
	if(nombre==""){
		showError("El nom &eacute;s obligatori.");
		errores++;
	}
	if(ciudad==""){
		showError("La ciutat &eacute;s obligatoria.");
		errores++;
	}
	if(email==""){
		showError("L'email &eacute;s obligatori.");
		errores++;
	}

	if(!validateEmail(email)){
		showError("El format de l'email no &eacute;s correcte.");
		errores++;
	}

	if(valcaptcha==""){
		showError("El codi de seguretat &eacute;s obligatori.");
		errores++;
	}

	if(errores==0){
		var param= 'pubman_action='+action+'&txtCaptcha='+valcaptcha+'&idpub='+id+'&name=' + nombre + '&city=' + ciudad + '&email=' + email+'&comments='+comments;
		var url = $('pubman_send_info').action;
	  	

		var ajaxRequest = new Ajax.Request(
				        url,
				        {
				              method: 'post',
				              parameters: param,
				              asynchronous: true,
				              onSuccess: showResponse,
						on500: handleErrors


				        });
	}
}

function showResponse(transport){
	Effect.Appear('pubman_ajax_info', { duration: 0.5 });

	var console=$('pubman_ajax_info').down();
	var docscontainer=$('pubman_docs_container');

	var response = transport.responseText || "error";
	
	if(response=="error"){
		$('pubman_ajax_info').addClassName("error");
		console.insert("No s'ha pogut enviar la informaci&oacute;. Nom&eacute;s pots descarregar un document un cop al dia.<br/>");
	}else if(response=="errorcaptcha"){
		$('pubman_ajax_info').addClassName("error");
		console.insert("El codi de seguretat no &eacute;s correcte.<br/>");
	}else{
		$('pubman_ajax_info').removeClassName("error");
		docscontainer.update(response);
		console.insert("Les dades s'han rebut correctament. Ja pots descarregar-te la publicaci&oacute;<br/>Moltes gr&agrave;cies pel teu inter&eacute;s!<br/>");
		//$('pubman_download_button').enable();
		
		/*$('pubman_download_button').observe('click',function(){
			document.location=response;
			this.disable();
		});*/
		
		$('pubman_send_button').disable();

		$('pubman_nombre').disable();
		$('pubman_ciudad').disable();
		$('pubman_email').disable();
		$('pubman_comments').disable();
	}
}

function handleErrors(transport){
      var console=$('pubman_ajax_info').down();
	  var response = transport.responseText || "no response text";
      console.insert("Error del servidor:" + response);
}
