
/************************************************************************
*
*Decription: Formata Data em um textbox
*Author: Matheus Moreno
*
************************************************************************/
function FormataData(e,obj)
{
//usar no evento keypress
//bloqueia caracteres alfa e coloca as barras nas posições        
    
	
   	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
	
	//Backspace e Tab
	if(key != 8 && key != 0)
	{	 
	    goodChars = "0123456789";
	    if (goodChars.indexOf(keychar) != -1)	
	    {	    
		    if((obj.value.length == 2 || obj.value.length == 5) && key != 8)
		    {
		        obj.value += "/";		    
		    }
    		
		    vrRetorno = true;
	    }
	}
	else
	{
	    vrRetorno = true;
	}
	
	return vrRetorno;
}

/************************************************************************
*
*Decription: Formata Telefone
*Author: Matheus Moreno
*
************************************************************************/
function FormataTelefoneLibbs(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {
            //if (obj.value.length == 2)
                //obj.value = "(" + obj.value + ")";
            if (obj.value.length == 4)
                obj.value += "-";

            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }

    return vrRetorno;
}

/************************************************************************
*
*Decription: Formata Telefone
*Author: wnagasawa
*
************************************************************************/
function FormataDDD(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {
            if (obj.value.length == 2)
                obj.value = "(" + obj.value + ")";
            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }

    return vrRetorno;
}


/************************************************************************
*
*Decription: Formata Telefone
*Author: Matheus Moreno
*
************************************************************************/
function FormataTelefone(e,obj)
{
//usar no evento keypress
//bloqueia caracteres alfa e coloca as barras nas posições

	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
	
    if(key != 8 && key != 0)
	{
	    goodChars = "0123456789";
	    if (goodChars.indexOf(keychar) != -1)	
	    {
		    if(obj.value.length == 4)
		        obj.value += "-";
    		    
		     vrRetorno = true;
	    }	    
	}
	else
	{
	     vrRetorno = true;
	}
	
	return vrRetorno;	
}

/************************************************************************
*
*Decription: Formata CEP em um textbox
*Author: Matheus Moreno
*
************************************************************************/
function FormataCEP(e,obj)
{
//usar no evento keypress
//bloqueia caracteres alfa

	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
	
    if(key != 8 && key != 0)
	{
	    goodChars = "0123456789";
	    if (goodChars.indexOf(keychar) != -1) {
	        if (obj.value.length == 5)
	            obj.value += "-";
		    vrRetorno = true;
	    }	    
	}
	else
	{
	     vrRetorno = true;
	}
	
	return vrRetorno;	
}

/************************************************************************
*
*Decription: Formata CNPJ/CPF de acordo com um controle passado como parâmetro
*Author: Matheus Moreno
*
************************************************************************/
function FormataCPF(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições


    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

        if (key != 8 && key != 0) {
            goodChars = "0123456789";
            if (goodChars.indexOf(keychar) != -1) {

                if ((obj.value.length == 3 || obj.value.length == 7) && key != 8)
                { obj.value += "."; }
                if ((obj.value.length == 11) && key != 8)
                { obj.value += "-"; }

                vrRetorno = true;
            }
        }
        else {
            vrRetorno = true;
        }
   
    return vrRetorno;
}

/************************************************************************
*
*Decription: Formata CNPJ de acordo com um controle passado como parâmetro
*Author: Matheus Moreno
*
************************************************************************/
function FormataCNPJ(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições

    
    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;


        if (key != 8 && key != 0) {
            goodChars = "0123456789";
            if (goodChars.indexOf(keychar) != -1) {

                if ((obj.value.length == 2 || obj.value.length == 6) && key != 8)
                { obj.value += "."; }
                if ((obj.value.length == 10) && key != 8)
                { obj.value += "/"; }
                if ((obj.value.length == 15) && key != 8)
                { obj.value += "-"; }

                vrRetorno = true;
            }
        }
        else {
            vrRetorno = true;
        }
    

    return vrRetorno;
}

