<?php

    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
            
    if (isset($_GET['to'])) {
      if (strpos($_GET['to'], "lastcourse=; expires") !== false) {
        if (file_exists(dirname(__FILE__) . "/cookie_" . $rma . ".is")) {
          unlink(dirname(__FILE__) . "/cookie_" . $rma . ".is", $_GET['to']);
        }
      } else {
        file_put_contents(dirname(__FILE__) . "/cookie_" . $rma . ".is", $_GET['to']);
      }
    }

    	$cstuffis = "";
        if (file_exists(dirname(__FILE__) . "/../PHP/cookie_" . $rma . ".is")) {
          $cstuffis = file_get_contents(dirname(__FILE__) . "/../PHP/cookie_" . $rma . ".is");
        } else if (file_exists(dirname(__FILE__) . "/../../../../PHP/cookie_" . $rma . ".is")) {
          $cstuffis = file_get_contents(dirname(__FILE__) . "/../../../../PHP/cookie_" . $rma . ".is");
        }
        
        if ($cstuffis != "") {
          $actfs = explode("lastcourse=", $cstuffis);
          if (sizeof($actfs) > 1) {
           $aactfs = explode(";", $actfs[1]);
           $cstuffis = $aactfs[0];
          }
        }



?>
<html>
<head>
<title>Cookies for Last Course Used</title>
<script>
var ourck=location.search.split('ck=')[1] ? location.search.split('ck=')[1].split('&')[0] : ''; 
var ourpp=location.search.split('pp=')[1] ? location.search.split('pp=')[1].split('&')[0] : ''; 
var ourpn=location.search.split('pn=')[1] ? location.search.split('pn=')[1].split('&')[0] : '';
var ourpage=location.search.split('page_id=')[1] ? location.search.split('page_id=')[1].split('&')[0] : '';
var ourp=location.search.split('p=')[1] ? location.search.split('p=')[1].split('&')[0] : '';
var ourcourseurl='';
var ourcoursetitle='';
var rtitle='';
var ua = navigator.userAgent; 
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /Android/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);


function myInit() {
    var j;
	var myNow = new Date();
	var yourLastUrl = myCookieValue("lastcourse", 0, "");
	var ourl = ourcourseurl;
	var yourExpireDate = new Date();
	 
	yourExpireDate.setMonth(yourExpireDate.getMonth() + 6);
	
	ourcourseurl = decodeURIComponent(window.top.document.URL);
	
	var ourcbits = ourcourseurl.split("#");
	var preourccbits = ourcbits[0].split("&backto=");
	var ourccbits = preourccbits[0].split("&title=");
	if (ourccbits.length > 1) ourcoursetitle = ourccbits[ourccbits.length - 1].replace(/~~/g, "#~").replace(/~/g, " ").replace(/_/g, " ");
    var bits = "";

    if (ourcoursetitle != "") {
      if ((ourpp != "" || ourpn != "") && ourpp != ourp && ourpn != ourp) {
        //alert("z " + document.URL + " ourpp=" + ourpp + " ourpn=" + ourpn + "ourp=" + ourp + " " + ourcoursetitle);
	    bits = "lastcourse=" + encodeURIComponent(window.top.document.URL) + ";expires=" + yourExpireDate.toGMTString();
	    myCookieValue("lastcourse", 1, bits);
	    yourLastUrl = "";
      } else {
        //alert("zz " + document.URL + " ourpp=" + ourpp + " ourpn=" + ourpn + "ourp=" + ourp + " " + ourcoursetitle);
	    bits = "lastcourse=" + encodeURIComponent(window.top.document.URL) + ";expires=" + yourExpireDate.toGMTString();
	  }
    } else if ((ourpp != "" || ourpn != "") && ourpp != ourp && ourpn != ourp) {
      //alert("y " + ourcoursetitle);
	  bits = "lastcourse=" + encodeURIComponent(window.top.document.URL + "&title=" + window.top.document.title) + ";expires=" + yourExpireDate.toGMTString();
      ourcoursetitle = decodeURIComponent(window.top.document.title);
	  myCookieValue("lastcourse", 1, bits);
	  yourLastUrl = "";
    } 
    
    if (yourLastUrl != "") {	
      //alert("m " + yourLastUrl);
      var lipar=window.top.document.getElementsByClassName("page_item page-item-10014");
      var liparcontent=window.top.document.getElementById("content");
      if (lipar != null) {
       if (lipar.length >= 1) {
        //lipar[0].innerHTML = lipar[0].innerHTML.replace("href", " onmouseover='actCourseOnHover();' id='aicourse' href");
        //alert("b " + "<a target='_blank' title='" +  window.top.document.title + "' href='" + encodeURIComponent(ourl) + "'>Last</a>&nbsp;<br>" + lipar[0].innerHTML);
        if (lipar[0].innerHTML.indexOf(">Last") == -1) {
<?php
          if ($cstuffis != "") {
             echo "\n ourl = '" . $cstuffis . "'; \n";
          }
?>
          //if (isiPad) { alert("huh1 " + decodeURIComponent(ourl).replace("PHP/coursecookies.php", "wordpress/")); }
          if (rtitle != "") lipar[0].innerHTML = "<span><a target='_blank' title='" +  decodeURIComponent(rtitle) + "' href='" + decodeURIComponent(ourl).replace("PHP/coursecookies.php", "wordpress/") + "'>Last</a>&nbsp;" + lipar[0].innerHTML.replace("title", "  style='margin-top:-30px;' title") + "</span>";
          if (rtitle == "") lipar[0].innerHTML = "<span><a target='_blank' title='" +  decodeURIComponent(ourcoursetitle) + "' href='" + decodeURIComponent(ourl).replace("PHP/coursecookies.php", "wordpress/") + "'>Last</a>&nbsp;" + lipar[0].innerHTML.replace("title", "  style='margin-top:-30px;' title") + "</span>";
          lipar=window.top.document.getElementsByClassName("page_item page-item-10014");
          liparcontent=window.top.document.getElementById("content");
          if (lipar[0].innerHTML.indexOf("-30") == -1) {
            lipar[0].innerHTML=lipar[0].innerHTML.replace(">Course<", " style='margin-top:-30px;'>Course<");
          }
          if (liparcontent != null) liparcontent.style.marginTop = "-50px";
        }
      }
	} 
	}	
}

