function checkFieldTask(aFieldName){
	var bool = true;
    switch(aFieldName){
		case "pie_motivo":
		case "motivo":
			if($("#"+aFieldName).val()==""){
				failField(aFieldName);
                bool = false;
            }else{
				passField(aFieldName);
            }
            break;
		case "pie_nombre":
		case "nombre":
			//var nom_valido=$("#"+aFieldName).val().match(/[^a-zA-Z áéíóúüÁÉÍÓÚÜÑñÇç·.]/);
            if(($("#"+aFieldName).val()=="") || ($("#"+aFieldName).val()=="Nombre y Apellidos...")/* || (nom_valido != null)*/){
				failField(aFieldName);
                bool = false;
            } else {
				passField(aFieldName);
            }
            break;
		case "pie_email":
		case "email":
			if(($("#"+aFieldName).val()=="") || ($("#"+aFieldName).val()=="Email...") || (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#"+aFieldName).val())))){
				failField(aFieldName);
                bool = false;
            }else{
				passField(aFieldName);
            }
            break;
		case "pie_telefono":
		case "telefono":
			if(($("#"+aFieldName).val()=="") || ($("#"+aFieldName).val()=="Telefono...")){
				failField(aFieldName);
                bool = false;
			}else{
				// Puede empezar con + o con un numero
				elemOk1 = $("#"+aFieldName).val().substr(0,1) == "+" || !isNaN($("#"+aFieldName).val().substr(0,1));
				// Tiene entre 9 y 20 caracteres
				elemOk2 = $("#"+aFieldName).val().length >= 9 && $("#"+aFieldName).val().length <= 20;
				// Todos los caracteres son numeros excepto el primero que podia ser un +
				for(j=1;j<$("#"+aFieldName).val().length;j++){
					if($("#"+aFieldName).val().substr(j,1) == " " || !isNaN($("#"+aFieldName).val().substr(j,1))){
						elemOk3 = true;
					}else{
						elemOk3	= false;
						break;
					}
				}
				elemOk = elemOk1 && elemOk2 && elemOk3;
				if (!elemOk) {
					failField(aFieldName);
					bool = false;
				}else{
					passField(aFieldName);
				}
			}
            break;
		case "pie_empresa":
		case "empresa":
			if(($("#"+aFieldName).val()=="") || ($("#"+aFieldName).val()=="Empresa...")){
				failField(aFieldName);
                bool = false;
            }else{
				passField(aFieldName);
            }
            break;
		case "pie_provincia":
		case "provincia":
			if(($("#"+aFieldName).val()=="") || ($("#"+aFieldName).val()=="Provincia...")){
				failField(aFieldName);
                bool = false;
            }else{
				passField(aFieldName);
            }
            break;
		case "pie_comentarios":
		case "comentarios":
			var com_valido=$("#"+aFieldName).val().match(/[^a-zA-Z0-9 áéíóúüÁÉÍÓÚÜÑñÇç·.()-_\n][\n]/);
            if(($("#"+aFieldName).val()=="") || ($("#"+aFieldName).val()=="Comentarios...") || (com_valido != null)){
				failField(aFieldName);
                bool = false;
            }else{
				passField(aFieldName);
            }
            break;
		case "pie_codigo":
		case "codigo":
			if(($("#"+aFieldName).val()=="") || ($("#"+aFieldName).val()=="Introduce el codigo de la imagen...")){
				failField(aFieldName);
                bool = false;
            }else{
				passField(aFieldName);
            }
            break;
		case "pie_acepta":
		case "acepta":
			if($("#"+aFieldName).attr("checked")==false){
				if(aFieldName.indexOf("pie_") == -1){//NO pie_
					$("span."+aFieldName).css("color","#ffb401");
				}else{
					$("span."+aFieldName).css("color","#ff0000");
					$("span."+aFieldName).css("background","#fcfda2");
				}
                bool = false;
            }else{
				if(aFieldName.indexOf("pie_") == -1){//NO pie_
					$("span."+aFieldName).css("color","#ffffff");
				}else{
					$("span."+aFieldName).css("color","#000000");
				}
            }
            break;
	}
    return bool;
}


function checkField(aField){
	return checkFieldTask(aField.name);
}
function checkForm(aForm){
	var bool = true;
    for(var i=0;i<aForm.elements.length;i++){
		if(!checkFieldTask(aForm.elements[i].name)){
			bool = false;
		}
	}
	if(bool){
		//passField("envio");
		//passField("pie_envio");
		cargarContenidoContactoOk(aForm);
	}else{
    	//failField("envio");
    	//failField("pie_envio");
	}
	return bool;
}
function passField(aFieldName){
	if(aFieldName.indexOf("pie_") == -1){//NO pie_
        $("#"+aFieldName).css("color","#ffffff");
	}else{
        $("#"+aFieldName).css("color","#000000");
	}
}
function failField(aFieldName){
	if(aFieldName.indexOf("pie_") == -1){//NO pie_
		$("#"+aFieldName).css("color","#ffb401");
	}else{
		$("#"+aFieldName).css("color","#ff0000");
		$("#"+aFieldName).css("background","#fcfda2");
	}
}

$(function() {
// clickable
	if($('.clickable').length != 0){
		makeClick();
	}
// vaciar/llenar campos formulario	
	$("input").focus(function () {
		if($(this).attr('id')!="envio"){
			switch($(this).attr('id')){
				//case "pie_nombre":
				case "nombre":
					if($(this).val() == "Nombre y Apellidos...")$(this).attr("value","");
					break;
				//case "pie_email":
				case "email":
					if($(this).val() == "Email...")$(this).attr("value","");
					break;
				//case "pie_telefono":
				case "telefono":
					if($(this).val() == "Telefono...")$(this).attr("value","");
					break;
				//case "pie_empresa":
				case "empresa":
					if($(this).val() == "Empresa...")$(this).attr("value","");
					break;
				//case "pie_provincia":
				case "provincia":
					if($(this).val() == "Provincia...")$(this).attr("value","");
					break;
				//case "pie_codigo":
				case "codigo":
					if($(this).val() == "Introduce el codigo de la imagen...")$(this).attr("value","");
					break;
				case "form_email_baja":
					if($(this).val() == "Email...")$(this).attr("value","");
					break;
				case "form_email_alta":
					if($(this).val() == "Email...")$(this).attr("value","");
					break;
			}
		}
    });
	$("textarea#comentarios").focus(function () {
		if($(this).val() == "Comentarios...")$(this).attr("value","");
    });
	$("input").blur(function () {
		switch($(this).attr('id')){
			//case "pie_nombre":
			case "nombre":
				if($(this).val() == "")$(this).attr("value","Nombre y Apellidos...");
				break;
			//case "pie_email":
			case "email":
				if($(this).val() == "")$(this).attr("value","Email...");
				break;
			//case "pie_telefono":
			case "telefono":
				if($(this).val() == "")$(this).attr("value","Telefono...");
				break;
			//case "pie_empresa":
			case "empresa":
				if($(this).val() == "")$(this).attr("value","Empresa...");
				break;
			//case "pie_provincia":
			case "provincia":
				if($(this).val() == "")$(this).attr("value","Provincia...");
				break;
			//case "pie_codigo":
			case "codigo":
				if($(this).val() == "")$(this).attr("value","Introduce el codigo de la imagen...");
				break;
			case "form_email_baja":
				if($(this).val() == "")$(this).attr("value","Email...");
				break;
			case "form_email_alta":
				if($(this).val() == "")$(this).attr("value","Email...");
				break;
		}
    });
	$("textarea#comentarios").blur(function () {
		if($(this).val() == "")$(this).attr("value","Comentarios...");
    });
});


	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 cargarContenidoContactoOk(aForm){
		if(aForm.id == "pie_contact-form"){
			t1 = aForm.pie_motivo.value;
			t2 = aForm.pie_nombre.value;
			t3 = aForm.pie_email.value;
			t4 = aForm.pie_telefono.value;
			t5 = aForm.pie_empresa.value;
			t6 = aForm.pie_provincia.value;
			t7 = aForm.pie_comentarios.value;
			t8 = aForm.pie_codigo.value;
			t9 = aForm.pie_agente.value;
			t10 = aForm.pie_seccion.value;
			t11 = aForm.pie_idioma.value;
			t12 = aForm.pie_charset.value;
			t13 = aForm.pie_dpto.value;
			t14 = aForm.pie_acepta.checked;
			t15 = aForm.pie_formulario.value;
			id = document.getElementById('pie_formenvio');
		}else{
			t1 = aForm.motivo.value;
			t2 = aForm.nombre.value;
			t3 = aForm.email.value;
			t4 = aForm.telefono.value;
			t5 = aForm.empresa.value;
			t6 = aForm.provincia.value;
			t7 = aForm.comentarios.value;
			t8 = aForm.codigo.value;
			t9 = aForm.agente.value;
			t10 = aForm.seccion.value;
			t11 = aForm.idioma.value;
			t12 = aForm.charset.value;
			t13 = aForm.dpto.value;
			t14 = aForm.acepta.checked;
			t15 = aForm.formulario.value;
			id = document.getElementById('formenvio');
		}
		ajax=nuevoAjax();
		ajax.open("POST", "/marketing/formContactHome/f_contactoOk.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				id.innerHTML = ajax.responseText
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=iso-8859-1");
		ajax.send("motivo="+t1+"&nombre="+t2+"&email="+t3+"&telefono="+t4+"&empresa="+t5+"&provincia="+t6+"&comentarios="+t7+"&codigo="+t8+"&agente="+t9+"&seccion="+t10+"&idioma="+t11+"&charset="+t12+"&dpto="+t13+"&acepta="+t14+"&formulario="+t15);
		
	}
	function cargarContenidoContactoError(situ){
		if(situ == "pie"){
			t1 = document.getElementById("pie_motivo").value;
			t2 = document.getElementById("pie_nombre").value;
			t3 = document.getElementById("pie_email").value;
			t4 = document.getElementById("pie_telefono").value;
			t5 = document.getElementById("pie_empresa").value;
			t6 = document.getElementById("pie_provincia").value;
			t7 = document.getElementById("pie_comentarios").value;
			t8 = document.getElementById("pie_seccion").value;
			id = document.getElementById('pie_formenvio');
			ajax=nuevoAjax();
			ajax.open("POST", "/marketing/formContactHome/f_contacto_pie.php",true);
		}else{
			t1 = document.getElementById("motivo").value;
			t2 = document.getElementById("nombre").value;
			t3 = document.getElementById("email").value;
			t4 = document.getElementById("telefono").value;
			t5 = document.getElementById("empresa").value;
			t6 = document.getElementById("provincia").value;
			t7 = document.getElementById("comentarios").value;
			t8 = document.getElementById("seccion").value;
			id = document.getElementById('formenvio');
			ajax=nuevoAjax();
			ajax.open("POST", "/marketing/formContactHome/f_contacto.php",true);
		}
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				id.innerHTML = ajax.responseText
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=iso-8859-1");
		ajax.send("motivo="+t1+"&nombre="+t2+"&email="+t3+"&telefono="+t4+"&empresa="+t5+"&provincia="+t6+"&comentarios="+t7+"&seccion="+t8);
	}

	random_img=0;
	function cargarContenidoImgSec(situ){
		if(situ == "pie"){
			id = document.getElementById('pie_img-php');
		}else{
			id = document.getElementById('img-php');
		}
		random_img++;
	   ajax=nuevoAjax();
	   ajax.open("POST","/marketing/formContactHome/f_contactoImgSec.php",true);
	   ajax.onreadystatechange=function() {
		  if (ajax.readyState==4) {
			 id.innerHTML = ajax.responseText;
		   }
	   }
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=iso-8859-1");
		ajax.send("random_img="+random_img);
	}
/* fin formulario contacto */
	
	
	
/* clickable */
//-----------------------------------------------------------------------------
function doBannerClick(_href){
	document.location = _href;
}

//-----------------------------------------------------------------------------
function doBannerClick_external(_href){
	window.open(_href);
}
//-----------------------------------------------------------------------------
function makeClick()
{
	var elems = document.getElementsByClassName("clickable");
	for (var i=0; i<elems.length; i++) {
		var hrefs = elems[i].getElementsByTagName("a");
		if (hrefs.length) {
			elems[i].href = hrefs[0].href;
			elems[i].onclick = function() { doBannerClick(this.href); };
		}
	}
	var elems_external = document.getElementsByClassName("clickable_external");
	for (var i=0; i<elems_external.length; i++) {
		var hrefs_external = elems_external[i].getElementsByTagName("a");
		if (hrefs_external.length) {
			elems_external[i].href = hrefs_external[0].href;
			elems_external[i].onclick = function() { doBannerClick_external(this.href); };
		}
	}
}
/* fin clickable */
