/*-- AJAX variables --*/
var jax_tracker    = false;

/*-- URL of the hound tracker --*/
var houndM_path = "https://www.kojk.net/houndMaster/tools/houndM.php";
var houndM_tracker_path = "./houndM_tracker.php";

/*-- Build the tracker arrays for the lands and clicks --*/
if (typeof(tracker_id) != 'undefined')
   {
   var land_t = new Array(tracker_id.length);
   var click_t = new Array(tracker_id.length);
   }

/*-- Build the landing id --*/
var lid = houndM_build_LID (6);

/*----------------------------------------------------------------------------*/
/*-- Initiate the main landing page                                         --*/
/*----------------------------------------------------------------------------*/
function houndM_initiate ()
  {
  /*-- Do preprocessing actions --*/
  houndM_initiate_preprocess ();

  /*-- AFFILIATE LINKS -------------------------------------*/
  houndM_build_affiliate ();

  /*-- SUBLAND LINKS ---------------------------------------*/
  houndM_build_subland ();

  /*-- IFRAME LINKS ----------------------------------------*/
  houndM_build_iframe ();

  /*-- PASSTHROUGH LINKS -----------------------------------*/
  houndM_build_passthrough ();

  /*-- TRACK REMAINING LANDS -------------------------------*/
  houndM_track_batchland (); 
  }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Do preprocessiing steps to the trackers and certain data.              --*/
/*----------------------------------------------------------------------------*/
function houndM_initiate_preprocess()
   {
   /*-- Get the query and detect if there is a sublanding involved. --*/
   var query = window.location.search.substring(1);
   if (!query || query == "") return;

   /*-- prepare the variables from the query --*/
   var tid = ""; 
   var cid = "";

   /*-- Separate the query --*/
   var vars = query.split("&");
   for (i in vars)
      {
      /*-- Separate the variable and value --*/
      var pair = vars[i].split("="); 

      /*-- Check and store the data --*/
      if (pair[0] == "t") tid = pair[1];
      if (pair[0] == "c") cid = pair[1];
      }

   /*-- Check if there are values present --*/
   if (tid && cid)
      {
      /*-- Create the global variables if it is not yet created --*/
      if (typeof(tracker_id) == 'undefined') tracker_id = new Array();
      if (typeof(click_id) == 'undefined')   click_id = new Array();
      if (typeof(land_t) == 'undefined')     land_t = new Array();
      if (typeof(click_t) == 'undefined')    click_t = new Array();

      /*-- Extend the arrays --*/
      tracker_id.length = tracker_id.length + 1;
      click_id.length   = click_id.length + 1;
      land_t.length     = land_t.length + 1;
      click_t.length    = click_t.length + 1;

      /*-- Attach the values to the arrays --*/
      tracker_id[100] = tid;
      click_id[100]   = cid;
      land_t[100]     = true;
      click_t[100]    = false;
      }
   }
/*----------------------------------------------------------------------------*/




/*----------------------------------------------------------------------------*/
/*-- BUILD LANDING PAGE FUNCTIONS --------------------------------------------*/
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Build the normal affiliate links actions here                          --*/
/*----------------------------------------------------------------------------*/
function houndM_build_affiliate ()
   {
   /*-- Check existance of affiliate links --*/
   var j = 0;
   var affiliate_elem = document.getElementById ('affiliate_link_' + j);
   while (affiliate_elem)
      {
      /*-- Get the anchor name and see if a tracker exists for that --*/
      i = affiliate_elem.name;
      if ((typeof(tracker_id) != 'undefined') 
         && (typeof(tracker_id[i]) != 'undefined')
         && (typeof(click_id) != 'undefined') 
         && (typeof(click_id[i]) != 'undefined'))
         {
         /*-- Build the resulting affiliate url --*/
         var affiliate_target = houndM_path + "?t=" + tracker_id[i] + "&c="
            + click_id[i];
         
         /*-- Attach the affiliate url to href if new_window is false --*/
         if ((typeof(new_window) == 'undefined') || (new_window == false))
            affiliate_elem.href = affiliate_target;

         /*-- Set the onclick to track the click --*/
         affiliate_elem.onclick = function ()
            {
            houndM_track (i, tracker_id[i], click_id[i], "CLICK");

            return true;
            }

         }

      /*-- Increment the counter and get the next element --*/
      j++; 
      affiliate_elem = document.getElementById ('affiliate_link_' + j);
      }
   }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Build the sublanding links actions here                                --*/
