﻿/**
* @author Riaz Missaghi
*/

/*
* Jonathan Howard
*
* jQuery Pause
* version 0.2
*
* Requires: jQuery 1.0 (tested with svn as of 7/20/2006)
*
* Feel free to do whatever you'd like with this, just please give credit where
* credit is do.
*
*
*
* pause() will hold everything in the queue for a given number of milliseconds,
* or 1000 milliseconds if none is given.
*
*
*
* unpause() will clear the queue of everything of a given type, or 'fx' if no
* type is given.
*/
$.fn.pause = function(milli, type) {
    milli = milli || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $.dequeue(self);
        }, milli);
    });
};

$.fn.clearQueue = $.fn.unpause = function(type) {
    return this.each(function() {
        type = type || "fx";
        if (this.queue && this.queue[type]) {
            this.queue[type].length = 0;
        }
    });
};

jQuery.fn.checkbox = function(opt) {
    jQuery("input[@type='checkbox']", this).hide().each(function() {
        jQuery("<img>")
					.attr({ src: this.checked ? opt.checked : opt.unchecked })
					.click(function() {
					    var check = jQuery(this).next()[0].checked == true;
					    if (jQuery(this).next()[0].className.length > 0) {
					        jQuery("." + jQuery(this).next()[0].className)
								.each(function() {
								    this.checked = false;
								    jQuery(this).prev().attr({ src: opt.unchecked })
								})
					    }
					    jQuery(this)
							.attr({ src: check ? opt.unchecked : opt.checked })
							.next()[0].checked = !check;
					    jQuery("." + jQuery(this).parent("td")[0].className + " input[@name=packageType]").click()

					    TallyTable();

					})
					.insertBefore(this);
    });
}

var filingFeeNotRequired = false;

var promocode = '';
var promoamount = '';
var promopercentage = '';
var promodescription = '';

$(document).ready(function() {


    //function docReady() {s
    $(".showInc").toggle();
    $(".showLLC, .showNP").toggle();
    ;

    //
    $().checkbox({ checked: "img/checked.png", unchecked: "img/unchecked.png" });

    $('form').submit(function() {
        if ($('input[@name=promo_code]').attr('value') != null && $('input[@name=promo_code]').attr('value').length > 0) {
            if (promocode == $('input[@name=promo_code]').attr('value')) {
                return checkComplete();
            }
            else {
                $('#btnPromo').click();
                return false;
            }
        }
        else {
            return checkComplete();
        }


    });


    $("input[@name=chkSeal]").prev().one("click", function() {
        //$('#trKit').css({background});
        $('#sealDeal').fadeIn('fast', function() {
            $('#sealDeal').pause(2000).fadeOut('slow')
        });
    })

    $('.state').html($('#selectState').attr("value"));
    $('#txtState')[0].options[0].selected = true;
    $('#txtState').change(function() {
        changeState(this.value);
        $("#fillingFeeStd input[@checked]").prev().click();
        $(".fillingFeeExp input[@checked]").prev().click();
        $(".fillingFee24hr input[@checked]").prev().click();
    });

    $('#btnPromo').click(function() { $.getScript("promocode.asp?code=" + $("input[@name=promo_code]").attr('value')); return false; });

    $("#notice").hide();
    $("#NHnotice").hide();

    $("input[@name=packageType]").click(function() {
        
        SelectCol(this);
    });
    $("input[@name=packageType]")[0].click();

    //$(".chkAgent]").change(function() { alert("regagent"); gotStateInfo(); })

    $("#entityType")
					.change(function() {
					    typeChange(this);
					    $("#fillingFeeStd input[@checked]").prev().click();
					    $(".fillingFeeExp input[@checked]").prev().click();
					    $(".fillingFee24hr input[@checked]").prev().click();
					});
    //.each(function() {if (this.value == "scorp" || this.value == null) {$("#trSCorp td").show();} else {$("#trSCorp td").hide();} });

    $('#quotetable a').Tooltip({
        delay: 0,
        track: true,
        showBody: " - "
    });
    $('.srvAll').Tooltip({
        delay: 0,
        track: true,
        showBody: " - "
    });
    $('#notice a').Tooltip({
        delay: 0,
        track: true,
        showBody: " - "
    });

    $('#completebtn').click(function() {
        //alert("wtf");
        return checkComplete();
        //false; 
    });

    $("#fillingFeeStd").hide();
    $(".fillingFeeExp").hide();
    $(".fillingFee24hr").hide();

    //$("#np501c3").hide();


    //$("#quotetable, #FilingFee").show()
    $(".astate").text(" A ");
    //}
});

