﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
// This is the initial jQuery starting point.  The 'main' function is run once the DOM of every page is established.

  $(document).ready(function() {
    main();
  });

  function main() {
        
         loadTab(1,1); //After page loading, active tab 1
         $("#preloader").hide();
       //loadTab(taboffset,iframe)
         $("#content_tab1").click(function(e){e.preventDefault(); loadTab(1,1);}); //Here e.preventDefault(); is to prevent the respective href from going the user off the link, that is the href url '#' which is appending to the URL will going off 
         $("#content_tab2").click(function(e){e.preventDefault(); loadTab(2,0);});
         $("#content_tab3").click(function(e){e.preventDefault(); loadTab(3,1);});    
        
  } 

  // This function must be called with all modal dialog boxes which have fields that
  // require validation.  It performs a call to the ASP.Net function, "Page_ClientValidate()",
  // and then continues on calling "CloseModalDialog()" if the validation is okay.
  // Note: Surprisingly, companion functions like "ValidatorValidate()" do not work, 
  // perhaps due to the way ThickBox rearranges DOM elements.
  function CheckValidationBeforeClose(element) {
    if (Page_ClientValidate())
      CloseModalDialog(element);
  }

  // Used to close a ThickBox modal dialog and then force a postback,
  // which a server-side control is seemingly unable to do on its own
  // when fired from within the confines of a ThickBox object.
  function CloseModalDialog(element) {
    tb_remove();
    setTimeout('__doPostBack(\'' + element.name + '\',\'\')', 500);  // 500ms seems to give ThickBox enough time to remove itself
  }


  // Used to close a ThickBox modal dialog without causing a postback.
  function CancelModalDialog() {
    tb_remove();
  }

  // Prepares all textboxes such that the background changes to a distinct
  // color upon focus and then returns to white after focus is lost.
  function PrepareDefaultEventHandlers() {
    $(":text").focus(textboxHighlight).blur(textboxRemoveHighlight);
    $(":password").focus(textboxHighlight).blur(textboxRemoveHighlight);
    $("textarea").focus(textboxHighlight).blur(textboxRemoveHighlight);
  }

  function textboxHighlight() {
    $("#" + this.id).css({ 'background-color': '#E1DCD5' });
  }

  function textboxRemoveHighlight() {
    $("#" + this.id).css({ 'background-color': 'white' });
  }
  
  var reqPageUrl = new Array();   
    reqPageUrl[1] = "/consumer/forms/register.aspx";
    reqPageUrl[2] = "/consumer/forms/empty.aspx";
    reqPageUrl[3] = "/consumer/forms/register.aspx";        
  
         
  function loadTab(id,iframe){
   
    if (reqPageUrl[id].length > 0){
      $("#preloader").show();
      $.ajax({
        url: reqPageUrl[id], 
        cache: false,
        error: function(XMLHttpRequest, textStatus, errorThrown)
          {
          $('#tabmenu a').removeClass('active'); //remove all class='active' for all anchors
          $("#content_tab"+id).toggleClass('active'); //add class to the current one
          $("#content").empty().append('<img src=\"images/error_caution.gif\" /> Error in Loading page, please do check with the path');//if there is any error in the request
          $("#preloader").hide();//hide the preloader
          },
        success: function(message) 
         {
          $('#tabmenu a').removeClass('active'); //remove all class='active' for all anchors
          $("#content_tab"+id).toggleClass('active'); //add class to the current one                
          if (iframe){            
            $("#content").empty().append('<iframe frameborder="0" height="500" width="500" src="' + reqPageUrl[id] +'"></iframe>');//first empty the content, then append content
          }
          else{
          $("#content").empty().append(message);//first empty the content, then append content
          }
          $("#preloader").hide();//hide the preloader
         }
      });
    }
  }
  function pageLoad(){
    PrepareDefaultEventHandlers();
  }
  

function portfolioResize(areaDivName) {
    if (gup("print") != "yes" && gup("print") != "true")
    {
        var areaDiv = document.getElementById(areaDivName);
        if (areaDiv) {
            var contentsHeight = areaDiv.scrollHeight;
            var theIframe = window.parent.document.getElementById("ctl00_GlobalHeader1_PortfolioPanel1_theIframe");
            var maxIframeHeight;
            if (!$.browser.msie) {
                maxIframeHeight = window.parent.innerHeight - 160;
            }
            else {
                maxIframeHeight = window.parent.document.documentElement.clientHeight - 160;
            }

            if (maxIframeHeight > contentsHeight) {
                theIframe.setAttribute("height", contentsHeight);
                areaDiv.style.height = contentsHeight + "px";
            }
            else {
                theIframe.setAttribute("height", maxIframeHeight);
                areaDiv.style.height = maxIframeHeight + "px";
            }
        }
    }
}

