
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"
}

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=\'admin/imagens/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,width,botoes){
	botoes = botoes?botoes:'<input type="button" class="botao" value="Fechar" onclick="$.iModal.close();">';
	$.iModal({ title:title, type:'ajax',url: url, 'width':width,buttons:botoes });
}

function changeElementValue(obj,value){
	obj = document.getElementById(obj);
	obj.innerHTML = value;
}

function addFavorites(site,titulo){
    var url      = site;
    var title    = titulo;
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

function checkRequiredFields(){
	var requiredFields = $('.FieldRequired');
	var valid = true;
	
	requiredFields.each(function() {
															 
		var namePart = this.name.replace(/^.*\[/, '');
		var fieldId = namePart.replace(/\].*$/, '');
		if(this.type=='checkbox' ) {
			if(!this.checked) {
				valid = false;
				alert("É necessário preencher todos os campos obrigatórios!");
				this.focus();
				this.select();
				return false;
			}
		} else if(this.value == '') {
				valid = false;
				alert("É necessário preencher todos os campos obrigatórios!");
				this.focus();
				this.select();
				return false;
		}
	});
	return valid;
}