var uCheckboxNames  // Names of checkboxes in a form
var uAllXML         /* xml representation of all input elements */

// make this uMergeElement, have second function for umerge XML and perhaps for text, js, etc.
function uMrgElemXML(e,x){                                                                                                                 // merge XML value of form element e with XML string x
    var sT="<"+e.name+">"   									// create XML start-tag for element name
    var eT="</"+e.name+">"  									// create XML end-tag for element name
    var v=uGetElemVal(e);
    var i = 0									// will return null if no val or empty element
    if ((e.type=="checkbox") && (v != null) && (v != '')) {  
	var sLR="<CHECKBOX_LIST>"  
	var eLR="</CHECKBOX_LIST>"
              var sR="<CHECKBOX>"   
              var eR="</CHECKBOX>" 
	if (!inUCheckboxNames(e.name)) { 							// has there been a checkbox with this name before?
//	    uCheckboxNames.push(e.name)  							// First encounter:  place checkbox name in checkbox name array
                  uCheckboxNames[i] = e.name; 
	    i++;
	    var z=x.indexOf(eLR)                                              					
                  if ( z == -1 ) {x = x + sLR +  sR + v + eR + eLR}  
                  else if ( z > -1 ) {                                                    
                         x = x.substring(0, z) + sR + v + eR + x.substring(z)   
                  }
              }  
    } else if (v!='') {                                                      						// (v ONLY '' if unchecked radio button - do nothing) */
	var z=x.indexOf(sT);                                              					// check for index of existing entry in XML */
	if(z>-1)x=x.substring(0,z)+x.substring(x.indexOf(eT)+eT.length);  				// extract old value if present */
	if(v!=null)x=x+sT+v+eT;                                           					// append the element value if not null */
    }
// alert(x);
    return x;  
}  

function uGetElemVal(e){     /* return value of form element f, or null if empty (exception: radio) */
  if(e.type=='input') return e.value; 
  if(e.type=='checkbox'&&e.checked)return e.value;                                  /* CHECKBOX */
  if(e.type=='radio'){if(e.checked){return e.value}else{return''}}                  /* RADIO: empty str ONLY for unchecked */
  if(e.type=='hidden'&&e.value!='')return e.value;                                  /* HIDDEN */
  if(e.type=='password'&&e.value!='')return e.value;                                /* PASSWORD */
  if(e.type.indexOf('text')>-1&&e.value!='')return e.value;                         /* TEXT or TEXTAREA */
  if(e.type.indexOf('select')>-1){                                                  /* SELECT: returns first of multiple */
    if(e.selectedIndex>-1){                                                         /* is an option selected? */
      if (e.options[e.selectedIndex].value)return e.options[e.selectedIndex].value  /* return value (if attributed) */
      return e.options[e.selectedIndex].text                                        /* else return text attribute */
    }
    return null;                                                                    /* no option selected, return null */
  }
  /* no handlers: IMAGE, BUTTON, FILE UPLOAD */
  return null;                                                                      /* DEFAULT return null */
}

function inUCheckboxNames(n){
    for(var i=0;i<uCheckboxNames.length;i++) { if(n==uCheckboxNames[i]) {return true} }
    return false;
}


function uMrgNV(n,v,x){           /* merge name value pair as XML with XML string x*/    
  var sT="<"+n+">"                                                    /* create XML start-tag for element name */
  var eT="</"+n+">"                                                   /* create XML end-tag for element name */  
  var z = x.indexOf(sT)                                               /* check for index of existing entry in XML */
  if(z>-1)x=x.substring(0,z)+x.substring(x.indexOf(eT)+eT.length)     /* extract old value if present */
  if(v!=null)x=x+sT+v+eT                                              /* append the element value if not null */
  return x
}

function uFormsIt(doc){           /* iterate over forms in doc, merge XML for each element */
  for (var i=0; i<doc.forms.length; i++){                               /* step through forms in doc */
    for (var j=0; j<doc.forms[i].length; j++){                          /* step through elements of form */
      uAllXML = uMrgElemXML(doc.forms[i].elements[j], uAllXML); /* reset uAllXML to the merge of it and element XML */
    }
  }
}

function uSubmit(actionId){   /* submit state by control page submit with allXML, go to next state */
   
	document.XMLForm.Xml.value  = "";
	uAllXML = "";  
	uCheckboxNames=new Array();  // clear and reset uCheckboxNames 
	for (var i=1; i<uSubmit.arguments.length; i++) {                    /* iterate through additional arguments */
		uAllXML=uMrgNV('Arg'+i,uSubmit.arguments[i],uAllXML);    /* merge args with allXML individually */
	} 
	uFormsIt(document);                                                /* harvest XML now from all input elements */    
	document.XMLForm.Xml.value  = uAllXML + "<ActionId>" + actionId + "</ActionId>";                    /* set cPage uX field to updated */   
	//alert(document.forms[0].Xml.value);   

	document.XMLForm.action="/USIPortal/action/invoker.jsp/?ActionId=" + actionId;
	document.XMLForm.submit();
}

function iSubmit(actionId){   /* submit state by control page submit with allXML, go to next state */
   
	document.XMLForm.Xml.value  = "";
	uAllXML = "";  
	uCheckboxNames=new Array();  // clear and reset uCheckboxNames 
	for (var i=1; i<iSubmit.arguments.length; i++) {                    /* iterate through additional arguments */
		uAllXML=uMrgNV('Arg'+i,iSubmit.arguments[i],uAllXML);    /* merge args with allXML individually */
	} 
	uFormsIt(document);                                                /* harvest XML now from all input elements */    
	document.XMLForm.Xml.value  = uAllXML + "<ActionId>" + actionId + "</ActionId>";                    /* set cPage uX field to updated */   
	//alert(document.forms[0].Xml.value);
	document.XMLForm.action=document.XMLForm.action + "?ActionId=" + actionId;   
	document.XMLForm.action=document.XMLForm.action + "&ActionAppId=" + document.XMLForm.ActionAppId.value;
	
	document.XMLForm.submit();
} 

function uFormElements(theForm) {           /* iterate over the specified form, merge XML for each element */
  for (var j=0; j<theForm.length; j++) {                        /* step through elements of the form */
      uAllXML = uMrgElemXML(theForm.elements[j], uAllXML); /* reset uAllXML to the merge of it and element XML */
  }
}

function fSubmit(theForm, actionId) {   /* submit state by control page submit with allXML and a specified form, go to next state */
   
	uAllXML = "";  
	uCheckboxNames=new Array();  // clear and reset uCheckboxNames 
	for (var i=2; i<fSubmit.arguments.length; i++) {             /* iterate through additional arguments */
		count = i - 1;
		uAllXML=uMrgNV('Arg'+count,fSubmit.arguments[i],uAllXML);    /* merge args with allXML individually */
	} 
	uFormElements(theForm);                                                /* harvest XML now from all input elements */    
	document.XMLForm.Xml.value  = uAllXML + "<ActionId>" + actionId + "</ActionId>";   /* set cPage uX field to updated */   
	//alert(document.XMLForm.Xml.value);   

	document.XMLForm.action=document.XMLForm.action + "?ActionId=" + actionId;   
	document.XMLForm.action=document.XMLForm.action + "&ActionAppId=" + document.XMLForm.ActionAppId.value;
	
	document.XMLForm.submit();
}