<!--

//do not change the name (expect to make a ton of changes elsewhere)
/**
    function to create an XMLHttpRequest object.
    this is done since some vendors (Microsoft)
    have their own non-standard object.
*/
function createXmlHttpRequest() {
	var request = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try { request = new XMLHttpRequest(); } 
		catch(e) { request = false; }
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try { request = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch(e) {
			try { request = new ActiveXObject("Microsoft.XMLHTTP"); } 
			catch(e) { request = false; }
		}
	}
	return request;
}

/*
    AJAX object that will be used to get the serial numbers
    for a system. the system is gotten by inspecting
    a select list with id "systemName" and a select list with
    id "systemRevision". a call is made to get an XML file
    of all the systems serial numbers. once the serial numbers
    are gotten then a select list with id of "serialNumber"
    is populated.
*/
function ShowSerialNumbers(contextPath, allText, serialNumbersSelect){
	this.url = contextPath + "/rDSServlet";
	
	this.allText = allText;
	
	this.numbers = serialNumbersSelect;

	var self = this;

	var request;
	
	function loadXmlDocument(parameters) {
		if (request) {
            request.abort();
        }
        request = false;
		request = self.createXmlHttpRequest();
		if(request) {
			request.onreadystatechange = processRequestChange;
			request.open("POST", self.url, true);
			request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			request.send(parameters);
		}
	}

	function processRequestChange() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				showItems();
			} else {
				//bad request
			}
		}
	}

	function showItems() {
		var select = self.numbers;
		
		select.length = 0;
		        
        select.options[select.options.length] = new Option(self.allText, "", false, true);
        
        try {
    		var xml = request.responseXML;
    
    		var serialNumbersElement = xml.getElementsByTagName("serialNumbers")[0];
    		
    		var serialNumberElementArray = serialNumbersElement.getElementsByTagName("serialNumber");
    		
    		var value = ""; 
    		for (var i = 0; i < serialNumberElementArray.length; i++) { //iterator through the option elements array
    			try { 
                    value = serialNumberElementArray[i].childNodes[0].nodeValue; 
                } catch (e) { 
                    value = ""; 
                }
                
    			select.options[select.options.length] = new Option(value, value, false, false);
    		}
        } catch (e) {
            //nop
        }
	}

	this.callAjax = function (name, revision) {
		var parameters = "table=ajax"
        parameters += "&action=getSerialNumbers";
		parameters += "&systemName=" + encodeURIComponent(name);
		parameters += "&systemRevision=" + encodeURIComponent(revision);
		
		loadXmlDocument(parameters);
	}

	//methods
	this.createXmlHttpRequest = createXmlHttpRequest;
}

/*
    AJAX object to populate a popup with the values
    for components in a model. the components descriptions
    are gotten using the components SGML ID.   
*/
function ShowComponents(contextPath, modelName, modelRevision) {
    //variables
    this.url = contextPath + "/rDSServlet";
    this.modelName = modelName;
    this.modelRevision = modelRevision;
    
    var request;
    var pop;
    var self = this;
    
    //functions
    //get the xml document from the server
    function loadXmlDocument(parameters) {
        if (request) {
            request.abort();
        }
        request = false;
        request = self.createXmlHttpRequest();
        if(request) {
            request.onreadystatechange = processRequestChange;
            request.open("POST", self.url, true);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send(parameters);
        }   
    }

    function processRequestChange() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                showComponents();
            } else {
                pop.populate(request.statusText);
                pop.refresh();
            }
        }
    }
    
    function popupInit(divId) {
        pop = new PopupWindow(divId);
        pop.autoHide();
        pop.offsetY=15;
    }
    
    function popupWaiting(anchorId) {
        var html = "";
		html += "<span style='width: 100%; height: 100%; font-size: 160%; font-weight: bold; color: #f00; text-align: center; vertical-align: middle;'>";
        html += "...";
        html += "</span>";
        
        pop.populate(html);
        pop.showPopup(anchorId);
    }
    
    function showComponents() {
        var xml = request.responseXML;
                        
        var html = "";
                                    
        try {
            html += "<ul style='margin-top: 2px; margin-bottom: 2px; margin-left: 1.5em; margin-right: 1.5em; padding: 0'>";
            
            var modelElement = xml.getElementsByTagName("model")[0];
            
            var componentElements = modelElement.getElementsByTagName("component");
            
            var value;
            var i;
            for (i = 0; i < componentElements.length; i++) {
                try {
                    value = componentElements[i].childNodes[0].nodeValue;
                } catch (e) {
                    value = null; 
                }
                if (html) {
                    html += "<li>";
                    html += value;
                    html += "</li>";
                }
            }
            html += "</ul>";
        } catch (e) {
            //nop
        }
        
        pop.populate(html);
        pop.refresh();
    }
    
    /*
        provide the id of the anchor element with which to place the popup.
        provide the id of the div element that is used as a popup.
        provide the sgmlIds as a comma seperated list. eventually
        a popup will appear with the component descriptions in an 
        unordered list.
    */
    this.callAjax = function (anchorId, divId, sgmlIds) {
        popupInit(divId);
        popupWaiting(anchorId);
            
        var parameters = "table=ajax";
        parameters += "&action=getComponents";
        parameters += "&modelName=" + encodeURIComponent(modelName);
        parameters += "&modelRevision=" + encodeURIComponent(modelRevision);
        parameters += "&sgmlIds=" + encodeURIComponent(sgmlIds);

        loadXmlDocument(parameters);
    }
    //methods
    this.createXmlHttpRequest = createXmlHttpRequest;
}

