// JavaScript Document


function validar(formulario) {
  if (formulario.nombre.value=="") {
    alert("Ooops! All fields are required...");
    formulario.nombre.focus();
    return (false);
  }
  if (formulario.apellidos.value=="") {
    alert("Ooops! All fields are required...");
    formulario.apellidos.focus();
    return (false);
  }if (formulario.correo.value=="") {
    alert("Ooops! All fields are required...");
    formulario.correo.focus();
    return (false);
  }
	if (formulario.telefono.value=="") {
    alert("Ooops! All fields are required...");
    formulario.telefono.focus();
    return (false);
  }
  	if (formulario.direccion.value=="") {
    alert("Ooops! All fields are required...");
    formulario.direccion.focus();
    return (false);
  }
  if (formulario.cp.value=="") {
    alert("Ooops! All fields are required...");
    formulario.cp.focus();
    return (false);
  }
  if (formulario.ciudad.value=="") {
    alert("Ooops! All fields are required...");
    formulario.ciudad.focus();
    return (false);
  }
  if (formulario.provincia.value=="") {
    alert("Ooops! All fields are required...");
    formulario.provincia.focus();
    return (false);
  }
  if (formulario.pais.value=="") {
    alert("Ooops! All fields are required...");
    formulario.pais.focus();
    return (false);
  }
  
  
  // Comprobar correo
  
  	var texto = formulario.correo.value;
	var mailres = true;            
	var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
	
	var arroba = texto.indexOf("@",0);
	if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
	
	var punto = texto.lastIndexOf(".");
				
	 for (var contador = 0 ; contador < texto.length ; contador++){
		if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
			mailres = false;
			break;
	 }
	}
	
	if (texto == "") mailres = false;
	if (texto == "tucorreo@ejemplo.com") mailres = false;
	
	if ((arroba > 1) && (arroba + 1 < punto) && (punto + 2 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
	 mailres = true;
	else
	{
	 mailres = false;
	}
	
	if (mailres == false) {
		alert("Oooops!! Invalid mail address!");
		formulario.correo.focus();
		return (false);
	}
  
  
}

function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}


function cargarFormuFP(){
	var contenedor;
	contenedor = document.getElementById('formufp');
	ajax=nuevoAjax();
	ajax.open("GET", "formufp.html",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			contenedor.innerHTML = ajax.responseText;
		}
	}
	 ajax.send(null)
}


function so_clearInnerHTML(obj) {
	while(obj.firstChild) obj.removeChild(obj.firstChild); // so long as obj has children, remove them
}


function buscar(){
	var term, contenedor;
	contenedor = document.getElementById('contenido_cat');
	term = document.getElementById('busqueda').value;
	ajax=nuevoAjax();
	ajax.open("GET", "search.php?busqueda="+term,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			/*capa = document.createElement("div"); // create a DIV element, using the variable eDIV as a reference to it
			capa.setAttribute("id","myDiv"); //use the setAttribute method to assign it an id
			capa.appendChild(document.createTextNode(ajax.responseText)); // add the text to the div with createTextNode			
			
			so_clearInnerHTML(document.getElementById("contenido_cat")); // call your function to remove all the children from your element

			document.getElementById("contenido_cat").appendChild(capa); // append your newly created DIV element to an already existing element.*/
			contenedor.innerHTML = ajax.responseText;
	 	}
		else {
			/*eDIV = document.createElement("div"); // create a DIV element, using the variable eDIV as a reference to it
			eDIV.setAttribute("id","myDiv"); //use the setAttribute method to assign it an id
			eDIV.appendChild(document.createTextNode("<span class='searching'>Searching...</span>")); // add the text to the div with createTextNode			
			
			so_clearInnerHTML(document.getElementById("contenido_cat")); // call your function to remove all the children from your element

			document.getElementById("contenido_cat").appendChild(eDIV); // append your newly created DIV element to an already existing element.*/
			
			contenedor.innerHTML = "<span class='searching'>Searching...</span>";
		}
	}
	ajax.send(null)
}

function abrirgastos(){ 
		var sTop=(screen.availHeight - 475)/2;  
		var  sLeft=(screen.availWidth - 775)/2;
		window.open('http://www.bang-records.net/site/gastos.php','','width=500,height=275,top=' + sTop + ',left=' + sLeft + ',scrollbars=yes,resizable=no,status=yes,location=yes' );
}