//=================================================================================
// MERCENT RETAIL
// Yahoo! Storefront HTML Parsing JavaScript
// For use with the Mercent Retail Tracking & Conversion JavaScript
// Browsers Supported: Microsoft Internet Explorer and Mozilla-based
//
// http://www.mercent.com
// Copyright 2005-2006 Mercent Corporation. All rights reserved.
//=================================================================================

function mr_yahoo_getOrderValues()
{
	var mr_yahoo_all;

	if(document.body.innerText) // IE, Opera, Safari
	{
	    /* Get matches
	    *   Subtotal:\s*\${0,1}.*\d+\.\d\d 
	    *       -- checks for the word to extract
	    *       -- followed by any number of space characters
	    *       -- an optional $ sign
	    *       -- finally, any characters preceeding a number match of #.##
	    *   NOTE: This expression is exactly the same for shipping and tax, just the hard coded value is changed
	    *
	    *   Order\s+Number:\s*.+\S
	    *       -- checks for the word to extract
	    *       -- followed by any number of space characters and characters
	    *
	    *   Bill\s+To.*\s*[\w\s\.,\/?'";:\\|\[\]\{\}=+-_\)\(*&^%$#@!`~]+[a-zA-Z]{2}\s+[0-9]{5}
	    *       -- checks for the word to extract
	    *       -- followed by any number of space characters and characters
	    *       -- ending with a two letters and a zip code
	    */
		mr_yahoo_all = document.body.innerText.match(/(Subtotal\s*:\s*\${0,1}.*\d+\.\d\d)|(Shipping\s*:\s*\${0,1}.*\d+\.\d\d)|(Tax\s*:\s*\${0,1}.*\d+\.\d\d)|(Order\s+Number\s*:\s*.+\S)|(Bill\s+To.*\s*[\w\s\.,\/\?'";:\\|\[\]\{\}=\+-_\)\(\*&^%$#@!`~]+[a-zA-Z]{2}\s+[0-9]{5})/gi);
	}
	else // Firefox, Netscape
	{
	    // Replace <br> with \n, needed for RegExp functionality
		mr_yahoo_all = document.body.innerHTML.replace(/<br>/gi,'\n');
		
		// Remove all html tags
		mr_yahoo_all = mr_yahoo_all.replace(/(<[^>]+>)/g,'');
		
		/* Get matches
	    *   Subtotal:\s*\${0,1}.*\d+\.\d\d 
	    *       -- checks for the word to extract
	    *       -- followed by any number of space characters
	    *       -- an optional $ sign
	    *       -- finally, any characters preceeding a number match of #.##
	    *   NOTE: This expression is exactly the same for shipping and tax, just the hard coded value is changed
	    *
	    *   Order\s+Number:\s*.+\S
	    *       -- checks for the word to extract
	    *       -- followed by any number of space characters and characters
	    *
	    *   Bill\s+To[\w\s\.,\/?'";:\\|\[\]\{\}=+-_\)\(*&^%$#@!`~]+[a-zA-Z]{2}\s+[0-9]{5}
	    *       -- checks for the word to extract
	    *       -- followed by any number of space characters and characters
	    *       -- ending with a two letters and a zip code
	    */
		mr_yahoo_all = mr_yahoo_all.match(/(Subtotal\s*:\s*\${0,1}.*\d+\.\d\d)|(Shipping\s*:\s*\${0,1}.*\d+\.\d\d)|(Tax\s*:\s*\${0,1}.*\d+\.\d\d)|(Order\s+Number\s*:\s*.*)|(Bill\s+To[\w\s\.,\/\?'";:\\|\[\]\{\}=\+-_\)\(\*&^%$#@!`~]+[a-zA-Z]{2}\s+[0-9]{5})/gi);
	}
	
	// Needed to check for Opera browser, handles regular expressions differently
	if(navigator.userAgent.toLowerCase().indexOf('opera') != -1)
	{
	    // Loop through results and assign then to the variables
		for(var i = 0; i < mr_yahoo_all.length; i++)
		{
			if(mr_yahoo_all[i].match(/Order\s+Number/i))
			{
				mr_yahoo_orderNumber = mr_yahoo_all[i].match(/[\w-\.\{\}_\+=]+$/);
			}

			if(mr_yahoo_all[i].match(/[a-zA-Z]{2}\s+[0-9]{5}/))
			{
				mr_yahoo_postalCode = mr_yahoo_all[i].match(/[0-9]{5}$/);
			}

			if(mr_yahoo_all[i].match(/Subtotal/i))
			{
				mr_yahoo_subtotal = mr_yahoo_getDollarAmount(mr_yahoo_all[i].match(/Subtotal\s*:\s*\${0,1}.*\d+\.\d{2}/).toString());
			}

			if(mr_yahoo_all[i].match(/Shipping/i))
			{
				mr_yahoo_shipping = mr_yahoo_getDollarAmount(mr_yahoo_all[i].match(/Shipping\s*:\s*\${0,1}.*\d+\.\d{2}/).toString());
			}

			if(mr_yahoo_all[i].match(/Tax/i))
			{
				mr_yahoo_tax = mr_yahoo_getDollarAmount(mr_yahoo_all[i].match(/Tax\s*:\s*\${0,1}.*\d+\.\d{2}/).toString());
			}
		}
	}
	else
	{
	    // Loop through results and assign then to the variables
		for (var i = 0; i < mr_yahoo_all.length; i++)
		{
			switch (mr_yahoo_all[i].match(/(^\w+\s+\w+)|(^\w+)/ig)[0])
			{
				case 'Order Number':
					mr_yahoo_orderNumber = mr_yahoo_all[i].match(/[\w-\.\{\}_\+=]+$/);
					break;
				case 'Subtotal':
					mr_yahoo_subtotal = mr_yahoo_getDollarAmount(mr_yahoo_all[i]);
					break;
				case 'Shipping':
					mr_yahoo_shipping = mr_yahoo_getDollarAmount(mr_yahoo_all[i]);
					break;
				case 'Tax':
					mr_yahoo_tax = mr_yahoo_getDollarAmount(mr_yahoo_all[i]);
					break;
				case 'Bill To':
					mr_yahoo_postalCode = mr_yahoo_all[i].match(/[0-9]{5}$/);
					break;
			}
		}
	}

	function mr_yahoo_getDollarAmount(mr_yahoo_regExpMatch)
	{
	    // Remove any commas in the number and return it
		return mr_yahoo_regExpMatch.replace(/,/g, '').match(/\d*\.\d{2}/);
	}

	// TESTING: To See Values:
	//alert('Subtotal: ' + (mr_yahoo_subtotal ? mr_yahoo_subtotal.toString() : "0") + '\nPostal Code: ' + (mr_yahoo_postalCode ? mr_yahoo_postalCode.toString() : "") + '\nShipping: ' + (mr_yahoo_shipping ? mr_yahoo_shipping.toString() : "0") + '\nTax: ' + (mr_yahoo_tax ? mr_yahoo_tax.toString() : "0") + '\nOrder Number: ' + (mr_yahoo_orderNumber ? mr_yahoo_orderNumber.toString() : ""));

	mr_isIncomplete = "1";
	mr_conv["type"] = "order";
	mr_conv["tax"] = mr_yahoo_tax ? mr_yahoo_tax.toString() : "0";
	mr_conv["shipping"] = mr_yahoo_shipping ? mr_yahoo_shipping.toString() : "0";
	mr_conv["amount"] = mr_yahoo_subtotal ? mr_yahoo_subtotal.toString() : "0";
	mr_conv["id"] = mr_yahoo_orderNumber ? mr_yahoo_orderNumber.toString() : "";
	mr_conv["postalCode"] = mr_yahoo_postalCode ? mr_yahoo_postalCode.toString() : "";
	mr_conv["customerId"] = ""; // Can not get
	mr_conv["countryCode"] = ""; // Can not get
	mr_conv["discount"] = "0"; // Can not get
	
	mr_sendConversion();
}

// Tracking variables
var mr_yahoo_subtotal, mr_yahoo_shipping, mr_yahoo_tax, mr_yahoo_orderNumber, mr_yahoo_postalCode;

// Check to see if regular expressions are supported by browser
if(window.RegExp)
{
	window.onload = mr_yahoo_getOrderValues;
}