//
// Add a repeater row to the table with the id <id>table.
// The variable "template" contains the HTML code that we need to 
// place in the table row we create.
//

var repeaterErrorCtr = 1;
var templateCtr = 0;
var _state;
var check = 0;
var defaultTemplate;
var _repeaterCountProperty;
var arrayIndexRegex = /\[[0-9]+\]/g;
var functionArrayIndexRegex = /\_[0-9]+\_/g;
var hrefArrayIndexRegex = /[0-9]+/g;
var traverseCtr;
var isSafari =(navigator.userAgent.indexOf('Safari') != -1);

function add(id, template,dummytemplate, layout, state, repeaterCountProperty) {
if(dummytemplate != null && dummytemplate != '' && dummytemplate != undefined)
defaultTemplate = dummytemplate;
if(template == null || template == undefined || template == '')
template = defaultTemplate;
if(repeaterCountProperty != null && repeaterCountProperty != '' && repeaterCountProperty != undefined)
_repeaterCountProperty = repeaterCountProperty;


   _state = state;


  //
  // Create a copy of the template so we can modify it
  // 
  var templateCopy = new String(template);
  templateCopy = "<div id='errors-section-repeater"+repeaterErrorCtr+"'></div>" + templateCopy;
  repeaterErrorCtr++;	
  
  //
  // Retrieve handle to repeater div
  //
 var repeaterDiv = document.getElementById(id+"div");
 var repeaterCountDiv = _repeaterCountProperty + "div";
 //var repeaterDivParent = document.getElementById(repeaterDiv).parentNode;
 if(document.getElementsByName(_repeaterCountProperty)[0] == null || document.getElementsByName(_repeaterCountProperty)[0] == '' || document.getElementsByName(_repeaterCountProperty)[0] == undefined) 
{
    var divElement = document.createElement("div");
    divElement.setAttribute("id",repeaterCountDiv);
    repeaterDiv.appendChild(divElement);
    divElement.innerHTML="<input type='hidden' name='" +_repeaterCountProperty + "'/>"; 
}
 //
  // Retrieve all "fieldset" children of div
  //

  var fieldSets = repeaterDiv.getElementsByTagName("fieldset");
  
  //
  // Look for "add" links, and replace them with delete links
  //
  for( var i = 0; i < fieldSets.length; i++) {

    var addAnchor = getAddDeleteAnchor(fieldSets[i], 'add');
    var deleteAnchor = getAddDeleteAnchor(fieldSets[i], 'delete');

    if (!deleteAnchor && _state != 'review' && _state != 'output') {
      removeAddDeleteAnchor(fieldSets[i], 'add');
      fieldSets[i].appendChild(createAddDeleteAnchor('delete', id, template, i));
    } else if (addAnchor) {
      fieldSets[i].removeChild(addAnchor);
    }
  }

  //
  // Replace all indexes of ("$i") with the current loop counter. 
  // 

//  while(templateCopy.indexOf('$i') != -1) {
//   templateCopy = templateCopy.replace('$i', ctr+1);
//   }

if(templateCopy != null && templateCopy != undefined && templateCopy != '') {
       templateCopy = templateCopy.replace(arrayIndexRegex,"["+templateCtr+"]");
       templateCopy = templateCopy.replace(functionArrayIndexRegex,"_"+templateCtr+"_");
                   templateCtr++;

}
  // New fieldset for repeater row
  //
  var fs = document.createElement("fieldset");
  if (layout) {
    fs.setAttribute("class", layout);
  }

  //
  // Assign inner HTML to table column
  //
  
  fs.innerHTML = templateCopy;
  //
  // Create Add link and set within table column
  //
  if(_state!='review' && _state != 'output')
  fs.appendChild(createAddDeleteAnchor('add', id, template));
  
  //
  // Create "delete" link only if we have at least one delete link already
  //
  
  if (fieldSets.length > 0 && _state!='review' && _state != 'output') {
  fs.appendChild(createAddDeleteAnchor('delete', id, template, (fieldSets.length), true));
  }
  repeaterDiv.appendChild(fs);
  refreshAllInputTagId(id);
  fieldSets = repeaterDiv.getElementsByTagName("fieldset");
  document.getElementsByName(_repeaterCountProperty)[0].value=fieldSets.length;

//window.location.reload(false);
}

//
// Remove a repeater row
//

function remove(id,rowIndex,template) {
  var repeaterDiv = document.getElementById(id+"div");
  var fieldSets = repeaterDiv.getElementsByTagName("fieldset");

  //
  // Delete last row, Special Case
  //

  if(rowIndex == fieldSets.length-1) {
    repeaterDiv.removeChild(fieldSets[fieldSets.length-1]);
    appendDeleteLinkToLastRow(id, template); 
  } else {

    //
    // Remove fieldset corresponding to index we're given
    //

    repeaterDiv.removeChild(fieldSets[rowIndex]);
    
    //
    // If we have only one row left, take away the "delete" link
    //

    if(fieldSets.length == 1) {
      removeAddDeleteAnchor(fieldSets[0], 'delete');
    }
  }
  refreshDeleteLinks(id,template);
  refreshAllInputTagId(id);
  fieldSets = repeaterDiv.getElementsByTagName("fieldset");
  document.getElementsByName(_repeaterCountProperty)[0].value=fieldSets.length;
}

