/**
 * eCart Client Functions Library
 * @author Cody Adkins (code018)
 * December 20,2010
 */
 
 
 /**
  * Update the Inner HTML or value of an element with the price.
  * @param {Object} element
  * @param {Object} html
  * @param {Object} value
  */
 function eCart_updateTotal(element,html,value){
	if(html)
	document.getElementById(element).HTML=value;
	else
	document.getElementById(element).value=value;
 }
 
  /**
  * Update the Inner HTML or value of an element with the item count.
  * @param {Object} element
  * @param {Object} html
  * @param {Object} value
  */
 function eCart_updateItemTotal(element,html,value){
 	if(html)
	document.getElementById(element).HTML=value;
	else
	document.getElementById(element).value=value;
 }
 
 
 function eCartBuyAlbum(id){
 	var url='./eCart/eCart_Processor.php?a=addAlbum&id='+id;
	//alert(url);
 	var xmlhttp = new GetXmlHttp();
 	//now we got the XmlHttpRequest object, send the request.
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
					if (xmlhttp.status == 200) {
						if (debug) {
							alert(xmlhttp.responseText);
						}
						//do the action
						alert("The album was added to cart sucessfully.");
						updateTotalItems();
						getTotal();
					}
					else 
						if (debug) {
							//document.Write(xmlhttp.responseText);
						}
				}
			}
			xmlhttp.open("GET", url, true);
			xmlhttp.send(null);
		}
	}
	
	function eCartBuySong(id){
 	var url='./eCart/eCart_Processor.php?a=addSong&id='+id;
	//alert(url);
 	var xmlhttp = new GetXmlHttp();
 	//now we got the XmlHttpRequest object, send the request.
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
					if (xmlhttp.status == 200) {
						if (debug) {
							alert(xmlhttp.responseText);
						}
						//do the action
						//alert("The Song was added to cart sucessfully.");
						document.getElementById('eCartSong'+id).href='javascript:eCartRemoveSong('+id+')';
						document.getElementById('eCartSong'+id).innerHTML="<img src='eCart/images/removenow_button.png' alt='Remove Song!' border='0'>";
						updateTotalItems();
						getTotal();
					}
					else 
						if (debug) {
							//document.Write(xmlhttp.responseText);
						}
				}
			}
			xmlhttp.open("GET", url, true);
			xmlhttp.send(null);
		}
	}
	
	function eCartRemoveSong(id){
 	var url='./eCart/eCart_Processor.php?a=removeSong&id='+id;
	//alert(url);
 	var xmlhttp = new GetXmlHttp();
 	//now we got the XmlHttpRequest object, send the request.
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
					if (xmlhttp.status == 200) {
						if (debug) {
							alert(xmlhttp.responseText);
						}
						//do the action
						//alert("The song was removed sucessfully.");
						document.getElementById('eCartSong'+id).href='javascript:eCartBuySong('+id+')';
						document.getElementById('eCartSong'+id).innerHTML="<img src='images/buynow.gif' alt='Buy Song!' border='0'>";
						updateTotalItems();
						getTotal();
					}
					else 
						if (debug) {
							//document.Write(xmlhttp.responseText);
						}
				}
			}
			xmlhttp.open("GET", url, true);
			xmlhttp.send(null);
		}
	}
	
	
	function eCartEmptyCart(){
 	var url='./eCart/eCart_Processor.php?a=emptyCart&id=0';
	//alert(url);
 	var xmlhttp = new GetXmlHttp();
 	//now we got the XmlHttpRequest object, send the request.
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
					if (xmlhttp.status == 200) {
						if (debug) {
							alert(xmlhttp.responseText);
							updateTotalItems();
						}
						//do the action
						alert("There is nothing in your cart.");
						updateTotalItems();
						getTotal();
						window.location.href=window.location.href;
					}
					else 
						if (debug) {
							//document.Write(xmlhttp.responseText);
						}
				}
			}
			xmlhttp.open("GET", url, true);
			xmlhttp.send(null);
		}
	}
	
	function updateTotalItems(){
 	var url='./eCart/eCart_Processor.php?a=totalItems&id=0';
	//alert(url);
 	var xmlhttp = new GetXmlHttp();
 	//now we got the XmlHttpRequest object, send the request.
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
					if (xmlhttp.status == 200) {
						if (debug) {
							alert(xmlhttp.responseText);
						}
						//do the action
						//alert(xmlhttp.responseText);
						document.getElementById('eCartItemCount').innerHTML=xmlhttp.responseText;
						getTotal();
					}
					else 
						if (debug) {
							//document.Write(xmlhttp.responseText);
						}
				}
			}
			xmlhttp.open("GET", url, true);
			xmlhttp.send(null);
		}
	}
	
	
	function getTotal(){
 	var url='./eCart/eCart_Processor.php?a=getTotal&id=0';
	//alert(url);
 	var xmlhttp = new GetXmlHttp();
 	//now we got the XmlHttpRequest object, send the request.
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
					if (xmlhttp.status == 200) {
						if (debug) {
							alert(xmlhttp.responseText);
						}
						//do the action
						//alert(xmlhttp.responseText);
						document.getElementById('eCartTotal').innerHTML=xmlhttp.responseText;
					}
					else 
						if (debug) {
							//document.Write(xmlhttp.responseText);
						}
				}
			}
			xmlhttp.open("GET", url, true);
			xmlhttp.send(null);
		}
	}
