﻿
/*
Reloads current page
*/
function ReloadPage() 
{ 
    window.location.reload();
}     

/*
Redirects browser to different page.
*/
function RedirectPage(url)
{
    parent.window.location.href = url;
}
    
/*
    Add the class "selected" to an item in the site's main navigation.
    @param anID the ID attribute of the link to apply the class to
*/
function fSelectMainNavLink(anID) {
    
    var strSelector = "#" + anID;
    $(strSelector).addClass('selected');
    $(strSelector).parents('li').prev(strMainNavSeparatorSelector).css('background','#ffffff').addClass('hide');
    $(strSelector).parents('li').next(strMainNavSeparatorSelector).css('background','#ffffff').addClass('hide');
}
    
/*
	Function to determine if it is a valid Canadian postal code.
	@param aPostalCode the Canadian postal code to test
*/
function fIsValidCanadianPostalCode(aPostalCode) {
    var objRegExp = /^([A-Za-z][0-9][A-Za-z] [0-9][A-Za-z][0-9])$/;
//alert("objRegExp.test(\"" + aPostalCode + "\") = " + objRegExp.test(aPostalCode) + "\n");
    return objRegExp.test(aPostalCode);
}

/*
	Function to determine if it is a valid us zip code
	@param aZip the zipcode to test
	@param shouldAllowNineDigits a boolean which will pass a zipcode such as "06001-1234" if passed as true
*/
function fIsValidUsZip(aZip, shouldAllowNineDigits) {
    var objRegExp;
    if(shouldAllowNineDigits) {
    	objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
    } else {
    	objRegExp = /(^\d{5}$)/;
    }
//alert("objRegExp.test(\"" + aZip + "\") = " + objRegExp.test(aZip) + "\n");
    return objRegExp.test(aZip);
}

//	Recursive function that returns the passed string with leading and trailing white space removed.
function fTrim(aString) {
	var output = aString;
	
	if(aString != undefined && aString != "") {
		var intFirstSpaceIndex = aString.indexOf(" ");
		var intLastSpaceIndex = aString.lastIndexOf(" ");
		var boolLeadingWhiteSpace = intFirstSpaceIndex == 0;
		var intLastCharIndex = aString.length - 1;
		var boolTrailingWhiteSpace = intLastSpaceIndex == intLastCharIndex;
			
	//alert("aString = " + aString + "\nintLastSpaceIndex = " + intLastSpaceIndex + "\nboolTrailingWhiteSpace = " + boolTrailingWhiteSpace + "\n");
		if(boolLeadingWhiteSpace) {
			output = fTrim(aString.substr(1));
		} else if(boolTrailingWhiteSpace) {
			output = fTrim(aString.substr(0, intLastCharIndex));
		} else {	//	The base case: no leading or trailing whitespace exists
			output = aString;
		}
	}
	
	return output;
}



/*
  Return the prefix appended to the beginning of a .NET web control by the 
  server.  The supplied string represents a classname that has been applied to 
  one of the web controls.  Using jquery, we can obtain the id attribute of a 
  form element with that class name, strip out the server-appended prefix and 
  return it.
  @param strFieldClassName the class attribute of one of the form fields
*/
function fGetDotNetPrefix(strFieldClassName) {
    var debug = "";

    debug += "fGetDotNetPrefix(\"" + strFieldClassName + "\") called.\n";
    //alert(debug);
      var strResponse = $("." + strFieldClassName).attr("id");
    debug += "strResponse = " + strResponse + "\n";
      if(strResponse == undefined) {
		    strResponse = "";
      } else {
		    var intIndex = strResponse.lastIndexOf("_");
		    if(intIndex == -1) {
			    strResponse = "";
		    } else {
			    strResponse = strResponse.substr(0, intIndex + 1);
    debug += "strResponse = " + strResponse + "\n";
		    }
      }
      
    //alert(debug);
  return strResponse;
}


