// JavaScript Document

function popup(id_var)
{
	var ventana;
	ventana=window.open('./ver_resultados.asp?Id_Encuesta='+id_var+'','ventana','status=yes,resizable=no,width=450,height=350'); 
}
						
function CambiaPag(num,pagina)
{
	if(num==1)
	{
		document.frm_principal.action="./default.asp?menu=Polls";
	}
	if(num==2)
	{
		document.frm_principal.action="./default.asp?menu=News";
	}
	if(num==3)
	{
		document.frm_principal.action="./default.asp?menu=Tips";
	}
	if(num==4)
	{
		document.frm_principal.action="./default.asp?menu=Newsletter Versions";
	}
	if(num==5)
	{
		document.frm_principal.action="./default.asp?menu=Newsletter Sections";
	}
	if(num==6)
	{
		document.frm_principal.action="./default.asp?menu=Newsletter";
	}
	document.frm_principal.pag.value=pagina;
	document.frm_principal.submit();
}
function Act(Num,Tipo)
{
	if(Tipo==1)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.Fecha_Noticia.disabled=false;
		document.frm_principal.submit();
	}
	if(Tipo==2)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.Fecha_Tip.disabled=false;
		document.frm_principal.submit();
	}
	if(Tipo==3)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.Fecha_Inicio_Encuesta.disabled=false;
		document.frm_principal.Fecha_Fin_Encuesta.disabled=false;
		document.frm_principal.submit();
	}
	if(Tipo==4)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.submit();
	}
}
function Order_By(opc,menu,pag)
{
	if(document.frm_principal.SortBy.value==opc)
	{
		if(document.frm_principal.OrderBy.value=='ASC')
		{
			document.frm_principal.SortBy.value=opc;
			document.frm_principal.OrderBy.value='DESC';
			document.frm_principal.action="default.asp?menu="+menu+"&pag="+pag;
			document.frm_principal.submit();
		}
		else
		{
			document.frm_principal.SortBy.value=opc;
			document.frm_principal.OrderBy.value='ASC';
			document.frm_principal.action="default.asp?menu="+menu+"&pag="+pag;
			document.frm_principal.submit();
		}
	}
	else
	{
		document.frm_principal.SortBy.value=opc;
		document.frm_principal.OrderBy.value='DESC'
		document.frm_principal.action="default.asp?menu="+menu+"&pag="+pag;
		document.frm_principal.submit();	
	}
}
//AGREGA CAMPOS DINAMICOS
function AgregarCampo(cont,contenido,pathimagen,rd_nomb,txt_nomb)
{
	//cont = Conteo de campos agregados
	//contenido = Variable que corresponde al id de la tabla donde donde se imprime el contenido
	//pathimagen = Ruta de la imagen.
	//nombre = Corresponde al nombre que se le asigna a los campos tipo RADIO BUTTON 
	var cont = document.getElementById(cont);
	cont.setAttribute("value", parseInt(cont.value,0)+1);
	var tabla = document.getElementById(contenido).tBodies[0];
	var fila = document.createElement("TR");
	
	//AGREGA LA PRIMERA CELDA CON EL NUMERO DE LA OPCION
	
	var celda1 = document.createElement("TD");
	celda1.setAttribute("width","23%");
	celda1.setAttribute("align","right");
	codigo = document.createTextNode(" ");
	celda1.appendChild(codigo);
	
	//AGREGA LA SEGUNDA CELDA CON EL CAMPO DE INGRESO DE LA POSIBLE RESPUESTA
	var celda2 = document.createElement("TD");
	celda2.setAttribute("width","77%");
	var codigo = document.createElement("INPUT");
	codigo.setAttribute("type","text");
	codigo.setAttribute("size","55");
	codigo.setAttribute("maxlength","150");
	//codigo.setAttribute("name","codigo[]" + cont.value);
	codigo.setAttribute("name",txt_nomb+"("+cont.value+")");
	celda2.appendChild(codigo);
	espacio = document.createTextNode("  ");
	celda2.appendChild(espacio);
	//AGREGA EL BOTON QUE VA A ELIMINAR LA OPCION
	var imagen = document.createElement('IMG');
	imagen.src=pathimagen;
	//boton.setAttribute('type','button');
	//boton.setAttribute('value','borrar');
	imagen.onclick=function(){BorrarFila(this,contenido);}
	celda2.appendChild(imagen);

	fila.appendChild(celda1);
	fila.appendChild(celda2);

	tabla.appendChild(fila);
}