/*----------------------------------------------------------------------------*/
function houndM_build_subland ()
   {
   /*-- Check existance of affiliate links --*/
   var j = 0;
   var subland_elem = document.getElementById ('subland_link_' + j);
   while (subland_elem)
      {
      /*-- Get the anchor name and see if a tracker exists for that --*/
      i = subland_elem.name;
      if ((typeof(tracker_id) != 'undefined') 
         && (typeof(tracker_id[i]) != 'undefined')
         && (typeof(click_id) != 'undefined') 
         && (typeof(click_id[i]) != 'undefined'))
         {
         /*-- check the href and see if there is something to build on --*/
         var target = houndM_path;
         if ((subland_elem.href) && (subland_elem.href != "") 
            && (subland_elem.href != "#"))
            target = subland_elem.href;

         /*-- build the sublanding target --*/
         var subland_target = target + "?t=" + tracker_id[i] + "&c=" 
            + click_id[i];

         /*-- Attach the affiliate url to href if new_window is false --*/
         if ((typeof(new_window) == 'undefined') || (new_window == false))
            {
            subland_elem.href = subland_target;
            }
         else
            {
            /*-- Set the onclick to open the new window --*/
            subland_elem.onclick = function ()
               {
               window.open (subland_target, 'linkwindow');
               return false;
               }

            /*-- Since this is a sublanding link, change the href to '#'  --*/
            /*--   to prevent redirecting the landing page to the subland --*/
            /*--   in a case where subnew_window is called.               --*/
            subland_elem.href = "#";
            }
         }

      /*-- Increment the counter and get the next element --*/
      j++; 
      subland_elem = document.getElementById ('subland_link_' + j);
      }
   }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Build the iframe links in  the landing page and set their src fields   --*/
/*----------------------------------------------------------------------------*/
function houndM_build_iframe ()
   {
   /*-- Check existance of iframe links --*/
   var j = 0;
   var iframe_elem = document.getElementById ('iframe_link_' + j);
   while (iframe_elem)
      {
      /*-- Get the iframe name and see if a tracker exists for that --*/
      i = iframe_elem.name;
      if ((typeof(tracker_id) != 'undefined') 
         && (typeof(tracker_id[i]) != 'undefined')
         && (typeof(click_id) != 'undefined') 
         && (typeof(click_id[i]) != 'undefined'))
         {
         /*-- Build the resulting iframe url --*/
         var iframe_target = houndM_path + "?t=" + tracker_id[i] + "&c="
            + click_id[i];

         /*-- Attach the target to the iframe's src --*/
         iframe_elem.src = iframe_target;

         /*-- Track the landing and click --*/
         houndM_track (i, tracker_id[i], click_id[i], "CLAND");
         }

      /*-- Increment the counter and get the next element --*/
      j++; 
      iframe_elem = document.getElementById ('iframe_link_' + j);
      }
   }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Initiate the passthrough link. This will redirect the page to the      --*/
/*--   affiliate automatically. Passthrough links only works on 1 tracker.  --*/
/*----------------------------------------------------------------------------*/
function houndM_build_passthrough ()
   {
   /*-- Check if a passthrough link is passed --*/
   if ((typeof(passthrough) != 'undefined') && (passthrough !== false))
      {
      /*-- Check that the first link is built --*/
      if ((typeof(tracker_id) != 'undefined') 
         && (typeof(tracker_id[0]) != 'undefined')
         && (typeof(click_id) != 'undefined') 
         && (typeof(click_id[0]) != 'undefined'))
         {
         /*-- build the affiliate link query --*/
         var affiliate_target = houndM_path + "?t=" + tracker_id[0] + "&c="
            + click_id[0];

         /*-- track the click and land --*/
         houndM_track (0, tracker_id[0], click_id[0], "CLAND");

         /*-- Now redirect the page --*/
         setTimeout ("location.href = '" + affiliate_target + "'", 
            passthrough * 1000);
         }
      }
   }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Generate the random string for the landing ID                          --*/
/*----------------------------------------------------------------------------*/
function houndM_build_LID(string_length)
   {
   var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
   var randomstring = '';

   /*-- For each string length, get a random character --*/
   for (var i=0; i<string_length; i++)
      {
      var rnum = Math.floor(Math.random() * chars.length);
      randomstring += chars.substring(rnum,rnum+1);
      }

   /*-- Return the created random string with the tpl_id --*/
   return randomstring;
   }
