// JavaScript Document

/* PRECARGA IMÁGENES BOTONERA */

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}


/* GALERÍA FOTOS JQUERY */

$(document).ready(function() {
	// Crea la variable con toda la información de todas las fotos
	var fotos = $('div','#fotos');
	
	//Al hacer click primero oculta todo y luego muestra la información del trabajo seleccionado
	$('a#link_foto1').click(function() {
	  fotos.hide();
	  $('#foto1').fadeIn(350);
  	});
  	$('a#link_foto2').click(function() {
	  fotos.hide();
	  $('#foto2').fadeIn(350);
  	});
  	$('a#link_foto3').click(function() {
	  fotos.hide();
	  $('#foto3').fadeIn(350);
	});
	$('a#link_foto4').click(function() {
	  fotos.hide();
	  $('#foto4').fadeIn(350);
	});
});


/* FORMULARIO PRESUPUESTO */

function elegir(x) {
  
	ItDepend=document.getElementById('idMod');
  
	if(!ItDepend) {
		return;
	}                 
	
	var mitems=new Array();
	mitems['']=['Elija una gama'];
  	mitems['Panel_Sandwich']=['Elija un modelo','H300','H400','H600','H700','H800'];
  	mitems['Tradicional']=['Elija un modelo','B300s/a','B12','B14','B400','B20','B600','B700','B800','Vest.14'];
  	mitems['Alta']=['Elija un modelo','P600'];
  	mitems['Contenedores']=['Elija un modelo','A243','A300','A600','A1200'];
	mitems['Sanitarios']=['Elija un modelo','B300','B400','B600','B300s/a','B400s/a','H200','H400','H600','CAB_WC','B12 SAN','B14 SAN','B15 SAN'];
  	mitems['Acristaladas']=['A medida'];
  	mitems['Control_Blindadas']=['Elija un modelo','Cabina de control','Cabina blindada'];
  	mitems['Grandes_Superficies']=['Elija un modelo','-'];
	
	ItDepend.options.length=0;
  	ItActual=mitems[x.options[x.selectedIndex].value];
  	if(!ItActual) {
		return;
	}
  	
	ItDepend.options.length=ItActual.length;
  	
	for(var i=0;i<ItActual.length;i++) {
    	ItDepend.options[i].text=ItActual[i];
    	ItDepend.options[i].value=ItActual[i];
  	}
}

function tipo_contrato(elemento) {

  if(elemento.value=="compra") {
     document.getElementById("idPer").disabled=true;
   } else {
     document.getElementById("idPer").disabled=false;
	 document.getElementById("idPer").focus();
   }

}

function seleccion_transporte(elemento) {

  if(elemento.value=="no") {
     document.getElementById("idDir").disabled=true;
   } else {
     document.getElementById("idDir").disabled=false;
	 document.getElementById("idDir").focus();
   }

}


/* CAMPOS OBLIGATORIOS */

function descripcionCampoNom()
{
var nom=document.getElementById("idNom");

if(nom.value=='Dato necesario')
	{
		nom.value='';
		return (0);
	}
}

function descripcionCampoApe()
{
var ape=document.getElementById("idApe");

if(ape.value=='Dato necesario')
	{
		ape.value='';
		return (0);
	}
}

function descripcionCampoEma()
{
var ema=document.getElementById("idEma");

if(ema.value=='Dato necesario')
	{
		ema.value='';
		return (0);
	}
}


/* VALIDACIÓN FORMULARIO CONTACTO */

function validarForm()
{
	
var nom=document.getElementById("idNom");
var ape=document.getElementById("idApe");
var ema=document.getElementById("idEma");
var com=document.getElementById("idCom");
var avi=document.getElementById("idAvi");

if(nom.value==null || nom.value.length == 0 || /^\s+$/.test(nom.value) || nom.value=='Dato necesario')
	{
		avi.value="Indica tu nombre";
		nom.focus();
		return false;
	}

else if(ape.value==null || ape.value.length == 0 || /^\s+$/.test(ape.value) || ape.value=='Dato necesario')
	{
		avi.value="Indica tus apellidos";
		ape.focus();
		return false;
	}

//Valida que el campo no esté vacío y contenga una @ y un "punto"
else if(ema.value==null || ema.value.length == 0 || !ema.value.match("@" + "."))
	{
		avi.value="Indica tu email";
		ema.focus();
		return false;
	}
	
else if(com.value==null || com.value.length == 0 || /^\s+$/.test(com.value))
	{
		avi.value="Escribe tu comentario";
		com.focus();
		return false;
	}
else
	{
		avi.value="Enviado. Muchas gracias.";
		var form=document.getElementById("idForm");
		form.submit();
	}

}


/* VALIDACIÓN FORMULARIO PRESUPUESTO */

function validarFormPre()
{
	
var gam=document.getElementById("idGam");	
var mod=document.getElementById("idMod");
var con=document.getElementsByName("contrato");
var per=document.getElementById("idPer");
var tra=document.getElementsByName("transporte");
var dir=document.getElementById("idDir");

var nom=document.getElementById("idNom");
var ape=document.getElementById("idApe");
var emp=document.getElementById("idEmp");
var ema=document.getElementById("idEma");
var tel=document.getElementById("idTel");
var com=document.getElementById("idCom");
var avi=document.getElementById("idAvi");


if(gam.value==null || gam.value==0)
	{
		avi.value="Selecciona una gama y un modelo";
		gam.focus();
		return false;
	}
else if(mod.value==null || mod.value=='Elija un modelo')
	{
		avi.value="Selecciona un modelo";
		mod.focus();
		return false;
	}
//El valor '1' de la variable 'con' corresponde a 'alquiler' ('0' es 'compra'). Se comprueba que si este radiobutton está seleccionado ('checked==true') no esté vacío el campo de 'Periodo'
else if(con[1].checked==true && (per.value==null || per.value.length==0))
	{
		avi.value="Indica el periodo de alquiler";
		per.focus();
		return false;
	}
//El valor '1' de la variable 'tra' corresponde a 'sí' ('0' es 'no'). Se comprueba que si este radiobutton está seleccionado ('checked==true') no esté vacío el campo de 'Dirección'
else if(tra[1].checked==true && (dir.value==null || dir.value.length==0))
	{
		avi.value="Indica el lugar de entrega";
		dir.focus();
		return false;
	}
else
	{	
		if(nom.value==null || nom.value.length == 0 || /^\s+$/.test(nom.value) || nom.value=='Dato necesario')
			{
				avi.value="Indica tu nombre";
				nom.focus();
				return false;
			}
		else if(ape.value==null || ape.value.length == 0 || /^\s+$/.test(ape.value) || ape.value=='Dato necesario')
			{
				avi.value="Indica tus apellidos";
				ape.focus();
				return false;
			}
		//Valida que el campo no esté vacío y contenga una @ y un "punto"
		else if(ema.value==null || ema.value.length == 0 || !ema.value.match("@" + "."))
			{
				avi.value="Indica tu email";
				ema.focus();
				return false;
			}
		else
			{
				avi.value="Enviado. Muchas gracias.";
				var formpre=document.getElementById("idFormPre");
				formpre.submit();
			}
	}

}
