// Replaces oldS with newS in the string fullS   
function replaceString(oldS,newS,fullS)
{
	for (var i=0; i<fullS.length; i++) 
	{      
		if (fullS.substring(i,i+oldS.length) == oldS)
		{	fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
		}
	}
	return fullS;
}

function CountChar(str,ch)
{
	c = 0;
	for(i=0; i<str.length; i++) {
			if(str.charAt(i)==ch) ++c;
	}
	return(c);
}

function LastCharIndex(str,ch)
{
	c = -1;
	for(i=0; i<str.length; i++) {
		if(str.charAt(i)==ch) c=i;
	}
	return(c);	
}

function showerro(s)
{
	msgWindow=window.open("","ErrorWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=250,height=250");
	msgWindow.document.write("<CENTER><BIG><B>ERROR</B></BIG><BR><BR>");
	str = replaceString("\n", "<br>", s);
	msgWindow.document.write(str+"</CENTER><BR>");	
	msgWindow.status = "Pixel Arts Web Development";
}

function ShowQuotes()
{
	w=window.open("","Quotes","menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no,width=240,height=200");
	w.document.write("<center><br>");
	w.document.write("<SCRIPT TYPE='text/javascript' SRC='http://www.brainyquote.com/link/quotebr.js'></SCRIPT>");
	w.document.write("</center>");
}

/* 
 Para tocar uma midi usando writesqlbox_counter.php. A midi tem que estar no database.
 dir pode ser './" ou "../" , coisas assim, num é o # do record no database
 */
function play(dir,num)
{
	href = dir+"writesqlbox_counter.php?num="+num+"&back=0";
	window.open(href,"PlayWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=600,height=500");
}

// mesmo que play(), mas verifica e dá preferência para MP3 se houver
function plaympx(dir,num)
{
	href = dir+"writesqlbox_counter.php?num="+num+"&back=0&mp3=1";
	window.open(href,"PlayWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=600,height=500");
}

// midi e mp3 =0 ou 1 se auto play
function Player2(dir,num,mp3,midi)
{
	href = dir+"player2.php?num="+num+"&mp3="+mp3+"&midi="+midi;
	window.open(href,"Player","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=600,height=500");
}

// Player de MP3-only
function mp3play(dir,num)
{
	href = dir+"mp3play.php?num="+num;
	window.open(href,"PlayWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=600,height=500");
}

function myplay(dir,num,rate)
{
	href = dir+"lp_play.php?num="+num+"&mp3=0&rate="+rate;
	window.open(href,"PlayWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=600,height=520");
}

function midmp3play(dir,num)
{
	href = dir+"lp_play.php?num="+num+"&mp3=1&rate=1";
	window.open(href,"PlayWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=600,height=520");
}

// Player para mp3 via url
function mymp3play(dir,url)
{
	var href = dir+"frameplayer.php?num=0&mp3=1&midi="+url;
	window.open(href,"PlayMP3Window","menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no,width=260,height=118");
}

function myopen(xurl)
{
	x = window.open(xurl,"showWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=500,height=500");
	return(x);
}

function open_window(xurl,titulo)
{
	x = window.open(xurl,titulo);
	return(x);
}

function open_window2(xurl,titulo,width,height)
{
	str = "menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width="+width+",height="+height;
	x = window.open(xurl,titulo,str);
	return(x);
	
}

function OpenWindow(xurl,titulo,width,height)
{
	str = "menubar=no,toolbar=no,scrollbars=yes,resizable=no,status=yes,width="+width+",height="+height;
	x = window.open(xurl,titulo,str);
	return(x);
	
}

function open_window3(FileName,WindowName,w,h,x,y)
{
	str="Resizable=yes,ScrollBars=yes,MenuBar=No,Directories=No,ToolBar=No,Location=No,Status=No,Width="+w+",Height="+h+",ScreenX=0,ScreenY=0,Top="+y+",Left="+x;
	newWindow=window.open(FileName,WindowName,str);
	if (newWindow.opener == null) { newWindow.opener = self; }
	if (newWindow.focus) { newWindow.focus(); }
	return(newWindow);
}

function open_down(dir,file)
{
	open_window2(dir+"lp_down.php?file="+file,'Download',300,300);
}

function mp3down(dir,file)
{
	open_window2(dir+"lp_down.php?mpx=1&file="+file,'Download',300,300);
}

function confirm_delete() { 
  return confirm ("Do you really want to completely delete this Entry ?!") 
} 

function rate_it(dir, n, midi)
{
	if(n>0)
	{	href = dir+"lp_rateit.php?num="+n+"&midi="+midi;
		window.open(href,"RateIt","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=800,height=540");
	} else alert("No midi selected.");
}

