// State vars
function getTax()
{
    var country = "";
    var state = "";
    
    if(typeof countryVAR != 'undefined' && mainForm.elements[countryVAR] !=null){
        country = mainForm.elements[countryVAR].value;
    }
    
    if(typeof stateVAR != 'undefined' && mainForm.elements[stateVAR] !=null){
        state = mainForm.elements[stateVAR].value;
    }
    
    if(country){
        new Ajax.Request(   'index.php?fuse=clients&action=GetTax&country='+country+'&state='+state+'&ignoreuser=1',
                            {   onSuccess   : setTax}
        );
    }else{
        if(typeof vatVAR != 'undefined' && mainForm.elements[vatVAR] != null){
            mainForm.elements[vatVAR].disabled = true;
            mainForm.elements[vatVAR].value = '';
        }
        
        if($('vatBlock') != undefined){
            $('vatBlock').style.display = 'none';
        }
    }
}

function setTax(responseObj)
{
    respArr = responseObj.responseText.split("|");
    
    if(typeof vatVAR != 'undefined' && mainForm.elements[vatVAR] !=null){
        if(respArr[1] == "1" || respArr[4] == "1"){
            mainForm.elements[vatVAR].disabled = false;
            
            if($('vatBlock') != undefined){
                $('vatBlock').style.display = '';
            }
        }else{
            mainForm.elements[vatVAR].disabled = true;
            mainForm.elements[vatVAR].value = '';
            
            if($('vatBlock') != undefined){
                $('vatBlock').style.display = 'none';
            }
        }
        
        checkVAT();
    }else{
        if($('vatBlock') != undefined){
            $('vatBlock').style.display='none';
        }
    }
}

function checkVAT()
{
    var country = '';
    var vatnum = '';
    
    if(typeof countryVAR != 'undefined' && mainForm.elements[countryVAR] !=null){
        country = mainForm.elements[countryVAR].value;
    }
    
    if($('vat_country') != undefined){
        // Greece has to be different...
        if(country == 'GR'){
            $('vat_country').innerHTML = 'EL';
        }else{
            $('vat_country').innerHTML = country;
        }
    }
    
    if(typeof vatVAR != 'undefined'
      && mainForm.elements[vatVAR] != null
      && mainForm.elements[vatVAR].value != ''){
        vatnum = mainForm.elements[vatVAR].value;
    }
    
    if(country){
        new Ajax.Request(   'index.php?fuse=clients&action=CheckVAT&country='+country+'&vat='+vatnum+'&ignoreuser=1',
                            {   onSuccess   : checkVAT_Callback}
        );
    }else{
        $('vat_valid').style.display = 'none';
        $('vat_invalid').style.display = 'none';
        
        if(typeof vatVAR != 'undefined' && mainForm.elements[vatVAR] != null){
            mainForm.elements[vatVAR].disabled = true;
            mainForm.elements[vatVAR].value = '';
        }
        
        if($('vatBlock') != undefined){
            $('vatBlock').style.display = 'none';
        }
    }
}

function checkVAT_Callback(responseObj)
{
    respArr = responseObj.responseText.split("|");
    
    if(respArr[1] != ""
      && typeof vatVAR != 'undefined'
      && mainForm.elements[vatVAR] != null
      && mainForm.elements[vatVAR].value == ''){
        mainForm.elements[vatVAR].value = respArr[1];
    }
    
    if(respArr[0] == "1"){
        $('vat_valid').style.display = '';
        $('vat_invalid').style.display = 'none';
    }else{
        $('vat_valid').style.display = 'none';
        
        if(respArr[1] != ""){
            $('vat_invalid').style.display = '';
        }else{
            $('vat_invalid').style.display = 'none';
        }
    }
}