/*----------------------------------------------------------------------------*/







/*----------------------------------------------------------------------------*/
/*-- TRACKING FUNCTION -------------------------------------------------------*/
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Generally loop for all available trackers and track the land if it is  --*/
/*--   not yet done so by the previous routines                             --*/
/*----------------------------------------------------------------------------*/
function houndM_track_batchland ()
   {
   /*-- Loop for the trackers --*/
   if (typeof(tracker_id) != 'undefined') 
      {
      for (i in tracker_id)
         {   
         /*-- Check the land tracking details --*/
         if ((typeof(land_t) != 'undefined')
            && (typeof(land_t[i]) != 'undefined') && (land_t[i] == true))
            continue;

         /*-- Not yet tracked, call the main tracker --*/
         houndM_track (i, tracker_id[i], click_id[i], "LAND");
         }
      }
   }
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*-- Main tracking subroutine                                               --*/
/*----------------------------------------------------------------------------*/
function houndM_track (i, tid, cid, type)
   {
   /*-- Build the general query --*/
   var query = "t=" + tid + "&c=" + cid;

   /*-- Add the tpl_id on the query --*/
   if (typeof tpl_id != 'undefined') query += "&tpl=" + tpl_id;

   /*-- Add the lid if it exists --*/
   if (typeof lid != 'undefined') query += "&lid=" + lid;

   /*-- Attach the referer if present --*/
   if (referer && referer != "") query += "&r=" + referer;

   /*-- Check if action is still valid and add the action tag --*/
   switch (type) 
      {
      case "LAND" : 
         /*-- Skip the tracking if it is already tracked! --*/
         if ((typeof(land_t) != 'undefined')
            && (typeof(land_t[i]) != 'undefined') && (land_t[i] == true))
            return;

         /*-- Attach the act --*/
         query += "&a=LAND";
         break;

      case "CLICK" :
         /*-- If a new window flag is set, launch it --*/
         if ((typeof new_window != 'undefined') && (new_window == true))
            window.open (houndM_path + "?" + query, 'linkwindow');

         /*-- Skip the tracking if it is already tracked! --*/
         if ((typeof(click_t) != 'undefined')
            && (typeof(click_t[i]) != 'undefined') && (click_t[i] == true))
            return;

         /*-- Attach the act --*/
         query += "&a=CLICK";
         break;

      case "CLAND" :
         /*-- Track only which type isn't done yet --*/
         var trackland = 1;
         var trackclick = 1;
         if ((typeof(land_t) != 'undefined')
            && (typeof(land_t[i]) != 'undefined') && (land_t[i] == true))
            trackland = 0;
         if ((typeof(click_t) != 'undefined')
            && (typeof(click_t[i]) != 'undefined') && (click_t[i] == true))
            trackclick = 0;

         /*-- Add the necessary act --*/
         if (trackland == 0 && trackclick == 0) return;
         else if (trackland == 1 && trackclick == 1) query += "&a=CLAND";
         else if (trackland == 1 && trackclick == 0) query += "&a=LAND";
         else if (trackland == 0 && trackclick == 1) query += "&a=CLICK";
         break;

      /*-- Unknown track type --*/
      default : return false;
      }

   /*-- Now build ajax the tracker --*/
   var x = (window.ActiveXObject)?
      new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
   
   if (x)
      {
      /*-- Set the content type --*/
      var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

      /*-- Initiate callback for the x object --*/
      x.onreadystatechange = function ()
         {
         /*-- Incase you want something done if it is a success --*/
         /*--   uncomment this part and add the handlers for    --*/
         /*--   the different track types.                      --*/
         if (x.readyState == 4 && x.status == 200)
            {
            if (x.responseText && x.responseText != "")
               {
               }
            }
         }

      /*-- Specify the load method then send the request --*/
      x.open("POST", houndM_tracker_path, jax_tracker);
      x.setRequestHeader("Content-Type", contentType);
      x.send(query);
      }

   /*-- Mark the recorded action --*/
   if (type == "LAND" || type == "CLAND") land_t[i]  = true;
   if (type == "CLICK" || type== "CLAND") click_t[i] = true;

   return true;
   }
/*----------------------------------------------------------------------------*/