//
// Add "Remove" link to last row, if there is more than 
// one row on the repeater; if not, add only a "Remove" link
//

function appendDeleteLinkToLastRow(id, template){
  var repeaterDiv = document.getElementById(id+"div");
  var fieldSets = repeaterDiv.getElementsByTagName("fieldset");
  var lastRow = fieldSets[fieldSets.length-1];

  if(fieldSets.length == 1) {
    //
    // Remove "Delete" link, and add "Add" link
    //

    removeAddDeleteAnchor(fieldSets[0], 'delete');
    if(_state != 'review' && _state != 'output')
    {
    fieldSets[0].appendChild(createAddDeleteAnchor('add', id, template));
    }
  } else {
    //
    // Replace 'delete' link on last row
    //

    removeAddDeleteAnchor(lastRow, 'delete');
    if(_state!='review' && _state != 'output')
    {
    lastRow.appendChild(createAddDeleteAnchor('delete', id, template, (fieldSets.length-1)));
    lastRow.appendChild(createAddDeleteAnchor('add', id, template));
    }
  }
}

//
// Refresh indices of delete links
//

function refreshDeleteLinks(id,template) {
  var repeaterDiv = document.getElementById(id+"div");
  var fieldSets = repeaterDiv.getElementsByTagName("fieldset");
  
  //
  // Start loop at 1 because we don't want to create a delete link for first row
  //

  for( var i = 1; i < fieldSets.length; i++) {
    removeAddDeleteAnchor(fieldSets[i], 'delete');
    if(_state!='review' && _state != 'output')
    {
    fieldSets[i].appendChild(createAddDeleteAnchor('delete', id, template, i));
    }
  }
}

//
// Refresh indices of all input tags after an add/delete operation
//

function refreshAllInputTagId(id) {

  var repeaterDiv = document.getElementById(id+"div");
  var fs = repeaterDiv.childNodes[0];
  var ctr = 0;
  traverseCtr = 0;
  while(fs.tagName != "FIELDSET")
	fs = fs.nextSibling;
  
  while(fs) {
    
    if(isSafari) {
       traverseAllTags(fs);
    } else {
       var allInputTags = fs.getElementsByTagName("INPUT");

       if(allInputTags != null) 
          refreshAllTags(allInputTags,ctr);

       var allTextAreaTags = fs.getElementsByTagName("TEXTAREA");
       if(allTextAreaTags != null)
          refreshAllTags(allTextAreaTags,ctr);
    
       var allSelectTags = fs.getElementsByTagName("SELECT");
       if(allSelectTags != null)
          refreshAllTags(allSelectTags,ctr);
       var allSelectTags = fs.getElementsByTagName("SPAN");
       if(allSelectTags != null)
          refreshAllTags(allSelectTags,ctr);

       var allAnchorTags = fs.getElementsByTagName("A");
       if(allAnchorTags != null)
          refreshAllAnchors(allAnchorTags,ctr);

       var allDivTags = fs.getElementsByTagName("DIV");
       if(allDivTags != null)
          refreshAllDivIds(allDivTags,ctr);
       var script = fs.getElementsByTagName("SCRIPT");
       var scriptContent="";
       for(var i=0; i < script.length; i++) {
          scriptContent="";
	  if(script[i].textContent != undefined && script[i].textContent != null) {
	     scriptContent = script[i].textContent;
          } else if(script[i].text != undefined && script[i].text != null) {
             scriptContent = script[i].text;
          }
   
          if(scriptContent != null && scriptContent != undefined && scriptContent != "") {
              scriptContent = scriptContent.replace(arrayIndexRegex,"["+ctr+"]");
              scriptContent = scriptContent.replace(functionArrayIndexRegex,"_"+ctr+"_");
              if(script[i].textContent != undefined && script[i].textContent != null) {
	         script[i].textContent=scriptContent;
	      }
	      if(script[i].text != undefined && script[i].text != null) {
	          script[i].text = scriptContent;
	      }
          }
       }
    }
    fs = fs.nextSibling;   
    while(fs != undefined && fs !=null && fs.tagName != "FIELDSET")
	fs = fs.nextSibling;
 
    ctr++;
    traverseCtr++;
  }
}


