var xmlHttp;

// Gets the XMLHttpRequest Object - REQUIRED for use of Ajax throughout system
function GetXmlHttpObject()
{ 
	if (xmlHttp && xmlHttp.readyState < 4) { xmlHttp.abort(); }
	
	var objXMLHttp = null;
	var msg = "";
	try {
	  objXMLHttp = new XMLHttpRequest();
	} catch (tryMS_2) {
  		try {
			objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
  		} catch (tryMS_original) {
    		try {
      			objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    		} catch (failed) {
      			objXMLHttp = null;
			}
		}
	}
	return objXMLHttp;
}	

// Shows a DIV in the middle of the screen
function centreDIVOnScreen(theDIV,wid,hgt) {
	var viewHeight = 0;
	var viewWidth = 0;
	var sTop = 0;
	var sLeft = 0;
	
	if (typeof(window.innerWidth) == 'number') {
		//Non-IE
		viewWidth += window.innerWidth;
		viewHeight += window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		viewWidth += document.documentElement.clientWidth;
		viewHeight += document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		viewWidth += document.body.clientWidth;
		viewHeight += document.body.clientHeight;
	}

  	if (typeof(window.pageYOffset) == 'number') {
		//Netscape compliant
		sLeft += window.pageXOffset;
		sTop += window.pageYOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		sLeft += document.body.scrollLeft;
		sTop += document.body.scrollTop;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		sLeft += document.documentElement.scrollLeft;
		sTop += document.documentElement.scrollTop;
	}
	
	document.getElementById(theDIV).style.width = wid+"px";
	document.getElementById(theDIV).style.height = hgt+"px";
	
	document.getElementById(theDIV).style.top = (((viewHeight - hgt) / 2) + sTop)+"px";
	document.getElementById(theDIV).style.left = (((viewWidth - wid) / 2) + sLeft)+"px";
}

// Parses out the <script> tags within the returned (usu Ajax) code and runs the javascript
// - also, sets the innerHTML of the "targetElement" to the non-JS code within the passed string
function parseAJAX(htmlCode,targetElement) {
	// Check the response text and parse out any JS script tags so we
	// can 'eval' them (run them)
	var respText = htmlCode;
	var jsEvalCode = "";
	var beginScript = respText.indexOf('<script');
	
	while (true) {
		// Looks like we have JS code, so parse it out and evaluate it
		beginScript = respText.indexOf('<script');
		if (beginScript < 0) { break; }
		
		var endScript = respText.indexOf('/script');
		var jsScript = respText.substr(beginScript,(endScript-beginScript-1));
		var endStartTag = jsScript.indexOf('>');
		jsEvalCode += jsScript.substr(endStartTag+1);
		var beforeJS = respText.substr(0,beginScript-1);
		var afterJS = respText.substr(endScript+8);
		respText = beforeJS+afterJS;
	}
	targetElement.innerHTML=respText;
	if (jsEvalCode != "") {
		jsEvalCode += ";";
		jsEvalCode = stripComments(jsEvalCode);
		eval(jsEvalCode);
	}
}

// Strip out comments within HTML/JS code
function stripComments(htmlCode) {
	var startComment = htmlCode.indexOf('<!--');
	var endComment = htmlCode.indexOf('//-->');
	var preCode = "";
	var postCode = "";
	
	while (true) {
		startComment = htmlCode.indexOf('<!--');
		endComment = htmlCode.indexOf('//-->');

		if (startComment < 0 && endComment < 0) { break; }
		
		if (startComment >= 0) {
			preCode = htmlCode.substr(0,startComment);
			postCode = htmlCode.substr(startComment+4);
			htmlCode = preCode+postCode;
		}
		
		endComment = htmlCode.indexOf('//-->');
		if (endComment >= 0) {
			preCode = htmlCode.substr(0,endComment);
			postCode = htmlCode.substr(endComment+5);
			htmlCode = preCode+postCode;
		}
	}
	return htmlCode;
}

function hidePopup(alpha) {
	var wOpacity = (alpha / 100);
	var pObj = document.getElementById('popupAction');

	if (alpha > 0) { 
		pObj.style.opacity = wOpacity; // W3 compliant
		if (pObj.filters) { pObj.filters.alpha.opacity = alpha; } // IE compliant
		
		alpha = alpha - 10; 
		setTimeout("hidePopup("+alpha+")",50);
	} else { 
		pObj.style.display = "none";
	}
}

function actionPopup(message,width,height,hideAfter) {
	document.getElementById('popupAction_message').innerHTML = message;
	document.getElementById('popupAction').style.display = "block";
	centreDIVOnScreen('popupAction',width,height);
	
	var inMS = (hideAfter * 1000);
	setTimeout("hidePopup(100)",inMS);
}

var divCatID = 0;

function toggleProductCats(groupID) {
	// hide current open group
	if (divCatID != 0) {
		document.getElementById("cat_"+divCatID).style.display = "none";
	}
	
    divCatID = groupID;
    
    var curDispState = document.getElementById("cat_"+divCatID).style.display; 
	var dispState = "none";
	
	if (curDispState == "none") { dispState = "block"; }
    document.getElementById("cat_"+divCatID).style.display = dispState;
    
    // update the "selected group"
    xmlHttp = GetXmlHttpObject();
    var url = "includes/browse.inc.php?cmd=selCat&group="+groupID;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function setOpenCat(catID) {
	divCatID = catID;
}