/* Script calling PDT functionality via Javascript */

var req;				// XMLHTTP object to send messages
var ctlResult;	// Control which will be populated with the message results

// =================================================
// Message functionality to access the app without submitting the form

// Create the XMLHttp object to send a message
function CreateReq()
{
	// These will work on IE
  try { 
		req = new ActiveXObject("Msxml2.XMLHTTP"); 
	}
  catch(e) {
		try {
			req = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
	  catch(oc) {
			req = null;  
		}
	}

  // Default situation
  if (!req && typeof XMLHttpRequest != "undefined")	{	
		req = new XMLHttpRequest();	
	}
  
}

function FormatId(value) {
		var sResult = "0000000000" + value;
		sResult = sResult.substring(sResult.length - 10, sResult.length);
		return sResult;
	}
	
function GetCategoryValues(sessionId, idCheck, categoryIds) {

	CreateReq();
	
  if (req != null)
  {
		// Create the message
		var url = "messages.aspx?data=001" + FormatId(sessionId) + idCheck;
		
		for(i = 0; i < categoryIds.length; i=i+10) {
			var id = categoryIds.substring(i, i+10);
			var ctl = document.getElementById("lstCategory" + (1 * id))
			if (ctl) {
				var value = ctl.value;
				if (value != 0) {
					url += categoryIds.substring(i, i+10) + ":" + FormatId(value) + ","
				}
			}
		}

		// Send the message
    req.onreadystatechange = ProcessGetCategoryValues;
    req.open("GET", url, true);
    req.send(null);

  }
}

// Displays summary information about a barcode - response
function ProcessGetCategoryValues()
{

	if (req.readyState == 4) {
		if (req.status == 200) { 
			var data = req.responseText;
			if (data.substring(0, 1) == "O") {
				// Success - update the list
				data = data.substring(1, 10000);

				var rows = new Array();
				rows = data.split('\r\n');

				// Total
				document.getElementById("lblInfo").innerHTML = rows[0];
				
				// Keywords				
				var ctl = document.getElementById("lstKeywords");
				for (i = ctl.options.length-1; i >= 0; i--) {
					 ctl.options[i] = null;
				}

				var rows2 = new Array();
				rows2 = rows[1].substring(0,1000).split('\t');
				for(j = 0; j < rows2.length - 1; j++)
				{
					ctl.options[j] = new Option(rows2[j]);
					
					if (ctl.options[j].value == selectedValue) {
						ctl.options[j].selected = true;
					} 
				}

				
				// Category Lists
				for(i = 2; i < rows.length; i++) {
					var id = rows[i].substring(0, 10);
					var ctl = document.getElementById("lstCategory" + (1 * id))
					if (ctl) {
						var selectedValue = ctl.value;
						
						// Clear the list of current items
						for (j = ctl.options.length-1; j >= 0; j--) {
							ctl.options[j] = null;
						}

						// Add the new items 
						var rows2 = new Array();
						rows2 = rows[i].substring(10,1000).split('\t');
						for(j = 0; j < rows2.length - 1; j++)
						{
							ctl.options[j] = new Option(rows2[j].substring(10, 50));
							ctl.options[j].value = 1 * rows2[j].substring(0, 10);
							
							if (ctl.options[j].value == selectedValue) {
								ctl.options[j].selected = true;
							} 
						}

					}
				}
			}
		}


	}
}