//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] = "Moyea-YouTube-Converter";
gSWREGCodes[gCount] = "43908-27";
gREGNOWCodes[gCount] = "13973-70";
gCount++;

gProducts[gCount] = "Moyea-Video-Converter";
gSWREGCodes[gCount] = "43908videocvt"; 
gREGNOWCodes[gCount] = "13973-28";
gCount++;

gProducts[gCount] = "Moyea-Video-to-iPod-Converter";
gSWREGCodes[gCount] = "43908v2ipod"; 
gREGNOWCodes[gCount] = "13973-2";
gCount++;

gProducts[gCount] = "Moyea-Video-to-Zune-Converter"; 
gSWREGCodes[gCount] = "43908v2zune"; 
gREGNOWCodes[gCount] = "13973-5";
gCount++;

gProducts[gCount] = "Moyea-Video-to-3GP-Converter"; 
gSWREGCodes[gCount] = "43908v23gp"; 
gREGNOWCodes[gCount] = "13973-7";
gCount++;

gProducts[gCount] = "Moyea-Video-to-iPhone-Converter"; 
gSWREGCodes[gCount] = "43908video2iphone"; 
gREGNOWCodes[gCount] = "13973-29";
gCount++;

gProducts[gCount] = "Moyea-Video-to-Apple-TV-Converter"; 
gSWREGCodes[gCount] = "43908v2appletv"; 
gREGNOWCodes[gCount] = "13973-30";
gCount++;

gProducts[gCount] = "Moyea-Video-to-PSP-Converter"; 
gSWREGCodes[gCount] = "43908v2psp"; 
gREGNOWCodes[gCount] = "13973-8";
gCount++;

gProducts[gCount] = "Moyea-DVD-Ripper"; 
gSWREGCodes[gCount] = "43908dvdripper"; 
gREGNOWCodes[gCount] = "13973-18";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-iPod-Converter"; 
gSWREGCodes[gCount] = "43908dvd2ipod"; 
gREGNOWCodes[gCount] = "13973-19";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-Zune-Converter"; 
gSWREGCodes[gCount] = "43908dvd2zune"; 
gREGNOWCodes[gCount] = "13973-22";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-3GP-Converter"; 
gSWREGCodes[gCount] = "43908dvd23gp"; 
gREGNOWCodes[gCount] = "13973-23";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-iPhone-Converter"; 
gSWREGCodes[gCount] = "43908dvd2iphone"; 
gREGNOWCodes[gCount] = "13973-20";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-Apple-TV-Converter"; 
gSWREGCodes[gCount] = "43908dvd2appletv"; 
gREGNOWCodes[gCount] = "13973-24";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-PSP-Converter"; 
gSWREGCodes[gCount] = "43908dvd2psp"; 
gREGNOWCodes[gCount] = "13973-21";
gCount++;

gProducts[gCount] = "Moyea-Video-to-Gphone-Converter";
gSWREGCodes[gCount] = "43908-18";
gREGNOWCodes[gCount] = "13973-61";
gCount++;

gProducts[gCount] = "Moyea-Video-to-Blackberry-Converter";
gSWREGCodes[gCount] = "43908-17";
gREGNOWCodes[gCount] = "13973-60";
gCount++;

gProducts[gCount] = "Moyea-MOD-Converter"; 
gSWREGCodes[gCount] = "43908-19"; 
gREGNOWCodes[gCount] = "13973-62";
gCount++;

gProducts[gCount] = "Moyea-MKV-Converter"; 
gSWREGCodes[gCount] = "43908-20"; 
gREGNOWCodes[gCount] = "13973-63";
gCount++;

gProducts[gCount] = "Moyea-FLV-Converter"; 
gSWREGCodes[gCount] = "43908-21"; 
gREGNOWCodes[gCount] = "13973-64";
gCount++;

gProducts[gCount] = "Moyea-HD-Video-Converter"; 
gSWREGCodes[gCount] = "43908-22"; 
gREGNOWCodes[gCount] = "13973-65";
gCount++;

gProducts[gCount] = "Moyea-TOD-Converter"; 
gSWREGCodes[gCount] = "43908-23"; 
gREGNOWCodes[gCount] = "13973-66";
gCount++;


gProducts[gCount] = "Moyea-MTS/M2TS-Converter"; 
gSWREGCodes[gCount] = "43908-24"; 
gREGNOWCodes[gCount] = "13973-67";
gCount++;

gProducts[gCount] = "Moyea-Blu-Ray-Ripper"; 
gSWREGCodes[gCount] = "43908-25"; 
gREGNOWCodes[gCount] = "13973-68";
gCount++;

gProducts[gCount] = "Moyea-DVD-Ripper-for-Mac";
gSWREGCodes[gCount] = "43908-8";
gREGNOWCodes[gCount] = "13973-50";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-iPod-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-9";
gREGNOWCodes[gCount] = "13973-51";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-Apple-TV-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-11";
gREGNOWCodes[gCount] = "13973-53";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-iPhone-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-10";
gREGNOWCodes[gCount] = "13973-52";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-3GP-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-16";
gREGNOWCodes[gCount] = "13973-58";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-PSP-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-15";
gREGNOWCodes[gCount] = "13973-57";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-Zune-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-14";
gREGNOWCodes[gCount] = "13973-56";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-Blackberry-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-12";
gREGNOWCodes[gCount] = "13973-54";
gCount++;

