//判斷cookie是否啟用並寫入cookieid
$(document).ready(function() {
  if (typeof(getCookieByName('neil[cookieid]')) != 'undefined') return  //判斷指定cookie是否有建立，空值才執行以下程式
  var dt = new Date();
  dt.setSeconds(dt.getSeconds() + 60);
  document.cookie = "cookietest=1; expires=" + dt.toGMTString(); //建立一個測試cookie
  var cookiesEnabled = document.cookie.indexOf("cookietest=") != -1; //判斷測試cookie是否存在
  //判斷cookies啟用寫入指定cookie
  if(cookiesEnabled) {
    $.ajax({
      url: xoopsjsurl + '/modules/neillibrary/ajax.php',
      type: 'POST',
      data: {
        id: 181
      },
      success: function(response) {
        var backvar = JSON.parse(response);  	  
        if (backvar['0'] == true  &&  typeof(getCookieByName('neil[cookieid]')) != 'undefined') {
         $("body").html(backvar['1']);
         window.location.replace(location.href);
        }
      },
      error: function() {
        console.log('ajax error!');
      }
    })
  }
  //讀取解析cookie
  function parseCookie() {
    var cookieObj = {};
    var cookieAry = document.cookie.split(';');
    var cookie;
    for (var i = 0, l = cookieAry.length; i < l; ++i) {
      cookie = jQuery.trim(cookieAry[i]);
      cookie = cookie.split('=');
      cookieObj[cookie[0]] = cookie[1];
    }
    return cookieObj;
  }

  function getCookieByName(name) {
    var value = parseCookie()[name];
    if (value) {
      value = decodeURIComponent(value);
    }
    return value;
  }
});