// Ajax Functions
var xmlHttp;

function GetXmlHttpObject() {
	var xmlHttp=null;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

function sendHttpRequest(url,callbackFunc,respXml) {
	var xmlobj = null;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlobj = new XMLHttpRequest();
	} catch(e) {
		//Internet Explorer
		try {
			xmlobj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				alert('AJAX is not supported by your browser!');
				return false;
			}
		}
	}

	xmlobj.onreadystatechange = function() {
		if(xmlobj.readyState == 4) {
			if(xmlobj.status == 200) {
				respXml ? eval(callbackFunc+'(xmlobj.responseXML)') : eval(callbackFunc+'(xmlobj.responseText)');
			}
		}
	}

	// open socket connection
	xmlobj.open('GET',url,true);
	// send http header
	xmlobj.setRequestHeader('Content-Type','text/plain;charset=UTF-8');
	// send http request
	xmlobj.send(null);
}

function htmlData(url, qStr) {
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	if (url.length==0) {
		document.getElementById("divCalendar").innerHTML="";
		return;
	}

	var url = url + "?" + qStr;
	url = url + "&sid=" + Math.random();
	xmlHttp.onreadystatechange = eventChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function eventChanged() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
		document.getElementById("divCalendar").innerHTML = xmlHttp.responseText;
	}
}

function deleteSponsor(str) { 
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="delSponsor.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateSponsorChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateSponsorChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("txtSHint").innerHTML=xmlHttp.responseText;
	} 
}

function deleteDetail(str) { 
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="delDetail.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateDetailChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateDetailChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("txtDHint").innerHTML=xmlHttp.responseText;
	} 
}

function getCartResults() {
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request")
		return
	} 

	var strHref = getURL();
	var url=strHref + "/store/library/getCartResults.php";
	xmlHttp.onreadystatechange=stateResultsChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateResultsChanged() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
		document.getElementById("cartResults").innerHTML = xmlHttp.responseText;
	} 
}

function getCartTotals() {
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request")
		return
	} 

	var strHref = getURL();
	var url=strHref + "/store/library/totalCart.php";
	xmlHttp.onreadystatechange=stateTotalsChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateTotalsChanged() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { 
		document.getElementById("showTotals").innerHTML = xmlHttp.responseText;
	} 
}

// display featured products
function displayFeatures(featureData){
	// parse banner data
	var features	= featureData.split("~");
	var featureCont	= document.getElementById('featurecontainer');

	if(!featureCont){return};

	// clean up feature container
	if (features.length > 1) {
		featureCont.innerHTML = '';
	} else {
		featureCont.innerHTML = '<h4>No Featured Products Available</h4>';
	}

	for (i=0; i<features.length-1; i++) {
		var featureKey		= features[i].split('|')[0];
		var categoryKey		= features[i].split('|')[1];
		var productKey		= features[i].split('|')[2];
		var productName		= features[i].split('|')[3];
		var productImg		= features[i].split('|')[4];
		var productThumb	= features[i].split('|')[5];

		if(!productThumb){return};

		// create feature link
		var a = document.createElement('a');
		a.setAttribute('href',"/store/index.php?v=item&c=" + categoryKey + "&p=" + productKey);
		// create feature image
		var img = document.createElement('img');
		// set feature image dimensions
		img.setAttribute('src',productThumb);
		img.setAttribute('alt', productName);
		img.setAttribute('border', 0);
		//img.setAttribute('width',96);
		//img.setAttribute('height',106);
		//img.setAttribute('vspace', 1);
		img.setAttribute('hspace', 20);
		// append feature image to link
		a.appendChild(img);
		// append feature link to container
		featureCont.appendChild(a);
	}

	// fetch feature recursively 
	strHref = getURL();
	setTimeout("sendHttpRequest('" + strHref + "/store/features.php','displayFeatures')",10*1000);
}

// display featured products
function displaySpecials(specialData){
	// parse banner data
	var specials	= specialData.split("~");
	var specialCont	= document.getElementById('specialcontainer');

	if(!specialCont){return};

	// clean up special container
	if (specials.length > 1) {
		specialCont.innerHTML = '';
	} else {
		specialCont.innerHTML = '<h4>No Special Products Available</h4>';
	}

	for (i=0; i<specials.length-1; i++) {
		var specialKey		= specials[i].split('|')[0];
		var categoryKey		= specials[i].split('|')[1];
		var productKey		= specials[i].split('|')[2];
		var productName		= specials[i].split('|')[3];
		var productImg		= specials[i].split('|')[4];
		var productThumb	= specials[i].split('|')[5];
		var specialPrice	= specials[i].split('|')[6];

		if(!productThumb){return};

		// create special link
		var a = document.createElement('a');
		a.setAttribute('href',"/store/index.php?v=item&c=" + categoryKey + "&p=" + productKey);
		// create special image
		var img = document.createElement('img');
		// set special image dimensions
		img.setAttribute('src',productThumb);
		img.setAttribute('alt', productName);
		img.setAttribute('border', 0);
		//img.setAttribute('width',96);
		//img.setAttribute('height',106);
		//img.setAttribute('vspace', 1);
		img.setAttribute('hspace', 20);
		// append special image to link
		a.appendChild(img);
		// append special link to container
		specialCont.appendChild(a);

		var d = document.createElement('div');
		d.innerHTML = '<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;&nbsp;&nbsp;&nbsp;' + specialPrice + '</p>';
		specialCont.appendChild(d);
	}

	// fetch special recursively 
	strHref = getURL();
	setTimeout("sendHttpRequest('" + strHref + "/store/specials.php','displaySpecials')",10*1000);
}

