// Chained Selects Parser

// Copyright Xin Yang 2004
// Web Site: www.yxScripts.com
// EMail: m_yangxin@hotmail.com
// Last Updated: 2005-02-09

var cs_subCount=1, cs_maxNest=1, cs_nestCount=1;
var cs_sepChar=",";

function parseOption(list, text, value, pick) {
  return 'addOption("'+list+'", "'+text+'", "'+value.replace(/\s+$/,"")+'"'+(pick?', 1':'')+");\n";
}

function parseListOption(list, text, value, sub, pick) {
  return 'addList("'+list+'", "'+text+'", "'+value.replace(/\s+$/,"")+'", "'+sub+'"'+(pick?', 1':'')+");\n";
}

function parseList(list, text, value, sub, pick, sublist) {
  var content="", subContent="", sc;
  var i=sublist.firstChild, item=null;

  while (i) {
    if (i.nodeType==1) {
      if (i.tagName && i.tagName=="LI") {
        if (item!=null) {
          content+=parseOption(sub, item[0], item[1]||item[0], item[2]||0);
        }

        j=i.firstChild;
        item=j.nodeValue.replace(/\s+$/,"").split(cs_sepChar);

        if (i.childNodes.length>1) {
          for (var x=0; x<i.childNodes.length; x++) {
            if (i.childNodes[x].tagName && i.childNodes[x].tagName=="UL") {
              cs_nestCount++;
              cs_maxNest=Math.max(cs_maxNest, cs_nestCount);
              sc=cs_subCount++;
              content+=parseListOption(sub, item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0);
              subContent+="\n"+parseList(sub, item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0, i.childNodes[x]);
              cs_nestCount--;

              item=null;
              break;
            }
          }
        }
      }
      else if (i.tagName && i.tagName=="UL") {
        if (item!=null) {
          cs_nestCount++;
          cs_maxNest=Math.max(cs_maxNest, cs_nestCount);
          sc=cs_subCount++;
          content+=parseListOption(sub, item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0);
          subContent+="\n"+parseList(sub, item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0, i);
          cs_nestCount--;

          item=null;
        }
      }
    }
    i=i.nextSibling;
  }

  if (item!=null) {
    content+=parseOption(sub, item[0], item[1]||item[0], item[2]||0);
  }

  return content+subContent;
}

function parseTop(list) {
  var content="", subContent="", sc, i=list.firstChild, item=null;

  while (i) {
    if (i.nodeType==1) {
      if (i.tagName && i.tagName=="LI") {
        if (item!=null) {
          content+=parseOption("cs-top", item[0], item[1]||item[0], item[2]||0);
        }

        j=i.firstChild;
        item=j.nodeValue.replace(/\s+$/,"").split(cs_sepChar);

        if (i.childNodes.length>1) {
          for (var x=0; x<i.childNodes.length; x++) {
            if (i.childNodes[x].tagName && i.childNodes[x].tagName=="UL") {
              cs_nestCount++;
              cs_maxNest=Math.max(cs_maxNest, cs_nestCount);
              sc=cs_subCount++;
              content+=parseListOption("cs-top", item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0);
              subContent+="\n"+parseList("cs-top", item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0, i.childNodes[x]);
              cs_nestCount--;

              item=null;
              break;
            }
          }
        }
      }
      else if (i.tagName && i.tagName=="UL") {
        if (item!=null) {
          cs_nestCount++;
          cs_maxNest=Math.max(cs_maxNest, cs_nestCount);
          sc=cs_subCount++;
          content+=parseListOption("cs-top", item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0);
          subContent+="\n"+parseList("cs-top", item[0], item[1]||item[0], "cs-sub-"+sc, item[2]||0, i);
          cs_nestCount--;

          item=null;
        }
      }
    }
    i=i.nextSibling;
  }

  if (item!=null) {
    content+=parseOption("cs-top", item[0], item[1]||item[0], item[2]||0);
  }

  return content+subContent;
}

function parseGroup(listGroup) {
  var content="", list=document.getElementById("CS")/*, listGroup=document.forms[0].listgroup.value*/;

  if (listGroup!="") {
    cs_subCount=1;
    cs_maxNest=1;
    cs_nestCount=1;
	content='var hide_empty_list=true;'+"\n\n";
	content='addListGroup("'+listGroup+'", "cs-top");'+"\n\n";
    content+=parseTop(list);

    return content;
  }
}

function execGroup(lst,str,holder) {
	js = "<!--\n";
	js+= str;
	js+= 'targetObj = document.getElementById("'+holder+'");';

	for (var i=1; i<cs_maxNest; i++) {
		js+= 'var newSelect = document.createElement("select");';
		js+= 'newSelect.setAttribute("id", "s'+i+'");';
		js+= 'newSelect.setAttribute("name", "s'+i+'");';
		if(window.addEventListener){ // Mozilla, Netscape, Firefox
			js+= 'newSelect.addEventListener("change", changeSelect, false);';
		} else if (window.attachEvent){ // IE
			js+= 'newSelect.attachEvent("onchange", changeSelect);';
		}
		js+= 'targetObj.appendChild(newSelect);';
	}

	js+= 'initListGroup("'+lst+'"';
    for (var i=1; i<cs_maxNest; i++) {
      js+= ', document.getElementById("s'+i+'")';
    }
    js+= ');';
	
	js+= "-->\n";
	
    try {
      eval(js.split("<!--").join("").split("-->").join(""));
    } catch(e) {
      alert(e);
    }
}

function showGroup(lst,str) {
  var code="";
  if (str!="") {

    code+= '<SCRIPT language="javascript" src="../../../chainedselects.js"></SCRIPT>';
    code+= '<SCRIPT language="javascript">';
    code+= str;
    code+= 'window.onload=function(){initListGroup("'+lst+'"';
    for (var i=1; i<cs_maxNest; i++) {
      code+= ', document.forms[0].s'+i;
    }
    code+= ');}>';
    code+= '</SCRIPT>';
	code+= '<FORM>';
    for (var i=1; i<cs_maxNest; i++) {
      code+= '<p><select style="width:200px;" name=s'+i+'></select></p>';
    }
    code+= '</FORM>';

	document.write(code);
  }
}

function showGroupNewWindow(lst,str) {
  if (str!="") {
    demoWin=window.open();

    with (demoWin.document) {
      writeln('<script language="javascript" src="../../../chainedselects.js"></script>');
      writeln('<script language="javascript">');
    }
    demoWin.document.writeln(str);
    demoWin.document.writeln('</script>');
    demoWin.document.write('<body onload="initListGroup('+"'"+lst+"'");

    with (demoWin.document) {
      for (var i=1; i<cs_maxNest; i++) {
        write(", document.forms[0].s"+i);
      }
      writeln(')">');
      writeln("<form>");
      for (var i=1; i<cs_maxNest; i++) {
        writeln("<p><select style='width:200px;' name=s"+i+"></select></p>");
      }
      writeln("</form>");
      writeln("</body>");
    }
    demoWin.document.close();
  }
}
