String.prototype.trim = function(){
	return	this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');	
}

String.prototype.isInteger = function () {
	return /^\d+$/.test (this);
}

String.prototype.isFloat = function () {
	return /^\d+\.?\d*$/.test (this);
}

Number.prototype.isInteger = function () {
	return /^\d+$/.test (this);
}

String.prototype.isValidDate = function () {
	return /^(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2})$/.test(this);
}

/**
* FCKEditor update functions - call before ajax submit on fckeditor enabled forms
**/

function getEditorValue( instanceName )
{
    // Get the editor instance that we want to interact with.
    var oEditor = FCKeditorAPI.GetInstance( instanceName ) ;

	// Get the editor contents as XHTML.
    return oEditor.GetXHTML( true ) ; // "true" means you want it formatted.
}

function setEditorValue( instanceName, text )
{
    // Get the editor instance that we want to interact with.
    var oEditor = FCKeditorAPI.GetInstance( instanceName ) ;

    // Set the editor contents.
    oEditor.SetHTML( text ) ;
}

/**
* Error output
**/

function writeError(errorText,elementId){

	outputText = "<div class=\"alert alignCenter\">"
				+"<div class=\"alert_icon\">"
				+"&nbsp;"
				+"</div>"
				+"<div class=\"alert_msg font_red strong size14 \">"
				+ errorText
				+ "</div>"
				+ "</div>";

	document.getElementById(elementId).innerHTML = outputText;

}

/**
* Success output
**/

function writeSuccess(successText,elementId){

	outputText = "<div class=\"sucess_alert alignCenter\">"
				+"<div class=\"sucess_icon\">"
				+"&nbsp;"
				+"</div>"
				+"<div class=\"sucess_msg font_red strong size14 \">"
				+ successText
				+ "</div>"
				+ "</div>";

	document.getElementById(elementId).innerHTML = outputText;

}

/**
* Waiting div
**/

function waiting(){

	var html = "<div class=\"caixa caixaGrande topMeio\">"
				+"<div class=\"caixaGrande_dentro\">"
				+"<div style=\"margin:auto;text-align:center\" class=\"size11 fontall brancoColor\">"
				+"<img src=\"/app/images/ajax-loader.gif\" border=\"0\"/><br/><br/>"
				+"A carregar... !"
				+"</div>"
				+"</div>"
				+"<div class=\"caixaGrande_bottomM bottomMeio\"></div>"
				+"</div>";
				
	document.getElementById("mainArea").innerHTML=html;

}

/**
* returns voting module
*/

function refreshMiddle(item_id){

/*	if(!item_id.isInteger())
		return false;*/
		
	new Ajax.Updater("mainBottom","app/actions/doGet.php",{
		parameters: {
			section: "videos_vote",
			item_id: item_id
		}
	});

}

/**
* Login and logout in site
**/

function doLoginInSite(params){
	
	username = document.getElementById("usernameIn").value;
	password = document.getElementById("passwordIn").value;
	
	if(username.trim() == ''){
	
		writeError("Por favor insira o nome de utilizador");
		document.getElementById("usernameIn").focus();
		return false;
		
	}
	
	if(password.trim() == ''){
	
		writeError("Por favor insira uma password");
		document.getElementById("passwordIn").focus();
		return false;
		
	}
	
	new Ajax.Updater('divLogin','app/actions/doLogin.php',{
		parameters: { 	
			username: username,
			password: password
		},
		onComplete: function(transport){
			if(!document.getElementById("username"))
				refreshMiddle(params);
		}
	});
		
}

/**
* Login and logout
**/

function doLogin(){

	username = document.getElementById("username").value;
	password = document.getElementById("password").value;
	
	if(username.trim() == ''){
	
		writeError("Por favor insira o nome de utilizador");
		document.form_login.username.focus();
		return false;
		
	}
	
	if(password.trim() == ''){
	
		writeError("Por favor insira uma password");
		document.form_login.username.focus();
		return false;
		
	}
	
	//password = hex_md5(password);
	
	new Ajax.Updater('divLogin','app/actions/doLogin.php',{
		parameters: { 	
			username: username,
			password: password
		}
	});
	
}

function doLogout(){
	
	new Ajax.Updater('divLogin','app/actions/doLogout.php',{
		evalScripts: true					 
	});
	
}

/**
* Getters and Setters
**/

function getAgendaDetail(ident){

	if(!ident.isInteger()){
		writeError("Identificador de Evento n&atilde;o v&eacute;lido.");
		return false;	
	}
	
	new Ajax.Updater('mainArea','app/actions/agenda/agendaDevolve.php',{
		evalScripts: true,
		parameters: { 	
			id: ident
		}
	});
}

function getNewsDetail(ident){

	if(!ident.isInteger()){
		writeError("Identificador de Noticia n&atilde;o v&aacute;lido.");
		return false;	
	}
	
	new Ajax.Updater('mainArea','app/actions/noticias/noticiaDevolve.php',{
		evalScripts: true,
		parameters: { 	
			id: ident
		}
	});
}

function getNovdDetail(ident){

	if(!ident.isInteger()){
		writeError("Identificador de Novidade não válido.");
		return false;	
	}
	
	new Ajax.Updater('mainArea','app/actions/novidades/novidadeDevolve.php',{
		evalScripts: true,
		parameters: { 	
			id: ident
		}
	});
}


