
// Created on May 19, 2007 by nhorvath
// NXWEB Technologies
// All Rights Reserved - Unauthorized Use Prohibited
// For information contact licensing@nxwebtechnologies.com
 

var g_callbackSpan;
var g_followUpFunction;
var g_urlHistory = new Array();

function makePOSTRequest(url, parameters, callbackSpan, followUpFunction) {
   g_callbackSpan = callbackSpan;
   g_followUpFunction = followUpFunction;
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   
   http_request.onreadystatechange = alertContents;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}

function alertContents() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		 if (document.getElementById(g_callbackSpan)) {
	         result = http_request.responseText;
	         document.getElementById(g_callbackSpan).innerHTML = result;
         }
         eval(g_followUpFunction);
      } else {
         throw ('NXWEB Ajax Error: - could not perform post');
      }
   }
}

function nxwebAjaxFormProcessor(theFormId, actionUrl, preProcessorFunction, followUpFunction, callbackSpan) {
	var parameterString = '';

	if (theFormId) {
	  if (verifyForm(document.getElementById(theFormId))) {
		  // get element array using the specified preProcessorFunction
		  argumentHash = eval(preProcessorFunction);
		  
		  for (var fieldName in argumentHash) {
		  	parameterString += fieldName+"="+encodeURIComponent(argumentHash[fieldName])+"&";
		  }
	  } else {
	  	  return false;
	  }
	}
	else {
		argumentHash = eval(preProcessorFunction);
		  
		for (var fieldName in argumentHash) {
		  	parameterString += fieldName+"="+encodeURIComponent(argumentHash[fieldName])+"&";
		}
	}
	
	// to avoid caching
	parameterString += "nxtilepost=true&decache=" + (Math.round((Math.random()*9)+1));
	
	 makePOSTRequest(actionUrl, parameterString, callbackSpan, followUpFunction);


}

function nxwebTileLoader(targetAction, targetSpan, followUpFunction, title) {

	if (title) {
		var elementIndex = g_urlHistory.length;
	    g_urlHistory[elementIndex] = new Array();
	    
	    g_urlHistory[elementIndex]['targetAction'] = targetAction;
	    g_urlHistory[elementIndex]['targetSpan'] = targetSpan;
	    g_urlHistory[elementIndex]['followUpFunction'] = followUpFunction;
	    g_urlHistory[elementIndex]['title'] = title;
    	updateBreadCrumbs();
    }
   // var callAudit = 'nxwebAjaxFormProcessor(\''+theFormId+, actionUrl, preProcessorFunction, followUpFunction, callbackSpan)';
	var parameterString = '';
	nxwebAjaxFormProcessor('', targetAction, '', followUpFunction,  targetSpan);
	
}



function breadCrumbForwarder(index) {
	var diffToMax = g_urlHistory.length - index - 1;
	g_urlHistory.splice(index+1, diffToMax);
	nxwebTileLoader(g_urlHistory[index]['targetAction'], g_urlHistory[index]['targetSpan'], g_urlHistory[index]['followUpFunction']);
	updateBreadCrumbs();
	
}

function backOne() {
	breadCrumbForwarder(g_urlHistory.length - 2) 
}

function updateBreadCrumbs() {
	if (document.getElementById('breadCrumbContainer')) {
		var breadCrumbString = '';
		var displayedLength = 0;
		var maxDisplayedLength = 40;
		
		if (g_urlHistory.length > 0) {
			for(keyVar in g_urlHistory) {
				displayedLength += g_urlHistory[keyVar]['title'].length;
			}
			var startingIndex = displayedLength - maxDisplayedLength;
			var currentDisplayedLength = 0;
			for(keyVar in g_urlHistory) {
				currentDisplayedLength += g_urlHistory[keyVar]['title'].length;
				var title = "";
				if (currentDisplayedLength > startingIndex) {
					title = g_urlHistory[keyVar]['title'];
    				breadCrumbString += ' &lt; <a href="javascript:breadCrumbForwarder('+keyVar+')" class="BreadCrumbLink">'+title+'</a>';
				}
			}
		}
		
		if (currentDisplayedLength < maxDisplayedLength) {
    		breadCrumbString = '<a href="inventorybrowser.php" class="BreadCrumbLink">Home</a>'+breadCrumbString;
		} else {
			breadCrumbString = '<a href="inventorybrowser.php" class="BreadCrumbLink">&lt; &lt; </a>'+breadCrumbString;
		}
		
		document.getElementById('breadCrumbLinks').innerHTML = breadCrumbString;
	}

}