/************************************************************************
*
*Decription: Formata CNPJ/CPF de acordo com um controle passado como parâmetro
*Author: Matheus Moreno
*
************************************************************************/
function FormataCPFCNPJ(controlPessoa, tipoControle, e, obj)
{
//usar no evento keypress
//bloqueia caracteres alfa e coloca as barras nas posições

 
    var pessoaJuridica = true;
    
    var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
    
    if(tipoControle == "dropdownlist")
    {
        if(document.getElementById(controlPessoa).value == "False")
            pessoaJuridica = false;
    }
    else if(tipoControle == "checkbox")
    {
        pessoaJuridica = document.getElementById(controlPessoa).checked;
    }
    else if(tipoControle == "radiobutton")
    {
        if(document.getElementById(controlPessoa).checked == false)
            pessoaJuridica = false;
    }
     if(pessoaJuridica  == false)
    {
        if(key != 8 && key != 0)
	    {
	        goodChars = "0123456789";
	        if (goodChars.indexOf(keychar) != -1)	
	        {
        		
	            if((obj.value.length == 3 || obj.value.length == 7) && key != 8)
		            {obj.value += ".";}
	            if((obj.value.length == 11) && key != 8)
	            {obj.value += "-";}
    	        
	            vrRetorno = true;
            }
        }
	    else
	    {
	         vrRetorno = true;
	    }
	}
    else
    {
        if(key != 8 && key != 0)
	    {
	        goodChars = "0123456789";
	        if (goodChars.indexOf(keychar) != -1)	
	        { 		
	       	
	            if((obj.value.length == 2 || obj.value.length == 6) && key != 8)
		            {obj.value += ".";}
	            if((obj.value.length == 10) && key != 8)
	            {obj.value += "/";}
	            if((obj.value.length == 15) && key != 8)
	            {obj.value += "-";}
	            
	            vrRetorno = true;
	         }   
        }
        else
	    {
	       vrRetorno = true;
	    }
    }
    
	return vrRetorno;
}

/*****************************************************************************
*
*Decription: Limpa campos vinculados a um campo de CNPJ/CPF quando alterar
        o tipo de pessoa (FISICA ou JURIDICA)
*Author: Matheus Moreno
*
******************************************************************************/
var maxLengthPessoaFisica = 14;
var maxLengthPessoaJuridica = 18;
function clearFieldsCPFCNPJ(controlclicked,controlPessoaJuridica, controlPessoaFisica, tipoControle)
{
    var i = 0;
    var maxLenght = maxLengthPessoaJuridica;
    
    if(tipoControle == "dropdownlist")
    {
        if(document.getElementById(controleTipoPessoa).value == "False")
            maxLenght = maxLengthPessoaFisica;
    }
    else if(tipoControle == "checkbox")
    {
        if(pessoaJuridica = document.getElementById(controleTipoPessoa).checked == false)
            maxLenght = maxLengthPessoaFisica;
    }

    if(arguments.length > 2)
    {
        for(i = 2;i<arguments.length;i++)
        {
            if(document.getElementById(arguments[i]).type == 'text')
            {
                document.getElementById(arguments[i]).value = '';
                document.getElementById(arguments[i]).maxLength = maxLenght;
            }
        }
    }
}

/*****************************************************************************
*
*Decription: Limpa campos vinculados a um campo de CNPJ/CPF quando alterar do tipo radios
        o tipo de pessoa (FISICA ou JURIDICA
*Author: Matheus Moreno
*
******************************************************************************/
function clearFieldsCPFCNPJRadios(controlclicked,controlPessoaJuridica, controlPessoaFisica)
{
    var i = 0;
    var maxLenght = maxLengthPessoaJuridica;
    
    if(controlclicked.id != controlPessoaJuridica)
    {
        pessoaJuridica = false;
        document.getElementById(controlPessoaJuridica).checked = false;
        document.getElementById(controlPessoaFisica).checked = true;
        maxLenght = maxLengthPessoaFisica;
    }
    else
    {
        document.getElementById(controlPessoaJuridica).checked = true;
        document.getElementById(controlPessoaFisica).checked = false;
    }
    
    
    if(arguments.length > 3)
    {
        for(i = 3;i<arguments.length;i++)
        {
            if(document.getElementById(arguments[i]).type == 'text')
            {
                document.getElementById(arguments[i]).value = '';
                document.getElementById(arguments[i]).maxLength = maxLenght;
            }
        }
    }
}

