/**
  * Cookie stuff
  *
  *
  */

// a really easy "crypt" function because we don't want the raw data in cookie
function crypt (Str)
{
   var s = new String;
   var i;
   var c = new Number;                   

   for (i=0;i<Str.length;i++)
   {
      c = Str.charCodeAt (i)^i;
      if (c < 16)
         s += '0'+c.toString (16);
      else
         s += c.toString (16);
   }

   return s;
}

function decrypt (Str)
{
   var s = new String;
   var i;

   for (i=0; i<Str.length; i+=2)
      s = s + String.fromCharCode (parseInt (Str.substr(i ,2), 16)^(i/2));

   return s;
}

// n is cookiename
function readCookie (n)
{
    a = document.cookie;
    res = '';
    while(a != '')
    {
        cookiename = a.substring (0, a.search('='));
        cookiewert = a.substring (a.search('=') +1, a.search (';'));
        if (cookiewert == '')
            cookiewert = a.substring (a.search ('=')  +1, a.length);

        if (n == cookiename)
        {
            res = cookiewert;
            res = decrypt (res);
            break;
        }

        i = a.search (';') +1;
        if (i == 0)
            i = a.length;
            
        a = a.substring (i, a.length);
    }
      
    return res;
}
  
function getProbeCounter ()
{
    var val = readCookie ('probe_limit');
    
    return ((!val)?1:++val);
}

function incProbeLimit ()
{
    var data = getProbeCounter ();
    
    var expDate = new Date();
    expDate = new Date (expDate.getTime() + 1000 * 60 * 60 * 24 *30); // about 1 month
    
    data = crypt (data.toString ());
    document.cookie = 'probe_limit=' + data + '; expires=' + expDate.toGMTString () +';' ; 
}

function addProbeName (strProductName)
{
    var strProbeNames = readCookie ('probe_names');
    
    if (strProbeNames)
        strProbeNames += ':' + strProductName;
    else
        strProbeNames = strProductName;
    
    var expDate = new Date();
    expDate = new Date (expDate.getTime() + 1000 * 60 * 60 * 24 *30); // about 1 month
    
    strProbeNames = crypt (strProbeNames.toString ());
    
    document.cookie = 'probe_names=' + strProbeNames + '; expires=' + expDate.toGMTString () +';' ;
}

function updateProbeCookie (strProductName)
{
    if (strProductName)
        addProbeName (strProductName);
    
    incProbeLimit ();
}

// check if probe order limit has been reached
function isProbeLimit ()
{
    if (!document.cookie)
        return false;
        
    return ((getProbeCounter () > 3))?true:false;
}

// check if probe has been ordered already
function isProbeName (strProductName)
{
    var isLimit = false;

    if (!document.cookie)
        return false;
        
    var val = readCookie ('probe_names');
    
    if (!val)
        return false;
    
    var probeNames = val.split (':');
    
    for(i=0; i < probeNames.length; i++)
    {
        if (strProductName == probeNames[i])
        {
            isLimit = true;
            break;
        }
    }

    return isLimit;
}  
        
if (isProbeLimit () || isProbeName (document.getElementById ('product').value))
{
    document.getElementById ('pp_block').style.display = "none";
    document.getElementById ('pp_block').style.visibility = "hidden";
}
else
{
    document.getElementById ('pp_block').style.display = "inline";
    document.getElementById ('pp_block').style.visibility = "visible";
}

// if state is 'thanks' always show text
if (document.getElementById ('state').value == 7)
{
    document.getElementById ('pp_block').style.display = "inline";
    document.getElementById ('pp_block').style.visibility = "visible";
}