/* ################################################################################################## */
//Funções de Backend

function doUpload(fotoId,formName){

	document.getElementById(formName).action = "/app/actions/doUpload.php?foto_id="+fotoId;
	document.getElementById(formName).target = "iFrame"+fotoId;
	document.getElementById(formName).submit();
	document.getElementById("divUpload"+fotoId).style.display="none";
	document.getElementById("loading"+fotoId).style.display="";

}

function showFoto(fotoId, is_error, feedback, foto_link){

	document.getElementById("loading"+fotoId).style.display = "none";
	document.getElementById("divUpload"+fotoId).style.display = "";
	if(is_error)
		writeError(feedback,"feedbackUpload"+fotoId);
	else{
		writeSuccess(feedback,"feedbackUpload"+fotoId);
		document.getElementById("hid"+fotoId).value = foto_link;
	}

}

function insereAgenda(is_new){
	
	id_agenda = (document.getElementById("id_agenda"))?document.getElementById("id_agenda").value:-1;
	data = document.getElementById("data").value;
	titulo = document.getElementById("titulo").value;
	localidade = document.getElementById("localidade").value;
	espaco = document.getElementById("espaco").value;
	url = document.getElementById("url").value;
	tipo = document.getElementById("tipo").value;
	categoria = document.getElementById("categoria").value;
	preco = document.getElementById("preco").value;
	artistas = new Array();
	current_index=0;
	for(i=0;i<document.getElementById("artistas[]").options.length;i++){
		if(document.getElementById("artistas[]").options[i].selected){
			artistas[current_index] = document.getElementById("artistas[]").options[i].value;
			current_index++;
		}
	}
	artistas_param = Object.toJSON(artistas);
	foto = document.getElementById("hidfoto").value;

	// Retrieve editor value
	document.getElementById("descricao").value = getEditorValue("descricao");

	descricao = document.getElementById("descricao").value;
	is_destaque = document.getElementById("is_destaque").value;
	is_suprasumo = document.getElementById("is_suprasumo").value;
	

	if(!data.isValidDate()){
		writeError("Por favor insira uma data","divError");
		document.form_agenda.data.focus();
		return false;	
	}
	
	if(titulo == ''){
		writeError("Por favor insira o t&iacute;tulo","divError");
		document.form_agenda.titulo.focus();
		return false;
	}

	if(localidade == ''){
		writeError("Por favor insira a localidade","divError");
		document.form_agenda.localidade.focus();
		return false;
	}

	if(tipo == ''){
		writeError("Por favor escolha o tipo","divError");
		document.form_agenda.tipo.focus();
		return false;
	}

	if(categoria == ''){
		writeError("Por favor escolha a categoria","divError");
		document.form_agenda.categoria.focus();
		return false;
	}

		
	if(!preco.isFloat() && preco != ''){
		writeError("O pre&ccedil;o deve ser um n&uacute;mero inteiro, sem s&iacute;mbolos","divError");
		document.form_agenda.preco.focus();
		return false;
	}

	if(foto == '' && is_new){
		writeError("Por favor escolha a foto","divError");
		document.form_agenda.foto.focus();
		return false;
	}

	if(descricao == ''){
		writeError("Por favor introduza a descri&ccedil;&atilde;o","divError");
		document.form_agenda.descricao.focus();
		return false;
	}

	if(is_destaque == ''){
		writeError("Por favor indique se &eacute; um evento em destaque","divError");
		document.form_agenda.is_destaque.focus();
		return false;
	}

	if(is_suprasumo == ''){
		writeError("Por favor indique se &eacute; um evento Principal","divError");
		document.form_agenda.is_suprasumo.focus();
		return false;
	}
	
	waiting();

	new Ajax.Updater('mainArea','app/actions/agenda/insere.php',{
		parameters: {
			id_agenda: id_agenda,
			data: data,
			titulo: titulo,
			localidade: localidade,
			espaco: espaco,
			url: url,
			tipo: tipo,
			categoria: categoria,
			preco: preco,
			artistas: artistas_param,
			foto: foto,
			descricao: descricao,
			is_destaque: is_destaque,
			is_suprasumo: is_suprasumo,
			is_new: is_new
		}
	});
	
}

function insereNovidade(is_new){
	
	id_novidade = (document.getElementById("id_novidade"))?document.getElementById("id_novidade").value:-1;
	data = document.getElementById("data").value;
	titulo = document.getElementById("titulo").value;
	artistas = new Array();
	current_index=0;
	for(i=0;i<document.getElementById("artistas[]").options.length;i++){
		if(document.getElementById("artistas[]").options[i].selected){
			artistas[current_index] = document.getElementById("artistas[]").options[i].value;
			current_index++;
		}
	}
	artistas_param = Object.toJSON(artistas);
	foto = document.getElementById("hidfoto").value;

	// Retrieve editor value
	document.getElementById("descricao").value = getEditorValue("descricao");

	descricao = document.getElementById("descricao").value;

	if(!data.isValidDate()){
		writeError("Por favor insira uma data","divError");
		document.form_agenda.data.focus();
		return false;	
	}
	
	if(titulo == ''){
		writeError("Por favor insira o t&iacute;tulo","divError");
		document.form_agenda.titulo.focus();
		return false;
	}

	if(foto == '' && is_new){
		writeError("Por favor escolha a foto","divError");
		document.form_agenda.foto.focus();
		return false;
	}

	if(descricao == ''){
		writeError("Por favor introduza a descri&ccedil;&atilde;o","divError");
		document.form_agenda.descricao.focus();
		return false;
	}
	waiting();
	new Ajax.Updater('mainArea','app/actions/novidades/insere.php',{
		parameters: {
			id_novidade: id_novidade,
			data: data,
			titulo: titulo,
			artistas: artistas_param,
			foto: foto,
			descricao: descricao,
			is_new: is_new
		}
	});
	
}