/*
	Verify that the user has chosen a password that satisfies the requirements.
*/
function fRegulatePassword(source, clientside_arguments) {
	var boolValid = false;
	var strDotNetIDPrefix = fGetDotNetPrefix("text");
	var intPasswordLength = document.getElementById(strDotNetIDPrefix + "txtPassword").value.length;
	if(intPasswordLength == 0 || intPasswordLength >= 5) {
	    boolValid = true;
	}
	clientside_arguments.IsValid = boolValid;
}



//Moves cursor automatically from one phone textbox to the next.
function PhoneAutoMove(){
	var strDotNetIDPrefix;
	var strDotNetNamePrefix;

	strDotNetIDPrefix = fGetDotNetPrefix("text");
    //alert("strDotNetIDPrefix = " + strDotNetIDPrefix);
	strDotNetNamePrefix = strDotNetIDPrefix.replace(/_/g, "$");

    $('.formInputPhone1').keydown(function(event){
        if(event.keyCode >= 65 && event.keyCode <= 90){ // character codes for a-z	
          return false;
        }
    });
	
    $('.formInputPhone2').keydown(function(event){
        if(event.keyCode >= 65 && event.keyCode <= 90){ // character codes for a-z	
          return false;
        }
    });
		
    $('.formInputPhone1').keyup(function(event){
    if((event.keyCode >= 48 && event.keyCode <= 57)||(event.keyCode >= 96 && event.keyCode <= 105)){ // character codes for 0-9
     if($(this).val().length == 3){  	      
        var thisID = $(this).attr('id');
        var found = false;
        $(':text').each(function(){
          if ($(this).attr('id') == thisID){
            found = true;              
          }else if (found){
            found = false;
            $(this).focus();
          }
        });
      }
     }
    });	
	
    $('.formInputPhone2').keyup(function(event){
    if((event.keyCode >= 48 && event.keyCode <= 57)||(event.keyCode >= 96 && event.keyCode <= 105)){ // character codes for 0-9
     if($(this).val().length == 4){  	      
        var thisID = $(this).attr('id');
        var found = false;
        $(':text').each(function(){
          if ($(this).attr('id') == thisID){
            found = true;              
          }else if (found){
            found = false;
            $(this).focus();
          }
        });
      }
     }
    });	
};		

function SetUpFAQs() {
    /****************************************************
                               FAQ
    *****************************************************/ 
    $('.hideReveal').toggle(function(){
        var strID = $(this).attr('id');
      
          //CLOSES ALL THE PREVIOUSLY OPEN BOX AND CHNAGES THE IMAGE *
         $('.ArrowImg').attr('src',$('.ArrowImg').attr('src').replace("_on.png","_off.png"));
         $('.FAQBoxes').hide();             
         $('#img_'+strID).attr('src',$('#img_'+strID).attr('src').replace("_off.png","_on.png"));
        $('#faq_'+strID).show();
       
    },function(){
         var strID = $(this).attr('id');
         $('#img_'+strID).attr('src',$('#img_'+strID).attr('src').replace("_on.png","_off.png"));
        $('#faq_'+strID).hide();
    }); 
    /***********************************************************
                            End FAQ
    ***********************************************************/
};


/****************************************************
                               BeCertain
*****************************************************/ 
function SetUpBeCertain() {
  $('.popupDiv').hide();
      
         $('#performanceslideshow').cycle({
           timeout: 6000
         });
        $('#styleslideshow').cycle({
           timeout: 6000
         });
        $('#energyslideshow').cycle({
           timeout: 6000
         });
         $('#sustainabilityslideshow').cycle({
           timeout: 6000
         });
       $('#showPerformance').click(function () 
          { 
           $('.popupDiv').hide();
           $('#PerformanceDiv').show();
          });
          
        $('#PerformanceDiv').click(function()
        {
        $(this).hide();
        
        });
        
        $('#showStyle').click(function () 
          { 
           $('.popupDiv').hide();
          $('#StyleDiv').show();
         
          });
          
        $('#StyleDiv').click(function()
        {
        $(this).hide();
        
        });
        
         $('#showEnergy').click(function () 
          { 
           $('.popupDiv').hide();
          $('#EnergyDiv').show();
         
          });
          
        $('#EnergyDiv').click(function()
        {
        $(this).hide();
        
        });
        
         $('#showSustainability').click(function () 
          { 
           $('.popupDiv').hide();
          $('#SustainabilityDiv').show();
         
          });
          
        $('#SustainabilityDiv').click(function()
        {
        $(this).hide();
        
        });

};