function typeChange(a) {
    if (a.value == "ccorp" || a.value == "pcorp") { $("#selectedEntity").attr('value', "INC"); }
    if (a.value == "scorp") { $("#selectedEntity").attr('value', "INC"); $("#trSCorp").show(); } else { $("#trSCorp").hide(); }
    if (a.value == "llc" || a.value == "pllc") { $("#selectedEntity").attr('value', "LLC"); $(".showLLC").show(); $(".showInc, .showNP").hide(); } else { $(".showInc").show(); $(".showLLC, .showNP").hide() }

    if (a.value == "npc") {
        $("#selectedEntity").attr('value', "NP");
        $(".showNP").show(); $(".showInc, .showLLC").hide();
        $("#np501c3").show();

        $("#trLanguage").show();
        $("#trFreeSample").show();
        $("#trBylaws").hide();
        $("#trCerts").hide();
        $("#chkENT")[0].value = 97;
        $("#chkULT")[0].value = 239;
        $("#chkBUS")[0].value = 377;
        $("#ENTprice").html("$97");
        $("#ULTprice").html("$239");
        $("#BUSprice").html("$377");
    }
    else {
        $("#np501c3").hide();
        if ($("#np501c3 input[@checked]").length > 0) {
            $("#np501c3 input[@checked]").prev().click();
        }
        $("#trLanguage").hide();
        $("#trFreeSample").hide();
        $("#trBylaws").show();
        $("#trCerts").show();
        $("#chkENT")[0].value = 97;
        $("#chkULT")[0].value = 239;
        $("#chkBUS")[0].value = 397;
        $("#ENTprice").html("$97");
        $("#ULTprice").html("$239");
        $("#BUSprice").html("$397");
    }
    TallyTable();
}

