Validation.add('validate-cemail', 'Please make sure your emails match.', function(v) {
    var conf = $('cemail') ? $('cemail') : $$('.validate-cemail')[0];
    var pass = false;
    if ($('email')) {
        mail = $('email');
    }

    var emailElements = $$('.validate-email');
    for (var i = 0; i < emailElements.size(); i++) {
        var emailElement = emailElements[i];
        if (emailElement.up('form').id == conf.up('form').id) {
            mail = emailElement;
        }
    }
    return (mail.value == conf.value);
});

PdtRegionUpdater = Class.create(RegionUpdater, {
    update: function()
    {
        if (this.regions[this.countryEl.value]) {
            var i, option, region, def;

            if (this.regionTextEl) {
                def = this.regionTextEl.value.toLowerCase();
                this.regionTextEl.value = '';
            }
            if (!def) {
                def = this.regionSelectEl.getAttribute('defaultValue');
            }

            this.regionSelectEl.options.length = 1;
            for (regionId in this.regions[this.countryEl.value]) {
                region = this.regions[this.countryEl.value][regionId];

                option = document.createElement('OPTION');
                option.value = regionId;
                option.text = region.name;

                if (this.regionSelectEl.options.add) {
                    this.regionSelectEl.options.add(option);
                } else {
                    this.regionSelectEl.appendChild(option);
                }

                if (regionId==def || region.name.toLowerCase()==def || region.code.toLowerCase()==def) {
                    this.regionSelectEl.value = regionId;
                }
            }

            if (this.disableAction=='hide') {
                if (this.regionTextEl) {
                    this.regionTextEl.up(3).hide()
                    this.regionTextEl.style.display = 'none';
                }

                this.regionSelectEl.up(1).show();
                this.regionSelectEl.style.display = '';
            } else if (this.disableAction=='disable') {
                if (this.regionTextEl) {
                    this.regionTextEl.disabled = true;
                }
                this.regionSelectEl.disabled = false;
            }
            this.setMarkDisplay(this.regionSelectEl, true);
        } else {
            if (this.disableAction=='hide') {
                if (this.regionTextEl) {
                    this.regionTextEl.style.display = '';
                    this.regionTextEl.up(3).show()
                }
                this.regionSelectEl.up(1).hide();
                this.regionSelectEl.style.display = 'none';
                Validation.reset(this.regionSelectEl);
            } else if (this.disableAction=='disable') {
                if (this.regionTextEl) {
                    this.regionTextEl.disabled = false;
                }
                this.regionSelectEl.disabled = true;
            } else if (this.disableAction=='nullify') {
                this.regionSelectEl.options.length = 1;
                this.regionSelectEl.value = '';
                this.regionSelectEl.selectedIndex = 0;
                this.lastCountryId = '';
            }
            this.setMarkDisplay(this.regionSelectEl, false);
        }

        // Make Zip and its label required/optional
        var zipUpdater = new ZipUpdater(this.countryEl.value, this.zipEl);
        zipUpdater.update();
    },
    
    setMarkDisplay: function(elem, display){
        elem = $(elem);
        var labelElement = elem.up(1).next('div.champObligatoire');
        

        if(labelElement) {
            inputElement = labelElement.previous();
            if (display) {
                labelElement.show();
                if (inputElement) {
                    inputElement.addClassName('required-entry');
                }
            } else {
                labelElement.hide();
                if (inputElement) {
                    inputElement.removeClassName('required-entry');
                }
            }
        }
    }
});

Object.extend(Validation, {
    showAdvice : function(elm, advice, adviceName){
        elm.title = advice.innerHTML;
        jQ(elm).tooltip({ 
            track: true, 
            delay: 0, 
            showURL: false, 
            fade: 250,
            id: "tooltipWrongData"
        });
    }
});
