
function escapeVal(s) {
  var retStr = escape(s);
  // replace all unicode symbols
  var ix = retStr.indexOf("%");
  while(ix >= 0){
    if(ix+1 > retStr.length){
      retStr = retStr.substring(0, ix) + "\\\\";
    }else{
      retStr = retStr.substring(0, ix) + "\\\\" + retStr.substring(ix+1);
    }
    ix = retStr.indexOf("%", ix+2);
  }

  // replace all spaces back
  ix = retStr.indexOf("\\\\20");
  while(ix >= 0){
    if(ix+1 > retStr.length){
      retStr = retStr.substring(0, ix) + "%20";
    }else{
      retStr = retStr.substring(0, ix) + "%20" + retStr.substring(ix+4);
    }
    ix = retStr.indexOf("\\\\20", ix+3);
  }

  return retStr;
}

//--------------------------
// form elements generation and management

varChoice0 = "X";


function literalExpr( fieldName  ) {

    document.write("<select style=\"width:100px;margin-left:4px\" class=\"select\" name=\"" + fieldName + "\">");
    document.write("<option value=\"is unknown\">is unknown</option>");
    document.write("<option value=\"is exactly =\">is exactly =</option>");
    document.write("<option value=\"starts with\">starts with</option>");
    document.write("<option value=\"ends with\">ends with</option>");
    document.write("<option value=\"contains\">contains</option>");
    document.write("<option value=\"is &gt; (before)\"> is &gt; (before)</option>");
    document.write("<option value=\"is &lt; (after)\"> is &lt; (after)</option>");
    document.write("</select> ");

    document.write( "<input type=\"text\" name=\"" + fieldName +
                                        "Text\" size=\"20\" class=\"edit\">");
} // literalExpr
//-----------------------------
// class Select/combobox

function classSel( fieldName ) {
    document.write( "<select style=\"width:150px;margin-left:4px\" class=\"select\" name=\"" + fieldName + "\"");
    document.write(" onChange=\"{classSelOnChange(" + fieldName + ");}\">");
    document.write("</select>");
    theSel = document.f1.elements[fieldName];
    theSel.dependent = new Array();
} // classSel

function initIndepClassSel( select ) {
    while (select.options.length > 1)
        select.options[1] = null;

    childrenAsOptions(select, cTop.children, "");

//  history.go(0);
} // initIndepClassSel


function classSelUpdate( select, propName ) {

    while (select.options.length > 0)
        select.options[0] = null;

        if (propName == "")
                return;

    property = eval("p" + propName);
    if (property != null) {
        childrenAsOptions(select, property.range, "");
    }

//  history.go(0);
} // classSelUpdate

function childrenAsOptions(sel, children, offset) {

  for (var i=0; i < children.length; i++) {
    var child = children[i];
    var newLabel = offset + child.label;
    newOpt = new Option(newLabel, child.name, false, false);
    var isAvailable = false;
    for(var j=0; j<sel.options.length; j++){
      if(sel.options[j].value == child.name){
        if(sel.options[j].text.length < newLabel.length){
          sel.options[j] = null;
    sel.options[sel.options.length] = newOpt;

    if (child.children.length > 0) {
        childrenAsOptions(sel, child.children, offset + "--")
    }
        }
        isAvailable = true;
      }
    } // for
    if(!isAvailable){
        sel.options[sel.options.length] = newOpt;

        if (child.children.length > 0) {
            childrenAsOptions(sel, child.children, offset + "--")
        }
    }
  } // for
} // childrenAsOptions

function classSelOnChange(field) {
	var dep = field.dependent;
	if(dep == null) return;
    for (var i=0; i < dep.length; i++) {
        var propSel = eval( "document.f1." + field.dependent[i]);
        var theProp = trimNS(field.value);
        propSelUpdate(propSel, theProp);
        propSelOnChange(propSel);
    } // for
} // classSelOnChange
//-----------------------------
// property Select/combobox