function insereNoticia(is_new){
	
	id_noticia = (document.getElementById("id_noticia"))?document.getElementById("id_noticia").value:-1;
	data = document.getElementById("data").value;
	titulo = document.getElementById("titulo").value;
	artistas = new Array();
	current_index=0;
	for(i=0;i<document.getElementById("artistas[]").options.length;i++){
		if(document.getElementById("artistas[]").options[i].selected){
			artistas[current_index] = document.getElementById("artistas[]").options[i].value;
			current_index++;
		}
	}
	artistas_param = Object.toJSON(artistas);

	foto = document.getElementById("hidfoto").value;

	// Retrieve editor value
	document.getElementById("descricao").value = getEditorValue("descricao");

	descricao = document.getElementById("descricao").value;

	if(!data.isValidDate()){
		writeError("Por favor insira uma data","divError");
		document.form_agenda.data.focus();
		return false;	
	}
	
	if(titulo == ''){
		writeError("Por favor insira o t&iacute;tulo","divError");
		document.form_agenda.titulo.focus();
		return false;
	}

	if(foto == '' && is_new){
		writeError("Por favor escolha a foto","divError");
		document.form_agenda.foto.focus();
		return false;
	}

	if(descricao == ''){
		writeError("Por favor introduza a descri&ccedil;&atilde;o","divError");
		document.form_agenda.descricao.focus();
		return false;
	}
	waiting();
	new Ajax.Updater('mainArea','app/actions/noticias/insere.php',{
		parameters: {
			id_noticia: id_noticia,
			data: data,
			titulo: titulo,
			artistas: artistas_param,
			foto: foto,
			descricao: descricao,
			is_new: is_new
		}
	});
	
}


function insereLink(is_new){
	
	id_link = (document.getElementById("id_link"))?document.getElementById("id_link").value:-1;
	titulo = document.getElementById("titulo").value;
	tipo = document.getElementById("tipo").value;
	url = document.getElementById("url").value;
	foto = document.getElementById("hidfoto").value;

	// Retrieve editor value
	document.getElementById("descricao").value = getEditorValue("descricao");

	descricao = document.getElementById("descricao").value;

	if(titulo == ''){
		writeError("Por favor insira o t&iacute;tulo","divError");
		document.form_links.titulo.focus();
		return false;
	}

	if(tipo == ''){
		writeError("Por favor insira o tipo","divError");
		document.form_links.tipo.focus();
		return false;
	}

	if(url == ''){
		writeError("Por favor insira o URL","divError");
		document.form_links.url.focus();
		return false;
	}

	if(foto == '' && is_new){
		writeError("Por favor escolha a foto","divError");
		document.form_links.foto.focus();
		return false;
	}

	if(descricao == ''){
		writeError("Por favor introduza a descri&ccedil;&atilde;o","divError");
		document.form_links.descricao.focus();
		return false;
	}
	waiting();
	new Ajax.Updater('mainArea','app/actions/links/insere.php',{
		parameters: {
			id_link: id_link,
			titulo: titulo,
			tipo: tipo,
			url: url,
			foto: foto,
			descricao: descricao,
			is_new: is_new
		}
	});
	
}



function insereDestaque(is_new){
	
	id_destaque = (document.getElementById("id_destaque"))?document.getElementById("id_destaque").value:-1;
	data = document.getElementById("data").value;
	titulo = document.getElementById("titulo").value;
	artistas = new Array();
	current_index=0;
	for(i=0;i<document.getElementById("artistas[]").options.length;i++){
		if(document.getElementById("artistas[]").options[i].selected){
			artistas[current_index] = document.getElementById("artistas[]").options[i].value;
			current_index++;
		}
	}
	artistas_param = Object.toJSON(artistas);	
	foto = document.getElementById("hidfoto").value;
	// Retrieve editor value
	document.getElementById("descricao").value = getEditorValue("descricao");
	descricao = document.getElementById("descricao").value;
	
	is_principal = document.getElementById("is_principal").value;

	if(!data.isValidDate()){
		writeError("Por favor insira uma data","divError");
		document.form_agenda.data.focus();
		return false;	
	}
	
	if(titulo == ''){
		writeError("Por favor insira o t&iacute;tulo","divError");
		document.form_agenda.titulo.focus();
		return false;
	}

	if(foto == '' && is_new){
		writeError("Por favor escolha a foto","divError");
		document.form_agenda.foto.focus();
		return false;
	}

	if(descricao == ''){
		writeError("Por favor introduza a descri&ccedil;&atilde;o","divError");
		document.form_agenda.descricao.focus();
		return false;
	}
	
	if(is_principal == ''){
		writeError("Por favor indique se &eacute; um evento Principal","divError");
		document.form_agenda.is_principal.focus();
		return false;
	}	
	waiting();
	new Ajax.Updater('mainArea','app/actions/destaques/insere.php',{
		parameters: {
			id_destaque: id_destaque,
			data: data,
			titulo: titulo,
			artistas: artistas_param,
			foto: foto,
			descricao: descricao,
			is_new: is_new,
			is_principal: is_principal
		}
	});
	
}

