﻿

function openWindow(urlPage, winName, winWidth, winHeight, winResize, winScroll, winCenter) {
    // Set Window POSITION to center screen
    var xPos, yPos
    xPos = 0; yPos = 0;
    if ((parseInt(navigator.appVersion) >= 4) && (winCenter)) {
        xPos = (screen.width - winWidth) / 2;
        yPos = (screen.height - winHeight) / 2;
    }

    // Set Window PARAMETERS
    var winPrm
    winPrm = ""
	+ "width=" + winWidth + ","
	+ "height=" + winHeight + ","
	+ "resizable=" + winResize + ","
	+ "scrollbars=" + winScroll + ","

	+ "titlebar=0,"
	+ "toolbar=0,"
	+ "menubar=0,"
	+ "status=1,"
	+ "hotkeys=0,"
	+ "location=0,"

	+ "left=" + xPos + ","     //IE Only
	+ "top=" + yPos;           //IE Only
    +"screenx=" + xPos + ","  //NN Only
	+ "screeny=" + yPos + ","  //NN Only

    window.open(urlPage, winName, winPrm);
}

function EnterPressedUserID() {
    if (event.keyCode == 13) {
        //document.forms[0].btnAddItem.focus();
        event.keyCode = 9
    } else {
        return true;
    }

}

function EnterPressedPassword() {
    if (event.keyCode == 13) {
        document.forms[0].btnLogin.focus();
        //event.keyCode = 9
    } else {
        return true;
    }

}

function EnterPressedTabCall() {
    if (event.keyCode == 13) {
        //document.forms[0].btnAddItem.focus();
        event.keyCode = 9
    } else {
        return true;
    }

}

function EnterPressedNew() {
    if (event.keyCode == 13) {
        document.forms[0].btnSubmit.focus();
        //event.keyCode = 9
    } else {
        return true;
    }

}

function SubmitButton() {
    document.forms[0].btnCalculate.submit();
}

function EnterPressedDownPay() {
    if (event.keyCode == 13) {
        IsNumeric('txtDownPay')
        document.forms[0].btnCalculate.focus();
        //setDownPay2();
        //event.keyCode = 9
    } else {
        return true;
    }

}

function EnterPressedDownPay2() {
    if (event.keyCode == 13) {
        IsNumeric('txtPCollect')
        document.forms[0].btnCalculate2.focus();
        //event.keyCode = 9
    } else {
        return true;
    }

}

function EnterPressedSub() {
    //var x = document.getElementById(btnName).value;
    //if (event.keyCode == 13) {
        //document.getElementById(btnName).focus();
    //} else {
    //    return true;
   // }
}

function EnterPressedSubmit() {
    var x = document.getElementById(btnName).value;
    if (event.keyCode == 13) {
    document.getElementById(btnName).focus();
    } else {
        return true;
    }
}

function checkMail(ctrlName) {
    var x = document.getElementById(ctrlName).value;

    if (x == "") {
    }
    else {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(x)) {
        }
        else {
            alert('Invalid Email Address!');
            document.getElementById(ctrlName).focus();
            document.getElementById(ctrlName).value = "";
        }
    }
}

function SSNValidation(ssn) {
    var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
    var numDashes = ssn.split('-').length - 1;
    if (matchArr == null || numDashes == 1) {
        alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
        msg = "does not appear to be valid";
    }
    else
        if (parseInt(matchArr[1], 10) == 0) {
        alert("Invalid SSN: SSN's can't start with 000.");
        msg = "does not appear to be valid";
    }
}

function DOBValidation(DOB) {
    var x = document.getElementById(DOB).value;

    if (x == "") {
    }
    else {
        var filter = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
        if (filter.test(x)) {
        }
        else {
            alert('Invalid DOB. Must be in the form mm/dd/yyyy.');
            document.getElementById(DOB).focus();
            document.getElementById(DOB).value = "";
        }
    }
}

function validateForm() {
    if (document.all.item("txbTextbox1").value != "") {
        IsNumeric(document.all.item("txbTextbox1"));
    }
}

function IsNumeric(ctrl)
// check for valid numer\ic strings
{
    var strString = document.getElementById(ctrl).value; //formatnumber(document.getElementById(ctrl).value,2);
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;
    strString = strString.toString().replace(/\$|\,/g, '');
    strString = strString.toString().replace(/\,|\,/g, '');
    // test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
            document.getElementById(ctrl).focus();
            document.getElementById(ctrl).value = "";
            alert("Please use numeric values!");
        } else if (ctrl != 'txtPCollect') {
            document.getElementById(ctrl).value = "$" + formatnumber(strString, 2);
        }
    }
    return blnResult;
}

function getGBalance(var1, var2, var3, var4, var5) {
    var x = document.getElementById(var1).value;
    var y = document.getElementById(var2).value;
    var z = 0;
    var j = 0.00;

    if (x == "" || y == "") {
    }
    else {
        x = x.toString().replace(/\$|\,/g, '');
        y = y.toString().replace(/\$|\,/g, '');
        j = parseFloat(x) * parseFloat(y)
        document.getElementById(var3).value = "$" + formatnumber(j, 2);

        z = document.getElementById(var3).value;
        z = z.toString().replace(/\$|\,/g, '');
        z = parseFloat(z) * .8
        document.getElementById(var4).value = "$" + formatnumber(z, 2);
        document.getElementById(var5).focus();
    }
}

function setDownPay2(ctrl1, ctrl2, ctrl3) {
    var x = document.getElementById(ctrl1).value;
    var y = document.getElementById(ctrl2).value;
    var z = 0.00;

    if (x == "") {
    }
    else {
        x = x.toString().replace(/\$|\,/g, '');
        y = y.toString().replace(/\$|\,/g, '');
        z = parseFloat(x) - parseFloat(y)

        document.getElementById(ctrl3).value = "$" + formatnumber(z, 2);
    }
}

function formatnumber(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
num.substring(num.length - (4 * i + 3));
    //return (((sign)?'':'-') + '$' + num + '.' + cents);
    return (((sign) ? '' : '-') + num + '.' + cents);
}

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 NextBox(ObjID, ObjLength, ObjNext) {
    var x = document.getElementById(ObjID).value.length;
    var y = document.getElementById(ObjID).value;

    if (x == ObjLength) {
        document.getElementById(ObjNext).focus();
    }
}

function setCarPayment(y) {
    var x = document.getElementById('txtPayment').value;
    alert(y);
    if (x == "") {
    }
    else {
        x = x.toString().replace(/\$|\,/g, '');
        x = parseFloat(x) / 2

        document.getElementById('txtPayment').value = "$" + formatnumber(x, 2);
    }
}

function SetFocus(ObjID) {
    var x = document.getElementById(ObjID).value;

    if (x == "") {
    }
    else {
        document.getElementById(ObjID).focus();
    }
}