//*****************************************************************************
//                  Copyright (c) 2007-2008, Lexmark International, Inc.           
//     THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY     
//        INFORMATION WHICH IS THE PROPERTY OF LEXMARK INTERNATIONAL, INC.     
//                                                                             
//   ANY DISCLOSURE, USE, OR REPRODUCTION, WITHOUT WRITTEN AUTHORIZATION FROM  
//              LEXMARK INTERNATIONAL, INC., IS STRICTLY PROHIBITED.           
//*****************************************************************************

//-----------------------------------------------------------------------------
// This file contains the generic handlers for controls.
//-----------------------------------------------------------------------------

// Global variable used to store the current screen ID.
var screenID;

//-----------------------------------------------------------------------------
// doAction
//   Method to invoke the action triggered by a button click.
//   Following are special button clicks which will open up another webpage:
//   - Clicking on ViewLog button will open up another activity log webpage.
//   - Clicking on CustomerSupport button will open up customer support webpage.
// Params:
//   actionId    The ID of the current action to perform.
//   logTextID   String ID to use when logging a conversation.
//-----------------------------------------------------------------------------
function doAction(actionID, logTextID)
{
	var frame = window.contentframe;
    screenID = frame.document.body.id;
	
	var actionResult = commonJS.Action(screenID, actionID);
}

function doActionAsync(actionID, logTextID)
{
    var frame = window.contentframe;
    screenID = frame.document.body.id;
	
	var actionResult = commonJS.ActionAsync(screenID, actionID); 
}

function RenderEmptySlot(subTableBody)
{
	//Add heading row and col
	var headingRow = contentframe.document.createElement("tr");
	var imageCell = contentframe.document.createElement("td");
	var headingCell = contentframe.document.createElement("td");

	headingCell.setAttribute("width", "100%");
	headingCell.setAttribute("height", "50%");
	headingCell.setAttribute("align", "left");
	
	
	headingRow.appendChild(headingCell);
	
	var descriptionRow = contentframe.document.createElement("tr");
	var descCell = contentframe.document.createElement("td");
	var dummyCell = contentframe.document.createElement("td");
	
	descCell.setAttribute("width", "100%");
	descCell.setAttribute("height", "50%");
	descCell.setAttribute("align", "align");
	descCell.setAttribute("id", "desc");
	
	dummyCell.setAttribute("id", "dummy");
	
	descriptionRow.appendChild(dummyCell);
	descriptionRow.appendChild(descCell);

	//var blank = "\& nbsp;\& nbsp;\& nbsp;";
	var blank = '\u00A0';
	var txtNode = contentframe.document.createTextNode(blank);
	headingCell.appendChild(txtNode);

	var desTxtNode = contentframe.document.createTextNode(blank);
	descCell.appendChild(desTxtNode);
	
	subTableBody.appendChild(headingRow);
	subTableBody.appendChild(descriptionRow);
}