function listRelated(sectionName,type,start,nohistory){
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="listRelated";
		historyItem["sectionName"]=sectionName;
		historyItem["type"]=type;
		historyItem["start"]=(start!=null)?start:0;
		dhtmlHistory.add("listRelated,"+sectionName+","+type+","+start,historyItem);
	}
	new Ajax.Updater('relatedVids',"app/actions/doList.php",{
		evalScripts: true,
		parameters: {
			section: sectionName,
			list_type: type,
			start: start
		}
	});
}

function vote(item_id,rating){

	new Ajax.Updater('mainBottom','app/actions/doVote.php',{
		parameters: {
			item_id: item_id,
			rating: rating
		}
	});

}

function edit(sectionName,itemId){
	waiting();
	new Ajax.Updater('mainArea',"app/actions/doEdit.php",{
		evalScripts: true,
		parameters: {
			section: sectionName,
			item_id: itemId	
		}
	});
}

function del(sectionName,itemId){
	waiting();
	new Ajax.Updater('mainArea',"app/actions/doDelete.php",{
		evalScripts: true,
		parameters: {
			section: sectionName,
			item_id: itemId
		}
	});
}

function list(sectionName,type,start,nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="list";
		historyItem["sectionName"]=sectionName;
		historyItem["type"]=type;
		historyItem["start"]=(start!=null)?start:0;
		dhtmlHistory.add("list,"+sectionName+","+type+","+start,historyItem);
	}	
	new Ajax.Updater('mainArea',"app/actions/doList.php",{
		evalScripts: true,
		parameters: {
			section: sectionName,
			list_type: type,
			start: start
		},
		onComplete: function(){
			pageTracker._trackPageview("app/actions/doList.php#list,"+sectionName+","+type+","+start);
			new Effect.ScrollTo('mainArea');
		}
	});
}

function get(sectionName,itemId,nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="get";
		historyItem["sectionName"]=sectionName;
		historyItem["itemId"]=itemId;
		dhtmlHistory.add("get,"+sectionName+","+itemId,historyItem);
	}
	new Ajax.Updater('mainArea',"app/actions/doGet.php",{
		evalScripts: true,
		parameters: {
			section: sectionName,
			item_id: itemId
		},
		onComplete: function(){
                        pageTracker._trackPageview("app/actions/doGet.php#get,"+sectionName+","+itemId);
			new Effect.ScrollTo('mainArea');
		}
	});
}

function insereVideo(e_novo){
		
	titulo = document.getElementById("titulo").value;
	data = document.getElementById("data").value;
	tipo = document.getElementById("tipo").value;
	fonte = document.getElementById("fonte").value;
	is_destaque = document.getElementById("is_destaque").value;
	url = document.getElementById("url").value;
	artistas = new Array();
	current_index=0;
	for(i=0;i<document.getElementById("artistas[]").options.length;i++){
		if(document.getElementById("artistas[]").options[i].selected){
			artistas[current_index] = document.getElementById("artistas[]").options[i].value;
			current_index++;
		}
	}
	artistas_param = Object.toJSON(artistas);
	
	if(titulo.trim() == ''){
		writeError("Por favor insira o titulo.","divError");
		document.form_video.titulo.focus();
		return false;
	}
	if(data == ''){
		writeError("Por favor insira uma data","divError");
		document.form_video.data.focus();
		return false;
	}
	if(tipo == ''){
		writeError("Por favor insira o tipo.","divError");
		document.form_video.tipo.focus();
		return false;
	}
	if(is_destaque == ''){
		writeError("Por favor indica se o Video é um destaque.","divError");
		document.form_video.tipo.focus();
		return false;
	}	
	if(fonte == ''){
		writeError("Por favor insira a fonte.","divError");
		document.form_video.fonte.focus();
		return false;
	}
	if(url == ''){
		writeError("Por favor insira codigo fornecido pelo Youtube.","divError");
		document.form_video.url.focus();
		return false;
	}
	waiting();
	new Ajax.Updater('mainArea','app/actions/video/insere.php',{
		parameters: { 	
			titulo: titulo,
			data: data,
			tipo: tipo,
			fonte: fonte,
			url: url,
			is_destaque: is_destaque,
			artistas: artistas_param,
			is_new: e_novo
		}
	});
	
}


