$(document).ready(function() {
    $(".printFriendly").click(function() {
        var thisPage = $("body").html();
        var thisTitle = document.title;
        var fullWindowText = '<head><title>' + thisTitle + '</title><link rel="stylesheet" type="text/css" href="/styles/printwindow.css" media="screen" /><link rel="stylesheet" type="text/css" href="/styles/print.css" media="print" /><script type="text/javascript" src="/scripts/jquery.js"></script></head><body><div id="utils"><h6 class="printThis"><a href="#" onclick="javascript:window.print();return false;">Print this page</a></h6><h6 class="closeThis"><a href="#" onclick="javascript:window.close();return false;">Close this page</a></h6></div>' +
            thisPage + '</body></html>';
        var printWindow = window.open("", "printPreview", "toolbar=0,scrollbars=1,status=0,location=0,width=720,height=600");
        printWindow.document.write(fullWindowText);
        printWindow.document.close();
        printWindow.location.reload(true);
        printWindow.focus();
        return false;
    });

    //if(navigator.userAgent.indexOf("AppleWebKit/419.3") == -1) {
    //$("a").each(function(i,e){$(e).html($(e).html()+"<span class='printOnly'>["+this.href+"]</span>");});
    //}

    $("#logIn").click(function() {
        var visState = $("#loginForm").css("visibility");
        if (visState == "hidden") {
            $("#loginForm").css("display", "none").css("visibility", "visible").toggle("slow");
        }
        else {
            $("#loginForm").toggle("slow");
        }
    });
    $("input#inputMastheadSearch, input#inputKeyword").defaultvalue(
		"Enter Search Term",
		"Enter descriptive term or phase"
	);
    $("input#inputFirstName, input#inputLastName, input#inputAddress1, input#inputAddress2, input#inputCity, input#inputZip, input#inputEmail, input#inputPhone, input#inputIDNum, input#inputGroupName, textarea#textareaComments").defaultvalue(
		"Enter your first name",
		"Enter your last name",
		"Enter your street address",
		"Enter your street address",
		"Enter your city",
		"Enter your ZIP Code",
		"Enter valid email address",
		"Enter phone number",
		"Enter policy/group number",
		"Enter group name",
		"Enter comment, question or feedback"
	);
    $("button, .button").corner("6px");
    $("ul#sectionNav li a.sectionNavItem").corner("8px");
    $("ul.secondaryNav").corner("5px");
    $("#calloutGoTo a").corner("5px");
    $("div.dynamicTableContainer").corner("6px");
    $("img.sectionLandingImage").corner("6px");
    $("#calloutMyProfile .callout").corner("6px");
    $("#calloutMarketingMaterials").corner("6px");
    $(".dynamicTable tr:even td").css("background-color", "#d9ecf4");
    $(".innerCalloutContainer .callout").corner("5px");
    // Increase Font Size
    $("#increaseTextSize").click(function() {
        var currentFontSize = $('body').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        if (currentFontSizeNum <= 18) {
            var newFontSize = currentFontSizeNum * 1.1;
            $('body').css('font-size', newFontSize);
            $("#decreaseTextSize").html("<a href='#'>-</a>");
            $("#decreaseTextSize a").css("display", "inline");
            if (newFontSize > 18) {
                $("#increaseTextSize").html("<span>+</span>");
                $("#increaseTextSize a").css("display", "none");
            }
        }
        else {
            $("#increaseTextSize").html("<span>+</span>");
            $("#increaseTextSize a").css("display", "none");
        }
        return false;
    });
    // Decrease Font Size
    $("#decreaseTextSize").click(function() {
        var currentFontSize = $('body').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        if (currentFontSizeNum >= 14.5) {
            var newFontSize = currentFontSizeNum * 0.9;
            $('body').css('font-size', newFontSize);
            $("#increaseTextSize").html("<a href='#'>+</a>");
            $("#increaseTextSize a").css("display", "inline");
            if (newFontSize < 14.5) {
                $("#decreaseTextSize").html("<span>-</span>");
                $("#decreaseTextSize a").css("display", "none");
            }
        }
        else {
            $("#decreaseTextSize").html("<span>-</span>");
            $("#decreaseTextSize a").css("display", "none");
        }
        return false;
    });
});

function NewWindow(i){

    text = "Your are now leaving the USAble Life website and being linked to the website of an unrelated third party." ;
    text += "\n";
    text += "USAble Life is providing this link solely as a convinience to obtain information and not as an endorsement of the content of the third party site or any products or services offered on that site.";
    text += "\n";
    text += "USAble Life is not responsible for the products or services offered or the content of any linked site or any linked site contained in a linked site and does not make any representations regarding the cost or quality of products or services offered, or the content or accuracy of the materials on such sites.";
    if(confirm(text))
        {window.open(i,"aux_window","height=500,width=600,scrollbars=1,resizable=1,status=yes,toolbar=no,menubar=no,location=no");};
    }

function textCounter(field, countfield, maxsize) {
    if (field.value.length > maxsize) {
        field.value = field.value.substring(0, maxsize);
    }
    else {
        countfield.removeChild(countfield.lastChild);
        countfield.appendChild(document.createTextNode(maxsize - field.value.length));
    }
}
function set_flashmap_state(partN) {
    if (partN != "") {
        display_selected_state(partN);
    }    
    window.document.usa_locator.SetVariable("perem1", "state" + get_state(partN));
}


function display_selected_state(stateName) {
    alert("You have selected " + stateName +
                        ".\nPlease complete the form below with your information, then select " +
                        "the USAble Life products you are intersted in. Click the submit button to send " +
                        "your information to our Representatives who will contact you as soon as possible. " +
                        "Thank you.");

}

