﻿// INSTATE ALL jQUERY EVENTS AND VARIABLES
function initalizePage()
{
   

    // ENABLE TERMS OF USE SIDING
    $("#termsOfUsePanelContainer").hide();
    $(".termsOfUseLink").click(function()
    {
        $("#termsOfUsePanelContainer").slideToggle("fast");
        return false;
    });

    $("a.showloginhelp").click(function() {
        $("div.loginhelp").slideToggle("slow");
        return false;
    });

    // ENABLE "[more]"('What Is Risk Management') SIDING
    $("#introduction").hide();

    $("#introductionLink").click(function() {
        $("#introduction").slideToggle("slow");
        return false;
    });

    $("#howDoesInsuranceWorkMore").hide();

    $("#howDoesInsuranceWorkMoreLink").click(function() {
         $("#howDoesInsuranceWorkMore").slideToggle("slow");
         return false;
     });

    $("#factorsAffectingPremiums").hide();

    $("#factorsAffectingPremiumsLink").click(function() {
    $("#factorsAffectingPremiums").slideToggle("slow");
        return false;
    });

    $("#whatIsRiskManagement").hide();

    $("#whatIsRiskManagementLink").click(function() {
    $("#whatIsRiskManagement").slideToggle("slow");
        return false;
    });

    $("#riskManagementProcess").hide();

    $("#riskManagementProcessLink").click(function() {
    $("#riskManagementProcess").slideToggle("slow");
        return false;
    });

     //steps to implement risk management

    $("#riskIdentification").hide();

    $("#riskIdentificationLink").click(function() {
    $("#riskIdentification").slideToggle();
        return false;
    });

    $("#quantifyPrioritize").hide();

    $("#quantifyPrioritizeLink").click(function() {
    $("#quantifyPrioritize").slideToggle();
        return false;
    });

    $("#riskSensitive").hide();

    $("#riskSensitiveLink").click(function() {
    $("#riskSensitive").slideToggle();
        return false;
    });

    $("#businessDecisions").hide();

    $("#businessDecisionsLink").click(function() {
    $("#businessDecisions").slideToggle();
        return false;
    });
     
     //Risk Management Tips 
     for (var i = 1; i <= 12; i++) {
        $("#quickTip" + i + "Link").click(function() {
            var quickTip = "#" + $(this).attr('id').replace("Link", "");
            $(quickTip).slideToggle("slow");
            return false;
        });
     }

    // PROFILE PAGE SAVE BUTTON
    $("#saveButtonPanel").hide();

    $("#ProfilePage input").keyup(function() {
         $("#saveButtonPanel").slideDown("slow");
    });
         
    // CHANGE PASSWORD JS VALIDATION
    var changePassword1 = "";
    var changePassword2 = "";
    $("#changePasswordErrorMessageBlock").hide();
    //$(".changePasswordErrorMessageBlock").hide();

    $(".changePasswordInput1").blur(function() {
        changePassword1 = $(this).val();
        changePasswordValidate();
    });

    $(".changePasswordInput2").blur(function() {
        changePassword2 = $(this).val();
        changePasswordValidate();
    });

    function changePasswordValidate() 
    {
        if ((changePassword1 != "") && (changePassword2 != "")) 
        {
            if (changePassword1 == changePassword2) 
            {
                if ( (changePassword1.length > 5) && (changePassword1.length < 26) )
                {
                    toggleErrorPanel(false, "Passwords Match, Good Stuff");
                }
                else 
                {
                    toggleErrorPanel(true, "Passwords must be between 6 and 25 characters.");
                }
            }
            else 
            {
                toggleErrorPanel(true, "Passwords do not match.");
            }
        }
    };
    
    function toggleErrorPanel( showErrorPanel, errorMessage) 
    {
        if(!showErrorPanel)
        {
            $("#changePasswordErrorMessageBlock").slideUp("slow");
            $(".changePasswordButton").removeAttr("disabled");
        } 
        else {
        
            $(".errorLabel")[0].innerHTML = errorMessage;
            $("#changePasswordErrorMessageBlock").slideDown("slow");
            $(".changePasswordButton").attr("disabled", "disabled");
        }
    };
 
   
 
    // GLOW FEATURE    
    $(".userNameBox").focus(function () {
      $(".userNameBox").toggleClass("highlight");
    })

    $(".emailBox").focus(function () {
        $(".emailBox").toggleClass("highlight");
    })

    $(".passwordInput1").focus(function () {
        $(".passwordInput1").toggleClass("highlight");
    })
    
     $(".passwordInput2").focus(function () {
        $(".passwordInput2").toggleClass("highlight");
    })   
           
    $(".agreeToTermsChkBox input[type=checkbox]").focus(function () {
        $(this).toggleClass("highlight");
    })
    
    $(".promoBox").focus(function () {
        $(this).toggleClass("highlight");
    })

    // EBOOK PAGE
    $(".submitEmailButton").load(function() {
        var match = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/.test($(".emailBoxEbook").val());
        if (match) {
            $(".submitEmailButton").removeAttr("disabled");
        }
        else {
            $(".submitEmailButton").attr("disabled", "disabled");
        }
    });

    $(".ebookError").hide();

    $(".nameBoxEbook").blur(function() {
        if ($(this).val() != "") {
            $(".submitEmailButton").removeAttr("disabled");
            $("#ebookNameError").slideUp("slow");
        }
        else {
            $(".submitEmailButton").attr("disabled", "disabled");
            $("#ebookNameError").slideDown("slow");
        }
    });
        
    $(".emailBoxEbook").blur(function() {
        var match = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/.test($(this).val());
        if (match) {
            $(".submitEmailButton").removeAttr("disabled");
            $("#ebookEmailError").slideUp("slow");
        }
        else {
            $(".submitEmailButton").attr("disabled", "disabled");
            $("#ebookEmailError").slideDown("slow");
        }
    });
};

