//set before this script or not set
var gProduct;
var gAffiliateID;

var gProducts = new Array();
var gSWREGCodes = new Array();
var gREGNOWCodes = new Array();
var gCount = 0;

//note: spaces in item of gProducts should be replace with "-"
gProducts[gCount] = "Pavtube-DVD-Copy";
gSWREGCodes[gCount] = "48737dvdcopy";
gREGNOWCodes[gCount] = "17041-1";
gCount++;

gProducts[gCount] = "Pavtube-Video-Converter"; 
gSWREGCodes[gCount] = "48737-1"; 
gREGNOWCodes[gCount] = "17041-3";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-iPod-Converter";
gSWREGCodes[gCount] = "48737-2"; 
gREGNOWCodes[gCount] = "17041-4";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-Zune-Converter"; 
gSWREGCodes[gCount] = "48737-3"; 
gREGNOWCodes[gCount] = "17041-5";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-3GP-Converter"; 
gSWREGCodes[gCount] = "48737-4"; 
gREGNOWCodes[gCount] = "17041-6";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-iPhone-Converter"; 
gSWREGCodes[gCount] = "48737-5"; 
gREGNOWCodes[gCount] = "17041-7";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-Apple-TV-Converter"; 
gSWREGCodes[gCount] = "48737-6"; 
gREGNOWCodes[gCount] = "17041-8";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-PSP-Converter"; 
gSWREGCodes[gCount] = "48737-7"; 
gREGNOWCodes[gCount] = "17041-9";
gCount++;

gProducts[gCount] = "Pavtube-DVD-Ripper"; 
gSWREGCodes[gCount] = "48737-8"; 
gREGNOWCodes[gCount] = "17041-10";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-iPod-Converter"; 
gSWREGCodes[gCount] = "48737-9"; 
gREGNOWCodes[gCount] = "17041-11";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Zune-Converter"; 
gSWREGCodes[gCount] = "48737-10"; 
gREGNOWCodes[gCount] = "17041-12";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-3GP-Converter"; 
gSWREGCodes[gCount] = "48737-11"; 
gREGNOWCodes[gCount] = "17041-13";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-iPhone-Converter"; 
gSWREGCodes[gCount] = "48737-12"; 
gREGNOWCodes[gCount] = "17041-14";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Apple-TV-Converter"; 
gSWREGCodes[gCount] = "48737-13"; 
gREGNOWCodes[gCount] = "17041-15";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-PSP-Converter"; 
gSWREGCodes[gCount] = "48737-14"; 
gREGNOWCodes[gCount] = "17041-16";
gCount++;

gProducts[gCount] = "Pavtube-Video-DVD-Converter-Ultimate"; 
gSWREGCodes[gCount] = "48737-15"; 
gREGNOWCodes[gCount] = "17041-17";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Gphone-Converter";
gSWREGCodes[gCount] = "48737-17";
gREGNOWCodes[gCount] = "17041-18";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Blackberry-Converter";
gSWREGCodes[gCount] = "48737-18";
gREGNOWCodes[gCount] = "17041-19";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-Gphone-Converter";
gSWREGCodes[gCount] = "48737-19";
gREGNOWCodes[gCount] = "17041-20";
gCount++;

gProducts[gCount] = "Pavtube-Video-to-Blackberry-Converter";
gSWREGCodes[gCount] = "48737-20";
gREGNOWCodes[gCount] = "17041-21";
gCount++;

gProducts[gCount] = "Pavtube-iPod-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-21";
gREGNOWCodes[gCount] = "17041-22";
gCount++;

gProducts[gCount] = "Pavtube-Apple-TV-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-22";
gREGNOWCodes[gCount] = "17041-23";
gCount++;

gProducts[gCount] = "Pavtube-iPhone-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-23";
gREGNOWCodes[gCount] = "17041-24";
gCount++;

gProducts[gCount] = "Pavtube-3GP-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-24";
gREGNOWCodes[gCount] = "17041-25";
gCount++;

gProducts[gCount] = "Pavtube-PSP-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-25";
gREGNOWCodes[gCount] = "17041-26";
gCount++;

gProducts[gCount] = "Pavtube-Zune-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-26";
gREGNOWCodes[gCount] = "17041-27";
gCount++;

gProducts[gCount] = "Pavtube-Gphone-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-27";
gREGNOWCodes[gCount] = "17041-28";
gCount++;

gProducts[gCount] = "Pavtube-Blackberry-Converter-Ultimate";
gSWREGCodes[gCount] = "48737-28";
gREGNOWCodes[gCount] = "17041-29";
gCount++;

gProducts[gCount] = "Pavtube-DVD-Ripper-for-Mac";
gSWREGCodes[gCount] = "48737-29";
gREGNOWCodes[gCount] = "17041-30";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-iPod-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-30";
gREGNOWCodes[gCount] = "17041-31";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Apple-TV-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-31";
gREGNOWCodes[gCount] = "17041-32";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-iPhone-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-32";
gREGNOWCodes[gCount] = "17041-33";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-3GP-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-33";
gREGNOWCodes[gCount] = "17041-34";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-PSP-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-34";
gREGNOWCodes[gCount] = "17041-35";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Zune-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-35";
gREGNOWCodes[gCount] = "17041-36";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Gphone-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-36";
gREGNOWCodes[gCount] = "17041-37";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Blackberry-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-37";
gREGNOWCodes[gCount] = "17041-38";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-MP4-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-38";
gREGNOWCodes[gCount] = "17041-39";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-AVI-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-39";
gREGNOWCodes[gCount] = "17041-40";
gCount++;

