var dom = {
	document	: document,
	functions	: new Array(),
	loaded 		: false,
	loadTimer	: ''
}

dom.onload = function(){
	this.loadTimer = '';
	if(this.document.body!=null){
		this.loaded = true;
		for(_i=0;_i<this.functions.length;_i++){
			eval(this.functions[_i]);
		}
	} else { if(this.loadTimer==''){ this.loadTimer = setTimeout("dom.onload()",300); } }
}

dom.addOnload = function(callback){
	dom.functions[dom.functions.length] = callback;
}

dom.onload();

function createElement(elmName){
	if(arguments.length==1){
		return document.createElement(elmName);
	} else {
		if(document.all==undefined){
			ret = document.createElement(elmName);
			for(i=1;i<arguments.length;i++){
				eval("ret.setAttribute('"+arguments[i]+"','"+arguments[++i]+"')");
			}
		} else {
			props = "";
			for(i=1;i<arguments.length;i++){
				props += ' '+arguments[i]+'="'+arguments[++i]+'"';
			}
			eval("ret = document.createElement('<'+elmName+props+'>')");
		}
		return ret;
	}
}

function destroyElement(elmName){
	if(getElement(elmName)){
		document.body.removeChild(getElement(elmName));
	}
}

function getElement(elm){
	try {
		return document.getElementById(elm);
	} catch(e){
		try {
			return document.all(elm);
		} catch(e){ return false; }
	}
}

function setStyle(obj,property,value){
	if(obj!=null){
		try{
			eval('obj.style.'+property+' = "'+value+'"');
		} catch (e){
			eval('obj.'+property+' = "'+value+'"');
		}
	}
}

function getStyle(obj,property){
	if(obj!=null){
		try{ eval('ret = obj.style.'+property+';'); }
		catch (e){ eval('ret = obj.'+property+';'); }
		return ret;
	}
	return null;
}

function scrollTop(){
	if (document.documentElement && document.documentElement.scrollTop)
		theTop = document.documentElement.scrollTop;
	else if (document.body)
		theTop = document.body.scrollTop;
	return theTop;
}

function scrollLeft(){
	if (document.documentElement && document.documentElement.scrollLeft)
		theTop = document.documentElement.scrollLeft;
	else if (document.body)
		theTop = document.body.scrollLeft;
	return theTop;
}

function setAttribute(obj,attr,value){
	if(typeof eval("obj."+attr)!="undefined") {
		eval("obj."+attr+" = function(){ "+value+" }");
	} else if(obj.setAttribute){
		obj.setAttribute(attr,value);
	}
}

function checkFCK(item){
	if(typeof(FCKeditorAPI)!="undefined"){
		return ((FCKeditorAPI.GetInstance(item).GetXHTML() != '' && FCKeditorAPI.GetInstance(item).GetXHTML() != '<br>'))?1:0;
	} else {
		return ((document.forms['main'].elements[item].value != '' && document.forms['main'].elements[item].value != '<br>'))?1:0;
	}
}

function fetchFCK(item){
	return escape(((typeof(FCKeditorAPI)!="undefined")?FCKeditorAPI.GetInstance(item).GetXHTML():document.forms['main'].elements[item].value));
}

var Table = {
};

Table.create = function(){
	table = createElement("table");
}
Table.addThead = function(){
	thead = createElement("thead");
	table.appendChild(thead);
}
Table.addTbody = function(){
	tbody = createElement("tbody");
	table.appendChild(tbody);
}
Table.addTfoot = function(){
	tfoot = createElement("tfoot");
	table.appendChild(tfoot);
}
Table.addTr = function(dest){
	tr = createElement("tr");
	eval("dest.appendChild(tr)");
	return tr;
}
Table.addTd = function(dest){
	td = createElement("td");
	eval("dest.appendChild(td)");
	return td;
}
Table.display = function(dest){
	eval("dest.appendChild(table)");
}
Table.object = function(){
	return table;
}

function offsetX(event){
	if(event.offsetX){
		return event.offsetX;
	} else if(event.layerX){
		return event.layerX;
	} else {
		return false;
	}
}

function offsetY(event){
	if(event.offsetY){
		return event.offsetY;
	} else if(event.layerY){
		return event.layerY;
	} else {
		return false;
	}
}

var tinyId;
tinyId = new Array();
function initEditor(id) {
	tinyMCE.execCommand('mceAddControl', false, id);
	tinyId[tinyId.length] = id;
}

function toggle(act){
	id = 'testo';
	if(act==false){
		alert("remove");
		tinyMCE.execCommand('mceRemoveControl', false, id);
	} else {
		alert("add");
		tinyMCE.execCommand('mceAddControl', false, id);
	}
}

function doLogout(){
	ajax.load('utenti.php','logout&sezione=main');
}

Array.in_array = function(arr,key){
	for(i=0;i<arr.length;i++){
		if(arr[i]==key){ return i; }
	}
	return -1;
}

function in_array(arr,key){
	for(i=0;i<arr.length;i++){
		if(arr[i]==key){ return i; }
	}
	return -1;
}

/* --------------------------------------------------------------------------------------- */

