﻿/* General Functions */
function CallBack(dest, method, arguments, triggeredmethod) {
    try {
        // Moz supports XMLHttpRequest. IE uses ActiveX.  
        // browser detction is bad. object detection works for any browser  
        xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
        // browser doesn't support ajax. handle however you want  
    }
    // the xmlhttp object triggers an event everytime the status changes  
    // triggered() function handles the events  
    xmlhttp.onreadystatechange = eval(triggeredmethod);
    // open takes in the HTTP method and url.  
    var date = new Date();
    var unique = date.getDay() + date.getHours() + date.getMinutes() + date.getSeconds() + date.getMilliseconds();
    var dest = dest + "?method=" + method + "&" + unique + "&" + arguments;

    xmlhttp.open("GET", dest);
    // send the request. if this is a POST request we would have  
    // sent post variables: send("name=aleem&gender=male)  

    // Moz is fine with just send(); but  
    // IE expects a value here, hence we do send(null);  
    xmlhttp.send(null);
}

function HandleLoading(ptr, showhide, msg) {
    if (ptr) {
        var ptrHandle = document.getElementById(ptr);
        if (ptrHandle) {
            if (showhide == "1") {
                ptrHandle.innerHTML = "<center><br /><br /><img src='images/progress.gif' border='0' alt='Progress' title='Progress' /><br><br><br></center>";
            } else {
                ptrHandle.innerHTML = msg;
            }
        }
    }
}

function SwapImage(ptr, file) {
    if (ptr && file) {
        ptr.src = "images/" + file;
    }
}

function HideTag(ptr) {
    if (ptr) {
        var ptrHandle = document.getElementById(ptr);
        if (ptrHandle) {
            ptrHandle.style.display = "none";
        }
    }
}

function ShowTag(ptr) {
    if (ptr) {
        var ptrHandle = document.getElementById(ptr);
        if (ptrHandle) {
            ptrHandle.style.display = "block";
        }
    }
}

function getPosition(element) {
    var result = new Object();
    result.x = 0;
    result.y = 0;
    result.width = 0;
    result.height = 0;
    if (element.offsetParent) {
        result.x = element.offsetLeft;
        result.y = element.offsetTop;
        var parent = element.offsetParent;
        while (parent) {
            result.x += parent.offsetLeft;
            result.y += parent.offsetTop;
            var parentTagName = parent.tagName.toLowerCase();
            if (parentTagName != "table" &&
                parentTagName != "body" &&
                parentTagName != "html" &&
                parentTagName != "div" &&
                parent.clientTop &&
                parent.clientLeft) {
                result.x += parent.clientLeft;
                result.y += parent.clientTop;
            }
            parent = parent.offsetParent;
        }
    }
    else if (element.left && element.top) {
        result.x = element.left;
        result.y = element.top;
    }
    else {
        if (element.x) {
            result.x = element.x;
        }
        if (element.y) {
            result.y = element.y;
        }
    }
    if (element.offsetWidth && element.offsetHeight) {
        result.width = element.offsetWidth;
        result.height = element.offsetHeight;
    }
    else if (element.style && element.style.pixelWidth && element.style.pixelHeight) {
        result.width = element.style.pixelWidth;
        result.height = element.style.pixelHeight;
    }
    return result;
}

function PrintElement(element,x,y) {
    var a = window.open('', '', 'width=662,height=580,scrollbars=1,left='+x+',top='+y+',screenX='+x+',screenY='+y);
    a.document.open("text/html");
    a.document.write("<html><header>");
    a.document.write("<link rel='stylesheet' href='css/main.css' type='text/css' />");
    a.document.write("<STYLE type='text/css'>body {margin:0;padding:0;}</STYLE>");
    a.document.write("</header><body><div class='custom-doc'>");
    a.document.write("<div class='print_header'>National Center for Education Statistics QuickStats</div><br><br>");
    var output = "";
    var input = document.getElementById(element).innerHTML;
    var startTabs = input.indexOf("<!-- TABS START HERE -->");
    if (startTabs != -1) {
        var endTabs = input.indexOf("<!-- TABS END HERE -->");
        var output_1 = input.substring(0, startTabs);
        var output_2 = input.substring(endTabs);
        output = output_1 + output_2;
    } else {
        output = document.getElementById(element).innerHTML;
    }
    a.document.write(output);
    a.document.write("</div></body></html>");
    a.document.close();
    a.print();
    //a.close();
}