gProducts[gCount] = "Pavtube-DVD-Audio-Ripper-for-Mac";
gSWREGCodes[gCount] = "48737-40";
gREGNOWCodes[gCount] = "17041-41";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Mobile-Phone-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-41";
gREGNOWCodes[gCount] = "17041-42";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-WMV-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-42";
gREGNOWCodes[gCount] = "17041-43";
gCount++;

gProducts[gCount] = "Pavtube-DVD-to-Pocket-PC-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-43";
gREGNOWCodes[gCount] = "17041-44";
gCount++;

gProducts[gCount] = "Pavtube-TOD-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-44";
gREGNOWCodes[gCount] = "17041-45";
gCount++;

gProducts[gCount] = "Pavtube-MOD-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-45";
gREGNOWCodes[gCount] = "17041-46";
gCount++;

gProducts[gCount] = "Pavtube-MTS/M2TS-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-46";
gREGNOWCodes[gCount] = "17041-47";
gCount++;

gProducts[gCount] = "Pavtube-FLV-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-47";
gREGNOWCodes[gCount] = "17041-48";
gCount++;

gProducts[gCount] = "Pavtube-MKV-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-48";
gREGNOWCodes[gCount] = "17041-49";
gCount++;

gProducts[gCount] = "Pavtube-Video-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-49";
gREGNOWCodes[gCount] = "17041-50";
gCount++;

gProducts[gCount] = "Pavtube-HD-Video-Converter-for-Mac";
gSWREGCodes[gCount] = "48737-50";
gREGNOWCodes[gCount] = "17041-51";
gCount++;

gProducts[gCount] = "Pavtube-MOD-Converter"; 
gSWREGCodes[gCount] = "48737-51"; 
gREGNOWCodes[gCount] = "17041-52";
gCount++;

gProducts[gCount] = "Pavtube-MKV-Converter"; 
gSWREGCodes[gCount] = "48737-52"; 
gREGNOWCodes[gCount] = "17041-53";
gCount++;

gProducts[gCount] = "Pavtube-FLV-Converter"; 
gSWREGCodes[gCount] = "48737-53"; 
gREGNOWCodes[gCount] = "17041-54";
gCount++;

gProducts[gCount] = "Pavtube-HD-Video-Converter"; 
gSWREGCodes[gCount] = "48737-54"; 
gREGNOWCodes[gCount] = "17041-55";
gCount++;

gProducts[gCount] = "Pavtube-TOD-Converter"; 
gSWREGCodes[gCount] = "48737-55"; 
gREGNOWCodes[gCount] = "17041-56";
gCount++;


gProducts[gCount] = "Pavtube-MTS/M2TS-Converter"; 
gSWREGCodes[gCount] = "48737-56"; 
gREGNOWCodes[gCount] = "17041-57";
gCount++;

gProducts[gCount] = "Pavtube-Blu-Ray-Converter"; 
gSWREGCodes[gCount] = "48737-58"; 
gREGNOWCodes[gCount] = "17041-59";
gCount++;

gProducts[gCount] = "Pavtube-YouTube-Converter-For-Mac"; 
gSWREGCodes[gCount] = "48737-57"; 
gREGNOWCodes[gCount] = "17041-58";
gCount++;

gProducts[gCount] = "Pavtube-YouTube-Converter"; 
gSWREGCodes[gCount] = "48737-59"; 
gREGNOWCodes[gCount] = "17041-60";
gCount++;

gProducts[gCount] = "Pavtube-Blu-Ray-Ripper-For-Mac"; 
gSWREGCodes[gCount] = "48737-60"; 
gREGNOWCodes[gCount] = "17041-61";
gCount++;

gProducts[gCount] = "Pavtube-DVD-Video-Converter-Ultimate-for-Mac"; 
gSWREGCodes[gCount] = "48737-61"; 
gREGNOWCodes[gCount] = "17041-62";
gCount++;

gProducts[gCount] = "Pavtube-Blu-Ray-Video-Converter-Ultimate"; 
gSWREGCodes[gCount] = "48737-62"; 
gREGNOWCodes[gCount] = "17041-63";
gCount++;

gProducts[gCount] = "Pavtube-Blu-Ray-Video-Converter-Ultimate-for-Mac"; 
gSWREGCodes[gCount] = "48737-63"; 
gREGNOWCodes[gCount] = "17041-64";
gCount++;

gProducts[gCount] = "Pavtube-iPhone-Ringtone-Maker"; 
gSWREGCodes[gCount] = "48737-64"; 
gREGNOWCodes[gCount] = "17041-65";
gCount++;