function refreshAllDivIds(allTags,ctr) {
  for ( var i = 0; i != allTags.length; i++) {
    if(allTags[i].getAttribute("id") != undefined &&  allTags[i].getAttribute("id") != null )
    {
	
        allTags[i].setAttribute("id",allTags[i].getAttribute("id").replace(functionArrayIndexRegex,"_"+ctr+"_"));
    }
    else
    {
	allTags[i].setAttribute("display",'');
    }
  }
}

//
// Regular-expression replace all input name indices with the current index
//

function refreshAllTags(allTags,ctr) {
  for ( var i = 0; i != allTags.length; i++) {

    // Changing the indices in outerHTML for IE 
    if(allTags[i].getAttribute("name") != undefined &&  allTags[i].getAttribute("name") != null ){
        allTags[i].setAttribute("name",allTags[i].getAttribute("name").replace(arrayIndexRegex,"["+ctr+"]"));
        allTags[i].setAttribute("name",allTags[i].getAttribute("name").replace(functionArrayIndexRegex,"_"+ctr+"_"));
      }
    if(allTags[i].getAttribute("id") != undefined &&  allTags[i].getAttribute("id") != null ){
    	allTags[i].setAttribute("id",allTags[i].getAttribute("id").replace(arrayIndexRegex,"["+ctr+"]"));
   	allTags[i].setAttribute("id",allTags[i].getAttribute("id").replace(functionArrayIndexRegex,"_"+ctr+"_"));
      }
    var onchangeValue="";
    var onclickValue="";
    var onfocusValue="";
    var onblurValue="";

    // Replacing the indices in outerHTML removes events value(onclick, onchange ...)
    // so, we are storing the value of event attributes to a variable before replacing the 
    // outerHTML

    if(allTags[i].getAttribute("onchange") != undefined &&  allTags[i].getAttribute("onchange") != null )
	onchangeValue = allTags[i].getAttribute("onchange");
    if(allTags[i].getAttribute("onclick") != undefined &&  allTags[i].getAttribute("onclick") != null )
        onclickValue = allTags[i].getAttribute("onclick");
    if(allTags[i].getAttribute("onfocus") != undefined &&  allTags[i].getAttribute("onfocus") != null )
	onfocusValue = allTags[i].getAttribute("onfocus");
    if(allTags[i].getAttribute("onblur") != undefined &&  allTags[i].getAttribute("onblur") != null )
	onblurValue = allTags[i].getAttribute("onblur");
    //For IE we need to replace outerHTML in order to replace the indices of name attribute
    if(!isSafari) {
        if(allTags[i].outerHTML != null && allTags[i].outerHTML != undefined && allTags[i].outerHTML != ''){
           allTags[i].outerHTML =  allTags[i].outerHTML.replace(arrayIndexRegex,"["+ctr+"]");
           allTags[i].outerHTML =  allTags[i].outerHTML.replace(functionArrayIndexRegex,"_"+ctr+"_");
         }
    }

    if(onchangeValue !=null && onchangeValue != undefined && onchangeValue != '')
    {
         if(typeof onchangeValue == 'function')
         {
	     //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
	     //the following code will extract the content of the function and creates new function 
	     //object and assigns it to the event.

             onchangeValue = onchangeValue.toString();
             if(onchangeValue.indexOf("{") >= 0 && onchangeValue.lastIndexOf("}") >=0)
             {
                onchangeValue = onchangeValue.substring(onchangeValue.indexOf("{")+1,onchangeValue.lastIndexOf("}"));
             }
             onchangeValue = onchangeValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
	     onchangeValue = onchangeValue.replace(arrayIndexRegex,"["+ctr+"]");
             var myObject=new Function(onchangeValue);
             allTags[i].setAttribute("onchange",myObject);
        }
        else if(typeof onchangeValue == 'string')
        {
	   //If the browser returns string object
           onchangeValue = onchangeValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onchangeValue = onchangeValue.replace(arrayIndexRegex,"["+ctr+"]");
           allTags[i].setAttribute("onchange",onchangeValue);
        }
	else
	  alert("This functionality is not supported in this browser");
     }

    if(onclickValue !=null && onclickValue != undefined && onclickValue != '')
    {

         if(typeof onclickValue == 'function')
	 {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

	     onclickValue = onclickValue.toString();
	     if(onclickValue.indexOf("{") >= 0 && onclickValue.lastIndexOf("}") >=0)
	     {
	        onclickValue = onclickValue.substring(onclickValue.indexOf("{")+1,onclickValue.lastIndexOf("}"));
	     }
	     onclickValue = onclickValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
	     onclickValue = onclickValue.replace(arrayIndexRegex,"["+ctr+"]");
	     var myObject=new Function(onclickValue);
	     allTags[i].setAttribute("onclick",myObject);	
         }
         else if(typeof onclickValue == 'string')
         {
	   //If the browser returns string object
           onclickValue = onclickValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onclickValue = onclickValue.replace(arrayIndexRegex,"["+ctr+"]");
           allTags[i].setAttribute("onclick",onclickValue);
         }
	 else
	    alert("This functionality is not supported in this browser");
    }

    if(onfocusValue !=null && onfocusValue != undefined && onfocusValue != '')
    {
         if(typeof onfocusValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.
	
             onfocusValue = onfocusValue.toString();
             if(onfocusValue.indexOf("{") >= 0 && onfocusValue.lastIndexOf("}") >=0)
             {
                onfocusValue = onfocusValue.substring(onfocusValue.indexOf("{")+1,onfocusValue.lastIndexOf("}"));
             }
             onfocusValue = onfocusValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
	     onfocusValue = onfocusValue.replace(arrayIndexRegex,"["+ctr+"]");
             var myObject=new Function(onfocusValue);
             allTags[i].setAttribute("onfocus",myObject);
         }
         else if(typeof onfocusValue == 'string')
         {
           onfocusValue = onfocusValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onfocusValue = onfocusValue.replace(arrayIndexRegex,"["+ctr+"]");
           allTags[i].setAttribute("onfocus",onfocusValue);
        }
	else
	   alert("This functionality is not supported in this browser");
    } 
    if(onblurValue !=null && onblurValue != undefined && onblurValue != '')
    {
         if(typeof onblurValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.
	
             onblurValue = onblurValue.toString();
             if(onblurValue.indexOf("{") >= 0 && onblurValue.lastIndexOf("}") >=0)
             {
                onblurValue = onblurValue.substring(onblurValue.indexOf("{")+1,onblurValue.lastIndexOf("}"));
             }
             onblurValue = onblurValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
	     onblurValue = onblurValue.replace(arrayIndexRegex,"["+ctr+"]");
             var myObject=new Function(onblurValue);
             allTags[i].setAttribute("onblur",myObject);
         }
         else if(typeof onblurValue == 'string')
         {
           onblurValue = onblurValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onblurValue = onblurValue.replace(arrayIndexRegex,"["+ctr+"]");
           allTags[i].setAttribute("onblur",onblurValue);
        }
	else
	   alert("This functionality is not supported in this browser");
    } 
 }
}

