//
// Global configuration settings
//
var sAjaxServerUrl = "http://saintjohn.roimanager.info/server_js_ajax.php";
var sClickTag = 'panadvertClick';
    //
    // http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
    //
    function getUrlVars() {
      var vars = [], hash;
      var hashes = window.location.href.slice (window.location.href.indexOf ('?') + 1).split ('&');

      for(var i = 0; i < hashes.length; i++) {
          hash = hashes[i].split ('=');
          vars.push (hash[0]);
          vars[hash[0]] = hash[1];
      }
      return vars;
    }

    //
    // http://jquery-howto.blogspot.com/2010/09/jquery-cookies-getsetdelete-plugin.html
    //
    function setCookie (name, value, secs) {
      if (secs) {
        var date = new Date();
        date.setTime (date.getTime() + (secs*1000));
        var expires = "; expires=" + date.toGMTString();
      } else {
        var expires = "";
      }
      document.cookie = name+"="+value+expires+"; path=/";
    }

    function getCookie (name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split (';');

      for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') 
          c = c.substring (1, c.length);
        if (c.indexOf (nameEQ) == 0) 
          return c.substring (nameEQ.length, c.length);
      }
      return null;
    }

    function deleteCookie (name) {
      setCookie (name, "", -1);
    }

    $(document).ready(function() {
      var aUrlVars = getUrlVars();
      var sCurrentPanadvertCookie = getCookie (sClickTag);
      var aData = [];
      var sShortcode;
      var sURL = '';
      var sSubmit = '';

      if (aUrlVars['pac'] !== undefined) {
        setCookie (sClickTag, aUrlVars['pac'], 1);
        sShortcode = aUrlVars['pac'];
      } else if (sCurrentPanadvertCookie === null) {
        setCookie (sClickTag, 'other', 86400);
        sShortcode = 'other';
      } else {
        sShortcode = getCookie ('panadvertClick');
      }

      aData = { shortcode: sShortcode, URL: window.location.href, URI: window.location.pathname, hostname: window.location.hostname, submit: 0 };
      $.post(sAjaxServerUrl, aData,
        function (data) {
          if (data.lifetime != '') {
//            alert (data.lifetime);
            setCookie (sClickTag, sShortcode, data.lifetime);
          } else {
            alert ("Error from server: " + data.lifetime);
          }
        }, "json"
      );

      $('#booking_form').submit(function(event) {
        var formMethod = this.method.toLowerCase();
        var self = this;

        $.ajaxSetup({async:false});
        aData = { shortcode: sShortcode, URL: window.location.href, URI: window.location.pathname, hostname: window.location.hostname, formaction: this.action, formtarget: this.target, formname: this.name, formmethod: this.method, submit: 1 };
        $.post(sAjaxServerUrl, aData,
          function (data) {
            if (data.error != '0') {
              alert ("Error from server: " + data.error);
            } else {
              if (data.lifetime != '') {
                setCookie (sClickTag, sShortcode, data.lifetime);
              }

              if (data.action != '') {
                if (formMethod == 'get') {
                  self.action = data.action;
                } else {
                  $('#booking_form').append (data.action);
                }
              } else {
                alert ("No ACTION");
              }
            }
          }, "json"
        );
        return true;
      });
    });

