

var userControlPrefix = "ucTechnicalArea_";
var TRUE = "true";

function HelloWorld()
{
	alert("hi");
	var chkid = event.srcElement.id;
	alert(chkid);
	var chkbox = document.getElementById(chkid);
	var techArea= chkbox.getAttribute("TechnicalArea");
	var subArea = chkbox.getAttribute("SubTechnicalArea");
	var hiddenctrl = chkbox.getAttribute("HiddenVar");
	alert(techArea);
	alert(subArea);
	alert(hiddenctrl);
}

function FlagTechnicalAreaChange()
{
	var hdnChangeByUser;
	hdnChangeByUser = document.getElementById("hdnChangeByUser");
	if(hdnChangeByUser)
	{
		hdnChangeByUser.value = "Y";
	}
	/*alert(document.clickedNodeIndex);
	if(this.clickedNodeIndex != null)
	 {
		this.queueEvent("oncheck", this.clickedNodeIndex);
	}*/	
}

function CheckTech()
{
	var element = document.getElementById('UCQuickRegistrationFirstPane_chkAddMe1');
	var element1 = document.getElementById('ucTechnicalArea_chkAddMe');
	if(element != null && element1 != null)
	{
	if(element1.checked)
	{
		element.checked = true;
	}
	else
	{
		element.checked = false;
	}
	}
}

