/*********************Preload Menu ***************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//////////////////////////////////////////

var seccion_anterior = "";
//var seccion_actual="";
var copies = {
				ingresar_usuario: 'Debes escribir nombre de usuario',
				ingresar_password: 'Debes Escribir tu password',
				cedula: 'Debes escribir tu cedula correctamente (11 digitos)',
				codigo: 'Debes escribir tu codigo correctamente',
				nombre: 'Debes escribir tu nombre correctamente',
				apellidos: 'Debes escribir tus apellidos correctamente',
				email: 'Debes escribir tu email correctamente',
				password: 'Debes escribir tu contraseña',
				genero: 'Debes elegir tu genero',
				direccion: 'Debes escribir tu Dirección correctamente',
				telefono: 'Debes escribir un telefono de 10 digitos',
				celular: 'Debes escribir un telefono de 10 digitos',
				ciudad: 'Debes escribir tu Ciudad',
				sector: 'Debes escribir tu Sector',
				terminos: 'Debes aceptar los términos y condiciones',
				fecha_nacimiento: 'Elige tu fecha de nacimiento',
				elegir_cerveza: 'Elige tu Cerveza Favorita',
				respuesta: 'Elige una respuesta',
				pais: 'Debes elegir tu Pais'
				};
		
function mostrarAlerta(msj){
	
		$('alerta').innerHTML	= msj;
		
		
	/*}else if ($('conment').getStyle('opacity') == '0'){
		cambiar = cambio =1;
	}*/
	//var morph = $('alerta').get('morph', {link: 'cancel', duration: 800});
	//morph.start({'opacity': [0, 1]});	
}
function mostrar_alerta(msj)
{
	var Contenido = "<div id='msj_alerta'>"+msj+"</div>";

	var detalle_combo = new light_box({lightBoxOpacity:0.7, tema:'alerta' });
	detalle_combo.abrirLightBox(Contenido);
}

