//Parses the url query string and returns the value of name
function getAffiliateFromQuery(name)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");

  for(var i=0;i<vars.length;i++)
  {
    var pair = vars[i].split("=");
    if(pair[0] == name)
    {
      return pair[1];
    }
  }
  return "none";
}

//Create a cookie
function createCookie(name,value,days)
{
  if(days)
  {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else
  {
    var expires = "";
  }  
  var cookie = name+"="+value+expires+"; path=/";
  document.cookie = cookie;
}

//Read cookie
function readCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i<ca.length;i++)
  {
    var c = ca[i];
    while(c.charAt(0)==' ') c = c.substring(1,c.length);
    if(c.indexOf(nameEQ) == 0)  return c.substring(nameEQ.length,c.length);
  }
  return null;
}

//Show the telephone img
function showImg(imgId)
{
  document.getElementById(imgId).style.visibility='visible';
}

function handler() //this should be called from the body tag
{
    //Process the affiliate link into the site
    var affiliate = getAffiliateFromQuery('adnetwork');
    if(affiliate != "none") 
    {
	createCookie("affiliate_networks",affiliate,3);
    }
    //The appropriate affiliate is now set

    //If there is no affiliate show telephone images
    var x = readCookie("affiliate_networks");
    //alert(x);
    if(readCookie("affiliate_networks") == null)
    {
	showImg("headtel");
    }
}

//Include the affiliate script file
function include(script_filename) 
{
  document.write('<' + 'script');
  document.write(' language="javascript"');
  document.write(' type="text/javascript"');
  document.write(' src="' + script_filename + '">');
  document.write('</' + 'script' + '>');
}

function chooseAffilliateOrderPageCode(orderid,total)
{
  var name = readCookie("affiliate_networks");  
  if(name == "webgains")
  {
   var wgOrderReference = orderid;
var wgOrderValue = total;
var wgEventID = 856;
var wgComment = "";
var wgLang = "en_GB";
var wgsLang = "javascript-client";
var wgVersion = "1.1";
var wgProgramID = 485;
var wgSubDomain = "track";
var wgCheckSum = "";
if(location.protocol.toLowerCase() == "https:") wgProtocol="https";
else wgProtocol = "http";

wgUri = wgProtocol + "://" + wgSubDomain + ".webgains.com/transaction.html" + "?wgver=" + wgVersion + "&wgprotocol=" + wgProtocol + "&wgsubdomain=" + wgSubDomain + "&wgslang=" + wgsLang + "&wglang=" + wgLang + "&wgprogramid=" + wgProgramID + "&wgeventid=" + wgEventID + "&wgvalue=" + wgOrderValue + "&wgchecksum=" + wgCheckSum + "&wgorderreference="  + wgOrderReference + "&wgcomment=" + escape(wgComment) + "&wglocation=" + escape(document.referrer);
document.write('<sc'+'ript language="JavaScript"  type="text/javascript" src="'+wgUri+'"></sc'+'ript>'); 

  }
  else if(name == "window")
  {
     /* Set your transaction parameters */
     var aw_mid = '962';
     var aw_sale = total;
     var aw_extra = orderid;
     var aw_parts = 'default:'+total;
     var aw_test = '0';

     /* Do not edit below here. */
     call  = location.protocol.toLowerCase()+"//www.aw";
     call += "in1.co"+"m/sread.j"+"s?a="+aw_mid+"&b="+aw_sale+"&c="+aw_extra+"&d="+aw_parts;
     call += "&t="+aw_test+"&l="+escape(window.location.href)+"&tv=1.0";
     document.write("<s"+"cript language=\"JavaS"+"cript\" type=\"text/javas"+"cript\" src=\""+call+"&tt=js\" id=\"aw_tag\"></s"+"cript>");
    var oImage = new Image;
    oImage.src = call+"&tt=ia";
  }
  else if(name == "ROI")
  {
    var merchant_id = '408416';
    var order_id = orderid;
    var order_amt = total;
    include("https://stat.DealTime.com/ROI/ROI.js?mid=408416");
  } 
}



//<![CDATA[
if(screen.width >= 1025) document.write("<link href='/shop/skin1/sheets/1024.css' rel='stylesheet' rel='stylesheet' type='text/css' />");
//]]>
