// JavaScript Document

function barra(percent, idTabela) {
	document.getElementById(idTabela).style.width = percent;
	valor = "<div class=\"progText\">"+percent+"<\/div>";
	document.getElementById(idTabela).innerHTML = valor;
}


var popUpWin=0;
function popUpWindow(URLStr, width, height, top, left ) {
  if(popUpWin) {
    if(!popUpWin.closed) popUpWin.close();
	}
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}


function IsNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
         }
      }
   return IsNumber;
   }

function fAddCarrinho(imgId) {
	//document.getElementById("atualizando").className = "show";
	codProd = imgId.replace("img", "");
	quantId = "quant" + codProd;
	quant = escape(document.getElementById(quantId).value);

pAtualizando = "pAtualizando"+codProd;
document.getElementById(pAtualizando).className = 'hide';

	imgAtualizando = "imgAtualizando"+codProd;
  document.getElementById(imgAtualizando).className = 'show';

  if(!(IsNumeric(quant)) || quant=="" || quant==0) {
    quant = 1;
  }
	url = "../../web/php/if_add_single.php?cod="+codProd+"&quant="+quant;
	document.getElementById('ifCarrinho').src = url;
}

function fAddCestinha(imgId) {
	// USADO SOMENTE EM TRIGUEIRINHO - TITULOS CDS
	codProd = imgId.replace("img", "");
	url = "../../web/php/if_add_cestinha.php?cod="+codProd+"&quant=1";
	document.getElementById('ifCarrinho').src = url;
}

function fAddDoacao(imgId) {
	//document.getElementById("atualizando").className = "show";
	codProd = imgId.replace("imgDoacao", "");
	precoId = "quant" + codProd;
	//preco = escape(document.getElementById('doacao_gravacao').selectedIndex.value);

	var mylist=document.getElementById("doacao_gravacao");
preco=mylist.options[mylist.selectedIndex].value;


pAtualizando = "pAtualizandoDoacao"+codProd;
document.getElementById(pAtualizando).className = 'hide';

	imgAtualizando = "imgAtualizandoDoacao"+codProd;
  document.getElementById(imgAtualizando).className = 'show';
  if(!(IsNumeric(preco)) || preco=="") {
    preco = 1;
  }
	url = "../../web/php/if_add_single.php?cod="+codProd+"&doacao=S&preco="+preco;
	document.getElementById('ifCarrinho').src = url;
}




