//write section


function parseQueryString()
{
    var obj = new Object();
    var nvpairs = location.search.substring(1).split("&");
    for (var idx = 0; idx <nvpairs.length; idx++)
    {
        var tokens = nvpairs[idx].split("=");
        obj[unescape(tokens[0])] = tokens.length == 2 ?
        unescape(tokens[1]) : undefined;
    }
    return obj;
} 


function formWrite(name, value, expDays, path, domain)
{
  var expire = "";
  if(expDays != null)
  {
  var expires_date = new Date((new Date()).getTime() + expDays*24*60*60*1000);
  document.cookie = name + "=" + escape( value ) +
  ( ( expDays ) ? ";expires=" + expires_date : "" ) + 
  ( ( path ) ? ";path=" + path : "" ) + 
  ( ( domain ) ? ";domain=" + domain : "" );
  }
}

// read section
function formRead(name)
{
    // Initial cookie value should be set to the default index page prefix. At this point it is ""
    var cookieValue = "";
    var search = name + "=";
    if(document.cookie.length > 0)
    {
      offset = document.cookie.indexOf(search);
      if (offset != -1)
      {
        offset += search.length;
        end = document.cookie.indexOf(";", offset);
        if (end == -1) end = document.cookie.length;
        cookieValue = unescape(document.cookie.substring(offset, end))
      }
    }
    return cookieValue;
}

// This function checks if the LFORM cookie is of valid value.
// If it is not, it will rewrite the cookie to be value ""
function isValidLform(lform)
{
    // When new LFROM values become legal, they need to be added to the string preface by a L.
    var validLform= "L L1 L2 L3 L4 L5 L6";
    var validValue = false;

    if (lform > 0) {
      // BFORM cookie exists, check if it is a valid value

      if (validLform.search("L"+lform) != -1) validValue = true;
      if (validValue != true) {
        // BFORM exists and invalid. Rewrite the cookie
        // lformWrite("LFORM", "1" , 31, "/", "halftheink.com");

      }

    }
    return validValue;
}

// This function checks if the BFORM cookie is of valid value.
// If it is not, it will rewrite the cookie to be value ""
function isValidBform(bform) {
    // When new BFROM values become legal, they need to be added to the string preface by a B.
    var validBform= "B B5 B6";

    var validValue = false;

    if (bform > 0) {
      // BFORM cookie exists, check if it is a valid value
      validValue = (validBform.search("B"+bform) != -1);

      if (validValue != true) {
        // BFORM exists and invalid. Rewrite the cookie
        // bformWrite("BFORM", "1" , 31, "/", "halftheink.com");
      }
    }
    return validValue;
}
// This function create the correct Home link
function createHomeLink(){
    var  lform = "";
    lform = formRead("LFORM");

    document.write("<a href=/index" + lform + ".php>Home</a>");
}

// This function creates the correct logo link
function createLogoLink() {
    var  lform = "";

    lform = formRead("LFORM");

    document.write("<a href=/index" + lform + ".php>" + "<img src=/images/logo-name.png alt=Halftheink.Com width=308 height=35 border=0/>" + "</a>");

}

// This function create the correct buy link.
function createBuyLink() {

  var bfrom = "";
  bform = formRead("BFORM");

  document.write("<a href=/buy" + bform + ".php>Buy</a>");

}

function createLargeDownloadButtonLink(){

      var bfrom = "";
      bform = formRead("BFORM");

      switch (bform){
          case "5": {
              //PrintCountry
              document.write("<a href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2632110'><img src='images/large-download-button.png' alt='Download PretonSaver' border='0'/></a>");
              break;
          }
          case "6": {
              //IPortnet
              document.write("<a href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2261227'><img src='images/large-download-button.png' alt='Download PretonSaver' border='0'/></a>");
              break;
          }
          default: {
              //hti download link
              document.write("<a href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2261227'><img src='images/large-download-button.png' alt='Download PretonSaver' border='0'/></a>")
          }
      }
}

function createMidDownloadButtonLink(){

      var bfrom = "";
      bform = formRead("BFORM");

      switch (bform){
          case "5": {
              //PrintCountry
              document.write("<a href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2632110'><img src='images/mid-download-button.png' alt='Download PretonSaver' border='0'/></a>");
              break;
          }
          case "6": {
              //IPortnet
              document.write("<a href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2261227'><img src='images/mid-download-button.png' alt='Download PretonSaver' border='0'/></a>");
              break;
          }
          default: {
              //hti download link
              document.write("<a href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2261227'><img src='images/mid-download-button.png' alt='Download PretonSaver' border='0'/></a>")
          }
      }
}

// Saving calculator  downalod button
function createCalcDownloadButtonLink(){

      var bfrom = "";
      bform = formRead("BFORM");

      switch (bform){
          case "5": {
              //PrintCountry
              document.write("<a id='calc_download' href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2632110'></a>");
              break;
          }
          case "6": {
              //IPortnet
              document.write("<a id='calc_download' href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2261227'></a>");
              break;
          }
          default: {
              //hti download link
              document.write("<a id='calc_download' href='https://www.plimus.com/jsp/download_trial.jsp?contractId=2261227'></a>");
          }
      }
}



// This function redirect the visitor to the appropiate index  page according to he lfrom cookie
function redirectIndexPage(NewWindow, currentIndex)     {
    var lform = "";
    var redirectTo = "/index";

    lform = formRead("LFORM");

    redirectTo = redirectTo.concat(lform + ".php");

    //check if the current page is the same as the redirectTo. If yes, do nothing.
    if (currentIndex != lform) {
      if (NewWindow)
        window.open(redirectTo);
      else
        window.location.href=redirectTo;
    }
    // do not remove the "return false"
    return false;

}


// This function redirect the visitor to the appropiate buy  page according to he bfrom cookie
function redirectBuyPage(NewWindow, currentIndex)     {
    var bform = "";
    var redirectTo = "/buy";

    bform = formRead("BFORM");

    redirectTo = redirectTo.concat(bform + ".php");

    //check if the current page is the same as the redirectTo. If yes, do nothing.
    if (currentIndex != bform) {
      if (NewWindow)
        window.open(redirectTo);
      else
        window.location.href=redirectTo;
    }
    // do not remove the "return false"
    return false;

}



// Init functions;
function initLform() {
    // Check if there is a lfrom parameter passed to the page, and grab its value.
    var g_querystring = parseQueryString();
    var referrer = g_querystring["lform"]; // "lform" is the form identifier i.e lform=[value]

    //only create the cookie if it is a valid lform value
    if  ( isValidLform(referrer) )  {
      formWrite("LFORM", referrer , 31, "/", "halftheink.com"); //DOMAIN SHOULD BE between "" i.e. "domain.com"
    }
  }

function initBform() {
    // Check if there is a bfrom parameter passed to the page, and grab its value.
    var g_querystring = parseQueryString();
    var referrer = g_querystring["bform"]; // "bform" is the form identifier i.e lform=[value]

    //only create the cookie if it is a valid bform value
    if  ( isValidBform(referrer) )  {
      formWrite("BFORM", referrer , 31, "/", "halftheink.com"); //DOMAIN SHOULD BE between "" i.e. "domain.com"
    }
  }

// This code will run at initiation.

initLform();
initBform();
