// JavaScript Document
var imagen;
var logueado;
var chequeado=false;
var CERTIFICA_PV = 200942;
var CERTIFICA_VIDEO = 200943;
var CERTIFICA_USABILIDAD = 200944;

function marcaPV(path){
	path=path.replace(/[^a-z0-9_\-\/]/ig,"");
    cert_registerHit(CERTIFICA_PV, path);
}
function marcaVideo(){
	path=pathCertificaVideo;
	path=path.replace(/[^a-z0-9_\-\/]/ig,"");
    cert_registerPeriodicHit(CERTIFICA_VIDEO, path);
}
function marcaUsabilidad(path,href){
	path=path.replace(/[^a-z0-9_\-\/]/ig,"");
    cert_registerHitAndRedirect(href, CERTIFICA_USABILIDAD, path);
    if (path.indexOf("/Portada/Centro/Videos/")==-1 && path.indexOf("/Header/Consigna/")==-1) {
        return false;
    }
}
function marcaUsabilidadConsola(path){
    path=path.replace(/[^a-z0-9_\-\/]/ig,"");
    cert_registerHit(CERTIFICA_USABILIDAD, path);
}

function getHost(){
		return "http://" + document.domain;
	}
	
function mostrarListadoConvocatorias(){
	if(document.getElementById('listadoConvocatorias').style.display == 'block'){
		document.getElementById('listadoConvocatorias').style.display = 'none';
	}
	else{
		document.getElementById('listadoConvocatorias').style.display = 'block';
	}
}

function checkSoloNumero(obj){
	
	  var check = true;
	  var value = obj.value; //get characters
	  var tmpValue = '';
	  
	  //check that all characters are digits, ., -, or ""
	  for(var i=0;i < obj.value.length; ++i){
		  
		  if(i+1 > 8){
			   check = false;
			   break;
		  }
		  		  
		   var new_key = value.charAt(i); //cycle through characters

		   if(i==0 && new_key == 0){
				check = false;
				break;
		   }
		   
		   if(((new_key < "0") || (new_key > "9")) && !(new_key == "")){
				check = false;
				break;
		   }	
		   
		   tmpValue += new_key; 
	  }
	  
	  //apply appropriate colour based on value
	  if(!check){
		   obj.value = tmpValue;
	  }	  
}

function expand(nombre, left, right, top, bottom) {
		var bannerDiv = document.getElementById(nombre);
		bannerDiv.style.clip="rect("+top+"px, "+right+"px, "+bottom+"px, "+left+"px)";
}

function reducirTodo() {
		if ((typeof banners_ids)!="undefined") {
				var i;
				for (i=0;i<banners_ids.length;i++) {
							 expand(banners_ids[i], banners_chico_left[i], banners_chico_right[i], banners_chico_top[i], banners_chico_bottom[i]);
				}
		}
}


