﻿
function rates_startup() {

    function getNodeName(node) {
        if (typeof (node.localName) == "undefined") {
            return node.baseName;
        }
        return node.localName;
    }

    function document_importNode(node) {
        if (typeof (node) == "undefined") {
            return node;
        }        
        
        var i;

        switch (node.nodeType) {
            case 1: //ELEMENT_NODE
                var newNode = document.createElement(getNodeName(node));

                if (node.attributes && node.attributes.length > 0) {
                    for (i = 0; i < node.attributes.length; ++i) {
                        try {
                            if (node.attributes[i].nodeName == "class") {
                                newNode.setAttribute("className", node.getAttribute(node.attributes[i].nodeName));
                                newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i].nodeName));
                            } else {

                                newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i].nodeName));


                                if (node.attributes[i].nodeName == "onclick") {
                                    newNode.onclick = new Function(node.getAttribute(node.attributes[i].nodeName));
                                }
                            }

                        }
                        catch (e) { }
                    }
                }

                if (node.childNodes && node.childNodes.length > 0)
                    for (i = 0; i < node.childNodes.length; ++i) {
                    newNode.appendChild(document_importNode(node.childNodes[i]));
                }
                return newNode;
                break;
            case 3: //TEXT_NODE
            case 4: //CDATA_SECTION_NODE
                return document.createTextNode(node.nodeValue);
                break;
        }
    }
    
    
    function readXML(xml) {
        // The XML data uses namespaces.
        // I found that browsers are inconsistent with xmlns.
        // Some treat the tag name as the unqualified name and some include
        // the namespace.
        var SourceTable;
        try {
            SourceTable = xml.getElementsByTagName("div")[0];
        }
        catch (e) { }

        try {
            if (SourceTable != undefined && SourceTable != null) {
                var TargetTable = document_importNode(SourceTable);
                document.getElementById('ratesPlaceholder').appendChild(TargetTable);
            }
        }
        catch (e) { }
    }    

    function processxml() {
    
        // tack a unique parameter to the request to bust the cache
        // jQuery also has an option to disable the cache. I don't know
        // how reliable this is, but tacking on the time should always work.        
        var param = "ms=" + new Date().getTime();
        var url = "MortgageRates/MortgageRates/Default.aspx?" + param;
        
        
        
        jQuery.ajax({ url: url, type: 'GET', cache: false, dataType: 'xml', 
            beforeSend : function() {
                Effect.Appear(jQuery("#ratesSpinner")[0], { duration: 2 });
            },
            complete : function() {
                Effect.Fade(jQuery("#ratesSpinner")[0], { duration: 0.25 });
                
            },
            success: function(data) {
            readXML(data);
        } 
        });       
        
    }

    processxml();
}

function OpenPopup(Url) {
    window.open(Url, "Popup", "menubar=0,location=1,status=0,scrollbars=1,width=650, height=500");
    return false;
}

//jQuery(document).ready(function() {
    rates_startup();
//});