﻿function Redimensionar() {
    vConteudo = document.getElementById('conteudo')
    //vForm = document.getElementById('aspnetForm')
    var size2 = document.documentElement.clientHeight;
    vHtml = document.body.parentNode;
    if (vConteudo != null)
    {
        var cliSize = vHtml.clientHeight;
        if (cliSize < 400)
            vConteudo.style.height = '400px';
        else
            vConteudo.style.height = (cliSize) + 'px';

        if (vHtml.clientWidth < 700)
            document.body.style.width = '700px';
        //else
            //document.body.style.width = '100%';
    }
}

function checkUncheckAll(theElement)
{
    var theForm = theElement.form, z = 0;
    for(z=0; z<theForm.length;z++)
    {
        if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
        {
	        theForm[z].checked = theElement.checked;
	    }
    }
}



function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

/*
*/
function ValidaDataAuto(oSrc, args)
{
    var texto = new String(args.Value);
    var size = texto.length;
    if (size == 10)
    {
        args.IsValid = verifica_data(texto);
    }
    else 
    {
        args.IsValid = false;
    }
}

/* esconde os itens de um elemento de id em ctohide.
 * dependendo do valor booleano bHide
*/
function HideElement(ctohide, bHide) 
{
    var vVisible = document.getElementById(ctohide);
    if (bHide)
    {
        vVisible.style.visibility = 'visible';
    }
    else
    {
        vVisible.style.visibility = 'hidden';
    }
}


/*Formata um campo para o formato de Data xx/xx/xxxx
*/
function FormataData(campo, teclapres)
{
    var position = doGetCaretPosition(campo);
    var tecla = teclapres.keyCode;
    
    if (tecla == 37 || tecla == 39) return;
    
    var strini = new String(campo.value);
    var vr = new String(campo.value);
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    
    vr = CortaNaoNumericos(vr);
    var tam = vr.length;
    
    var result = vr.substr(0, 2);
    if (tam > 2) result += "/" + vr.substr(2, 2);
    if (tam > 4) result += "/" + vr.substr(4, 4);
    //if (tam == 8) if (!verifica_data(result)) ;
    
    campo.value = result;
    if (position < strini.length)
    {
        setCaretPosition(campo, position);
    }
}

function FormataHoraMin(campo, teclapres) {
    var position = doGetCaretPosition(campo);
    var tecla = teclapres.keyCode;

    if (tecla == 37 || tecla == 39) return;

    var strini = new String(campo.value);
    var vr = new String(campo.value);
    vr = vr.replace(":", "");
    vr = vr.replace(":", "");

    vr = CortaNaoNumericos(vr);
    var tam = vr.length;

    var result = vr.substr(0, 2);
    if (tam > 2) result += ":" + vr.substr(2, 2);
    //if (tam == 8) if (!verifica_data(result)) ;

    campo.value = result;
    if (position < strini.length) {
        setCaretPosition(campo, position);
    }
}

function FormataValorMonetario(campo, teclapres)
{
    var position = doGetCaretPosition(campo);
    var tecla = teclapres.keyCode;
    
    if (tecla == 37 || tecla == 39) return;
    
    var strini = new String(campo.value);
    var vr = new String(campo.value);
    vr = vr.replace(".", "");
    vr = vr.replace(",", "");
    
    vr = CortaNaoNumericos(vr);
    
    while (vr.substr(0,1) == "0")
    {
        if (vr == "0") 
        {
            vr = "000";
            break;
        }
        vr = vr.substr(1);
    }

    var tam = vr.length;
    
    var result = vr.substr(tam - 2, 2);
    if (tam > 2) result = vr.substr(0, tam - 2) + ","+ result;
    if (tam == 1) result = "0,0" + result;
    if (tam == 2) result = "0," + result;
    //if (tam == 8) if (!verifica_data(result)) ;
    
    campo.value = result;
    if (position < strini.length)
    {
        setCaretPosition(campo, position);
    }
}

/* pega a posição do cursor do controle
*/
function doGetCaretPosition (ctrl) {
	var CaretPos = 0;	// IE Support
	if (document.selection) {
	ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}

/* Seta a posição do cursor do controle
*/
function setCaretPosition(ctrl, pos){
	if(ctrl.setSelectionRange)
	{
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}
	else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}

/* Verifica se a data está correta.
*/
function verifica_data (strdata)
{
    var dia = strdata.substring(0,2); 
    var mes = strdata.substring(3,5); 
    var ano = strdata.substring(6,10);
    
    if (strdata == "") situacao = false;

    var situacao = true; 
    // verifica o dia valido para cada mes 
    if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
        situacao = false; 
    } 

    // verifica se o mes e valido 
    if (mes < 01 || mes > 12 ) { 
        situacao = false; 
    } 

    // verifica se e ano bissexto 
    if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
        situacao = false; 
    } 

    if (strdata == "") { 
        situacao = false; 
    } 

    if (!situacao) { 
        alert("Data inválida!"); 
        return false;
    } 
    return true;
} 


/* Corta todos os textos que não são números
*/
function CortaNaoNumericos(texto)
{
    var str = texto + '';
    var rgx = /^\d|\.|-$/;
    var out = '';
    for( var i = 0; i < str.length; i++ )
    {
        if( rgx.test( str.charAt(i) ) ){
            if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
                ( str.charAt(i) == '-' && out.length != 0 ) ) ){
                out += str.charAt(i);
            }
        }
    }
    return out;
}


function DoPrint(){
    if (!window.print){
        alert("Não foi possível imprimir, vá em imprimir pelo navegador.");
        return;
    }
    window.print();
}