function refreshAllAnchors(allTags,ctr) {
  var fName;
  for ( var i = 0; i != allTags.length; i++) {
    if(allTags[i].getAttribute("name") != undefined &&  allTags[i].getAttribute("name") != null ){
    allTags[i].setAttribute("name",allTags[i].getAttribute("name").replace(arrayIndexRegex,"["+ctr+"]"));
   allTags[i].setAttribute("name",allTags[i].getAttribute("name").replace(functionArrayIndexRegex,"_"+ctr+"_"));

}
    if(allTags[i].getAttribute("id") != undefined &&  allTags[i].getAttribute("id") != null ){
    allTags[i].setAttribute("id",allTags[i].getAttribute("id").replace(arrayIndexRegex,"["+ctr+"]"));
   allTags[i].setAttribute("id",allTags[i].getAttribute("id").replace(functionArrayIndexRegex,"_"+ctr+"_"));
}
    if(allTags[i].getAttribute("id") != undefined &&  allTags[i].getAttribute("id") != null && allTags[i].getAttribute("id") == 'delete')
    {
        allTags[i].setAttribute("href",allTags[i].getAttribute("href").replace(hrefArrayIndexRegex,ctr));
        allTags[i].setAttribute("href",allTags[i].getAttribute("href").replace(arrayIndexRegex,"["+ctr+"]"));
        allTags[i].setAttribute("href",allTags[i].getAttribute("href").replace(functionArrayIndexRegex,"_"+ctr+"_"));
    }
   
    var onmouseoverValue="";
    var onmouseoutValue="";
    if(allTags[i].getAttribute("onmouseover") != undefined &&  allTags[i].getAttribute("onmouseover") != null )
        onmouseoverValue = allTags[i].getAttribute("onmouseover");
    if(allTags[i].getAttribute("onmouseout") != undefined &&  allTags[i].getAttribute("onmouseout") != null )
        onmouseoutValue = allTags[i].getAttribute("onmouseout");
    //For IE we need to replace outerHTML in order to replace the indices of name attribute
    if(!isSafari) {
       if(allTags[i].outerHTML != null && allTags[i].outerHTML != undefined && allTags[i].outerHTML != ''){ 
         allTags[i].outerHTML =  allTags[i].outerHTML.replace(arrayIndexRegex,"["+ctr+"]");
         allTags[i].outerHTML =  allTags[i].outerHTML.replace(functionArrayIndexRegex,"_"+ctr+"_");
       }
    }

    if(onmouseoverValue !=null && onmouseoverValue != undefined && onmouseoverValue != '')
    {
         if(typeof onmouseoverValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onmouseoverValue = onmouseoverValue.toString();
             if(onmouseoverValue.indexOf("{") >= 0 && onmouseoverValue.lastIndexOf("}") >=0)
             {
                onmouseoverValue = onmouseoverValue.substring(onmouseoverValue.indexOf("{")+1,onmouseoverValue.lastIndexOf("}"));
             }
             onmouseoverValue = onmouseoverValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             onmouseoverValue = onmouseoverValue.replace(arrayIndexRegex,"["+ctr+"]");
             var myObject=new Function(onmouseoverValue);
             allTags[i].setAttribute("onmouseover",myObject);
        }
        else if(typeof onmouseoverValue == 'string')
        {
           //If the browser returns string object
             onmouseoverValue = onmouseoverValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             onmouseoverValue = onmouseoverValue.replace(arrayIndexRegex,"["+ctr+"]");
             allTags[i].setAttribute("onmouseover",onmouseoverValue);
        }
        else
          alert("This functionality is not supported in this browser");
     }

    if(onmouseoutValue !=null && onmouseoutValue != undefined && onmouseoutValue != '')
    {
         if(typeof onmouseoutValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onmouseoutValue = onmouseoutValue.toString();
             if(onmouseoutValue.indexOf("{") >= 0 && onmouseoutValue.lastIndexOf("}") >=0)
             {
                onmouseoutValue = onmouseoutValue.substring(onmouseoutValue.indexOf("{")+1,onmouseoutValue.lastIndexOf("}"));
             }
             onmouseoutValue = onmouseoutValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
	     onmouseoutValue = onmouseoutValue.replace(arrayIndexRegex,"["+ctr+"]");
             var myObject=new Function(onmouseoutValue);
             allTags[i].setAttribute("onmouseout",myObject);
        }
        else if(typeof onmouseoutValue == 'string')
        {
           //If the browser returns string object
           onmouseoutValue = onmouseoutValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onmouseoutValue = onmouseoutValue.replace(arrayIndexRegex,"["+ctr+"]");	
           allTags[i].setAttribute("onmouseout",onmouseoutValue);
        }
        else
          alert("This functionality is not supported in this browser");
     }

  }
}

