/*	JavaScript Functions for registration page	*/
	function codeDisplayWrapper( display, id )
	{
		try
		{
			codeDisplay( display, id );
		}
		catch( exception1 )
		{}
	}

	function testField() {
        var shipToCountry = trim(document.RegisterForm.shipToCountry.value.toLowerCase());
        var billToCountry = trim(document.RegisterForm.billToCountry.value.toLowerCase());
        if ((shipToCountry == "fr")||(billToCountry == "fr")) {
        	codeDisplayWrapper('show', 'fr_privacy_policy');
        } else {
            codeDisplayWrapper('hide', 'fr_privacy_policy');
        }
        countryChange();
	}
	
	function toggleBillingFields() {
        if (document.RegisterForm.sameAddress.checked) {
            codeDisplayWrapper('show', 'billing_fields');
        } else {
            codeDisplayWrapper('hide', 'billing_fields');
        }
        countryChange();
	}		
    
    // When the ShipTo or BillTo country changes, need to determine
    // if it is Brazil.  When Brazil need to show the Brazil specific
    // fields.  
    function countryChange() {
        try {
            var countryCode = null;
            if (document.RegisterForm.sameAddress.checked) {
                // BillTo matters when addresses are the same.
                countryCode = document.RegisterForm.billToCountry.value.toLowerCase();
            } else {
                // ShipTo matters when addresses are different.
                countryCode = document.RegisterForm.shipToCountry.value.toLowerCase();
            }
            
            //alert("countryCode=" + countryCode);
            
            if (countryCode == "br") {
                codeDisplayWrapper('show', 'brazilSpecific');
            } else {
                codeDisplayWrapper('hide', 'brazilSpecific');
            }
        } catch (exception) {
            alert("Error in countryChange()\n" + exception.description);
        }
    }       

    //testFieldBrazil();
    function testFieldBrazil() {
        var legalEnt = null;
        var prodUse = null;
        try {
            legalEnt = trim(document.RegisterForm.legalEntity.value.toLowerCase());
            prodUse = trim(document.RegisterForm.orderProductUse.value.toLowerCase());
        } catch(event) {
           setTimeout("testField()",10)
        }
    
        //alert("legalEnt=" + legalEnt + "\n prodUse=" + prodUse); 
        if(legalEnt=='br' && prodUse=='company use') {
            codeDisplayWrapper('show', 'companyuse_fields');
            codeDisplayWrapper('hide', 'individualuse_fields');
        } else if (legalEnt == "br" && prodUse == "individual use") {
            codeDisplayWrapper('hide', 'companyuse_fields');
            codeDisplayWrapper('show', 'individualuse_fields');
        } else {
            codeDisplayWrapper('hide', 'companyuse_fields');
            codeDisplayWrapper('hide', 'individualuse_fields');
        }    
    }

    function validate() { 
        /*   
        var pswdflag;
        var arr;
        var arr2;
        //get state codes
        
        msgflag = "false";
        pswdflag = "false";
        msg=document.RegisterForm.validation_message.value + ":\r";
        
        ret = validate_brazil(); 
    
        if (msgflag == "true") {
          alert(msg);
        }
        
        if (msgflag == "false") {        
            document.RegisterForm.submit();
        }
        */
        
        try {
            var countryCode = null;
            if (document.RegisterForm.sameAddress.checked) {
                // BillTo matters when addresses are the same.
                countryCode = document.RegisterForm.billToCountry.value.toLowerCase();
            } else {
                // ShipTo matters when addresses are different.
                countryCode = document.RegisterForm.shipToCountry.value.toLowerCase();
            }
            
            //alert("countryCode=" + countryCode);
            
            if (countryCode == "br") {
                if (validateBrazil() == true) {
                    document.RegisterForm.submit();
                }
            } else {
                document.RegisterForm.submit();
            }
        } catch (exception) {
            alert("Error in validate()\n" + exception.description);
        }
        
    }

    function validateBrazil() {   
        var msg = "";
        var msgflag = false;

        try { 
	        msg = document.RegisterForm.validation_message.value + ":\r";
	
	        //Validation Validation
	        //--- Legal Entity 
	        if (document.RegisterForm.legalEntity.selectedIndex < 1 || document.RegisterForm.legalEntity[document.RegisterForm.legalEntity.selectedIndex].value == "empty" ) {
	            msg += "\r" + document.RegisterForm.legalEntity_msg.value;
	            msgflag = true;
	            document.RegisterForm.legalEntity.focus();
	        }
	
	        //--- If the LE is not Brazil then just submit
	        if (document.RegisterForm.legalEntity[document.RegisterForm.legalEntity.selectedIndex].value == "BR" ) {
	
	            //--- Product Use
	            if (document.RegisterForm.orderProductUse.selectedIndex < 1 || document.RegisterForm.orderProductUse[document.RegisterForm.orderProductUse.selectedIndex].value == "empty" ) {
	                msg += "\r" + document.RegisterForm.orderProductUse_msg.value;
	                msgflag = true;
	                document.RegisterForm.orderProductUse.focus();
	            }
	
	            //--- Federal Tax ID    
	            if (document.RegisterForm.orderProductUse[document.RegisterForm.orderProductUse.selectedIndex].value == "Company Use" ) {    
	                if (document.RegisterForm.orderCnpjTaxId.value == "") {
	                    msg += "\r" + document.RegisterForm.orderCnpjTaxId_msg.value;
	                    msgflag = true;
	                    document.RegisterForm.orderCnpjTaxId.focus();
	                } else { 
	                    if (validateCNPJ(document.RegisterForm.orderCnpjTaxId.value) == false) {
	                        msg += "\r" + document.RegisterForm.orderCnpjTaxId_invalid_msg.value;
	                        msgflag = true;
	                        document.RegisterForm.orderCnpjTaxId.focus();
	                    }
	                }
	            }
	
	            //--- Individual Federal Tax ID CPF
	            if (document.RegisterForm.orderProductUse[document.RegisterForm.orderProductUse.selectedIndex].value == "Individual Use" ) {    
	                if (document.RegisterForm.orderCpfTaxId.value == "") {
	                    msg += "\r" + document.RegisterForm.orderCpfTaxId_msg.value;
	                    msgflag = true;
	                    document.RegisterForm.orderCpfTaxId.focus();
	                } else { 
	                    if (validateCpf(document.RegisterForm.orderCpfTaxId.value) == false) {
	                        msg += "\r" + document.RegisterForm.orderCpfTaxId_invalid_msg.value;
	                        msgflag = true;
	                        document.RegisterForm.orderCpfTaxId.focus();
	                    }
	                }
	            }
	        
	            //--- State Tax ID    
	            if (document.RegisterForm.orderProductUse[document.RegisterForm.orderProductUse.selectedIndex].value == "Company Use" ) {    
	                if (document.RegisterForm.orderStateTaxId.value == "") {
	                    msg += "\r" + document.RegisterForm.orderStateTaxId_msg.value;
	                    msgflag = true;
	                    document.RegisterForm.orderStateTaxId.focus();
	                }
	            }        
            }	            	            
        } catch (exception) {
            alert("Error in validateBrazil()\n" + exception.description);
            return false;
        }
        if (msgflag == true) {
            alert (msg);
            return false;
        } else {
            return true;
        }
    }

    function adjust_cnpj(input) {
        if ((event.keyCode<48) || (event.keyCode>57)) {
            event.returnValue = false; 
        } else { 
            if ((input.value.length == 2) || (input.value.length == 6)) input.value=input.value + "." ;
            if ((input.value.length == 10)) input.value=input.value + "/" ;
            if ((input.value.length == 15)) input.value=input.value + "-" ;
       }
    }

    function adjust_cnpf(input) {
        if ((event.keyCode < 48) || (event.keyCode > 57)) {
            event.returnValue = false; 
        } else { 
            if ((input.value.length == 12)) input.value=input.value + "-" ;
        }
    }

    function adjust_cpf(input) {
        if ((event.keyCode < 48) || (event.keyCode > 57)) {
            event.returnValue = false;
        } else { 
            if((input.value.length == 3) || (input.value.length == 7)) input.value = input.value + "." ;
            else {
                if(input.value.length == 11) input.value=input.value + "-" ;
            }
        }
    }

    // 
    function validateCpf(cpf) {
        try {
	        var i; 
	        s = cpf;
	        s = clean(s);
	        if (s.length > 11) return false;
	        for (i = 0; i < 11; i++) {
	            j = new String(i);
	            j = j+j+j+j+j+j+j+j+j+j+j;
	            if (cpf == j) return false;
	        }
	        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) return false; 
	    
	        d1 = 11 - (d1 % 11); 
	        if (d1 > 9) d1 = 0; 
	        if (dv.charAt(0) != d1) 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) return false; 
	    
	        return true; 
	    } catch (exception) {
            alert("Error in validateCpf()\n" + exception.description);
            return false;
	    }
    } 

    // Limpa a string digitada, removendo caracteres sujos
    function clean(cpf) {
        while((cpfx=cpf.indexOf("-"))!=-1) cpf = cpf.substring(0,cpfx)+cpf.substring(cpfx+1);
        while((cpfx=cpf.indexOf("/"))!=-1) cpf = cpf.substring(0,cpfx)+cpf.substring(cpfx+1);
        while((cpfx=cpf.indexOf(","))!=-1) cpf = cpf.substring(0,cpfx)+cpf.substring(cpfx+1);
        while((cpfx=cpf.indexOf("."))!=-1) cpf = cpf.substring(0,cpfx)+cpf.substring(cpfx+1);
        while((cpfx=cpf.indexOf("("))!=-1) cpf = cpf.substring(0,cpfx)+cpf.substring(cpfx+1);
        while((cpfx=cpf.indexOf(")"))!=-1) cpf = cpf.substring(0,cpfx)+cpf.substring(cpfx+1);
        while((cpfx=cpf.indexOf(" "))!=-1) cpf = cpf.substring(0,cpfx)+cpf.substring(cpfx+1);
        return(cpf);
    }

    // CNPJ is in the form of nn.nnn.nnn/nnnn-nn
    // The last 2 numbers are determined based on the rest of the numbers entered.
    function validateCNPJ(CNPJ) {
        try {
	        erro = new String;
	        if (CNPJ.length < 18) return false;
	        if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")) {
	            return false;
	        }
	        //substituir os caracteres que não são números
	        if (document.layers && parseInt(navigator.appVersion) == 4) {
				x = CNPJ.substring(0,2);
				x += CNPJ. substring (3,6);
				x += CNPJ. substring (7,10);
				x += CNPJ. substring (11,15);
				x += CNPJ. substring (16,18);
				CNPJ = x; 
	        } else {
                CNPJ = CNPJ. replace (".","");
                CNPJ = CNPJ. replace (".","");
                CNPJ = CNPJ. replace ("-","");
                CNPJ = CNPJ. replace ("/","");
	        }
	        
	        var nonNumbers = /\D/;
	        if (nonNumbers.test(CNPJ)) return false;
	                  
	        var a = [];
	        var b = new Number;
	        var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	        for (i = 0; i < 12; i++) {
	            a[i] = CNPJ.charAt(i);
	            b += a[i] * c[i+1];
	        }
	      
	        if ((x = b % 11) < 2) { 
	            a[12] = 0;
	        } else { 
	            a[12] = 11 - x;
	        }
	         
	        b = 0;
	        for (y = 0; y < 13; y++) {
	            b += (a[y] * c[y]); 
	        }
	        if ((x = b % 11) < 2) { 
	            a[13] = 0;
	        } else {
	            a[13] = 11 - x;
	        }
	
            // alert("CNPJ.charAt(12)=" + CNPJ.charAt(12) + "\t a[12]=" + a[12] + "\n CNPJ.charAt(13)=" + CNPJ.charAt(13) + "\t a[13]= " + a[13]);
            if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])) {
	            return false;
            }
	        if (erro.length > 0) {
	            return false;
	        } else {
	            return true;
	        }
	        
	        return true;
	    } catch (exception) {
	        alert("Error in validateCNPJ()\n" + exception.description);
	        return false;
	    }
    }
