// JScript source code
// JScript File

var isFRB;
var isSpanish;
var printContent;

function assignDivText(divName, divMessage) {
    var div = document.getElementById(divName);
    div.innerHTML = divMessage.replace(/\@/g, "");
}

function alertText(divMessage) {
    alert(divMessage.replace(/\@/g, "\n"));
}

function CheckInitial(tboxBalance, tboxRate) {
    var valBalance = document.getElementById(tboxBalance).value;
    var valRate = document.getElementById(tboxRate).value;

    valBalance = RemoveChar(valBalance, ",");
    valRate = RemoveChar(valRate, ",");
    
    valBalance = RemoveChar(valBalance, "$");
    valRate = RemoveChar(valRate, "%");

    valBalance = RemoveChar(valBalance, " ");
    valRate = RemoveChar(valRate, " ");


    var valBalance = Math.ceil(valBalance);
    var valRate = parseFloat(valRate);

    var message = "";
    var alertMessage = "";

    
    var div = document.getElementById('divInitError');
    div.innerHTML = "";
    
    if (!isSpanish) {
        if (valBalance == "" || isNaN(valBalance) || valBalance <= 0) {
            message = "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Provide a positive numeric value for balance.</div><br>";
            alertMessage = alertMessage + "Provide a positive numeric value for balance.\n";
        }

        if (valRate == "" || isNaN(valRate) || valRate <= 0) {
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Provide a positive numeric value for interest rate.</div><br>";
            alertMessage = alertMessage + "Provide a positive numeric value for interest rate.\n";
        }
    }
    else {
        if (valBalance == "" || isNaN(valBalance) || valBalance <= 0) {
            message = "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Proporcione un valor numerico positivo para el saldo total.</div><br>"; //Provide a positive numeric value for balance.
            alertMessage = alertMessage + "Proporcione un valor numerico positivo para el saldo total.\n"; //Provide a positive numeric value for balance.
        }

        if (valRate == "" || isNaN(valRate) || valRate <= 0) {
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Proporcione un valor numerico para el porcentaje anual (APR).</div><br>";//Provide a positive numeric value for interest rate.
            alertMessage = alertMessage + "Proporcione un valor numerico para el porcentaje anual (APR).\n";//Provide a positive numeric value for interest rate.
        }
    }

    if (message == "") {
        div.style.display='none';
        document.getElementById(tboxBalance).value = valBalance;
        document.getElementById(tboxRate).value = valRate;
        AddZeros(document.getElementById(tboxRate));
        return true;
    }
    div.innerHTML = message;
    alert(alertMessage);
    return false;
}

function CheckYears(tboxYears, hdfldTerm) {
    var valYears = document.getElementById(tboxYears).value;
    
    var maxTerm = document.getElementById(hdfldTerm).value;
    
    valYears = RemoveChar(valYears, ",");

    valYears = RemoveChar(valYears, " ");
    
    var valYears = parseInt(valYears);
    
    var maxTerm = parseInt(maxTerm);
    
    var div = document.getElementById('divYearError');
    div.innerHTML = "";
    
    var message = "";
    var alertMessage = "";
    
    if (!isSpanish) {
        if (valYears == "" || isNaN(valYears) || valYears <= 0) {
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Provide a positive whole number for years.<br></div>";
            alertMessage = alertMessage + "Provide a positive whole number for years.";
        }
        if(message == "" && valYears >= maxTerm){
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;The number of years you entered is longer than your estimated repayment period.<br></div>";
            alertMessage = alertMessage + "The number of years you entered is longer than your estimated repayment period.";
        }
    }
    else {
        if (valYears == "" || isNaN(valYears) || valYears <= 0) {
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Proporcione un numero entero positivo para anos.<br></div>";//Provide a positive whole number for years.
            alertMessage = alertMessage + "Proporcione un numero entero positivo para anos.";//Provide a positive whole number for years.
        }
        if(message == "" && valYears >= maxTerm){
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;El número de años que ha introducido es más largo que su período de amortización estimado.<br></div>";//The number of years you entered is longer than your estimated repayment period.
            alertMessage = alertMessage + "El número de años que ha introducido es más largo que su período de amortización estimado.";//The number of years you entered is longer than your estimated repayment period.
        }
    }
    
    if (message == "") {
        div.style.display='none';
        return true;
    }
    
    showhide('answer1', false); 
    div.innerHTML = message;
    alert(alertMessage);
    return false;
}