function gotStateInfo() {
    var expfee, stdfee, stdDesc, expDesc;
    //alert($("#entityType")[0].value);
    //if ($("#entityType")[0].value == 'llc') // llc selectedEntity checking wrong one
    if ($("#selectedEntity")[0].value.toLowerCase() == 'llc') // llc 
    { expfee = llc_expedite_fee; stdfee = llc_fee; stdDesc = llc_description; expDesc = llc_expedite_description; }
    else if ($("#selectedEntity")[0].value.toLowerCase() == 'np') //non-profit
    { expfee = parseInt(inc_expedite_fee) - parseInt(inc_fee) + parseInt(nonprofit_fee); stdfee = nonprofit_fee; ; stdDesc = inc_description; expDesc = inc_expedite_description; }
    else { expfee = inc_expedite_fee; stdfee = inc_fee; stdDesc = inc_description; expDesc = inc_expedite_description; } //inc
    $('#sfilingfee').html("$" + stdfee);
    $('#xfilingfee').html("$" + expfee);
    $('#filingfee24h').html("$" + String(parseInt(expfee) + 200));

    $('#stdServiceName').html(stdDesc); //service description
    $('#expServiceName').html(expDesc); //service description

    $('#sradFilingFee').attr("value", stdfee);
    $('#eradFilingFee').attr("value", expfee);
    $('#radFilingFee24h').attr("value", String(parseInt(expfee) + 200));

    //update blurbs
    $('.feeblurb').attr({ title: "State Filing Fees - <ul><li>" + stdDesc + " $" + stdfee + "</li><li>" + expDesc + " $" + expfee + "</li></ul>" });
    //reset then show hide filling services

    $("#fillingFeeStd").hide();
    $(".fillingFeeExp").hide();
    $(".fillingFee24hr").hide();

    $("#notice").hide();
    filingFeeNotRequired = true
    var regagent = "none";
    if ($("input[@name=chkAgent][@checked]").size() > 0)
    { regagent = $("input[@name=chkAgent][@checked]").parent("td")[0].className; }
    if ($("input[@name=packageType][@checked]").parent("td")[0].className == regagent) {
        filingFeeNotRequired = false;
        $("#fillingFeeStd").show();
        $(".fillingFeeExp").show();
        if (can_hand_file.toLowerCase() == 'yes') { $(".fillingFee24hr").show() }

    }
    else {
        if (state_filing.toLowerCase() == 'both') {
            filingFeeNotRequired = false;
            $("#fillingFeeStd").show();
            $(".fillingFeeExp").show();
            if (can_hand_file.toLowerCase() == 'yes') { $(".fillingFee24hr").show() }

        }
        else if (state_filing.toLowerCase() == 'llc') {
            if ($("#entityType")[0].value == 'llc') {
                filingFeeNotRequired = false;
                $("#fillingFeeStd").show();
                $(".fillingFeeExp").show();
                if (can_hand_file.toLowerCase() == 'yes') { $(".fillingFee24hr").show() }


            }
            else {
                $("#noticeArticles").html("Articles of Incorporation for all new corporations");
                $("#notice").show();
            }
        }
        else if (state_filing.toLowerCase() == 'inc') {
            if ($("#entityType")[0].value != 'llc') {
                filingFeeNotRequired = false;
                $("#fillingFeeStd").show();
                $(".fillingFeeExp").show();
                if (can_hand_file.toLowerCase() == 'yes') { $(".fillingFee24hr").show() }

            }
            else {
                $("#noticeArticles").html("Articles of Organization for all new LLC formations");
                $("#notice").show();
            }
        }
        else //if (state_filing.toLowerCase() == 'no')
        {
            if ($("#entityType")[0].value == 'llc') {
                $("#noticeArticles").html("Articles of Organization for all new LLC formations");
            }
            else {
                $("#noticeArticles").html("Articles of Incorporation for all new corporations");
            }
            $("#notice").show();
            $("#fillingFeeStd").hide();
            $(".fillingFeeExp").hide();
            $(".fillingFee24hr").hide();
        }



    }

    if (abbr == "NH" && $("#entityType")[0].value == 'llc') {
        $("#notice").hide();
        $("#NHnotice").show();
    }
    else
    { $("#NHnotice").hide(); }

    //hide options without descriptions
    if ($("#entityType")[0].value != 'llc') {
        if (inc_description.length < 1) { $("#fillingFeeStd").hide(); }
        if (inc_expedite_description.length < 1) { $(".fillingFeeExp").hide(); $(".fillingFee24hr").hide(); }
    }
    if ($("#entityType")[0].value == 'llc') {
        if (llc_description.length < 1) { $("#fillingFeeStd").hide(); }
        if (llc_expedite_description.length < 1) { $(".fillingFeeExp").hide(); $(".fillingFee24hr").hide(); }
    }

    //alert($('#FilingFee tr:visible').length);		 
    //alert($('#FilingFee tr:visible input[@checked]').length);
    if ($('#FilingFee tr:visible').length == 3) {
        if ($('#FilingFee tr:visible input[@checked]').length == 0) {

            $('#FilingFee tr:visible input').prev().click();
        }
    }
}

function changeState(newState) {
    var stateInfo = newState.split(",");
    $('#selectedState').attr('value', stateInfo[0]);
    $('.state').text(stateInfo[0] + " ");
    if (stateInfo[0].indexOf("A") + stateInfo[0].indexOf("E") + stateInfo[0].indexOf("I") + stateInfo[0].indexOf("O") + stateInfo[0].indexOf("U") > -5) {
        $('.astate').text(" An " + stateInfo[0] + " ");
    }
    else {
        $('.astate').text(" A " + stateInfo[0] + " ");
    }

    if (stateInfo[0] == "California") {
        $('#California')
					.show()
					.siblings().hide();
    }
    else {
        $('#California')
					.hide()
					.siblings().show();
    }
    $.getScript("stateinfo.asp?state=" + stateInfo[0], TallyTable());
}

function wtf() {
    alert('wtf');

}

