$(function(){
	if (jQuery.browser.msie) {
		if(parseInt(jQuery.browser.version) == 6){
			$("li")
				.hover(
					function(){ $(this).addClass("hover") },
					function(){ $(this).removeClass("hover") }
				)
		}
	}
	
	$('#pesquisar').click(function() {
		popup('popup.asp',796,676);
		return false;
	})
	
	$('#loginusuario,#loginsenha').focus(function(){
		if ($(this).val() == 'Usuário' || $(this).val() == 'Senha') {
			$(this).val('');
		}
	})
	
	$('#nav_topo a.favoritos').click(function() {
	   if(parseInt(jQuery.browser.version) >= 4 && jQuery.browser.msie) {
			AdicionarFavoritos()
		}
		else {
			CreateBookmarkLink()
		}
	})
});

function popup(pagina,larg,alt) {
    var left = (window.screen.width/2)-(larg/2);
    var top = (window.screen.height/2)-(alt/2);
    
    var new_window = window.open(pagina,'pagina','width='+larg+',height='+alt+',scrollbars=YES,Resizable=YES,Directories=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO,Left='+left+',Top='+top);
    new_window.focus();
}

function AdicionarFavoritos() {
    if (navigator.appName == "Microsoft Internet Explorer" &&
        parseInt(navigator.appVersion) >= 4) {
        window.external.AddFavorite(document.location, "Costeira");
    }
}

function CreateBookmarkLink() {
    title = "Costeira";
    url = "http://www.costeira.com.br/";
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (window.opera && window.print){
        return true;
    }
}

	///////////////////////////////////////////////////////////////////////////////////
	function pula(campo, tamT, Proximo){

		var vr = new String(campo.value);

		tam = vr.length;
		if (tam == tamT){
				if(Proximo){
					Proximo.select();
					Proximo.focus();
				}
			}

	}
	
	/*/////////////////////////////////////////////////////////////////////////////////
	// Validar a data
	/////////////////////////////////////////////////////////////////////////////////*/
	function ValidaData(dateStr) {
		if (dateStr.length==0){
			return true;
		}
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat); // is the format ok?
		
		if (matchArray == null) {
			alert("O formato da data é 00/00/0000");
			return false;
		}
		
		day = matchArray[1]; // p@rse date into variables
		month = matchArray[3];
		year = matchArray[5];
		
		if (month < 1 || month > 12) { // check month range
			alert("Mês deve ser entre 1 a 12");
			return false;
		}
		
		if (day < 1 || day > 31) {
			alert("Dia deve ser entre 1 e 31.");
			return false;
		}
		
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			alert("Mês "+month+" não tem 31 dias.")
			return false;
		}
		
		if (month == 2) { // check for february 29th
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day==29 && !isleap)) {
				alert("Fevereiro " + year + " não tem " + day + " dias!");
				return false;
			}
		}
		return true; // date is valid
	}
	/*/////////////////////////////////////////////////////////////////////////////////
	// Coloca o campo no formato de Hora
	/////////////////////////////////////////////////////////////////////////////////*/
	function FormataHora(Campo, Tecla, Proximo){
		var Tecla = Tecla.keyCode;
	
		var vr = new String(Campo.value);
		vr = vr.replace(":", "");

		tam = vr.length;

		if (Tecla != 9 && Tecla != 8 && Tecla != 46){
			if (tam >= 2 && tam < 5)
				Campo.value = vr.substr(0,2) + ':' + vr.substr(2,2);
			if (tam == 5){
				if(Proximo){
					Proximo.select();
					Proximo.focus();
				}
			}
		}
	}
	
	/*/////////////////////////////////////////////////////////////////////////////////
	// Coloca o campo no formato de Data
	/////////////////////////////////////////////////////////////////////////////////*/
	function FormataData(Campo, Tecla){
		var Tecla = Tecla.keyCode;
	
		var vr = new String(Campo.value);
		vr = vr.replace("/", "");
		vr = vr.replace("/", "");

		tam = vr.length;

		if (Tecla != 9 && Tecla != 8 && Tecla != 46){
			if (tam >= 2 && tam < 4)
				Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2);
			if (tam >= 4 && tam <= 6)
				Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);

		}
	}
	/*/////////////////////////////////////////////////////////////////////////////////
	// Coloca o campo no formato de CEP
	/////////////////////////////////////////////////////////////////////////////////*/
	function FormataCEP(Campo, Tecla, Proximo){
		var Tecla = Tecla.keyCode;
	
		var vr = new String(Campo.value);
		vr = vr.replace("-", "");

		tam = vr.length;

		if (Tecla != 9 && Tecla != 8 && Tecla != 46){
			if (tam >= 5 && tam < 9)
				Campo.value = vr.substr(0,5) + '-' + vr.substr(5,3);
			if (tam == 9){
				if(Proximo){
					Proximo.select();
					Proximo.focus();
				}
			}
		}
	}
	
	/*/////////////////////////////////////////////////////////////////////////////////
	// Coloca o campo no formato de TELEFONE 0000-0000
	/////////////////////////////////////////////////////////////////////////////////*/
	function FormataTELEFONE(Campo, Tecla){
		var Tecla = Tecla.keyCode;
	
		var vr = new String(Campo.value);
		vr = vr.replace("-", "");

		tam = vr.length;

		if (Tecla != 9 && Tecla != 8 && Tecla != 46){
			if (tam >= 4 && tam < 9)
				Campo.value = vr.substr(0,4) + '-' + vr.substr(4,4);
			if (tam == 9){
				if(Proximo){
					Proximo.select();
					Proximo.focus();
				}
			}
		}
	}
	/*/////////////////////////////////////////////////////////////////////////////////
	// Coloca o campo no formato CNPJ
	/////////////////////////////////////////////////////////////////////////////////*/
	function FormataCNPJ(Campo, Tecla,Proximo){
		var tecla = Tecla.keyCode;

		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("/", "");
		vr = vr.replace("-", "");

		tam = vr.length + 1 ;
		
		if (tecla != 9 && tecla != 8 && tecla != 46){
			if (tam > 2 && tam < 6)
				Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
			if (tam >= 6 && tam < 9)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
			if (tam >= 9 && tam < 13)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
			if (tam >= 13 && tam < 15)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
			if (vr.length == 14)
				if(Proximo){
					Proximo.select();
					Proximo.focus();
				}
		}
		
	}

	/*/////////////////////////////////////////////////////////////////////////////////
	// Coloca o campo no formato CNPJ
	/////////////////////////////////////////////////////////////////////////////////*/
	
	function FormataCPF(Campo, Tecla, Proximo){
		var tecla = Tecla.keyCode;
		var vr=new String(Campo.value);
		tam=vr.length;
		
		if (tecla != 9 && tecla != 8 && tecla != 46){
			if (tam==3)
				Campo.value=Campo.value + '.';
				
			if (tam==7)
				Campo.value=Campo.value + '.';
		
			if (tam==11)
				Campo.value=Campo.value + '-';
		}	
	
	}