var ec = {

   /**
    * Ask the user for confirmation
    * @param message The message to diplay. Default: 'Weet je het zeker?'.
    */
   confirmation: function (message)
   {
      var confirmationMessage = "Weet je het zeker?";
      if (message && message.length > 0)
      {
         confirmationMessage = message;
      }
      return confirm(confirmationMessage);
   },
   /**
    * If a user confirms the request the url request is executed otherwise the use stays on the current page.
    * @param url The url to go to
    * @param message The message to diplay. Default: 'Weet je het zeker?'.
    */
   confirmRequest: function (url, message)
   {
      if (ec.confirmation(message))
      {
         window.location = url;
      }
   },

   /**
    * @param cookieName the name of the cookie to look for.
    * @return the value of the specified cookieName, if no cookie is found an empty string is returned.
    */
   readCookie: function(cookieName)
   {
      var theCookie = "" + document.cookie;
      var indexCookieName = theCookie.indexOf(cookieName);
      if (indexCookieName == -1 || cookieName == "")
      {
         return "";
      }
      var indexSemicolon = theCookie.indexOf(';', indexCookieName);
      if (indexSemicolon == -1)
      {
         indexSemicolon = theCookie.length;
      }
      return unescape(theCookie.substring(indexCookieName + cookieName.length + 1, indexSemicolon));
   },

   /**
    * stores a cookie for the complete domain
    * @param cookieName the name for the cookie
    * @param cookieValue the value of the cookie
    * @param daysToLive the number of days the should live
    */
   setDomainCookie: function(cookieName, cookieValue, daysToLive)
   {
      ec.setCookie(cookieName, cookieValue, daysToLive, "/");
   },
   /**
    * stores a cookie
    * @param cookieName the name for the cookie
    * @param cookieValue the value of the cookie
    * @param daysToLive the number of days the should live
    */
   setCookie: function(cookieName, cookieValue, daysToLive, path)
   {
      var today = new Date();
      var expire = new Date();
      if (daysToLive === null || daysToLive === 0)
      {
         daysToLive = 1;
      }
      expire.setTime(today.getTime() + 3600000 * 24 * daysToLive);
      var cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
      if (path)
      {
         cookie += ";path="+path;
      }
      document.cookie = cookie;
   },
   /**
    *  gets or stores a cookie.
    * If only the cookieName is specified it will return the value of that cookie or an empty string if the cookie isn't found.
    * Otherwise the cookie will be stored under the cookieName with the cookieValue as it's value.
    * @param cookieName the name for the cookie
    * @param cookieValue the value of the cookie
    * @param daysToLive the number of days the should live
    */
   cookie: function(cookieName, cookieValue, daysToLive)
   {
      if (cookieValue=== undefined)
      {
         return ec.readCookie(cookieName);
      }
      else
      {
         ec.setCookie(cookieName, cookieValue, daysToLive);
      }
   },
   /**
    *  gets or stores a cookie.
    * If only the cookieName is specified it will return the javascript object of the value of that cookie or an empty object if the cookie isn't found.
    * Otherwise the cookie will be stored under the cookieName with the jsonData as it's value.
    * @param cookieName the name for the cookie
    * @param jsonData the value of the cookie
    * @param daysToLive the number of days the should live
    */
   jsonCookie: function(cookieName, jsonData, daysToLive)
   {
      if (jsonData === undefined)
      {
         var cookieValue = ec.readCookie(cookieName);
         // no data specified return the value of the cookie
         if (cookieValue === undefined || cookieValue === null || cookieValue == "")
         {
            return {};
         }
         else
         {
            return  jQuery.evalJSON(ec.readCookie(cookieName));
         }
      }
      else
      {
         // store the cookie
         ec.setCookie(cookieName, jQuery.toJSON(jsonData), daysToLive);
         return {};
      }
   },
   deleteCookie: function(cookieName)
   {
      document.cookie = cookieName + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
   },
   /**
    *
    * @param name the request parameter
    * @return the value of the request parameter
    */
   getParam: function (name)
   {
      var nameChecked = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + nameChecked + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.href);
      if (results === null)
      {
         return null;
      }
      else
      {
         return results[1];
      }
   },

   /**
    * set the focus on the login text field if it isn't present it will focus on the first text field
    */
   setTextFieldFocus: function ()
   {
      jQuery("input:text:visible:first").focus();
      jQuery("#userName").focus();
      jQuery("#loginModuleName").focus();
   },

   /**
    * adds on click event to all elements with the confirmDelete style class.
    */
   confirmDelete: function(message)
   {
      jQuery('.confirmDelete').click(function (event)
      {
         if (!ec.confirmation(message))
         {
            event.preventDefault();
         }
      });
   },

   /*

    */
   mailAForm: function(mailDataParams)
   {
      var mailData = {
         "ec_mail_it":"",
         "ec_mail_to[0].email":"cvw@fundament.nl",
         "ec_mail_to[0].name":"EC",
         "ec_mail_xsl":"",
         "ec_mail_subject":"UNAUTHORIZED MAIL SEND",
         "xml_gender":"",
         "xml_initials":"",
         "xml_interpolation":"",
         "xml_surname":"",
         "XMLOnly":""

      };
      //      $(mailData).extend(mailDataParams);
      $.post('/Communitydienst/dummyAction', mailData, function(data)
      {
         //log(data);
         var httpRequestParams = $("ECResponse httpRequest parameter", data).text();
         log("e-mail send. httpRequestParams=" + httpRequestParams);
         //log(communityName);
      });

   },

   /**
    * dynamically 'extend'(implement) a prototype.
    * Can be used for 'late binding' to a prototype
    */
   extendPrototype : function(obj, proto)
   {
      return (function(object)
      {
         for (property in object.prototype)
         {
            this.prototype[property] = object.prototype[property];
         }
         return this;
      }).apply(obj, [proto]);
   },

   showSplashPage: function(url) {
      var splashPageTriggered = 'splashPageTriggered';
      var domain = document.location.protocol+'//'+document.location.hostname;
      if (!document.referrer.startsWith(domain))
      {
         ec.forceSplashPage(url);
         ec.setCookie(splashPageTriggered, 'true');
      }
   },
   forceSplashPage: function(url) {
      document.location = url;
   }

};