function insereUser(e_novo){
		
	nome = document.getElementById("nome").value;
	apelido = document.getElementById("apelido").value;
	login = document.getElementById("login").value;
	pass = document.getElementById("pass").value;
	pass2 = document.getElementById("pass2").value;
	telefone = document.getElementById("telefone").value;
	email = document.getElementById("email").value;
	perfil = document.getElementById("tipo").value;

	if(nome.trim() == ''){
		writeError("Por favor insira o seu Nome.","divError");
		document.form_registo.nome.focus();
		return false;
	}
	if(apelido == ''){
		writeError("Por favor insira o seu Apelido","divError");
		document.form_registo.apelido.focus();
		return false;
	}
	if(login == ''){
		writeError("Por favor insira o Login.","divError");
		document.form_registo.login.focus();
		return false;
	}
	if(pass == '' || pass2 == ''){
		writeError("Por favor insira e confirme a password.","divError");
		document.form_registo.pass.focus();
		return false;
	}
	if(pass != pass2){
		writeError("A password nao condiz com a confirmação.","divError");
		document.form_registo.pass.focus();
		return false;
	}

	if(telefone.trim() !="" && (!telefone.isInteger() || telefone.lenght < 9)){
		writeError("Telefone tem de ter 9 digitos.","divError");
		document.form_registo.telefone.focus();
		return false;	
	}
	waiting();
	new Ajax.Updater('mainArea','app/actions/registo/insere.php',{
		parameters: { 	
			nome: nome,
			apelido: apelido,
			login: login,
			pass: pass,
			telefone: telefone,
			email: email,
			perfil: perfil,
			is_new: e_novo
		}
	});
	
}



function insereMultimedia(is_new){
	
	id_multimedia = (document.getElementById("id_multimedia"))?document.getElementById("id_multimedia").value:-1;
	data = document.getElementById("data").value;
	titulo = document.getElementById("titulo").value;
	tipo = document.getElementById("tipo").value;
	foto = document.getElementById("hidfoto").value;

	// Retrieve editor value
	document.getElementById("descricao").value = getEditorValue("descricao");

	descricao = document.getElementById("descricao").value;

	if(!data.isValidDate()){
		writeError("Por favor insira uma data","divError");
		document.form_multimedia.data.focus();
		return false;	
	}
	
	if(titulo == ''){
		writeError("Por favor insira o t&iacute;tulo","divError");
		document.form_multimedia.titulo.focus();
		return false;
	}

	if(tipo == ''){
		writeError("Por favor insira o tipo","divError");
		document.form_multimedia.titulo.focus();
		return false;
	}


	if(foto == ''){
		writeError("Por favor escolha a foto","divError");
		document.form_multimedia.foto.focus();
		return false;
	}

	if(descricao == ''){
		writeError("Por favor introduza a descri&ccedil;&atilde;o","divError");
		document.form_multimedia.descricao.focus();
		return false;
	}
	waiting();
	new Ajax.Updater('mainArea','app/actions/multimedia/insere.php',{
		parameters: {
			id_multimedia: id_multimedia,
			data: data,
			titulo: titulo,
			tipo: tipo,
			foto: foto,
			descricao: descricao,
			is_new: is_new
		}
	});
	
}


function insereArtista(is_new){
	
	id_artista = (document.getElementById("id_artista"))?document.getElementById("id_artista").value:-1;
	nome = document.getElementById("nome").value;
	is_nacional = document.getElementById("is_nacional").value;
	is_artista_mes = document.getElementById("is_artista_mes").value;
	tipo = document.getElementById("tipo").value;
	foto = document.getElementById("hidfoto").value;
	thumb = document.getElementById("hidthumb").value;
	ano_criacao = document.getElementById("ano_criacao").value;
	nacionalidade = document.getElementById("nacionalidade").value;
	site = document.getElementById("site").value;
	myspace = document.getElementById("myspace").value;


	// Retrieve editor value
	document.getElementById("biografia").value = getEditorValue("biografia");
	biografia = document.getElementById("biografia").value;

	document.getElementById("resumo").value = getEditorValue("resumo");
	biografia = document.getElementById("resumo").value;


	if(nome == ''){
		writeError("Por favor insira o nome","divError");
		document.form_artistas.nome.focus();
		return false;
	}

	if(is_nacional == ''){
		writeError("Por favor indique se o artista é nacional.","divError");
		document.form_artistas.is_nacional.focus();
		return false;
	}
	
	if(is_artista_mes == ''){
		writeError("Por favor indique se é artista do mês.","divError");
		document.form_artistas.is_artista_mes.focus();
		return false;
	}

	if(tipo == ''){
		writeError("Por favor indique se o tipo.","divError");
		document.form_artistas.tipo.focus();
		return false;
	}

	if(foto == '' && is_new){
		writeError("Por favor escolha a foto","divError");
		document.form_artistas.foto.focus();
		return false;
	}

	if(thumb == ''){
		writeError("Por favor escolha a thumbnail","divError");
		document.form_artistas.thumb.focus();
		return false;
	}

/*	if(ano_criacao == ''){
		writeError("Por favor indique o ano de criacao.","divError");
		document.form_artistas.ano_criacao.focus();
		return false;
	}*/

	if(nacionalidade == ''){
		writeError("Por favor indique a nacionalidade.","divError");
		document.form_artistas.nacionalidade.focus();
		return false;
	}

/*	if(site == ''){
		writeError("Por favor indique a nacionalidade.","divError");
		document.form_artistas.site.focus();
		return false;
	}*/
	if(resumo == ''){
		writeError("Por favor introduza o resumo.","divError");
		document.form_artistas.resumo.focus();
		return false;
	}
	if(biografia == ''){
		writeError("Por favor introduza a biografia.","divError");
		document.form_artistas.biografia.focus();
		return false;
	}
	waiting();
	new Ajax.Updater('mainArea','app/actions/artistas/insere.php',{
		parameters: {
			id_artista: id_artista,
			nome: nome,
			is_nacional: is_nacional,
			tipo: tipo,
			foto: foto,
			ano_criacao: ano_criacao,
			nacionalidade: nacionalidade,
			biografia: biografia,
			site: site,
			myspace: myspace,
			is_artista_mes: is_artista_mes,
			is_new: is_new
		}
	});
	
}


