﻿$(document).ready(function () {
   
    //LOGON FORM
    $('#password-clear').show();
    $('#password-password').hide();

    $('#password-clear').focus(function () {
        $('#password-clear').hide();
        $('#password-password').show();
        $('#password-password').focus();
    });
    $('#password-password').blur(function () {
        if ($('#password-password').val() == '') {
            $('#password-clear').show();
            $('#password-password').hide();
        }
    });



    $("ul.sf-menu").supersubs({
        minWidth: 12,   
        maxWidth: 27,   
        extraWidth: 1     
    }).superfish();



    $('.default-value').each(function () {
        var default_value = this.value;
        $(this).focus(function () {
            if (this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function () {
            if (this.value == '') {
                this.value = default_value;
            }
        });
    });


    initFancyboxes();




});



  function openWindow(URL,SHEIGHT,SWIDTH)  {
	window.open(URL,'PopUp','top=100,left=20,width=' + SWIDTH + ',height=' + SHEIGHT + ',scrollbars=yes,resizable=no,location=0,toolbar=no,menubar=no');
  }

  function initFancyboxes() {
      $("a.zoom").fancybox({
          titlePosition: 'inside',
          'width': 700,
          'height': 500,
          'autoScale': false,
          'autoDimensions': false,
          'centerOnScroll': true
      });
  }


  function initColorbox(){
      $("a.zoom").colorbox();
      alert('test');
  }


  function openInFancyBox(url) {
      $.fancybox({
          //'orig'			: $(this),
          'padding': 0,
          'href': '/Resources/Pages/Events/EventDetailModal.aspx?EventID=6',
          'title': 'Invoice Details',
          'transitionIn': 'elastic',
          'transitionOut': 'elastic'
      });
  }

  function openFancy(url) {
      $.fancybox({
          'padding': 0,
          'href': url,
          'title': 'Invoice Details',
          'transitionIn': 'elastic',
          'transitionOut': 'elastic',
          'width': '75%',
          'height': '75%',
          'type': 'iframe',
          'autoscale': false
      });
  }


  //CalendarMonth('calendar_large', 'back')
  function CalendarMonth(divID, month, year, rowcount, colcount) {

      $.ajax({
          type: "POST",
          url: "/Resources/WebServices/AjaxCalls.asmx/GetCalendarHTML",
          //data: "{'Month':'" + month + "','fav':'false'}",
          data: "{'Month':'" + month + "','Year':'" + year + "','RowCount':'" + rowcount + "','ColCount':'" + colcount + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {
              //alert('test');
              // Replace the div's content with the page method's return.
              //$("#Result").text(msg.d);
              //alert(msg.d);
              //$(divID).text(msg.d);
              var odiv = document.getElementById(divID);
              odiv.innerHTML = msg.d;
              initFancyboxes();
              //initColorbox();
          },

          error: function (e) {
              alert('Error: ' + e);
          }
      });

      


      return false;

  }

  function LoginForm() {
      var email = document.getElementById('txt_username').value;
      var password = document.getElementById('password-password').value;
      var loggedin = false;

      $.ajax({
          type: "POST",
          url: "/Resources/WebServices/AjaxCalls.asmx/CheckUser",
          data: "{'Email':'" + email + "','Password':'" + password + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {

              //alert('test');
              // Replace the div's content with the page method's return.
              //$("#Result").text(msg.d);
              //alert(msg.d);
              //$(divID).text(msg.d);
              //var odiv = document.getElementById(divID);
              //odiv.innerHTML = msg.d;
              //initFancyboxes();
              //initColorbox();

              if (msg.d == true) {
                  //alert('Logged in!');
                  //return true;
                  loggedin = true;
              }
              else {
                  alert('Invalid Username / Password');
                  loggedin = false;
              }

              loadLogonForm();
          },

          error: function (e) {
              alert('Error: ' + e);
          }
      
      });

      


  }

  function loadLogonForm() {
      //alert(document.location.pathname);
  $.ajax({
          type: "POST",
          url: "/Resources/WebServices/AjaxCalls.asmx/GetLoginFormHTML",
          data: "{'PageURL':'" + document.location.pathname + "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {

              //alert('test');
              // Replace the div's content with the page method's return.
              //$("#Result").text(msg.d);
              //alert(msg.d);
              //$(divID).text(msg.d);
              var odiv = document.getElementById('loginform');
              odiv.innerHTML = msg.d;
              //initFancyboxes();
              //initColorbox();
          },

          error: function (e) {
              alert('Error: ' + e);
          }
      });
  }


  function getQuerystring(key, default_) {
      if (default_ == null) default_ = "";
      key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
      var qs = regex.exec(window.location.href);
      if (qs == null)
          return default_;
      else
          return qs[1];
  }



  function isDate(dateStr) {
      //Modified by DO 12/31/2003
      var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;

      var matchArray = dateStr.match(datePat); // is the format ok?

      var datestatus = true;

      datemsg = "";



      if (matchArray == null || matchArray[1] == null) {

          datemsg = "-----> Please enter date as mm/dd/yyyy " + "\n";

          return false;

      }

      else {

          if (matchArray[3] = null || matchArray[5] == null) {

              datemsg = "-----> Please enter date as mm/dd/yyyy " + "\n";

              return false;

          }

      }



      month = matchArray[1]; // p@rse date into variables



      day = matchArray[3];



      year = matchArray[5];



      if (month < 1 || month > 12) { // check month range

          datemsg = datemsg + "-----> Month must be between 1 and 12." + "\n";

          datestatus = false;

      }



      if (day < 1 || day > 31) {

          datemsg = datemsg + "-----> Day must be between 1 and 31." + "\n";

          datestatus = false;

      }



      if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {

          datemsg = datemsg + "-----> Month " + month + " doesn`t have 31 days!" + "\n";

          datestatus = false;

      }



      if (month == 2) { // check for february 29th

          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

          if (day > 29 || (day == 29 && !isleap)) {

              datemsg = datemsg + "-----> February " + year + " doesn`t have " + day + " days!" + "\n";

              datestatus = false;

          }

      }

      return datestatus;

  }


  function getDropdownValue(DropdownID) {
      var ctl = document.getElementById(DropdownID);
      var idx = ctl.selectedIndex;
      var sval = ctl.options[idx].value;

      return sval;
  }
