
function rerankConstraints(dir,distance) {

    var curr = document.forms["conEditor"].currConstraints;
    if(dir!=1) dir = 0;
    if(distance!=1) distance = 0;
    
    var temp = [];
    var tempValue = [];
    var tempText  = [];
    
    for (var i=0 ; i< curr.options.length ; i++) {
        if (curr.options[i].selected) {
            temp[temp.length] = i;
            tempValue[tempValue.length] = curr.options[i].value
            tempText[tempText.length] = curr.options[i].text
        }
    }

    if ((temp[temp.length-1]-temp[0]) != (temp.length-1)) {
        alert("Please select a contiguous block of constraints to rerank.\nUse the SHIFT key to select more than one constraint");
    } else {

        if(distance==0) {

            if (dir==0 && temp[0]>0) {
                var saveValue = curr.options[temp[0]-1].value;
                var saveText  = curr.options[temp[0]-1].text;
                for(var i=0 ; i<temp.length ; i++) {
                    curr.options[temp[i]-1].value = curr.options[temp[i]].value;
                    curr.options[temp[i]-1].text  = curr.options[temp[i]].text;
                    curr.options[temp[i]-1].selected = true;
                }
                curr.options[temp[temp.length-1]].value = saveValue;
                curr.options[temp[temp.length-1]].text = saveText;
                curr.options[temp[temp.length-1]].selected = false;
            }
            if (dir==1 && temp[temp.length-1]<curr.options.length-1) {
                var saveValue = curr.options[temp[temp.length-1]+1].value;
                var saveText  = curr.options[temp[temp.length-1]+1].text;
                for(var i=temp.length-1 ; i>=0 ; i--) {
                    curr.options[temp[i]+1].value = curr.options[temp[i]].value;
                    curr.options[temp[i]+1].text  = curr.options[temp[i]].text;
                    curr.options[temp[i]+1].selected = true;
                }
                curr.options[temp[0]].value = saveValue;
                curr.options[temp[0]].text = saveText;
                curr.options[temp[0]].selected = false;
            }

        } else {
            if (dir==0 && temp[0]>0) {
                for(var i=(temp[0]-1) ; i>=0 ; i--) {
                    curr.options[i+temp.length].value = curr.options[i].value;
                    curr.options[i+temp.length].text  = curr.options[i].text;
                }
                for(var i=0 ; i < temp.length ; i++) {
                    curr.options[i].value = tempValue[i];
                    curr.options[i].text  = tempText[i];
                    curr.options[i].selected = true;
                    curr.options[temp[i]].selected = false
                }

            }
            if (dir==1 && temp[temp.length-1]<curr.options.length-1) {
                for(var i=(temp.length+temp[0]); i<curr.options.length ; i++) {
                    curr.options[i-temp.length].value = curr.options[i].value;
                    curr.options[i-temp.length].text  = curr.options[i].text;
                    curr.options[i-temp.length].selected = false
                }
                for(var i=0 ; i < temp.length ; i++) {
                    curr.options[curr.options.length-temp.length+i].value = tempValue[i];
                    curr.options[curr.options.length-temp.length+i].text  = tempText[i];
                    curr.options[curr.options.length-temp.length+i].selected = true;
                }
            }
        }
        writeCON();
    }
}

function transferConstraints(dir) {

    var con = [];
    con[0] = document.forms["conEditor"].currConstraints;
    con[1] = document.forms["conEditor"].unusedConstraints;
    if(dir!=1) dir = 0;

    var existSelect = false
    for (var i=0 ; i < con[dir].options.length ; i++) {
        if(con[dir].options[i].selected) {
            existSelect = true;
            break;
        }
    }

    if (existSelect) {
        for (var i=0 ; i < con[1-dir].options.length ; i++) {
            con[1-dir].options[i].selected = false
        }
        
        for (var i=0 ; i < con[dir].options.length ; i++) {
            if(con[dir].options[i].selected) {

                var tempValue = con[dir].options[i].value;
                var tempText  = con[dir].options[i].text;

                for (var j=i ; j<(con[dir].options.length-1) ; j++) {
                    con[dir].options[j].value    = con[dir].options[j+1].value;
                    con[dir].options[j].text     = con[dir].options[j+1].text;
                    con[dir].options[j].selected = con[dir].options[j+1].selected;
                }
                con[dir].options.length--;

                con[1-dir].options.length++
                con[1-dir].options[con[1-dir].options.length-1].value = tempValue;
                con[1-dir].options[con[1-dir].options.length-1].text  = tempText;
                con[1-dir].options[con[1-dir].options.length-1].selected = true;
                i--;
            }
        }
        writeCON();
    } else {
        alert("Please select one constraint or more to move.\nUse the SHIFT and/or CTRL keys to select more than one constraint");
    }
}

function writeCON() {
    
    var curr = document.forms["conEditor"].currConstraints.options;

    var CON = [];
    for (var i=0; i <curr.length ; i++) CON[CON.length] = curr[i].value;

    document.cookie = "CON=" + CON.join(",");
}