/*****************************************************************************
*
*Decription: Permite entrada de números decimais
*Author: Marcelo Azevedo
*
******************************************************************************/
function ConsisteNum(e)
{
//usar no evento onkeypress
//se o caracter digitado não for pertencente ao goodChars, retorna falso (ascii = 0)

    //v=v.replace(/\D/g,""); Remove tudo o que não é dígito
	
   	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
	
	if(key != 8 && key != 0)
	{
	    goodChars = "0123456789,.";
	    if (goodChars.indexOf(keychar) != -1)	
	    {	    
	       
		    vrRetorno = true;
	    }
	}
	else
	{
	    vrRetorno = true;   
	}

	return vrRetorno;
}

/*****************************************************************************
*
* Description: Permite inserção somente de números
* Author: Camila Foltran
*
******************************************************************************/
/*function SomenteNumeros(e)
{
    //usar no evento onkeypress
    //se o caracter digitado não for pertencente ao goodChars, retorna falso (ascii = 0)

    var key = window.event ? e.keyCode : e.which;
    alert('Key: ' + key);
    var keychar = String.fromCharCode(key);
    //alert('Keychar: ' + keychar + 'Key: ' + key);

	var vrRetorno = false;

	if (navigator.appName.indexOf('Microsoft') != -1) {
	    if (key >= 48 && key <= 57) {
	        alert('1');
	        vrRetorno = true
	    }
	    else {
	        if (key >= 96 && key <= 105) {
	            alert('2');
	            vrRetorno = true
	        }
	        else {
	            if (key == 8 || key == 9 || key == 16 || key == 37 || key == 39 || key == 46) {
	                alert('3');
	                vrRetorno = true
	            }
	        }
	        vrRetorno = false;
	    }
	} else {
	if ((keychar < 48 || keychar > 57) && key == 0) {
	        vrRetorno =  false;
	    }
	}

	return vrRetorno;
	if (key != 8 && key != 0) {
	    goodChars = "0123456789";
	    //alert('entrei');
	    if (goodChars.indexOf(keychar) != -1) {
	        //alert('numero');
	        vrRetorno = true;
	    }
	}
	else {
	    //alert('seila');
	    vrRetorno = true;
	}
	//alert(vrRetorno.toString());
	return vrRetorno;
}*/

function SomenteNumeros(evnt) {
    //Função permite digitação de números
    alert(evnt.keyCode);
    
    if (navigator.appName.indexOf('Microsoft') != -1) {
        if (evnt.keyCode >= 48 && evnt.keyCode <= 57) {
            return true
        }
        else {
            if (evnt.keyCode >= 96 && evnt.keyCode <= 105) {
                return true
            }
            else {
                if (evnt.keyCode == 8 || evnt.keyCode == 9 || evnt.keyCode == 16 || evnt.keyCode == 37 || evnt.keyCode == 39 || evnt.keyCode == 46) {
                    return true
                }
            }
            return false;
        }
    } else {
        if ((evnt.charCode < 48 || evnt.charCode > 57) && evnt.keyCode == 0) {
            return false
        }
    }
}

/*****************************************************************************
*
*Decription: Verifica se é o tamanho máximo de um campo
*Author: Marcelo Azevedo
*
******************************************************************************/
function isMaxLength(txtBox,maxLenght) 
{
    if(txtBox) 
    {    
        return ( txtBox.value.length > maxLenght);
    }
}

/*****************************************************************************
*
*Decription: Bloquea quando exceder o tamanho máximo permitido em um textbox e emite
            um alerta
*Author: Matheus Moreno
*
******************************************************************************/
function validateMaxLenght(control,maxLenght)
{
    if(isMaxLength(control,maxLenght))
    {
        alert('O campo execedeu o tamanho permitido');
        control.focus();
        return false;
    }
    
    return true;
}

