﻿//CREACIÓN OBJETO XMLHTTPREQ
function nuevoAjax(){
	var xmlHttpReq = false; 
	var self=this;
	if (window.XMLHttpRequest) {
		  self.xmlHttpReq = new XMLHttpRequest();
		  } else if (window.ActiveXObject) {
			   self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return self.xmlHttpReq;
}
//AJAX STANDARD
function ajaxQuery(file,vars,div,ready) { 
	ajax=nuevoAjax();
	if(self.xmlHttpReq) {
		 var div = document.getElementById(div);
		 var loader = document.getElementById("loader");
		 ajax.open("POST",file); 
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		 ajax.onreadystatechange = function()  { 
			  if (ajax.readyState<4) { 
				  loader.style.visibility="visible"; 			
			  } 
			 if (ajax.readyState == 4) { 
					eval(ready+"(div)");				
			  }
		 }
ajax.send(vars); 
  }
}

function ajaxGeneral(div){
	var loader = document.getElementById("loader");
	loader.style.visibility="hidden"; 					
	div.innerHTML = self.xmlHttpReq.responseText; 
}

function ajaxFicha(div){
	document.getElementById("loader").style.visibility="hidden"; 
	document.getElementById("fondo").style.visibility="visible"; 
	div.style.visibility="visible"; 					
	div.innerHTML = self.xmlHttpReq.responseText; 
}

function ajaxFormulari(div){
	document.getElementById("loader").style.visibility="hidden"; 
	document.getElementById("fondo").style.visibility="visible"; 
	div.style.visibility="visible"; 					
	div.innerHTML = self.xmlHttpReq.responseText; 
	borrarInput();
	//formu
    var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
        clearForm: true        // clear all form fields after successful submit 
    }; 
    $('#formPreguntes').ajaxForm(options); 
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//función menu salto
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function cerrar(div){
	document.getElementById("fondo").style.visibility="hidden";  	
document.getElementById(div).style.visibility="hidden";  	

}

//VALIDAR FORMULARIOS
function validar(formu){
//nom
	if(document.forms[formu]['nom']){
	  if(document.forms[formu]['nom'].value == ''){
		alert('Inserta el teu nom');
		document.forms[formu]['nom'].focus();
		return false;
	  }
  }
//mail
  if(document.forms[formu]['email']){
	  if(document.forms[formu]['email'].value == 0){
		alert('Inserta el teu email de contacte');
		document.forms[formu]['email'].focus();
		return false;
	  }
  }
//missatge
  if(document.forms[formu]['missatge']){
	  if(document.forms[formu]['missatge'].value == 0){
		alert('Inserta el teu missatge');
		document.forms[formu]['missatge'].focus();
		return false;
	  }
  }

  eval("document."+formu+".submit();");
}
// JavaScript Document
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LOADER
function loading(div){	
	$(div).append('<div id="loader2"></div><img id="loaderImg" src="img/loader.gif" alt="" />');
	$("#loader2").css('opacity', 0);
	$("#loader2").fadeTo("fast",0.2);
}
function fadeloading(){
	$("#loaderImg").remove();
	$("#loader2").fadeTo("fast",0);
	$("#loader2").remove();	
}

function error(mensaje){
	$("#wrapper").append('<div id="loader2"></div><div id="error"><a href="#" onclick="cerrar2(\'#error\');return false;"><img src="img/cerrar.png" alt="Cerrar" /></a><p>* '+mensaje+'</p></div>');
	$("#loader2").css('opacity', 0);
	$("#loader2").fadeTo("fast",0.2);	
}
function cerrar2(div){
	$(div).fadeOut(400,function(){ 
	$("#loader2").fadeOut(400,function(){$("#loader2").remove(); });	
	$(div).remove(); });	
}

function showRequest(formData, jqForm, options) { 
   loading('#wrapper'); 
    return true; 
} 

function showResponse(responseText, statusText, xhr, $form)  { 
   fadeloading();
   error(responseText);
} 
//Quita texto deinput al hacer clic
function borrarInput(){
	 $('.filter').click(function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	$('.filter').blur(function() {
		if (this.value == '') {
			this.value = this.defaultValue;
		}
	});	 
}

