﻿// JScript File

function CalculateItemTotals(PrevInd) {
    theTotal = 0;
    theProductCount = document.getElementById('txtProductCount').value
    // alert(theProductCount);
    //alert(theForm.ctl00$Page2$txtProduct1Purchased.value);
    if (theProductCount > 0) {
        if (theForm.ctl00$Page2$txtProduct1Purchased.value != '' && theForm.ctl00$Page2$txtProduct1Purchased.value != 0 && IsNumeric(theForm.ctl00$Page2$txtProduct1Purchased.value)) {
            document.getElementById('ctl00_Page2_lblProduct1Total').innerHTML = formatCurrency(theForm.ctl00$Page2$txtProduct1Purchased.value * document.getElementById('ctl00_Page2_lblProduct1Cost').innerHTML);
            theTotal += theForm.ctl00$Page2$txtProduct1Purchased.value * document.getElementById('ctl00_Page2_lblProduct1Cost').innerHTML;
        }
        else {
            document.getElementById('ctl00_Page2_lblProduct1Total').innerHTML = '';
        }
    }
    if (theProductCount > 1) {
        if (theForm.ctl00$Page2$txtProduct2Purchased.value != '' && theForm.ctl00$Page2$txtProduct2Purchased.value != 0 && IsNumeric(theForm.ctl00$Page2$txtProduct2Purchased.value)) {
            document.getElementById('ctl00_Page2_lblProduct2Total').innerHTML = formatCurrency(theForm.ctl00$Page2$txtProduct2Purchased.value * document.getElementById('ctl00_Page2_lblProduct2Cost').innerHTML);
            theTotal += theForm.ctl00$Page2$txtProduct2Purchased.value * document.getElementById('ctl00_Page2_lblProduct2Cost').innerHTML;
        }
        else {
            document.getElementById('ctl00_Page2_lblProduct2Total').innerHTML = '';
        }
    }
    if (theProductCount > 2) {
        if (theForm.ctl00$Page2$txtProduct3Purchased.value != '' && theForm.ctl00$Page2$txtProduct3Purchased.value != 0 && IsNumeric(theForm.ctl00$Page2$txtProduct3Purchased.value)) {
            document.getElementById('ctl00_Page2_lblProduct3Total').innerHTML = formatCurrency(theForm.ctl00$Page2$txtProduct3Purchased.value * document.getElementById('ctl00_Page2_lblProduct3Cost').innerHTML);
            theTotal += theForm.ctl00$Page2$txtProduct3Purchased.value * document.getElementById('ctl00_Page2_lblProduct3Cost').innerHTML;
        }
        else {
            document.getElementById('ctl00_Page2_lblProduct3Total').innerHTML = '';
        }
    }
    if (theProductCount > 3) {
        if (theForm.ctl00$Page2$txtProduct4Purchased.value != '' && theForm.ctl00$Page2$txtProduct4Purchased.value != 0 && IsNumeric(theForm.ctl00$Page2$txtProduct4Purchased.value)) {
            document.getElementById('ctl00_Page2_lblProduct4Total').innerHTML = formatCurrency(theForm.ctl00$Page2$txtProduct4Purchased.value * document.getElementById('ctl00_Page2_lblProduct4Cost').innerHTML);
            theTotal += theForm.ctl00$Page2$txtProduct4Purchased.value * document.getElementById('ctl00_Page2_lblProduct4Cost').innerHTML;
        }
        else {
            document.getElementById('ctl00_Page2_lblProduct4Total').innerHTML = '';
        }
    }
    if (theProductCount > 4) {
        if (theForm.ctl00$Page2$txtProduct5Purchased.value != '' && theForm.ctl00$Page2$txtProduct5Purchased.value != 0 && IsNumeric(theForm.ctl00$Page2$txtProduct5Purchased.value)) {
            document.getElementById('ctl00_Page2_lblProduct5Total').innerHTML = formatCurrency(theForm.ctl00$Page2$txtProduct5Purchased.value * document.getElementById('ctl00_Page2_lblProduct5Cost').innerHTML);
            theTotal += theForm.ctl00$Page2$txtProduct5Purchased.value * document.getElementById('ctl00_Page2_lblProduct5Cost').innerHTML;
        }
        else {
            document.getElementById('ctl00_Page2_lblProduct5Total').innerHTML = '';
        }
    }
    //    alert(theTotal);
    document.getElementById('ctl00_Page2_lblTotalPurchaseAmount').innerHTML = "<strong>" + formatCurrency(theTotal) + "</strong>";
    document.getElementById('txtTotalProductsPurchased').value = theTotal
    document.getElementById('txtPrevInd').value = PrevInd
    //   document.getElementById('lblTotalPurchaseAmount').value = "<strong>"+formatCurrency(theTotal)+"</strong>";
    return true;
}
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;

}

function CheckCancelOrder() {
    var answer = confirm("Are you sure you want to cancel this order?")
    if (answer) {
        return true;
    }
    else {
        return false;
    }
}

function formatCurrency(strValue) {
    strValue = strValue.toString().replace(/\$|\,/g, '');
    dblValue = parseFloat(strValue);

    blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    dblValue = Math.floor(dblValue * 100 + 0.50000000001);
    intCents = dblValue % 100;
    strCents = intCents.toString();
    dblValue = Math.floor(dblValue / 100).toString();
    if (intCents < 10)
        strCents = "0" + strCents;
    for (var i = 0; i < Math.floor((dblValue.length - (1 + i)) / 3); i++)
        dblValue = dblValue.substring(0, dblValue.length - (4 * i + 3)) + ',' +
		dblValue.substring(dblValue.length - (4 * i + 3));
    return (((blnSign) ? '' : '-') + '$' + dblValue + '.' + strCents);
}
function ValidateProductsPurchased(source, arguments) {
    PurchaseAmount = document.getElementById('txtTotalProductsPurchased').value;
    PrevInd = document.getElementById('txtPrevInd').value;
    //   alert(PrevInd);

    //    alert('test');
    if (PrevInd <= 'next') {
        if (PurchaseAmount > 0)
            arguments.IsValid = true;
        else
            arguments.IsValid = false;
    }
}

