﻿// JScript File
function ValidateMinQty(tb,minQty) {
    if (tb) {
        if (IsNumeric(tb.value)) {
            var tbQty = parseFloat(tb.value);
            if (tbQty < minQty) {
                alert('Minimum of ' + minQty + ' required.');
                tb.value = minQty;
                tb.focus();
            }
        } else {
            alert('Minimum of ' + minQty + ' required.');
            tb.value = minQty;
            tb.focus();
        }
    }
}
//    function CalcReplacePrice(senderID) {
//        var sender = document.getElementById(senderID);

//        if (sender) {
//            if (sender.cType == 'N' || sender.cType == 'A') {
//                return 0;
//            } else {
//                
//            }
//        } else {
//            return 0;
//        }
//    }
    function updItemCode(o,lblId) {
        if (o) {
     //alert('here: ' + o.getAttribute("changeItemCode") + ' id: ' + o.id);
            if (o.getAttribute("changeItemCode") == 'True') {
                var pclbl = document.getElementById(lblId);
               if (pclbl) {
                    var oType = o.getAttribute("oType");
                    if (oType == 'ddl') {
                        //working with drop down list
                        //alert('otype: ' + senderID + ' OID: '+sender.oid);
                        var myindex  = o.selectedIndex;
                        //var ovid = sender.options[myindex].ovid;
                        pclbl.innerHTML = o.options[myindex].getAttribute("ItemCode");
                        //alert('DDLCode: ' + o.options[myindex].getAttribute("ItemCode"));   
                        //appendCode = true;                     
                    } else if (oType == 'cb') {
                    //alert("cb");
                        if (o.checked) {
                        //alert('CBCode: ' + o.getAttribute("ItemCode"));
                            //pclbl.innerHTML = o.getAttribute("ItemCode");
                            if (appendCode == true) {
                                pclbl.innerHTML = pclbl.innerHTML +  ", " + o.getAttribute("ItemCode");
                            } else {
                                pclbl.innerHTML = o.getAttribute("ItemCode");
                            }
                            appendCode = true;                     
                        }
                    } else if (oType == 'rb') {
                    //alert("rb");
                        if (o.checked) {
                        //alert('RBCode: ' + o.getAttribute("ItemCode"));
                            pclbl.innerHTML = o.getAttribute("ItemCode");
                            //appendCode = true;                     
                        }
                    }
                }
            }
        }
    }
    function CalcOptPrice(sender) {
        //alert('here');
        //var sender = document.getElementById(senderID);
        
        //alert('value: '+sender.innerHTML);
        //alert('value: '+pLabel.originalp);
            //var oid = sender.oid;
        if (sender) {
        var cType = sender.getAttribute("cType");
        //alert('ctypea: ' + sender.getAttribute("cType"));
            if (cType == 'N') {
                return 0;
            } else {
                var oType = sender.getAttribute("oType");
                if (oType == 'ddl') {
                    //working with drop down list
                    //alert('otype: ' + senderID + ' OID: '+sender.oid);
                    var myindex  = sender.selectedIndex;
                    //var ovid = sender.options[myindex].ovid;
                    var charge = parseFloat(sender.options[myindex].getAttribute("charge"));
                    //alert('charge: ' + charge);
                    return charge;
                    //var newp = parseFloat(originalp) + parseFloat(charge);
                    
                } else if (oType == 'cb') {
                    if (sender.checked) {
                        return parseFloat(sender.getAttribute("charge"));
                    } else {
                        return 0;
                    }
                } else if (oType == 'rb') {
                    if (sender.checked) {
                        return parseFloat(sender.getAttribute("charge"));
                    } else {
                        return 0;
                    }
                } else {
                    return 0;
                }
            }
        } else {
            return 0;
        }
    }
    function FormatPrice(pLabel,price) {
        //var pLabel = document.getElementById(plabelid);
        //var originalp = parseFloat(pLabel.originalp);
        //var newp = (originalp + price).toFixed(2);
        pLabel.innerHTML = '$'+addCommas(price.toFixed(2));
        
    }
    function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function IsNumeric(sText)
{
//alert('here');
    if (sText) { 
    // if is not null validate value
        if (sText == '') {  
        //if is blank return false
            return false;
        } else { 
        // if not empty check characters/

            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;
        }
    } else { 
    // if is null return false
        return false;
    }
}
