/// <reference name="MicrosoftAjax.js"/>
/// <reference path="~/Script/jQuery/jquery.js" />

/*-------------------------------------------------------------------------
*$Id$
* This file will handle the way to show validation summary. It shows the validation summary in a popup dialog.
* AUTHOR : Tran Hoang Chuong
* DEPENDENCIES :
* - Page_ClientValidate : This offical method handle to validate all validators in a page.
* - Page_ValidationSummaries : an array of validation summaries (but please use only one validation summary per page)
-------------------------------------------------------------------------*/
function wrapOfficialClientValition()
{
   if (typeof (Page_ClientValidate) == 'function')
   {
      gfncOldPageValidateClient = Page_ClientValidate; //keeps the original function
      Page_ClientValidate = function(validationGroup)
      {
         var blnShowMessageBox = false;
         var validationSummary = null;
         //checks if the validation summary shows error message in a message box
         if (typeof (Page_ValidationSummaries) != 'undefined')
         {
            var validationSummary = Page_ValidationSummaries[0];
            if (validationSummary.showmessagebox == "True")
            {
               blnShowMessageBox = true;
               validationSummary.showmessagebox = false;
               validationSummary.customeShowMessageBox = true;
            }
            else if (typeof (validationSummary.customeShowMessageBox) != 'undefined')
               blnShowMessageBox = validationSummary.customeShowMessageBox;
         }
         //let the original function do validation
         var result = gfncOldPageValidateClient(validationGroup);
         // This code is for PhoneNumberControl. DO NOT TOUCH THEM!
         if (typeof cg_phonenumber_forceupdate != "undefined")
            cg_phonenumber_forceupdate();
         if (typeof cg_validatePhoneNumbers != 'undefined')
         {
            var _validatephonenumber = cg_validatePhoneNumbers();
            // Disable those code since the error message already be displayed inside the phonenumber control.
            // In the future, if we want to change it to show the message in the summaryvalidator then enable it back.
            //              if (!_validatephonenumber) {
            //                  // Build up the li object to include in the summary validation control.
            //                  var _ul = $(validationSummary).find("ul:first");
            //                  if (_ul.length == 0) {
            //                      $(validationSummary).append($("<ul>"));
            //                  }
            //                  $(validationSummary).find("ul:first").append($("<li>").text(TextResources.PhoneNumberIncorrect));
            //              }
            result = result && _validatephonenumber;
         }

         //handles to show error meesage
         if (typeof (Page_ValidationSummaries) != 'undefined')
         {
            if (!result)
            {
               if (blnShowMessageBox && validationSummary)
               {
                  var strErrorMessage = $($('<div></div>').html($(validationSummary).clone())).html();
                  validationSummary.style.display = "none";
                  var dlg = cgAlert(window.document.title, strErrorMessage);
                  dlg.setSize(dlg.getInnerWindow().document.body.scrollWidth + 30, dlg.getH());
                  dlg.moveCenter();
                  dlg.getInnerWindow().focus();
               }
            }
         }
         return result;
      }
   }
}