function ValidateGroups(buttonId, validationGroupList) {

    $("#" + buttonId).click(function(e) {       

        if (window.location.toString().indexOf("/RMI") > -1) {
            if (isPacketChecked == "on") {
                $("#rmiExpand1").addClass("expandOpened");
                $("#rmiExpand1 .expandInner").show();
            }
        }

        //clear out previous error messaging
        $(".errorHeading").hide();
        $(".correctMe").each(function(i) {
            this.className = this.className.replace("correctMe", "");
        });
        HideValidationErrors();

        //Bind the logic to the specified button with jQuery
        var list = validationGroupList.split(',');
        Page_IsValid = true;
        //Loop through the entire set of page validators
        $.each(Page_Validators, function() {

            if ((this.validationGroup && ExistsGroup(list, this.validationGroup)) || (!this.validationGroup && ExistsGroup(list, ''))) {
                ValidatorValidate(this, this.validationGroup);
                Page_IsValid = Page_IsValid && this.isvalid;

                var fieldToHighlight = null;
                if (this.className != "") {
                    if (document.getElementById(this.className)) {
                        fieldToHighlight = document.getElementById(this.className);
                    }
                    else if (document.getElementById("ctl00_ContentPlaceHolder1_" + this.className)) {
                        fieldToHighlight = document.getElementById("ctl00_ContentPlaceHolder1_" + this.className);
                    }
                }

                if (!this.isvalid && fieldToHighlight) {
                    fieldToHighlight.className += " correctMe";
                    $(".errorHeading").show();
                }
            }
        });
        //Reflect validation in ValidationSummary's if there are any in the page
        $.each(list, function() {
            ValidationSummaryOnSubmit(this);
        });

        //If Page_IsValid is false the execution flow will be interrupted
        return Page_IsValid;
        e.preventDefault();
    });
}

function ExistsGroup(list, group) {
    var found = false;
    for (i = 0; i < list.length; i++) {
        if (list[i] == group) {
            found = true;
            break;
        }
    }
    return found;
}

function HideValidationErrors() {  
    //Hide all validation errors      
    if (window.Page_Validators){
      for (var vI = 0; vI < Page_Validators.length; vI++) {  
        var vValidator = Page_Validators[vI];  
        vValidator.isvalid = true;  
        ValidatorUpdateDisplay(vValidator);  
      }
    }  
    //Hide all validaiton summaries  
    if (typeof (Page_ValidationSummaries) != "undefined") { //hide the validation summaries  
      for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {  
        summary = Page_ValidationSummaries[sums];  
        summary.style.display = "none";  
      }  
    }  
  }

  function lightboxResize() {     
      if (parent.frames.length > 0) {
          var areaDiv = document.getElementById("lightboxContent");
          if (areaDiv) {
              var isCta = false;
              if (window.parent.document.getElementById("ctaFormPanel")) {
                if(window.parent.document.getElementById("ctaFormPanel").className.indexOf("open") > -1) {
                    isCta = true;
                }              
              }

              var contentsHeight = areaDiv.scrollHeight;
              var theIframe;
              var theContainer;

              if (isCta) {
                  theIframe = window.parent.document.getElementById("ctaIFrame");
                  theContainer = window.parent.document.getElementById("ctaFormPanel");
              }
              else {
                  theIframe = window.parent.document.getElementById("ctl00_Lightbox1_lightboxIFrame");
                  theContainer = window.parent.document.getElementById("lightboxPanel");
              }
              
              if (theIframe.width == 627) {
                  document.getElementsByTagName("body")[0].className += " wide";
              };

              var maxIframeHeight;
              if (!$.browser.msie) {
                  maxIframeHeight = window.parent.innerHeight - 170;
              }
              else {
                  maxIframeHeight = window.parent.document.documentElement.clientHeight - 170;
              }

              if (maxIframeHeight > contentsHeight) {
                  theIframe.setAttribute("height", contentsHeight + 100);
                  areaDiv.style.height = contentsHeight + "px";
                  if (!isCta) {
                      theContainer.style.marginTop = (0 - (contentsHeight + 60) / 2) + "px";
                  }
              }
              else {
                  theIframe.setAttribute("height", maxIframeHeight + 100);
                  areaDiv.style.height = maxIframeHeight + "px";
                  if (!isCta) {
                      theContainer.style.marginTop = (0 - (maxIframeHeight + 60) / 2) + "px";
                  }
              }
          }       
          
      }
}