// Modified from Bill Dortch's Cookie Functions (hidaho.com) 
// (found in JavaScript Bible)
function setCookie(name,value,hours,path,domain,secure) {
  var expires, date;
  if (typeof hours == "number") {
    date = new Date();
    //date.setTime( date.getTime() + (days*24*60*60*1000) );  // trocamos days por horas
    date.setTime( date.getTime() + (hours*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}




// Modified from Jesse Chisholm or Scott Andrew Lepera ?
// (found at both www.dansteinman.com/dynapi/ and www.scottandrew.com/junkyard/js/)
function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}


// from Bill Dortch's Cookie Functions (hidaho.com)
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}



function fRunIFrame(theFrame, url) {
	document.getElementById(theFrame).src = url;
}

function blinkIt() {
 if (!document.all) return;
 else {
   for(i=0;i<document.all.tags('blink').length;i++){
      s=document.all.tags('blink')[i];
      s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
   }
 }
}

function fHideAll(tag_name, class_name) {
	var divs = document.getElementsByTagName(tag_name);
	for (var i = 0; i < divs.length; i++) {
		var div = divs[i];
		if (div.className == class_name) {
			div.style.display = 'none';
		}
	}
}

function fToggleShow(id, tag_name, class_name) {
	fHideAll(tag_name, class_name);
	document.getElementById(id).style.display = 'inline';
	setTimeout( 'fHideAllBlink()', 3000 );
} 

function wait(msecs) {
	var start = new Date().getTime();
	var cur = start
	while(cur - start < msecs) 	{
		cur = new Date().getTime();
	}
}

function fHideAllBlink() {
	var divs = document.getElementsByTagName('p');
	for (var i = 0; i < divs.length; i++) {
		var div = divs[i];
		if (div.className == 'aguarde') {
			div.style.display = 'none';
		}
	}
}




function scrollToCoordinates() {
  window.scrollTo(0, 0);
} 

function fValidateForm(formId) {
	var theForm = document.getElementById(formId);
	for(var i=0; i<theForm.elements.length; i++){
		type = theForm.elements[i].type;
		var inputId = trim(theForm.elements[i].id);
		var inputValue = theForm.elements[i].value;
		if(type=="text" || type=="select-one" || type=="password" || type=="checkbox" || type=="radio" || type=="file") {
			for(var j=0; j<arrRequired.length; j++){
				var obrig = trim(arrRequired[j]);
				if(inputId==obrig && trim(inputValue)=="") {
					alert("O campo " + arrDesc[j] + " é obrigatório");
					scrollToCoordinates();
					document.getElementById(inputId).focus();
					return false;
				}
			}
		
			if(document.getElementById("mesmo_endereco").checked==false) {
				for(var k=0; k<arrRequiredEntrega.length; k++){
					var obrig = trim(arrRequiredEntrega[k]);
					if(inputId==obrig && trim(inputValue)=="") {
						alert("O campo " + arrDescEntrega[k] + " é obrigatório");
						scrollToCoordinates();
						document.getElementById(inputId).focus();
						return false;
					}
				}
			}
		}
	}
	return true;
}


function fDisableInputs(checkedBox, sectionId) {
  obj = document.getElementById(sectionId);
	tags = new Array("input","select");
	for(j=0; j < tags.length; j++) {
		inputs = obj.getElementsByTagName(tags[j]);
		for(var i=0; i < inputs.length; i++) {
			if(checkedBox.checked==true) {
				inputs[i].disabled=true;
				fAddClass(inputs[i], 'inputDisabled');
			} else {
				inputs[i].disabled=false;
				fRemoveClass(inputs[i], 'inputDisabled');
			}
		}
	}
} // end function



function fSetSelect(changedValue, theTarget, theFrame, url) {
  // configura o src do iframe que vai montar o campo select
	document.getElementById(theTarget).options.length = 0;
	var novo = document.createElement("option");
	novo.value = "";
	novo.text  = " *** Aguarde. Atualizando ... *** ";
	document.getElementById(theTarget).options.add(novo);
	var newUrl = url + "?targetId=" + theTarget + "&changedValue=" + changedValue; 
  document.getElementById(theFrame).src = newUrl;
}


function fCreateSelect(id) {
	table = document.getElementById("tabledata"); 
	rows = table.getElementsByTagName("tr");
	numRegistros = rows.length;
	// limpa options e cria opcao Selecionar inicial
	parent.document.getElementById(id).options.length = 0;
	var novo = document.createElement("option");
	novo.value = "";
	novo.text  = " ";
	parent.document.getElementById(id).options.add(novo);

var intNrOfTrs = table.tBodies[0].rows.length;
if(numRegistros > 0) {
		for(var i = 0 ; i < intNrOfTrs ; i++) {
			valor = table.tBodies[0].rows[i].cells[0].firstChild.nodeValue;
			descricao = table.tBodies[0].rows[i].cells[1].firstChild.nodeValue;
			var novo = document.createElement("option");
			novo.value = valor;
			novo.text  = descricao;
			parent.document.getElementById(id).options.add(novo);
		}
	}
}  // END FUNCTION



function setCheckedRadioInput(campo, valor) {
	var numForms = document.forms.length;
	for (var j = 0; j < numForms; j++) {
		for (var i = 0; i < document.forms[j].elements.length; i++) {
			if (document.forms[j].elements[i].name == campo & document.forms[j].elements[i].value == valor) {
					document.forms[j].elements[i].checked = true;
			}
		}
	}
}


function setActiveClass(id, cssClass) { 
	// cssClass eh a classe css que vai definir o link ativo no menu
	// set class do link ativo no menu de navegao
	var foo = "";
	if(document.getElementById(id)) {
		foo = document.getElementById(id);
		foo.className = cssClass;
	}
}

function setActiveMenuLink(prefix, cssClass) { 
	// PARA MENUS COM LINKS POR PAGINAS
	// prefix - p.ex: cab_ eh usado no id de cada link no menu - cab_index, cab_produtos, etc
	// cssClass eh a classe css que vai definir o link ativo no menu
	// chamada onLoad em todas as paginas TEMPLATE
	// set class do link ativo no menu de navegao
	var path_name=location.pathname;
	var base_name=path_name.lastIndexOf("/")+1;
	pagina = path_name.substring(base_name);
	pagina = pagina.replace( ".php", "" ); 
	id = prefix + pagina; 
	var foo = "";
	if(document.getElementById(id)) {
		foo = document.getElementById(id);
		foo.className = cssClass;
	}
}


function confirma(mensagem) {
     if ( confirm(mensagem)) {
          return true }
       else {         
          return false
     }
}

function abrir(URL, width, height) {
   //var width = 200;
   //var height = 250;
   var left = 99;
   var top = 99;
   window.open(URL,'janela', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=yes, fullscreen=no');
}


var newWindow = '';
function fPopup(url, width, height, top, left) {
	if (!newWindow.closed && newWindow.location) {
		newWindow.location.href = url;
	} else {
		//newWindow=window.open(url,'name','height=200,width=150');
		newWindow = window.open(url,'janela','width='+width+',height='+height+',top='+top+',left='+left+',scrollbars=yes,status=no,toolbar=no,location=no,directories=no,menubar=no,resizable=yes,fullscreen=no');	
		if (!newWindow.opener) newWindow.opener = self;
	}
	if (window.focus) {newWindow.focus()}
	return false;
}


function jumpto(url_list){
   var URL = url_list.options[url_list.selectedIndex].value;
   window.location.href = escape(URL);
}

function fUrlOpen(url) {
   window.location.href = url;
}

/*function fHasClass(ele, cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function fAddClass(ele, cls) {
	if (!this.fHasClass(ele, cls)) ele.className += " "+cls;
}
function fRemoveClass(ele, cls) {
	if (fHasClass(ele, cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}
*/
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function fAddClass(obj, newClass) {
	//document.getElementById(id).style.background = 'none';
  obj.className += " "+newClass;
	//document.getElementById(id).className = 'inputFocus';
}

function fRemoveClass(obj, newClass) {
	obj.className = obj.className.replace(newClass,'');
	obj.className = obj.className.replace(/\s+/,' ');
}
function filesize (url) {
    // http://kevin.vanzonneveld.net
    // +   original by: Enrique Gonzalez
    // +      input by: Jani Hartikainen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: T. Wild
    // %        note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain.
    // %        note 1: Synchronous so may lock up browser, mainly here for study purposes.
    // *     example 1: filesize('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
    // *     returns 1: '3'

    var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    if (!req) {throw new Error('XMLHttpRequest not supported');}

    req.open('HEAD', url, false);
    req.send(null);

    if (!req.getResponseHeader) {
        try {
            throw new Error('No getResponseHeader!');
        } catch (e){
            return false;
        }
    } else if (!req.getResponseHeader('Content-Length')) {
        try {
            throw new Error('No Content-Length!');
        } catch (e2){
            return false;
        }
    } else {
        return req.getResponseHeader('Content-Length');
    }
}


/*function fAddClass(id, newClass) {
	//document.getElementById(id).style.background = 'none';
  document.getElementById(id).className += " "+newClass;
	//document.getElementById(id).className = 'inputFocus';
}

function fRemoveClass(id, newClass) {
	document.getElementById(id).className = document.getElementById(id).className.replace(newClass,'');
	document.getElementById(id).className = document.getElementById(id).className.replace(/\s+/,' ');
}
*/
