﻿function CheckMinQuntity(textControl, p, v) {
    var x = textControl.value;
    var minquntity = document.getElementById('minQuantity_' + p + '_' + v).value;
    if (minquntity == '') {
        minquntity = 0;
    }
    if (minquntity > 0) {
        if (x < minquntity) {
            alert('enter minimum quantity=' + minquntity);
            textControl.value = minquntity;
            Changed(textControl, p, v);
            return false;
        }
    }
}

function Changed(textControl, p, v) {
    var x = textControl.value;
    var xprice = document.getElementById('Price_' + p + '_' + v);
    var txPrice = document.getElementById('txPrice_' + p + '_' + v);
    var xSalePrice = document.getElementById('SalePrice_' + p + '_' + v);
    var txsaleprice = document.getElementById('txSalePrice_' + p + '_' + v);
    var pSpec = document.getElementById('hidPriceSpec_' + p + '_' + v).value;
    var qtyType = document.getElementById('qtyType_' + p + '_' + v).value;
    
    var n = pSpec.split(' ');
    var xf = n[0];
    var xl = n[2];
    if (qtyType == 'int') {
        if (IsPositiveInteger(x)) {
            SetPrice(txPrice, txsaleprice, xSalePrice, xprice, x, xf, xl);
        }
        else {
            var p = GetIntegerPart(x);

            textControl.value = p;
            SetPrice(txPrice, txsaleprice, xSalePrice, xprice, p, xf, xl);
        }
    }
    else if (qtyType == 'dec') {
        if (IsNumeric(x)) {
            if (CountDecimal(x)) {
                SetPrice(txPrice, txsaleprice, xSalePrice, xprice, x, xf, xl);
            }
            else {
                var ps = FirstDecimal(x);
                textControl.value = ps;
                SetPrice(txPrice, txsaleprice, xSalePrice, xprice, ps, xf, xl);
            }
        }
        else {
            var ps = x.substring(0, x.length - 1);
            textControl.value = ps;
            SetPrice(txPrice, txsaleprice, xSalePrice, xprice, ps, xf, xl);
        }
    }


}

function SetPrice(objPrice, objSalePrice, saleprice, price, qty, xf, xl) {
    var txt = $get('<%=txtQuantity.ClientID %>');
    try
    {
        txt.value = qty;
    }
    catch(err) {
        //alert(err);
    }
    if (typeof(xl) == 'undefined') {
        xl = '';
    }
    if (saleprice != null) {
        var os=xf + ' ' + parseFloat(saleprice.value * qty).toFixed(2) + ' ' + xl;
        objSalePrice.value = os;
        var x = 7 * os.length;
        objSalePrice.style.width = x + 'px';
    }
    if (price != null) {
        var op=xf + ' ' + parseFloat(price.value * qty).toFixed(2) + ' ' + xl;
        objPrice.value = op;
        var x = 7 * op.length;
        objPrice.style.width =  x+ 'px';     
    }
}

function IsPositiveInteger(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;
        }
    }
    if (IsNumber) {
        if (sText == 0)
            IsNumber = false;
    }
    return IsNumber;

}

function IsNumeric(sText) {
    CountDecimal(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 CountDecimal(str) {
    //Counting all the vowels except y.
    var decim = "."
    var ndecim = 0;
    for (pos = 0; pos < str.length; pos++) {
        chr = str.charAt(pos)
        if (decim.indexOf(chr) != -1) ndecim++
    }
    if (ndecim > 1) {
        return false;
    }
    else {
        return true;
    }
}

function FirstDecimal(str) {
    //Counting all the decimal points.
    var decim = "."
    var ndecim = 0;
    for (pos = 0; pos < str.length; pos++) {
        chr = str.charAt(pos)
        if (decim.indexOf(chr) != -1) {
            ndecim++
            if (ndecim == 2) {
                return str.substring(0, pos);
            }
        }
        var c = str.charAt(i);

    }
    if (ndecim > 1) {
        return false;
    }
}

function GetIntegerPart(s) {
    var i;
    s = s.toString();
    for (i = 0; i < s.length; i++) {

        var c = s.charAt(i);
        if (isNaN(c)) {
            var stX = s.substring(0, s.indexOf(c));
            //alert("Given value is not a number");
            return stX;
        }
    }
    return s;
}
