$(function(){

$('#main h3 a').hover(
    function(){
        $(this).next('.pop').css('left', $(this).width()).css('top', -($(this).next('.pop').height())+$(this).height()+8).show().parent().css('z-index', 100);
    },
    function(){
        $(this).next('.pop').hide().parent().css('z-index', 0);
    }
);

$('.gallery a:first').addClass('a');
$('.gallery a').click(function(){
    $('.gallery a').removeClass();
    $(this).addClass('a').blur();
    return false;
});


// voip
$('.voip #x').keypress(function(e){
    if (e.which > 0 && e.which != 8 && (e.which < 48 || e.which > 57)) return false;
}).keyup(function(event){
    var x = parseInt($(this).val());
    x = isNaN(x) ? 0 : x;
    var ansX = 0;
    var invoice = 0;
    var cost = 0;

    if (x > 0) {
        if (x < 1300000) {
            ansX = 1000;
        } else if (x < 3000000) {
            ansX = x * 0.00077;
        } else if (x < 5000000) {
            ansX = x * 0.00067;
        } else if (x < 10000000) {
            ansX = x * 0.0006;
        } else if (x < 18000000) {
            ansX = x * 0.0005;
        } else if (x < 32000000) {
            ansX = x * 0.00044;
        } else if (x >= 32000000) {
            ansX = 'call';
        }
    }

    if (ansX == 'call') {
        $('.voip #invoice').html('For special rate offer please call +1&nbsp;212-221-0057');
        $('.voip #cost').html('');
    } else {
        monthly = ansX;
        invoice = monthly > 0 ? monthly.toFixed(2) + ' USD' : '';
        cost = x > 0 ? monthly / x : '';
        if (cost > 0) {
            if (cost < 0.00001) cost = 0.00001;
            else if (cost < 0.001) cost = cost.toFixed(5);
            else if (cost < 0.01) cost = cost.toFixed(4);
            else if (cost < 0.1) cost = cost.toFixed(3);
            else cost = cost.toFixed(2);
            cost = cost + ' USD';
        }
        $('.voip #invoice').html(invoice);
        $('.voip #cost').html(cost);
    }
});


// tdm
var tdmW = $('.tdm #w');
var tdmX = $('.tdm #x');
var tdmY = $('.tdm #y');
var tdmZ = $('.tdm #z');
var tdmA = $('.tdm #a');
var tdmB = $('.tdm #b');
var tdmC = $('.tdm #c');
var tdmD = $('.tdm #d');

$('.tdm input[type="text"]').keypress(function(e){
    if (e.which > 0 && e.which != 8 && (e.which < 48 || e.which > 57)) return false;
});
$('.tdm input[type="text"]').keyup(function(){
    tdmCount();
});
$('.tdm input[type="checkbox"]').click(function(){
    tdmCount();
});

function tdmCount() {
    var x = parseInt(tdmX.val());
    x = isNaN(x) ? 0 : x;
    var y = parseInt(tdmY.val());
    y = isNaN(y) ? 0 : y;
    var z = parseInt(tdmZ.val());
    z = isNaN(z) ? 0 : z;
    var w = parseInt(tdmW.val());
    w = isNaN(w) ? 0 : w;
    var d = tdmD.attr('checked');
    var a = tdmA.attr('checked');
    var b = tdmB.attr('checked');
    var c = tdmC.attr('checked');
    var ansX = 0;
    var ansY = 0;
    var ansZ = 0;
    var ans_checkboxA = 0;
    var ans_checkboxB = 0;
    var ans_checkboxC = 0;
    var ans_checkboxD = 0;
    var invoice = 0;
    var cost = 0;

    ansX = x > 0 ? 1000 : 0;

    if (y > 0) {
        if (y <= 3) {
            ansY = 1000;
        } else if (y < 5) {
            ansX = x * 300;
            ansY = y * 300;
        } else if (y < 21) {
            ansX = x * 220;
            ansY = y * 210;
        } else if (y < 42) {
            ansX = x * 180;
            ansY = y * 170;
        } else if (y < 63) {
            ansX = x * 150;
            ansY = y * 150;
        } else if (y >= 63) {
            ansX = 'call';
            ansY = 'call';
        }
    }

    if (z == 1) {
        ansZ = 200;
    } else if (z == 2) {
        ansZ = 300;
    } else if (z >= 3) {
        ansZ = z * 100;
    }

    ansA = a ? 1.3 : 1;
    ansB = b ? 1 : 1;
    ansC = c ? 1 : 1;
    ansD = d ? 1.1 : 1;

//    alert('(' + ansX + '+' + ansY + '+' + ansZ + ')' + '*' + ansA + '*' + ansB + '*' + ansC + '*' + ansD);

    if (ansX == 'call') {
        $('.tdm #invoice').html('For special rate offer please call +1&nbsp;212-221-0057');
        $('.tdm #cost').html('');
    } else {
        monthly = (ansX + ansY + ansZ) * ansA * ansB * ansC * ansD;
        monthly = (monthly > 0 && monthly < 997) ? 997 : monthly;
        invoice = monthly > 0 ? '$ ' + monthly.toFixed(2) : '';
        cost = (monthly > 0 && w > 0) ? monthly / w : '';
        if (cost > 0) {
            if (cost < 0.00001) cost = 0.00001;
            else if (cost < 0.001) cost = cost.toFixed(5);
            else if (cost < 0.01) cost = cost.toFixed(4);
            else if (cost < 0.1) cost = cost.toFixed(3);
            else cost = cost.toFixed(2);
            cost = '$ ' + cost;
        }
        $('.tdm #invoice').html(invoice);
        $('.tdm #cost').html(cost);
    }
}


// mycost
var mycostX = $('.mycost #x');
var mycostY = $('.mycost #y');
var mycostZ = $('.mycost #z');
var mycostA = $('.mycost #a');
var mycostB = $('.mycost #b');
var mycostC = $('.mycost #c');
var mycostD = $('.mycost #d');
var mycostE = $('.mycost #e');

$('.mycost input[type="text"]').keypress(function(e){
    if (e.which > 0 && e.which != 8 && (e.which < 48 || e.which > 57)) return false;
});
$('.mycost input[type="text"]').keyup(function(){
    mycostCount($(this));
});


function mycostCount(field) {
    var x = parseInt(mycostX.val());
    x = isNaN(x) ? '' : x;
    var y = parseInt(mycostY.val());
    y = isNaN(y) ? '' : y;
    var z = parseInt(mycostZ.val());
    z = isNaN(z) ? '' : z;
    var a = parseInt(mycostA.val());
    a = isNaN(a) ? '' : a;
    var b = parseInt(mycostB.val());
    b = isNaN(b) ? '' : b;
    var c = parseInt(mycostC.val());
    c = isNaN(c) ? '' : c;
    var d = parseInt(mycostD.val());
    d = isNaN(d) ? '' : d;
    var e = parseInt(mycostE.val());
    e = isNaN(e) ? '' : e;

    var ansY = y;
    var ansZ = z;
    var ansA = a;
    var ansB = b;
    var ansC = c;
    var ansD = d;
    var ansE = e;

    var cost = 0;
    var netpermin = 0;
    var rroffer = 0;
    var savings = 0;

    if (field.attr('id') == 'x') {

        if (x >= 40) {
            ansY = 150000;
            ansZ = 40000;
            ansA = 30000;
            ansB = 20000;
            ansC = 70000;
            ansD = 15000;
        } else if (x >= 20) {
            ansY = 100000;
            ansZ = 30000;
            ansA = 30000;
            ansB = 15000;
            ansC = 50000;
            ansD = 15000;
        } else if (x >= 8) {
            ansY = 80000;
            ansZ = 20000;
            ansA = 30000;
            ansB = 10000;
            ansC = 25000;
            ansD = 10000;
        } else if (x >= 4) {
            ansY = 50000;
            ansZ = 15000;
            ansA = 10000;
            ansB = 10000;
            ansC = 15000;
            ansD = 5000;
        } else if (x >= 1) {
            ansY = 30000;
            ansZ = 10000;
            ansA = 10000;
            ansB = 10000;
            ansC = 10000;
            ansD = 0;
        } else {
            ansY = 0;
            ansZ = 0;
            ansA = 0;
            ansB = 0;
            ansC = 0;
            ansD = 0;
        }

        if (field.attr('id') == 'x' || field.attr('id') == 'z') ansE = ansZ/2;
    }
//alert('ansY'+ansY+', ansZ'+ansZ+', ansA'+ansA+', ansB'+ansB+', ansC'+ansC+', ansD'+ansD+', ansE'+ansE);
    $('.mycost #y').val(ansY > 0 ? ansY : '');
    $('.mycost #z').val(ansZ > 0 ? ansZ : '');
    $('.mycost #a').val(ansA > 0 ? ansA : '');
    $('.mycost #b').val(ansB > 0 ? ansB : '');
    $('.mycost #c').val(ansC > 0 ? ansC : '');
    $('.mycost #d').val(ansD > 0 ? ansD : '');
    $('.mycost #e').val(ansE > 0 ? ansE : '');

    if (x > 0) {
        cost = ansY + ansZ + ansA + ansB + ansC + ansD + ansE;
        netpermin = cost / (x * 12000000);
        if (x <= 3) {
            rroffer = 0.00067;
        } else if (x <= 5) {
            rroffer = 0.0006;
        } else if (x <= 10) {
            rroffer = 0.0005;
        } else if (x > 10) {
            rroffer = 0.00044;
        }
        savings = cost - (x * 12000000 * rroffer);

        if (netpermin > 0) {
            if (netpermin < 0.00001) netpermin = 0.00001;
            else if (netpermin < 0.001) netpermin = netpermin.toFixed(5);
            else if (netpermin < 0.01) netpermin = netpermin.toFixed(4);
            else if (netpermin < 0.1) netpermin = netpermin.toFixed(3);
            else netpermin = netpermin.toFixed(2);
        }
        rroffer = rroffer.toFixed(5);
        savings = savings > 0 ? savings.toFixed(0) : 0;

        $('.mycost #cost').html('$ '+cost);
        $('.mycost #netpermin').html('$ '+netpermin);
        $('.mycost #rroffer').html('$ '+rroffer);
        $('.mycost #savings').html('$ '+savings);
    } else {
        $('.mycost #cost').html('');
        $('.mycost #netpermin').html('');
        $('.mycost #rroffer').html('');
        $('.mycost #savings').html('');
    }
}


});