function doChangeStyle(bg_class){
	
	new Ajax.Request("app/actions/doChangeStyle.php", {
		method: "post",
		parameters: {
			bg_class: bg_class
		},
		onSuccess: function cookieSet(){
			document.body.className = bg_class;
		}
	
	});
}



function getContacts(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getContacts";
		dhtmlHistory.add("getContacts",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/getContacts.php',{
		evalScripts: true,
		onComplete: function(){
			pageTracker._trackPageview("app/actions/getContacts.php" );
		}
	});
	
}


function getHomeArtistas(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getHomeArtistas";
		dhtmlHistory.add("getHomeArtistas",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/artistas/home.php',{
		evalScripts: true
	});
}



function getDevolveArtistas(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getDevolveArtistas";
		dhtmlHistory.add("getDevolveArtistas",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/artistas/devolve.php',{
		evalScripts: true
	});
}



function getPassatempo(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getPassatempo";
		dhtmlHistory.add("getPassatempo",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/getPassatempo.php',{
		evalScripts: true,
		onComplete: function(){
                        pageTracker._trackPageview("app/actions/getPassatempo.php" );
                }
	});
	
}

function getPassatempo2(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getPassatempo2";
		dhtmlHistory.add("getPassatempo2",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/getPassatempo2.php',{
		evalScripts: true,
		onComplete: function(){
                        pageTracker._trackPageview("app/actions/getPassatempo2.php" );
                }

	});
	
}

function getPassatempo3(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getPassatempo3";
		dhtmlHistory.add("getPassatempo3",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/getPassatempo3.php',{
		evalScripts: true
	});
	
}

function getPassatempo4(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getPassatempo4";
		dhtmlHistory.add("getPassatempo4",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/getPassatempo4.php',{
		evalScripts: true
	});
	
}

function getPassatempo5(nohistory){
        waiting();
        if(!nohistory){
                historyItem = new Array();
                historyItem["action"]="getPassatempo5";
                dhtmlHistory.add("getPassatemp5",historyItem);
        }
        new Ajax.Updater('mainArea','app/actions/getPassatempo5.php',{
                evalScripts: true
        });

}


function getPassatempoList(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getPassatempoList";
		dhtmlHistory.add("getPassatempoList",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/getPassatempoList.php',{
		evalScripts: true
	});
	
}


function getZonareggae(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getZonareggae";
		dhtmlHistory.add("getZonareggae",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/multimedia/zonareggae/devolve.php',{
		evalScripts: true,
		onComplete: function(){
                        pageTracker._trackPageview("app/actions/multimedia/zonareggae/devolve.php" );
                }

	});
}

//No Mercy Sound Clash
function getHomeNMSC(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getHomeNMSC";
		dhtmlHistory.add("getHomeNMSC",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/parcerias/devolveHome.php',{
		evalScripts: true
	});
}

function getLineUpNMSC(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getLineUpNMSC";
		dhtmlHistory.add("getLineUpNMSC",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/parcerias/devolveLineUp.php',{
		evalScripts: true
	});
}

function getEventoNMSC(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getEventoNMSC";
		dhtmlHistory.add("getEventoNMSC",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/parcerias/devolveEvento.php',{
		evalScripts: true
	});
}

function getRegrasNMSC(nohistory){
	waiting();
	if(!nohistory){
		historyItem = new Array();
		historyItem["action"]="getRegrasNMSC";
		dhtmlHistory.add("getRegrasNMSC",historyItem);
	}
	new Ajax.Updater('mainArea','app/actions/parcerias/devolveRegras.php',{
		evalScripts: true
	});
}
//


/**
* Ajax History script
*/
window.dhtmlHistory.create({
        toJSON: function(o) {
                return Object.toJSON(o);
        }
        , fromJSON: function(s) {
                return s.evalJSON();
        }
});


var myHistChange = function (newLocation, historyData) {

	if(historyData == null){
		get("main",0,true);
		return;
	}

	switch(historyData["action"]){
	
		case "get":
			get(historyData["sectionName"],historyData["itemId"],true);
		break;
		case "list":
			list(historyData["sectionName"],historyData["type"],historyData["start"],true);
		break;
		case "listRelated":
			listRelated(historyData["sectionName"],historyData["type"],historyData["start"],true);
		break;
		case "getPassatempo":
			getPassatempo(true);
		break;
		case "getPassatempo2":
			getPassatempo2(true);
		break;
		case "getPassatempo3":
			getPassatempo3(true);
		break;
		case "getContacts":
			getContacts(true);
		break;
		case "getHomeArtistas":
			getHomeArtistas(true);
		break;
		case "getDevolveArtistas":
			getDevolveArtistas(true);
		break;
		case "getZonareggae":
			getZonareggae(true);
		break;
		//No Mercy Sound Clash
		case "getHomeNMSC":
			getHomeNMSC(true);
		break;
		case "getLineUpNMSC":
			getLineUpNMSC(true);
		break;
		case "getEventoNMSC":
			getEventoNMSC(true);
		break;
		case "getRegrasNMSC":
			getRegrasNMSC(true);
		break;
		//
		default:get("main",0,true);
			//return;
			
	}
  
}