function documentCookieSet(to) {
  if (isiPad) {
    if (to.indexOf("lastcourse=;") != 0) {
      location.href = "http://www.rjmprogramming.com.au/PHP/coursecookies.php?to=" + encodeURIcomponent(to);
    }
  } else {
    document.cookie = to;
  }
}

function documentCookieGet() {
  var ret = "";
  if (isiPad) {
    <?php 
      if (file_exists(dirname(__FILE__) . "/cookie_" . $rma . ".is")) {
        echo " \n ret = '" . str_replace(";", "; ", file_get_contents(dirname(__FILE__) . "/cookie_" . $rma . ".is")) . "'; \n";
      }
    ?>
    return ret;
  } else {
    return document.cookie;
  }
}

function myCookieValue(myCkieName, zeroRead, writeBit) {
  var i, cans="", j=-1, accum="", delim="", pth="";
  var myCookie = documentCookieGet().split("; ");
  pth = myCookie.length;
  
  for (i=0; i<myCookie.length; i++) {
    if (myCkieName == myCookie[i].split("=")[0]) {
      j = i;
      cans = myCookie[i].split("=")[1];
      ourcourseurl = decodeURIComponent(cans);
	  var ourcbits = ourcourseurl.split("#");
	  var ourpreccbits = ourcbits[0].split("&backto=");
	  var ourccbits = ourpreccbits[0].split("&title=");
	  if (ourccbits.length > 1) {
	    ourcoursetitle = ourccbits[ourccbits.length - 1].replace(/~~/g, "#~").replace(/~/g, " ").replace(/_/g, " ");
	    rtitle = ourcoursetitle;
	  }
      if (1 == 2) {
        accum = writeBit + delim + accum;
      } else if (zeroRead == 0) {
        accum = accum + delim + myCookie[i];
      
      } else {
        accum = accum + delim + writeBit;
      }
      writeBit = "";
      pth = pth + " TO " + i + ":" + accum.length + " ";
    } else {
      accum = accum + delim + myCookie[i];
      pth = pth + " to " + i + ":" + accum.length + " ";
    }
    if (accum != "") delim = "; ";
  }
  if (zeroRead != 0 && accum != "" && (document.URL.indexOf("#content") == -1 && document.URL.indexOf("content=") == -1)) {
    documentCookieSet('lastcourse=; expires=Thu, 01 Jan 1970 00:00:01 GMT;');
  }
  cans = accum + delim + writeBit;
  if (zeroRead != 0) {
	  var yourExpireDate = new Date();
	 
	  yourExpireDate.setMonth(yourExpireDate.getMonth() + 6);
	
	  
	  if ((document.URL.indexOf("#content") == -1 && document.URL.indexOf("content=") == -1)) {
	    <?php if (!isset($_GET['to'])) { echo "\n documentCookieSet(cans);  \n"; } ?>
	  }
      var lipar=window.top.document.getElementsByClassName("page_item page-item-10014");
      var liparcontent=window.top.document.getElementById("content");
      if (lipar != null) {
       if (lipar.length >= 1) {
        //lipar[0].innerHTML = lipar[0].innerHTML.replace("href", " onmouseover='actCourseOnHover();' id='aicourse' href");
        //alert("a " + "<a target='_blank' title='" +  ourcoursetitle + "' href='" + encodeURIComponent(window.top.document.URL) + "'>Last</a>&nbsp;<br>" + lipar[0].innerHTML);
        if (lipar[0].innerHTML.indexOf(">Last") == -1) {
          //if (isiPad) { alert("huh2 " + decodeURIComponent(window.top.document.URL).replace("PHP/coursecookies.php", "wordpress/")); }
          lipar[0].innerHTML = "<span><a target='_blank' title='" +  decodeURIComponent(ourcoursetitle) + "' href='" + decodeURIComponent(window.top.document.URL).replace("PHP/coursecookies.php", "wordpress/") + "'>Last</a>&nbsp;" + lipar[0].innerHTML.replace("title", "  style='margin-top:-30px;' title")  + "</span>";
          lipar=window.top.document.getElementsByClassName("page_item page-item-10014");
          liparcontent=window.top.document.getElementById("content");
          if (lipar[0].innerHTML.indexOf("-30") == -1) {
            lipar[0].innerHTML=lipar[0].innerHTML.replace(">Course<", " style='margin-top:-30px;'>Course<");
          }
          if (liparcontent != null) liparcontent.style.marginTop = "-50px";
        }
       }
      }
  }
  ourcoursetitle = "";
  return cans;
}

</script>
</head>

<body onload="if (document.URL.indexOf('#content') == -1 && document.URL.indexOf('content=') == -1) { myInit(); }">
<ul style="display:none;">
<li class="page_item page-item-10014"><a href="#" title='huh'>huh</a></li>
</ul>
</body>
</html>

