//Preload the images
var ctr=0;
var x_ImgArray = new Array();
  x_ImgArray[ctr++] = '/shared/images/xTree_minus.gif';
  x_ImgArray[ctr++] = '/shared/images/xTree_minus_folder.gif';
  x_ImgArray[ctr++] = '/shared/images/xTree_plus.gif';
  x_ImgArray[ctr++] = '/shared/images/xTree_plus_folder.gif';
  x_ImgArray[ctr++] = '/shared/images/folder_spacer.gif';
var preLoadArray = new Array();
for (i = 0; i < x_ImgArray.length; i++) {
  preLoadArray[i] = new Image();
  preLoadArray[i].src = x_ImgArray[i];
}

//Determine the users browser 
isDOM=(document.getElementById)?true:false
isOpera=(window.opera)?true:false
isOpera5=isOpera && isDOM
isOpera6=(isOpera5 && window.print)?true:false
isMSIE=isIE=(document.all && document.all.item && !isOpera)?true:false
isNS4=(document.layers)?true:false
isNS6=isMozilla=isDOM && navigator.appName=="Netscape"
isNS=isNS4 || isNS6

function expand(node) {
  eval("window.document." + formName + ".xCoordinate.value=getScrollX();");
  eval("window.document." + formName + ".yCoordinate.value=getScrollY();");
  eval("window.document." + formName + ".expand.value=node;");
  eval("window.document." + formName + ".collapse.value='';");
  eval("window.document." + formName + ".submit();");
}

function collapse(node) {
  eval("window.document." + formName + ".xCoordinate.value=getScrollX();");
  eval("window.document." + formName + ".yCoordinate.value=getScrollY();");
  eval("window.document." + formName + ".expand.value='';");
  eval("window.document." + formName + ".collapse.value=node;");
  eval("window.document." + formName + ".submit();");
}

function displayDocs(node,prodCd) {
  eval("window.document." + formName + ".xCoordinate.value=getScrollX();");
  eval("window.document." + formName + ".yCoordinate.value=getScrollY();");
  eval("window.document." + formName + ".expand.value='';");
  eval("window.document." + formName + ".collapse.value='';");
  eval("window.document." + formName + ".displayDocsNodeId.value=node;");
  eval("window.document." + formName + ".displayDocsProdCd.value=prodCd;");
  eval("window.document." + formName + ".submit();");
}

function setMouseCoordinates() {
  eval("window.document." + formName + ".yCoordinateMouse.value=window.event.y;");
}

function completeRegistration() {
  eval("window.document." + formName + ".registrationComplete.value='true';");
  eval("window.document." + formName + ".submit();");
}

function getScrollX() {
  if(isMSIE) return(self.document.body.scrollLeft)
  if(isNS) return(self.pageXOffset)
}

function getScrollY() {
  if(isMSIE) return(self.document.body.scrollTop)
  if(isNS) return(self.pageYOffset)
}

function resetForm() {
  //eval("window.document." + formName + ".reset();");
  //eval("window.document." + formName + ".submit();");
  eval("window.document." + formName + ".xCoordinate.value=getScrollX();");
  eval("window.document." + formName + ".yCoordinate.value=getScrollY();");
  eval("window.document." + formName + ".expand.value='';");
  eval("window.document." + formName + ".collapse.value='';");
  eval("window.document." + formName + ".submit();");


}

function expandProdTree(value) {
  eval("window.document." + formName + ".xCoordinate.value=getScrollX();");
  eval("window.document." + formName + ".yCoordinate.value=getScrollY();");
  eval("window.document." + formName + ".displayAllProducts.value=value;");
  eval("window.document." + formName + ".submit();");
}

function updateSelectedNode(value, prodCd) {
  eval("window.document." + formName + ".displayDocsNodeId.value=value;");
  eval("window.document." + formName + ".displayDocsProdCd.value=prodCd;");
}

function expandAll() {
  expand('all');
}

function collapseAll() {
  collapse('all');
}

//-- Autopopulate Checkboxes START --
var isParentUnchecked = new String("false");