/*
    AJAX object to get serial numbers and symptoms for a 
    particular system. when calling the get the XML
    the system is inferred from a select list with id
    "systemName" and select list with id of "systemRevision".
    in some instances we will also get the serial number from
    a select list with id of "serialNumber". once the xml is
    passed back the select list with id "serialNumber" and select
    list with id "symptom" will be filled in.
*/
function ReportConfig1(contextPath, allText, numbersSelect, symptomsSelect){
	this.url = contextPath + "/rDSServlet";
	
	this.allText = allText;
	
	this.numbers = numbersSelect;
	
	this.symptoms = symptomsSelect;

	var type;

	var self = this;

	var request;
	
	function loadXmlDocument(parameters) {
		if (request) {
            request.abort();
        }
        request = false;
		request = self.createXmlHttpRequest();
		if(request) {
			request.onreadystatechange = processRequestChange;
			request.open("POST", self.url, true);
			request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			request.send(parameters);
		}
	}

	function processRequestChange() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				showItems();
			} else {
				//bad request
			}
		}
	}

	function showItems() {
		var xml = request.responseXML; //pull the xml from the request stream
		
		var select; //variable used for the select box which you wish to populate
		
		var value = "";
        var text = "";
		
		var element;
		var elements;

		if (type == "both" || type == "serialNumbers") {
			select = self.numbers;
			
			select.length = 0;
			
            select.options[select.options.length] = new Option(self.allText, "");
                        
            try {
    			element = xml.getElementsByTagName("serialNumbers")[0];
                
                elements = element.getElementsByTagName("serialNumber");
    			for (var i = 0; i < elements.length; i++) { //iterator through the option elements array	
    				try { 
    					value = elements[i].getElementsByTagName("value")[0].childNodes[0].nodeValue; 
    				} catch (e) { 
    					value = ""; 
    				}
                    try {
                        text = elements[i].getElementsByTagName("text")[0].childNodes[0].nodeValue;
                    } catch (e) {
                        text = "";
                    }
    
    				select.options[select.options.length] = new Option(text, value); //create a new option in the select list box
    			}
            } catch (e) {
                //nop
            }
		}
		if (type == "both" || type == "symptoms") {
			select = self.symptoms;
			
			select.length = 0;
            
            select.options[select.options.length] = new Option(self.allText, "");
			            
            try {
                element = xml.getElementsByTagName("symptoms")[0];
			     
    			elements = element.getElementsByTagName("symptom");
    			for (var i = 0; i < elements.length; i++) { //iterator through the option elements array	
    				try { 
    					value = elements[i].getElementsByTagName("value")[0].childNodes[0].nodeValue; 
    				} catch (e) { 
    					value = ""; 
    				}
                    try {
                        text = elements[i].getElementsByTagName("text")[0].childNodes[0].nodeValue;
                    } catch (e) {
                        text = "";
                    }
                    
    				select.options[select.options.length] = new Option(text, value);
    			}
            } catch (e) {
                //nop
            }
		}
	}
    
	this.callAjax = function (displayType, name, revision, number) {		
		type = displayType;

		var parameters = "table=ajax"
		parameters += "&action=reportConfig1";
		parameters += "&systemName=" + encodeURIComponent(name);
		parameters += "&systemRevision=" + encodeURIComponent(revision);
		parameters += "&serialNumber=" + encodeURIComponent(number);
		
		loadXmlDocument(parameters);
	}

	//methods
	this.createXmlHttpRequest = createXmlHttpRequest;
}

