/*
'----------------------------------------------------------------------------
'  (C)opyright 2009
'
'                           Quest TechnoLogy Group, Inc.
'                           Quest Dynamix, LLC
'                           3751 Maguire Blvd, Suite 150
'                           Orlando, FL 32803
'
'  All rights reserved.
'
'  Proprietary and Confidential.
'  May not be reproduced, transmitted or reverse engineered in any form or
'  by any means, electronic, mechanical, photocopying, recording, or
'  otherwise, without the prior written consent of Quest TechnoLogy Group Inc.
'  and Quest Dynamix, LLC.  Access to this software, including but not limited
'  to runtime applications, source code, binaries and all documentation is
'  restricted to authorized personnel of Quest TechnoLogy Group, Inc and Quest
'  Dynamix, LLC.
'  Information contained herein is confidential and proprietary to Quest
'  TechnoLogy Group and Quest Dynamix, LLC.  Violation of these restrictions
'  may be punishable by law.
'
'  No co-ownership provisions apply.
'----------------------------------------------------------------------------
*/

function getXMLHTTP()
{
	var oXMLHTTP = null;
	
	if (window.ActiveXObject) { // IE
        try {
            oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
        } 
		catch (e) {
            try {
                oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
            } 
			catch (e) {
			}
        }
    } 
	else if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        oXMLHTTP = new XMLHttpRequest();
		if ( oXMLHTTP ) {
			if (oXMLHTTP.overrideMimeType) {
				oXMLHTTP.overrideMimeType('text/xml');
			}
		}	
    } 
	else {	
        alert("Browser doesn't support XMLHTTP");
        return null;
    }
    
	return oXMLHTTP;	
}

function LoadSerialContent(oActiveContent, sURL, sInput, bLoadScripts) 
{

	var errMsg = '';
	var bGood = false;

	//Clear out any existing content.
	if ( oActiveContent.firstChild ) {
		oActiveContent.removeChild(oActiveContent.firstChild);
	}	
	
	//Show loading message
	var oMessage = document.createElement('p');
	oMessage.innerHTML = "&nbsp;&nbsp;&nbsp;Loading...";
	oActiveContent.appendChild(oMessage);
	oActiveContent.style.cursor='wait';
	document.refresh;

	//Post request
	var oXMLHTTP = getXMLHTTP();
	if ( oXMLHTTP ) {
		oXMLHTTP.open('POST', sURL, false);
		oXMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			
		// Execute the request
		oXMLHTTP.send(encodeURI(sInput));
			
		// Got result 
		if (oXMLHTTP.status == 200) {
			if ( oActiveContent.firstChild ) {
				oActiveContent.removeChild(oActiveContent.firstChild);
			}	
			
			var oRequestContent = document.createElement('div');
			oRequestContent.setAttribute('id',oActiveContent.id + 'Child'); 
			oRequestContent.innerHTML = oXMLHTTP.responseText;
			if ( bLoadScripts == true ) {
				loadScripts(oRequestContent);
			}
			
			oActiveContent.appendChild(oRequestContent);

			bGood = true;
		}
		
		// Run time error 
		else if ( oXMLHTTP.status == 500 ) {
			if ( oActiveContent.firstChild ) {
				oActiveContent.removeChild(oActiveContent.firstChild);
			}	
	
			var win = window.open("", "win", "width=800,height=600,resizable=yes,scrollbars=yes");
				win.document.open("text/html", "replace");
				win.document.write(oXMLHTTP.responseText);
				win.document.close();
				win.focus();
				
				errMsg = 'Request failed.'
		}
		else {
			if ( oActiveContent.firstChild ) {
				oActiveContent.removeChild(oActiveContent.firstChild);
			}	
	
			errMsg = 'There was a problem with the request. Status: ' + oXMLHTTP.status;
		}
	}
	else {
		errMsg = 'Failed to create request object.';
	}
	
	if ( !bGood && errMsg.length > 0 ) {
		if ( oActiveContent.firstChild ) {
			oActiveContent.removeChild(oActiveContent.firstChild);
		}	

		oMessage.innerHTML = "&nbsp;&nbsp;&nbsp;" + errMsg;
		oActiveContent.appendChild(oMessage);
	}	

	oActiveContent.style.cursor='auto';
	document.refresh;
	
	return bGood;
}

// EXPECTS 'ACK' RESPONSE
function SubmitSerialRequest(sURL, sInput) 
{

	var errMsg = '';
	var bGood = false;

	//Post request
	var oXMLHTTP = getXMLHTTP();
	if ( oXMLHTTP ) {
		oXMLHTTP.open('POST', sURL, false);
		oXMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			
		// Execute the request
		oXMLHTTP.send(encodeURI(sInput));
			
		// Got result 
		if (oXMLHTTP.status == 200) {
			if ( oXMLHTTP.responseText == 'ACK' ) {
				bGood = true;
			}
			else {
				errMsg = oXMLHTTP.responseText;
				alert(errMsg);
				if ( errMsg.length == 0 ) {
					errMsg = 'No message returned from service request; status of request is unknown'; 
				}
			}	
		}

		else if ( oXMLHTTP.status == 500 ) {
			var win = window.open("", "win", "width=800,height=600,resizable=yes,scrollbars=yes");
				win.document.open("text/html", "replace");
				win.document.write(oXMLHTTP.responseText);
				win.document.close();
				win.focus();
				
				errMsg = 'Request failed.'
		}		
		else {
			errMsg = 'HTTP ' + oXMLHTTP.status + ' Error.  Request could not be completed.';
		}
	}
	else {
		errMsg = 'Failed to create HTTP request object.';
	}

	if (! bGood ) {
		errMsg = errMsg + '<br>Request: ' + sURL + '<br>Parameters: ' + sInput
	}
	else {
		errMsg = ''
	}
			 
	return errMsg; //errMsg;
}