function propertySel( fieldName, dependentClassVar ) {

    document.write("<select style=\"width:120px;margin-left:4px\" class=\"select\" name=\"" + fieldName +
                    "\" onChange=\"{propSelOnChange(" + fieldName + ");}\">");
    document.write("<option value=\"\"></option>");
    document.write("</select>");

    theSel = document.f1.elements[fieldName];
    theSel.dependent = dependentClassVar;
} // propertySel

function propSelOnChange(field) {
    var theClassSel = eval( "document.f1." + field.dependent);
    if (theClassSel == null)
                return;

    var theClass = trimNS(field.value);
    classSelUpdate(theClassSel, theClass);
    classSelOnChange(theClassSel);
} // propSelOnChange


function propSelUpdate( select, className ) {

    while (select.options.length > 0)
        select.options[0] = null;
    select.options[0] = new Option("---", "", false, false);
    if (className == "")
                return;
    theClass = eval("c" + className);
    if (theClass != null) {
                if (select.name.charAt(0) == 'p')
                        childrenAsOptions(select, theClass.props, "");
                else
                        childrenAsOptions(select, theClass.attribs, "");
    }

//  history.go(0);
} // propSelUpdate

//-----------------------------
// variable Select/combobox

function varSel(varSelName, vars, dependent) {
    document.write("<select style=\"width:34px;margin-left:4px\" class=\"edit1\" name=\"" + varSelName +
                    "\" onChange=\"{varSelOnChange(" + varSelName + ");}\">");

    for (var i=0; i<vars.length; i++) {
        document.write("<option value=\"" + vars[i] + "\">" + vars[i] + "</option>");
    }
    document.write("</select>");

    theSel = eval("document.f1." + varSelName);
    theSel.dependent = dependent;
    theSel.prevVal = theSel.value;

    newClassSel = eval("document.f1.class" + theSel.value);
    push(newClassSel.dependent,theSel.dependent);
} // varSel

function varSelOnChange(aVarSel) {
    prevClassSel = eval("document.f1.class" + aVarSel.prevVal);
    for (var i=0; i < prevClassSel.dependent.length; i++) {
        if (prevClassSel.dependent[i] == aVarSel.dependent) {
            prevClassSel.dependent = splice(prevClassSel.dependent, i, 1);
//            prevClassSel.dependent.splice(i,1);
            break;
        }
    } // for

    newClassSel = eval("document.f1.class" + aVarSel.value);
    push(newClassSel.dependent, aVarSel.dependent);

    var theClass = trimNS(newClassSel.value);
    propSelUpdate(eval( "document.f1." + aVarSel.dependent), theClass);

    aVarSel.preVal = aVarSel.value;
} // varSelOnChange

//-----------------------------
// attribute Select/combobox

function attributeSel( fieldName ) {

    document.write("<select style=\"width:120px;margin-left:4px\" class=\"select\" name=\"" + fieldName + "\">");
    document.write("<option value=\"\"></option>");
    document.write("</select>");

} // attributeSel


//--------------------------------------------
// UI styles and behaviour

// function for edit boxes
function txtNormal(obj) {
        obj.className="edit2";
}
function txtFocus(obj) {
  obj.className="edit2";
  obj.select();
}

// functions for buttons
function btnNormal(obj) {
  obj.className="button1";
}
function btnOnFocus(obj) {
  obj.className="button2";
}

//--------------------------------------------
// select an item in a combobox
function selComboItem(comboControl, matchValue){
	if(comboControl != null){
		for(var i=0; i<comboControl.options.length; i++){
			if(comboControl.options[i].value == matchValue){
				comboControl.selectedIndex = i;
				return;
			}
		}
	}
}

//--------------------------------------------
// trims the NS
function trimNS(value){
     var retStr = value.toString();
	 var ix = retStr.lastIndexOf('#');
     if(ix>-1){
      retStr = retStr.substring(ix+1,retStr.length);
    }
    return retStr;
}