function validaFormularioRegistro(formu,checkPerfil){
	var error=new Array();
	
	$('errorFormulario').style.display = 'none';
	
	$('nick').value=$$('nick').trim();
	$('nickSpan').className = '';
	
	$('pass1').value=$$('pass1').trim();
	$('pass1Span').className = '';
	
	$('nombres').value=$$('nombres').trim();
	$('nombresSpan').className = '';
	
	$('apellidos').value=$$('apellidos').trim();
	$('apellidosSpan').className = '';
	
	$('rut').value=$$('rut').trim();	
	$('rutSpan').className = '';
	
	$('telefonoMovil').value=$$('telefonoMovil').trim();	
	$('telefonoMovilSpan').className = '';
	
	$('email').value = $$('email').trim();
	$('emailSpan').className = '';
	
	//Validar
	if ($('nick').value==''){
		$('nickSpan').className = 'alerta-error';
 		error.push("Ingrese un Nombre de usuario");
	}	
	
	if(checkPerfil){
		if ($('pass1').value==''){
			$('pass1Span').className = 'alerta-error';
			error.push("Ingrese su Constraseña");
		}	
	}
	if ($('nombres').value==''){
		$('nombresSpan').className = 'alerta-error';
 		error.push("Ingrese su Nombre");
	}
	if ($('apellidos').value==''){
		$('apellidosSpan').className = 'alerta-error';
 		error.push("Ingrese su Apellido");
	}
	if ($('telefonoMovil').value==''){
		$('telefonoMovilSpan').className = 'alerta-error';
 		error.push("Ingrese su Celular");
	}
	
	checkSoloNumero($('telefonoMovil'));
	
	if($('email').value==''){
		$('emailSpan').className = 'alerta-error';
		error.push("Ingrese su Email");
	}
	if(!$$('email').isEmail()){
		$('emailSpan').className = 'alerta-error';
		error.push('Escriba correo válido');
	}
	
	if($('rut').value != ''){
		/*if ($('rut').value==''){
			$('rutSpan').className = 'alerta-error';
			error.push("Ingrese su Rut");
		}else	     */
		if(!Rut($('rut').value)){
			$('rutSpan').className = 'alerta-error';
			error.push("Ingrese su Rut correcto");
		}
	}
	
	if ($('imagen').value!=''){
 		if(validaImagen($('imagen').value)){
			$('imagenSpan').className = 'alerta-error';
			error.push("Formato de Imagen no soportado");
		}
	}
	
	if (error.length){
		//alert(error.join("\n"));
		if(checkPerfil){
			marcaPV("/Registro/Formulario/Error/");
		}else{
			marcaPV("/Perfil/Formulario/Error/");
		}
		$('errorFormulario').style.display = 'block';
		
		return false;
	}
	else{
		return true;
	}
	
}



function validaFormulario(formu,validapass){
	var error=new Array();
	
	$('nick').value=$$('nick').trim();
	$('nombres').value=$$('nombres').trim();
	$('apellidos').value=$$('apellidos').trim();
	$('rut').value=$$('rut').trim();
	$('pass1').value=$$('pass1').trim();
	$('email').value=$$('email').trim();
	
	if ($('nick').value=='')
	{
 		error.push("Ingrese un Nombre de usuario");
	}
	if ($('nombres').value=='')
	{
 		error.push("Ingrese su Nombre");
	}
	if ($('apellidos').value=='')
	{
 		error.push("Ingrese su Apellido");
	}
	/*if ($('rut').value=='')
	{
 		error.push("Ingrese su Rut");
	}*/
	if(validapass){
		if ($('pass1').value=='')
		{
			error.push("Ingrese una Contraseña");
		}
	}
	if ($('imagen').value!='')
	{
 		if(validaImagen($('imagen').value)){
			error.push("Formato de Imagen no soportado");
		}
	}
	
	if ($('email').value=='')
	{
 		error.push("Ingrese un Correo");
	}
	if(!$('email').value.isEmail()){
		error.push("Ingrese un Correo válido");
	}
	if (error.length)
	{
		alert(error.join("\n"));
		return false;
	}
	else
	{
		return true;
	}
	
}

function validaPass(pass1,pass2){
	if(pass1 != pass2){
		alert('Contraseñas No Coinciden');
		return false;
	}
	else{
		return true;
	}
}
function GetFileExtension(Filename) {
	var I = Filename.lastIndexOf(".");
	return (I > -1) ? Filename.substring(I + 1, Filename.length).toLowerCase() : "";
}
function validaImagen(File1){
	if (File1 != "") {
		Ext = GetFileExtension(File1);
		if (Ext != "jpeg" && Ext != "jpg" && Ext != "png" && Ext != "gif"){ 
			//alert("El archivo no es una imagen válida"); 
			return true; 
		}
		else{			
			return false;
		}
	}
	else{
		return true;
	}
	 	
}
function validaVideo(File1){
	if (File1 != "") {
		
		Ext = GetFileExtension(File1);
		if (Ext != "avi" && Ext != "mpg" && Ext != "mpeg" && Ext != "flv" && Ext != "wmv"){ 
			return true; 
		}
		else{			
			return false;
		}
	}
	else{
		return true;
	}
	 	
}

