/*-----------------------------------------------------------------------------+
| Funções Gerais JavaScript                                                    |
+-----------------------------------------------------------------------------*/

function popup(url,w,h){
	window.open(url,"","toobar=no,location=no,directories=no,maximize=no,menubar=no,scrollbars=yes,status=no,resizable=no,WIDTH="+w+",HEIGHT="+h+",top=0,left=50");
}

function roundNumber(preco) {
	var numberField = preco;
	var rlength = 2; // The number of decimal places to round to
	var newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}


function IsNumeric(sText)
{
   var ValidChars = "123456789 ";
   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;
}


// ------------------------------------------------------------------------------------------------------------------------------------------
ProdutosMenu = function() {
	if ( document.getElementById("navmenu") == null )
		return;

	var lis = document.getElementById("navmenu").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			this.className+=" iehover";
		}
		lis[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", ProdutosMenu);

// ------------------------------------------------------------------------------------------------------------------------------------------------

/* 
	Hover das imagens

	modo de utilização <img src="..." hsrc="...imagem em estado hover"  />

	no body : onLoad="sss_hover();"
*/
function sss_hover() {
  if (!document.getElementById) return
  var imgOriginSrc;
  var imgTemp = new Array();
  var imgarr = document.getElementsByTagName('img');
  for (var i = 0; i < imgarr.length; i++) {
    if (imgarr[i].getAttribute('hsrc')) {
        imgTemp[i] = new Image();
        imgTemp[i].src = imgarr[i].getAttribute('hsrc');
        imgarr[i].onmouseover = function() {
            imgOriginSrc = this.getAttribute('src');
            this.setAttribute('src',this.getAttribute('hsrc'))
        }
        imgarr[i].onmouseout = function() {
            this.setAttribute('src',imgOriginSrc)
        }
    }
  }
}


/* 
	Controla as dimensões de uma imagem a ser exibida.

	modo de utilização <img src="..." onLoad="limitaTamanho(this, comprimento, altura);" />
*/

function sss_limitaTamanho( obj, maxT, maxA ) {

	oldT = obj.width;
	oldA = obj.height;

	if ( obj.width > maxT ) {
		obj.width = maxT;
		obj.height = parseInt ( ( maxT * oldA ) / oldT );

		oldT = obj.width;
		oldA = obj.height;
	}

	if ( obj.height > maxA ) {
		obj.height = maxA;
		obj.width = parseInt ( ( maxA * oldT ) / oldA );
	}
}

/*
	Submit de um determinado form
*/

function sss_formSubmit( form ) {
	document.forms[form].submit();
}


function execPesquisa() {
	var tf = document.forms["pesquisa"];
	var chave = tf.chave.value;
	chave = chave.replace(new RegExp(/^\s+/),"");
	if (chave !="" )
	{
		tf.submit();
	}
	return false;
}

function execNewsletter() {
	var tf = document.forms["newsletter"];
	var chave = tf.email.value;
	chave = chave.replace(new RegExp(/^\s+/),"");
	if (chave !="" )
	{
		tf.submit();
	}
	return false;
}

function execLogin() {
	var tf = document.forms["login"];
	var usr = tf.usr.value;
	var pwd = tf.pwd.value;
	usr = usr.replace(new RegExp(/^\s+/),"");
	pwd = pwd.replace(new RegExp(/^\s+/),"");
	if (usr!="" && pwd !="" )
	{
		tf.submit();
	} else {
		alert("Ambos os campos são de preenchimento obrigatório.");
	}
	return false;
}

function  addFavoritos(url, title) {

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }




// ------------------------------------------------------------------------------------------------------------------------------------------
// Funções usadas pelo catalogo

function selectAllProducts( form, estado  ) {
	var tf = document.forms[form];
	var all = document.forms["SelectAll"].checkbox.checked;

	estado = ( all == true ) ? 1 : 0;

	for ( i = 0; i< tf.elements.length; i++)
	{
		if ( tf.elements[i].type == "checkbox" )
		{
			tf.elements[i].checked = estado;
		}
	}
}

function generatePDF(form, idC ) {
	var tf = document.forms[form];
	var total = 0;
	var strSel = "|";
	for ( i = 0; i< tf.elements.length; i++)
	{
		if ( tf.elements[i].type == "checkbox" )
		{
			if ( tf.elements[i].checked == 1 )
			{
				total++;
				strSel = strSel + tf.elements[i].value + "|";
			}
		}
	}

	if ( total == 0 )
	{
		$("#showInfo").show("slow");
	} else {
		window.open("/catalogo/gerapdf_lista.php?cat=" + idC + "&pdf=0&ids="+ strSel, "janelaPDF", "width=680, height=600");
		selectAllProducts( form, 0);
	}
}

function printListProducts(form, idC ) {
	var tf = document.forms[form];
	var total = 0;
	var strSel = "|";
	for ( i = 0; i< tf.elements.length; i++)
	{
		if ( tf.elements[i].type == "checkbox" )
		{
			if ( tf.elements[i].checked == 1 )
			{
				total++;
				strSel = strSel + tf.elements[i].value + "|";
			}
		}
	}

	if ( total == 0 )
	{
		$("#showInfo").show("slow");
	} else {
		window.open("/catalogo/imprime_lista.php?cat=" + idC + "&pdf=0&ids="+ strSel, "janelaPDF", "width=680, height=600");
		selectAllProducts( form, 0);
	}
}

function imprimeFicha(idP) {
	window.open("/catalogo/imprime_ficha.php?id=" + idP, "janelaPDF", "width=680, height=600");
}

function setindex()
{
	document.getElementById("sort").style.zIndex=1;
}