function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
function abrepopup(URL,Width,Height){
  Left = (window.screen.availWidth - Width) / 2 - 13;
	Top  = (window.screen.availHeight - Height) / 2
	window.open(URL,'Popup', 'width='+Width+', height='+Height+', top='+Top+', left='+Left+', scrollbars=yes, status=yes, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

function abrebanner(URL,Width,Height){
	if (window.navigator.userAgent.indexOf('MSIE 6.0') != -1 && window.navigator.userAgent.indexOf('SV1') != -1) { 
						 i=17; //IE 6.x on Windows XP SP2
	} else if (window.navigator.userAgent.indexOf('MSIE 6.0') != -1) { 
						 i=17; //IE 6.x somewhere else
	} else if (window.navigator.userAgent.indexOf('MSIE 7.0') != -1) { 
						 i=0;  //IE 7.x 
	} else if (window.navigator.userAgent.indexOf('Firefox') != -1 && window.navigator.userAgent.indexOf("Windows") != -1) { 
						 i=2; //Firefox on Windows
	} else if (window.navigator.userAgent.indexOf('Mozilla') != -1 && window.navigator.userAgent.indexOf("Windows") != -1 && window.navigator.userAgent.indexOf("MSIE") == -1) { 
						 i=2; //Mozilla on Windows, but not IE7		
	} else if (window.opera && document.childNodes) {
						 i=2; //Opera 7+
	} else if (navigator.vendor == 'KDE' && window.navigator.userAgent.indexOf("Konqueror") != -1) {
										 i=-4; //Konqueror- this works ok with small images but not so great with large ones
					 //if you tweak it make sure i remains negative
	} else { 
						 i=70; //All other browsers
	}
	Width = Width+i;
	Height = Height+3;
	window.open(URL,'Popup', 'width='+Width+', height='+Height+', top=60%,left=60%, scrollbars=yes, status=yes, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

function rowOverEffect(object) {
  if (object.className == 'linha_comum') object.className = 'linha_selecionada';
}

function rowOutEffect(object) {
  if (object.className == 'linha_selecionada' && object.id == "") object.className = 'linha_comum';
}
function hidePopup()
{
 document.getElementById('banner_flat').style.display="none"
 for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length-3, imgName.length) == "GIF"){
			img.src = img.src;
		}
	}	
}

/*** 
* Descrição.: formata um campo do formulário de 
* acordo com a máscara informada... 
* Parâmetros: - objForm (o Objeto Form) 
* - strField (string contendo o nome do textbox) 
* - sMask (mascara que define o 
* formato que o dado será apresentado, usando o algarismo "9" para definir números e o símbolo "!" para qualquer caracter... 
* - evtKeyPress (evento) 
* Uso.......: <input type="textbox" 
* name="xxx"..... 
* onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);"> 
* Observação: As máscaras podem ser representadas como os exemplos abaixo: 
* CEP -> 99.999-999 
* CPF -> 999.999.999-99 
* CNPJ -> 99.999.999/9999-99 
* Data -> 99/99/9999 
* Tel Resid -> (99) 999-9999 
* Tel Cel -> (99) 9999-9999 
* Processo -> 99.999999999/999-99 
* C/C -> 999999-! 
* E por aí vai... 
***/
function txtFormat(strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	var isNN = (navigator.appName.indexOf("Netscape")!=-1);
	nTecla = (isNN) ? evtKeyPress.which : evtKeyPress.keyCode;
	sValue = strField.value;
	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ",", "" );
	sValue = sValue.toString().replace( ",", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;
	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " ") || (sMask.charAt(i) == ","))
		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++; }
		else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}
		i++;
	}
	if (nTecla != 8) { // backspace
		strField.value = sCod;
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return (((nTecla > 47) && (nTecla < 58)) || (nTecla == 0)); } // números de 0 a 9
		else { // qualquer caracter...
			return true;
		} }
	else {
		return true;
	}
}
//Fim da Função Máscaras Gerais
//-->
function exibearea( sheet  )  { document.getElementById(sheet).style.display = 'block'; }
function ocultaarea( sheet  ) { document.getElementById(sheet).style.display = 'none'; }