/****************************************************
                         End BeCertain
*****************************************************/ 
/****************************************************
                               BeCertainmobile
*****************************************************/ 
function SetUpBeCertainMobile() {

         
       $('.popupDiv').hide();
        
       $('#showPerformance').click(function () 
          { 
           
           $('.popupDiv').hide();
           $('#PerformanceDivmob').show();
          });
          
        $('#PerformanceDivmob').click(function()
        {
        $(this).hide();
        
        });
        
        $('#showStyle').click(function () 
          { 
           
           $('.popupDiv').hide();
          $('#StyleDivmob').show();
         
          });
          
        $('#StyleDivmob').click(function()
        {
        $(this).hide();
        
        });
        
         $('#showEnergy').click(function () 
          { 
           $('.popupDiv').hide();
          $('#EnergyDivmob').show();
         
          });
          
        $('#EnergyDivmob').click(function()
        {
        $(this).hide();
        
        });
        
         $('#showSustainability').click(function () 
          { 
           $('.popupDiv').hide();
          $('#SustainabilityDivmob').show();
         
          });
          
        $('#SustainabilityDivmob').click(function()
        {
        $(this).hide();
        
        });
          
      };

/****************************************************
                         End BeCertainmobile
*****************************************************/ 



/****************************************************
                      sustainableinsulation
*****************************************************/ 
function SetUpSustainableInsulation() {
  $('.popupDiv').hide();
       


      $('#showqualityofliving').click(function () 
          { 
           $('.popupDiv').hide();
           $('#QualityLivingOuterDiv').show();
          });
          
        $('#QualityLivingOuterDiv').click(function()
        {
            $(this).hide();
        
        });
        
        $('#showrenewable').click(function () 
          { 
           $('.popupDiv').hide();
           $('#RenewableOuterDiv').show();
          });
          
        $('#RenewableOuterDiv').click(function()
        {
            $(this).hide();
        
        });
        
        $('#showmakeitmove').click(function () 
          { 
           $('.popupDiv').hide();
           $('#MakeItMoveOuterDiv').show();
          });
          
        $('#MakeItMoveOuterDiv').click(function()
        {
            $(this).hide();
        
        });
        
         $('#showinstallation').click(function () 
          { 
           $('.popupDiv').hide();
           $('#InstallationOuterDiv').show();
          });
          
        $('#InstallationOuterDiv').click(function()
        {
            $(this).hide();
        
        });

};

/****************************************************
                         End sustainableinsulation
*****************************************************/ 

/****************************************************
                      sustainableinsulationmobile
*****************************************************/ 
function SetUpSustainableInsulationMobile() {
  $('.popupDiv').hide();
       


      $('#showqualityofliving').click(function () 
          { 
           $('.popupDiv').hide();
           $('#QualityLivingOuterDivmob').show();
          });
          
        $('#QualityLivingOuterDivmob').click(function()
        {
            $(this).hide();
        
        });
        
        $('#showrenewable').click(function () 
          { 
           $('.popupDiv').hide();
           $('#RenewableOuterDivmob').show();
          });
          
        $('#RenewableOuterDivmob').click(function()
        {
            $(this).hide();
        
        });
        
        $('#showmakeitmove').click(function () 
          { 
           $('.popupDiv').hide();
           $('#MakeItMoveOuterDivmob').show();
          });
          
        $('#MakeItMoveOuterDivmob').click(function()
        {
            $(this).hide();
        
        });
        
         $('#showinstallation').click(function () 
          { 
           $('.popupDiv').hide();
           $('#InstallationOuterDivmob').show();
          });
          
        $('#InstallationOuterDivmob').click(function()
        {
            $(this).hide();
        
        });

};