function PrintAgreement(element, x, y) {
    var a = window.open('', '', 'width=500,height=570,scrollbars=1,left=' + x + ',top=' + y + ',screenX=' + x + ',screenY=' + y);
    a.document.open("text/html");
    a.document.write("<html><header>");
    a.document.write("<link rel='stylesheet' href='css/index.css' type='text/css' />");
    a.document.write("<STYLE type='text/css'>body {margin:0;padding:0;}</STYLE>");
    a.document.write("</header><body><div class='indexmain'>");
    a.document.write("<div class='print_header'>National Center for Education Statistics QuickStats</div><br><br>");
    var output = "";
    var input = document.getElementById(element).innerHTML;
    a.document.write(input);
    a.document.write("</div></body></html>");
    a.document.close();
    a.print();
    //a.close();
}

function SetTimeOutMethod(func, delay) {
    timer = setTimeout(func, delay);
}

function ClearTimeOutMethod() {
    if (timer) {
        clearTimeout(timer);
        timer = null;
    }
}

function PrintMsg(msg, e) {
    if (e) {
        var ptr = document.getElementById(e);
        if (ptr) {
            ptr.value = msg;
        }
    }
}

// To use:
// var windowDimensionsObject = getWindowDimensions();
// alert('Width: ' + windowDimensionsObject.width + ' Height: ' + windowDimensionsObject.height);
getWindowDimensions = function() {
    var windowWidth = 0;
    var windowHeight = 0;

    if ((document.documentElement) && (document.documentElement.clientWidth))
        windowWidth = document.documentElement.clientWidth;
    else if ((document.body) && (document.body.clientWidth))
        windowWidth = document.body.clientWidth;
    else if ((document.body) && (document.body.offsetWidth))
        windowWidth = document.body.offsetWidth;
    else if (window.innerWidth)
        windowWidth = window.innerWidth - 18;

    if ((document.documentElement) && (document.documentElement.clientHeight))
        windowHeight = document.documentElement.clientHeight;
    else if ((document.body) && (document.body.clientHeight))
        windowHeight = document.body.clientHeight;
    else if ((document.body) && (document.body.offsetHeight))
        windowHeight = document.body.offsetHeight;
    else if (window.innerHeight)
        windowHeight = window.innerHeight - 18;

    return { width: windowWidth, height: windowHeight };
}

function GrayOutPage(coverId) {
    var _isIE = (window.navigator.appName.toLowerCase().indexOf('explorer') != -1);
    var cvr = document.getElementById(coverId);
    cvr.style.display = "block";

    cvr.style.width = "100%";
    if (_isIE) {
        cvr.style.height = (document.body.offsetHeight + 30) + "px";  //"1000%";
    }
    else {
        //PrintDvRaed(document.body.clientHeight+","+document.documentElement.clientHeight+","+window.innerHeight);
        cvr.style.height = document.documentElement.clientHeight + window.scrollMaxY + "px";
    }
}

function UndoGrayOutPage(coverId) {
    var cvr = document.getElementById(coverId)
    cvr.style.display = "none";
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

String.prototype.replaceAll = function(pcFrom, pcTo) {
    var i = this.indexOf(pcFrom);
    var c = this;
    var previ = i;
    
    while (i > -1) {
        previ = i;
        c = c.replace(pcFrom, pcTo);
        i = c.indexOf(pcFrom, (previ*1+pcTo.length));
    }
    return c;
}

String.prototype.encodeHTML = function() {
    var str = this;
    //str = escape(str);
    str = str.replace(/\//g, "%2F");
    str = str.replace(/\?/g, "%3F");
    str = str.replace(/=/g, "%3D");
    str = str.replace(/&/g, "%26");
    //str = str.replace(/@/g, "%40");
    //str = str.replace(/</g, "&lt;");
    //str = str.replace(/>/g, "&gt;");
    str = str.replace(/\+/g, "%2B");
    //str = str.replace(/\\/g, "%5C");
    
    return str;
}

function ResetForm(ptr) {
    if (ptr) {
        var arrElements = document.getElementById(ptr).childNodes;
        if (arrElements && arrElements.length > 0) {
            for (var i = 0; i < arrElements.length; i++) {
                // Firefox puts in lots of #text nodes...skip these
                if (arrElements[i].nodeName == '#text') continue;

                if (arrElements[i].checked) {
                    arrElements[i].checked = false;
                }

                if (arrElements[i].childNodes.length > 0) {
                    ResetForm(arrElements[i].id);
                }
            }
        }
    }
} 
