/*	AJAX FUNCTIONS
	primarily used for showing mini-listings (browse.php) and changing images w/o pageloads
 */

var req;
var selectedListingId = null;

function addToPaypalCart(listing_id)
{
	loadAjax();
	
	req.onreadystatechange = function ()
	{
		if (4 == req.readyState)
		{
			if (200 == req.status)
			{
				var xml = req.responseXML.documentElement;
				
				document.getElementById('paypal_amount_add').value = xml.getElementsByTagName('amount')[0].firstChild.nodeValue;
				document.getElementById('paypal_business_add').value = xml.getElementsByTagName('business')[0].firstChild.nodeValue;
				if ('' == xml.getElementsByTagName('store_front')[0].firstChild.nodeValue)
				{
					document.getElementById('paypal_item_name_add').value = 'CozyBug.com: ' + xml.getElementsByTagName('item_name')[0].firstChild.nodeValue;
				}
				else
				{
					document.getElementById('paypal_item_name_add').value = 'CozyBug.com: ' + xml.getElementsByTagName('store_front')[0].firstChild.nodeValue + ' - ' + xml.getElementsByTagName('item_name')[0].firstChild.nodeValue;
				}
				
				document.add_to_cart.submit();
			}
			else
			{
				alert('There was a problem adding that item to your cart.  Please try again.');
			}
		}
	}
	req.open('GET', 'xml/get_listing_paypal.php?id=' + listing_id, true);
	req.send(null);
}

function viewPaypalCart(user_id)
{
	loadAjax();
	
	req.onreadystatechange = function ()
	{
		if (4 == req.readyState)
		{
			if (200 == req.status)
			{
				var xml = req.responseXML.documentElement;
				
				document.getElementById('paypal_business_view').value = xml.getElementsByTagName('business')[0].firstChild.nodeValue;
				
				document.view_cart.submit();
			}
			else
			{
				alert('There was a problem viewing your cart.  Please try again.');
			}
		}
	}
	req.open('GET', 'xml/get_user_paypal.php?id=' + user_id, true);
	req.send(null);
}