function setStar(id){
	getElement('star1').src=path+(id>=1?"images/star.png":"images/emptystar.png");
	getElement('star2').src=path+(id>=2?"images/star.png":"images/emptystar.png");
	getElement('star3').src=path+(id>=3?"images/star.png":"images/emptystar.png");
	getElement('star4').src=path+(id>=4?"images/star.png":"images/emptystar.png");
	getElement('star5').src=path+(id==5?"images/star.png":"images/emptystar.png");
}

function resetStar(val){
	getElement('star1').src=path+(val>=1?"images/star.png":(val<0.5?"images/emptystar.png":"images/halfstar.png"));
	getElement('star2').src=path+(val>=2?"images/star.png":(val<1.5?"images/emptystar.png":"images/halfstar.png"));
	getElement('star3').src=path+(val>=3?"images/star.png":(val<2.5?"images/emptystar.png":"images/halfstar.png"));
	getElement('star4').src=path+(val>=4?"images/star.png":(val<3.5?"images/emptystar.png":"images/halfstar.png"));
	getElement('star5').src=path+(val==5?"images/star.png":(val<4.5?"images/emptystar.png":"images/halfstar.png"));
}

function addVote(i,v){
	ajax.load(document.location,'v&v='+v+'&i='+i);
}

function sendComment(i,v){
	ajax.load(document.location,'c&v='+v+'&i='+i);
}

function loadMostVoted(){
	ajax.load(document.location,'r');
}

function loadMostVisited(){
	
	ajax.load(document.location,'v');
}

function loadTopTen(){
	ajax.load(document.location,'t');
}

