
function encodeAddr(x)
{
    var res = "", i, c; 
    for (i = 0; i < x.length; i++) {
        c = x.charCodeAt(i); 
        if (res.length > 0) res += ","; 
        res += c; 
    }
    return res; 
}

function getTimeOptionValue(ipt1,ipt2)
{
    var timeOptionValue;
    if(ipt1==1)
        timeOptionValue=" between '"+ipt2+"' and DateAdd(dd,1,'"+ipt2+"')";
    if(ipt1==2)
        timeOptionValue=" > '"+ipt2+"'";
    if(ipt1==3)
        timeOptionValue=" >= '"+ipt2+"'";
    if(ipt1==4)
        timeOptionValue=" < '"+ipt2+"'";
    if(ipt1==5)
        timeOptionValue=" <= '"+ipt2+"'";
    if(ipt1==6)
        timeOptionValue=" != '"+ipt2+"'";
    return timeOptionValue;
}

function getNumericOptionValue(ipt1,ipt2)
{
    var NumericOptionValue;
    if(ipt1==1)
        NumericOptionValue=" ="+ipt2;
    if(ipt1==2)
        NumericOptionValue=" >"+ipt2;
    if(ipt1==3)
        NumericOptionValue=" >="+ipt2;
    if(ipt1==4)
        NumericOptionValue=" <"+ipt2;
    if(ipt1==5)
        NumericOptionValue=" <="+ipt2;
    if(ipt1==6)
        NumericOptionValue=" !="+ipt2;
    return NumericOptionValue;
}

function getStringOptionValue(ipt1,ipt2)
{
    var stringOptionValue;
    if(ipt1==1)
        stringOptionValue=" = '"+ipt2+"'";
    if(ipt1==2)
        stringOptionValue=" like '"+ipt2+"%'";
    if(ipt1==3)
        stringOptionValue=" like '%"+ipt2+"'";
    if(ipt1==4)
        stringOptionValue=" like '%"+ipt2+"%'";
    return stringOptionValue;
}


function getValue(obj) {
    if (obj == null) return ""; 
    if (typeof(obj) == "string") return obj; 
    if (typeof(obj) == "number") return "" + obj; 
    if (obj.value != null) return obj.value; 
    if (obj.text != null) return obj.text; 
    return "" + obj; 
}


function removeCriteriaFields(ctr) {
    var more = true; 
    while (more) {
        more = false; 
        var i; 
        for (i = 0; i < ctr.elements.length; i++) {
            var obj = ctr.elements[i]; 
            if (typeof(obj) == "object") {
                if (obj.name.substring(obj.name.length - 3) == "_nm") {
                    more = true; 
                    prefix = obj.name.substring(0, obj.name.length - 3); 
                    ctr.item(prefix + "_op").outerHTML = "*"; 
                    ctr.item(prefix).outerHTML = getValue(ctr.item(prefix)); 
                    if (ctr.item(prefix + "_nm"))
                        ctr.item(prefix + "_nm").outerHTML = ""; //getValue(ctr.item(prefix + "_nm")); 
                    if (ctr.item(prefix + "_rl"))
                        ctr.item(prefix + "_rl").outerHTML = ""; 
                    break; 
                }
            }
        }
    }
}


function getCriteria(ctr, remove) {
    var sqlPartial = ""; 
    var prefix; 
    var frel, ftype, fname, fop, fraw, fval; 
    var i; 
    for (i = 0; i < ctr.elements.length; i++) {
        var obj = ctr.elements[i]; 
        if (typeof(obj) == "object") {
            if (obj.name.substring(obj.name.length - 3) == "_nm") {
                prefix = obj.name.substring(0, obj.name.length - 3); 
                frel = getValue(ctr.item(prefix + "_rl")); 
                ftype = prefix.substring(0, 1); 
                fname = getValue(obj); 
                fop  = getValue(ctr.item(prefix + "_op")); 
                fraw = getValue(ctr.item(prefix)); 
                
                if (frel == "") {
                    if (fraw == "") continue; 
                    frel = "and"; 
                }
                
                switch (ftype) {
                    case "s": 
                    case "d": 
                    case "t": 
                        fval = "\'" + fraw + "\'"; 
                        break; 
                    default: 
                        fval = fraw; 
                }
                
                if (sqlPartial != "")
                    sqlPartial += " " + frel + " "; 
                
                // ... <frel> <fname> <fop> <fval>
                switch (fop) {
                    case "eq":  sqlPartial += fname + "=" + fval;   break; 
                    case "ne":  sqlPartial += fname + "!=" + fval;   break; 
                    case "lt":  sqlPartial += fname + "<" + fval;   break; 
                    case "le":  sqlPartial += fname + "<=" + fval;   break; 
                    case "gt":  sqlPartial += fname + ">" + fval;   break; 
                    case "ge":  sqlPartial += fname + ">=" + fval;   break; 
                    case "lk":  sqlPartial += fname + " like '%" + fraw + "%'";   break; 
                    case "ll":  sqlPartial += fname + " like '" + fraw + "%'";   break; 
                    case "lr":  sqlPartial += fname + " like '%" + fraw + "'";   break; 
                    case "nll": sqlPartial += fname + " not like '%" + fraw + "'";   break; 
                    case "nlr": sqlPartial += fname + " not like '" + fraw + "%'";   break; 
                    case "def": 
                        if (fraw == '') sqlPartial += fname + " is null"; 
                        else sqlPartial += fname + " not is null"; 
                        break; 
                    default:    sqlPartial += fname + " " + fop + " " + fval; break; 
                }
            }
        }
    }
    if (remove) removeCriteriaFields(ctr); 
    return sqlPartial; 
}
