// JavaScript Document

//BROWSER DETECTION
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
//END BROWSER DETECTION



//AJAX
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
}
// FIM DE AJAX

function notclick() {
	if (event.button==2||event.button==3) {
	 oncontextmenu='return false';
	  }
}
//document.onmousedown=notclick
//document.oncontextmenu = new Function("return false;")


function txtlogin_over(txt,name){
	if(txt=='login'){
		txtlogin.value='';
		return;
	}
	if(txt==''){
		txtlogin.value=name;
		return;
	}
	
}
function txtsenha_over(txt,name){
	if(txt=='senha'){
		senha.value='';
		return;
	}
	if(txt==''){
		senha.value=name;
		return;
	}
	
}


function Ccount(fd) {
(fd.value.length>500) ? fd.value=fd.value.substring(0,500) : document.getElementById('caracteres').childNodes[0].data=(500-fd.value.length);
}
function ontime(){
	document.getElementById('time1').style.display = 'block';
}
function outtime(){
	document.getElementById('time1').style.display = 'none';
}

function popup(url,name,w,h,s,r){	
	window.open(url,name,"height="+h+",width="+w+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars="+s+",resizable="+r)
}

function autoTab(input,len, e) {
	var isNN = (navigator.appName.indexOf("Netscape")!=-1);	
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
		}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
			found = true;
			else
			index++;
			return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
			return index;
		}
	return true;
}
function somente_numeros(){
if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;
}
function checkForm(form)
	{
	if (form.action[0].checked)
		return checkEmail(form.email);
	else
		return checkEmail(form.email) && checkPassword(form.senha) ;	
	}

