﻿function BillInputController(billIdTextBoxID, paymentIdTextBoxID, billIdValidatorId, paymentIdValidatorId, workingMode) {
    this.billIdTextBoxID = billIdTextBoxID;
    this.paymentIdTextBoxID = paymentIdTextBoxID;
    this.isBillChecking = false;
    this.isPaymentChecking = false;
    this.billIdValidatorId = billIdValidatorId;
    this.paymentIdValidatorId = paymentIdValidatorId;
    this.workingMode = workingMode;
}
BillInputController.prototype.GetBillIDTextBox = function ()
{
	return document.getElementById(this.billIdTextBoxID);
}
BillInputController.prototype.GetBillID = function() {
    var billIdTextBox = this.GetBillIDTextBox();
    return billIdTextBox == null ? "" : (billIdTextBox.value + "");
}
BillInputController.prototype.GetPaymentIDTextBox = function()
{
	return document.getElementById(this.paymentIdTextBoxID);
}
BillInputController.prototype.GetPaymentID = function() {
    var paymentIdTextBox = this.GetPaymentIDTextBox();

    return paymentIdTextBox == null ? "" : (paymentIdTextBox.value + "");
}
BillInputController.prototype.GetBillIDValidator = function ()
{
	return document.getElementById(this.billIdValidatorId);
}
BillInputController.prototype.GetPaymentIDValidator = function ()
{
    return document.getElementById(this.paymentIdValidatorId);
}
BillInputController.prototype.CalTheBit = function (num) {
    var sum = 0;
    var Base = 2;
    for (var i = 0; i < num.length; i++) {
        if (Base == 8)
            Base = 2;
        sum += num.substring(num.length - 1 - i, num.length - i) * Base;
        Base++;
    }
    sum %= 11;
    if (sum < 2)
        sum = 0;
    else
        sum = 11 - sum;
    return sum;
}
BillInputController.prototype.CheckBill = function(sender, e) {
    switch (this.workkingMode) {
        default:
        case "normal_bill":
            this.CheckBill_Normal(sender, e);
            break;
        case "irancell":
            this.CheckBill_Irancell(sender, e);
            break;
    }
}
BillInputController.prototype.CheckBill_Normal = function(sender, e) {
    var billId = this.GetBillID();

    if (billId.length < 6)
        e.IsValid = false;
    else {
        var controlBit = billId.substring(billId.length - 1);
        billId = billId.substring(0, billId.length - 1);

        var result = this.CalTheBit(billId);

        e.IsValid = (result == controlBit);
    }

    if (this.isBillChecking)
        return;

    this.isBillChecking = true;

    var pValidator = this.GetPaymentIDValidator();
    ValidatorValidate(pValidator);
    ValidatorUpdateDisplay(pValidator);

    isBillChecking = false;
}
BillInputController.prototype.CheckBill_Irancell = function(sender, e) {
    var billId = this.GetBillID();

    while (billId.length < 13)
        billId = "0" + billId;

    if (billId.length != 13)
        e.IsValid = false;

    var checkArr = new Array(11, 10, 9, 1, 2, 3, 4, 5, 6, 7, 8);
    var checkSum = 0;

    for (var i = 0; i < 11; i++)
        checkSum += checkArr[i] * parseInt(billId[i]);

    checkSum = checkSum % 99;

    e.IsValid = checkSum == parseInt(billId.substring(11, 13));

    if (this.isBillChecking)
        return;

    this.isBillChecking = true;

    var pValidator = this.GetPaymentIDValidator();
    if (pValidator != null) {
        ValidatorValidate(pValidator);
        ValidatorUpdateDisplay(pValidator);
    }
    isBillChecking = false;
}
BillInputController.prototype.CheckPayment = function(sender, e) {
    switch (this.workingMode) {
        default:
        case "normal_bill":
            this.CheckPayment_Normal(sender, e);
            break;
        case "irancell":
            this.CheckPayment_Irancell(sender, e);
            break;
    }
}
BillInputController.prototype.CheckPayment_Normal = function(sender, e) {
    if (this.isPaymentChecking)
        return;

    this.isPaymentChecking = true;

    var bValidator = this.GetBillIDValidator();
    if (bValidator != null) {
        ValidatorValidate(bValidator);
        ValidatorUpdateDisplay(bValidator);
    }

    this.isPaymentChecking = false;

    var billId = this.GetBillIDTextBox().value + "";

    if (billId.length < 6) {
        e.IsValid = false;
        return;
    }

    if ((bValidator == null) || !bValidator.isvalid)
        return;

    var paymentId = this.GetPaymentID();

    if (paymentId.length < 6) {
        e.IsValid = false;
        return;
    }

    var firstControllBit = paymentId.charAt(paymentId.length - 2) + "";
    var secondControlBit = paymentId.charAt(paymentId.length - 1) + "";
    paymentId = paymentId.substring(0, paymentId.length - 2);

    e.IsValid =
        (this.CalTheBit(paymentId) == firstControllBit) &&
        (this.CalTheBit(billId + paymentId + firstControllBit) == secondControlBit);
}
BillInputController.prototype.CheckPayment_Irancell = function(sender, e) {
    if (this.isPaymentChecking)
        return;

    this.isPaymentChecking = true;

    var bValidator = GetBillIDValidator();

    if (bValidator != null) {
        ValidatorValidate(bValidator);
        ValidatorUpdateDisplay(bValidator);

        return;
    }
    this.isPaymentChecking = false;
    var amount = parseInt(this.GetPaymentID());
    e.IsValid = (anount % 1000 == 0) && (amount < 100000000);

    if (!bValidator.isvalid)
        return;
}
function CheckBill(sender, e) {
    var control = document.getElementById(sender.controltovalidate);
//    var billController = document.documentElement.(control.attributes["BillControllerId"]);
    var billController=window[control.attributes["BillControllerId"].value];
    if (billController != null)
        billController.CheckBill(sender, e);
}
function CheckPayment(sender, e) {
    var control = document.getElementById(sender.controltovalidate);
//    var billController = document.getElementById(control.attributes["BillControllerId"]);
    var billController=window[control.attributes["BillControllerId"].value];
    if (billController != null)
        billController.CheckPayment(sender, e);
}
