//信息查询
//设置查询条件，添加一个条件
function ConditionAdd()
{
    var conditionTable = document.getElementById("ConditionTable");
    var insertPos = conditionTable.rows.length - 1;
    var addConditionTable = document.getElementById("AddConditionTable");
    
    var fields = document.getElementById("AddFields").getElementsByTagName('select')[0];
    //debugger
    var relations = document.getElementById("AddRelations").getElementsByTagName('select')[0];
    var logicals = document.getElementById("AddLogicals").getElementsByTagName('select')[0];
    
    if(fields.value=="")
    {
        alert("对不起,您将要添加的查询条件不存在!");
        return;
    }
    //有效性检查
    if (!IsValidConditionAdd(fields.value, relations.value, logicals.value))
    {
        alert("对不起,您将要添加的查询条件已经存在!");
        return;
    }
    var del = document.createElement("input");
    del.setAttribute("type", "button");
    del.setAttribute("class", "btn btn-xs btn-primary");
    debugger;
    if (document.all) 
    {
        //IE
        del.attachEvent("onclick", ConditionRowDelete); 
    }
    else
    {
        del.addEventListener("onclick", ConditionRowDelete,false); 
    }
    del.setAttribute("value", "删 除");
    
    conditionTable.insertRow(insertPos);
    conditionTable.rows[insertPos].insertCell(0);
    conditionTable.rows[insertPos].cells[0].appendChild(fields.cloneNode(true));
    conditionTable.rows[insertPos].cells[0].childNodes[0].selectedIndex = fields.selectedIndex;
    
    conditionTable.rows[insertPos].insertCell(1);
    conditionTable.rows[insertPos].cells[1].appendChild(relations.cloneNode(true));
    conditionTable.rows[insertPos].cells[1].childNodes[0].selectedIndex = relations.selectedIndex;

    conditionTable.rows[insertPos].insertCell(2);
    conditionTable.rows[insertPos].cells[2].appendChild(document.createTextNode("前台用户输入值"));    
    
    conditionTable.rows[insertPos].insertCell(3);
    conditionTable.rows[insertPos].cells[3].appendChild(logicals.cloneNode(true));
    conditionTable.rows[insertPos].cells[3].childNodes[0].selectedIndex = logicals.selectedIndex;
    
    conditionTable.rows[insertPos].insertCell(4);
    conditionTable.rows[insertPos].cells[4].appendChild(del);
    
}
//验证是否添加条件有重复
function IsValidConditionAdd(field, relation, logical)
{
    var f = true;
    var conditionTable = document.getElementById("ConditionTable");
    var fields,relations, logicals;
    for (var i = 1; i < conditionTable.rows.length - 1; i++)
    {
        fields = conditionTable.rows[i].cells[0].childNodes[0].value;
        relations = conditionTable.rows[i].cells[1].childNodes[0].value;
        logicals = conditionTable.rows[i].cells[3].childNodes[0].value;
        
        if (fields == field && relations == relation && logicals == logical)
        {
            f = false;
            break;
        }    
    }
    return f;
}

//删除一个条件
function ConditionRowDelete(ev)
{
    ev = ev || window.event;
    var iIndex = ev.srcElement.parentElement.parentElement.rowIndex;
    //alert(iIndex);
    var conditionTable = document.getElementById("ConditionTable");
    conditionTable.deleteRow(iIndex);
}

//分析并保存条件
function ConditionSavebb()
{
    if(!IsValidConditionSave())
    {
        alert("对不起,查询条件有重复项!");
        return false;
    }
    var conditionTable = document.getElementById("ConditionTable");
    var condition = ""
    var whereCondition = "";
    var fields,relations, logicals;
    for (var i = 1; i < conditionTable.rows.length - 1; i++)
    {
        fields = conditionTable.rows[i].cells[0].childNodes[0];
        relations = conditionTable.rows[i].cells[1].childNodes[0];
        logicals = conditionTable.rows[i].cells[3].childNodes[0];
        
        condition += fields.value + "," + relations.value + "," + logicals.value + ";";
        if (relations.value == "LIKE")
        {
            whereCondition += fields.value + " LIKE '%@" + fields.value + "%' " + logicals.value + " ";
        }        
        else
        {
            whereCondition += fields.value + " " + relations.value + " '@" + fields.value + "' " + logicals.value + " ";
        }        
    }
    if (condition.length > 0)
    {
        condition = condition.substring(0, condition.length - 1);
    }
    if (whereCondition.length > 3)
    {
        whereCondition = whereCondition.substring(0, whereCondition.length - 4);
    }
    document.getElementById("ConditionValueList").value = condition;
    document.getElementById("WhereConditionValueList").value = whereCondition;
    return true;
}

function IsValidConditionSave()
{
    var f = true;
    var conditionTable = document.getElementById("ConditionTable");
    var fields,relations, logicals;
    var field,relation, logical;
    for (var i = 1; i < conditionTable.rows.length - 1; i++)
    {
        fields = conditionTable.rows[i].cells[0].childNodes[0].value;
        relations = conditionTable.rows[i].cells[1].childNodes[0].value;
        logicals = conditionTable.rows[i].cells[3].childNodes[0].value;
        
        for (var j = i + 1; j < conditionTable.rows.length - 1; j++)
        {
            field = conditionTable.rows[j].cells[0].childNodes[0].value;
            relation = conditionTable.rows[j].cells[1].childNodes[0].value;
            logical = conditionTable.rows[j].cells[3].childNodes[0].value;
            if (fields == field && relations == relation && logicals == logical)
            {
                f = false;
                break;
            }
        }
        if (!f)
        {
            break;
        }
    }
    
    return f;
}


