var xmlDoc;
var outputWindow;

function doWork() {
   outputWindow = window.open("", "mywin", "left=20,top=20,width=500,height=500,toolbar=0,resizable=1,scrollbars=1");
   var fileName = document.myForm.urlField.value;

   if (document.implementation && document.implementation.createDocument) {
      xmlDoc = document.implementation.createDocument("", "doc", null);
      xmlDoc.async = false;

      try {
         success = xmlDoc.load(fileName);
      } catch(e) {
         success = new Boolean("false");
      }

      if(success) {
         outputWindow.document.open();
         processXML();
      } else {
         alert("Error in XML");
      }
   } else if (window.ActiveXObject) {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = false;
            
      xmlDoc.load(fileName);
      
      if (xmlDoc.parseError.errorCode != 0) {
	handleIEError();
      } else {
        outputWindow.document.open();
	processXML();
      }
   } else {
      alert('Your browser cannot parse the xml document');
      return;
   }
}



function processXML() {
   outputWindow.document.open();
   outputWindow.document.writeln("<html><head>" );
   
   var root = xmlDoc.getElementsByTagName("userinterface");   
   var items = root[0].childNodes;

   
   for (i = 0; i < items.length; i++) {
      var nodeName = items[i].nodeName.toLowerCase();
      
      if(nodeName == "break") {
         outputWindow.document.writeln("<br/>");
      } else if(nodeName == "title") {
         outputWindow.document.writeln("<title>" + items[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue + "</title>");
         outputWindow.document.writeln("</head>");
         
         outputWindow.document.writeln("<body>");
         outputWindow.document.writeln("<h1>" + items[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue + "</h1>");
         
      } else if(nodeName == "textbox") {
         var textboxName = items[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
         var caption = items[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue;

	 outputWindow.document.writeln(caption + " <input type=textbox name=" + textboxName + "/>");         

      } else if(nodeName == "checkbox") {
         var name = items[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
         var caption = items[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue;
         
	 outputWindow.document.writeln("<input type=\"checkbox\" name=\"" + name + "\"/>" + caption);   
	 
      } else if(nodeName == "select") {
      	 var name = items[i].getElementsByTagName("name");
      	 var captions = items[i].getElementsByTagName("caption");
      	 
      	 outputWindow.document.writeln("<select name=\"" + name[0].childNodes[0].nodeValue + "\"/>"); 

      	 for( j = 0; j < captions.length; j++) {
      	    var selectValue = captions[j].childNodes[0].nodeValue;
      	    
      	    outputWindow.document.writeln("<option value=\"" + selectValue + "\">" + selectValue + "</option>"); 
      	 }

      	 outputWindow.document.writeln("</select>"); 
      	 
      } else if(nodeName == "radio") {
	 var name = items[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
	 var allElements = items[i].getElementsByTagName("*");
	 
	 for( j = 0; j < allElements.length; j++) {
	    var elementName = allElements[j].nodeName.toLowerCase();
	    
	    if(elementName == "caption") {
	       var radioValue = allElements[j].childNodes[0].nodeValue;
	       outputWindow.document.writeln("<input type=\"radio\" name=\"" + name + "\" value=\"" + radioValue + "\">" + radioValue + "</input>");
	    } else if(elementName == "break") {
	       outputWindow.document.writeln("<br/>");
	    } 
         }
         
      } else if(nodeName == "submit") {
         var caption = items[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue;
      
         outputWindow.document.writeln("<input type=\"submit\" value=\"" + caption + "\"/>");
      } else {
         /*Ignore #text from Mozilla*/
      }
   }
   
   outputWindow.document.writeln("</body></html>");
   outputWindow.document.close();

}


function handleIEError() {
   var myError = xmlDoc.parseError;
   
   outputWindow.document.open();
   outputWindow.document.writeln("<html>");
   outputWindow.document.writeln("<head><title>Parse Result</title></head>");
   outputWindow.document.writeln("<body>");
   outputWindow.document.writeln("<h1>XML Parsing Error</h1>");

   outputWindow.document.writeln("Error #: " + myError.errorCode + "<br/>");
   outputWindow.document.writeln("Description: " + myError.reason + "<br/>");
   outputWindow.document.writeln("In file: " + myError.url + "<br/>");
   outputWindow.document.writeln("Line #: " + myError.line + "<br/>");
   outputWindow.document.writeln("Character # in line: " + myError.linepos + "<br/>");
   outputWindow.document.writeln("Character # in file: " + myError.filepos + "<br/>");
   outputWindow.document.writeln("Source line: " + myError.srcText + "<br/>");
   
   outputWindow.document.writeln("</body></html>");
   outputWindow.document.close();
}