//BORRAR CAMPOS DINAMICOS
function BorrarFila(button,contenido)
{
	var fila = button.parentNode.parentNode;
	var tabla = document.getElementById(contenido).getElementsByTagName('tbody')[0];
	tabla.removeChild(fila);
}

function Validar_Tip(Num)
{
	var valor=0;
	titulo=allTrim(document.frm_principal.Titulo_Tip.value);
	if(titulo==0)
	{
		MostrarElemento('error_titulo');
		valor++;
	}
	else
	{
		OcultarElemento('error_titulo');
	}
	fecha=allTrim(document.frm_principal.Fecha_Tip.value);
	if(fecha==0)
	{
		MostrarElemento('error_fecha');
		valor++;
	}
	else
	{
		OcultarElemento('error_fecha');
	}
	contenido=allTrim(document.frm_principal.Contenido_Tip.value);
	if(contenido==0)
	{
		MostrarElemento('error_contenido');
		valor++;
	}
	else
	{
		OcultarElemento('error_contenido');
	}
	if(valor==0)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.Fecha_Tip.disabled=false;
		document.frm_principal.submit();
	}
}

function Validar_Noticia(Num)
{
	var valor=0;
	titulo=allTrim(document.frm_principal.Titulo_Noticia.value);
	if(titulo==0)
	{
		MostrarElemento('error_titulo');
		valor++;
	}
	else
	{
		OcultarElemento('error_titulo');
	}
	fecha=allTrim(document.frm_principal.Fecha_Noticia.value);
	if(fecha==0)
	{
		MostrarElemento('error_fecha');
		valor++;
	}
	else
	{
		OcultarElemento('error_fecha');
	}
	contenido=allTrim(document.frm_principal.Contenido_Noticia.value);
	if(contenido==0)
	{
		MostrarElemento('error_contenido');
		valor++;
	}
	else
	{
		OcultarElemento('error_contenido');
	}
	if(valor==0)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.Fecha_Noticia.disabled=false;
		document.frm_principal.submit();
	}
}

function Validar_Encuesta(Num)
{
	var valor=0;
	titulo=allTrim(document.frm_principal.Titulo_Encuesta.value);
	if(titulo==0)
	{
		MostrarElemento('error_titulo');
		valor++;
	}
	else
	{
		OcultarElemento('error_titulo');
		OcultarElemento('error_op');
	}
	fecha1=allTrim(document.frm_principal.Fecha_Inicio_Encuesta.value);
	if(fecha1==0)
	{
		MostrarElemento('error_fecha1');
		valor++;
	}
	else
	{
		OcultarElemento('error_fecha1');
		OcultarElemento('error_op');
	}
	fecha2=allTrim(document.frm_principal.Fecha_Fin_Encuesta.value);
	if(fecha2==0)
	{
		MostrarElemento('error_fecha2');
		valor++;
	}
	else
	{
		OcultarElemento('error_fecha2');
		OcultarElemento('error_op');
	}
	auxfec1=Date.parse(fecha1);
	auxfec2=Date.parse(fecha2);
	if (auxfec1>auxfec2)
	{
		MostrarElemento('error_fecha3');
		valor++;
	}
	else
	{
		OcultarElemento('error_fecha3');
		OcultarElemento('error_op');
	}
	if(valor==0)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.Fecha_Inicio_Encuesta.disabled=false;
		document.frm_principal.Fecha_Fin_Encuesta.disabled=false;
		document.frm_principal.submit();
	}
}

function Validar_Contacto()
{
	var valor=0;
	nombre=allTrim(document.frm_principal.nombre_contacto.value);
	if(nombre==0)
	{
		MostrarElemento('error_nombre');
		valor++;
	}
	else
	{
		OcultarElemento('error_nombre');
	}
	email=validar_email(document.frm_principal.email_contacto.value);
	if(email==0)
	{
		MostrarElemento('error_mail');
		valor++;
	}
	else
	{
		OcultarElemento('error_mail');
	}
	telefono=allTrim(document.frm_principal.telefono_contacto.value);
	if(telefono==0)
	{
		MostrarElemento('error_telefono');
		valor++;
	}
	else
	{
		OcultarElemento('error_telefono');
	}
	pais=allTrim(document.frm_principal.pais_contacto.value);
	if(pais==0)
	{
		MostrarElemento('error_pais');
		valor++;
	}
	else
	{
		OcultarElemento('error_pais');
	}
	comentarios=allTrim(document.frm_principal.comentarios_contacto.value);
	if(comentarios==0)
	{
		MostrarElemento('error_comentarios');
		valor++;
	}
	else
	{
		OcultarElemento('error_comentarios');
	}
	if(valor==0)
	{
		document.frm_principal.submit();
	}
}

