function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}

function inicialitza_formulari() {
	if(!document.getElementById('formG')) {
		return;
	}
	var boto_enviar = document.getElementById('bt_enviar');
	boto_enviar.onclick = function() {
		var validat = validar();
			if(validat==true) {
				envia_formulari(document.getElementById('formG'));
			}
			return false;
	}
}

function validar() {
	var inputs = document.getElementsByTagName('input');
	var ok = true;
	for (var i=0; i<inputs.length-1; i++) {
		if(inputs[i].value=='') {
			ok = false;
			inputs[i].className = 'form_campo no_complet';
		} else {
			inputs[i].className = 'form_campo';
		}
	}
	var comentaris = document.getElementById('f_comentarios');
	if(comentaris.value=='') {
		ok = false;
		comentaris.className = 'form_campo no_complet';
	} else {
		comentaris.className = 'form_campo';
	}
	if(!ok) {
		alert("Debe rellenar todos los campos");
		return false;
	}
	var regex=new RegExp("^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int|biz|info|name|pro)$");
	var f_email = document.getElementById('f_email');
	if(regex.test(f_email.value)==false) {
		alert("La dirección de email no es válida");
		return false;
	}
	return true;
}

function envia_formulari(form) {
	var request = getHTTPObject();
	if (request) {
		mostra_progres(document.getElementById('contenidos'));
    	request.onreadystatechange = function() {
			if (request.readyState == 4) {
    			if (request.status == 200 || request.status == 304) {
				document.getElementById('contenidos').removeChild(document.getElementById('capa_sending'));
      			informa_ok(request);
   			 }
  			}
    	};
		var query_str = conforma_query_string(form);
    	request.open("POST", 'scripts/procesar_mail.php', true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	request.send(query_str);
  	}
}

function informa_ok(req) {
	var missatge = req.responseText;
	var contenidor_missatge = document.getElementById('missatge_ok_error');
	contenidor_missatge.innerHTML = missatge;
}

function conforma_query_string(form) {
	var inputs = document.getElementsByTagName('input');
	var query = '';
	for(var i=0; i<inputs.length-1; i++) {
		var nom_vble = inputs[i].getAttribute('name');
		query = query + nom_vble + '=' + inputs[i].value + '&';
	}
	//ara fem el mateix amb el textarea
	var txt_area = document.getElementById('f_comentarios');
	query += 'f_comentarios=' + txt_area.value;
	
	
	/*var nom = document.getElementById('nom').value;
	var empresa = (document.getElementById('empresa').value=='')?'-':document.getElementById('empresa').value;
	var ciudad = (document.getElementById('ciudad').value=='')?'-':document.getElementById('ciudad').value;
	var telefono = (document.getElementById('telefono').value=='')?'-':document.getElementById('telefono').value;
	var email = document.getElementById('email').value;
	var asunto = document.getElementById('asunto').value;
	var imatge_proteccio = document.getElementById('imatge_proteccio').value;
	var missatge = document.getElementById('missatge').value;
	var query = 'nom='+nom+'&empresa='+empresa+'&ciudad='+ciudad+'&telefono='+telefono+'&email='+email+'&asunto='+asunto+'&imatge_proteccio='+imatge_proteccio+'&missatge='+missatge;*/
	return query;
}

function mostra_progres(contenidor) {
	var div = document.createElement('div');
	div.setAttribute('id', 'capa_sending');
	var image = document.createElement('img');
	image.setAttribute('src', 'img/img_sending.gif');
	image.setAttribute('alt', 'Enviando...');
	image.setAttribute('id', 'img_sending');
	div.appendChild(image);
	contenidor.appendChild(div);
}