function popup(page, height, width)
{
  var url = page;
  var h = height;
  var w = width;
  var left=(screen.width)?(screen.width - width)/2:100;
  var top=(screen.height)?(screen.height - height)/2:100;
  

 var popup = window.open(url,'popUp','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=' + w + ',height=' + h + ',left='+left+',top='+top);
 popup.focus();
}
function popup_2(page, height, width)
{
  var url = page;
  var h = height;
  var w = width;
  var left=(screen.width)?(screen.width - width)/2:100;
  var top=(screen.height)?(screen.height - height)/2:100;
  

 var popup = window.open(url,'popUp','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + w + ',height=' + h + ',left='+left+',top='+top);
 popup.focus();
}
function abrirTerminos(){
	popup("php/html/tyc.html", screen.height-150 , 520);
}
function seccion_actual(seccion){
	seccion_anterior=seccion;
}
function volver_seccion(){
	if(seccion_anterior=="codigos"){
		light_box_codigos();
	}
	seccion_anterior="";
}
function request_estado()
{	
	new Request(
			{
			method: 'get',
			url: '?opcion=inicio&a=verificar_login',
			//onLoading:loading(),
			onSuccess: function(texto){
			//	$(div).innerHTML = texto;
			},
			evalScripts:true
			}).send();
}
function request_logout()
{	
	new Request(
			{
			method: 'get',
			url: '?opcion=login&a=logout',
			//onLoading:loading(),
			onSuccess: function(texto){
			//	$(div).innerHTML = texto;
			},
			evalScripts:true
			}).send();
}
function request_loading(div)
{	
	new Request(
			{
			method: 'get',
			url: '?opcion=gdata&a=uploadForm',
			//onLoading:loading(),
			onSuccess: function(texto){
				$(div).innerHTML = texto;
			},
			evalScripts:true
			}).send();
}
function validarLogin(){
	
	var texto = /(^\S([\D]+)$)/;
	var texto_mail = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
	var Regcontrasegna = /^[aA-zZ0-9_-]{1,18}$/;
	var usuario = $('usuario').value;
	var password = $('password').value;
	
	if (!(usuario.match(texto_mail)) || usuario == ""){
		mostrarAlerta(copies.ingresar_usuario);
		$('usuario').focus();
		return;
	}else if (!(password.match(Regcontrasegna)) || password == ""){
		mostrarAlerta(copies.ingresar_password);
		$('password').focus();
		return;
	}else {
		estado_usuario();
		$('form1').submit();
	}
}
function validarOlvidoPass(){
	var texto_mail = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
	var mail = $('mail').value;
	
	if (!(mail.match(texto_mail)) || mail == ""){
		mostrarAlerta(copies.email);
		$('mail').focus();
		return;
	}else {
		$('olvido').submit();
	}
}
function loged_in(){
	var obj = window.top.document.getElementById('flashcontent');
	obj.loged_in();
	window.close();
}
function loged_out(){
	var obj = window.top.document.getElementById('flashcontent');
	obj.loged_out();
	//window.close();
}
function estado_usuario(){
	var obj = window.top.document.getElementById('flashcontent');
	obj.estado_usuario();
}
function cerrar_lightbox(){
	if(top.lightbox_actual){
		top.lightbox_actual.cerrarLight();
		top.lightbox_actual = null;
	}
}
function contactenosUp()
{	
	popup_2('?opcion=contactenos', 600, 600);
}
function contactenosValidar()
{	
	tinyMCE.triggerSave();
	var nombre = $('nombre_ayuda').value;
	var mail = $('mail_ayuda').value;
	var tipo = $('tipo').value;
	var editor = $('editor').value;

	if (nombre==""){
		mostrarAlerta("DEBES ESCRIBIR TU NOMBRE");
		$('nombre').focus();
		return;
	}else if (mail==""){
		mostrarAlerta("DEBES ESCRIBIR TU E-MAIL");
		$('mail').focus();
		return;
	}else if (tipo==0){
		mostrarAlerta("DEBES ELEGIR UN TIPO DE PROBLEMA");
		$('tipo').focus();
		return;
	}else if (editor==""){
		mostrarAlerta("DEBES ESCRIBIR LA DESCRIPCION DEL PROBLEMA");
		$('editor').focus();
		return;
	}else{
		$('contacto').submit();
	}	
}
function light_box_encuestas()
{	
	lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	lightbox_actual.abrirLightBox('?opcion=encuestas&a=inicio');
}
function light_box_FBcomments()
{	
	lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	lightbox_actual.abrirLightBox('?opcion=comments');
}
function light_box_minipromos()
{	
	lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	lightbox_actual.abrirLightBox('?opcion=minipromos');
}

function light_box_eventos()
{	
	lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	lightbox_actual.abrirLightBox('?opcion=eventos&a=inicio');
}
function light_box_ver_eventos(id)
{	
	lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	lightbox_actual.abrirLightBox('?opcion=eventos&a=ver_evento&go=1&id='+id);
}
function light_box_descargables()
{	
	lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	lightbox_actual.abrirLightBox('?opcion=descargables&a=inicio');
}
function light_box_registro()
{	
	//cerrar_lightbox();
	lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	lightbox_actual.abrirLightBox('?opcion=registro&a=form_registro');
}

function light_box_login()
{	
	cerrar_lightbox();
	top.lightbox_actual = new frame_box({lightBoxOpacity:0.7, tema:'video' });
	top.lightbox_actual.abrirLightBox('?opcion=login');
}
function light_box_codigos()
{	
	cerrar_lightbox();
	lightbox_actual = light_reg = new frame_box({lightBoxOpacity:0.7, tema:'video'});
	light_reg.abrirLightBox('?opcion=codigos&a=form_codigos');
	top.light_reg = light_reg;
}
function light_box_galeria()
{	
	cerrar_lightbox();
	lightbox_actual = light_reg = new frame_box({lightBoxOpacity:0.7, tema:'video'});
	light_reg.abrirLightBox('?opcion=gdata');
	top.light_reg = light_reg;
}

function abrirVideo(id_video){
	cerrar_lightbox();
	lightbox_actual = light_reg = new frame_box({lightBoxOpacity:0.7, tema:'video_player'});
	light_reg.abrirLightBox('?opcion=gdata&a=showVideo&id='+id_video);
	top.light_reg = light_reg;
}

function ajaxfilemanager(field_name, url, type, win) {
	var ajaxfilemanagerurl = "Scripts/tinymce/plugins/ajaxfilemanager/ajaxfilemanager.php";
	switch (type) {
		case "image":
			break;
		case "media":
			break;
		case "flash": 
			break;
		case "file":
			break;
		default:
			return false;
	}
    tinyMCE.activeEditor.windowManager.open({
        url: "Scripts/tinymce/plugins/ajaxfilemanager/ajaxfilemanager.php",
        width: 782,
        height: 440,
        inline : "yes",
        close_previous : "no"
    },{
        window : win,
        input : field_name
    });
}

function validarCodigo(){
	tinyMCE.triggerSave();
	var texto_mail = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
	var texto_codigo = /(^\S([\D]+)$)/;
	var numeros = /^([0-9]{3,20})+/;
	var codigo = $('codigo').value;
	var codigo2 = $('codigo2').value;
	var codigo3 = $('codigo3').value;
	if (!(codigo.match(texto_codigo)) || codigo.length !=8){
		mostrarAlerta(copies.codigo);
		$('codigo').focus();
	}else if ((!(codigo2.match(texto_codigo)) || codigo2.length !=8)&& codigo2!= ''){
				mostrarAlerta(copies.codigo);
				$('codigo2').focus();
		}else if ((!(codigo3.match(texto_codigo)) || codigo3.length !=8) && codigo3!= ''){
						mostrarAlerta(copies.codigo);
						$('codigo3').focus();
			}else{
				$('form_codigo').submit();
			}	
}	
function validarRegistro(){
	tinyMCE.triggerSave();
	var texto_mail = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
	var texto = /^[aA-zZ0-9_-]{1,25}$/;  
	var texto_espacios = /^(([\w\s][\s\.]?){1,50})+/;
	
	var Regcontrasegna = /^[aA-zZ0-9_-]{1,18}$/
	var numeros = /^([0-9]{3,20})+/;
	var reg_cedula = /^([0-9]{11,11})+/;
	var reg_telefono = /^([0-9]{10,10})+/;
	var reg_pais = /^([0-9]{1,3})+/;
	var reg_direccion = /^(([\w\s][\s\.]?){1,50})+/;
	var numero = /^([0-9]{1,5})+/;
	

	var cedula = $('cedula').value;
	var nombre = $('nombre').value;
	var apellidos = $('apellidos').value;
	var mail= $('mail').value;
	var password = $('password').value;
	var telefono = $('telefono').value;
	var celular = $('celular').value;
	var direccion = $('direccion').value;
	var sector = $('sector').value;
	var pais = $('pais').value;
	var amail = $('amail').checked;
	var terminos = $('terminos').checked;

	if (!(mail.match(texto_mail))|| mail == ''){
		mostrarAlerta(copies.email);
		$('mail').focus();
	}else if (!(cedula.match(reg_cedula))|| cedula.length > 11){
		mostrarAlerta(copies.cedula);
		$('cedula').focus();
	}else if (!(nombre.match(texto_espacios))|| nombre == ''){
		mostrarAlerta(copies.nombre);
		$('nombre').focus();
	}else if (!(password.match(Regcontrasegna))|| password == ''){
		mostrarAlerta(copies.password);
		$('password').focus();
	}else if (!(apellidos.match(texto_espacios))|| apellidos == ''){
		mostrarAlerta(copies.apellidos);
		$('apellidos').focus();
	}else if (!(telefono.match(reg_telefono))|| telefono.length > 10 ){
		mostrarAlerta(copies.telefono);
		$('telefono').focus();	
	}else if (!(validarRadioButtonList("genero",copies.genero))){
		$('genero').focus();
	}else if (!(terminos== true)){
		mostrarAlerta(copies.terminos);
		$('terminos').focus();
	}else if (!(validarRadioButtonList("cerveza",copies.elegir_cerveza))){
		$('cerveza').focus();
	}else
		$('perfil').submit();
}

function validarEncuesta(){
	tinyMCE.triggerSave();
	
	if (!(validarRadioButtonList("respuesta",copies.respuesta))){
		$('respuesta').focus();
	}else{
		$('encuesta').submit();
	}	
}

function validarRadioButtonList(groupname, message)
{
    var inputs = document.getElementsByTagName("input");
    var hasItems = false;

    for (var i = 0; i < inputs.length; i++)
    {
        if (inputs[i].type == 'radio')
        {
            var name = inputs[i].name;
            var isFromGroup = (name.lastIndexOf(groupname) + groupname.length ) == name.length;
           
            if (isFromGroup)
            {  
                hasItems = true;
                if (inputs[i].checked)
                {
                	return true;
                }
            }
        }
    }
   
    if (hasItems)
    {
        if (!message) message = 'Debes elegir una opción';
        mostrarAlerta(message);
        return false;
    }
    else
    {
     	return;
    }
}
function modificarPorcentaje(cantidad, n){
	$('porcentaje_'+n).setStyle('width',Math.floor(cantidad));
}