function CheckPayment(tboxPayment) {
    var valPayment = document.getElementById(tboxPayment).value;

    valPayment = RemoveChar(valPayment, ",");

    valPayment = RemoveChar(valPayment, " ");
    
    valPayment = RemoveChar(valPayment, "$");

    var valPayment = Math.ceil(valPayment);
    
    var div = document.getElementById('divPaymentError');
    div.innerHTML = "";
    
    var message = "";
    var alertMessage = "";
    
    
    if (valPayment == "" || isNaN(valPayment) || valPayment <= 0) {
        if (!isSpanish){
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Provide a positive numeric value for monthly payment.<br></div>";
            alertMessage = alertMessage + "Provide a positive numeric value for monthly payment.";  
        }
        else
        {
            message = message + "<div class='error'><img src='gifjpg/error_icon.gif' align='absmiddle'/>&nbsp;Propocione un numero entero positivo para el pago mensual.<br></div>";//Provide a positive numeric value for monthly payment.
            alertMessage = alertMessage + "Propocione un numero entero positivo para el pago mensual.";//Provide a positive numeric value for monthly payment.    
        }
    }
    if (message == "") {
        div.style.display='none';
        document.getElementById(tboxPayment).value = valPayment;
        return true;
    }
    showhide('answer2', false); 
    div.innerHTML = message;
    alert(alertMessage);
    return false;
}

function openHelp(helpItem){
   var url = "";

   if (!isSpanish) 
   {
        if (!isFRB) url = "HelpInfo.aspx?Source=FTC" + helpItem;
        else url = "HelpInfo.aspx?Source=FRB" + helpItem;
   }
   else 
   {
        if (!isFRB) url = "HelpInfo_SP.aspx?Source=FTC" + helpItem;
        else url = "HelpInfo_SP.aspx?Source=FRB" + helpItem;
   }
   var w = 1000;
   var h = 700;
   var winsize = ',width='+w+',height='+h; 
   var winpos  = (window.screen && document.layers) ? 
        ',screenX='+((screen.availWidth-w)/2)+',screenY='+((screen.availHeight-h)/2) 
        : (window.screen && !document.layers) ? 
        ',left='+((screen.availWidth-w)/2)+',top='+((screen.availHeight-h)/2) : '';
   
   popUp = window.open(url,'popupHelp','toolbar=yes,resizable=yes,scrollbars=yes,location=no'+winsize+winpos); 
   popUp.focus(); 
}

function RemoveChar(str, chartoremove) {
    if (str.length > 0){
        var i = str.indexOf(chartoremove, 0); 
        while ( i!=-1 ) { 
            str = str.substring(0,i) + str.substring(i+1, str.length); 
            i = str.indexOf(chartoremove, i); 
        }
    }
    return str;
}

function AddZeros(obj)
{
    var newValue;
    newValue = Number(obj.value);
    if(isNaN(newValue)) 
    {
        newValue = 0;
        obj.value = newValue.toFixed(2);
    }
    else
    {
        obj.value = newValue.toFixed(2);
    }
}

function showhide(layer_ref, show) 
{
    var state = '';

    if (show) state = 'visible';
    else state = 'hidden';

    if (document.all) { //IS IE 4 or 5 (or 6 beta)
        eval( "document.all." + layer_ref + ".style.visibility = state");
    }
    if (document.layers) { //IS NETSCAPE 4 or below
        document.layers[layer_ref].visibility = state;
    }
    if (document.getElementById && !document.all) {
        var obj = document.getElementById(layer_ref);
        obj.style.visibility = state;
    }
}