function validarSubidaVideo(formu){
	var error=new Array();
	
	$('titulo').value=$$('titulo').trim();
	$('descripcion').value=$$('descripcion').trim();
	//$('video').value=$$('video').trim(); 
	
	if(!chequeado){
		error.push("Debe aceptar los terminos");
	}
	if ($('titulo').value=='')
	{
 		error.push("Ingrese un titulo");
	}
	if ($('descripcion').value=='')
	{
 		error.push("Ingrese una descripcion");
	}
	if ($('video').value=='')
	{
 		error.push("Ingrese un video");
	}
	
	if(validaVideo($('video').value)){
		error.push("Formato de video no soportado");
	}
	
	if (error.length)
	{
		alert(error.join("\n"));
		return false;
	}
	else
	{
		return true;
	}
}

function validaAudio(File1){
	if (File1 != "") {
		
		Ext = GetFileExtension(File1);
		if (Ext != "mp3" && Ext != "wav"){ 
			return true; 
		}
		else{			
			return false;
		}
	}
	else{
		return true;
	}
	 	
}

function validarSubidaAudio(formu){
	var error=new Array();
	
	$('titulo').value=$$('titulo').trim();
	$('descripcion').value=$$('descripcion').trim();
	
	if ($('titulo').value=='')
	{
 		error.push("Ingrese un titulo");
	}
	if ($('descripcion').value=='')
	{
 		error.push("Ingrese una descripcion");
	}
	if ($('audio').value=='')
	{
 		error.push("Ingrese un archivo de audio");
	}
	
	if(validaAudio($('audio').value)){
		error.push("Formato de audio no soportado");
	}
	
	if (error.length)
	{
		alert(error.join("\n"));
		return false;
	}
	else
	{
		return true;
	}
}




function validarSubidaFoto(formu){
	var error=new Array();
	
	$('titulo').value=$$('titulo').trim();
	$('descripcion').value=$$('descripcion').trim();
	
	if ($('titulo').value=='')
	{
 		error.push("Ingrese un titulo");
	}
	if ($('descripcion').value=='')
	{
 		error.push("Ingrese una descripcion");
	}
	if ($('foto').value=='')
	{
 		error.push("Ingrese un archivo de audio");
	}

	if(validaImagen($('foto').value)){
		error.push("Formato de Imagen no soportado");
	}
	
	if (error.length)
	{
		alert(error.join("\n"));
		return false;
	}
	else
	{
		return true;
	}
}


function abreVentana_origi(pagina,alto,ancho){
	if(logueado){
		var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width="+alto+", height="+ancho+", top=85, left=140";
		window.open(pagina,"",opciones);
	}
	else{
		alert('Debe estar logueado para realizar esa acción');
	}
}

function abreVentana(ruta){
	if(!logueado){
		alert('Debe estar logueado para realizar esa acción');
	}
	else{
		window.location.href=ruta;
	}
	return logueado;
}
function cambiaChequeado(){
	if(chequeado){
		chequeado=false;
	}else{
		chequeado=true;
	}
	
}

function FBShare(video){
    FB.Connect.showShareDialog("http://todosuno.canal13.cl/php/" + video, function() {});
}
function TwitterShare(video,texto) {
    var param=texto + " http://todosuno.canal13.cl/php/" + video;
    window.open("http://twitter.com/home?status=" + encodeURIComponent(param),"_blank");
}

function reload(){
    window.location.reload();
}

function logout() {
	var imgCMD=document.getElementById("imgCMD");
	imgCMD.src="logout.php";
		
	if(FB.Connect.get_status().result==FB.ConnectState.connected) {
		FB.Connect.logout();
	}else {
		window.setTimeout(reload,500);
	}
}