//
// Create an "Add" or "Delete" anchor
//

function createAddDeleteAnchor(type, id, template, counter) {
  if (type == 'add' ) {
    var addAnchor = document.createElement("a");
    var dummyCounter = 0;
    templete = template.replace(arrayIndexRegex,"["+counter+"]");
    if(counter != null && counter != undefined && counter != '')
    dummyCounter = counter+1;
    defaultTemplete = defaultTemplate.replace(arrayIndexRegex,"["+dummyCounter +"]");
    addAnchor.setAttribute("href","javascript:add('"+id+"', \"\", \"\",'','','');");
    addAnchor.setAttribute("id", "add");
    addAnchor.setAttribute("class", "addlink");
    addAnchor.innerHTML="ADD ANOTHER";
    return addAnchor;
  }

  if (type == 'delete') {
    var deleteAnchor = document.createElement("a");
    deleteAnchor.setAttribute("href","javascript:remove('"+id+"',"+counter+","+"\"\");");
    deleteAnchor.setAttribute("id", "delete");
    deleteAnchor.setAttribute("class", "removelink");
    deleteAnchor.innerHTML="DELETE";
    return deleteAnchor;
  }    
}

//
// Return all the links in a fieldset
//

function getFieldsetLinks(fieldset) {
  return fieldset.getElementsByTagName("a");
}

//
// Return a reference to the "add" or "delete" link in a row
//

function getAddDeleteAnchor(fieldset, type) {
  var anchors = getFieldsetLinks(fieldset);
  for (var counter = 0; counter < anchors.length; counter++) {
    if (anchors[counter].id == type) {
      return anchors[counter];
    }
  }
}

//
// Remove an "Add" or "Remove" anchor from a repeater row
//

function removeAddDeleteAnchor(fieldset, type) {
  var anchor = getAddDeleteAnchor(fieldset, type);
  if (anchor) {
    fieldset.removeChild(anchor);
  }
}



// This Block of Code is for Safari //