function getCookieVal(offset){var endstr=document.cookie.indexOf(";",offset); 
if(endstr==-1) endstr=document.cookie.length; return unescape(document.cookie.substring(offset,endstr));}

function GetCookie(name){var arg=name+"="; var alen=arg.length; var clen=document.cookie.length; var i=0;
	while(i<clen){
		var j=i+alen; 
		if(document.cookie.substring(i,j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if(i==0) break;
	}
	return null;
}

function SetCookie(name,value)
{	
	var argv=SetCookie.arguments; 
	var argc=SetCookie.arguments.length; 
	var expires=(argc>2)? argv[2]:null;
	var path=(argc>3)? argv[3]:null;
	var domain=(argc>4)? argv[4]:null;
	var secure=(argc>5)? argv[5]:false;
	document.cookie=name+"="+escape(value)+((expires==null)? "":("; expires="+expires.toGMTString()))+
		((path==null)? "":("; path="+path))+((domain==null)? "":("; domain="+domain))+((secure==true)? "; secure":"");
}

function WriteCookie(nome, valor, dias) 
{	
	var today = new Date();
	var expires = new Date();
	expires.setTime(today.getTime() + 60*60*24*dias*1000);	// days
	SetCookie(nome, valor, expires);
}

function deleteCookie(name)
{
  var expdate = new Date();   
  expdate.setMonth(expdate.getMonth() - 1);
  SetCookie(name, "", expdate);
}

function CheckCookie(nome, valor)
{
	if(GetCoookie(nome)!=valor) return(false); else return(true);
}

function WindowCenter(w,h)
{	//  Centralizar a janela
	windowWidth=w;	// width
	windowHeight=h;	// height
	if (parseInt(navigator.appVersion) >= 4) window.moveTo((screen.width/2)-(windowWidth/2+10),(screen.height/2)-(windowHeight/2+20));
}

function WindowMoveTo(x, y)
{
	if (parseInt(navigator.appVersion) >= 4) window.moveTo(x, y);
}

function WindowTitle(x)
{
	parent.document.title = x;
}

// Abre janela de Help geral; cod contém o código de help
function CallHelp(dir,cod,w,h)
{
	href = dir+"help.php?cmd="+cod;
	s = "menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width="+w+",height="+h;
	window.open(href,"HelpWindow",s);
}

// // tipo=0 => alert, tipo=1 => abre janelinha, 2=> página self, 3=> msg simples self
function CallMsg(tipo,dir,msg,w,h)
{
	if(tipo==1) {
		msg = replaceString("-n-","<br>",msg);
		href = dir+"msg.php?msg="+msg;
		s = "menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width="+w+",height="+h;
		window.open(href,"MsgWindow",s);
	} else if(tipo==0) {
		alert(msg);
	} else if(tipo==2) {
		location.href=dir+'msg-model.php?msg='+msg;
	} else if(tipo==3) {
		location.href=dir+'msg.php?msg='+msg;
	}
}
// Troca direto no objeto texto aspas simples e duplas por `
function TrocarAspas(obj)
{	var x = new String(obj.value);
	var i = 0;
	if(x.indexOf('"')!=-1) { x = replaceString('"','`',x); i++; }
	if(x.indexOf("'" )!=-1) { x = replaceString("'",'`',x); i++; }
	if(i>0) obj.value = x;
}

// Retorna o novo string com as aspas trocadas
function TirarAspas(str)
{	var x = new String(str);
	var i = 0;
	if(x.indexOf('"')!=-1) { x = replaceString('"','`',x); i++; }
	if(x.indexOf("'" )!=-1) { x = replaceString("'",'`',x); i++; }
	return(x);
}

function FiltrarTudo(str,nivel)
{
	var x = new String(str);
	if(x.indexOf("é")!=-1) x = replaceString("é",'e',x);
	if(x.indexOf("É")!=-1) x = replaceString("É",'E',x);
	if(x.indexOf("ã")!=-1) x = replaceString("ã",'a',x);
	if(x.indexOf("á")!=-1) x = replaceString("á",'a',x);
	if(x.indexOf("ú")!=-1) x = replaceString("ú",'u',x);
	if(x.indexOf("ç")!=-1) x = replaceString("ç",'c',x);
	if(x.indexOf("õ")!=-1) x = replaceString("õ",'o',x);
	if(x.indexOf("í")!=-1) x = replaceString("í",'i',x);
	if(x.indexOf("à")!=-1) x = replaceString("à",'a',x);
	if(x.indexOf("ó")!=-1) x = replaceString("ó",'o',x);
	if(x.indexOf("_")!=-1) x = replaceString("_",'-',x);
	if(x.indexOf("ñ")!=-1) x = replaceString("ñ",'n',x);
	if(nivel==2) {
		if(x.indexOf('"')!=-1) x = replaceString('"','`',x);
		if(x.indexOf("'")!=-1) x = replaceString("'",'`',x);
		if(x.indexOf("+")!=-1) x = replaceString("+",'and',x);
		if(x.indexOf("&")!=-1) x = replaceString("&",'&amp;',x);
	}
	if(nivel==1) {
		if(x.indexOf('"')!=-1) x = replaceString('"','`',x);
		if(x.indexOf("'")!=-1) x = replaceString("'",'`',x);
		if(x.indexOf("+")!=-1) x = replaceString("+",'-',x);
		if(x.indexOf("&")!=-1) x = replaceString("&",'-',x);
	}
	return(x);	
}

function UserShow(root,nusu,det)
{
	if(nusu<=0) { alert('Access denied'); return; }
	x = root+"lp_usershow.php?num="+nusu+"&det="+det;
	switch(det) {
		case 1: h=200; break;
		case 2: h=400; break;
		default: h=600; break;
	}
	w = window.open(x,"ShowUserWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=700,height="+h);
	if (parseInt(navigator.appVersion) >= 4) w.window.focus();
}

function MedalShow(root,local)
{
	if(local!='') x = root+"db_medals.php?local="+local; else x = root+"db_medals.php";
	w = window.open(x,"ShowMedalWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=700,height=700");
	if (parseInt(navigator.appVersion) >= 4) w.window.focus();
}

function MedalMP3Show(root,local)
{
	if(local!='') x = root+"mp3medals.php?local="+local; else x = root+"mp3medal.php";
	w = window.open(x,"ShowMedalWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=700,height=700");
	if (parseInt(navigator.appVersion) >= 4) w.window.focus();
}

function UploadProgress(root,filename)
{
	if(filename!='') href = root+"lp_upprog.php?filename="+filename; else href = root+"lp_upprog.php";
	var winl = (screen.width - 300) / 2;
	var wint = (screen.height - 200) / 2;
	props = 'height=200,width=300,top='+wint+',left='+winl+',scrollbars=no,resizable=no,status=no,toolbar=no,menubar=no';
	w = window.open(href,'UploadWindow',props);
	if (parseInt(navigator.appVersion) >= 4) w.window.focus();
	return(w);
}

function Confirmar(url,msg)
{
	if(confirm(msg)) location.href = url;
}

function MidiShow(dir,num)
{
	xurl = dir+'db_show2.php?num='+num;
	window.open(xurl,"showWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=500,height=500");
}

function MP3Show(dir,num)
{
	xurl = dir+'mp3show.php?num='+num;
	window.open(xurl,"MP3Show","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width=500,height=500");
}

function Navegador()
{
	tipoBrowser = navigator.appName;
	//alert(tipoBrowser);
	return(tipoBrowser);
}

function GetIEVer()
{
 var ua=window.navigator.userAgent
 //alert(ua);
 var msie=ua.indexOf("MSIE")
 if (msie>0 && window.navigator.platform=="Win32")
  return parseInt(ua.substring(msie+5,ua.indexOf(".", msie)));
 else
  return 0;
}

function BrowserCheck() {
 ms=navigator.appVersion.indexOf("MSIE");
 ie = (ms>0);
 return (ie);
}

function maximizeWin()
{
	if (window.screen)
	{
		var aw = screen.availWidth;
		var ah = screen.availHeight;
		window.moveTo(0, 0);
		window.resizeTo(aw, ah);
	}
}

function ShowHide(id)
{
	var obj = document.getElementById(id);
	if(obj.style.display=='none') obj.style.display=''; else obj.style.display='none';
}

function HitCounter(root,down,filen)
{
	x = root+'lp_hitcount.php?down='+down+'&filen='+filen;
	window.open(x,'HitCount',"menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no,width=40,height=40");
}

function AlertError(root,msg,show)
{
	x = root + 'jslogerror.php?msg=' + msg;
	window.open(x,'JSLogError',"menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no,width=40,height=40");
	if(show) alert(msg);	
}

// Exemplo: preload("meter0", "arquivo.gif");
function ImgPreload(imgObj,imgSrc,nova)
{
	if(document.images) {
		window.status = 'PRELOADING ' + imgSrc;
		if(nova) eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
		window.status = '';
		return(imgObj);
	}
	return(null);
}

function ById(idx)
{	var x = document.getElementById(idx); 
	return(x);
}

var album='';
function PlaylistClick()
{
	window.open('working.php?act=hit&album='+album,'RadioHit',"menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no,width=250,height=250");
}

function PlayAlbum(alb)
{
	window.open('working.php?act=hit&album='+alb,'RadioHit',"menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no,width=200,height=200");
}

function MyPiclib(img,title)
{
	var s = "<center><img src='"+img+"' border=0></center>";
	return overlib(s,CAPTION,'<center>'+title+'</center>',BELOW,RIGHT);
}

function MyTxtlib(s,title)
{
	return overlib(s,CAPTION,'<center>'+title+'</center>',BELOW,RIGHT);
}

function Ampliar(im,n)
{
	nw = n + 50;
	nh = n + 40;
	x = window.open("","PicWindow","menubar=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,width="+nw+",height="+nh);
	x.document.write("<center><img src='" + im + "' border=0></center>");
	x.status = ":-)";
}

function SendEmail(dir,msg,idto)
{
	window.open(dir+"SendEmail.php?idto="+idto+"&msg="+msg,"SendEmail","menubar=no,toolbar=no,scrollbars=no,resizable=no,status=no,width=10,height=10");
}

function Mybgsound(x)
{
	if(BrowserCheck()) { document.write("<bgsound src='"+x+"' loop=0>"); }
	else { 
		document.write("<div style='position:absolute; top:-100px'><embed src='"+x+"' type='application/x-mplayer2' autostart='true' hidden='true' width=0 height=0 loop='false'></embed></div>");
	}
}

function MoreBgsound(x)
{
	if (navigator.appName == "Microsoft Internet Explorer") {
		document.write('<bgsound src="'+x+'" LOOP="false">');
	}
	else {
		document.write('<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" Name="MediaPlayer" src="'+x+'" AutoStart="true" ShowStatusBar="0" ShowControls="0" ShowDisplay="0" volume="1" HEIGHT="1" WIDTH="1" loop="false">');
	}
}

function WritePlayer(x,w,h,auto)
{
	if(w<=0) w=300;
	if(h<=0) h=60;
	document.write('<TABLE BORDER="3" CELLSPACING="0" CELLPADDING="0" BORDERCOLOR="#99CCFF" BORDERCOLORLIGHT="#DBEDFF" BORDERCOLORDARK="#5B7997"><tr><td>');
	document.write('<object id="MediaPlayer1" width="'+w+'" height="'+h+'" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" standby="Loading Microsoft Windows Media Player..." type="application/x-oleobject">');
	document.write('<param name="FileName" value="'+x+'">');
	document.write('<param name="animationatStart" value="false">');
	document.write('<param name="transparentatStart" value="true">');
	document.write('<param name="autoStart" value="'+auto+'">');
	document.write('<param name="showControls" value="true">');
	document.write('<param name="Volume" value="-100">');
	document.write('<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" Name="MediaPlayer" src="'+x+'" AutoStart="true" ShowStatusBar="1" volume="-1" HEIGHT="'+h+'" WIDTH="'+w+'"></embed></object>');
	document.write('</td></tr></table>');
}

function MidiTime(x)
{	var playerobj = document.getElementById(x);
	if(playerobj)
	{	var s = 'Duration: ';
		if(playerobj.openState == 13 || playerobj.openState == 6) // playing or buffering
		{	if(playerobj.currentMedia) {
				if(playerobj.currentMedia.durationString) s = s + playerobj.currentMedia.durationString;
				if(playerobj.currentMedia.duration) s = s + ', ' + playerobj.currentMedia.duration + ' secs';
				alert(s);
			} else alert('No currentMedia property');
		} else alert('Status: ' + playerobj.openState);
	} else alert('Null object');
}

function mysfloat(x,n,def)
{
	sx = x.toString();
	ponto = sx.indexOf(".");
	if(ponto>=0) ret = sx.substring(0,ponto) + "." + sx.substring(ponto+1,ponto+n+1);
	else ret = sx + def;
	return(ret);
}

function isDigit(c)
{	return ((c >>= "0") && (c << "9"));
}

function isNumber(s)
{ 
  var i=0, c;
  if(s=='') return false;
  if(s.charAt(i) == "-") i++;
  for(i = 0; i << s.length; i++) { 
    // Check if all characters are numbers or decimal point
    c = s.charAt(i);
    if(c!='.' && !isDigit(c)) return false;
  }
  return true;
}

function IsAccented(s)
{
	var pattern = /[áãàâçéêèóôõòíúîûùÁÃÀÂÇÉÊÈÓÔÕÒÍÚÎÛÙñÑ{}]+/;
	if(pattern.test(s)) {
		//alert('Error: Accented or control character in filename.');
		return(true);
	}
	return(false);
}