function RenderControlsOnRowCol(controls, index, descriptionRow)
{
	if(index >= controls.length)
		return;
	else 
	{
		var imageCell = descriptionRow.childNodes[0];
		var descCell = descriptionRow.childNodes[1];
		var lastCell = descriptionRow.childNodes[2];

		switch(controls[index].tagName)
		{
			case "Text":
				var textNode = controls[index];
				if(textNode != null)
				{
					var localizedText = stringTableMap[textNode.text];
					var txtNode = contentframe.document.createTextNode(localizedText);
					descCell.appendChild(txtNode);
				}

				break;

			case "Macro":
				var macroNode = controls[index];
				if(macroNode != null)
				{
					var diagText = commonJS.GetDiagnosisInfo(macroNode.text);
					var txtNode = contentframe.document.createTextNode(diagText);
					descCell.appendChild(txtNode);
				}

				break;

			case "Space":
				var blank = '\u00A0';
				blank = blank;
				var txtNode = contentframe.document.createTextNode(blank);
				descCell.appendChild(txtNode);
				

				break;

			case "Break":
				var breakNode = contentframe.document.createElement('<br/>');
				descCell.appendChild(breakNode);

				break;

			case "Hyper":
				var hyperNode = controls[index];
				if(hyperNode != null)
				{
					var actionId = hyperNode.getAttribute('id');
					var hyperTextNode = hyperNode.getElementsByTagName('Text')[0];

					var linkNode = contentframe.document.createElement('a');
					linkNode.onclick =  new Function ("onLinkClick('"+actionId+"')");
					linkNode.setAttribute('href', 'javascript:void(0)');
					var localizedText = stringTableMap[hyperTextNode.text];

					var txtNode = contentframe.document.createTextNode(localizedText);
					
					linkNode.appendChild(txtNode);
					descCell.appendChild(linkNode);
				}

				break;

			case "HyperIcon":
				var hyperNode = controls[index];
				if(hyperNode != null)
				{
					var actionId = hyperNode.getAttribute('id');
					var hyperImageNode = hyperNode.getElementsByTagName('Image')[0];
					var iconName = hyperImageNode.text;

					var image = contentframe.document.createElement("img");
					iconName = 'images/'+ iconName + '.gif';
					image.setAttribute('src', iconName);
					image.setAttribute("width", '30');
					image.setAttribute("height", '30');

					var linkNode = contentframe.document.createElement('a');
					linkNode.onclick =  new Function ("onLinkClick('"+actionId+"')");
					linkNode.setAttribute('href', 'javascript:void(0)');

					linkNode.appendChild(image);

					imageCell.appendChild(linkNode);
				}

				break;

			case "Icon":
				var icon = controls[index];
				if(icon != null)
				{
					var iconName = icon.text;
					
					if(iconName != '')
					{
						var image = contentframe.document.createElement("img");
						iconName = 'images/'+ iconName + '.gif';
						image.setAttribute('src', iconName);
						image.setAttribute("width", '30');
						image.setAttribute("height", '30');
						imageCell.appendChild(image);
					}
				}

				break;

			case "Button":
				var buttonNode = controls[index];
				if(buttonNode != null)
				{
					var actionId = buttonNode.getAttribute('id');
					var buttonTextNode = buttonNode.getElementsByTagName('Text')[0];

					var buttonControl = contentframe.document.createElement('button');
					buttonControl.onclick =  new Function ("onLinkClick('"+actionId+"')");
					buttonControl.setAttribute('href', 'javascript:void(0)');
					var localizedText = stringTableMap[buttonTextNode.text];
					var txtNode = contentframe.document.createTextNode(localizedText);
					buttonControl.appendChild(txtNode);

					lastCell.appendChild(buttonControl);
				}

				break;
		}
		RenderControlsOnRowCol(controls, index+1, descriptionRow);
	}
}

function RenderControlsOnSubTable(subTableBody, sectionObj)
{
	
	var contentframe = window.contentframe;
	if(sectionObj == null)
	{
		RenderEmptySlot(subTableBody);
		return;
	}

	var controlListObj = sectionObj.getElementsByTagName('ControlList');
	

	//process heading node
	var headingNode = sectionObj.getElementsByTagName('Heading')[0];
	if(headingNode != null)
	{
		var headingRow = contentframe.document.createElement("tr");
		var imageCell = contentframe.document.createElement("td");
		var headingCell = contentframe.document.createElement("td");
		var headingLastCell = contentframe.document.createElement("td");

		headingCell.setAttribute("width", "80%");
		headingCell.setAttribute("height", "50%");
		headingCell.setAttribute("align", "left");
		headingCell.style.fontSize = 14;
		headingCell.style.fontWeight = 'bold';
		
		headingLastCell.setAttribute("width", "10%");
		headingLastCell.setAttribute("height", "50%");
		headingLastCell.setAttribute("align", "left");

		imageCell.setAttribute("width", "10%");
		imageCell.setAttribute("height", "50%");
		imageCell.setAttribute("align", "left");

		headingRow.appendChild(imageCell);
		headingRow.appendChild(headingCell);
		headingRow.appendChild(headingLastCell);

		subTableBody.appendChild(headingRow);
		RenderControlsOnRowCol(headingNode.childNodes, 0, headingRow);
	}


	if(controlListObj != null && controlListObj[0] != null)
	{
		var descriptionRow = contentframe.document.createElement("tr");
		var descCell = contentframe.document.createElement("td");
		var dummyCell = contentframe.document.createElement("td");
		var lastCell = contentframe.document.createElement("td");
		
		descCell.setAttribute("height", "50%");
		descCell.setAttribute("align", "left");
		descCell.style.fontSize = 14;
		
		descriptionRow.appendChild(dummyCell);
		descriptionRow.appendChild(descCell);
		descriptionRow.appendChild(lastCell);
		
		subTableBody.appendChild(descriptionRow);
		RenderControlsOnRowCol(controlListObj[0].childNodes, 0, descriptionRow);
	}
}

function RenderSubTables(mainTableCol, sectionObj)
{
	var subTable = window.contentframe.document.createElement("table");
	subTable.setAttribute("width", "100%");
	subTable.setAttribute("height", "25%");
	
	var subTableBody = window.contentframe.document.createElement("tbody");
	
	subTable.appendChild(subTableBody);
	mainTableCol.appendChild(subTable);

	//subTable.border = "2";

	RenderControlsOnSubTable(subTableBody, sectionObj);
}