function traverseAllTags(currentTag) {
  if(null != currentTag && undefined != currentTag) {
   
    var child_Nodes = currentTag.childNodes;
    if(null != child_Nodes && undefined != child_Nodes) {
       for(var loopctr=0; loopctr < child_Nodes.length; loopctr++) {
          var cNode = child_Nodes[loopctr];
          if(null != cNode && undefined != cNode) {
             if(cNode.tagName == "FIELDSET" || cNode.tagName == "DIV" || cNode.tagName == "TABLE" || cNode.tagName == "TBODY" || cNode.tagName == "TR" || cNode.tagName == "TD") {
	       if(cNode.tagName == "DIV") 
		 refreshDivIds(cNode, traverseCtr); 
               traverseAllTags(cNode);
	     } else {
                var ccNode = child_Nodes[loopctr];
                if(ccNode.tagName == "INPUT" || ccNode.tagName == "input") {
                   refreshTag(ccNode,traverseCtr);
                } else if(ccNode.tagName == "SELECT" || ccNode.tagName == "select") {
                   refreshTag(ccNode,traverseCtr);
                } else if(ccNode.tagName == "TEXTAREA" || ccNode.tagName == "textarea") {
                   refreshTag(ccNode,traverseCtr);
                } else if(ccNode.tagName == "A" || ccNode.tagName == "a") {
                   refreshAnchorTag(ccNode,traverseCtr);
                } else if(ccNode.tagName == "SPAN" || ccNode.tagName == "span") {
                   refreshTag(ccNode,traverseCtr);
                } else if(ccNode.tagName == "script" || ccNode.tagName == "SCRIPT") {

                   var scriptContent="";
                   if(ccNode.innerHTML != undefined && ccNode.innerHTML != null) {
                      scriptContent = ccNode.innerHTML;
                   } else if(ccNode.text != undefined && ccNode.text != null) {
                      scriptContent = ccNode.text;
                   }
                   if(scriptContent != null && scriptContent != undefined && scriptContent != "") {
                     scriptContent = scriptContent.replace(arrayIndexRegex,"["+traverseCtr+"]");
                     scriptContent = scriptContent.replace(functionArrayIndexRegex,"_"+traverseCtr+"_");
                      var s = document.createElement("script");
                      if(s.innerHTML != null && s.innerHTML != undefined)
                         s.innerHTML = scriptContent;
                      var p = ccNode.parentNode;
                      p.appendChild(s);
                      p.removeChild(ccNode);
                   }
                }
	     }
          }
	  child_Nodes = currentTag.childNodes;
	  for(var ctr=0; ctr < child_Nodes.length; ctr++) {
	     ccNode = child_Nodes[ctr];
             if(ccNode.tagName == "INPUT" || ccNode.tagName == "input") {
		refreshTag(ccNode,traverseCtr);
	     } else if(ccNode.tagName == "SELECT" || ccNode.tagName == "select") {
                refreshTag(ccNode,traverseCtr);
             } else if(ccNode.tagName == "TEXTAREA" || ccNode.tagName == "textarea") {
                refreshTag(ccNode,traverseCtr);
             } else if(ccNode.tagName == "A" || ccNode.tagName == "a") {
                refreshAnchorTag(ccNode,traverseCtr);
	     } else if(ccNode.tagName == "SPAN" || ccNode.tagName == "span") {
		refreshTag(ccNode,traverseCtr);
             } else if(ccNode.tagName == "script" || ccNode.tagName == "SCRIPT") {
		
                scriptContent="";
                if(ccNode.innerHTML != undefined && ccNode.innerHTML != null) {
                   scriptContent = ccNode.innerHTML;
                } else if(ccNode.text != undefined && ccNode.text != null) {
                   scriptContent = ccNode.text;
                }
                if(scriptContent != null && scriptContent != undefined && scriptContent != "") {
                   scriptContent = scriptContent.replace(arrayIndexRegex,"["+traverseCtr+"]");
                   scriptContent = scriptContent.replace(functionArrayIndexRegex,"_"+traverseCtr+"_");
                      s = document.createElement("script");
                      if(s.innerHTML != null && s.innerHTML != undefined)
                         s.innerHTML = scriptContent;
		      var p = ccNode.parentNode;
		      p.appendChild(s);
		      p.removeChild(ccNode);
                }
	   }
        }
      }
    }
  }

}

function refreshDivIds(tag,ctr) {
    if(tag.getAttribute("id") != undefined && tag.getAttribute("id") != null )
    {

        tag.setAttribute("id",tag.getAttribute("id").replace(functionArrayIndexRegex,"_"+ctr+"_"));
    }
    else
    {
        tag.setAttribute("display",'');
    }
}


