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;
	}
	
	// if wid / hgt is a %, calculate that now
	if (typeof(wid) != 'number' && wid.indexOf('%') > 0) {
		wid = wid.replace(/\%/,"");
		wid = viewWidth * (wid / 100);
	} 
	
	if (typeof(hgt) != 'number' && hgt.indexOf('%') > 0) { 
		hgt = hgt.replace(/\%/,"");
		hgt = viewHeight * (hgt / 100);
	} 

	if (wid > 0) { document.getElementById(theDIV).style.width = wid+"px"; }
	if (hgt > 0) { 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.onreadystatechange=updateContent;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function updateContent() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById('mainContent').innerHTML = xmlHttp.responseText;
		
		// if available, hide navigation links while ads are showing
		if (document.getElementById('navLinks_top')) { document.getElementById('navLinks_top').style.display = "none"; }
		if (document.getElementById('navLinks_bot')) { document.getElementById('navLinks_bot').style.display = "none"; }
		 
	}
}

function setOpenCat(catID) {
	divCatID = catID;
}

function lightBoxLogin(brightness) {
	if (brightness == null || brightness == "") {  brightness = "dark"; }
	
	var LBClass = "dimBG_"+brightness;
	
	document.getElementById('LBLogin').style.display = "block";
	centreDIVOnScreen('LBLogin','50%',250);

	document.getElementById('LBBG').className = LBClass;
	document.getElementById('LBBG').style.display = "block";
}

function closeLightBox(shouldLogin) {
	document.getElementById('LBBG').style.display = "none";
	document.getElementById('LBLogin').style.display = "none";
}

function lb_login() {
	var uName = document.getElementById('LB_LOGIN').value;
	var uPass = document.getElementById('LB_PASS').value;
	
	var msg = "The following fields are required in order to login:\n";
	var err = "";
	if (uName == "") { err += "Login\n"; }
	if (uPass == "") { err += "Password\n"; }
	
	// by default, we do not intend to remember login info (unless selected otherwise by the user)
	var remUser = "N";
	if (document.getElementById('LB_REMEMBER_USER').checked == true) { remUser = "Y"; }
	
	if (err != "") {
		alert(msg + err);
	} else {
		// authenticate via AJAX
		xmlHttp = GetXmlHttpObject();
		var url = "includes/lbAuth.inc.php?LOGIN="+uName+"&PASS="+uPass+"&REMEMBER_USER="+remUser;

		// make our ajax call
		xmlHttp.onreadystatechange=doLBLogin;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function doLBLogin() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		var dResponse = xmlHttp.responseText;
		if (dResponse.indexOf("Invalid") >= 0) {
			// authentication failed - notify user
			dResponse = "- " + dResponse;
			alert("Login failed:\n\n" + dResponse);
		} 
		else { window.location.reload(); }
	} 
}

function doAJAXRequest(url) {
	xmlHttp = GetXmlHttpObject();
	
	xmlHttp.onreadystatechange=silentEvent;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function silentEvent() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		window.location.reload();
	}
};