$(function() {
    initalizePage()


    

    // FAQ
    $("#openfaq").click(function() {
        $("#faq>blockquote").slideDown();
        $(".answer_section>blockquote").slideDown();
        $("#faq>div").slideDown();
        return false;
    });

    $("#closefaq").click(function() {
        $("#faq>blockquote").slideUp();
        $(".answer_section>blockquote").slideUp();
        $("#faq>div").slideUp();
        return false;
    });


    $(".question_section").click(function() {
        $(this).parent().next().slideToggle();
        return false;
    });


    $(".question a").click(function() {
        $(this).parent().parent().next().slideToggle();
        return false;
    });
    
    // ENABLE Image bar SLIDING
    $("#imageControlContainer").hide();

    $(".toggleImageLoadLinks").click(function() {
        $("#imageControlContainer").slideToggle();
        return false;
    });
    
    $(".RiskCategoryTitle").click(function() {
        $(this).parent().parent().next().slideToggle();
        return false;
    });
    
    // Check all for risk page
    $(".SelectAllLink").click(function() {
        var parentPanel = $(this).parent().parent().parent().children();
        $(parentPanel).find("input:checkbox").attr("checked", true);
        $(parentPanel).find("input:checkbox[disabled]").attr("checked", false); return false;
    });
    
    $(".DeSelectAllLink").click(function() {
        var parentPanel = $(this).parent().parent().parent().children();
        $(parentPanel).find("input:checkbox").attr("checked", false); return false;
    });
  


    //resource editor

    $('.riskToolName').blur(function() {
        if ($('.riskToolName').val() == "") {
            $.scrollToElement($('.risktoolnamevalidator'), 1300);
        }
    });

});

// MORE/LESS TOGGLE
function toggleMoreLess(elemId) {
    var link = document.getElementById(elemId);
   
    if (link.innerHTML.indexOf("more") != -1) {
        link.innerHTML = "[less]";
    }
    else {  
        link.innerHTML = "[more]";
    }
}
    
// ATTACHMENT EDITOR
var extentions2 = ['ai'];
var extentions3 = ['doc', 'dot', 'htm', 'odt', 'pdf', 'rtf', 'txt', 'wpd', 'wps', 'xls', 'odc', 'ods', 'csv', 'jpg', 'gif', 'png', 'bmp', 'tif', 'tga', 'psd', 'eps' , 'xsl'];
var extentions4 = ['docx', 'docm', 'dotx', 'dotm', 'html', 'jpeg', 'xlsm', 'xlsx', 'xlst' ];
var extentions =  extentions2.concat(extentions3, extentions4);

