// JavaScript Document
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function eId(id){
	return document.getElementById(id);
}

function validarCategoria(){
	var f = document.categoria;
	if(f.nome.value == ""){
		alert("Por favor, Digite a categoria !");
		f.nome.focus();
	}else{
		eId("enviar").value    = "Enviando...";
		eId("enviar").disabled = true;
		f.submit();
	}
}

function validarProduto(){
	var f = document.produto;
	if(f.nome.value == ""){
		alert("Por favor, digite um nome !");
		f.nome.focus();
	}else if(f.descricao.value == ""){
		alert("Por favor, digite uma descricao !");
		f.descricao.focus();
	}else if(f.preco.value == ""){
		alert("Por favor, digite um preço ex. (2, 55) !");
		f.preco.focus();
	}else{
		eId("enviar").value    = "Enviando...";
		eId("enviar").disabled = true;
		f.submit();
	}
}

function validarAdmin(){
	var f = document.administrador;
	if(f.nome.value == ""){
		alert("Por favor, digite um nome !");
		f.nome.focus();
	}else if(f.email.value == ""){
		alert("Por favor, digite um e-mail !");
		f.email.focus();
	}else if(f.usuario){
		if(f.usuario.value == ""){
			alert("Por favor, digite um nome de Usuário !");
			f.usuario.focus();
		}
	}else if(f.senha.value == ""){
		alert("Por favor, digite uma Senha !");
		f.senha.focus();
	}else{
		eId("enviar").value    = "Enviando...";
		eId("enviar").disabled = true;
		f.submit();
	}
}

function excluirCategoria(id, p){
	var texto = ((p.indexOf('sub') == -1)?"Categoria":"Sub-Categoria");
	if(confirm("Atenção: Ao excluir esta "+texto+" você estará apagando também todos os itens desta "+texto+", tem certeza que gostaria de excluir esta "+texto+" ?")){
		window.location.href = "?p=" + p + "&id=" + id;
	}
}

function validarEmail(email){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    return er.test(email);
}
function validarNumero(n){
	var re = new RegExp(/[0-9]{1,3}/);
	return re.test(n);
}

function validarCompra(){
	var f = document.comprar;
	if(validarNumero(f.quantidade.value) == false || f.quantidade.value == "0"){
		alert("Digite uma quantidade válida ex: 3");
		f.quantidade.focus();
	}else if(f.nome.value == ""){
		alert("Digite o seu nome completo");
		f.nome.focus();
	}else if(!validarEmail(f.email.value)){
		alert("Digite um e-mail válido");
		f.email.focus();
	}else if(validarNumero(f.ddd.value) == false || f.ddd.value == "0"){
		alert("Digite um DDD válido");
		f.ddd.focus();
	}else if(f.telefone.value == ""){
		alert("Digite seu numero de telefone");
		f.telefone.focus();
	}else{
		eId("enviar").value = "Enviando...";
		eId("enviar").disabled = true;
		f.submit();
	}
}
function validarContato(){
	var f = document.contato;
	if(f.nome.value == ""){
		alert("Digite o seu nome completo");
		f.nome.focus();
	}else if(!validarEmail(f.email.value)){
		alert("Digite um e-mail válido");
		f.email.focus();
	}else if(validarNumero(f.ddd.value) == false || f.ddd.value == "0"){
		alert("Digite um DDD válido");
		f.ddd.focus();
	}else if(f.telefone.value == ""){
		alert("Digite seu numero de telefone");
		f.telefone.focus();
	}else if(f.mensagem.value == ""){
		alert("Digite a sua mensagem");
		f.mensagem.focus();
	}else{
		eId("enviar").value = "Enviando...";
		eId("enviar").disabled = true;
		f.submit();
	}
}

var calculo = new Array(26);
<!-- Autor: Francisco C Paulino - Tofinha (fcptofinha@globo.com) -->
<!-- Data: 08/11/2002 - 11:55hs -->
<!-- Script que formata Valores em reais ao digitar -->

