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
    * @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)
   {
      var today = new Date();
      var expire = new Date();
      if (daysToLive === null || daysToLive === 0)
      {
         daysToLive = 1;
      }
      expire.setTime(today.getTime() + 3600000 * 24 * daysToLive);
      document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
   },

   /**
    *
    * @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);
      });

   }
};



;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);
   });
};
