// JavaScript Document

var esIE = (window.navigator.userAgent.indexOf("MSIE")> 0);  

if (! esIE){   
  HTMLElement.prototype.__defineGetter__("innerText",function () { return(this.textContent); });   
  HTMLElement.prototype.__defineSetter__("innerText",function (txt) { this.textContent = txt; });
}

var nav4 = window.Event ? true : false;
function acceptNum(evt){	
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57	
var key = nav4 ? evt.which : evt.keyCode;	
return (key <= 13 || (key >= 48 && key <= 57));
}

function mostraSecondHome(formulari, quin){
	var checkyes = formulari['sh_yes'];
	var checkno = formulari['sh_no'];
	//alert("si "+checkyes.checked);
	//alert("no "+checkno.checked);
	if(quin == 1){
		if(checkyes.checked==true){
			document.getElementById('capa_sh_yes').style.display = '';
			document.getElementById('newsletter').style.display = '';
			document.getElementById('capa_sh_no').style.display = 'none';
			document.getElementById('missatge_inicial').style.display = 'none';
			checkno.checked=false;
		} else{
			document.getElementById('capa_sh_yes').style.display = 'none';
			document.getElementById('missatge_inicial').style.display = '';
			document.getElementById('newsletter').style.display = 'none';
		}
	} else if(quin == 0){
		if(checkno.checked==true){
			document.getElementById('capa_sh_no').style.display = '';
			
			if(document.getElementById('holiday_yes').checked==true || document.getElementById('holiday_no').checked==true)
				document.getElementById('newsletter').style.display = '';
			else document.getElementById('newsletter').style.display = 'none';
			document.getElementById('capa_sh_yes').style.display = 'none';
			document.getElementById('missatge_inicial').style.display = 'none';
			checkyes.checked=false;
		} else{
			document.getElementById('capa_sh_no').style.display = 'none';
			document.getElementById('missatge_inicial').style.display = '';
			document.getElementById('newsletter').style.display = 'none';
		}
	}
}

function mostraHolidayHome(formulari, quin){
	var checkyes = formulari['holiday_yes'];
	var checkno = formulari['holiday_no'];
	document.getElementById('newsletter').style.display = '';
	if(quin == 1){
		if(checkyes.checked==true){
			document.getElementById('hh_yes').style.display = '';
			checkno.checked=false;
		} else{
			document.getElementById('hh_yes').style.display = 'none';
			checkno.checked=true;
		}
	} else if(quin == 0){
		if(checkno.checked==true){
			document.getElementById('hh_yes').style.display = 'none';
			checkyes.checked=false;
		} else{
			document.getElementById('hh_yes').style.display = '';
			checkyes.checked=true;
		}
	}
}

function escullVendre(formulari, quin){
	var checkyes = formulari['selling_yes'];
	var checkno = formulari['selling_no'];
	if(quin==1){
		if(checkyes.checked==true) checkno.checked=false;
		else checkno.checked=true;
	} else if(quin==0){
		if(checkno.checked==true) checkyes.checked=false;
		else checkyes.checked=true;
	}
}

function escullCompra(formulari, quin){
	var checkyes = formulari['buying_yes'];
	var checkno = formulari['buying_no'];
	if(quin==1){
		if(checkyes.checked==true) checkno.checked=false;
		else checkno.checked=true;
	} else if(quin==0){
		if(checkno.checked==true) checkyes.checked=false;
		else checkyes.checked=true;
	}
}

function resetejaCheckeds(){
	document.getElementById('sh_yes').checked=false;
	document.getElementById('sh_no').checked=false;
	document.getElementById('holiday_yes').checked=false;
	document.getElementById('holiday_no').checked=false;
	document.getElementById('selling_yes').checked=false;
	document.getElementById('selling_no').checked=false;
	document.getElementById('buying_yes').checked=false;
	document.getElementById('buying_no').checked=false;
	document.getElementById('contacted').checked=true;
	document.getElementById('receive').checked=true;
	document.getElementById('conditions').checked=true;
}

function validaDia(){
	var expresion = new RegExp("^\\d\\d$");
	dia = document.getElementById("dd").value;
	if(!expresion.test(dia)){
		//alert("Error! El dia introduït és incorrecte.");
		return false;
	} else if(dia>=1 && dia<=31){
		return true;
	} else{
		//alert("Dia més petit que 1 o més gran que 31");
		return false;
	}
}

function validaMes(){
	var expresion = new RegExp("^\\d\\d$");
	mes = document.getElementById("mm").value;
	if(!expresion.test(mes)){
		//alert("Error! El mes introduït és incorrecte.");
		return false;
	} else if(mes>=1 && mes<=12){
		return true;
	} else{
		//alert("Mes més petit que 1 o més gran que 12");
		return false;
	}
}