function refreshTag(tag,ctr) {
    // Changing the indices in outerHTML for IE
    if(tag.getAttribute("name") != undefined &&  tag.getAttribute("name") != null ){
        tag.setAttribute("name",tag.getAttribute("name").replace(arrayIndexRegex,"["+ctr+"]"));
        tag.setAttribute("name",tag.getAttribute("name").replace(functionArrayIndexRegex,"_"+ctr+"_"));

    }
    if(tag.getAttribute("id") != undefined &&  tag.getAttribute("id") != null ){
        tag.setAttribute("id",tag.getAttribute("id").replace(arrayIndexRegex,"["+ctr+"]"));
        tag.setAttribute("id",tag.getAttribute("id").replace(functionArrayIndexRegex,"_"+ctr+"_"));
   }
    var onchangeValue="";
    var onclickValue="";
    var onfocusValue="";
    var onblurValue="";

    // Replacing the indices in outerHTML removes events value(onclick, onchange ...)
    // so, we are storing the value of event attributes to a variable before replacing the
    // outerHTML

    if(tag.getAttribute("onchange") != undefined &&  tag.getAttribute("onchange") != null )
        onchangeValue = tag.getAttribute("onchange");
    if(tag.getAttribute("onclick") != undefined &&  tag.getAttribute("onclick") != null ) {

        onclickValue = tag.getAttribute("onclick");
    }
    if(tag.getAttribute("onfocus") != undefined &&  tag.getAttribute("onfocus") != null )
        onfocusValue = tag.getAttribute("onfocus");
    if(tag.getAttribute("onblur") != undefined &&  tag.getAttribute("onblur") != null ){
        onblurValue = tag.getAttribute("onblur");
    }
    if(onchangeValue !=null && onchangeValue != undefined && onchangeValue != '')
    {
         if(typeof onchangeValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onchangeValue = onchangeValue.toString();
             if(onchangeValue.indexOf("{") >= 0 && onchangeValue.lastIndexOf("}") >=0)
             {
                onchangeValue = onchangeValue.substring(onchangeValue.indexOf("{")+1,onchangeValue.lastIndexOf("}"));
             }
             onchangeValue = onchangeValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             var myObject=new Function(onchangeValue);
             tag.setAttribute("onchange",myObject);
        }
        else if(typeof onchangeValue == 'string')
        {
           //If the browser returns string object
           onchangeValue = onchangeValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onchangeValue = onchangeValue.replace(arrayIndexRegex,"["+ctr+"]");
           tag.setAttribute("onchange",onchangeValue);
        }
        else
          alert("This functionality is not supported in this browser");
     }
    if(onclickValue !=null && onclickValue != undefined && onclickValue != '')
    {
         if(typeof onclickValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onclickValue = onclickValue.toString();
             if(onclickValue.indexOf("{") >= 0 && onclickValue.lastIndexOf("}") >=0)
             {
                onclickValue = onclickValue.substring(onclickValue.indexOf("{")+1,onclickValue.lastIndexOf("}"));
             }
             onclickValue = onclickValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             var myObject=new Function(onclickValue);
             tag.setAttribute("onclick",myObject);
         }
         else if(typeof onclickValue == 'string')
         {
	   onclickValue = onclickValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
	   onclickValue = onclickValue.replace(arrayIndexRegex,"["+ctr+"]");
           //If the browser returns string object
           tag.setAttribute("onclick",onclickValue);
         }
         else
            alert("This functionality is not supported in this browser");
    }

    if(onfocusValue !=null && onfocusValue != undefined && onfocusValue != '')
    {
         if(typeof onfocusValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onfocusValue = onfocusValue.toString();
             if(onfocusValue.indexOf("{") >= 0 && onfocusValue.lastIndexOf("}") >=0)
             {
                onfocusValue = onfocusValue.substring(onfocusValue.indexOf("{")+1,onfocusValue.lastIndexOf("}"));
             }
             onfocusValue = onfocusValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             var myObject=new Function(onfocusValue);
             tag.setAttribute("onfocus",myObject);
         }
         else if(typeof onfocusValue == 'string')
         {
           onfocusValue = onfocusValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onfocusValue = onfocusValue.replace(arrayIndexRegex,"["+ctr+"]");
           tag.setAttribute("onfocus",onfocusValue);
        }
        else
           alert("This functionality is not supported in this browser");
    }
if(onblurValue !=null && onblurValue != undefined && onblurValue != '')
    {
         if(typeof onblurValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onblurValue = onblurValue.toString();
             if(onblurValue.indexOf("{") >= 0 && onblurValue.lastIndexOf("}") >=0)
             {
                onblurValue = onblurValue.substring(onblurValue.indexOf("{")+1,onblurValue.lastIndexOf("}"));
             }
             onblurValue = onblurValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             var myObject=new Function(onblurValue);
             tag.setAttribute("onblur",myObject);
         }
         else if(typeof onblurValue == 'string')
         {
           onblurValue = onblurValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onblurValue = onblurValue.replace(arrayIndexRegex,"["+ctr+"]");
           tag.setAttribute("onblur",onblurValue);
        }
        else
           alert("This functionality is not supported in this browser");
    }
}