function checkComplete() {
    var allcool = false;
    if ($("#txtState")[0].value == '0') {
        alert("Please select a state");

        $('#completebtn').parent().TransferTo({
            duration: 800,
            to: '#txtState',
            className: 'transferer1'
        });
        $('#main').ScrollTo(400, 'easeout');
        $('#txtState').prev("span").css({ color: "Red" });

    }
    else if ($("#entityType")[0].value == '0') {
        alert("Please select an Entity Type");

        $('#completebtn').parent().TransferTo({
            duration: 800,
            to: '#entityType',
            className: 'transferer1'
        });
        $('#main').ScrollTo(400, 'easeout');
        $('#entityType').prev("span").css({ color: "Red" });

    }
    else {
        if ($("input[@name=radFilingFee][@checked]").size() == 1 || filingFeeNotRequired) {
            var formfields = "";
            for (i = 0; i < document.forms[0].elements.length; i++) {
                formfields += "\n" + document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value
            }
            //alert(formfields);
            //alert($('#selectedPackage').attr('value')); 

            if ($('#entityType')[0].value == "npc") {
                document.forms[0].action = "order/fstartorderNP.asp";
            }
            else {
                document.forms[0].action = "order/fstartorder.asp";
            }

            //document.forms[0].submit(); 
            allcool = true;
        }
        else {
            //alert("Please select a state filing fee.");
            $('#completebtn').parent().TransferTo({
                duration: 500,
                to: '#FilingFee',
                className: 'transferer1'
            });
            $('#FilingFee').fadeIn(function() { $('.sff').css({ color: "red" }); });
        }

    }
    if (allcool) {
        if ($('#entityType')[0].value == "npc") {
            document.forms[0].action = "order/fstartorderNP.asp";
        }
        else {
            document.forms[0].action = "order/fstartorder.asp";
        }
    }
    return allcool;
}

function SelectCol(inputitem) {
    $("." + $(inputitem).parent("td")[0].className)
						.css({ background: "#e6f0f8" })
						.siblings().css({ background: "#fff" });

    //uncheck all other columns checkboxes and check this columns checkboxes where the image is checked

    $("." + $(inputitem).parent("td")[0].className)
						.siblings().find("input[@type='checkbox']").each(function() { this.checked = false; });
    $("." + $(inputitem).parent("td")[0].className)
						.find("input[@type='checkbox']").each(function() { var check = $(this).prev().attr("src").indexOf("unchecked") == -1; this.checked = check; });

    TallyTable();
    $('#selectedPackage').attr('value', $(inputitem).attr('id').replace('chk', ''));
}

function TallyTable() {

    if ($('#txtState')[0].value != '0' && window.inc_expedite_fee)
    { gotStateInfo(); }

    var total = parseInt($("input[@name=packageType][@checked]")[0].value);
    $("#SelPackPrice").html(total);
    $("#SelectedPackage").html($("input[@name=packageType][@checked]").siblings(".label").children(".packageName").html());

    //update filling fee hidden values
    if ($('#sradFilingFee')[0].checked) {
        $('#stateFee').attr('value', $('#sradFilingFee').attr('value'));
        $('#handFiling').attr('value', "0");
    }
    if ($('#eradFilingFee')[0].checked) {
        $('#stateFee').attr('value', $('#eradFilingFee').attr('value'));
        $('#handFiling').attr('value', "0");
    }
    if ($('#radFilingFee24h')[0].checked) {
        $('#stateFee').attr('value', $('#eradFilingFee').attr('value'));
        $('#handFiling').attr('value', "200");
    }

    $("input[@name=radFilingFee][@checked]").each(function() {
        $("#SelFilePrice").html(parseInt(this.value));
        //$("#SelectedFilingService").text($(this).parent().next().children(".serviceName").text())
        total = total + parseInt(this.value);
    });


    $('#totalsTable .itemized').remove();
    var selectedCol = $("input[@name=packageType][@checked]").parent("td")[0].className;
    $("#quotetable ." + selectedCol + " input[@type='checkbox']").each(function() {
        if (this.checked) {
            total += parseInt(this.value);
            $('#totalsTable').append("<tr class=\"itemized\"><td>" + $(this).parent().parent().children(".tdone").children('span:first').text() + " </td><td> $" + this.value + "</td></tr>");
        }
    });

    //promo

    if (promocode != '') {
        $("input[@name=promo_code]").attr('value', promocode);
        if (promoamount > 0) {

            total -= promoamount
            $('#totalsTable').append("<tr class=\"itemized\"><td>" + promodescription + " </td><td> ($" + formatAsMoney(promoamount) + ")</td></tr>");
        }
        else if (promopercentage > 0) {
            var discount = ((promopercentage / 100) * parseInt($("input[@name=packageType][@checked]")[0].value));
            total -= discount;
            $('#totalsTable').append("<tr class=\"itemized\"><td>" + promodescription + " </td><td> ($" + formatAsMoney(discount) + ")</td></tr>");
        }
    }



    $("#grandTotal").html(formatAsMoney(total));
    $("#btnTotal span").html(formatAsMoney(total));
    $("#txtTotal").attr('value', formatAsMoney(total));











}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt * 100)) / 100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00'
              : ((mnt * 10 == Math.floor(mnt * 10)) ?
                       mnt + '0' : mnt);
}
		