function validaAny(){
	var expresion = new RegExp("^\\d\\d\\d\\d$");
	any = document.getElementById("yyyy").value;
	if(!expresion.test(any)){
		//alert("Error! L'any introduït és incorrecte.");
		return false;
	} else if(any<=2008){
		return true;
	} else{
		//alert("any més gran que 2008");
		return false;
	}
}

function validaCodigoPostal(){
	//var expresion = new RegExp("^\\d\\d\\d\\d\\d$");
	codigo = document.getElementById("postal_code").value;
	if(codigo.length>=3 && codigo.length<=6){ 
		var num=!isNaN(codigo);
		return num;
		/*if(!expresion.test(codigo)){
			//alert("Error! El codi postal introduït és incorrecte.");
			return false;
		} else{
			return true;
		}*/
	} else return false;
}

function validaEmail(){
	email = document.getElementById("email").value;
	var RegExPattern = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	if (email.match(RegExPattern)){
		return true;
	} else{
		//alert("Error! Correu incorrecte");
		return false;
	}
}

function validaTelefon(){
	var telefon = document.getElementById("telephone").value;
	var telefon1 = document.getElementById("telephone1").value;
	telefon = telefon.split(" ").join("");
	telefon1 = telefon1.split(" ").join("");
	
	//alert(telefon);
	
	var num1=!isNaN(telefon);
	var num2=!isNaN(telefon1);
	
	if(num1==false || num2==false){
		//alert("Error! El telefon introduït és incorrecte.");
		return false;
	} else{
		return true;	
	}
}

function validaEuros(){
	if(formValidaPle(document.getElementById("hh_price").value)==false) return true;
	else{
		preu = document.getElementById("hh_price").value;
		preu = preu.split(".").join("");
		preu = preu.split(",").join("");
		//alert(preu);
		var num=!isNaN(preu);
		//if(num==false) alert("Error! Valor de preu no correcte");
		return num;
	}
}

/*function validaForm(){
	if(document.getElementById('conditions').checked==true){
		document.getElementById('textCondicions').style.color="#000000";
		if(validaDia() && validaMes() && validaAny() && validaCodigoPostal() && validaEmail() && validaTelefon()){
			return true;
		}else{
			return false;
		}
	} else{
		document.getElementById('textCondicions').style.color="#F54029";
		return false;
	}
}*/

function formValidaPle(valor) {
	var j;
	for ( j = 0; j < valor.length; j++ ) {
		if ( valor.charAt(j) != " " ) {
			return true;
		}
	}
	return false;
}

