/* perform server-side instant-validation */
function performInstantValidation(value, name, imgName, url) {

    document.images[imgName].src = url.concat('?name=').concat(
            name).concat('&value=').concat(encodeURIComponent(value));
}

/* perform client-side instant-validation */
function performValidation(elem, imgName) {
    if (elem) {
        
        var len = elem.value.length;
        if (len >= 5 && len <= 50) {

            document.images[imgName].src = 'images/validator/valid.png';
        }
        else {

            document.images[imgName].src = 'images/validator/invalid.png';
        }
    }
}


/* update country-selector's displayed image when selection changes */
function changeCountryImage(elem) {
    if (elem) {
        
        if (elem.selectedIndex > 0) {
            
            elem.style.backgroundImage = 'url(images/flags/' + 
                    elem.value.toLowerCase() + '.png)';
        }
        else {
            
            elem.style.backgroundImage = '';
        }
    }
}

/* update state-selector */
function regformCountryChanged(elem) {
    if (elem) {
        
        // update image
        changeCountryImage(elem);
        
        // clear state and update options
        var state = document.forms[elem.form.name].elements['state'];
        
        state.selectedIndex = 0;
        for (i=1; i<state.options.length; i++) {
            
            if (state.options[i].className == elem.value) {
                
                state.options[i].style.display = '';
            }
            else {
                
                state.options[i].style.display = 'none';
            }
        }
    }
}

/* balloons */
function showBalloon(id, x, y) {
    var elem = document.getElementById(id);
    if (elem) {
        
        if (elem.style.display == 'none') {
            
            if (x) elem.style.left = (getInt(x) + 'px');
            if (y) elem.style.top = (getInt(y) + 'px');
            
            if (typeof(Effect) == "undefined") {
                
                elem.style.display = '';
            }
            else {
                
                Effect.Appear(elem.id, {duration:1.5});
            }
        }
    }
}

function hideBalloon(id) {
    var elem = document.getElementById(id);
    if (elem) {
        
        /*
        if (typeof(Effect) == "undefined") {
            
            elem.style.display = 'none';
        }
        else {
            
            Effect.Fade(elem.id, {duration:0.5});
        }
        */
        elem.style.display = 'none';
    }
}

var qmark = (void(0));

function qmark_set(id) {
    var oid = (id + 'help');
    if (qmark) {
        
        if (qmark.id != oid) {
            hideBalloon(qmark.id);
        }
    }
    
    if (!(qmark) || (qmark && qmark.style.display == 'none')) {
        
        qmark = document.getElementById(oid);
        if (qmark) {
            showBalloon(qmark.id);
        }
    }
}

function qmark_clear() {
    qmark_set(void(0));
}

function explode(delimiter, value) {
    var v = new Array();
    if (value) {

        var s = String(value);
        if (s.length > 0) {

            var pos = 0;
            while ((pos = s.indexOf(delimiter)) != -1) {

                v.push(s.substring(0, pos));
                if (pos < s.length) {

                    s = s.substr(pos+1);
                }
                else {
                    
                    break;
                }
            }
            if ((pos == -1) && (s.length > 0)) {

                v.push(s);
            }
        }
    }
    return v;
}