<!-- Início da Função FormataReais -->
function FormataReais(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var nam = fld.name;
	var n 	=  strReplace("punit_", "", nam);
	var whichCode = (window.Event) ? e.which : e.keyCode;
	 if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
	return true;
	key = String.fromCharCode(whichCode);  // Valor para o código da Chave
	if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	calculo[n] = fld.value;
	return false;
}
//Fim da Função FormataReais -->

function strReplace(s1, s2, texto){
	return texto.split(s1).join(s2);
}

function float2moeda(num) {
	x = 0;
	
	if(num<0) {
	num = Math.abs(num);
	x = 1;
	} if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	
	num = Math.floor((num*100+0.5)/100).toString();
	
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'
	+num.substring(num.length-(4*i+3)); ret = num + ',' + cents; if (x == 1) ret = ' - ' + ret;return ret;

}

function moeda2float(moeda){
	moeda = moeda.strReplace(".","");
	moeda = moeda.strReplace(",",".");
	return parseFloat(moeda);
}

function soNum(e){
	var strCheck = '0123456789';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
	return true;
	var key = String.fromCharCode(whichCode);  // Valor para o código da Chave
	if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
}

function carregarSub(id){
	janela.location.href = "subcategoria.php?id="+id;
}

function subIframe(){
	if(document.getElementById('id_subcategoria') && janela.document.body.innerHTML != ''){
		document.getElementById('id_subcategoria').innerHTML = janela.document.body.innerHTML;
		document.getElementById('id_subcategoria').disabled=false;
	}
}

function add(){
	var html = "";
	var n 	 = parseInt(document.getElementById("n").value);
	if(n != 9){
		n = n + 1;
		for(var i = 0; i < n; i++){
			html = html + '<table width="500" border="0" cellspacing="0" cellpadding="0">';
			html = html + ' <tr>';
			html = html + '<td width="249" align="right">Foto ' + (i+1) + ': </td>';
			html = html + ' <td width="251" align="left"><input name="fotos[]" type="file" id="fotos[]"></td>';
			html = html + ' </tr>';
			html = html + ' <tr>';
			html = html + ' <td align="right">Legenda ' + (i+1) + ': </td>';
			html = html + ' <td align="left"><input name="legendas[]" type="text" id="legendas[]"></td>';
			html = html + '</tr>';
			html = html + ' </table>		';	
		}	
			document.getElementById("n").value = n;
			document.getElementById("up_fotos").innerHTML = html;
		}
	if(n == 9){
		document.getElementById("bt_add").disabled = true;
	}else{
		document.getElementById("bt_add").disabled = false;
		document.getElementById("bt_del").disabled = false;
	}
}

function del(){

	var html  = "";
	var n 	  = parseInt(document.getElementById("n").value);
	var ativo = 0;
		n = (n - 1);
		for(var i = 0; i < n; i++){
			html = html + '<table width="500" border="0" cellspacing="0" cellpadding="0">';
			html = html + ' <tr>';
			html = html + '<td width="249" align="right">Foto ' + (i+1) + ': </td>';
			html = html + ' <td width="251" align="left"><input name="fotos[]" type="file" id="fotos[]"></td>';
			html = html + ' </tr>';
			html = html + ' <tr>';
			html = html + ' <td align="right">Legenda ' + (i+1) + ': </td>';
			html = html + ' <td align="left"><input name="legendas[]" type="text" id="legendas[]"></td>';
			html = html + '</tr>';
			html = html + ' </table>		';	
				ativo = 1;
		}
		if(ativo){
			document.getElementById("n").value 		= n;
			document.getElementById("up_fotos").innerHTML = html;
		}
	if(n == 1){
		document.getElementById("bt_del").disabled = true;
	}else{
		document.getElementById("bt_del").disabled = false;
		document.getElementById("bt_add").disabled = false;
	}
}

function Num(v){  
	 v.value = v.value.replace(/\D/g,"");
	 if(v.value == ""){
	 	v.value = 1;
	 }
}