function fileUploadChecker(){
    var filename = $('.fileUpload')[0].value;    
    var filesplit = filename.split('.');
    var extention = filesplit[filesplit.length-1];    
    var extentionsuse;
    var extentionlength = extention.length;
    var lengthallowed = true;
    
    if(extentionlength==2)
    {
        extentionsuse = extentions2;
    }
    else if(extentionlength==3)
    {
        extentionsuse = extentions3;
    }
    else if(extentionlength==4)
    {
        extentionsuse = extentions4;
    }
    else{
        lengthallowed = false;
    }
        
    var extentionallowed = false;
    
    if(lengthallowed)
    {
        for(var i=0; i<extentionsuse.length; i++)
        {
            if(extention==extentionsuse[i])
            {
                extentionallowed = true;
            }
        }    
    }  
    if(extentionallowed){
        $('.uploadButton').removeAttr('disabled');
        $('.uploadLink').attr('href', '/SecurePortal/Editor/Attachment/Modals/FileUpload.htm?width=270&height=100&modal=true');
        $('.uploadLink').attr('class', 'uploadLink thickbox');
        tb_init('a.thickbox');
    }
    else{
        $('.uploadButton').attr('disabled', 'disabled');
        $('.uploadLink').removeAttr('href');
        $('.uploadLink').attr('class', 'uploadLink');
        
        var message = 'Please choose a file with one of the following extentions: ';
        for(var j=0; j<extentions.length; j++)
        {
            message += '.' + extentions[j] + '  ';
        }
        alert(message);
        $('.fileUpload').value = '';
    }
}

// INVITE DELEGATE PAGE
var email = $(".delegateEmailBox").val();
var delegateEmailIsGood = false;

function checkDelegateEmail() 
{
      var delegateEmail = $(".delegateEmailBox").val();
      var match = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/.test(delegateEmail);
      if (match) 
      {
          $(".delegateEmail").removeAttr('style');
          $(".delegateEmailBox").removeAttr('style');
          delegateEmailIsGood = true;
      }
      else 
      {
          $(".delegateEmail").attr('style', 'color: #f00;');
          $(".delegateEmailBox").attr('style', 'border-color: #f00;');
          delegateEmailIsGood = false;
      }
};
  
function toggleDelegateButton() {        
    if (delegateEmailIsGood)
    {
        $('.delegateSubmitButton').attr('class', 'delegateSubmitButton signupEnabled');
        $(".delegateSubmitButton").removeAttr("disabled");
        
    }
    else 
    { 
        $('.delegateSubmitButton').attr('class', 'delegateSubmitButton signupDisabled');
        $(".delegateSubmitButton").attr("disabled", "disabled");
    }
}

$(".delegateEmailBox").keyup(function() {
      email = $(this).val();
      checkDelegateEmail();
      toggleDelegateButton();
});

$(".delegateEmailBox").blur(function() {
      email = $(this).val();
      $(this).toggleClass("highlight");
      toggleDelegateButton();
});

// RCC
function checkRCCEmail() 
{
    var email = $(".emailTextBox").val();
    var match = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/.test(email);
    if (match) 
    {
        $(".emailLabel").removeAttr('style');
        $(".emailTextBox").removeAttr('style');
        $(".altSubmitEmailButton").removeAttr('disabled');
    }
    else 
    {
        $(".emailLabel").attr('style', 'color: #f00;');
        $(".emailTextBox").attr('style', 'border-color: #f00;');
        $(".altSubmitEmailButton").attr('disabled', 'disabled');
    }
};

$(".emailTextBox").keyup(function() {
      checkRCCEmail();
});

$(document).ready(function()
{
    //HIGHLIGHT FEATURE FOR SEARCH PAGE
    queryString = window.location.search.substring(1);
    queries = queryString.split("&");
    
    for (i=0; i < queries.length; i++) 
    {
        singleQuery = queries[i].split("=");
        if (singleQuery[0] == "hl") 
        {
            terms = singleQuery[1];
            termsListing = terms.split("%20");    
            
            console.log(termsListing);
            
            for( j=0; j < termsListing.length; j++)
            {
                if(termsListing[j].length > 1)
                {
                    console.log("Highlighting - " + termsListing[j]);    
                   $("#searchResults").highlight(termsListing[j]);
                }
            }
        }
    }
});


/* ScrollToElement 1.0
* Copyright (c) 2009 Lauri Huovila, Neovica Oy
* lauri.huovila@neovica.fi
* http://www.neovica.fi
*
* Dual licensed under the MIT and GPL licenses.
*/
(function($) {
$.scrollToElement = function( $element, speed ) {

speed = speed || 750;

$("html, body").animate({
scrollTop: $element.offset().top,
scrollLeft: $element.offset().left
}, speed);
return $element;
};

$.fn.scrollTo = function( speed ) {
speed = speed || "normal";
return $.scrollToElement( this, speed );
};
})(jQuery);