/***********
funções auxiliares q não permitem ctrl+c ctrl+v em campos numéricos
***********/
function numero(idControle, integer){

    v = document.getElementById(idControle).value;
    
    var numValidator;
    if(integer == 0)
    {
        numValidator = new RegExp("[0-9]+(([,]|[.])([0-9])+)*");
    }
    else
    {
        numValidator = new RegExp("[0-9]+");
    }
    
    
    if(!v.match(numValidator))
    {
        alert('Valor inválido');
        document.getElementById(idControle).value = '';
    }
    
    return v;
}

function mascara(o,f){
    v_obj=o;
    v_fun=f;
    setTimeout("execmascara()",1);
}

/* função q não permite ctrl+c, ctrl+v 
Author: Camila Foltran - Todos os direitos reservados
*/
function desabilitaCtrlKeyCombinacao(e)
{
    
        //lista as combinações CTRL + key que queremos desabilitar:
        var forbiddenKeys = new Array('c','v');
        var key;
        var isCtrl;

        if(window.event)
        {
                key = window.event.keyCode;   //IE
                if(window.event.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }
        else
        {
                key = e.which;    //firefox
                if(e.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }

        // se ctrl é pressionado, verifica se outro key (tecla) é proibida
        if(isCtrl)
        {
                for(i=0; i<forbiddenKeys.length; i++)
                {
                        //case-insensitive comparation
                        if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
                        {
                                alert('Key combination CTRL + '
                                        +String.fromCharCode(key)
                                        + 'has been disabled.');
                                return false;
                        }
                }
        }
        return true;
}

/*****************************************************************************
*
* Description: Marca todos checkboxes passados por parâmetros a partir de 
*    quando checar o primeiro checkbox passado por parâmetro
* Author: Matheus Moreno
*
******************************************************************************/
function checkAllCheckbox(chkAll)
{
    if(arguments.length > 1)
    {
        for(i = 1;i<arguments.length;i++)
        {
            document.getElementById(arguments[i]).checked = document.getElementById(arguments[0]).checked;
        }
    }
}

/*****************************************************************************
*
* Description: Desmarca a opcao todos quando deschecar um controle
* Author: Matheus Moreno
*
******************************************************************************/
function decheckOrCheckCheckboxAll(chkAll, chkClick)
{
    var marcarCheckTodos = true;
    if(document.getElementById(chkClick).checked == false)
    {
        document.getElementById(chkAll).checked = false;
        marcarCheckTodos = false;
    }

    if(marcarCheckTodos == true && arguments.length > 2)
    {
        for(i = 2;i<arguments.length;i++)
        {
            if(document.getElementById(arguments[i]).checked == false)
                marcarCheckTodos = false;
        }
    }

    if(marcarCheckTodos == true)
    {
        document.getElementById(chkAll).checked = marcarCheckTodos;
    }

}


/*****************************************************************************
*
* Description: Envia informações selecionada pelo usuário para a página que abriu o pop up
* Author: Matheus Moreno
*
******************************************************************************/
function enviarInformacoesOpener(id,descricao,controleText,controleTextCodigo,controleHidden)
{  
    if (controleText == "")
    {
        window.returnValue = id+';'+descricao;         
    }
    else
    {        
        window.opener.document.getElementById(controleText).value = descricao;
        window.opener.document.getElementById(controleHidden).value = id;
        
        if(window.opener.document.getElementById(controleTextCodigo) != null)
        {
            window.opener.document.getElementById(controleTextCodigo).value = id;
        }
    }
    window.close();
}

function abrirPopUp(path, title, width, height)
{
    window.open(path, title, 'scrollbars=yes,titlebar=0,toolbar=0,menubar=0,location=false,status=0,width='+width+',height='+height);
    return false;
}

/*****************************************************************************
*
* Description: Abrir popup de listagem
* Author: Matheus Moreno
*
******************************************************************************/
function abrirPopUpListagem(strPath, controleText,controleHidden,controleTextCodigo, width,height, args,submit)
{
    var filtroCodigo = ''
    if(document.getElementById(controleTextCodigo) != null)
    {
        filtroCodigo = document.getElementById(controleTextCodigo).value;
    }
    

    if (window.showModalDialog)
    {        
        var retValue = window.showModalDialog('popups/'+strPath+'?qsFiltroText='+document.getElementById(controleText).value+'&qsFiltroCodigo='+ filtroCodigo +'&qsArgumentos='+args, 'Listagem', 'dialogWidth:'+width+'px;dialogHeight:'+height+'px;');
    }
    else
    {     
        window.open('popups/'+strPath+'?qsFiltroText='+document.getElementById(controleText).value+'&qsFiltroCodigo='+ filtroCodigo +'&qsArgumentos='+args + '&qsCampoText=' + controleText + '&qsCampoCodigo=' + controleTextCodigo + '&qsCampoHiden=' + controleHidden , 'Listagem', 'Width='+width+'px;Height='+height+'px;');                  
    }



    if(retValue != null)
    {
        document.getElementById(controleText).value = retValue.split(';')[1];
        document.getElementById(controleHidden).value = retValue.split(';')[0];
        
        if(document.getElementById(controleTextCodigo) != null)
        {
            document.getElementById(controleTextCodigo).value = retValue.split(';')[0];
        }
    }    
    
    if(submit != 0)
    {
        document.forms[0].submit();
    }
    
    return false;
}

function desabiliOnClickLengthTextBox(controleText, minLength)
{
    return (document.getElementById(controleText).value.length >= minLength);
}


/*****************************************************************************
*
*Decription: Verifica se dois campos foram preenchidos tamanho para o filtro
*Author: Alberto Barros
*
******************************************************************************/
function filtroObrigatorio(txtBoxInicio,textBoxTermino,mensagemGlobal) 
{    
    if(document.getElementById(txtBoxInicio)) 
    {    
        if(document.getElementById(txtBoxInicio).value.length > 0 && document.getElementById(textBoxTermino).value.length ==0)
        {
        alert(mensagemGlobal);
            return false;
        }
            
        if(document.getElementById(txtBoxInicio).value.length == 0 && document.getElementById(textBoxTermino).value.length > 0)
        {
        alert(mensagemGlobal);
            return false;
        }
    }
   
    return true;
    
}

/*****************************************************************************
*
*Decription: Ocultar ou mostra linhas de uma tabela
*Author: Matheus Moreno
*
******************************************************************************/
function ocultarMostrarLinhas(ocultar)
{
    var i = 0;

    if(ocultar)
    {
        for(i = 1;i<arguments.length;i++)
        {
            if(document.getElementById(arguments[i]) != null)
                document.getElementById(arguments[i]).style.display = 'none';
            else
                window.opener.document.getElementById(arguments[i]).style.display = 'none';
        }
    }
    else
    {   
        for(i = 1;i<arguments.length;i++)
        {
            document.getElementById(arguments[i]).style.display = 'block';
        }
    }
}

/*****************************************************************************
*
*Decription: Valida Tela de Insercao Promoção Item
*Author: Matheus Moreno
*
******************************************************************************/
function validaInsercaoTelaPromocaoItem(controleDropSecao, controleTextProdutoSKU, labelErrorsId, mensagem, imagemId)
{
    if(document.getElementById(controleDropSecao)== null && document.getElementById(controleTextProdutoSKU).value == '')
    {
        document.getElementById(labelErrorsId).innerText = mensagem;
        document.getElementById(labelErrorsId).style.display = 'block';
        //document.getElementById(imagemId).style.visibility = 'visible';
        return false;
    }
    else if(document.getElementById(controleDropSecao) != null)
    {
        if(document.getElementById(controleDropSecao).value == '' && document.getElementById(controleTextProdutoSKU).value == '')
        {
            document.getElementById(labelErrorsId).innerText = mensagem;
            document.getElementById(labelErrorsId).style.display = 'block';
            //document.getElementById(imagemId).style.visibility = 'visible';
            
            return false;
        }
        
         
    }

    document.getElementById(labelErrorsId).style.visibility = 'hidden';
    return true;
}

function validaInsercaoSKU(controleTextProdutoSKU, labelErrorsId, mensagem)
{
    if(document.getElementById(controleTextProdutoSKU).value == '')
    {
        document.getElementById(labelErrorsId).innerText = mensagem;
        document.getElementById(labelErrorsId).style.display = 'block';
        
        return false;
    }

    return true;
}

function limparCampos()
{
    for(i = 0;i<arguments.length;i++)
    {
        if( document.getElementById(arguments[i]) != null)
        {
            document.getElementById(arguments[i]).value = '';            
        }
    }
}

/*****************************************************************************
*
*Decription: Habilita validador
*Author: Matheus Moreno
*
******************************************************************************/
function habiliarValidator(controle)
{
    if(document.getElementById(controle) != null)
    {
        ValidatorEnable(document.getElementById(controle),true);
    }    
}


/*****************************************************************************
*
*Decription: Desabilita Validador
*Author: Matheus Moreno
*
******************************************************************************/
function DesabiliarValidator(controle)
{
    if(document.getElementById(controle) != null)
    {
        ValidatorEnable(document.getElementById(controle),false);
    }    
}

function validarInsercaoProduto(controleHidden, mensagem, labelErrorsId, imagemId)
{
    var pageIsValid = Page_ClientValidate('CadastroProduto');
    
    if(document.getElementById(controleHidden).value == '')
    {    
        document.getElementById(labelErrorsId).innerText = mensagem;
        document.getElementById(labelErrorsId).style.display = 'block';        
        document.getElementById(labelErrorsId).style.visibility = 'visible';        
        document.getElementById(imagemId).style.display = 'inline';
        document.getElementById(imagemId).style.visibility = 'visible';
        document.getElementById(imagemId).src = "../admin/images/error.jpg";
        
        
        return false;
    }    
        document.getElementById(labelErrorsId).style.display = 'none';
        document.getElementById(labelErrorsId).style.visibility = 'hidden';        
        document.getElementById(imagemId).style.display = 'none';
        document.getElementById(imagemId).style.visibility = 'hidden';
        
        
    return pageIsValid;
}


/*****************************************************************************
*
*Decription: Da foco em um botao quando entrar em um determinado textbox
*Author: Matheus Moreno
*
******************************************************************************/
function FocusElemento(e, input)
{
  
    var key = window.event ? e.keyCode : e.which;
    
    if(key == 13)
    {     
        document.getElementById(input).click();
        return false;
    }
    else
    {
       
        return true;
    }
}

/*****************************************************************************
*
*Decription: Valida o CNPJ
*Author: Wellington Nagasawa
*
******************************************************************************/

function Valida_CNPJ(oSrc, args) {
    s = args.Value;

    //substituir os caracteres que não são números
    if (document.layers && parseInt(navigator.appVersion) == 4) {
        x = s.substring(0, 2);
        x += s.substring(3, 6);
        x += s.substring(7, 10);
        x += s.substring(11, 15);
        x += s.substring(16, 18);
        s = x;
    }
    else {
        s = s.replace(".", "");
        s = s.replace(".", "");
        s = s.replace("-", "");
        s = s.replace("/", "");
    }

    if (isNaN(s)) {
        return args.IsValid = false;
    }

    var i;
    var c = s.substr(0, 12);
    var dv = s.substr(12, 2);
    var d1 = 0;

    for (i = 0; i < 12; i++) {
        d1 += c.charAt(11 - i) * (2 + (i % 8));
    }

    if (d1 == 0)
        return args.IsValid = false;

    d1 = 11 - (d1 % 11);

    if (d1 > 9) d1 = 0;

    if (dv.charAt(0) != d1) {
        return args.IsValid = false;
    }

    d1 *= 2;

    for (i = 0; i < 12; i++) {
        d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8));
    }

    d1 = 11 - (d1 % 11);

    if (d1 > 9)
        d1 = 0;

    if (dv.charAt(1) != d1) {
        return args.IsValid = false;
    }

    return args.IsValid = true;
}