function validaFormulari(){
	var control = 0;
	var dataControl = 0;
	var telefonControl = 0;
	
	if(formValidaPle(document.getElementById('name').value)==false){ document.getElementById('nom').style.color="#F54029"; control = 1; }
	else document.getElementById('nom').style.color="#000000";
	if(formValidaPle(document.getElementById('surname').value)==false){ document.getElementById('cognom').style.color="#F54029"; control = 1; }
	else document.getElementById('cognom').style.color="#000000";
	if(formValidaPle(document.getElementById('mm').value)==false){ document.getElementById('data').style.color="#F54029"; control = 1; dataControl=1; }
	if(formValidaPle(document.getElementById('dd').value)==false){ document.getElementById('data').style.color="#F54029"; control = 1; dataControl=1; }
	if(formValidaPle(document.getElementById('yyyy').value)==false){ document.getElementById('data').style.color="#F54029"; control = 1; dataControl=1; }
	if(dataControl==0){
		if(validaDia() && validaMes() && validaAny()) document.getElementById('data').style.color="#000000";
		else{ document.getElementById('data').style.color="#F54029"; control=1; }
	}
	if(formValidaPle(document.getElementById('address').value)==false){ document.getElementById('adresa').style.color="#F54029"; control = 1; }
	else document.getElementById('adresa').style.color="#000000";
	if(formValidaPle(document.getElementById('postal_code').value)==false){ document.getElementById('codi').style.color="#F54029"; control = 1; }
	else{
		document.getElementById('codi').style.color="#000000";
		//if(validaCodigoPostal()) document.getElementById('codi').style.color="#000000";
		//else { document.getElementById('codi').style.color="#F54029"; control = 1; }
	}
	if(formValidaPle(document.getElementById('city').value)==false){ document.getElementById('ciutat').style.color="#F54029"; control = 1; }
	else document.getElementById('ciutat').style.color="#000000";
	if(formValidaPle(document.getElementById('country').value)==false){ document.getElementById('pais').style.color="#F54029"; control = 1; }
	else document.getElementById('pais').style.color="#000000";
	if(formValidaPle(document.getElementById('email').value)==false){ document.getElementById('correu').style.color="#F54029"; control = 1; }
	else{
		if(validaEmail()) document.getElementById('correu').style.color="#000000";
		else { document.getElementById('correu').style.color="#F54029"; control = 1; }
	}
	if(formValidaPle(document.getElementById('telephone1').value)==false){document.getElementById('telefon').style.color="#F54029"; control = 1; telefonControl=1;}
	if(formValidaPle(document.getElementById('telephone').value)==false){document.getElementById('telefon').style.color="#F54029"; control = 1; telefonControl=1;}
	if(telefonControl==0){
		if(validaTelefon()) document.getElementById('telefon').style.color="#000000";
		else { document.getElementById('telefon').style.color="#F54029"; control = 1; }
	}
	if(document.getElementById('sh_yes').checked==false && document.getElementById('sh_no').checked==false){ document.getElementById('segonacasa').style.color="#F54029"; control = 1;}
	
	else if(document.getElementById('sh_yes').checked==true){
		if(document.getElementById('sh_size').value==0){ document.getElementById('sh_tamany').style.color="#F54029"; control = 1; }
		else document.getElementById('sh_tamany').style.color="#000000";
		if(document.getElementById('sh_bedrooms').value==0){ document.getElementById('sh_dormitori').style.color="#F54029"; control = 1; }
		else document.getElementById('sh_dormitori').style.color="#000000";
		if(formValidaPle(document.getElementById('sh_situated').value)==false){ document.getElementById('sh_situat').style.color="#F54029"; control = 1; }
		else document.getElementById('sh_situat').style.color="#000000";
		if(formValidaPle(document.getElementById('sh_country').value)==false){ document.getElementById('sh_pais').style.color="#F54029"; control = 1; }
		else document.getElementById('sh_pais').style.color="#000000";
		if(formValidaPle(document.getElementById('sh_area').value)==false){ document.getElementById('sh_poble').style.color="#F54029"; control = 1; }
		else document.getElementById('sh_poble').style.color="#000000";
		if(document.getElementById('selling_yes').checked==false && document.getElementById('selling_no').checked==false){ document.getElementById('sh_vendre').style.color="#F54029"; control = 1;}
		else document.getElementById('sh_vendre').style.color="#000000";
		if(document.getElementById('buying_yes').checked==false && document.getElementById('buying_no').checked==false){ document.getElementById('sh_comprar').style.color="#F54029"; control = 1;}
		else document.getElementById('sh_comprar').style.color="#000000";
		
	} else if(document.getElementById('sh_no').checked==true){
		if(document.getElementById('holiday_yes').checked==false && document.getElementById('holiday_no').checked==false){ document.getElementById('casavacances').style.color="#F54029"; control = 1;}
		else document.getElementById('casavacances').style.color="#000000";
		if(document.getElementById('holiday_yes').checked==true){
			if(document.getElementById('hh_size').value==0){ document.getElementById('hh_tamany').style.color="#F54029"; control = 1; }
			else document.getElementById('hh_tamany').style.color="#000000";
			if(document.getElementById('hh_bedrooms').value==0){ document.getElementById('hh_dormitori').style.color="#F54029"; control = 1; }
			else document.getElementById('hh_dormitori').style.color="#000000";
			if(formValidaPle(document.getElementById('hh_country').value)==false){ document.getElementById('hh_pais').style.color="#F54029"; control = 1; }
			else document.getElementById('hh_pais').style.color="#000000";
			if(formValidaPle(document.getElementById('hh_area').value)==false){ document.getElementById('hh_poble').style.color="#F54029"; control = 1; }
			else document.getElementById('hh_poble').style.color="#000000";
			if(!validaEuros()){ document.getElementById('hh_preu').style.color="#F54029"; control = 1; }
			else document.getElementById('hh_preu').style.color="#000000";
		}
	}
	
	if(document.getElementById('conditions').checked==true) document.getElementById('textCondicions').style.color="#000000";
	else{ document.getElementById('textCondicions').style.color="#F54029"; control=1; }
	
	if(control==0){
		//alert("retorna true");
		return true;
	} else{
		//alert("retorna false");
		document.getElementById('faltenCamps').style.display = '';
		return false;
	}
	
}

var page = "conditions.php";
var windowprops = "width=600,height=600,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no";
function SaltaPopup(){
	//window.moveTo(0,0);
	//window.resizeTo(screen.width, screen.height);
	window.open(page, "", windowprops);
}