// EXPECTS JSON RESPONSE
function SubmitSerialJSONRequest(sURL, sInput) 
{

	//Post request
	var oXMLHTTP = getXMLHTTP();
	if ( oXMLHTTP ) {
		oXMLHTTP.open('POST', sURL, false);
		oXMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			
		// Execute the request
		oXMLHTTP.send(encodeURI(sInput));
			
		// Got result 
		if (oXMLHTTP.status == 200) {
			return eval(oXMLHTTP.responseText);
		}
		else if ( oXMLHTTP.status == 500 ) {
			var win = window.open("", "win", "width=800,height=600,resizable=yes,scrollbars=yes");
				win.document.open("text/html", "replace");
				win.document.write(oXMLHTTP.responseText);
				win.document.close();
				win.focus();
		}
		else {
			alert('There was a problem with the request. Status: ' + oXMLHTTP.status);
		}
	}
			 
	return null;
}

function loadScripts(oRequestContent)
{
	if ( oRequestContent ) {
		var cScripts = oRequestContent.getElementsByTagName("script");
		for(var i = 0; i < cScripts.length; i++) {
    		installScript(cScripts[i]);
  		} 
	}
}

function installScript( oScript )
{
    if ( !oScript )
        return;

    if ( oScript.src ) {
        var head = document.getElementsByTagName("head")[0];
        var scriptObj = document.createElement("script");
		
        scriptObj.setAttribute("type", "text/javascript");
        scriptObj.setAttribute("src", oScript.src);  

        head.appendChild(scriptObj);

    }
    else if ( oScript.innerHTML ){
        if (window.execScript) {
            window.execScript( oScript.innerHTML );
        }
		else
            window.setTimeout( oScript.innerHTML, 0 );
    } 
}

function getInput(oNode, n)
{
	var inputStr = "";
	if ( oNode ) {
		if ( oNode.hasChildNodes() ) { 
			var i = 0;
			//while ( i < oNode.childNodes.length ) {
			for (var i=0; i < oNode.childNodes.length; i++) {
				 
				var childNode = oNode.childNodes[i];
				if ( childNode.tagName ) {
					if (childNode.tagName == "INPUT") {
						if (childNode.type == "text") {
						   inputStr += childNode.name + "=" + getSafeValue(childNode.value) + "&";
						}
						else if (childNode.type == "hidden") {
						   inputStr += childNode.name + "=" + getSafeValue(childNode.value) + "&";
						}
						else if (childNode.type == "checkbox") {
							if (childNode.checked) {
							  inputStr += childNode.name + "=" + getSafeValue(childNode.value) + "&";
							} 
							else {
							  inputStr += childNode.name + "=&";
							}
						}
						else if (childNode.type == "radio") {
							if (childNode.checked) {
							  inputStr += childNode.name + "=" + getSafeValue(childNode.value) + "&";
							}
						}
						else if (childNode.type == "password") {
						   inputStr += childNode.name + "=" + getSafeValue(childNode.value) + "&";
						}
					}   
					else if (childNode.tagName == "SELECT") {
						if ( childNode.selectedIndex < 0 ) {
							inputStr += childNode.name + "=&";
						}
						else {
							inputStr += childNode.name + "=" + getSafeValue(childNode.options[childNode.selectedIndex].value) + "&";
						}    
					}
					else if (childNode.tagName == "TEXTAREA") {
						inputStr += childNode.name + "=" + getSafeValue(childNode.innerHTML) + "&";
					}
					else {
						inputStr += getInput(childNode, n+1);
					}
				} 
					
				//i = i + 1;
			}
		}
	}	
	
	return inputStr;   
}

function getSafeValue(sStr)
{
	return encodeURIComponent(sStr);
} 

function clearNode(oNode)
{
    while ( oNode.firstChild ) {
        oNode.removeChild(oNode.firstChild);
    }
}

function findNodeName(oNode, sName)
{
	var inputStr = "";
	var oFoundNode = null;
	if ( oNode.hasChildNodes() ) { 
		var i = 0;
		//while ( i < oNode.childNodes.length ) {
		for (var i=0; i < oNode.childNodes.length; i++) {
			 
			var childNode = oNode.childNodes[i];
			if ( childNode.name ) {
				if (childNode.name == sName) {
					oFoundNode = childNode;
					break;
				}   
			} 
			oFoundNode = findNodeName(childNode, sName)
			if ( oFoundNode ) {
				break;
			}	
		}
	}
	
	return oFoundNode;   
} 
