var anchoPantalla;
var altoPantalla;
var posicionScroll;

function preloadImages(img){
	image = new Image();
	image.src = img;
}

function valEmail(valor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor)){
        return false;
    }else{
        return true;
    }
}


function valTelf(valor){
	if( !(/^\d{9}$/.test(valor)) ) {
		return false;
	}else{
		return true;
	}
}


function ponerCampoNormal(cual){
	if (cual.name == "comentarios") cual.className = "campoTextoGrande";
	else cual.className = "campoTexto";
}

function validarFormulario(){
	var validado = true;
	
	if (document.contacto.nombre.value == ""){
		validado = false;
		document.contacto.nombre.className = "error";
	}
	
	if (document.contacto.direccion.value == ""){
		validado = false;
		document.contacto.direccion.className = "error";
	}
	
	if (document.contacto.telefono.value == "" || !valTelf(document.contacto.telefono.value)){
		validado = false;
		document.contacto.telefono.className = "error";
	}
	
	if (document.contacto.fax.value != "" && !valTelf(document.contacto.fax.value)){
		validado = false;
		document.contacto.fax.className = "error";
	}
	
	if (document.contacto.email.value == ""){
		validado = false;
		document.contacto.email.className = "error";
	}
	
	if (document.contacto.email.value == "" || !valEmail(document.contacto.email.value)){
		validado = false;
		document.contacto.email.className = "error";
	}
	
	if (document.contacto.comentarios.value == ""){
		validado = false;
		document.contacto.comentarios.className = "errorGrande";
	}
	
	if (validado) document.contacto.submit();
}

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 calcularAltoPantalla(){
	if (self.innerHeight) {
		altoPantalla = parseInt(self.innerHeight);
	}else if (document.documentElement && document.documentElement.clientHeight) {
		altoPantalla = parseInt(document.documentElement.clientHeight);
	}else if (document.body.clientHeight) {
		altoPantalla = parseInt(document.body.clientHeight);
	}
	
	return altoPantalla;
}

function calcularAnchoPantalla(){
	if (self.innerWidth) {
		anchoPantalla = parseInt(self.innerWidth);
	}else if (document.documentElement && document.documentElement.clientWidth) {
		anchoPantalla = parseInt(document.documentElement.clientWidth);
	}else if (document.body.clientWidth) {
		anchoPantalla = parseInt(document.body.clientWidth);
	}
	
	if (navigator.appVersion.indexOf("MSIE 8") != -1) anchoPantalla = anchoPantalla + 20
	
	return anchoPantalla;
}

function posicionarAviso(){
	anchoPantalla = calcularAnchoPantalla();
	altoPantalla = calcularAltoPantalla();
	posicionScroll = calcularposScroll();
	
	if (document.getElementById('fondoVideo').style.visibility == 'visible') {
		document.getElementById('fondoVideo').style.width = anchoPantalla + "px";
		document.getElementById('fondoVideo').style.height = altoPantalla + "px";
		document.getElementById('fondoVideo').style.visibility = 'visible';
		document.getElementById('fondoVideo').style.top = posicionScroll + "px";
		
		document.getElementById('video').style.left = anchoPantalla/2 - 320/2 + "px";
		document.getElementById('video').style.top = posicionScroll + altoPantalla/2 - 240/2 + "px";
	}
}

function calcularposScroll(){
	if (navigator.appVersion.indexOf("MSIE") != -1) return document.body.parentNode.scrollTop;
	else return window.scrollY;
}

function cerrarVideo(){
	document.getElementById('video').innerHTML = "";
	document.getElementById('fondoVideo').style.width = "10px";
	document.getElementById('fondoVideo').style.height = "10px";
	document.getElementById('fondoVideo').style.visibility = 'hidden';
	
	if (navigator.appVersion.indexOf("Safari") != -1) document.body.className = "scrollBarVisibleSafari";
	else document.body.className = "scrollBarVisible";
}

function abrirVideo(cual){
	anchoPantalla = calcularAnchoPantalla();
	altoPantalla = calcularAltoPantalla();
	posicionScroll = calcularposScroll();
	
	document.body.className = "scrollBarHidden";
	
	document.getElementById('fondoVideo').style.width = anchoPantalla + "px";
	document.getElementById('fondoVideo').style.height = altoPantalla + "px";
	document.getElementById('fondoVideo').style.visibility = 'visible';
	document.getElementById('fondoVideo').style.top = posicionScroll + "px";
	
	document.getElementById('video').style.left = anchoPantalla/2 - 320/2 + "px";
	document.getElementById('video').style.top = posicionScroll + altoPantalla/2 - 240/2 + "px";
	
	var ap=nuevoAjax();
	ap.open("POST", "areadoc/media/" +cual, true);
	ap.onreadystatechange=function(){
		if (ap.readyState==4){ 
			document.getElementById('video').innerHTML = ap.responseText;
		} 
	}
	
    ap.send(null);
}