function get_state(state_name) {
    var state_array = [['', 0],
                        ['Alabama', 31],
                        ['Alaska', 49],
                        ['Arizona', 23],
                        ['Arkansas', 27],
                        ['California', 15],
                        ['Colorado', 18],
                        ['Connecticut', 43],
                        ['Delaware', 45],
                        ['District of Columbia', 51],
                        ['Florida', 33],
                        ['Georgia', 32],
                        ['Hawaii', 50],
                        ['Idaho', 2],
                        ['Illinois', 12],
                        ['Indiana', 13],
                        ['Iowa', 11],
                        ['Kansas', 20],
                        ['Kentucky', 22],
                        ['Louisiana', 28],
                        ['Maine', 40],
                        ['Maryland', 35],
                        ['Massachusetts', 41],
                        ['Michigan', 7],
                        ['Minnesota', 5],
                        ['Mississippi', 29],
                        ['Missouri', 21],
                        ['Montana', 3],
                        ['Nebraska', 19],
                        ['Nevada', 16],
                        ['New Hampshire', 39],
                        ['New Jersey', 44],
                        ['New Mexico', 24],
                        ['New York', 37],
                        ['North Carolina', 47],
                        ['North Dakota', 4],
                        ['Ohio', 14],
                        ['Oklahoma', 26],
                        ['Oregon', 8],
                        ['Pennsylvania', 36],
                        ['Rhode Island', 42],
                        ['South Carolina', 48],
                        ['South Dakota', 10],
                        ['Tennessee', 30],
                        ['Texas', 25],
                        ['Utah', 17],
                        ['Vermont', 38],
                        ['Virginia', 46],
                        ['Washington', 1],
                        ['West Virginia', 34],
                        ['Wisconsin', 6],
                        ['Wyoming', 9]];
    for (var index = 0; index < state_array.length; index++) {
        if (state_array[index][0] == state_name) {
            return state_array[index][1];
        }        
    }
}
window.blockConfirm = function(text, mozEvent, oWidth, oHeight, callerObj, oTitle) {
    var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually 
    //Cancel the event 
    ev.cancelBubble = true;
    ev.returnValue = false;
    if (ev.stopPropagation) ev.stopPropagation();
    if (ev.preventDefault) ev.preventDefault();

    //Determine who is the caller 
    var callerObj = ev.srcElement ? ev.srcElement : ev.target;

    //Call the original radconfirm and pass it all necessary parameters 
    if (callerObj) {
        //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again. 
        var callBackFn = function(arg) {
            if (arg) {
                callerObj["onclick"] = "";
                if (callerObj.click) callerObj.click(); //Works fine every time in IE, but does not work for links in Moz 
                else if (callerObj.tagName == "A") //We assume it is a link button! 
                {
                    try {
                        eval(callerObj.href)
                    }
                    catch (e) { }
                }
            }
        }

        radconfirm(text, callBackFn, oWidth, oHeight, callerObj, oTitle);
    }
    return false;
}
window.radBlockConfirm = function(text, mozEvent, oWidth, oHeight, callerObj, oTitle) {
    var ev = mozEvent ? mozEvent : window.event; //Moz support requires passing the event argument manually 
    //Cancel the event 
    ev.cancelBubble = true;
    ev.returnValue = false;
    if (ev.stopPropagation) ev.stopPropagation();
    if (ev.preventDefault) ev.preventDefault();

    //Determine who is the caller 
    var callerObj = ev.srcElement ? ev.srcElement : ev.target;

    //Call the original radconfirm and pass it all necessary parameters 
    if (callerObj) {
        //Show the confirm, then when it is closing, if returned value was true, automatically call the caller's click method again. 
        var callBackFn = function(arg) {
            if (arg) {
                callerObj["onclick"] = "";
                if (callerObj.click) callerObj.click(); //Works fine every time in IE, but does not work for links in Moz 
                else if (callerObj.tagName == "A") //We assume it is a link button! 
                {
                    try {
                        eval(callerObj.href)
                    }
                    catch (e) { }
                }
            }
        }

        radconfirm(text, callBackFn, oWidth, oHeight, callerObj, oTitle);
    }
    return false;
}
function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}
function CloseWindow() {
    var window = GetRadWindow();
    window.close();
}
function RedirectToLoginPage(time)
{
    setTimeout("location.href='/Login.aspx'", time);
}
function getParameterByName(name) {
    name = name.replace("/[\[]/", "\\\[").replace("/[\]]/", "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
//From http://www.web-source.net/web_development/currency_formatting.htm
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function CommaFormatted(amount)
{
    var sign = "$";
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	if (minus == '-')
	    amount = "(" + sign + amount + ")";
	else
	    amount = sign + amount;
	return amount;
}
// http://blog.pengoworks.com/index.cfm/2009/4/21/Fixing-jQuerys-slideDown-effect-ie-Jumpy-Animation
// this is a fix for the jQuery slide effects
function slideToggle(el, bShow) {
    var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");

    // if the bShow isn't present, get the current visibility and reverse it
    if (arguments.length == 1) bShow = !visible;

    // if the current visiblilty is the same as the requested state, cancel
    if (bShow == visible) return false;

    // get the original height
    if (!height) {
        // get original height
        height = $el.show().height();
        // update the height
        $el.data("originalHeight", height);
        // if the element was hidden, hide it again
        if (!visible) $el.hide().css({ height: 0 });
    }

    // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
    if (bShow) {
        $el.show().animate({ height: height }, { duration: 250 });
    } else {
        $el.animate({ height: 0 }, { duration: 250, complete: function () {
            $el.hide();
        }
        });
    }
}