function SelectResult(callback, result, isValidResult)
{
    showModalDialog("manageinfoSearchSelect.html?callback=" + callback + "&result=" + result + "&isvalidresult=" + isValidResult, window, "dialogWidth:500px;dialogHeight:425px;help:no;scroll:no;status:no");
}

function IsValidResult(result)
{
    var v = document.getElementById(result).value.split(",");
    if (v[0].length == 0)
    {
        return "对不起,您至少需要选择一项!";
    }
    else
    {
        return true;
    }
}

function InfoSearchRequest(result)
{
    var s = document.getElementById("Result");         
    if (s)
    {
        eval(s.getAttribute("href"));  
    }
}


//信息查询
//加载设置的字段格式
function ShowResultFormat()
{
    var table = document.getElementById("ResultFormatTable");
    var txt;
    var pos;
    
    for (var i = 0; i < fieldlist.length; i++)
    {
        pos = table.rows.length;
        table.insertRow(pos);
        
        //字段名称        
        table.rows[pos].insertCell(0);
        table.rows[pos].cells[0].innerHTML = fieldlist[i].fieldName;
        
        //宽度
        table.rows[pos].insertCell(1);
        txt = document.createElement("input");
        txt.type = "text";
        txt.style.width = "40px";
        txt.value = fieldlist[i].width;
        
        table.rows[pos].cells[1].appendChild(txt);
        //table.rows[pos].cells[1].innerHTML += "px";
        
        //换行
        table.rows[pos].insertCell(2);
        txt = document.createElement("select");
        txt.options.add(new Option("不换行", "nowrap"));
        txt.options.add(new Option("换行", "wrap"));
        if (fieldlist[i].wrap == "wrap")
        {
            txt.options[1].selected = true;
        }
        else
        {
            txt.options[0].selected = true;
        }
        table.rows[pos].cells[2].appendChild(txt);

        //字体颜色
        table.rows[pos].insertCell(3);
        txt = document.createElement("input");
        txt.id = "txt" + pos.toString() + "3";
        txt.type = "text";
        txt.style.width = "80px";
        txt.value = fieldlist[i].fontColor;
        //txt.style.color = txt.value;        
        //txt.addEventListener("onclick", function(ev){Oxsir.Common.PaletteDialog(ev, "../Wpedt/Dialog/");});        
        //txt.addEventListener("onchange", function(ev){var o = ev.srcElement; o.style.backgroundColor = o.value});
        table.rows[pos].cells[3].appendChild(txt);

        //背景颜色
        table.rows[pos].insertCell(4);
        txt = document.createElement("input");
        txt.id = "txt" + pos.toString() + "4";
        txt.type = "text";
        txt.style.width = "80px";        
        txt.value = fieldlist[i].backgroundColor;
        //txt.style.backgroundColor = txt.value;
        //txt.addEventListener("onclick", function(ev){Oxsir.Common.PaletteDialog(ev, "../Wpedt/Dialog/");});        
        //txt.addEventListener("onchange", function(ev){var o = ev.srcElement; o.style.backgroundColor = o.value});
        table.rows[pos].cells[4].appendChild(txt);

        //字体大小
        table.rows[pos].insertCell(5);
        txt = document.createElement("input");
        txt.type = "text";
        txt.style.width = "40px";
        txt.value = fieldlist[i].fontSize;        
        table.rows[pos].cells[5].appendChild(txt);
        //table.rows[pos].cells[5].innerHTML += "px";

        //表现形式
        table.rows[pos].insertCell(6);
        txt = document.createElement("select");
        txt.options.add(new Option("普通文本", "0"));
        txt.options.add(new Option("超级链接", "1"));
        txt.options.add(new Option("电子邮件", "2"));        
        txt.options[fieldlist[i].format].selected = true;
        
        table.rows[pos].cells[6].appendChild(txt);
    }
}

//信息查询
//保存设置显示字段格式
function ResultFormatSave()
{
    var table = document.getElementById("ResultFormatTable");
    for (var i = 1; i < table.rows.length; i++)
    {   
        
        fieldlist[i-1].width = table.rows[i].cells[1].firstChild.value;
        fieldlist[i-1].wrap =  table.rows[i].cells[2].firstChild.value;
        fieldlist[i-1].fontColor = table.rows[i].cells[3].firstChild.value;
        fieldlist[i-1].backgroundColor = table.rows[i].cells[4].firstChild.value;
        fieldlist[i-1].fontSize = table.rows[i].cells[5].firstChild.value;
        fieldlist[i-1].format = table.rows[i].cells[6].firstChild.value
    }
    
    var result = "";
    for (var i = 0; i < fieldlist.length; i++)
    {
        result += fieldlist[i].fieldName + ";" +
                  fieldlist[i].width + ";" +
                  fieldlist[i].wrap + ";" + 
                  fieldlist[i].fontColor + ";" +
                  fieldlist[i].backgroundColor + ";" +
                  fieldlist[i].fontSize + ";" +
                  fieldlist[i].format;
                  
        if (i != fieldlist.length - 1)
        {
            result += ",";
        }
    }
    document.getElementById("TxtResultFormat").value = result;
    return true;
}