//function pondra un banner diferente todas las 05 min
function objetoAjax(){
  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 envioAjax(url,id){
  ajax = new objetoAjax();
  divResultado = document.getElementById(id);
  ajax.open("get", url, true);
  ajax.onreadystatechange=function() {
    if (ajax.readyState==4) {
      divResultado.innerHTML = ajax.responseText
    }
  } 
    ajax.send(null); 
}


//DESPECABLE
function despecable(id){
  var forml = document.getElementById('contactar'); 
	if(id=="comentarios"){
    document.getElementById("modelo").style.visibility='hidden';
    document.getElementById("modelo").style.display='none';
    for(i=0; i<forml.elements.length; i++){
      if(forml.elements[i].className=='s hide regexp' || forml.elements[i].className=='m hide regexp'){
        forml.elements[i].className = forml.elements[i].className.replace(' regexp', '');  
      }
    }   
	}else if(id=="modelo" || id=="presupuesto"){
		  document.getElementById("modelo").style.visibility='visible';
      document.getElementById("modelo").style.display='block';
      if(id=="modelo"){
	      document.getElementById("modelo2").style.visibility='hidden';
	      document.getElementById("modelo2").style.display='none';      	
      } else {
        document.getElementById("modelo2").style.visibility='visible';
        document.getElementById("modelo2").style.display='block';        	
      }
	    for(i=0; i<forml.elements.length; i++){
	      if(forml.elements[i].className=='s hide regexp' || forml.elements[i].className=='s hide' || forml.elements[i].className=='m hide'){
          if(forml.elements[i].className=='s hide regexp'){
            forml.elements[i].className = forml.elements[i].className.replace(' regexp', '');                         	
          }
          if(id=='modelo'){
	          if(forml.elements[i].name!='cantidad'){
	            forml.elements[i].className = forml.elements[i].className + ' regexp';  
	          } else {
              forml.elements[i].className = forml.elements[i].className.replace(' regexp', '');  	          	
	          }          	
          } else {
              forml.elements[i].className = forml.elements[i].className + ' regexp';           	
          }
	      }
	    }  
		}else{
	    if(document.getElementById(id).style.visibility=='visible'){
	      document.getElementById(id).style.visibility='hidden';
	      document.getElementById(id).style.display='none';
	    }else{
	      document.getElementById(id).style.visibility='visible';
	      document.getElementById(id).style.display='block';  
	    }			
		}//fin else modelo==id || presupuesto  
}// fin function despecable


function valid_form(){
	//Asunto
	_x=document.contactar.asunto;
	if(_x.value=="modelo"){
		//familia
	  _x1=document.contactar.familia;
	  if(_x1.value==0){
	  	alert("Debe elegir una familia");
	  	_x1.focus();
	  	return 0;
	  }
	  //serie 
	  _x2=document.contactar.serie;
	  if(_x2.value==0){
      alert("Debe elegir una serie");	
      _x2.focus();
      return 0;
    }
	  //modelo 
	  _x3=document.contactar.modelo;
    if(_x3.value.length==0){
      alert("Debe escribir un modelo");
      _x3.focus();
      return 0;
    }  
	  //cantidad
	  _x4=document.contactar.cantidad;
	  
    if(_x4.value.length==0){
      alert("Debe introducir una cantidad");    	
      _x4.focus();
      return 0;
    }
    if(isNaN(_x4.value * 1)){
      alert("La cantidad no es un numero");      
      _x4.focus();
      return 0;
    }	  
	}//Si _x == modelo
	//Empresa
	_x5=document.contactar.empresa;
  if(_x5.value.length==0){
  	alert("Debe introducir el nombre de empresa"); 
    _x5.focus();
    return 0;
  }	
	//Nombre
  _x6=document.contactar.nombre;
  if(_x6.value.length==0){
  	alert("Debe indicarnos su Nombre ");
    _x6.focus();
    return 0;
  }
	//Apellidos
  _x7=document.contactar.apellidos;
  if(_x7.value.length==0){
    alert("Debe indicarnos sus Apellidos ");  	
    _x7.focus();
    return 0;
  }
	//teléfono
  _x8=document.contactar.telefono;
  var re = new RegExp(/[+]?\d{6,12}/);
  var test = re.exec(_x8.value);
  if(_x8.value.length==0){
    alert("Debe indicarnos su tel&eacute;fono ");   
    _x8.focus();
    return 0;
  }
  if(test==null){
  	alert("El formato de su teléfono es incorrecto");   
    _x8.focus();
    return 0;
  } 
  
	//correo
  _x9=document.contactar.email;
  if(_x9.value.length==0){
  	alert("Debe indicarnos su correo ");
  	_x9.focus();
    return 0;
  }else{
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";   
    var arroba = _x9.value.indexOf("@",0);
    if ((_x9.value.lastIndexOf("@")) != arroba) arroba = -1;   
    var punto = _x9.value.lastIndexOf(".");               
    for (var contador = 0 ; contador < _x9.value.length ; contador++){
      if (cadena.indexOf(_x9.value.substr(contador, 1),0) == -1){
        alert("Debe indicarnos su correo ");
        _x9.focus();
        return 0;
        break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (_x9.value.length)) && (_x9.value.indexOf("..",0) == -1)){
    }else{
    	alert("Debe indicarnos su correo ");
    	_x9.focus();
      return 0;
    }            
  }	
  document.contactar.submit();
}

function selection_foto(option,plantilla){
      url="foto.php?foto=" + option + "&pl=" + plantilla + "";
      //alert(url);
      new Ajax.Request(url, {
        method:'get',
        onSuccess: function(transport) {
          var d = transport.responseText.evalJSON();
          for(var i = 0; i < d.length; i++) {
            document.getElementById(d[i][0]).src = 'img/productos/' + d[i][1] + '';
          }
        }
      });    
}

function selecion_select(option,id,f,s,c,t){
		  url="include/select_buscar.php?f="+f+"&s="+s+"&c="+c+"&t="+t+"";
		  new Ajax.Request(url, {
		    method:'get',
		    onSuccess: function(transport) {
		      var d = transport.responseText.evalJSON();
		      for(var i = 0; i < d.length; i++) {
		        document.getElementById(d[i][0]).innerHTML = d[i][1];
		      }
		    }
		  });    
}

function popup(file,nombre){
  window.open(file,nombre,'width=600,toolbar=0,resizable=0,scrollbars=1');
}

function mas(){
  window.open('resultados.php');
  despecable('mas');
}
function resete(){
      url="buscador.php";
      new Ajax.Request(url, {
        method:'get',
        onSuccess: function(transport) {
            document.getElementById('list').innerHTML = responseTEXT;
          }
      }); 	
}

function Carga(foto){
	var c=0;
	for(var i=0; i<foto.length;i++){
		c++;
		var d=new image();
		d.src=foto[i];
		d.onLoad=function(){
			--c;
			if(c!=0){
				document.getElementById('').visibility=visible;
			}
		}
	}
	
}
  