function staticLoad(historyData) {

	if(historyData == null){
		get("main",0,true);
		return;
	}

	switch(historyData["action"]){
	
		case "get":
			get(historyData["sectionName"],historyData["itemId"],true);
		break;
		case "list":
			list(historyData["sectionName"],historyData["type"],historyData["start"],true);
		break;
		case "listRelated":
			listRelated(historyData["sectionName"],historyData["type"],historyData["start"],true);
		break;
		case "getPassatempo":
			getPassatempo(true);
		break;
		case "getPassatempo2":
			getPassatempo2(true);
		break;
		case "getPassatempo3":
			getPassatempo3(true);
		break;
		case "getContacts":
			getContacts(true);
		break;
		case "getHomeArtistas":
			getHomeArtistas(true);
		break;
		case "getDevolveArtistas":
			getDevolveArtistas(true);
		break;
		case "getZonareggae":
			getZonareggae(true);
		break;
		//No Mercy Sound Clash
		case "getHomeNMSC":
			getHomeNMSC(true);
		break;
		case "getLineUpNMSC":
			getLineUpNMSC(true);
		break;
		case "getEventoNMSC":
			getEventoNMSC(true);
		break;
		case "getRegrasNMSC":
			getRegrasNMSC(true);
		break;
		//

		default: //return;
			get("main",0,true);
			
	}
  
}

function loadFromHash(hash){

	hash = hash.substring(1);

	params = hash.split(",");

	var historyData = new Array();
	
	historyData["action"] = params[0];
	
	if(params[0]=="list" || params[0]=="listRelated"){

		if(params[1]!=null)
			historyData["sectionName"]=params[1];
		if(params[2]!=null)
			historyData["type"]=params[2];
		if(params[3]!=null)	
			historyData["start"]=params[3];		
	}
	
	if(params[0]=="get" && params[1]!= null){
		historyData["sectionName"]=params[1];
		if(params[2]!=null)
			historyData["itemId"] = params[2];
	}
		
	staticLoad(historyData);
	
}

var isLive = false;

function set_live(){

	d = new Date();
	dayOfWeek = d.getUTCDay();
	hour = d.getUTCHours();
	if(dayOfWeek==3 && hour >= 23)
		isLive = true;
	else
		isLive = false;

}

function cron_zonareggae(){

	old_state = isLive;

	set_live();
	
	if(old_state != isLive){

		if(isLive){
			document.getElementById("img_zonareggae").src = "/images/btn_zonareggae_noar.png";
			if(document.getElementById("pod_islive"))
				document.getElementById("pod_islive").innerHTML = '<a href="javascript:setLivePod()" class="LinkAmarelo12">OUVIR EMISS&Atilde;O AO VIVO!!!</a>';
		}
		else{
			if(document.getElementById("pod_islive")){
				document.getElementById("pod_islive").innerHTML = '';
				document.getElementById("pod_topo").innerHTML = "Fim de emiss&atilde;o. :(";
				document.getElementById("pod_title").innerHTML = "Fim de emiss&atilde;o. :(";
				document.getElementById("pod_pubdate").innerHTML = "";
				document.getElementById("pod_description").innerHTML = "";
				document.getElementById("pod_link").href = "http://zonareggae.wordpress.com";
				document.getElementById("pod_player").innerHTML = "";
			}
			document.getElementById("img_zonareggae").src = "/app/images/btn_zonareggae2.png";
		}

	}

}

window.onload = function(){
	// initialize the DHTML History
	// framework
	dhtmlHistory.initialize();

	// subscribe to DHTML history change
	// events
    	dhtmlHistory.addListener(myHistChange);
	
	/** Our callback to receive history change
     	events. */
	 
	if(dhtmlHistory.isFirstLoad()){
		if(document.location.hash)
			loadFromHash(document.location.hash);
	}
	
	new PeriodicalExecuter(cron_zonareggae,5);
}

var jsonItems;

function setLivePod(){

	document.getElementById("pod_islive").innerHTML = "";
	document.getElementById("pod_topo").innerHTML = "EMISS&Atilde;O DIRECTA!!!";
	document.getElementById("pod_title").innerHTML = "EMISS&Atilde;O DIRECTA!!!";
	document.getElementById("pod_pubdate").innerHTML = "Em curso...";
	document.getElementById("pod_description").innerHTML = "";
	document.getElementById("pod_link").href = "mms://stream.radio.com.pt/ROLI-ENC-536";
		document.getElementById("pod_player").innerHTML = "<object id=\"ROLIWMP\"name=\"ROLIWMP\" height=\"30px\" width=\"280px\" type=\"application/x-ms-wmp\" data=\"mms://stream.radio.com.pt/ROLI-ENC-536\" uiMode='none'><param name=\"volume\" value=\"61\" /></object>";
	

}

