/**
 * @author Rich Jones
 */

var gXMLDoc;


function loadXMLDoc(fname){
	
	var xmlDoc = null;
	
	req = false;
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
		} 
		catch (e) {
			alert(e);
			req = false;
		}
	}
	else 
		if (window.ActiveXObject) {
			try {
				// Note: for some reason IE is not loading or liking
				// the Doc after is is loaded using the XMLHttpRequest.
				// Making the call to the old Doc.load call for now.
				// Todo: deal with this problem.
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = false;
				xmlDoc.load(fname);
				return xmlDoc;
			} 
			catch (e) {
				try {
					req = new ActiveXObject("Msxml2.XMLHTTP");
				} 
				catch (e) {
					try {
						req = new ActiveXObject("Microsoft.XMLHTTP");
					} 
					catch (e) {
						alert(e);
						req = false;
					}
				}
			}
		}
	
	if (req) {
		// calling open XMLHttpRequest
		req.open("GET", fname, false);
		req.send("");
//		if (checkXMLLoadRequest(req)) {
			xmlDoc = req.responseXML;
//		}
	}

	return xmlDoc;
}

function checkXMLLoadRequest(req) {
	if (req.readyState == 4) {
		// only if "OK" (status of zero is odd, but it seems to work
		alert('req.status = ' + req.status)
		if (req.status == 200 || req.status == 0) {
			return true;
		}
		else 
			if (request.status == 404) {
				alert("Products data file not found.");
			}
			else {
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
	}
	return false;	
}

/*
Note: old code for loading XML data - this will not work with Safari as
      Safari does not implement the Doc.Load method.
	// code for IE
	if (window.ActiveXObject) {
		alert('creating ActiveXObject')
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	} else
	// code for Mozilla, Firefox, Opera, etc.
	if (document.implementation && document.implementation.createDocument) {
		alert('creating document.implementation.createDocument')
		try {
			xmlDoc = document.implementation.createDocument("", null, null);
		} 
		catch (e) {
			alert('unable to create xmldoc. error: ' + e);
		}
		alert('created document.implementation.createDocument')
	} else {
		alert('Your browser cannot handle this script');
	}
	alert('assigning async')
	xmlDoc.async = false;
	alert('calling load')
	xmlDoc.load(fname);
	alert('loaded')
	
	if (xmlDoc == null) {
		alert('Your browser cannot handle this script');
		abort;
	}
	return xmlDoc;
}
*/


/*
Note: this function would be used in the case the XMLHttpRequest is used asynchronously  
function processReqChange(){
	// only if req shows "loaded" 
	if (req.readyState == 4) {
		// only if "OK" 
		if (req.status == 200) {
			// ...processing statements go here... 
			alert("everything ok");
			gXMLDoc = req.responseXML;
			gFileLoaded = true;
			if (gXMLDoc == null) {
				alert('gXMLDoc is null')
				abort;
			}
		}
		else 
			if (request.status == 404) {
				alert("Products data file not founf.");
			}
			else {
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
				abort;
			}
	}
}
*/

function loadProducts() {
	if (gXMLDoc == null) {
		gXMLDoc = loadXMLDoc("data/vvproducts.xml");		
	}	
	if (gXMLDoc == null) {
		alert('did not find product database');	
		abort;
	}
}

function findItem(aID) {
	loadProducts();
	
	var n = gXMLDoc.getElementsByTagName('id');
	if (n == null) {
		alert('Invalid Products database.');	
		return -1;	
	}	

	for(i = 0; i < n.length; i++) {
		var lID = n[i].childNodes[0].nodeValue;
		
		if (lID==aID) {
			return i
		}
 	}
	
	alert('Item not found: ' + aID);	
	return -1;	
}

function getValue(aNum, aName) {
	var s = null;
	var e = gXMLDoc.getElementsByTagName(aName)[aNum];
	if (e.hasChildNodes()) {
		s = e.childNodes[0].nodeValue; 
	}
	return s;
}

function getName(aNum) {
	return getValue(aNum, "name");
}

function getPrice(aNum) {
	return getValue(aNum, "price");
}

function getShipping(aNum) {
	return getValue(aNum, "shipping");
}

function getShippingAdditional(aNum) {
	return getValue(aNum, "shippingadditional");
}

function getImage(aNum) {
	var a = new Array();
	a[0] = getValue(aNum, "imagesmall");
	a[1] = getValue(aNum, "image");
	return a;
}

function getDesc(aNum) {
	return getValue(aNum, "desc");
}

function getNote(aNum) {
	var s = null;
	var e = gXMLDoc.getElementsByTagName("notes")[aNum];
	if (e.hasChildNodes()) {
		s = e.childNodes[0].nodeValue; 
	}
	return s;	
}

function getInStock(aNum) {
	var b = getValue(aNum, "instock");
	if (b.toLowerCase() == "true") {
		return true;
	} else {
		return false;
	}
}

function addProductItemBuyButton(sProductID) {

	var lItem = findItem(sProductID);
	if (lItem == -1) {
		alert('Product item: ' + sProductID + ' not found in database.');
		return;
	}

	var lName = getName(lItem);		
	var lPrice = getPrice(lItem);		
	var lShipping = getShipping(lItem);		
	var lShippingAdditional = getShippingAdditional(lItem);		

	// ADD TO CART button code.
	document.write('<form action="https://www.e-junkie.com/ecom/fgb.php?c=cart&cl=1&ejc=2" target="ej_ejc">');

	// paypal email(remove if not using PayPal) 
	document.write('<input type="hidden" name="business" value="admin@virtueandvalor.com"/>')

	// google merchant id (remove if not using Google Checkout)
	// document.write('<input type="hidden" name="merchant_id" value="404865209440851"/>');

	// site url
	document.write('<input type="hidden" name="site_url" value="http://virtueandvalor.com"/>');

	// contact email (where we can notify of the updates)
	document.write('<input type="hidden" name="contact_email" value="admin@virtueandvalor.com"/>');

	// item name
	document.write('<input type="hidden" name="item_name" value="' + lName + '"/>');

	// item number (should be different for each product)
	document.write('<input type="hidden" name="item_number" value="' + sProductID + '"/>');

	// item price
	document.write('<input type="hidden" name="amount" value="' + lPrice + '"/>');

	// initial quantity - note: always 1
	document.write('<input type="hidden" name="quantity" value="1"/>');

	// shipping cost
	document.write('<input type="hidden" name="shipping" value="' + lShipping + '">');

	// shipping cost of each additional unit
	document.write('<input type="hidden" name="shipping2" value="' + lShippingAdditional + '">');

	// handling cost - note: always 0
	document.write('<input type="hidden" name="handling" value="0">');

	// tax (flat amount, NOT percentage) - note: always 0
	document.write('<input type="hidden" name="tax" value="0.00"/>');
	
	// you thank you page
	document.write('<input type="hidden" name="return_url" value="http://www.e-junkie.com/"/>');

	// any custom info you want to pass
	// document.write('<input type="hidden" name="custom" value="anything"/>

	// currency (USD for Google Checkout USA, GBP for Google Checkout UK. 
	// For PayPal: any currency that PayPal supports
	document.write('<input type="hidden" name="currency_code" value="USD"/>');
	document.write('<input type="image" src="images/addtocartbutton.gif" border="0" ');
	document.write('onClick="javascript:return EJEJC_frm(this.parentNode);">');
	document.write('</form>');
}

function addViewCartButton() {
	// VIEW CART button code. 
	document.write('<a href="https://www.e-junkie.com/ecom/fgb.php?c=cart&cl=1&ejc=1&');
	document.write('business=admin@virtueandvalor.com" target="ej_ejc" class="ec_ejc_thkbx" ');
	document.write('onClick="javascript:return EJEJC_lc(this);"><img src="images/viewcartbutton.gif" border="0"></a>');
}

function EJEJC_lc(th) { return false; }
function EJEJC_config() {}
function EJEJC_shown() {}