function refreshAnchorTag(anchorTag,ctr) {
    if(anchorTag.getAttribute("name") != undefined &&  anchorTag.getAttribute("name") != null ){
     anchorTag.setAttribute("name",anchorTag.getAttribute("name").replace(arrayIndexRegex,"["+ctr+"]"));
     anchorTag.setAttribute("name",anchorTag.getAttribute("name").replace(functionArrayIndexRegex,"_"+ctr+"_"));
    }
    if(anchorTag.getAttribute("id") != undefined &&  anchorTag.getAttribute("id") != null ){
     anchorTag.setAttribute("id",anchorTag.getAttribute("id").replace(arrayIndexRegex,"["+ctr+"]"));
     anchorTag.setAttribute("id",anchorTag.getAttribute("id").replace(functionArrayIndexRegex,"_"+ctr+"_"));
    }
    if(anchorTag.getAttribute("id") != undefined &&  anchorTag.getAttribute("id") != null && anchorTag.getAttribute("id") == 'delete')
    {
        anchorTag.setAttribute("href",anchorTag.getAttribute("href").replace(hrefArrayIndexRegex,ctr));
        anchorTag.setAttribute("href",anchorTag.getAttribute("href").replace(arrayIndexRegex,"["+ctr+"]"));
        anchorTag.setAttribute("href",anchorTag.getAttribute("href").replace(functionArrayIndexRegex,"_"+ctr+"_"));
    }

    var onmouseoverValue="";
    var onmouseoutValue="";
    var onclickValue="";
    if(anchorTag.getAttribute("onmouseover") != undefined &&  anchorTag.getAttribute("onmouseover") != null )
        onmouseoverValue = anchorTag.getAttribute("onmouseover");
    if(anchorTag.getAttribute("onmouseout") != undefined &&  anchorTag.getAttribute("onmouseout") != null )
        onmouseoutValue = anchorTag.getAttribute("onmouseout");
    if(anchorTag.getAttribute("onclick") != undefined &&  anchorTag.getAttribute("onclick") != null )
        onclickValue = anchorTag.getAttribute("onclick");
    //For IE we need to replace outerHTML in order to replace the indices of name attribute
    if(onmouseoverValue !=null && onmouseoverValue != undefined && onmouseoverValue != '')
    {
         if(typeof onmouseoverValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onmouseoverValue = onmouseoverValue.toString();
             if(onmouseoverValue.indexOf("{") >= 0 && onmouseoverValue.lastIndexOf("}") >=0)
             {
                onmouseoverValue = onmouseoverValue.substring(onmouseoverValue.indexOf("{")+1,onmouseoverValue.lastIndexOf("}"));
             }
             onmouseoverValue = onmouseoverValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             onmouseoverValue = onmouseoverValue.replace(arrayIndexRegex,"["+ctr+"]");
             var myObject=new Function(onmouseoverValue);
             anchorTag.setAttribute("onmouseover",myObject);
        }
        else if(typeof onmouseoverValue == 'string')
        {
           //If the browser returns string object
             onmouseoverValue = onmouseoverValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             onmouseoverValue = onmouseoverValue.replace(arrayIndexRegex,"["+ctr+"]");
             anchorTag.setAttribute("onmouseover",onmouseoverValue);
        }
        else
          alert("This functionality is not supported in this browser");
     }
    if(onmouseoutValue !=null && onmouseoutValue != undefined && onmouseoutValue != '')
    {
         if(typeof onmouseoutValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onmouseoutValue = onmouseoutValue.toString();
             if(onmouseoutValue.indexOf("{") >= 0 && onmouseoutValue.lastIndexOf("}") >=0)
             {
                onmouseoutValue = onmouseoutValue.substring(onmouseoutValue.indexOf("{")+1,onmouseoutValue.lastIndexOf("}"));
             }
             onmouseoutValue = onmouseoutValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             onmouseoutValue = onmouseoutValue.replace(arrayIndexRegex,"["+ctr+"]");
             var myObject=new Function(onmouseoutValue);
             anchorTag.setAttribute("onmouseout",myObject);
        }
        else if(typeof onmouseoutValue == 'string')
        {
           //If the browser returns string object
           onmouseoutValue = onmouseoutValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onmouseoutValue = onmouseoutValue.replace(arrayIndexRegex,"["+ctr+"]");
           anchorTag.setAttribute("onmouseout",onmouseoutValue);
        }
        else
          alert("This functionality is not supported in this browser");
     }

    if(onclickValue !=null && onclickValue != undefined && onclickValue != '')
    {
         if(typeof onclickValue == 'function')
         {
             //IE returns function object when we get the Attribute value of events(onchange,onclick ...)
             //the following code will extract the content of the function and creates new function
             //object and assigns it to the event.

             onclickValue = onclickValue.toString();
             if(onclickValue.indexOf("{") >= 0 && onclickValue.lastIndexOf("}") >=0)
             {
                onclickValue = onclickValue.substring(onclickValue.indexOf("{")+1,onclickValue.lastIndexOf("}"));
             }
             onclickValue = onclickValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
             var myObject=new Function(onclickValue);
             anchorTag.setAttribute("onclick",myObject);
         }
         else if(typeof onclickValue == 'string')
         {
           onclickValue = onclickValue.replace(functionArrayIndexRegex,"_"+ctr+"_");
           onclickValue = onclickValue.replace(arrayIndexRegex,"["+ctr+"]");
           //If the browser returns string object
           anchorTag.setAttribute("onclick",onclickValue);
         }
         else
            alert("This functionality is not supported in this browser");
    }

}

