function getURLparam(name)
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}
// handles add to cart links based on PromoID in URL
$(document).ready(function()
{
	var uid = new Date();
	uid = uid.getTime();
	var programID_url, product, upsell, pid, program_id, offer_id
	programID_url = getURLparam("id");
	$.ajax(
	{
		type: "GET",
		//url: "linkinfo.xml?"+uid,
		url: "http://store.trendmicro.com/DRHM/Storefront/Site/tmamer/pb/multimedia/linkinfo.xml?"+uid,
		dataType: "xml",
		success: function(xml)
		{
			$(xml).find("row").each(function()
			{
				if (programID_url == $(this).children("program_id").text()) // if promo ID in URL matches promo ID in xml set buy link value
				{
					product = $(this).children("product").text();
					pid = $(this).children("pid").text();
					program_id = $(this).children("program_id").text();
					offer_id = $(this).children("offer_id").text();
					
					// set prices based on renewal or trial -- renewal has discounted pricing
					if (getURLparam("type") == "renewal") { 
						tavyear = "09"; 
						if (program_id == '14065200' || program_id == '14065400' || program_id == '14065500' || program_id == '14065600' || program_id == '14066400' || program_id == '14066600' || program_id == '14066700' || program_id == '14066800') {
							tavprice = 39.95;
							tisprice = 49.95;
							tisproprice = 69.95;
						} else {
							tavprice = 31.95; 
							tisprice = 39.95; 
							tisproprice = 55.95;
						}
					} else { 
						tavyear = "08"; 
						tavprice = 39.95; 
						tisprice = 49.95; 
						tisproprice = 69.95;
					}
			
					// comparison chart prices
					$("#priceRow strong:eq(0)").html("$"+tavprice+"!");
					$("#priceRow strong:eq(1)").html("$"+tisprice+"!");
					$("#priceRow strong:eq(2)").html("$"+tisproprice+"!");

					if (product.indexOf("tav") != -1)
					{
						// redesigned pd page price content
							// radio button labels
							$("#radioWrapper label strong:eq(0)").html("$"+tavprice);
							$("#radioWrapper label strong:eq(1)").html("$"+tisprice);					
						
						// tm pd page price content
						$("li.price").html("$"+tavprice);

						// interstitial content
						$("#current").html('<img src="//drh.img.digitalriver.com/DRHM/Storefront/Site/tmamer/cm/images/tavtis/box_tav'+tavyear+'_small.jpg" alt="AntiVirus plus AntiSpyware" class="left" />				<p>AntiVirus plus AntiSpyware</p>				<p>$'+tavprice+'</p>');
						$("#upsell").html('<div class="left" style="padding: 0 0 50px 0;"><img src="//drh.img.digitalriver.com/DRHM/Storefront/Site/tmamer/cm/images/tavtis/box_tis09_1.gif" alt="Internet Security 2009"  /></div>				<p><strong>For only $'+(tisprice-tavprice).toFixed(2)+' more...</strong>					<ul>						<li>Secure your home network</li><li>Stop attacks on your PC from potentially dangerous websites</li><li>Prevent access to inappropriate websites</li><li>Protect your information from online theft</li>					</ul></p>				<p id="cta">					<strong>$'+tisprice+'</strong><br />					<a href="//store.trendmicro.com/store/tmamer/AddItemToRequisition/productID.'+pid+'/pgm.'+program_id+'/OfferID.'+offer_id+'">No thanks, proceed to checkout.</a><br />					<a href="//store.trendmicro.com/store/tmamer/AddItemToRequisition/productID.106226000/pgm.'+program_id+'/OfferID.'+offer_id+'"><img src="//drh.img.digitalriver.com/DRHM/Storefront/Site/tmamer/cm/images/tavtis/btn_powerupOrder.gif" alt="Yes, Powerup My Order!" /></a></p>');
					}
					else if (product.indexOf("tis") != -1)
					{
						// redesigned pd page price content
						$("#radioWrapper label strong:eq(0)").html("$"+tisprice);
						$("#radioWrapper label strong:eq(1)").html("$"+tisproprice);
						
						// tm pd page price content
						$("li.price").html("$"+tisprice);
						
						// interstitial content
						$("#current").html('<img src="//drh.img.digitalriver.com/DRHM/Storefront/Site/tmamer/cm/images/tavtis/box_tis09_small.jpg" alt="Internet Security 2009" class="left" />				<p>Internet Security 2009</p>				<p>$'+tisprice+'</p>');
						$("#upsell").html('<div class="left" style="padding: 0 0 50px 0;"><img src="//drh.img.digitalriver.com/DRHM/Storefront/Site/tmamer/cm/images/tavtis/box_tis09pro.gif" alt="Internet Security 2009 Pro"  /></div>				<p><strong>For only $'+(tisproprice-tisprice).toFixed(2)+' more...</strong>					<ul>						<li>Validate wireless hotspots and Wi-Fi networks</li>						<li>Prevent keystroke loggers from stealing your data</li>						<li>Remotely lock folders and files in case of computer theft</li>						<li>Clean up your PC for faster performance</li>						<li>Restore Windows after serious damage from malicious attacks</li>					</ul></p>				<p id="cta">					<strong>$'+tisproprice+'</strong><br />					<a href="//store.trendmicro.com/store/tmamer/AddItemToRequisition/productID.'+pid+'/pgm.'+program_id+'/OfferID.'+offer_id+'">No thanks, proceed to checkout.</a><br />					<a href="//store.trendmicro.com/store/tmamer/AddItemToRequisition/productID.106251000/pgm.'+program_id+'/OfferID.'+offer_id+'"><img src="//drh.img.digitalriver.com/DRHM/Storefront/Site/tmamer/cm/images/tavtis/btn_powerupOrder.gif" alt="Yes, Powerup My Order!" /></a></p>');
					}
					
					$("#buynow").click(function() // change buy now button on redesigned pd page
					{
						if ($("input:checked").attr("id") == "radio2") // if second radio button is selected (upsell) define what the upsell pid is
						{
							if (product.indexOf("tav") != -1) { pid = "106226000"; } // if product is tav - pid is tis
							if (product.indexOf("tis") != -1) { pid = "106251000"; } // if product is tis - pid is tis pro
						}
						window.location = "http://store.trendmicro.com/store/tmamer/AddItemToRequisition/productID."+pid+"/pgm."+program_id+"/OfferID."+offer_id;
					});
					return false;
				}
			});
		} // close success
	}); //close $.ajax(
}); //close document.ready(