function loadingAjax(obj,msg,float){
	float = float || 'left';
	msg = msg || "Carregando...";
	if (document.getElementById(obj)){
		document.getElementById(obj).innerHTML = "<div style='float:"+float+";'><img src=\'http://p11.hostingprod.com/@com.py/files/loading.gif\' style=\'margin-right:8px;float:left;vertical-align:top;\'><font color=\'#000000\' face='Trebuchet MS, Arial' size=2>"+msg+"</font></div>";
	}
}

function validaNewsletter(){ 
		if (document.newsletter.email_address.value.length < 6) { alert("Informe o E-MAIL para cadastro de newsletter"); return false; }
		else if (!checkMail(document.newsletter.email_address.value)) { alert("Informe um endereço de e-mail VÁLIDO para cadastro de newsletter."); return false; }
		else return true;
}
var conteudo_notifica_produto = "";
function salvaNotificaoProduto(id){
	  conteudo_notifica_produto = document.getElementById("notificao_produto").innerHTML;
		var notifica_nome = document.getElementById("notifica_nome").value;
		var notifica_email = document.getElementById("notifica_email").value;
		var notifica_news = document.getElementById("newsletter").checked;
		if (notifica_nome.length < 2 ) { alert("Informe o seu NOME para cadastro."); document.getElementById("notifica_nome").focus(); }
		else if (notifica_email.length < 5 ) { alert("Informe o E-MAIL para cadastro."); document.getElementById("notifica_email").focus();  }
		else if (!checkMail(notifica_email)) { alert("Informe um endereço de E-MAIL VÁLIDO para cadastro."); document.getElementById("notifica_email").focus();  }
		else {
			loadingAjax("notificao_produto","Aguarde...");
			$.post('index.php?action=notify',
				{ id_produto : id, nome : notifica_nome, email: notifica_email, newsletter : notifica_news },
					function(resposta){  
						if (resposta!="")
							document.getElementById("notificao_produto").innerHTML = "<center><b>"+resposta+"</b><br><input type='button' name='bt_volta' value='OK' onClick='document.getElementById(\"notificao_produto\").innerHTML = conteudo_notifica_produto;'></center>";
						else
							document.getElementById("notificao_produto").innerHTML = "<b>Seu contato foi registrado com sucesso!</b><br>Quando o produto estiver novamente disponível você receberá uma notificação.";
					}
				);
			
		}
		return false;
}


function abreDialog(title,url,maximize,modal,resizable,width,height){

	var el = document.getElementById("dgl"); 
	if (el) el.parentNode.removeChild(el);

	var currentElement = document.createElement("div");
	currentElement.setAttribute("id", "dgl");
	currentElement.setAttribute("title", title);
	currentElement.innerHTML = '<div><img src="http://p11.hostingprod.com/@com.py/files/loading.gif" style="margin-right:8px;float:left;vertical-align:top;"><font color="#000000" face="Trebuchet MS, Arial" size=2>Carregando...</font></div>'; 
  document.body.appendChild(currentElement);

	$('#dgl').load(url).dialog({
		title: (title) ? title : '', 
		maximize: (maximize) ? maximize : false, 
		modal:(modal) ? modal : false,
		resizable:(resizable) ? resizable : false,
		width: (width) ? width : 600, 
		height: (height) ? height : 400,
		overlay:{background:'#000000', opacity:0.4}
  });
	$('#dgl').dialog('open');

}

function CalcularFrete(id_produto){ 
		abreDialog('Calcular frete de produto','popup_calcular_frete.php?id_produto='+id_produto,false,true,true,800,520);
}
function saibaComoPagar(id_produto){
		abreDialog('Saiba como pagar','popup_formas_pagamento.php?id_produto='+id_produto,false,true,true,400,500);
}