function RenderMainTable(contentframe, sectionListObj)
{
	var sections = sectionListObj.getElementsByTagName('Section');

	var mainTable = contentframe.document.createElement("table");
	mainTable.setAttribute("width", "100%");
	mainTable.setAttribute("height", "100%");
	var mainTableBody = contentframe.document.createElement("tbody");

	mainTable.appendChild(mainTableBody);
	//mainTable.border = "20";
	var body = contentframe.document.getElementsByTagName("body")[0];
	if(body != null)
	{
		body.appendChild(mainTable);
	}
       
	for(i=0;i< 4;i++)
    {
		var mainTableRow = contentframe.document.createElement("tr");
		var mainTableCol = contentframe.document.createElement("td");
		mainTableRow.appendChild(mainTableCol);
		mainTableBody.appendChild(mainTableRow);

		var section = findSectionWithSlot(sections, 0, i+1);

		RenderSubTables(mainTableCol, section);
    }

}

function findSectionWithSlot(sections, index, slot)
{
	if(index >= sections.length)
	{
		return null;
	}
	else if(sections[index].getAttribute('slot') == slot)
	{
		return sections[index];
	}
	else
	{
		return findSectionWithSlot(sections, index+1, slot);
	}
}

function RenderSections(sectionListObj)
{
	if( sectionListObj == null)
	{
		return;
	}

	var sections = sectionListObj.getElementsByTagName('Section');

	var contentframe = window.contentframe;
	RenderMainTable(contentframe, sectionListObj);
}

function RenderScreen(diagScreenObj)
{
	var sectionListObj = diagScreenObj.getElementsByTagName('SectionList');
	RenderSections(sectionListObj[0]);
}

//-----------------------------------------------------------------------------
// WebPageNavigation
//   Mimic WebBrowser for app navigation, especially enable/disable
//   Back and Forward buttons properly
// Params:
//   nextScreenID   The screen ID of the next screen that is to be displayed.
//-----------------------------------------------------------------------------
function WebPageNavigation(nextScreenID)
{
	window.contentframe.document.body.innerHTML = "";
	//Render the xml Received into a html
	window.contentframe.document.body.id = nextScreenID;

	var xmlObject=new ActiveXObject("Microsoft.XMLDOM");
    xmlObject.async=false;
    xmlObject.load(nextScreenID);
    
	var diagScreenObjList = xmlObject.getElementsByTagName('DiagScreen');
	if(diagScreenObjList != null)
	{
		var diagScreenObj =  diagScreenObjList[0]; 
		if(diagScreenObj != null)
		{
			var diagType = diagScreenObj.getAttribute('type');
			if(diagType == 'SmallScreen')
			{
				RenderScreen(diagScreenObj);
			}
			else if(diagType == 'FourSection')
			{
				commonJS.ExpandDialog();
				RenderScreen(diagScreenObj);
			}
			else 		
			{
				commonJS.ExpandDialog();
				RenderScreen(diagScreenObj);
			}
		}
		else
		{
			//alert('Diag Screen NOT found!!');
		}
	}
}

//-----------------------------------------------------------------------------
// HandleCallbackResults
//   Method called during the HalResultsCallback process. It handles the 
//   screenId or predefined values returned by the asynchronous APIs:
//   HalAction and HalStartup.
// Params:
//   nextScreenId    Usually, this will be the Id of the next screen to be 
//                   displayed. However there are predefined exceptions for 
//                   other possible values that the above APIs can return. 
//-----------------------------------------------------------------------------
function HandleCallbackResults(nextScreenId)
{
	try
	{
	    //set the current content frame in focus
	    window.contentframe.focus();
	    
		if( (nextScreenId != null) && (screenID != nextScreenId))
		{
			var scriptName = commonJS.GetScriptName();
				
			if ((scriptName != null) && (scriptName != ""))
			{
				WebPageNavigation(scriptName);
			}
			else
			{    
				WebPageNavigation(nextScreenId);
			}
		}
	}
	catch(e)
	{
	}
}


function onLinkClick(actionID)
{

    // Use try/catch block in case there's a Dispatch issue
    try
    {
       //alert('onlink click called with action id:' + actionID);
		if(actionID == "Verify")
            doActionAsync(actionID);
		else if(actionID == "Diagonose")
		{
			doActionAsync(actionID);
		}
		else if(actionID == "Close")
		{
			commonJS.Exit();
		}
       else
            doAction(actionID,'');
       
    }
    catch(e) {}

}