function register(){
	f = document.forms['register'];
	if(f.nickname.value==''){
		f.nickname.focus();
		alert('Il campo Nickname e\' obbligatorio!');
		return;
	} else if(f.nickname.value.length<3 || f.nickname.value.length>25){
		f.nickname.focus();
		alert('Il campo Nickname dev\'essere compreso tra 3 e 25 caratteri!');
		return;
	}
	if(f.pass.value==''){
		f.pass.focus();
		alert('Il campo Password e\' obbligatorio!');
		return;
	} else if(f.pass.value.length<3 || f.pass.value.length>25){
		f.pass.focus();
		alert('Il campo Password dev\'essere compreso tra 3 e 25 caratteri!');
		return;
	} else if(f.pass.value!=f.repass.value){
		f.pass.focus();
		alert('I campi Password e Riperti Password non coincidono!');
		return;
	}
	if(f.nome.value==''){
		f.nome.focus();
		alert('Il campo Nome e\' obbligatorio!');
		return;
	} else if(f.nome.value.length<3 || f.nome.value.length>50){
		f.nome.focus();
		alert('Il campo Nome dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}
	if(f.cognome.value==''){
		f.cognome.focus();
		alert('Il campo Cognome e\' obbligatorio!');
		return;
	} else if(f.cognome.value.length<3 || f.cognome.value.length>50){
		f.cognome.focus();
		alert('Il campo Cognome dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}
	if(f.nascita.value==''){
		f.nascita.focus();
		alert('Il campo Data di Nascita e\' obbligatorio!');
		return;
	} else {
		go = false;
		n = f.nascita.value.split("/");
		if(n.length==3){
			if((n[0]>0 && n[0]<32) && (n[1]>0 && n[1]<13) && (n[2]>1900 && n[2]<2008)){
				go = true;
			}
		}
		if(!go){
			f.nome.focus();
			alert('Il campo Data di Nascita dev\'essere nel formato giorno/mese/anno!');
			return;
		}
	}
	if(f.provincia.options[f.provincia.options.selectedIndex].value==''){
		f.provincia.focus();
		alert('Selezionare la provincia di residenza!');
	}
	/*if(f.cap.value==''){
		f.cap.focus();
		alert('Il campo C.A.P. e\' obbligatorio!');
		return;
	}
	if(f.localita.value==''){
		f.localita.focus();
		alert('Il campo Localita e\' obbligatorio!');
		return;
	} else if(f.localita.value.length<3 || f.localita.value.length>50){
		f.localita.focus();
		alert('Il campo Localita dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}
	if(f.indirizzo.value==''){
		f.indirizzo.focus();
		alert('Il campo Indirizzo e\' obbligatorio!');
		return;
	} else if(f.indirizzo.value.length<3 || f.indirizzo.value.length>50){
		f.nome.focus();
		alert('Il campo Indirizzo dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}
	if(f.civico.value==''){
		f.civico.focus();
		alert('Il campo Civico e\' obbligatorio!');
		return;
	} else if(f.civico.value.length<3 || f.civico.value.length>50){
		f.civico.focus();
		alert('Il campo Civico dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}
	if(f.telefono.value==''){
		f.telefono.focus();
		alert('Il campo Recapito Telefonico e\' obbligatorio!');
		return;
	} else if(f.telefono.value.length<3 || f.telefono.value.length>50){
		f.telefono.focus();
		alert('Il campo Recapito Telefonico dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}*/
	if(f.email.value==''){
		f.email.focus();
		alert('Il campo Email e\' obbligatorio!');
		return;
	} else if(f.email.value.length<3 || f.email.value.length>50){
		f.email.focus();
		alert('Il campo Email dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	} else if(!checkMail(f.email.value)){
		alert('Il campo Email deve contenere un indirizzo email valido!');
		return;
	}
	if(getElement('terminisi').checked!=true){
		alert('Per poter proseguire nel processo di registrazione si devono accettare i Termini D\'Uso!');
		return;
	}
	if(getElement('infosi').checked!=true){
		alert('Per poter proseguire nel processo di registrazione si deve accettare l\'informativa per la Privacy!');
		return;
	}
	if(getElement('fan').checked!=true && getElement('artista').checked!=true){
		alert('Per poter proseguire nel processo di registrazione si deve selezionare tra Fan e Artista!');
		return;
	}
	f.action = 'music-showcase/registrati/';
	f.submit();
}

function checkMail(field) {
	var goodEmail = field.match(/\b(^(\S{1,}@).{3,}((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,6}))$)\b/gi);
	if (goodEmail){
		return true;
	} else {
		return false;
	}
}

function registranewsletter(){
	with(document.forms['newsletter']){
		if(mail.value==''){
			mail.focus();
			alert('Il campo Email dev\'essere compreso tra 3 e 50 caratteri!');
		} else if(!checkMail(mail.value)){
			alert('Il campo Email deve contenere un indirizzo email valido!');
		} else {
			act = getElement('iscrivi').checked?"i":"c";
			ajax.load(path+"newsletter/",act+'&m='+mail.value);
		}
	}
}

function doSub(o,e){
	if((e?e.keyCode:window.event.keyCode)=="13"){ o.submit();}
}

function checkpass(f){
	f = document.forms[f];
	if(f.pass.value==''){
		f.pass.focus();
		alert('Il campo Password e\' obbligatorio!');
		return;
	} else if(f.pass.value.length<3 || f.pass.value.length>25){
		f.pass.focus();
		alert('Il campo Password dev\'essere compreso tra 3 e 25 caratteri!');
		return;
	} else if(f.pass.value!=f.repass.value){
		f.pass.focus();
		alert('I campi Password e Riperti Password non coincidono!');
		return;
	}
	f.action = 'music-showcase/my-musicanelsangue/';
	f.submit();
}

function modificadati(){
	f = document.forms['moddati'];
	if(f.nome.value==''){
		f.nome.focus();
		alert('Il campo Nome e\' obbligatorio!');
		return;
	} else if(f.nome.value.length<3 || f.nome.value.length>50){
		f.nome.focus();
		alert('Il campo Nome dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}
	if(f.cognome.value==''){
		f.cognome.focus();
		alert('Il campo Cognome e\' obbligatorio!');
		return;
	} else if(f.cognome.value.length<3 || f.cognome.value.length>50){
		f.cognome.focus();
		alert('Il campo Cognome dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	}
	if(f.nascita.value==''){
		f.nascita.focus();
		alert('Il campo Data di Nascita e\' obbligatorio!');
		return;
	} else {
		go = false;
		n = f.nascita.value.split("/");
		if(n.length==3){
			if((n[0]>0 && n[0]<32) && (n[1]>0 && n[1]<13) && (n[2]>1900 && n[2]<2008)){
				go = true;
			}
		}
		if(!go){
			f.nome.focus();
			alert('Il campo Data di Nascita dev\'essere nel formato giorno/mese/anno!');
			return;
		}
	}
	if(f.provincia.options[f.provincia.options.selectedIndex].value==''){
		f.provincia.focus();
		alert('Selezionare la provincia di residenza!');
	}
	if(f.email.value==''){
		f.email.focus();
		alert('Il campo Email e\' obbligatorio!');
		return;
	} else if(f.email.value.length<3 || f.email.value.length>50){
		f.email.focus();
		alert('Il campo Email dev\'essere compreso tra 3 e 50 caratteri!');
		return;
	} else if(!checkMail(f.email.value)){
		alert('Il campo Email deve contenere un indirizzo email valido!');
		return;
	}
	f.action = 'music-showcase/my-musicanelsangue/';
	f.submit();
}

function est(str){
	str = str.substr(str.lastIndexOf(".")+1,str.length);
	return (str=='gif' || str=="jpg" || str=="png" || str=='jpeg')?true:false;
}

function doSearch(o){
	r = / /gi;
	document.location.replace(document.forms['search'].action+o.search.value.replace(r,"-")+"/");
}

function submitNew(url,f){
	if(f.url.value=='' || f.cat.options[f.cat.options.selectedIndex].value=='' || f.email.value=='' || f.titolo.value==''){
		alert('I campi sono obbligatori!');
	} else if(!checkMail(f.email.value)){
		alert('Inserire un indirizzo email valido!');
	} else {
		ajax.save(url,'add-new-blog',f);
	}
}

function submitContact(url,f){
	if(f.url.value=='' || f.mesg.value==''){
		alert('I campi sono obbligatori!');
	} else if(!checkMail(f.email.value)){
		alert('Inserire un indirizzo email valido!');
	} else {
		ajax.save(url,'add-contact',f);
	}
}