/*	variables globales	*/
var goMsg = {};
var gcMsg_telefono = "> El tel&eacute;fono debe tener 9 d&iacute;gitos.<br>";
var gcMsg_nombre = "> El nombre es un campo obligatorio.<br>";
var gcMsg_email_vacio = "> El email es un campo obligatorio.<br>";
var gcMsg_email_novalido = "> El email no tiene un formato correcto.<br>";
var gcMsg_mensaje = "> El mensaje es un campo obligatorio.<br>";
var gcMsg_asunto = "> El asunto es un campo obligatorio.<br>";
var gcMsg_web = " > La p&aacute;gina web no tiene formato correcto<br>";

/*	funciones globales	*/
function ocultar(poElemento){Element.addClassName(poElemento, 'oculto');}
function mostrar(poElemento){Element.removeClassName(poElemento, 'oculto');}
function quitarError(poElemento){Element.removeClassName(poElemento.parentNode, 'error');}
function ponerError(poElemento){Element.addClassName(poElemento.parentNode, 'error');}

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 cargarListaPeliculas(){
	ajax=nuevoAjax();
	ajax.open("POST", "buscarPelicula.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			var respuesta = ajax.responseText;
			if(respuesta!=""){
				$('resultados').innerHTML=respuesta;
				$('resultados').style.display="block";
			} else {
				$('resultados').style.display="none";
			}
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id_busqueda="+escape($F('id_busqueda')));
}

function cargarContacto(){
	ajax=nuevoAjax();
	ajax.open("POST", "includes/inserciones.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			$('msg-confirmacion').innerHTML = '<h3>Gracias por escribirnos ' + $F('id_nombre') +'!!!</h3> En breve nos pondremos en contacto contigo.</p>'
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("nombre="+$F('id_nombre')+"&email="+ $F('id_email')+ "&tlf=" + $F('id_tlf')+ "&asunto=" + $F('id_asunto')+ "&mensaje="+ $F('id_mensaje'));
	$('id_nombre').clear();
	$('id_email').clear();
	$('id_tlf').clear();
	$('id_asunto').clear();
	$('id_mensaje').value='';
}

function cargarComentario(){
	ajax=nuevoAjax();
	ajax.open("POST", "includes/inserciones.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			$('msg-confirmacion').innerHTML = $('msg-confirmacion').innerHTML = '<h3>Gracias por escribirnos ' + $F('id_nombre') +'!!!</h3> El comentario se publicar&aacute; lo antes posible.</p>'
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id_noticia="+$F('id_noticia')+"&nombre="+escape($F('id_nombre'))+"&email="+ $F('id_email')+ "&web=" + $F('id_web')+ "&comentario="+ escape($F('id_mensaje')));
	$('id_nombre').clear();
	$('id_email').clear();
	$('id_web').value='http://';
	$('id_mensaje').value='';
}



/*	funcion de inicio	*/	
/*	-	_onLoad()
		siempre se carga en el evento onload	*/
Event.observe(window, 'load', _onLoad, false);

function _onLoad(){
	
	if ($('msg')) {
		goMsg = new Msg($('msg'));
		goMsg.ocultar();
	}
	
	if ($('msg-confirmacion')){
		goMsgConfirmacion = new Msg($('msg-confirmacion'));
		goMsgConfirmacion.ocultar();
	}
		
	//FORMULARIO DE CONTACTO
	if ($('form-contacto')) {
		Event.observe('b-contacto', 'click', function(e){
			var bCorrecto = true;
			var iNCampos = 0;
			
			goMsg.limpiar();
			
			//	primero valido los campos del formulario
			if ($('id_mensaje').value == '') {
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-mensaje'), 'errorLabel');
				goMsg.nuevo('em', 'msg-alerta', gcMsg_mensaje);
			}
			else {
				Element.removeClassName($('label-mensaje'), 'errorLabel');
			}
			
			if ($('id_nombre').value == '') {
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-nombre'), 'errorLabel');
				goMsg.nuevo('em', 'msg-alerta', gcMsg_nombre);
			}
			else {
				Element.removeClassName($('label-nombre'), 'errorLabel');
			}
			
			if ($('id_email').value == '' || !validarEmail($('id_email').value)) {
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-email'), 'errorLabel');
				if ($('id_email').value == '') {
					goMsg.nuevo('em', 'msg-alerta', gcMsg_email_vacio);
				}
				else {
					goMsg.nuevo('em', 'msg-alerta', gcMsg_email_novalido);
				}
			}
			else {
				Element.removeClassName($('label-email'), 'errorLabel');
			}
			
			if ($('id_tlf').value > 0 && $('id_tlf').value.length < 9 || isNaN($('id_tlf').value)) {
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-tlf'), 'errorLabel');
				goMsg.nuevo('em', 'msg-alerta', gcMsg_telefono);
			}
			else {
				Element.removeClassName($('label-tlf'), 'errorLabel');
			}
			
			if ($('id_asunto').value == '') {
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-asunto'), 'errorLabel');
				goMsg.nuevo('em', 'msg-alerta', gcMsg_asunto);
			}
			else {
				Element.removeClassName($('label-asunto'), 'errorLabel');
			}
			
			if (iNCampos > 0) {
				goMsg.nuevo('em', 'msg-info', "Revisa los siguientes errores: <br>");
			}
			
			if (bCorrecto) {
				goMsg.ocultar();
				goMsgConfirmacion.mostrar();
				cargarContacto();
			}
			else {
				goMsg.mostrar();
			}
			Event.stop(e);			
		}, false);
		
		Event.observe('id_tlf', 'click', function(e){
			Element.removeClassName($('label-tlf'), 'errorLabel');
			goMsgConfirmacion.ocultar(); 
			return false;
		}, false);
	    
		Event.observe('id_nombre', 'click', function(e){
			Element.removeClassName($('label-nombre'), 'errorLabel'); 
			goMsgConfirmacion.ocultar();
			return false;
		}, false);
	    
		Event.observe('id_email', 'click', function(e){
			Element.removeClassName($('label-email'), 'errorLabel'); 
			goMsgConfirmacion.ocultar();
			return false;
		}, false);
		
		Event.observe('id_asunto', 'click', function(e){
			Element.removeClassName($('label-asunto'), 'errorLabel'); 
			goMsgConfirmacion.ocultar();
			return false;
			}, false);
	    
		Event.observe('id_mensaje', 'click', function(e){
			Element.removeClassName($('label-mensaje'), 'errorLabel'); 
			goMsgConfirmacion.ocultar();
			return false;
			}, false);
		
		
		Event.observe('b-contacto', 'mouseover', function(e){
			Element.removeClassName($('b-contacto'), 'boton');
			Element.addClassName($('b-contacto'), 'botonhover');
			return false;
		}, false);
		
		Event.observe('b-contacto', 'mouseout', function(e){
			Element.removeClassName($('b-contacto'), 'botonhover');
			Element.addClassName($('b-contacto'), 'boton');
			return false;
		}, false);
	}
	
	
	//FORMULARIO DE COMENTARIOS
	if ($('form-comentario')) {
			Event.observe('b-comentario', 'click', function(e){
			var bCorrecto = true;
			var iNCampos = 0;
			
			goMsg.limpiar();
			
			//	primero valido los campos del formulario
			if ($('id_mensaje').value == ''){
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-mensaje'), 'errorLabel');
				goMsg.nuevo('em', 'msg-alerta', gcMsg_mensaje);
			} 
			else {Element.removeClassName($('label-mensaje'), 'errorLabel');}		
			
			if ($('id_nombre').value == ''){
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-nombre'), 'errorLabel');
				goMsg.nuevo('em', 'msg-alerta', gcMsg_nombre);
			} else {Element.removeClassName($('label-nombre'), 'errorLabel');}
			
		
			if ($('id_email').value == '' || !validarEmail($('id_email').value)){
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-email'), 'errorLabel');
				if ($('id_email').value == '') {
					goMsg.nuevo('em', 'msg-alerta', gcMsg_email_vacio);
				} else {
					goMsg.nuevo('em', 'msg-alerta', gcMsg_email_novalido);
				}
			} else {Element.removeClassName($('label-email'), 'errorLabel');}
			
			if ($('id_web').value != '' && $('id_web').value !='http://' && !validarUrl($('id_web').value)){
				bCorrecto = false;
				iNCampos++;
				Element.addClassName($('label-web'), 'errorLabel');
				goMsg.nuevo('em', 'msg-alerta', gcMsg_web);
			}
			else {
				Element.removeClassName($('label-web'), 'errorLabel');
			}
			
			if (iNCampos > 0){
				goMsg.nuevo('em', 'msg-info', "Revisa los siguientes errores: <br>");
			}
			
			if (bCorrecto){
				goMsg.ocultar();
				goMsgConfirmacion.mostrar();
				cargarComentario();
			} else {
				goMsg.mostrar();
			}
			Event.stop(e);
		}, false);
		
		Event.observe('id_web', 'click', function(e){
			Element.removeClassName($('label-web'), 'errorLabel');
			goMsgConfirmacion.ocultar(); 
			return false;
		}, false);
	    
		Event.observe('id_nombre', 'click', function(e){
			Element.removeClassName($('label-nombre'), 'errorLabel'); 
			goMsgConfirmacion.ocultar();
			return false;
		}, false);
	    
		Event.observe('id_email', 'click', function(e){
			Element.removeClassName($('label-email'), 'errorLabel'); 
			goMsgConfirmacion.ocultar();
			return false;
		}, false);
	    
		Event.observe('id_mensaje', 'click', function(e){
			Element.removeClassName($('label-mensaje'), 'errorLabel'); 
			goMsgConfirmacion.ocultar();
			return false;
			}, false);
		
		Event.observe('b-comentario', 'mouseover', function(e){
			Element.removeClassName($('b-comentario'), 'boton');
			Element.addClassName($('b-comentario'), 'botonhover');
			return false;
		}, false);
		
		Event.observe('b-comentario', 'mouseout', function(e){
			Element.removeClassName($('b-comentario'), 'botonhover');
			Element.addClassName($('b-comentario'), 'boton');
			return false;
		}, false);

	}
	
	//FORMULARIO DE CATALOGOS
	if ($('form-catalogo')) {		
		var php_action = 'catalogo-peliculas.php';
		
		$('s-clasificacion').selectedIndex  = $F('hidden_clasificacion');
		
		if (document.URL.substring(document.URL.lastIndexOf("/")+1) == 'material-gratuito.php'){
			php_action = 'material-gratuito.php';
		}
		
		Event.observe('id_valorBusqueda', 'click', function(e){
			$('id_valorBusqueda').value='';
			$('id_valorBusqueda').style.color='black';
			Event.stop(e);
		}, false);
		
		Event.observe('s-clasificacion', 'change', function(e){
			$('hidden_clasificacion').value = $('s-clasificacion').selectedIndex;
			Event.stop(e);
		}, false);
		
		Event.observe('b-catalogo', 'click', function(e){
			$('form-catalogo').action = php_action;
			$('form-catalogo').submit();
			Event.stop(e);
		}, false);
		
		Event.observe('b-catalogo', 'mouseover', function(e){
			Element.removeClassName($('b-catalogo'), 'boton');
			Element.addClassName($('b-catalogo'), 'botonhover');
			return false;
		}, false);
		
		Event.observe('b-catalogo', 'mouseout', function(e){
			Element.removeClassName($('b-catalogo'), 'botonhover');
			Element.addClassName($('b-catalogo'), 'boton');
			return false;
		}, false);
		
		Event.observe('b-pdf', 'click', function(e){
			$('form-catalogo').action = 'catalogo-peliculas-pdf.php';
			$('form-catalogo').submit();
			Event.stop(e);
		}, false);
		
		Event.observe('b-pdf', 'mouseover', function(e){
			Element.removeClassName($('b-pdf'), 'boton');
			Element.addClassName($('b-pdf'), 'botonhover');
			return false;
		}, false);
		
		Event.observe('b-pdf', 'mouseout', function(e){
			Element.removeClassName($('b-pdf'), 'botonhover');
			Element.addClassName($('b-pdf'), 'boton');
			return false;
		}, false);
	}
}

//-------------------------INICIO OBJETO Msg------------------------//
var Msg = Class.create();
	
Msg.prototype = {
	initialize: function(poElemento){
		//	funcion de inicio, se ejecuta al crear la instancia de la clase
		this.id = poElemento.id;
	},
	limpiar: function(){
		var aoMsg = $(this.id).childNodes;
		var iMax = aoMsg.length;
		
		for (var i = 0; i < iMax; i++) {
			Element.remove(aoMsg[0]);
		}
	},
	nuevo: function(pcTagName, pcClassName, pcInnerHTML, pcPosition){
		var cHTML = '<' + pcTagName + ' class="' + pcClassName + '">' + pcInnerHTML + '</' + pcTagName + '>';
		
		switch (pcPosition) {
			case 'After':
				new Insertion.After($(this.id), cHTML);
				break;
			case 'Before':
				new Insertion.Before($(this.id), cHTML);
				break;
			case 'Bottom':
				new Insertion.Bottom($(this.id), cHTML);
				break;
			case 'Top':
				new Insertion.Top($(this.id), cHTML);
				break;
			default:
				new Insertion.Top($(this.id), cHTML);
		}
	},
	mover: function(poElemento, pcPosition){
		var oMsg = $(this.id).cloneNode(true);
		var cHTML = '';
		
		oMsg.id = this.id + '-proxy';
		cHTML = '<div id="' + this.id + '" class="' + oMsg.className + '">' + oMsg.innerHTML + '</div>';
		Element.remove(this.id);
		oMsg.id = this.id;
		
		switch (pcPosition) {
			case 'After':
				new Insertion.After(poElemento, cHTML);
				break;
			case 'Before':
				new Insertion.Before(poElemento, cHTML);
				break;
			case 'Bottom':
				new Insertion.Bottom(poElemento, cHTML);
				break;
			case 'Top':
				new Insertion.Top(poElemento, cHTML);
				break;
			default:
				new Insertion.Top(poElemento, cHTML);
		}
	},
	ocultar: function(){
		Element.addClassName($(this.id), 'oculto');
	},
	mostrar: function(){
		Element.removeClassName($(this.id), 'oculto');
	}
}
