// Funcions Pragma Backoffice
// Isaac Roca - iroca at pragmapublicitat dot cat

var FnLoad=new Array();

carregant='<center><img style="float:none; margin-top:200px;" src="'+wwwroot+'/img/carregant_'+canal+'.gif"><br />'+carregant+'</center>';

var showCarregant = function() {
		jQuery('#contingut').html(carregant); 
		jQuery('#contingut').show();
	}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}
	

var redirect = function(o) {
	//alert('RESPONSETEXT: '+o);
	arr=o.split("|");
	if (arr[1]==undefined) { 
		alert('En construcción'); 
	} else {
		if (arr[1]!='') { alert(arr[1]); } 
	} 
	if(parseInt(arr[0])) {
		var str=""; for (i=2;i<arr.length;i++) str+=arr[i]+"|";
		jQuery('#contingut').html(str.substr(0,str.length-1));
		ajaxiza();
		jQuery("#lateral").css({ display:'block' });
		jsload(sec);
	}
}

var redirectMembers = function() {
	//alert('RESPONSETEXT: '+o);
	//document.location='/'+canal;
	//window.location.reload();
	document.location='/members/';
}

var redirectLogin = function(o) {
	arr=o.split("|");
	if (arr[1]==undefined) { 
		alert('En construcción'); 
	} else {
		if (arr[1]!='' && parseInt(arr[0])!=2) { alert(arr[1]); } 
	} 
	if(parseInt(arr[0])==1) {
		var str=""; for (i=2;i<arr.length;i++) str+=arr[i]+"|";
		jQuery('#contingut').html(str.substr(0,str.length-1));
		ajaxiza();
		jQuery("#lateral").css({ display:'block' });
		jsload(sec);
	} else if (parseInt(arr[0])==2) {
		document.location=arr[1];
	}
}

/*function hide_fileds_renewal() {
	if (jQuery(".renewal").css("display")=="none") {
		jQuery(".renewal").slideDown("fast");
	} else {
		jQuery(".renewal").slideUp("fast");
	}
}

function hide_fileds_renewal2() {
		jQuery(".renewal").slideUp("fast");
}
function hide_fileds_renewal() {
		jQuery(".renewal").slideDown("fast");
}*/

function hide_fileds_renewal2() {
		jQuery(".renewal").hide();
}
function hide_fileds_renewal() {
		jQuery(".renewal").show();
}

/* funcions admin */

function editar(ident) {
	//alert(ident);
	sec=$('sec').value;
	showCarregant();
	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&id='+ident, success: redirect });
}

function llistar(nom,camp,ordre,pagina) {
	sec=$('sec').value;
	jQuery('#'+nom).html("Carregant...");
	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'accio=llistar&canal='+canal+'&sec='+sec+'&camp='+camp+'&ordre='+ordre+'&pagina='+pagina, success: function(o) {
		jQuery('#'+nom).html(o);
	} });

}

function llistaUsuaris() {
	var v=valorsLlistaUsuari();
	showCarregant();
	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&pagina='+v.pagina+'&id=-1', success: redirect });
}

function valorsLlistaUsuari() {
		return { 
			pagina: $F('pagina')
		};	
}

function logout() {
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=logout&canal='+canal, success: redirectLogin });
		return false;
	}
	
function login() {
		var str = $('formulari').serialize();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=login&canal='+canal, success: redirectLogin });
		return false;
	}

function loginMembers(section,formu) {
		var str = $(formu).serialize();
		showCarregant();
		if (section!='members') {
			jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=login&canal='+canal, success: redirectLogin });
		} else {
			jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=login&canal='+canal, success: redirectMembers });
		}
		return false;
	}

function guardar() {
		var str = $('formulari').serialize();
		// alert(str);
		showCarregant();

		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=guarda&canal='+canal, success: redirect });
		return false;
	}

