// JavaScript Document
//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = 'js/box_false.jpg';
var imgTrue = 'js/box_true.jpg';

//this function runs when the page is loaded, put all your other onload stuff in here too.
function init() {
//	desactiva();
	replaceChecks();
}

function replaceChecks() {
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') {
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(inputs[i].checked) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
			}

			//set image ID and onclick action
			img.id = 'checkImage'+i;
			//set image 
			img.onclick = new Function('checkChange('+i+')');
			//place image in front of the checkbox
			inputs[i].parentNode.insertBefore(img, inputs[i]);
			
			//hide the checkbox
			inputs[i].style.display='none';
		}
	}
}

//change the checkbox status and the replacement image
function checkChange(i) {

	if(inputs[i].checked) {
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
        
//desactiva el boton de envio si no se aceptan las condiciones legales
//        if (document.forms[0].legal_ok.checked==0) 
//		{ desactiva();
//		}
		
	} else {
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;

        if (document.forms[0].legal_ok.checked==1)
		{
		   document.getElementById('legal').style.backgroundColor='#fff';							
		}
	
//activa el boton de envio si se aceptan las condiciones legales
//	if (document.forms[0].legal_ok.checked==1) 
//		{ activa('Form_es', 'Envia', this.value!='');
//		} 
		
	}
}

window.onload = init;

function borra_checks(){
	
	//get all the input fields on the page
	inputs = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i=0; i < inputs.length; i++) {

		//check if the input is a checkbox
		if(inputs[i].getAttribute('type') == 'checkbox') 
		  {
		     document.getElementById('checkImage'+i).src=imgFalse;
		  }
	}
}


// Activa o desactiva el envio, en el caso de que estuviera desabilitada la CSS y las imagenes false y true
//function comprueba(){
//	
//	if (document.forms[0].legal_ok.checked==1) 
//		{ activa('Form', 'Envia', this.value!=''); } 
//		else {  document.forms[0].Envia.disabled=1; }	
//	
//}

function desactiva(){
  
    document.forms[0].Envia.disabled=1;
}

function activa(){

	var state = !arguments[arguments.length-1];
	for(var i=1; i<arguments.length-1; i++) 
	{
		document.forms[arguments[0]][arguments[i]].disabled = state;
	}
}


// check formulario y envio
function comprueba_envia(){

var ok=0;

if ((document.forms[0].Nom.value == '') || (document.forms[0].Adreca.value == '') || (document.forms[0].Poblacio.value == '') || (document.forms[0].Provincia.value == '') || (document.forms[0].CP.value == '') || (document.forms[0].Tel.value == '') || (document.forms[0].Email.value == '') || (document.forms[0].legal_ok.checked == '0'))
{
	ok = 0;
	if (document.form_es) {
							document.forms[0].Error.value ='Rellene los campos marcados en rojo';
							document.forms[0].Error.disabled=0
							} else {
									document.forms[0].Error.value ='Ompli els camps marcats en vermell';
									document.forms[0].Error.disabled=0 }
	
} else 	ok = 1;
		
		
		

//colores y texto error en cajas
if (document.forms[0].Nom.value == '')
	{ document.forms[0].Nom.style.backgroundColor = '#fcc';
	  document.forms[0].Nom.style.borderColor = '#f00';
	} 
	 
	 
if (document.forms[0].Adreca.value == '')
	{ document.forms[0].Adreca.style.backgroundColor = '#fcc';
	  document.forms[0].Adreca.style.borderColor = '#f00';
	 } 
	 
if (document.forms[0].Poblacio.value == '')
	{ document.forms[0].Poblacio.style.backgroundColor = '#fcc';
	  document.forms[0].Poblacio.style.borderColor = '#f00';
	 } 
	 
if (document.forms[0].Provincia.value == '')
	{ document.forms[0].Provincia.style.backgroundColor = '#fcc';
	  document.forms[0].Provincia.style.borderColor = '#f00';
	 } 
	 
if (document.forms[0].CP.value == '')
	{ document.forms[0].CP.style.backgroundColor = '#fcc';
	  document.forms[0].CP.style.borderColor = '#f00';
	 } 	 
	 	 
if (document.forms[0].Tel.value == '')
	{ document.forms[0].Tel.style.backgroundColor = '#fcc';
	  document.forms[0].Tel.style.borderColor = '#f00';
	 } 
	 
if (document.forms[0].Email.value == '') 
	{ document.forms[0].Email.style.backgroundColor = '#fcc';
	  document.forms[0].Email.style.borderColor = '#f00';
	 } 
if (document.forms[0].legal_ok.checked == '0') 
	{ document.getElementById('legal').style.backgroundColor = '#fcc';
	  //document.forms[0].legal_ok.style.backgroundColor = '#fcc';
	  //document.forms[0].legal_ok.style.borderColor = '#f00';
	 } 

///alert (ok);
if (ok == 1) { document.forms[0].submit();}
}