function CheckFRB() {
    var strQueryString="";

    var hasQueryString = document.URL.indexOf('?');

    if (hasQueryString != -1) {
        strQueryString = document.URL.substring(hasQueryString + 1, document.URL.length).toLowerCase();
        if (strQueryString == "source=ftc") isFRB = false;
        else isFRB = true;
    }
    else isFRB = true;
}

function CheckSpanish() {
    var strQueryString="";
    strQueryString = document.URL.toLowerCase();
    var hasQueryString = strQueryString.search(/default_sp/);

    if (hasQueryString != -1) isSpanish = true;
    else isSpanish = false;
}

function SetEnvironment() {
    CheckFRB();
    CheckSpanish();
}


function printable(b)
{ 
    var titleName = "";
    var headerImg = "";
    
    if (!isSpanish) {
        if (isFRB){
            titleName = "Board of Governors of the Federal Reserve System";
            headerImg = "<DIV id=headerMasthead><IMG alt='Board of Governors of the Federal Reserve System' src='gifjpg/masthead2.jpg'></DIV>"
        }
        else
            titleName = "Federal Trade Commission";
    }
    else {
        if (isFRB){
            titleName = "La Reserva Federal";
            headerImg = "<DIV id=headerMasthead><IMG alt='La Reserva Federal' src='gifjpg/masthead2.jpg'></DIV>"
        }
        else
            titleName = "La Comision Federal de Comercio";
    }
        
        var print = "<script language='javascript' type='text/javascript'> window.print(); </script>"; 
        var top = "<html><head><title>Printer Version - " + titleName + " </title><link rel='stylesheet' href='" + b + "' type='text/css'/>" +
            print + "<\/head><body leftmargin='10' topmargin='10' marginwidth='10' marginheight='10'>";
        var bottom = "</body></html>";
        var getthis=document.all?document.all['printThis']:document.getElementById('printThis');
        var popurl="/PrintVersion.htm";
        winpops=window.open(popurl,"","width=625,height=480,status,scrollbars,menubar,resizable,");
        getthis = getthis.innerHTML.replace(/<\/?SCRIPT[^>]*>/gi, "");
        getthis = getthis.replace(/<a/gi, "<span");
        getthis = getthis.replace(/<a>/gi, "<span>");
        getthis = getthis.replace(/<\/a>/gi, "</span>");
        var temp = getthis.replace(/onclick[^\s]*/gi, "");
        winpops.document.write(top + headerImg + temp + bottom);
        winpops.document.close();
}

function openPrint() {
    var url = "";
    if (!isFRB)
        url = "printable.htm?Source=FTC";
    else
        url = "printable.htm?Source=FRB";
    var w = 1000;
    var h = 700;
    var winsize = ',width=' + w + ',height=' + h;
    var winpos = (window.screen && document.layers) ?
        ',screenX=' + ((screen.availWidth - w) / 2) + ',screenY=' + ((screen.availHeight - h) / 2)
        : (window.screen && !document.layers) ?
        ',left=' + ((screen.availWidth - w) / 2) + ',top=' + ((screen.availHeight - h) / 2) : '';


    var popUp = window.open(url, 'popupHelp', 'toolbar=yes,resizable=yes,scrollbars=yes,location=no' + winsize + winpos);
    popUp.focus();
}

function SetChildWindow(divID) {
    var isInIFrame = (window.location != window.parent.location) ? true : false;
    if (isInIFrame) {
        var parentDem = getFrameSize(parent.window.frames[0].name);
        var divTag = document.getElementById(divID);
        divTag.style.width = parentDem.width - 10;
        divTag.style.height = parentDem.height;
    }
}

function getFrameSize(frameID) {
    var result = { height: 0, width: 0 };
    if (document.getElementById) {
        var frame = parent.document.getElementById(frameID);
        if (frame.scrollWidth) {
            result.height = frame.scrollHeight;
            result.width = frame.scrollWidth;
        }
    }
    return result;
}

function waitMessage()
{

    var btn = document.getElementById('lnkCalculate');
    if (!btn.disabled)
    {
        btn.value = "Calculating....Please Wait";
        btn.disabled = true;
    }
}