/****************************************************
                         End sustainableinsulationmobile
*****************************************************/ 
/*******************************************
           AirRenew function
******************************************/           
function rotateBanner() {
 $('#bannerslideshow').cycle({
           timeout: 4000
         });
	
}
//*********end airrenew*******************************//

/****************************************************
    The following will add the proper English designator 
    to any link that has been tagged with the given classes
    *****************************************************/ 
$(document).ready(function()
       {    
        
     
       if ($.cookie("UserLanguage") != null && $.cookie("UserLanguage") == 'fr-CA' )
         {
           $('.addlanguagedash_sc').each(function()
            {     
              $(this).text
               (
	            $(this).text() + " - Anglais"
              );
            }); 
        
            
           $('.addlanguageparen_sc').each(function()
            {     
              $(this).text
               (
	            $(this).text() + " (Anglais)"
              );
            }); 
            
         }
         
        else if ($.cookie("UserLanguage") != null && $.cookie("UserLanguage") == 'es-MX' )
        {
  
           $('.addlanguagedash_sc').each(function()
            {     
              $(this).text
               (
	            $(this).text() + ' - Inglés').html()
            }); 
        
            
           $('.addlanguageparen_sc').each(function()
            {     
             $(this).text(
            $(this).text() + ' (Inglés)').html();

             
            }); 
         }



      
      });
      
    function OpenCorporateSites() 
    {
        var WinSettings = "center:yes;resizable:no;dialogHeight:510px;dialogWidth:1010px"

        var MyArgs = window.showModalDialog('corporatesites.aspx', window, WinSettings);
    
    }
    
    
    /******************************************************************
    The following is used to popup the event information in a "bubble"
    *******************************************************************/ 
   
var mousePositionX;
var mousePositionY;
var pagePositionX;
var pagePositionY;

function getMousePosition(){
 //Continually Getting mouse position       
 $(document).mousemove(function(e){
    pagePositionX = e.pageX;
    pagePositionY = e.pageY
    mousePositionX = pagePositionX - $(window).scrollLeft();
    mousePositionY = pagePositionY - $(window).scrollTop();
 }); 
}
  