//void function UpdateTechnicalAreaItem(technicalArea,technicalItem,hdnMain)
function UpdateTechnicalAreaItem(currentCheckBox)
{
		var isNN = false;
		var isIE = false;
		if (document.implementation && 
			document.implementation.createDocument) 
		{
			isNN = true;
		}
		else (window.ActiveXObject)
		{
			isIE = true;
		}
		//var technicalArea;
		//var technicalItem;
		//var hdnMain;
		var oDom
		var isSuccess;
		var oNodes;
		var rowcounter;
		var technicalAreaName;
		var oChildNodes;
		var colCounter;
		var chkBoxId;				
		//var hiddenControl = document.getElementById(userControlPrefix + hdnMain);
		var isChecked = currentCheckBox.checked;		
		//alert (isChecked);
		var hdChangeByUserControlName;
		var hdChangeByUser;
		var chkbox = currentCheckBox;
		var technicalArea= chkbox.getAttribute("TechnicalArea");
		//alert(technicalArea);
		var technicalItem = chkbox.getAttribute("SubTechnicalArea");
		//alert(technicalItem);
		var hdnMain = chkbox.getAttribute("HiddenVar");
		//alert(hdnMain);
		var hiddenControl = document.getElementById(userControlPrefix + hdnMain);
		
		//Any change by user is stored in hidden variable
		hdChangeByUserControlName = userControlPrefix + "hdChangeByUser";
		hdChangeByUser = document.getElementById(hdChangeByUserControlName);
		hdChangeByUser.value = "Y";
		FlagTechnicalAreaChange();
				
		//The xml is loaded from hidden variable
		//determine if this is a standards-compliant browser like Mozilla
		if (isNN) {

			//create the DOM Document the standards way
			//oDom = document.implementation.createDocument("","", null);
			//oDom.async = false;
					
			//create a DOMParser
			var objDOMParser = new DOMParser();
			        
			//create new document from string
			oDom = objDOMParser.parseFromString(hiddenControl.value, "text/xml");

			isSuccess = true;
			//alert(isSuccess);

		} else if (isIE) {

			//create the DOM Document the IE way
			oDom = new ActiveXObject("MSXML2.DOMDocument");
			oDom.async = false;
			isSuccess = oDom.loadXML(hiddenControl.value);		
			//alert(isSuccess);
		}

		//oDom = new ActiveXObject("MSXML2.DOMDocument");	
		//oDom.async = false;
		//document.write(hiddenControl.value);
		//alert(hiddenControl.value);
		//isSuccess = oDom.loadXML(hiddenControl.value);		
		if (isSuccess==true)
		{	
			//The xml is read node by node		
			if (isIE)
			{
				oNodes = oDom.documentElement.childNodes;
			}
			else if (isNN)
			{
				oNodes = oDom.childNodes;
			}
			//alert(oNodes.length);
			for (rowcounter = 0; rowcounter < oNodes.length; ++rowcounter)
			{	
				//alert("inside rows");				
				technicalAreaName = oNodes[rowcounter].attributes[1].nodeValue;
				//alert(technicalAreaName);
				//alert(technicalArea);
				//For the node corresponding to technical area selected by user the child nodes are updated
				if(technicalAreaName == technicalArea)
				{					
					oChildNodes = oNodes.item(rowcounter).childNodes;
					for (colCounter = 0; colCounter < oChildNodes.length; ++colCounter)
					{	
						//alert(technicalItem);
						//alert(oChildNodes(colCounter).attributes(1).nodeValue);	
						if(oChildNodes[colCounter].attributes[1].nodeValue == technicalItem)
						{
							var chkboxIdInCode = "chk" + oChildNodes[colCounter].attributes[1].nodeValue;
							//alert(chkboxIdInCode);
							chkBoxId = document.getElementById(chkboxIdInCode);								
							//alert(chkBoxId.id);
							//alert(isChecked);
							//The child node is updated and the corresponding checkbox is checked or unchecked
							//alert(isChecked);
							if(isChecked == true)
							{
								chkBoxId.checked = true;
								oChildNodes[colCounter].attributes[2].nodeValue = "Y";
							}
							else
							{
								chkBoxId.checked = false;
								oChildNodes[colCounter].attributes[2].nodeValue = "N";
							}
							UpdateMainNode(oNodes,rowcounter);							
							break;
						}//end of if child node						
					}//end for colcounter					
					break;
				}//end if technical area name comparison
				
			}//end row counter
			
			//A new xml string is created to reflect the changes
			CreateUpdatedXMLString(oDom,hiddenControl);			
		}
		else
		{
			alert("Error in loading technical area of interest");
		}
	}
	
	function UpdateMainNode(oNodes,rowcounter)
	{
		var oChildNodes;
		var colCounter;
		var isPresent;
		
		isPresent = false;
		//Get all the child nodes
		oChildNodes = oNodes.item(rowcounter).childNodes;
		
		//The child nodes are traversed node by node and all the values for child nodes 
		// changed as per user's choice
		for (colCounter = 0; colCounter < oChildNodes.length; ++colCounter)
		{		
			if(oChildNodes[colCounter].attributes[2].nodeValue == "Y")
			{
				isPresent =  true;
			}	
			
		}
		if(isPresent)
		{
			oNodes[rowcounter].attributes[2].nodeValue = "Y";
		}
		else
		{
			oNodes[rowcounter].attributes[2].nodeValue = "N";
		}				
	}
	
	function CreateUpdatedXMLString(oDom,hiddenControl)
	{
		//alert("in create updated xml");
		var xmlString="";
		var QUOTE = "'";
		var rowcounter;
		var oNodes;
		var oChildNodes;
		var colCounter;
		var hdTechnicalArea
		
		//The start of XML is build
		xmlString += "<?xml version=" +  QUOTE + "1.0" + QUOTE + "?>";		
		xmlString += "<TechnicalAreaData>";				
		oNodes = oDom.documentElement.childNodes;
		//The xml document is read node by node and the values are read into a new xml
			for (rowcounter = 0; rowcounter < oNodes.length; ++rowcounter)
			{	
				var attributeName;
				var attributeValue;
				attributeName = oNodes[rowcounter].attributes[0].nodeValue;
				attributeID = oNodes[rowcounter].attributes[1].nodeValue;
				attributeValue = oNodes[rowcounter].attributes[2].nodeValue;
				xmlString += "<TechnicalArea Name=" + QUOTE + attributeName + QUOTE + " ID=" + QUOTE + attributeID + QUOTE + " Value=" + QUOTE + attributeValue + QUOTE + ">";				
				oChildNodes = oNodes.item(rowcounter).childNodes;
				for (colCounter = 0; colCounter < oChildNodes.length; ++colCounter)
				{	
					var itemName;
					var itemValue;
					var itemId;
					itemName = oChildNodes[colCounter].attributes[0].nodeValue;
					itemId = oChildNodes[colCounter].attributes[1].nodeValue;
					itemValue = oChildNodes[colCounter].attributes[2].nodeValue;
					
					xmlString += "<Item Name=" + QUOTE + itemName + QUOTE + " Id=" + QUOTE + itemId + QUOTE + " Value=" + QUOTE + itemValue + QUOTE + "/>";					
				}
				xmlString += "</TechnicalArea>";				
			}
			xmlString += "</TechnicalAreaData>";
			
			//The XML is reassigned to the hidden variable so that it can be read from the code behind
			hiddenControl.value = "";
			//alert(hiddenControl.id);
			//alert(xmlString);
			hiddenControl.value = xmlString;			
	}