function esborrar(mensaje) {
		if (mensaje) if (!confirm(mensaje)) return 0;
		var str = $('formulari').serialize();
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: str+'&accio=esborra&canal='+canal, success: redirect });
	}
	
function valorsLlistaUsuari() {
		var v = { 
			mostrar: $F('mostrar'), 
			ordenarper: $F('ordenarper'), 
			ordre: $F('ordre'), 
			pagina: $F('pagina') 
		};
	
	return v;	
}
	
function llistaUsuaris() {
		var v=valorsLlistaUsuari();
		
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&mostrar='+v.mostrar+'&ordenarper='+v.ordenarper+'&ordre='+v.ordre+'&pagina='+v.pagina+'&id=-1', success: redirect });
}

function checkPass() {
		($F('password1').length>3)? jQuery('#password1').css("background-color","#74FF60") : jQuery('#password1').css("background-color","#FF7070");
		(($F('password1').length>3)&&($F('password1')==$F('password2')))? jQuery('#password2').css("background-color","#74FF60") : jQuery('#password2').css("background-color","#FF7070");
}

function editPass() { 
	jQuery('#chpass').html('<p>'+noupassword+': <br /><input onkeyup="checkPass();" name="password1" id="password1" style="width:200px;" type="password" value=""  /></p><p>'+repeticio+': <br /><input onkeyup="checkPass();" name="password2" id="password2" style="width:200px;" type="password" value=""  /></p>');
	checkPass();
}

function editarUsuari(ident) {
		var v=valorsLlistaUsuari();
		
		showCarregant();
		jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', data: 'canal='+canal+'&sec='+sec+'&mostrar='+v.mostrar+'&ordenarper='+v.ordenarper+'&ordre='+v.ordre+'&id='+ident, success: redirect });
}
	
	
function obreMenu(codi) {
	/*jQuery("ul.menuRoot li ul").each(function (){
		if (this.id!=codi) jQuery(this).hide("fast");
	});
	jQuery("#"+codi).each(function () {
			if (jQuery(this).css("display")=="none") {
				jQuery(this).show("fast");
			} else {
				jQuery(this).hide("fast");
			}
	});*/

	jQuery("ul.menuRoot li ul").each(function (){
		
		if (this.id!=codi) {
			if (jQuery(this).css("display")=="block") {
				 jQuery(this).css({ display:"none" });
			}
		} else {
			if (jQuery(this).css("display")=="none") {
				jQuery(this).css({	opacity: 0});
				jQuery(this).animate({ opacity: 1 },{ speed: 6000, complete: function() { jQuery(this).css({"display":"block"}); } });
			}
		}
	});

}

function tancaMenu() {

}


function jsload(sec) {
	
	for (i=0;i<FnLoad.length;i++) {
		if (FnLoad[i].sec==sec) {
			FnLoad[i].fn();
			break;
		}	
	}
}

function obreSeccio(sec) {
	showCarregant();
	//alert(wwwroot+'/ajax');

	if ("https:" == document.location.protocol) {
		document.location=wwwroot+'/'+sec;
	}

	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', dataType:'html', data: 'canal='+canal+'&sec='+sec, success: redirect });
}

	
function getAjax(obj) {
	sec=obj.getAttribute("rel");
	showCarregant();
	//alert(wwwroot+'/ajax');

	if ("https:" == document.location.protocol) {
		document.location=wwwroot+'/'+sec;
	}


	jQuery.ajax({ type: "POST", url: wwwroot+'/ajax', dataType:'html', data: 'canal='+canal+'&sec='+sec, success: redirect });
	return false;
}

function ajaxiza() {
	jQuery("a[rel]").each(function () {
		if (this.rel!="lightbox[congress]") {
			this.href='#';
			if(this.rel!="#") { 
				this.onclick=function () { getAjax(this); return false; } 
			}
		}
	});
}
/* si els links tenen rel, es converteixen en ajax cridar secció */
jQuery(document).ready(function() {

	jsload(sec);
	ajaxiza();
	
});


function init() {

}



