$(document).ready(function() {
   $('a.external').click(function() {
        window.open($(this).attr('href'));
        return false;
    });
    $('.cluster').lightBox();
    $('.expand').click(function() {
        if($(this).parent().next().css('display') == 'none') {
            var span = $(this).find('span');
            if(span.length == 0) {
                span = $(this).next().find('span'); 
            }
            span.css('display', 'none'); 
        } else {
            var span = $(this).find('span');
            if(span.length == 0) {
                span = $(this).next().find('span'); 
            }
            span.css('display', ''); 
        }
        var c = $(this).parent().nextAll();
        $(c).each(function() {
            if($(this).children(':first').hasClass('tbl_email_first')) {
                return false;
            } else {
                $(this).css('display', ($(this).css('display') == 'none') ? '' : 'none');
            }
        });
    });
    var on_trust = false;
    $('.show_trust').click(function() {
        if(!on_trust) {
            $("#trustlogo").dialog({
                bgiframe: true,
                height: 480,
                width: 800,
                title: 'Standard TrustLogo',
                modal: true
            });
            on_trust = true;
        } else {
            $("#trustlogo").dialog('open');
        }
    });
    var on_trans = false;
    $('.domain_pricing').click(function() {
        if(!on_trans) {
            $("#domain_p").dialog({
                bgiframe: true,
                height: 310,
                width: 500,
                title: 'Domain pricing',
                modal: true
            });
            on_trans = true;
        } else {
            $("#domain_p").dialog('open');
        }
    });
    $('input[id^="auto_renew_"]').click(function() {
        var id = $(this).attr('id').replace('auto_renew_','');
        var current_status = ($(this).attr('src').match(/ok/) != null ? true : false);
        if(!current_status && !confirm('To make use of the auto-renewal feature it must be activated at least 40 days prior to domain expiry.\n\nYour payment/Credit Card details must be up to date as well.\n\nPlease press OK to turn on the auto-renewal feature for this domain or cancel to leave the current status unchanged.')) {
            return;
        } else if(current_status && !confirm('Are you sure you want to turn off automatic domain renewal?\n\nPlease press OK to turn off the auto-renewal feature for this domain or cancel to leave the current status unchanged.')) {
            return;
        }
        $(this).attr('src','/images/ajax-loader.gif');
        $.ajax({
            url: 'https://www.webaccess.com.au/clientarea.php?action=domaindetails',
            data: 'id=' + id + '&autorenew=' + (current_status ? 'disable' : 'enable'),
            cache: false,
            type: 'POST',
            context: $(this),
            success: function(html) {
                this.context.attr('src','/images/status' + (html.match(/current status: <b><span style='color:green'>enabled/im) != null ? 'ok' : 'failed') + '.gif');
            }
        });
    });
    $('.show_addon').click(function() {
        var t = $(this);
        var shown = t.find('.in_o').css('display');
        var word = t.attr('id');
        $('div.cartbox').css('display', 'none');
        $('div.cartbox').next().css('display', 'none');
        $('div.show_addon').find('.in_o').css('display', '');
        if(shown != 'none') {
            t.find('.in_o').css('display', 'none');    
        }
        $('div.cartbox:contains("'+word+'")').each(function() {
            if(shown == 'none') {
                $(this).css('display', 'none');
                $(this).next().css('display', 'none');
            } else {
                $(this).css('display', ''); 
                $(this).next().css('display', '');  
            }
        });
        
    });
    $('#reg_cert').click(function() {
        $.ajax({
            url: 'https://www.webaccess.com.au/idx/domains',
            data: 'sub=certificate',
            cache: false,
            type: 'POST',   
            success: function(html){
                if(html != 'false') {
                    var ow = window.open('', 'Certificate');
                    ow.document.write(html);
                    ow.document.close();
                }
            }
        });
        return false;        
    });
    $('.cartbox.customfields input[name="customfield[16]"]').keyup(function() {
        var website = $(this).attr('value');
        var web = (website == '') ? 'DOMAIN' : website.replace('www.', '');
        $('.cartbox.customfields select[name="customfield[25]"] option').each(function() {
            var str = $(this).text();
            var spt = str.split('@');
            $(this).text(spt[0]+'@'+web);       
        });
    });
    $.ajax({
        url: 'idx.php',
        data: 'chat',
        cache: false,
        type: 'GET',   
        success: function(html){
            $('#chatonoff').html(html);
        }
    });
    $('#chat').live('click', function() {
        $.ajax({
            url: 'idx.php',
            data: 'chatpage',
            cache: false,
            type: 'GET',   
            success: function(html){
                $('#page').html($(html).find('.wa_content').html());
                $('h2.contentheading').html('Chat');
            }
        });
        return false;        
    });
    $.tablesorter.addParser({
        id: "jDate",
        is: function(s) {
            return false;
        },
        format: function(s) {
            var dt = s.split('/');
            var s2 = dt[2]+'-'+dt[1]+'-'+dt[0];
            return $.tablesorter.formatFloat((s2 != "") ? new Date(s2.replace(new RegExp(/-/g),"/")).getTime() : "0");
        },
        type: "numeric" 
    });
    $.tablesorter.addParser({
        id: "jService",
        is: function(s) {
            return false;
        },
        format: function(s) {
            var dt = s.split('<br>');
            return dt[1];
        },
        type: "text" 
    });
    $(".clientareatable.pcts.pr").tablesorter({headers: { 0: {sorter: 'jService'}, 3: {sorter: 'jDate'}, 4: {sorter: false} } }); 
    $(".clientareatable.pcts.dm").tablesorter({headers: { 0: {sorter: 'jDate'}, 2: {sorter: 'jDate'}, 4: {sorter: false}, 5: {sorter: false} } });
});