ec.selectOptions || (function($)
{
   var _selectOptions = function()
   {
      var theHeight = '18';
      $('.ec_selectoptions').css({'height': theHeight + 'px', 'overflow': 'hidden'});
      $('.option1').parent().click(function()
      {
         var cssLeft = $(this).css('left');
         var cssTop = $(this).css('top');
         if ($(this).css('height') == theHeight + 'px')
         {
            $(this).css({'height': 'auto',
               'z-index': '2000',
               'left': cssLeft,
               'top': cssTop});
         }
         else
         {
            $(this).css({'height': theHeight + 'px',
               'z-index': '1999',
               'position': 'static'});
         }
      });

      ec.confirmDelete();
   };

   ec.selectOptions = _selectOptions;
})(jQuery);

/**
 * add exists funtion to jquery. It returns 'true' if and element is found otherwise false is returned.
 */
jQuery.fn.exists = function()
{
   return jQuery(this).length > 0;
};

/*
 * Send a form using ajax. No page refresh is performed.
 */
jQuery.fn.ajaxForm = function()
{
   var formAction = jQuery(this).attr('action');
   var formData = jQuery(this).serialize();
   jQuery.post(formAction, formData, function(data)
   {
      //log(data);
      var httpRequestParams = jQuery("ECResponse httpRequest parameter", data).text();
      log("e-mail send. httpRequestParams=" + httpRequestParams);
      //log(communityName);
   });
};

String.prototype.startsWith = function(str){
    return (this.indexOf(str) === 0);
};

/**
 *
 * @param params
 *       { startTime: time when the banner must be displayed,
 *         endTime: time when the banner should be removed }
 */
jQuery.fn.timedBanner = function(params)
{
   $(this).hide();

   // only check banner if a startTime and endTime are specified
   if (params && params.startTime && params.endTime)
   {
      var currentTimestamp = +new Date();

      var timedBanner = document.getElementById("timedBanner");
      var startTime = params.startTime;
      var endTime = params.endTime;

      var startTimestamp = Date.parse(startTime);
      var endTimestamp = Date.parse(endTime);

      if (startTimestamp < currentTimestamp && currentTimestamp < endTimestamp)
      {
         // SHOW THE BANNER
         $(this).show();
      }
   }
};


