var xmlhttp = false;

try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); // IE 5
} catch (e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE 4
	} catch (E) {
		xmlhttp = false; // Not IE
	}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') xmlhttp = new XMLHttpRequest(); 	// Non-IE browser

/* -------------------- shopping cart -------------------- */

function addProductToCart(form) {
	var ts = new Date().getTime();

	if (confirm("Are you sure you want to add this item to your cart?")) {
		if (form.elements['Quantity'].value>0) {
			var serverPage = "/include/ajax_addtocart.php?productid="+form.elements['ProductId'].value+"&quantity="+form.elements['Quantity'].value+"&ts="+ts;
			if (form.elements['SSN']) serverPage+="&ssn="+form.elements['SSN'].value;

			var obj = document.getElementById("cartcontainer");
			xmlhttp.open("GET", serverPage);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) obj.innerHTML = xmlhttp.responseText;
			}
			xmlhttp.send(null);
		}

		// hide GX button
		// if (form.elements['ProductId'].value==58) document.getElementById("gxbutton").style.display="none";
	}
	return false;
}

function addCardOrderToCart(productid, numcards, id) {
	var ts = new Date().getTime();

	if (numcards>0) {
		var serverPage = "/include/ajax_addtocart.php?productid="+productid+"&quantity="+numcards+"&cardorderid="+id+"&ts="+ts;

		var obj = document.getElementById("cartcontainer");
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) obj.innerHTML = xmlhttp.responseText;
		}
		xmlhttp.send(null);
	}
}

function addSoftwareToCart(form) {
	var ts = new Date().getTime();

	if (confirm("Are you sure you want to add this item to your cart?")) {
		if (form.elements['Quantity'].value>0) {
			var serverPage = "/include/ajax_addtocart.php?productid="+form.elements['ProductId'].value+"&quantity="+form.elements['Quantity'].value+"&ssn_id="+form.elements['SSNId'].value+"&ts="+ts;

			var obj = document.getElementById("cartcontainer");
			xmlhttp.open("GET", serverPage);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) obj.innerHTML = xmlhttp.responseText;
			}
			xmlhttp.send(null);
		}
	}
	return false;
}

function removeProductFromCart(id) {
	var ts = new Date().getTime();

	if (confirm("Are you sure you want to remove this item from your cart?")) {
		var serverPage = "/include/ajax_addtocart.php?itemid="+id+"&delete"+"&ts="+ts;

		var obj = document.getElementById("cartcontainer");
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) obj.innerHTML = xmlhttp.responseText;
		}
		xmlhttp.send(null);

		// show GX button
		// if just deleted GX document.getElementById("gxbutton").style.display="block";
	}
	return false;
}

/* -------------------- ip lookup -------------------- */

function ipLookup(ipaddress, objname) {
	xmlhttp.open("GET", "/include/ajax_iplookup.php?ip="+ipaddress);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) objname.innerHTML = xmlhttp.responseText;
	}
	xmlhttp.send(null);
}

/* -------------------- generic autocomplete -------------------- */

function autoComplete(thevalue, elementname, formtype) {
	var ts = new Date().getTime();

	var theObject = document.getElementById(elementname);
	if (thevalue.length>2) {

		theObject.style.display = "block";
		if (formtype!="ssn") document.getElementById("adduserid").value = "";

		var addvar="";

		// differentiate between the different forms here.
		if (formtype=="ltd") addvar="&ltd";
		else if (formtype=="support") addvar="&support";
		else if (formtype=="ssn") addvar="&ssn";
		else addvar="";

		var serverPage = "/include/ajax_autocomplete.php?str="+thevalue+addvar+"&t="+ts;

		var obj = document.getElementById(elementname);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) obj.innerHTML = xmlhttp.responseText;
		}
		xmlhttp.send(null);
	}
	else {
		theObject.style.display = "none";

		if (formtype=="ssn") document.getElementById("CdIcon").innerHTML = "";
	}
}

/* -------------------- username autocomplete -------------------- */

function setValue (value1, value2) {
	var theObject = document.getElementById("autocompletediv");

	// hide name list
	theObject.style.display = "none";

	// populate form
	document.getElementById("addusername").value = value1;
	document.getElementById("adduserid").value = value2;
}

/* -------------------- ICTicket autocomplete -------------------- */

