// upgrade.js
// JavaScript for upgrade.htm page
// check for available netYAK software upgrades
// Copyright 2002-2004 netYAK.com, inc.
// last updated: 01/21/2004

var myType = "";
var myVersion = 0;
var myLicense = 0;
var MySource = "";
var URLupgrade="http://www.netyak.com/YAKbites/YAKbites.htm";
var URLfreeupgrade="http://www.netyak.com/YAKbites/auto_download.htm";
var URLrenew="http://www.netyak.com/YAKbites/orderform.html";

function delineate (str)
{
  theleft = str.indexOf("=") + 1;
  theright = str.indexOf("&");
  //theright = str.lastIndexOf("&");
  return(str.substring(theleft, theright));
}

function delineate2 (str)
{
  point = str.lastIndexOf("=");
  return(str.substring(point+1,str.length));
}

function checkLicense ()
{
  switch (myLicense)
  {
  case -1:
    {
      document.write("<BR><BR><B>Your software is not currently licensed. There are many benefits to owning a licensed version of the software. Please wait while we redirect you...</B>");
      return(URLupgrade);
    }
  case 0:
    {
      document.write("<BR><BR><B>Your software is not currently licensed. There are many benefits to owning a licensed version of the software. Please wait while we redirect you...</B>");
      return(URLupgrade);
    }
  case 1:
    {
      document.write("<BR><BR><B>Your software license is current. Thank you for using netYAK.</B>");
      return("");
    }
  case 2:
    {
      document.write("<BR><BR><B>Your software is license for free upgrades is expired. We suggest that you renew your license. Please wait while we redirect you...</B>");
      return(URLrenew);
    }
  default:
    {
      document.write("<BR><BR><B>Your software is not currently licensed. There are many benefits to owning a licensed version of the software. Please wait while we redirect you...</B>");
      return(URLupgrade);
    }
  }
}

function walkQueryString (astr)
{
  var str = astr;
  //document.write(str + "<br><br>");  // test test
  var splitQ = str.split("?");		// two strings, L&R of ?
  //document.write("QueryString=" + "<br><br>");  // test test
  //document.write("QueryStringLength=" + splitQ.length + "<br><br>");  // test test
  //document.write("QueryString=" + splitQ[1] + "<br><br>");  // test test
  if (splitQ.length > 1)
  {
    var splitA = splitQ[1].split("&");	// four strings, by &
    //document.write("splitA=" + splitA[0] + "<br><br>");  // test test
    for (i = 0; i < splitA.length; i++)
      {
        var splitE = splitA[i].split("=");
        switch (splitE[0].toLowerCase())
          {
          case "lic":
            {
              myLicense = parseInt(splitE[1]);
              break;
            }
          case "src":
            {
              mySource = splitE[1];
              break;
            }
          case "type":
            {
              myType = splitE[1].toLowerCase();
              break;
            }
          case "version":
            {
              myVersion = parseInt(splitE[1]);
              break;
            }
          default:
            {
              document.write("<BR><BR>Unknown query string element encountered: " + splitE[0] + "<BR><BR>");
              break;
            }
          }
        //document.write("Attribute=" + splitE[0] + ", Value=" + splitE[1] + "<BR><BR>");  // TEST TEST
      }
  }
}

function myRedirect (strURL)
{
  if (strURL != "")
  {
    parent.location.href=strURL;
  }
  return("");
}


function checkVersion (mytext)
{
  //var tooOldVersion = 109705;
  //var currentVersion = 109800;
  var tooOldVersion = 109800;
  var currentVersion = 110005;
  var version = delineate(mytext);
  //point = str.lastIndexOf("=");
  //return(str.substring(point+1,str.length));

  document.write("Checking version = " +version +" ... ");
  document.write("Done.<br><br>");
  walkQueryString(mytext);
  if (version < tooOldVersion)
  { 
    //If Voice Client version is too old to use		
    document.write("<B>You must upgrade to the latest version.  Your version is no longer supported.<br><br>Please wait while we redirect you ... </B>");
    //pause(15);
    //parent.location.href="http://www.netyak.com/netyak/netyak_download.htm";
    if (myLicense == 1)
      {
        return(URLfreeupgrade);
      }
    return(URLupgrade);
  }

  if (version < currentVersion)
  { 
    //If Voice Client version is earlier than current version		
    document.write("<B>An upgrade is available.  Please wait while we redirect you ... </B>");
    //pause(15);
    //parent.location.href="http://www.netyak.com/netyak/netyak_download.htm";
    if (myLicense == 1)
      {
        return(URLfreeupgrade);
      }
    return(URLupgrade);
  }

  if (version == currentVersion)
  { 
    //If Voice Client version is same as current version		
    //parent.location.href="http://www.netyak.com/netyak/netyak_download.htm"
    document.write("<B>Congratulations! Your version " +delineate(mytext) + " is current. There is no need to upgrade.</B>");
    return(checkLicense());
  }

  if (version > currentVersion)
  { 
    //If Voice Client version is newer than current version		
    //parent.location.href="http://www.netyak.com/netyak/netyak_download.htm"
    document.write("<B>Congratulations! Your version " +delineate(mytext) + " is more than current. There is no need to upgrade.</B>");
    return(checkLicense());
  }

  document.write("<B>Your version information '" + delineate(mytext) + "' is not understood.<br>Please contact <a href='mailto:support@netyak.com?subject=netyak/upgrade.htm version problem:" + delineate(mytext) + "'>Techical Support</a>. </B>");
  document.write("<BR><B>Or <a href=" + URLupgrade + ">click here</a> to upgrade your software. </B>");
  
  return("");
}