function loadAjax()
{
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function loadXMLDoc(url) {
	loadAjax();
	
	req.onreadystatechange = processReqChange;
	req.open("GET", url, true);
	req.send(null);
}

function processReqChange() {
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			var response  = req.responseXML.documentElement;
			
			if(req.responseText.indexOf("<cbrate>") > -1){
				// GET CALCULATED RATE
				calcualtedTotal = response.getElementsByTagName('cbrate')[0].childNodes[0].nodeValue;
				document.getElementById("calculatedPrice").value = calcualtedTotal;
				document.getElementById("calculatedAmount").innerHTML = calcualtedTotal;
				//document.getElementById("cbComment").innerHTML = response.getElementsByTagName('comment')[0].childNodes[0].nodeValue;
				//alert(calculatedTotal);
				
			} else {
				// UPDATE LISTING INLINE
				response  = req.responseXML.documentElement;
				xmlId = 			response.getElementsByTagName('lId')[0].childNodes[0].nodeValue;
				xmlTitle = 			response.getElementsByTagName('lTitle')[0].childNodes[0].nodeValue;
				xmlDescription = 	response.getElementsByTagName('lDescription')[0].childNodes[0].nodeValue;
				xmlDate = 			response.getElementsByTagName('lDate')[0].childNodes[0].nodeValue;
				xmlPrice = 			response.getElementsByTagName('lPrice')[0].childNodes[0].nodeValue;
				xmlSeller = 		response.getElementsByTagName('lSeller')[0].childNodes[0].nodeValue;
				xmlContact = 		response.getElementsByTagName('lContact')[0].childNodes[0].nodeValue;
				xmlPaypal =			response.getElementsByTagName('lPaypal')[0].childNodes[0].nodeValue;
					
				// show loading graphic first
				document.getElementById("mainImg").src = "images/loading.gif";

				// put xml values into html elements
				document.getElementById("lTitle").innerHTML = xmlTitle;
				document.getElementById("lDescription").innerHTML = xmlDescription;
				if(xmlPrice.indexOf("$") > (-1)) document.getElementById("lPrice").innerHTML = "Price: " + xmlPrice;
				else document.getElementById("lPrice").innerHTML = "Location: " + xmlPrice;
				document.getElementById("lSeller").innerHTML = "Seller: " + xmlSeller;
				document.getElementById("lDate").innerHTML = "<hr class=\"dotted\" />Sale Date: " + xmlDate;
				document.getElementById("lContact").innerHTML = xmlContact;
				document.getElementById("lPaypal").innerHTML = xmlPaypal + '<p>&nbsp;</p>';
				
				xmlMainImage = 		response.getElementsByTagName('lMainImage')[0].firstChild.data;
				xmlImage1 = 		response.getElementsByTagName('ltImage1')[0].firstChild.data;
				xmlImage2 = 		response.getElementsByTagName('ltImage2')[0].firstChild.data;
				xmlImage3 = 		response.getElementsByTagName('ltImage3')[0].firstChild.data;
				xmlImage4 = 		response.getElementsByTagName('ltImage4')[0].firstChild.data;
				
				if (xmlMainImage == "noImage.gif")
				{
					document.getElementById("mainImg").src = "images/noImage.gif";
				}
				else if (0 == xmlMainImage.indexOf("http://"))
				{
					document.getElementById("mainImg").src = xmlMainImage;
				}
				else
				{
					document.getElementById("mainImg").src = "uploads/listings/" + xmlId + "/" + xmlMainImage;
				}
			
				// up to 4 possible thumbs below main image
				for(i=1; i<5; i++){
					if(i==1) xmlImage = xmlImage1; else 
					if(i==2) xmlImage = xmlImage2; else
					if(i==3) xmlImage = xmlImage3; else
					if(i==4) xmlImage = xmlImage4;
				
					if(xmlImage != "null" && (i==1 && xmlImage2 != "null" || i>1)){
						xmlImg = "uploads/listings/" + xmlId + "/" + xmlImage;
						xmlHtml = "<img src=\"" + xmlImg + "\" width=\"48\" height=\"48\" class=\"productThumb\" onmouseover=\"javascript:changePhoto('mainImg','" + xmlImg + "');\">";
						document.getElementById("tDiv"+i).innerHTML = xmlHtml;
				
					} else {
						document.getElementById("tDiv"+i).innerHTML = "&nbsp;";
					}
				}
				
				if ('1' == response.getElementsByTagName('lDetail')[0].firstChild.data)
				{
					xmlAvatar = response.getElementsByTagName('lAvatar')[0].firstChild.data;
					
					if ('' != xmlAvatar)
					{
						document.getElementById('lAvatar').innerHTML = '<img src="' + xmlAvatar + '" alt="Avatar" />';
					}
				}
				
				selectedListingId = xmlId;
				fixColumnHeight();
			}
			
		} else {
			alert("There was a problem retrieving data:\n" + req.statusText);
		}
	}
}


// SWITCH CLASS FOR SELECTED/DESELECTED IMAGE THUMB
function switchThumbClass(index, total)
{
	for (var i = 0; i < total; ++i)
	{
		selectedImageId = "listingThumb_" + i;
		
		if (document.getElementById(selectedImageId))
		{
			if (index == i)
			{
				document.getElementById(selectedImageId).className = "productThumb_sel";
			}
			else
			{
				document.getElementById(selectedImageId).className = "productThumb";
			}
		}
	}
}

// CHANGE MINI-VIEW IMAGE ON LISTING PAGE(S)
function changePhoto(srcId, newImg){
	document.getElementById(srcId).src = newImg.replace("_thumb.jpg",".jpg");
}

// CALCULATE COZYBUCKS TOTAL
function cozyBucks_calculate(){
	var calcualtedTotal = 0;
	var userInput = document.getElementById("other_amount").value;
	if(parseInt(userInput) < 1){
		alert("Other Amount must be 1 CozyBuck or greater.");
	} else {
		document.getElementById("radio_other").checked = true;
		loadXMLDoc("xml/get_cozybucks_rate.php?amount="+userInput);
	}
}