function checkPassword(PasswordTextObjetc)
{
	/**** OG 01/30/2000 - password length changed to 4 */
    if (PasswordTextObjetc.value.length < 4) {
		alert("A Senha deve ter mais de 4 caracteres");
		PasswordTextObjetc.select();
		PasswordTextObjetc.focus();
		return false;
	}	
}
function popup(url,name,w,h,s,r){	
	window.open(url,name,"height="+h+",width="+w+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars="+s+",resizable="+r)
}

//funçoes do popup do carrinho
function checkLocation() {
	if (navigator.appName == "Netscape") {			
		if(document.getElementById('div_popup_cart').style.display==''){
			//alert('Netscape');
			document.getElementById('div_popup_cart').style.top=window.pageYOffset+135;		
			}
		}
	else {
		if(document.getElementById('div_popup_cart').style.display==''){
			//alert('Outros');
			document.getElementById('div_popup_cart').style.pixelTop=document.body.scrollTop+135;
			}
		}
	setTimeout("checkLocation()",1);
}
function validacpf(obj_cpf){ 
  var i; 
  s = obj_cpf.value; 
  s=s.replace(".", "") ; //primeiro ponto
  s=s.replace(".", "") ; //segundo ponto
  s=s.replace("-", "") ;

  var c = s.substr(0,9); 
  var dv = s.substr(9,2); 
  var d1 = 0; 
  for (i = 0; i < 9; i++){ 
  	d1 += c.charAt(i)*(10-i); 
  	} 
  if (d1 == 0){ 
  	alert("CPF Invalido! Verifique o que foi digitado. Por favor digite apenas os números!");
	obj_cpf.value='';
	obj_cpf.focus();
  	return false; 
  	} 
  d1 = 11 - (d1 % 11); 
  if (d1 > 9) d1 = 0; 
  if (dv.charAt(0) != d1){   
	alert("CPF Invalido!! Verifique o que foi digitado. Por favor digite apenas os números!");
	obj_cpf.value='';
	obj_cpf.focus();
  	return false; 
  	} 
  d1 *= 2; 
  for (i = 0; i < 9; i++){ 
  	d1 += c.charAt(i)*(11-i); 
  	} 
  d1 = 11 - (d1 % 11); 
  if (d1 > 9) d1 = 0; 
  if (dv.charAt(1) != d1){ 
  	alert("CPF Invalido!!! Verifique o que foi digitado. Por favor digite apenas os números!");
	obj_cpf.value='';
	obj_cpf.focus();
  	return false; 
  	}
  else{
  	return true;
	}
}


function checkEmail(obj){
	var EmailText=obj.value;
	if ((EmailText.search(/;/i) == -1) &&
	    (EmailText.search(/,/i) == -1) &&
	    (EmailText.indexOf("..") == -1) &&
	    (EmailText.indexOf(".@") == -1) &&
	    (EmailText.indexOf("@.") == -1) &&
	    (EmailText.search(/ /i) == -1) &&
	    (EmailText.search(/"/i) == -1) &&
	    (EmailText.search(/'/i) == -1) &&
	    (EmailText.indexOf("^") == -1) &&
	    (EmailText.search(/`/i) == -1) &&
	    (EmailText.search(/~/i) == -1) &&
	    (EmailText.search(/ç/i) == -1) &&
	    (EmailText.length != 0) &&
	    (EmailText.search(/@/i) >= 1) &&
	    (EmailText.substr(0,1) != ".") && /*Não pode começar com ponto*/
		(EmailText.substr(0,1) != "@") && /*Não pode começar com arroba*/
	    (EmailText.substr(EmailText.length-1) != ".") && /*Não pode terminar com ponto*/
		(EmailText.substr(EmailText.length-1) != "@") && /*Não pode terminar com arroba*/
	    (EmailText.indexOf("hotmail.com.br") == -1) &&
	    (EmailText.indexOf("@aol.com.br") == -1))
		return true;
	else if (EmailText.indexOf("hotmail.com.br") >= 0)
		{
		alert("Atenção: O E-mail HOTMAIL não termina com .BR\nEle será automaticamente corrigido");
		obj.value = EmailText.replace("hotmail.com.br", "hotmail.com");
		obj.focus();
		return false;
		}
	else if (EmailText.indexOf("aol.com.br") >= 0)
		{
		alert("Atenção: O E-mail AOL não termina com .BR\nEle será automaticamente corrigido");
		obj.value = EmailText.replace("aol.com.br", "aol.com");
		obj.focus();
		return false;
		}
	else if (EmailText.indexOf(" ") >= 0)
		{
		alert("Atenção: O E-mail não deve conter espaços em branco\nEle será automaticamente corrigido");
		while(EmailText.indexOf(" ") >= 0)
			EmailText= EmailText.replace(" ", "");
		obj.value = EmailText
		obj.focus();
		return false;
		}
	else
	    {
		alert("Formato do endereço de E-mail Incorreto!");
		obj.select();
		obj.focus();
		return false; 
	    }
}

function VerificaData(digData) {
	var bissexto = 0;
	var data = digData;
	var tam = data.length;
	if (tam == 10) {
		var dia = data.substr(0,2)
		var mes = data.substr(3,2)
		var ano = data.substr(6,4)
		if ((ano > 1900)||(ano < 2100)){
			switch (mes){
				case '01':
				case '03':
				case '05':
				case '07':
				case '08':
				case '10':
				case '12':
					if  (dia <= 31){
						return true;
					}
				break
		
				case '04':        
				case '06':
				case '09':
				case '11':
					if  (dia <= 30) {
						return true;
					}
				break
		
				case '02':
				/* Validando ano Bissexto / fevereiro / dia */
					if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)){
						bissexto = 1;
					}
					if ((bissexto == 1) && (dia <= 29)) {
						return true;                
					}
					if ((bissexto != 1) && (dia <= 28)) {
						return true;
					}            
				break                        
			}
		}
	}    
	alert("A Data "+data+" é inválida!");
	return false;
}

function AvisoAlert(text) {
	alert(text)
}

function checkSubmitRecados(varUrl,varContent,varDiv,varData) {
	
	if (document.form.recado.value == ''){
		alert('O recado é obrigatório!');
		aux = false;
	}
	else{
		aux = true;
	}
		
	if (aux == true) {
		varRecado = document.getElementById("recado").value;
		varRecado = replaceAll(varRecado," ","||")
		varRecado2 = escape(varRecado);
		varData = "id="+document.getElementById("id").value+"&recado="+varRecado+"&recadoCod="+varRecado2;
		SendData(varUrl,varContent,varDiv,varData);
	}
}


function checkSubmitBugReport(varUrl,varContent,varDiv,varData) {
	
	aux = true;
	
	if (aux == true) {
		varComments = replaceAll(document.getElementById("comments").value," ","||")
		var varData = "comments="+varComments;
		varData = varData +"&bt="+document.getElementById("bt").value;
		
		SendData(varUrl,varContent,varDiv,varData);
	}
}

function checkSubmitLogin(varUrl,varContent,varDiv,varData) {
	
	
	aux = true;

	if (document.getElementById("email").value == ''){
		alert('O email é obrigatório!');
		aux = false;
	}
	else if (document.getElementById("senha").value == '' && aux == true){
		alert('A senha é obrigatória!');
		aux = false;
	}
	else{
		aux = true;
	}
		
	if (aux == true) {
		var varData = "email="+document.getElementById("email").value;
		varData = varData +"&senha="+document.getElementById("senha").value;
		varData = varData +"&url="+document.getElementById("url").value;
		
		SendData(varUrl,varContent,varDiv,varData);
	}
}

function openBugReport() {
	if (document.getElementById("tberror").style.display == '') {
		document.getElementById("tberror").style.display = "none";
	}
	else {
		document.getElementById("tberror").style.display = "";
	}

}

//Retorna em um elemento div, span, p
function textCounter(field, countfield, maxlimit) {
  	if (field != null) {
		if (field.value.length > maxlimit) {
			field.value = field.value.substring(0, maxlimit);
		}
		else {
			if (countfield != null) {
				countfield.innerHTML = field.value.length;
			}
		}
	}
}

//Retorna em um campo texto
function textCounterField(field, countfield, maxlimit) {
	if (field != null) {
		if (field.value.length > maxlimit) {
			field.value = field.value.substring(0, maxlimit);
		}
		else {
			if (countfield != null) {
				countfield.value = field.value.length;
			}
		}
	}
}

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}

function checkSubmitLoginForum(varUrl,varContent,varDiv,varData) {
		
	aux = true;

	if (document.getElementById("email").value == ''){
		alert('O email é obrigatório!');
		aux = false;
	}
	else if (document.getElementById("senha").value == '' && aux == true){
		alert('A senha é obrigatória!');
		aux = false;
	}
	else{
		aux = true;
	}
		
	if (aux == true) {
		if (document.getElementById(document.getElementById("divRetorno")) != null) {
			vardiv = document.getElementById("divRetorno").value;
			document.getElementById(varDiv).style.display = '';
		}
		var varData = "email="+document.getElementById("email").value;
		varData = varData +"&senha="+document.getElementById("senha").value;
		varData = varData +"&url="+document.getElementById("url").value;
		varData = varData +"&divRetorno="+document.getElementById("divRetorno").value;
		
		SendData(varUrl,varContent,varDiv,varData);
	}
}


