function changeCountry(url) {
    url = typeof(url) != 'undefined' ? url : settings.baseUrl + '/json/regions/country/';

    //clear lists
    var options = $j('#city')[0].options;
    while (options.length){
        options[0] = null;
    }
    options = $j('#region')[0].options;
    while (options.length > 1){
        options[0] = null;
    }
    var country = $j('#country').attr('value');

    //return if empty
    if (!country) {
        $j('#region')[0].options[0] = new Option(translate("Select Country first"), "");
        $j('#city')[0].options[0] = new Option(translate("Select Country and Region first"), "");
        return;
    } else {
        $j('#city')[0].options[0] = new Option(translate("Select Country and Region first"), "");
    }

    $j('#region')[0].options[0] = new Option(translate("Loading..."), "");
    $j('#region')[0].selectedIndex = 0;

    $j.getJSON(url + country, {}, function(json){
        options[0] = new Option(translate('Select region'), '');
        for(var i=0; i < json.length; i++)
            options[options.length] = new Option(json[i][1], json[i][0]);
        $j('#region')[0].selectedIndex = 0;
    });
}

function showTC() {
    loadingBox();
    $j.getJSON(settings.baseUrl + '/json/content/key/tc/', {}, function(response){
        if (response.status == "ok") {
            bigInfoBox(response.content, response.title);
        } else
            errorBox(response.message);

    });
}


var registrationStarted = false;

function regInfoBox(boxType,user,avatar){

    if(registrationStarted){
        return false;
    }
    else{
        registrationStarted = true;
    }

    var part = '';
    if (boxType=='listing'){
        if ( typeof(user) !='undefined'){
            var userX = '<span class="regUserX">'+user+'</span>';
            part = '<div class="regUser">'
            +'<h1 class="registration_popup_text1">'+translate('registration_popup_text2',userX)+'</h1>'
            +'<div class="regAvWr"><img src="'+$('#'+avatar).attr('src')+'" class="regAvatar" alt="'+user+'" title="'+user+'" /></div>'
            +'<a href="#" id="signUpLink" onclick="Boxy.get(this).hide(); getElementById(&quot;login&quot;).focus(); return false;"><br /><br /><br />Zaloguj się<br />Mam już konto na swatka.pl</a>'
            +'<div class="clear"></div>'
            +'</div>';
        }
    }
    if(part == ''){
        part = '<div id="regLady">\n\
        <img width="260" height="281" src="/img/regpopup/registration-lady.jpg">\n\
        <a href="#" id="signUpLink" onclick="Boxy.get(this).hide(); getElementById(&quot;login&quot;).focus(); return false;">Zaloguj się<br />Mam już konto na swatka.pl</a>\n\
        </div>';
    }
    /*
    if (boxType=='registration'){

    part = '<div id="regLady">\n\
    <img width="260" height="281" src="/img/regpopup/registration-lady.jpg">\n\
    <a href="#" id="signUpLink" onclick="Boxy.get(this).hide(); getElementById(&quot;login&quot;).focus(); return false;">Zaloguj się</a>\n\
    </div>';
         
    }



    if (boxType=='listing'){
        if ( typeof(user) !='undefined'){
            var userX = '<span class="regUserX">'+user+'</span>';
            part = '<div class="regUser">'
            +'<h1 class="registration_popup_text1">'+translate('registration_popup_text1')+'</h1>'
            +'<p class="registration_popup_text2">'+translate('registration_popup_text2',userX)+'</p>'
            +'<div class="regAvWr"><img src="'+$('#'+avatar).attr('src')+'" class="regAvatar" alt="'+user+'" title="'+user+'" /></div>'
            //+'<p class="registration_popup_text3">'+translate('registration_popup_text3')+'</p>'
            +'<div class="clear"></div>'
            +'</div>'; 
        }
    }else if(boxType == 'account'){
        part = '<div class="regAccount">'
        +'<h1 class="registration_popup_text4">'+translate('registration_popup_text4')+'</h1>'
        +'<p class="registration_popup_text5">'+translate('registration_popup_text5')+'</p>'
        +'<div class="clear"></div>'
        +'</div>';

    }
    */
    //if(typeof($('body').data('registrationForm')) == ''){
    var form_content = '';
    if($('body').data('registrationForm') == null){
        $.get('/registration/form/r/'
            ,function(response){
                $('body').data('registrationForm', response);
                form_content = changeContentForm(response,part);
                registrationForm(form_content,part);
            });
    }else{
        
        form_content = $('body').data('registrationForm');
        form_content = changeContentForm(form_content,part);
        registrationForm(form_content,part);
    }
}

function changeContentForm(form_content,part){
/*
    if(part!=''){
        var res = $(form_content);

        $(res[0]).find('.logForm').html(part);
        //$(res[0]).find('.logForm').html(part);
        form_content = '';
        res.each(
            function(data){
                if(res[data].type == 'text/javascript'){
                    form_content += '<script type="text/javascript">'+res[data].innerHTML+'</script>';
                }
                else{
                    form_content += '<'+res[data].nodeName+' class="'+ res[data].className+'" id="'+res[data].id+'">'+res[data].innerHTML+'</'+res[data].nodeName+'>';
                }
            }
            );
    }
    */
    return form_content;
}

function registrationForm(message,part){
    var res = $(message);
    newRegBox(message);
    for(var i=0;i<res.length;i++){
        if(res[i].type == 'text/javascript'){
            eval(res[i].innerHTML);
        }
    }
    if(part != ''){
        $('.logForm').after(part);
        $('.logForm').hide();
    }
    registrationStarted = false;
}
function regBox(message) {
    var code = '<div id="popupDiv" class="boxContent">'
    +'<div class="closeBox"><span onclick="Boxy.get(this).hide();">x</span></div>'
    + message
    +'</div>';
    new Boxy(code,{
        modal:true,
        unloadOnHide:true,
        closeable:true
    });
}
function newRegBox(message) {

    new Boxy(message,{
        modal:true,
        unloadOnHide:true,
        closeable:false
    });

    pos = Boxy.get($('#regForm')).getPosition();
    Boxy.get($('#regForm')).moveTo(pos[0],29);

    $('#regForm').submit(function(){
        $('#regForm').data('submited',true);
    });

    //IE6
    if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
        $('select').hide();
        $('#regForm select').show();
    }
}

function closeRegBox(obj) {
    Boxy.get(obj).hide();
    if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
        $('select').show();
    }
}
 
function setBirthDate(obj){
    var birthdate = $('#birthdate').val().split('-');
    this.year = birthdate[0];
    this.month = birthdate[1];
    this.day = birthdate[2];
    eval('this.'+obj.name+'= obj.value;');
    $('#birthdate').val(this.year +'-'+ this.month +'-'+ this.day);
    if($('#regForm').data('submited')){
        $('#regForm').valid();
    }
}

function getYearList(age, value){
    if(!age){
        age = 30;
    }
    var optionTemplate = '<option %3$s value="%1$s">%2$s</option>';
    var content = sprintf(optionTemplate,'','YYYY');
    var now = new Date();

    for (var i = now.getFullYear() - age; i >= 1900; i--){
        var selected = '';
        if(value == i ){
            selected = 'SELECTED';
        }
        content += sprintf(optionTemplate, i, i, selected);
    }

    return content;
}