function setSupportValue (value1, value2, value3, value4) {
	var theObject = document.getElementById("autocompletediv");

	// hide name list
	theObject.style.display = "none";

	// populate form
	document.getElementById("addusername").value = value1;
	document.getElementById("adduserid").value = value2;
	if (value3>0) {
		document.getElementById("TicketIcon").innerHTML = "<a href='index.php?id=168&searchtype=desc&search="+value4+"' title='User has "+value3+" tickets'>"+value3+"</a>";
		document.getElementById("TicketIcon").style.display="inline";
	}
	else document.getElementById("TicketIcon").style.display="none";
}

function setCdKey (value1, value2) {
	var theObject = document.getElementById("autocdkey");
	theObject.style.display = "none";
	document.getElementById("CdKey").value = value1;
	document.getElementById("CdIcon").innerHTML = "<a href='index.php?id=150&ssn="+value1.replace(/-/g, "")+"' target='_blank'><img src='images/"+value2+"'></a>";
}

/* -------------------- Card Configurator -------------------- */

function checkCardType(obj) {
	var ts = new Date().getTime();
	var objval=obj.value;

	if (objval=="") {
		// if no card selected, hide all other steps
		document.getElementById("step2a").style.display="none";
		document.getElementById("step2b").style.display="none";
		document.getElementById("step2c").style.display="none";
		document.getElementById("step3").style.display="none";
		document.forms['config'].elements['Card1Format'].selectedIndex="";
	}
	else {
		xmlhttp.open("GET", "/include/ajax_cardtype.php?id="+objval+"&ts="+ts);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var reply = xmlhttp.responseText;									// reply is in the format xx where 0=categoryid and 1=dualformat (Y/N)
				if (reply!="0") {													// if card selected, display step 2
					document.getElementById("step2a").style.display="block";
					if (reply.charAt(1)=="Y") {										// if reponse==2 then card is dual format. display dual format checkbox + list
						document.getElementById("step2b").style.display="block";
						if (document.forms['config'].elements['DualFormat'].checked) document.getElementById("step2c").style.display="block";
						else document.getElementById("step2c").style.display="none";
					}
					else {															// if reponse==1 then card is not dual format
						document.getElementById("step2b").style.display="none";
						document.getElementById("step2c").style.display="none";
					}
				}
				/* else {
					document.getElementById("step2a").style.display="none";
					document.getElementById("step2b").style.display="none";
					document.getElementById("step2c").style.display="none";
					document.getElementById("step3").style.display="none";
				} */

				if (reply.charAt(0)=="1") {			// printable cards
					document.getElementById("step4card").style.display="block";
					document.getElementById("step4tag").style.display="none";
					document.getElementById("step4disc").style.display="none";
				}
				else if (reply.charAt(0)=="2") {	// key tags
					document.getElementById("step4card").style.display="none";
					document.getElementById("step4tag").style.display="block";
					document.getElementById("step4disc").style.display="none";
				}
				else {								// ??
					document.getElementById("step4card").style.display="none";
					document.getElementById("step4tag").style.display="none";
					document.getElementById("step4disc").style.display="block";
				}
			}
		}
		xmlhttp.send(null);
	}
}

/* -------------------- Online Exams -------------------- */

function updateExamTime(starttime, endtime, objname) {
	var ts = new Date().getTime();
	xmlhttp.open("GET", "/include/ajax_examtime.php?start="+starttime+"&end="+endtime+"&t="+ts);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) objname.innerHTML = xmlhttp.responseText;
	}
	xmlhttp.send(null);
}

/* -------------------- Header Search -------------------- */

function autoSearch(thevalue) {
	var theObject = document.getElementById("autosearch");
	if (thevalue.length>=2) {

		theObject.style.display = "block";

		var serverPage = "/include/ajax_autosearch.php?str="+thevalue;

		var obj = document.getElementById("autosearch");
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) obj.innerHTML = xmlhttp.responseText;
		}
		xmlhttp.send(null);
	}
	else theObject.style.display = "none";
}

/* -------------------- Follow Article -------------------- */

function followArticle(id) {
	var serverPage = "/include/ajax_followarticle.php?id="+id;

	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		var response=xmlhttp.responseText;
		if (response=="0") document.getElementById("followbutton").src="/images/subscribe.png";
		else document.getElementById("followbutton").src="/images/unsubscribe.png";
	}
	xmlhttp.send(null);
	return false;
}

/* -------------------- Follow RMA -------------------- */

function followRMA(id) {
	var serverPage = "/include/ajax_followrma.php?id="+id;

	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		var response=xmlhttp.responseText;
		if (response=="0") document.getElementById("followlink").innerHTML="Follow this RMA";
		else document.getElementById("followlink").innerHTML="Stop following this RMA";
	}
	xmlhttp.send(null);
	return false;
}