function activateCalendarPopUp(evt, divID){
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
    }

    var x, y, winX, winY, xPosition, yPosition, div, divClass, divTop, divLeft;

    //Hide other open pop ups
    $(".popUpBubbleLeftTop").hide();
    $(".popUpBubbleRightTop").hide();
    $(".popUpBubbleLeftBottom").hide();
    $(".popUpBubbleRightBottom").hide();

    x = mousePositionX;
    y = mousePositionY;
     
     //Next, get size of window
     winX = $(window).width();
     winY = $(window).height();

     //alert(x + " - " + winX + "\n" + y + " - " + winY);

     //Figure out the horizontal orientation of the pop up
     if ((winX - x < 350) && (x > 350)){ 
     // if ((winX - x < 150) && (x > 150)){ 
        //If the cursor was closer than 350px to the right of the window, 
        //the pop up should show up on the left of the cursor (Img name = right)
        //(unless the cursor is also < 350px from the left, in which case the window is small and the user will have to scroll to the right)
        xPosition = "right";
       // alert("Left of Cursor");        
     }
     else{ 
        //The cursor was not 350px from the right, or the window is too small, 
        //the popup should show up on the right of the cursor (Img name = left)
        xPosition = "left";
        //alert("Right of Cursor");        
     }
     
     //Figure out the vertical orientation of the pop up
     if ((winY - y < 250) && (y > 250)){ 
     //if ((winY - y < 150) && (y > 150)){
        //If the cursor was closer than 425px to the bottom of the window, 
        //the pop up should show up on the top of the cursor (Img name = bottom)
        //(unless the cursor is also < 425px from the top, in which case the window is small and the user will have to scroll down)
        yPosition = "bottom";
        //alert("Top of Cursor");
        
     }
     else{ 
        //The cursor was not 425px from the bottom, or the window is too small, 
        //the popup should show up on the bottom of the cursor (Img name = Top)
        yPosition = "top";
        //alert("Bottom of Cursor");
        
     }
    
     //Get Div Object 
    div = $("#" + divID);
     
     //Set div Object with Correct CSS Class & Background
     //Then Position div correctly
     if ($(div)){
     //alert(div)
       if (ieversion==6)
       {
       
       divClass = "popupdiv";
       $(div).addClass(divClass);
         $(div).show();
         return;
       }
      $(div).css({"top" : ''});
      $(div).css({"bottom" : ''});
      $(div).css({"left" : ''});
      $(div).css({"right" : ''});
   
        if (xPosition == "left" && yPosition == "top"){
            divClass = "popUpBubbleLeftTop popupdiv";
            divTop = pagePositionY - 10 + "px";
            divLeft = pagePositionX + 15 + "px";
             $(div).css({"top" : divTop});
             $(div).css({"left" : divLeft});
             //alert("TopLEFT");
        }
        else if (xPosition == "right" && yPosition == "top"){
            divClass = "popUpBubbleRightTop popupdiv";
            divTop = pagePositionY - 10 + "px";
            divLeft = pagePositionX - 360 + "px";
             $(div).css({"top" : divTop});
             $(div).css({"left" : divLeft});
             //alert("TopRight");
        }
        else if (xPosition == "left" && yPosition == "bottom"){
            divClass = "popUpBubbleLeftBottom popupdiv";
            divTop = winY - (pagePositionY+5)  + "px";
            
            divLeft = pagePositionX + 5 + "px";
             //alert("BotomLEFT");
            
             $(div).css({"bottom" : divTop});
             $(div).css({"left" : divLeft});
        }
        else if (xPosition == "right" && yPosition == "bottom"){
            divClass = "popUpBubbleRightBottom popupdiv";
            divTop = winY - (pagePositionY+5) + "px";
            divLeft = pagePositionX - 360 + "px";
             //alert("BotomRight");
            
              $(div).css({"bottom" : divTop});
             $(div).css({"left" : divLeft});
        }
        
        $(div).removeClass();
        $(div).addClass(divClass);
        //alert(divClass)
      
     }
     
     //Show Div
     $(div).show();
     
}

//function to pop up window without scroll bars
function popUpScroll(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=1000,height=690');");
    eval("page" + id + ".moveTo(0,0);");
}

/*CODE PERTAINING TO ENERGYTAXCREDIT*/

function executeETCJS() //This function is the "main function" called from codebehind of EnergyTaxCredit.ascx - used to call all other functions
{
    //$('#slide1_Content').show();
    $('.selContractor').attr('checked','');//RESET contractor type Selection
    $('.selPType').attr('checked','');//RESET type of service(retailer/distributor)
    $('.corpETCSelDistance').attr('selected','10');//Reset the distance to 10 miles
    $('#txtFAPZip').val('');//RESET the FAP zip CODE
    $('#txtWTBZip').val('');//RESET the WTB zip CODE
    $('.error').hide();//HIDES ALL THE ERRORs

    toggleETCBottomSectionLogos();
    sideBarSlideFunctionality();    
}

function toggleETCBottomSectionLogos(){
   $('.onOffState').hover(function(){
        imgSrc = $(this).attr('src');
        imgOnSrc = imgSrc.replace('_off','_on');
        $(this).attr('src',imgOnSrc);
   },function(){
        imgOnSrc = $(this).attr('src');
        imgSrc = imgOnSrc.replace('_on','_off');
        $(this).attr('src',imgSrc);
   });   
   
   $('.onOffStateEnergyStar').hover(function(){
        imgSrc = $(this).attr('src');
        imgOnSrc = imgSrc.replace('gray','blue');
        $(this).attr('src',imgOnSrc);
   },function(){
        imgOnSrc = $(this).attr('src');
        imgSrc = imgOnSrc.replace('blue','gray');
        $(this).attr('src',imgSrc);
   }); 
}