gProducts[gCount] = "Pavtube-Ringtone-Maker"; 
gSWREGCodes[gCount] = "48737-65"; 
gREGNOWCodes[gCount] = "17041-66";
gCount++;
//add more product entries

function AffiliateItem(pid, affid)
{
  this.product = pid;
  this.affiliateid = affid;
}

function GetAffiliateItems()
{
var s = "" + document.cookie;
var ss = s.split("; ");
var i, j;
var product_affids = new Array();

product_affids.length = ss.length+1;

for(i=0,j=0; i<ss.length; ++i) {    
  if (ss[i].slice(0, 8) == "PRODUCT_") {
    product_affids[j] = new AffiliateItem(ss[i].split("_")[1], ss[i].split("=")[1]);
    ++j;
  }
}  

//new product overrides the old one
if (gProduct != undefined) {
  if (gProduct != "") {
  	for (i=0; i<j; ++i) {
     		if (product_affids[i].product == gProduct) {
	          product_affids[i].affiliateid = gAffiliateID;
        	  break;
		}
  	}
	if (i == j) {
	    product_affids[j] = new AffiliateItem(gProduct, gAffiliateID);
	    ++j;
  	}
   } 
} 


product_affids.length = j;
return product_affids;
}

function GetIsUsingRegnow()
{
  var s = ""+document.cookie;
  var ss = s.split("; ");
  var ss2;
  for(var i=0; i<ss.length; ++i) {
     ss2 = ss[i].split("=");
     if (ss2[0] == "USE_REGNOW_SYSTEM") {
        if (ss2[1] == "yes") {
           return true;
        }
     }
  } 
  return false;
}

var gAffiliateItems = GetAffiliateItems();
var gUsingRegnowSystem = GetIsUsingRegnow();

function Internal_GetAffiliateID(pc)
{
  for(var i=0; i<gAffiliateItems.length; ++i) {
     if (pc == gAffiliateItems[i].product) return gAffiliateItems[i].affiliateid;
  }
  return "";
}

function SWREG_GetAffiliateID(pc)
{
	for(var i=0; i<gCount; ++i) {
	  if (pc == gSWREGCodes[i]) {
		return Internal_GetAffiliateID(gProducts[i]);		
	  }
	}
	return "";
}

function REGNOW_GetAffiliateID(pc)
{
	for(var i=0; i<gCount; ++i) {
	  if (pc == gREGNOWCodes[i]) {
		return Internal_GetAffiliateID(gProducts[i]);		
	  }
	}
	return "";
}

function SWREG_TO_REGNOW(pc)
{
  for(var i=0; i<gCount; ++i) {
     if (gSWREGCodes[i] == pc) return gREGNOWCodes[i];
  }
  return "";
}

function SWREG_ParseProductID(href)
{
  var ss0 = href.split("?");
  var ss = ss0[1].split("&");
  var ss2;

  for(var i=0; i<ss.length; ++i) {
    ss2 = ss[i].split("=");
    if (ss2[0] == "p") {
      if (ss2[1].indexOf(":") != -1) return ""; //recursive orders ?
      else return ss2[1];
    }
  }
  return "";
}

function REGNOW_ParseProductID(href)
{
  var ss0 = href.split("?");
  var ss = ss0[1].split("&");
  var ss2;

  for(var i=0; i<ss.length; ++i) {
    ss2 = ss[i].split("=");
    if (ss2[0] == "item") {
      return ss2[1];
    }
  }
  return "";
}

function REGNOW_BuildLink(pc, affid)
{
   if (affid != "") {
      return "https://www.regnow.com/softsell/nph-softsell.cgi?item=" + pc + "&affiliate=" + affid; 
   } else {
      return "https://www.regnow.com/softsell/nph-softsell.cgi?item=" + pc;
   }
}

function UpdatePurchaseLink()
{
  for(i=0; i<document.links.length; ++i) {     
     if (document.links[i].className == "purchase") {
        var href = document.links[i].href;
        if (href == "" || href == undefined) continue;
	var pc;
	var affid;
        if (href.indexOf(".swreg.org/cgi-bin/s.cgi?") != -1) { //swreg link           
	   pc = SWREG_ParseProductID(href);
	   if (pc != "") {
              affid = SWREG_GetAffiliateID(pc);
	      if (affid != "") {
		pc = SWREG_TO_REGNOW(pc);
                if (pc != "") {
		   document.links[i].href = REGNOW_BuildLink(pc, affid);
                }
	      } else if (gUsingRegnowSystem) {
                pc = SWREG_TO_REGNOW(pc);
                if (pc != "") {
		   document.links[i].href = REGNOW_BuildLink(pc, "");
                }
              }
	   }
        } else if (href.indexOf(".regnow.com/softsell/nph-softsell.cgi?") != -1) { //regnow link	   
	   pc = REGNOW_ParseProductID(href);
	   if (pc != "") {
             affid = REGNOW_GetAffiliateID(pc);
	     if (affid != "") {
		document.links[i].href = REGNOW_BuildLink(pc, affid);
	      }
	   }
        }
     }
  }
}

function SafeUpdate()
{ 
  try {
    UpdatePurchaseLink();
  } catch(e)
  { 
    ;
  }
}

SafeUpdate();