/*
    AJAX object used by ReportConfig3.jsp to provide
    a popup which compares technicians repair times
    for a particular symptom.
*/
function ReportConfig3(contextPath, imagePath, header1, header2) {
    //variables
    this.url = contextPath + "/rDSServlet";
    
	var imagePath = imagePath;
	
    //popup table headers
    this.header1 = header1;
    this.header2 = header2;
    
    var request;
    var pop;
    var self = this;
    
    //functions
    //get the xml document from the server
    function loadXmlDocument(parameters) {
        if (request) {
            request.abort();
        }
        request = false;
        request = self.createXmlHttpRequest();
        if(request) {
            request.onreadystatechange = processRequestChange;
            request.open("POST", self.url, true);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send(parameters);
        }   
    }

    function processRequestChange() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                showComparison();
            } else {
                pop.populate(request.statusText);
                pop.refresh();
            }
        }
    }
    
    function popupInit(divId) {
		if (pop) {
			pop.hidePopup();
		}

		var element = document.getElementById(divId);
		if (element)
			element.style.height = "auto";

        pop = new PopupWindow(divId);
        //pop.autoHide();
        pop.offsetY=15;
    }
    
    function popupWaiting(anchorId) {
        var html = "";
        html += "<div style='width:100%;text-align:center;'><img src=\"" + imagePath + "/ajax-loader.gif\" border=\"0\" /></div>"
        
        pop.populate(html);
        pop.showPopup(anchorId);
    }
    
    function showComparison() {
        var table = "<table class='listing' cellspacing='0' align='center'>";
        table += "<thead>";
		table += "<tr>";
        table += "<th>";
        table += self.header1;
        table += "</th>";
        table += "<th>";
		
		table += "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">";
		table += "<thead>";
		table += "<tr>";
		table += "<th style='border: none;'>";
		table += self.header2;
		table += "</th>";
		table += "<th align=\"right\" style='border: none;'>";
		//TODO: onclick needs to close the popup by referencing the object with
		//the popup window object. could not figure out quickly hence this is a hack.
		//a hack since "compare" is the name of the object instantiated
		table += "<a href=\"#\" onclick=\"compare.closePopup(); return false;\" class=\"nav\">";
		table += "<img src=\"" + imagePath + "/close.gif\" border=\"0\" style=\"margin-left: 10px;\" />";
		table += "</a>";
		table += "</th>";
		table += "</tr>";
		table += "</thead>";
		table += "</table>";
		
		table += "</th>";
        table += "</tr>";
        table += "</thead>"
		table += "<tbody>";
        try {
            var xml = request.responseXML;
            
            var element = xml.getElementsByTagName("comparisons")[0];
            
            var elements = element.getElementsByTagName("comparison");
            
            var row;

            var rowClass = "";
        
            var i;
            for (i = 0; i < elements.length; i++) {
                rowClass = "row" + ((i + 1) % 2);
				try {
                    row = "<tr class='" + rowClass + "'>";
                    row += "<td width='50%' align='left'>";
                    row += elements[i].getElementsByTagName("technician")[0].childNodes[0].nodeValue;
                    row += "</td>";
                    row += "<td width='50%' align='right'>";
                    row += elements[i].getElementsByTagName("averageTime")[0].childNodes[0].nodeValue;
                    row += "</td>";
                    row += "</tr>";
                                                           
                    table += row;
                } catch (e) {
                    //nop
                }
            }
        } catch (e) {
            //nop
        }
		table += "</tbody>";
        table += "</table>";
                
        pop.populate(table);
        pop.refresh();
    }

	/*
        provide the id of the anchor element with which to place the popup.
        provide the id of the div element that is used as a popup.
        provide the symptom name that you want to do a technician comparison
        of.
    */
    this.callAjax = function (anchorId, divId, symptomName) {
        popupInit(divId);
        popupWaiting(anchorId);
            
        var parameters = "table=ajax";
        parameters += "&action=reportConfig3";
        parameters += "&symptom=" + encodeURIComponent(symptomName);

        loadXmlDocument(parameters);
    }
    
    //methods
    this.createXmlHttpRequest = createXmlHttpRequest;

	this.closePopup = function () {
		pop.hidePopup();
	}
}

/* 
    class for showing the untranslated and translated strings
    uses AJAX to get the strings from the server asynchronously
*/
function TranslateModel(contextPath){
    this.url = contextPath + "/rDSServlet";

    var request;

    var self = this;
    
    function loadXmlDocument(parameters) {
        if (request) {
            request.abort();
        }
        request = false;
        request = self.createXmlHttpRequest();
        if(request) {
            request.onreadystatechange = processRequestChange;
            request.open("GET", self.url + "?" + parameters, true);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send(null);
        }
    }

    function processRequestChange() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                showItems();
            } else {
                rewriteHtml(request.statusText);
            }
        }
    }

    function showItems() {
        var untranslated = "";
        var translated = "";
        
        try {
            var xml = request.responseXML;
            
            var documentElement = xml.getElementsByTagName("document")[0];
                                                                
            try { 
                untranslated = documentElement.getElementsByTagName("untranslated")[0].childNodes[0].nodeValue; 
            } catch (e) { 
                untranslated = ""; 
            }
            try { 
                translated = documentElement.getElementsByTagName("translated")[0].childNodes[0].nodeValue; 
            } catch (e) { 
                translated = ""; 
            }
        } catch (e) {
            //nop
        }
        document.getElementById("untranslated").innerHTML = untranslated;
        
        self.rewriteHtml(translated);
    }
    
    this.callAjax = function (checksum, language) {
        var parameters = "table=ajax";
        parameters += "&action=translateModel";
        parameters += "&checksum=" + encodeURIComponent(checksum);
        parameters += "&language=" + encodeURIComponent(language);

        loadXmlDocument(parameters);
    }

    //methods
    this.createXmlHttpRequest = createXmlHttpRequest;

    this.rewriteHtml;
}

// -->