function Validar_Version(Num)
{
	var valor=0;
	mes=allTrim(document.frm_principal.Mes_Version.value);
	if(mes==0)
	{
		MostrarElemento('error_mes');
		valor++;
	}
	else
	{
		OcultarElemento('error_mes');
	}
	ano=allTrim(document.frm_principal.Ano_Version.value);
	if(ano==0)
	{
		MostrarElemento('error_ano');
		valor++;
	}
	else
	{
		OcultarElemento('error_ano');
	}
	numero=allTrim(document.frm_principal.Numero_Version.value);
	if(numero==0)
	{
		MostrarElemento('error_numero');
		valor++;
	}
	else
	{
		OcultarElemento('error_numero');
	}
	if(valor==0)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.submit();
	}
}

function Validar_Seccion(Num)
{
	var valor=0;
	nombre=allTrim(document.frm_principal.Nombre_Seccion.value);
	if(nombre==0)
	{
		MostrarElemento('error_nombre');
		valor++;
	}
	else
	{
		OcultarElemento('error_nombre');
	}
	if(valor==0)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.submit();
	}
}

function Validar_Boletin(Num)
{
	var valor=0;
	contenido=allTrim(document.frm_principal.Contenido_Boletin.value);
	if(contenido==0)
	{
		MostrarElemento('error_contenido');
		valor++;
	}
	else
	{
		OcultarElemento('error_contenido');
	}
	if(valor==0)
	{
		document.frm_principal.Accion.value=Num;
		document.frm_principal.submit();
	}
}

function Validar_Img()
{
	var valor=0;
	img=allTrim(document.subir_imagen.Imagen.value);
	if(img==0)
	{
		MostrarElemento('error_imagen');
		valor++;
	}
	else
	{
		OcultarElemento('error_imagen');
	}
	if(valor==0)
	{
		document.subir_imagen.submit();
	}
}

function Validar_CorreoHome()
{
	var valor=0;
	email=validar_email(document.frm_principal.correo.value);
	if(email==0)
	{
		MostrarElemento('error_correo');
		valor++;
	}
	else
	{
		OcultarElemento('error_correo');
	}
	if(valor==0)
	{
		document.frm_principal.submit();
	}
}

function MostrarElemento(id) 
{
	var elemento = document.getElementById(id);
	if(navigator.appName.indexOf("Microsoft") > -1)
	{
		var visible = 'block'
	}
	else
	{
		var visible = 'table-row';
	}
	elemento.style.display = visible;
}

function OcultarElemento(id) 
{
	var elemento = document.getElementById(id);
	elemento.style.display = 'none';
}

function validar_email(cadena){//FUNCION PARA VALIDAR UN CORREO
	var email;
	var ind1, ind2, ind3;
	var plant 
	plant = /(^[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,3})$/
	ind1 = cadena.indexOf('@');//SE BUSCA EL PRIMER ARROBA DE IZQUIERDA A DERECHA
	ind2 = cadena.indexOf('.');//SE BUSCA EL PRIMER PUNTO DE IZQUIERDA A DERECHA
	ind3 = cadena.lastIndexOf('@');
	ind4 = cadena.lastIndexOf('.');
	ind5 = cadena.length;
	if(plant.test(cadena)){
		email=1;
		return email;
	}else{
		email=0;
		return email;
	}
}

function lTrim(sStr){ //FUNCION PARA QUITAR LOS ESPACIOS EN BLANCO DE LA IZQUIERDA
	while (sStr.charAt(0) == " ") 
	sStr = sStr.substr(1, sStr.length - 1); 
	return sStr; 
	} 
																		
function rTrim(sStr){  //FUNCION PARA QUITAR LOS ESPACIOS EN BLANCO DE LA DERECHA
	while (sStr.charAt(sStr.length - 1) == " ") 
	sStr = sStr.substr(0, sStr.length - 1); 
	return sStr; 
} 

function allTrim(sStr){  //FUNCION PARA QUITAR LOS ESPACIOS EN BLANCO DE LA IZQUIERDA Y DERECHA
	 return rTrim(lTrim(sStr)); 
} 
function Logeo(){
	var tecla = window.event.keyCode; 
	if(tecla==39 || tecla == 47 || tecla == 45 || tecla == 42 || tecla == 43) 
	{
		window.event.keyCode=0;
	}
}