$(document).ready(function() {
    browserDetection();
    fixIE6alphaTransparency();
    attachScrollbars();
    attachCalendar();
    $('#form-join').submit(attachValidateJoin);
    $("#attach").click(function () {
        $item = $("#template").clone(true);
        $item.insertBefore($(this).parent());
        $item.removeAttr("id");
        attachScrollbars();
        return false;
    });
    $('#gallery a').click(function () {
        $('#gallery').next('td').children("a[rel^='lightbox']").click();
        return false;
    });
});

function openWindow(URL,width,height) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(" + URL + ", '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left="+((screen.width - width)/2)+",top="+((screen.height - height)/2)+"');");
    return false;
}

function isEmail(address) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(address) == false) {
        return false;
    } else {
        return true;
    }

}

function attachScrollbars() {
    if (document.getElementById("page-scrollbars") != null) {
        $('#page-scrollbars').jScrollPane({
            showArrows:true,
            scrollbarWidth:28,
            scrollbarMargin:40,
            dragMinHeight:21,
            dragMaxHeight:28
        });
    }
}

function attachCalendar() {
    $('a.calendar').click(function () {
        $loader = '<img alt="Please wait..." src="resources/images/ajax-loader.gif" style="display:block;margin:160px auto;" />';
        $('#calendar').html($loader);
        $('#prevMonth').hide();
        $('#nextMonth').hide();
        $('#calendar-nav span').hide();
        $url = $(this).attr('href').replace(/&amp;/, "&");
        $.getJSON("process.php", {action: "calendar", url: $url }, function(data){
            if (data['prev'] != '') {
                $('#prevMonth').attr('href', data['prev']);
                $('#prevMonth').show();
            }
            if (data['next'] != '') {
                $('#nextMonth').attr('href', data['next']);
                $('#nextMonth').show();
            }
            $('#calendar-nav span').html(data['dt']);
            $('#calendar-nav span').show();
            $('#calendar').html(data['content']);
            attachScrollbars();
        }); 
        return false;
    });
}

function attachValidateJoin() {
    if ($.trim($('#name').attr('value')) == '') {
        alert('Please enter Name of Artist or Band');
        $('#name').focus();
        return false;
    }
    if ($.trim($('#contact').attr('value')) == '') {
        alert('Please enter Primary Contact Person');
        $('#contact').focus();
        return false;
    }
    if ($.trim($('#address').attr('value')) == '') {
        alert('Please enter Street Address');
        $('#address').focus();
        return false;
    }
    if ($.trim($('#city').attr('value')) == '') {
        alert('Please enter City');
        $('#city').focus();
        return false;
    }
    if ($.trim($('#country').attr('value')) == 'United States') {
        if ($.trim($('#state').attr('value')) == '') {
            alert('Please choose State');
            $('#state').focus();
            return false;
        }
    }
    if ($.trim($('#zip').attr('value')) == '') {
        alert('Please enter Postal Code');
        $('#zip').focus();
        return false;
    }
    if ($.trim($('#country').attr('value')) == '') {
        alert('Please choose Country');
        $('#country').focus();
        return false;
    }
    if (!isEmail($('#email').attr('value'))) {
        alert('Please enter Email');
        $('#email').focus();
        return false;
    }
    if ($.trim($('#phone').attr('value')) == '') {
        alert('Please enter Phone');
        $('#phone').focus();
        return false;
    }
    if ($.trim($('#profile').attr('value')) == '') {
        alert('Please enter Artist or Band Bio/Profile');
        $('#profile').focus();
        return false;
    }
    return true;
}
