/* CLOSED_IMAGE - the image to be displayed when the sublists are closed
 * OPEN_IMAGE   - the image to be displayed when the sublists are opened
 */
CLOSED_IMAGE='/wp-content/plugins/dd-formmailer/images/plus.png';
OPEN_IMAGE='/wp-content/plugins/dd-formmailer/images/minus.png';


/* makeCollapsible - makes a list have collapsible sublists
 *
 * listElement - the element representing the list to make collapsible
 */

function removeValue(inputString, fromString) {
   var toString = '';
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


function makeCollapsible(listElement,textElementName){

  // removed list item bullets and the sapce they occupy
  listElement.style.listStyle='none';
  listElement.style.marginLeft='0';
  listElement.style.paddingLeft='0';

  // loop over all child elements of the list
  var child=listElement.firstChild;
  while (child!=null){
  		childName = child.innerHTML;
        childName = childName.toString();
        childName = childName.substring(0,childName.indexOf('<ul>'));

    // only process li elements (and not text elements)
    if (child.nodeType==1){

      // build a list of child ol and ul elements and hide them
      var list=new Array();
      var grandchild=child.firstChild;
      var greatgrandchild;
      while (grandchild!=null){
        if (grandchild.tagName=='OL' || grandchild.tagName=='UL'){
          grandchild.style.display='none';
          grandchild.style.cursor = 'default';
          grandchild.style.listStyle="none";

          greatgrandchild=grandchild.firstChild;
          while (greatgrandchild!=null){
              greatgrandchild.innerHTML='<INPUT class="checklist" TYPE=CHECKBOX NAME="'+greatgrandchild.innerHTML+'" onclick="(this.checked)?document.getElementById(\''+textElementName+'\').value=document.getElementById(\''+textElementName+'\').value+\''+greatgrandchild.innerHTML+';\':document.getElementById(\''+textElementName+'\').value=removeValue(document.getElementById(\''+textElementName+'\').value,\''+greatgrandchild.innerHTML+';\');">'+greatgrandchild.innerHTML;
              greatgrandchild.name='li';
              greatgrandchild=greatgrandchild.nextSibling;
          }


          list.push(grandchild);
        }
        grandchild=grandchild.nextSibling;
      }

      // add toggle buttons
      var node=document.createElement('img');
      node.setAttribute('src',CLOSED_IMAGE);
      node.setAttribute('class','collapsibleClosed');

      var nodecheckbox = document.createElement('input');
      nodecheckbox.setAttribute('type','checkbox');
      nodecheckbox.style.width='25px';
      nodecheckbox.onclick=createToggleFunction(node,list,textElementName,childName);
      child.insertBefore(nodecheckbox,child.firstChild);

      child.insertBefore(node,child.firstChild);
      //node.onclick=createToggleFunction(node,list);
      //child.style.cursor = 'pointer';

    }

    child=child.nextSibling;
  }

}

/* createToggleFunction - returns a function that toggles the sublist display
 *
 * toggleElement  - the element representing the toggle gadget
 * sublistElement - an array of elements representing the sublists that should
 *                  be opened or closed when the toggle gadget is clicked
 */
function createToggleFunction(toggleElement,sublistElements,textElementName,childName){


  return function(){



      // toggle status of toggle gadget
      if (toggleElement.getAttribute('class')=='collapsibleClosed'){
        toggleElement.setAttribute('class','collapsibleOpen');
        toggleElement.setAttribute('src',OPEN_IMAGE);
		document.getElementById(textElementName).value=document.getElementById(textElementName).value+childName+';';
      }else{
        toggleElement.setAttribute('class','collapsibleClosed');
        toggleElement.setAttribute('src',CLOSED_IMAGE);
		document.getElementById(textElementName).value=document.getElementById(textElementName).value=removeValue(document.getElementById(textElementName).value,childName+';');
      }

      // toggle display of sublists
      for (var i=0;i<sublistElements.length;i++){
        sublistElements[i].style.display=
            (sublistElements[i].style.display=='block')?'none':'block';
      }

  }

}

function whichelement(elem) {
	var x = event.clientX
	var y = event.clientY
	if (window.event.srcElement.name)
		status = window.event.srcElement.name;
	else
		status = "";

	return status;
}