function changeItem(item_id){
	document.getElementById("pod_topo").innerHTML = jsonItems[item_id].title;
	document.getElementById("pod_title").innerHTML = jsonItems[item_id].title;
	document.getElementById("pod_pubdate").innerHTML = jsonItems[item_id].pubdate;
	document.getElementById("pod_description").innerHTML = jsonItems[item_id].description;
	if(jsonItems[item_id].enclosures){
		document.getElementById("pod_link").href = jsonItems[item_id].enclosures[0].url;
		document.getElementById("pod_player").innerHTML = "<object id=\"ROLIWMP\"name=\"ROLIWMP\" height=\"30px\" width=\"280px\" type=\"application/x-ms-wmp\" data=\""+jsonItems[item_id].enclosures[0].url+"\" uiMode='none'><param name=\"volume\" value=\"61\" /></object>";
	}
	/*document.getElementById("ROLIWMP").controls.stop();
	document.getElementById("ROLIWMP").URL = jsonItems[item_id].enclosures[0].url;
	document.getElementById("ROLIWMP").controls.play();*/
	//document.getElementById("ROLIWMP").url = jsonItems[item_id].enclosures[0].url;
}

function mute(){
	
	if(!document.getElementById('ROLIWMP').settings.mute)
		document.getElementById('ROLIWMP').style.backgroundImage = "url(../images/radio_icon_sound_off.png)"
	else
		document.getElementById('ROLIWMP').style.backgroundImage = "url(../images/radio_icon_sound_on.png)"
		
	document.getElementById('ROLIWMP').settings.mute = !document.getElementById('ROLIWMP').settings.mute;
	
}

function setVolume(value){

	document.getElementById('ROLIWMP').settings.volume += value;
	if(document.getElementById('ROLIWMP').settings.volume > 0)
		document.getElementById('imgVol1').src = "/app/images/radio_icon_volselect.png";
	else{
		document.getElementById('imgVol1').src = "/app/images/radio_icon_volsel.png";
		return true;
	}
	if(document.getElementById('ROLIWMP').settings.volume > 20)
		document.getElementById('imgVol2').src = "/app/images/radio_icon_volselect.png";
	else{
		document.getElementById('imgVol2').src = "/app/images/radio_icon_volsel.png";
		return true;
	}
	if(document.getElementById('ROLIWMP').settings.volume > 40)
		document.getElementById('imgVol3').src = "/app/images/radio_icon_volselect.png";
	else{
		document.getElementById('imgVol3').src = "/app/images/radio_icon_volsel.png";
		return true;
	}
	if(document.getElementById('ROLIWMP').settings.volume > 60)
		document.getElementById('imgVol4').src = "/app/images/radio_icon_volselect.png";
	else{
		document.getElementById('imgVol4').src = "/app/images/radio_icon_volsel.png";
		return true;
	}
	if(document.getElementById('ROLIWMP').settings.volume > 80)
		document.getElementById('imgVol5').src = "/app/images/radio_icon_volselect.png";
	else{
		document.getElementById('imgVol5').src = "/app/images/radio_icon_volsel.png";
		document.getElementById('imgVol6').src = "/app/images/radio_icon_volsel.png";
		return true;
	}
	if(document.getElementById('ROLIWMP').settings.volume == 100)
		document.getElementById('imgVol6').src = "/app/images/radio_icon_volselect.png";
	else{
		document.getElementById('imgVol6').src = "/app/images/radio_icon_volsel.png";
		return true;
	}
		

}

function setPodPage(page){

	arr_index = page*6;

	htmlOutput = "";
	for(i=0;i<6 && (arr_index+i)<jsonItems.length;i++){
		htmlOutput += "<div class=\"clear\"></div><div class=\"play21 font_Branco11\">"+
			"<div class=\"floatL\">"+
			"<img width=\"15\" height=\"26\""+ "src=\"http://www.lusotek.net/inforeggae//app/images/radio_icon_volsel.png\"/>"+
			"</div>"+
			"<div class=\"play22\">"+
			"<a class=\"Linkbranco11\" href=\"javascript:changeItem('"+(arr_index+i)+"')\">"+jsonItems[(arr_index+i)].title+"</a>"+
			"</div>"+
			"<div class=\"play23\">"+
			"<img width=\"377\" height=\"2\" src=\"http://www.lusotek.net/inforeggae//app/images/zonareggae_separador.png\"/>"+
			"</div>"+
			"</div>";
	}

	htmlPaging = "<div class=\"arrows fontall size9 brancoColor\" align=\"center\">";

	if(page >0)
		htmlPaging += "<a href=\"javascript:setPodPage("+(page-1)+")\" class=\"Linkcinza10 size10\">&lt; Anterior</a>";
	else
		htmlPaging += "&nbsp;";

	htmlPaging += " | ";

	if((arr_index+i)<jsonItems.length)
		htmlPaging += "<a href=\"javascript:setPodPage("+(page+1)+")\" class=\"Linkcinza10 size10\">Pr&oacute;xima &gt;</a>";
	else
		htmlPaging += "&nbsp;";

	htmlPaging += "</div><div class=\"numeroPags fontall size10 brancoColor\" align=\"right\" style=\"width:208px\">";

	for(j=0,k=1;j<5 && (page+j)<(jsonItems.length/6);j++,k++)
		htmlPaging += "<a href=\"javascript:setPodPage("+(page+j)+")\" class=\"Linkcinza10 size10\">"+(page+k)+"</a>&nbsp;";

	htmlPaging += "</div><div class=\"numeroTotal fontall size11 brancoColor\" align=\"center\">"+Math.round(jsonItems.length/6)+" P&aacute;gs. </div>";

	document.getElementById("pod_list").innerHTML = htmlOutput;
	document.getElementById("pod_paging").innerHTML = htmlPaging;

}

	

