/* 


Author: Jazman RACGP September 2011


 */

$(document).ready(function ()
{
   // Put login lables into the fields and do the fade on focus

   $("#homelogin label").inFieldLabels();



   // Make gplearning logo  link to homepage

   $('#gplearninglogo').click(function ()
   {
      location.href = 'home.aspx'
   });


   // add to bookmarks

   $("#bookmark").click(function ()
   {
      var bookmarkUrl = this.href;
      var bookmarkTitle = this.title;

      if($.browser.mozilla) // For Mozilla Firefox Bookmark
      {
         window.sidebar.addPanel(bookmarkTitle, bookmarkUrl, "");
      }
      else if($.browser.msie || $.browser.webkit) // For IE Favorite
      {
         window.external.AddFavorite(bookmarkUrl, bookmarkTitle);
      }
      else if($.browser.opera) // For Opera Browsers
      {
         $("#bookmark").attr("href", bookmarkUrl);
         $("#bookmark").attr("title", bookmarkTitle);
         $("#bookmark").attr("rel", "sidebar");
         $("#bookmark").click();
      }
      else // for other browsers which does not support
      {
         alert('Please hold CTRL+D and click the link to bookmark it in your browser.');
      }
      return false;
   });

   // Highlight terms nav

   // get current url
   var currentUrl = $(location).attr('href');


   //get the url folder after base url
   var splitUrl = (currentUrl.split("/")[4]);

   // alert(splitUrl);

   switch(splitUrl)
   {

      case "privacy.aspx":

         $("#navprivacy").addClass("selected");

         break;

      case "terms.aspx":

         $("#navterms").addClass("selected");

         break;


      case "partner.aspx":

         $("#navpartner").addClass("selected");

         break;

      default:

   }

      $("#GPLearningSignUp_CreateBirthdateTextBox").datepicker({
         showOn: "button",
         buttonImage: "./images/calendar.gif",
         buttonImageOnly: true,
         showOtherMonths: true,
         selectOtherMonths: true,
         changeMonth: true,
         changeYear: true,
         dateFormat: "dd/mm/yy",
         yearRange: "-111:-18"
      });

      $("#GPLearningSignUp_CreateCountryDropDownList").change(UpdateAddressValidation);

      $("#GPLearningSignUp_CreatePostcodeTextBox").keypress(InputNumbersOnlyWithZero);
      $("#GPLearningSignUp_CreatePhoneHomeTextBox").keypress(InputPhoneNumber);
      $("#GPLearningSignUp_CreatePhoneWorkTextBox").keypress(InputPhoneNumber);
      $("#GPLearningSignUp_CreatePhoneMobileTextBox").keypress(InputPhoneNumber);
   });




   function InputNumbersOnlyWithZero(event)
   {
      // Backspace, tab, enter, end, home, left, right
      // We don't support the del key in Opera because del == . == 46.
      var controlKeys = [8, 9, 13, 35, 36, 37, 39];

      // IE doesn't support indexOf
      var isControlKey = controlKeys.join(",").match(new RegExp(event.which));

      // Some browsers just don't raise events for control keys. Easy.
      // e.g. Safari backspace.
      if(!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
            (48 <= event.which && event.which <= 57) || // Always 0 through 9
            isControlKey)
      { // Opera assigns values for control keys.
         return;
      }
      else
      {
         event.preventDefault();
      }
   }
   function InputPhoneNumber(event)
   {
      // Backspace, tab, enter, end, home, left, right
      // We don't support the del key in Opera because del == . == 46.
      var controlKeys = [8, 9, 13, 35, 36, 37, 39];

      // IE doesn't support indexOf
      var isControlKey = controlKeys.join(",").match(new RegExp(event.which));

      // Some browsers just don't raise events for control keys. Easy.
      // e.g. Safari backspace.
      if(!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
            (48 <= event.which && event.which <= 57) || // Always 0 through 9
            32 == event.which ||
            isControlKey)
      { // Opera assigns values for control keys.
         return;
      }
      else
      {
         event.preventDefault();
      }
   }

   function UpdateAddressValidation()
   {
      if("australia" == $("#GPLearningSignUp_CreateCountryDropDownList").val().toLowerCase())
      {
         $("#GPLearningSignUp_CreateStateDropDownList").val("");
         $("#GPLearningSignUp_CreateStateDropDownList").removeAttr("disabled");
      }
      else
      {
         $("#GPLearningSignUp_CreateStateDropDownList").val("O");
         $("#GPLearningSignUp_CreateStateDropDownList").attr("disabled", "disabled");
      }
   }

   function GPLearningSignUp_CreatePostcodeCustomValidator_ClientValidate(source, args)
   {
      var regExTest = "^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$";
      if("australia" != $("#GPLearningSignUp_CreateCountryDropDownList").val().toLowerCase())
      {
         regExTest = "^([0-9]{1,8})$";
      }

      var regEx = new RegExp(regExTest);
      if(null != regEx.exec($("#GPLearningSignUp_CreatePostcodeTextBox").val()))
      {
         args.IsValid = true;
      }
      else
      {
         args.IsValid = false;
      }
   }
   function GPLearningSignUp_CreatePhoneCustomValidator_ClientValidate(source, args)
   {
      args.IsValid = false;

      var phoneHome = document.getElementById("GPLearningSignUp_CreatePhoneHomeTextBox");
      var phoneWork = document.getElementById("GPLearningSignUp_CreatePhoneWorkTextBox");
      var phoneMobile = document.getElementById("GPLearningSignUp_CreatePhoneMobileTextBox");

      if((false == args.IsValid) && (null != phoneHome) && ("" != phoneHome.value))
      {
         args.IsValid = true;
      }
      if((false == args.IsValid) && (null != phoneWork) && ("" != phoneWork.value))
      {
         args.IsValid = true;
      }
      if((false == args.IsValid) && (null != phoneMobile) && ("" != phoneMobile.value))
      {
         args.IsValid = true;
      }
   }
   function GPLearningSignUp_CreatePhoneHomeCustomValidator_ClientValidate(source, args)
   {
      args.IsValid = true;

      if("" != $("#GPLearningSignUp_CreatePhoneHomeTextBox").val())
      {
         var regExTest = "^(0)(2|3|7|8)( )[0-9]{4}( )[0-9]{4}$";
         if("australia" != $("#GPLearningSignUp_CreateCountryDropDownList").val().toLowerCase())
         {
            regExTest = "^([0-9| ]{1,20})$";
         }

         var regEx = new RegExp(regExTest);

         if(null != regEx.exec($("#GPLearningSignUp_CreatePhoneHomeTextBox").val()))
         {
            args.IsValid = true;
         }
         else
         {
            args.IsValid = false;
         }
      }
   }
   function GPLearningSignUp_CreatePhoneWorkCustomValidator_ClientValidate(source, args)
   {
      args.IsValid = true;

      if("" != $("#GPLearningSignUp_CreatePhoneWorkTextBox").val())
      {
         var regExTest = "^(0)(2|3|7|8)( )[0-9]{4}( )[0-9]{4}$";
         if("australia" != $("#GPLearningSignUp_CreateCountryDropDownList").val().toLowerCase())
         {
            regExTest = "^([0-9| ]{1,20})$";
         }

         var regEx = new RegExp(regExTest);
         if(null != regEx.exec($("#GPLearningSignUp_CreatePhoneWorkTextBox").val()))
         {
            args.IsValid = true;
         }
         else
         {
            args.IsValid = false;
         }
      }
   }
   function GPLearningSignUp_CreatePhoneMobileCustomValidator_ClientValidate(source, args)
   {
      args.IsValid = true;

      if("" != $("#GPLearningSignUp_CreatePhoneMobileTextBox").val())
      {
         var regExTest = "^(0)(4)[0-9]{2}( )[0-9]{3}( )[0-9]{3}$";
         if("australia" != $("#GPLearningSignUp_CreateCountryDropDownList").val().toLowerCase())
         {
            regExTest = "^([0-9| ]{1,20})$";
         }

         var regEx = new RegExp(regExTest);
         if(null != regEx.exec($("#GPLearningSignUp_CreatePhoneMobileTextBox").val()))
         {
            args.IsValid = true;
         }
         else
         {
            args.IsValid = false;
         }
      }
   }
   function GPLearningSignUp_TermsAndConditionsCustomValidator_ClientValidate(source, args)
   {
      var chkTandC = document.getElementById("GPLearningSignUp_TermsAndConditionsCheckBox");

      if(null != chkTandC)
      {
         args.IsValid = chkTandC.checked;
      }
   }