function validateCheckbox(interestCheckbox) {
 //--From SAP2, interestCheckbox contains categoryId and not nodeId, hence the nodeId variable used here points to categoryId 
  var nodeId = new String(interestCheckbox.value);
  var interestsLength = this.document.InterestsForm.Interest.length;
  var nodeDelimiterCount = getDelimiterCount(nodeId);
  var tmpDelimiterCount;
  isParentUnchecked = new String("false");

  for(i=0; i < interestsLength; i++) {
    var sub = new String(this.document.InterestsForm.Interest[i].value.substr(0,nodeId.length));
    if(nodeId.match(sub)) {
      if(interestCheckbox.checked) {
        interestCheckbox.checked = true;
		
		//WR 2767  modified by r9187z Start
		if(document.getElementById("deletedNodes") !=null) {
			var node = document.getElementById("deletedNodes").value;
			var nodes = node.split(",");
			
			this.document.InterestsForm.deletedNodes.value = 'delete';
			for(var k=1; k<nodes.length; k++){
				if (nodes[k] != interestCheckbox.value) {					
					this.document.InterestsForm.deletedNodes.value = this.document.InterestsForm.deletedNodes.value+','+ nodes[k];					
				}
			}			
		}
		//WR 2767  modified by r9187z End
		
        tmpDelimiterCount = getDelimiterCount(sub);
        
        //FINISH HERE!!!!!!!!!!
        if(nodeDelimiterCount == (tmpDelimiterCount)) {
          this.document.InterestsForm.Interest[i].checked = true;
			//WR 2767  modified by r9187z Start
			if(document.getElementById("deletedNodes") !=null) {
				var node = document.getElementById("deletedNodes").value;
				var nodes = node.split(",");
				this.document.InterestsForm.deletedNodes.value = 'delete';
				for(var k=1; k<nodes.length; k++){
					if (nodes[k] != this.document.InterestsForm.Interest[i].value) {						
						this.document.InterestsForm.deletedNodes.value = this.document.InterestsForm.deletedNodes.value+','+ nodes[k];						
					}
				}				
			}
			//WR 2767  modified by r9187z End
        }
      } else {
        if(isParentUnchecked.match("false")) {
          unCheckParent(nodeId,interestsLength);
         }
        interestCheckbox.checked = false;
		
		//WR 2767  modified by r9187z Start
		if(document.getElementById("deletedNodes") !=null) {
			this.document.InterestsForm.deletedNodes.value = this.document.InterestsForm.deletedNodes.value+','+interestCheckbox.value				
		}
		//WR 2767  modified by r9187z End

        this.document.InterestsForm.Interest[i].checked = false;
		
		//WR 2767  modified by r9187z Start
		if(document.getElementById("deletedNodes") !=null) {
			this.document.InterestsForm.deletedNodes.value = this.document.InterestsForm.deletedNodes.value+','+this.document.InterestsForm.Interest[i].value				
		}
		//WR 2767  modified by r9187z End
      }
    }
  }
}

function unCheckParent(nodeId,interestsLength) {
  var lastDecimalIndex = nodeId.lastIndexOf(".");

  for(j=0; j < interestsLength; j++) {
    var parent = new String(this.document.InterestsForm.Interest[j].value);
    if(nodeId.substr(0,lastDecimalIndex).match(parent)) {
      this.document.InterestsForm.Interest[j].checked = false;
	  //WR 2767  modified by r9187z Start
	  if(document.getElementById("deletedNodes") !=null) {
			this.document.InterestsForm.deletedNodes.value = this.document.InterestsForm.deletedNodes.value+','+this.document.InterestsForm.Interest[j].value
	  }
	  //WR 2767  modified by r9187z End	
    }
  }
  isParentUnchecked = new String("true");
  return;
}


function getDelimiterCount(delimitedNodeId) {
  var delimiterCount = 0;
  for(k=0; k < delimitedNodeId.length; k++) {
    if(delimitedNodeId.charAt(k) == '.') {
      delimiterCount++;
    }
  }

  //alert("delimitedNodeId=[" + delimitedNodeId + "]  delimiterCount=[" + delimiterCount + "]");
  return(delimiterCount);
}

//-- Autopopulate Checkboxes END --

//-- pre populate checkboxes on initial page load
function setDefaultPageView(x,y) {
  window.scrollTo(x,y);

  var interestsLength = this.document.InterestsForm.Interest.length;
  for(i=0; i < interestsLength; i++) {
    var nodeId = this.document.InterestsForm.Interest[i].value;
    var nodeIdLength = nodeId.length;
    if(this.document.InterestsForm.Interest[i].checked == true) {
      for(j=0; j < interestsLength; j++) {
        var tmpId = this.document.InterestsForm.Interest[j].value;
        var tmpIdLength = tmpId.length;

        if(tmpIdLength > nodeIdLength) {
          var sub = new String(tmpId.substr(0,nodeIdLength));
          if(sub.match(nodeId)) {
            this.document.InterestsForm.Interest[j].checked = true;
          }
        }
      }
    }
  }
}



function completeRegistration_withimagehide() {
	document.getElementById('completeRegistrationButton').style.visibility = 'hidden';
	completeRegistration();
}