/*****************************
SLIDE FUNCTION
*****************************/
function sideBarSlideFunctionality()
{
    $('.corpETCSlideToggle,#lnk_slide2,#lnk_slide3').click(function(){
  // alert("there"); 
       strID = $(this).attr('id');
       sp  =strID.split('_');
       
       
       $('.corpETCSelContractor').each(function(){
            $(this).attr('checked','');
       });
       
      //alert(strID);
      
      if($('#'+sp[1]+'_Content').hasClass('Open') )
      {
        // finding the image directly under the slider link
        imgID = $('#lnk_'+sp[1]+'> img').attr('src').replace('down', 'right');
        $('#lnk_'+sp[1]+'> img').attr('src',imgID); 
         $('#'+sp[1]+'_Content').hide();//Allows user to close the open slider box
        $('#'+sp[1]+'_Content').removeClass('Open') ;
      }
      else  
      {
            $('.corpETCSliderContent').hide();
            $('.corpETCSlideToggle').each(function(){
                eID = $(this).attr("id");
                //alert("eID: "+eID); 
                 eSp = eID.split("_");
                 // finding the image directly under the slider link
                 imgID = $('#lnk_'+sp[1]+'> img').attr('src').replace('down', 'right');
                $('#lnk_'+sp[1]+'> img').attr('src',imgID);    
            }); 
           
             $('.corpETCSliderContent').removeClass('Open') ;
                $('#'+sp[1]+'_Content').show();
           $('#'+sp[1]+'_Content').toggleClass('Open') ;
           // finding the image directly under the slider link
           imgID = $('#lnk_'+sp[1]+'> img').attr('src').replace('right', 'down');
           $('#lnk_'+sp[1]+'> img').attr('src',imgID);  
           if(sp[1]=="slide2"){
                $('#hdnPro').val('pro');
                $('#hdnWhere').val('');
           } else if(sp[1]=="slide3"){
                $('#hdnPro').val('');
                $('#hdnWhere').val('where');
           } 
          $('.error').hide();
       }
    });
}

/*END CODE PERTAINING TO ENERGYTAXCREDIT*/
						
function fVerifySelect(strDotNetIDPrefix, strID) {
//alert("document.getElementById(\"" + strDotNetIDPrefix + strID + "\") = " + document.getElementById(strDotNetIDPrefix + strID));
	if(document.getElementById(strDotNetIDPrefix + strID).selectedIndex > 0) {
		return true;
	} else {
		return false;
	}
}


//function for pop up - taken from Legacy, used multiple places, including Tech Pages of Pro Sitelette for Addtl. Resources.
function popUpScrollLibraryCulture(URL, culture) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=940,height=690');");
    eval("page" + id + ".moveTo(0,0);");
}

/*****************************
POP UP FUNCTION
*****************************/
function setPopUpLinks() {
    $('.openInNewWin').click(function(){
        openPopUpWindow(this.href);
        return false;
    });
}

function openPopUpWindow(url) {
    // defaults
    var width = 900;
    var height = 550;
    
    // adjust height and width if larger than set resolution
    if (screen.width - width < 100) {
        width = screen.width - 100;
    }
    if (screen.height - height < 100) {
        height = screen.height - 100;
    }
    
    // calculate where left and top need to be for centering
    // window was too low, so subtracted another 50 from top
    var left = (screen.width - width) / 2;
    var top = (screen.height - height - 50) / 2;
    
    if (left < 0 ) left = 0;
    if (top < 0) top = 0;
    
    mywindow = window.open(url, '', 'width=' + width + ', height=' + height + ', scrollbars=1, resizable=1, location=0, directories=0, menubar=0, toolbar=0, status=0, top=' + top + ', left=' + left + '');   
    
    // use moveTo also for wider browser compatibility
    mywindow.moveTo(left,top);
}