gProducts[gCount] = "Moyea-DVD-to-MP4-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-13";
gREGNOWCodes[gCount] = "13973-55";
gCount++;

gProducts[gCount] = "Moyea-TOD-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-1";
gREGNOWCodes[gCount] = "13973-13";
gCount++;

gProducts[gCount] = "Moyea-MOD-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-2";
gREGNOWCodes[gCount] = "13973-14";
gCount++;

gProducts[gCount] = "Moyea-MTS/M2TS-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-3";
gREGNOWCodes[gCount] = "13973-15";
gCount++;

gProducts[gCount] = "Moyea-FLV-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-4";
gREGNOWCodes[gCount] = "13973-17";
gCount++;

gProducts[gCount] = "Moyea-MKV-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-5";
gREGNOWCodes[gCount] = "13973-47";
gCount++;

gProducts[gCount] = "Moyea-Video-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-6";
gREGNOWCodes[gCount] = "13973-48";
gCount++;

gProducts[gCount] = "Moyea-HD-Video-Converter-for-Mac";
gSWREGCodes[gCount] = "43908-7";
gREGNOWCodes[gCount] = "13973-49";
gCount++;

gProducts[gCount] = "Moyea-YouTube-Converter-For-Mac"; 
gSWREGCodes[gCount] = "43908-26"; 
gREGNOWCodes[gCount] = "13973-69";
gCount++;

gProducts[gCount] = "Moyea-Blu-Ray-Ripper-for-Mac"; 
gSWREGCodes[gCount] = "43908-28"; 
gREGNOWCodes[gCount] = "13973-71";
gCount++;

gProducts[gCount] = "Moyea-Apple-TV-Converter-Pack";
gSWREGCodes[gCount] = "43908appletvcvtpk";
gREGNOWCodes[gCount] = "13973-24,13973-30,13973-37";
gCount++;

gProducts[gCount] = "Moyea-3GP-Converter-Pack";
gSWREGCodes[gCount] = "439083gpcvtpk";
gREGNOWCodes[gCount] = "13973-23,13973-7,13973-36";
gCount++;

gProducts[gCount] = "Moyea-Zune-Converter-Pack";
gSWREGCodes[gCount] = "43908zunecvtpk";
gREGNOWCodes[gCount] = "13973-22,13973-5,13973-35";
gCount++;

gProducts[gCount] = "Moyea-PSP-Converter-Pack";
gSWREGCodes[gCount] = "43908pspcvtpk";
gREGNOWCodes[gCount] = "13973-21,13973-8,13973-34";
gCount++;

gProducts[gCount] = "Moyea-iPhone-Converter-Pack";
gSWREGCodes[gCount] = "43908iphonecvtpk";
gREGNOWCodes[gCount] = "13973-20,13973-29,13973-33";
gCount++;

gProducts[gCount] = "Moyea-iPod-Converter-Pack";
gSWREGCodes[gCount] = "43908ipodcvtpk";
gREGNOWCodes[gCount] = "13973-19,13973-2,13973-32";
gCount++;

gProducts[gCount] = "Moyea-Converter-Pack";
gSWREGCodes[gCount] = "43908mycvtpk";
gREGNOWCodes[gCount] = "13973-18,13973-28,13973-31";
gCount++;

gProducts[gCount] = "Moyea-Video-DVD-Converter-Ultimate-for-MAC";
gSWREGCodes[gCount] = "43908-29";
gREGNOWCodes[gCount] = "13973-75";
gCount++;

gProducts[gCount] = "Moyea-Video-DVD-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-30";
gREGNOWCodes[gCount] = "13973-76";
gCount++;

gProducts[gCount] = "Moyea-iPod-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-31";
gREGNOWCodes[gCount] = "13973-77";
gCount++;

gProducts[gCount] = "Moyea-iPhone-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-32";
gREGNOWCodes[gCount] = "13973-78";
gCount++;

gProducts[gCount] = "Moyea-3GP-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-33";
gREGNOWCodes[gCount] = "13973-79";
gCount++;

gProducts[gCount] = "Moyea-Zune-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-34";
gREGNOWCodes[gCount] = "13973-80";
gCount++;

gProducts[gCount] = "Moyea-PSP-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-35";
gREGNOWCodes[gCount] = "13973-81";
gCount++;

gProducts[gCount] = "Moyea-Apple-TV-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-36";
gREGNOWCodes[gCount] = "13973-82";
gCount++;

gProducts[gCount] = "Moyea-Blu-ray-Video-Converter-Ultimate";
gSWREGCodes[gCount] = "43908-37";
gREGNOWCodes[gCount] = "13973-83";
gCount++;

gProducts[gCount] = "Moyea-Blu-ray-Video-Converter-Ultimate-for-MAC";
gSWREGCodes[gCount] = "43908-38";
gREGNOWCodes[gCount] = "13973-84";
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" || ss2[0] == "items") {
      return ss2[1];
    }
  }
  return "";
}

function REGNOW_BuildLink(pc, affid)
{
   if (affid != "") {
      return "https://www.regnow.com/softsell/nph-softsell.cgi?items=" + pc + "&affiliate=" + affid; 
   } else {
      return "https://www.regnow.com/softsell/nph-softsell.cgi?items=" + 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();