﻿/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  ecl_global.js
 * ##### VERSION: v1.0
 * ##### UPDATED: 22/07/08 (Eclipse Group)
/* ########################################################################### *
/* ########################################################################### *
/* ***** INDEX *************************************************************** *
/* ########################################################################### *
/* ##### INITIALISATION
/* #####
/* ##### FUNCTIONALITY:
/* ##### Check all checkboxes
/* ##### Tooltip
/* ##### Popup window
/* ##### Smooth anchor scroll
/* #####
/* ##### PRESENTATION
/* ##### Table alternate rows
/* ##### IE6 tweaks
/* ##### PNG fix
/* ##### Iframe resize
/* ##### Style Sheet Switch
/* ##### Font Size Switch   (CR # 763)
/* ########################################################################### */
/* ########################################################################### *
/* ##### INITIALISATION
/* ########################################################################### */
$(document).ready(ecl_init_globalFunctionality);
$(document).ready(ecl_init_globalPresentation);
function ecl_init_globalFunctionality()
{
    // Code Added By Joy Saha (CR # SCBT)
    // Browser Detect
    //detect_browser();
    // Code Added By Joy Saha (CR # SCBT)

    // Tooltips
    ecl_tooltip();
    
    // Password Strength check
    check_password_strength();
    
    // Search box
    ecl_search();
    // Terms and Conditions, Privacy etc
    ecl_replace();
    
    // Popup window
    ecl_popup_window();
    
    // Forms
    ecl_forms_checkAllCheckboxes();    
    
    // Accessibility: Font Size Buttons & Skip Content
    ecl_accessibilityFontSize();    
    ecl_skipContent();
    
    // Enable smooth anchor scrolling
    ecl_init_anchorSmoothScroll();
    
    // Forms: Select input on click
    ecl_forms_selectOnClick();
    
    // Add a class to all external links
    ecl_init_externalLinkTarget();
    
}
function ecl_init_globalPresentation()
{
    // Homepage Random Images
    ecl_randomImage();    
    
    // Table alternate rows
    ecl_tableAltRows();    
    
    // IE tweaks
    ecl_ieTweaks();
    
    // Buttons
    /*ecl_forms_buttons();*/
    
    // Check Styles
    checkStyles();

    // CR # 763
    // Font Resize
    checkFontStyles();
    // CR # 763
}
/* ########################################################################### *
/* ##### FUNCTIONALITY
/* ########################################################################### */
/* ##### FORMS */
// Check all checkboxes
// Note: This only supports 1 check all button.
// DESC: Mark all checkable checkboxes with class .ecl_forms_checkableWithAll, mark the 'click to check all' checkbox with .ecl_forms_checkAllCheckboxes
function ecl_forms_checkAllCheckboxes()
{
    $('.ecl_forms_checkAllCheckboxes').click(function()
    {
        var checkedStatus = this.checked;
        $('.ecl_forms_checkableWithAll').each(function()
        {
            this.checked = checkedStatus;
        });
    });
}
// Select contents of input on click
function ecl_forms_selectOnClick()
{
    $(".ecl_forms_selectOnClick").focus(function() 
    {  
        $(this).select(); 
    });
    return false;
}
/* Form buttons (including rounded buttons with background image) */
function ecl_forms_buttons()
{
    // If browser is not IE6 (or less)
    if ($.browser.msie == false || ($.browser.msie && $.browser.version > 6)) 
    {
        $(".button").each(function(){
            // Only apply script to buttons that haven't already been rounded                       
            if (!($(this).next().is("div[class^='buttonEnd']"))) {
                if ($(this).hasClass("style1")) {
                    $(this).addClass("button_curve_style1");
                    $(this).after("<div class='buttonEnd_style1'></div>");
                    $(this).hover(buttonHoverOver_style1, buttonHoverOut_style1);
                }
                else if ($(this).hasClass("style2")) {
                    $(this).addClass("button_curve_style2");
                    $(this).after("<div class='buttonEnd_style2'></div>");
                    $(this).hover(buttonHoverOver_style2, buttonHoverOut_style2);
                }
                else if ($(this).hasClass("style3")) {
                    $(this).addClass("button_curve_style3");
                    $(this).after("<div class='buttonEnd_style3'></div>");
                    $(this).hover(buttonHoverOver_style3, buttonHoverOut_style3);
                }
                else if ($(this).hasClass("style4")) {
                    $(this).addClass("button_curve_style4");
                    $(this).after("<div class='buttonEnd_style4'></div>");
                    $(this).hover(buttonHoverOver_style4, buttonHoverOut_style4);
                }
                else if ($(this).hasClass("style5")) {
                    $(this).addClass("button_curve_style5");
                    $(this).after("<div class='buttonEnd_style5'></div>");
                    $(this).hover(buttonHoverOver_style5, buttonHoverOut_style5);
                }
                else if ($(this).hasClass("style6")) {
                    $(this).addClass("button_curve_style6");
                    $(this).after("<div class='buttonEnd_style6'></div>");
                    $(this).hover(buttonHoverOver_style6, buttonHoverOut_style6);
                } 
                else if ($(this).hasClass("style7")) {
                    $(this).addClass("button_curve_style7");
                    $(this).after("<div class='buttonEnd_style7'></div>");
                    $(this).hover(buttonHoverOver_style7, buttonHoverOut_style7);
                } 
                else if ($(this).hasClass("style8")) {
                    $(this).addClass("button_curve_style8");
                    $(this).after("<div class='buttonEnd_style8'></div>");
                    $(this).hover(buttonHoverOver_style8, buttonHoverOut_style8);
                } 
                else {
                    /*$(this).addClass("button_curve_style1"); 
                    $(this).after("<div class='buttonEnd_style1'></div>");         
                    $(this).hover(buttonHoverOver_style1, buttonHoverOut_style1);*/
                }           
              
            }
        });
    }
}
 
function buttonHoverOver_style1()
{
    $(this).addClass("button_hover_style1");
    $(this).next("div").addClass("buttonEnd_hover_style1");
}
 
function buttonHoverOut_style1()
{
    $(this).removeClass("button_hover_style1");
    $(this).next("div").removeClass("buttonEnd_hover_style1");
}
 
function buttonHoverOver_style2()
{
    $(this).addClass("button_hover_style2");
    $(this).next("div").addClass("buttonEnd_hover_style2");
}
 
function buttonHoverOut_style2()
{
    $(this).removeClass("button_hover_style2");
    $(this).next("div").removeClass("buttonEnd_hover_style2");
}
function buttonHoverOver_style3()
{
    $(this).addClass("button_hover_style3");
    $(this).next("div").addClass("buttonEnd_hover_style3");
}
 
function buttonHoverOut_style3()
{
    $(this).removeClass("button_hover_style3");
    $(this).next("div").removeClass("buttonEnd_hover_style3");
}
function buttonHoverOver_style4()
{
    $(this).addClass("button_hover_style4");
    $(this).next("div").addClass("buttonEnd_hover_style4");
}
 
function buttonHoverOut_style4()
{
    $(this).removeClass("button_hover_style4");
    $(this).next("div").removeClass("buttonEnd_hover_style4");
}
function buttonHoverOver_style5()
{
    $(this).addClass("button_hover_style5");
    $(this).next("div").addClass("buttonEnd_hover_style5");
}
 
function buttonHoverOut_style5()
{
    $(this).removeClass("button_hover_style5");
    $(this).next("div").removeClass("buttonEnd_hover_style5");
}
function buttonHoverOver_style6()
{
    $(this).addClass("button_hover_style6");
    $(this).next("div").addClass("buttonEnd_hover_style6");
}
 
function buttonHoverOut_style6()
{
    $(this).removeClass("button_hover_style6");
    $(this).next("div").removeClass("buttonEnd_hover_style6");
}
function buttonHoverOver_style7()
{
    $(this).addClass("button_hover_style7");
    $(this).next("div").addClass("buttonEnd_hover_style7");
}
 
function buttonHoverOut_style7()
{
    $(this).removeClass("button_hover_style7");
    $(this).next("div").removeClass("buttonEnd_hover_style7");
}
function buttonHoverOver_style8()
{
    $(this).addClass("button_hover_style8");
    $(this).next("div").addClass("buttonEnd_hover_style8");
}
 
function buttonHoverOut_style8()
{
    $(this).removeClass("button_hover_style8");
    $(this).next("div").removeClass("buttonEnd_hover_style8");
}
/* ##### SEARCH */
function ecl_search()
{
    // Redirect the search box to Synergy's search page.
    // For accessibility, the box defaults to point straight to Google.
    $("#cse-search-box").attr("action", $("#searchResults").val() );
}
/* ##### TOOLTIPS */
function ecl_tooltip()
{
    $("input[type='text'],input[type='password'],input[type='file'],select").focus(function(){
        $(this).parents(".clearfix:first").find(".hintText").removeClass("hidden");
    }).blur(function(){
        $(this).parents(".clearfix:first").find(".hintText").addClass("hidden");
    });
}
/* ##### Terms and Conditions and Privacy */
function ecl_replace()
{
    $(".replace").each( 
            function() {
                    var originalText = $( "#hidden" + $(this).attr("id") );
                    originalText.removeClass("hidden");
                    $(this).replaceWith( originalText );
                }
                     )
}
/* ##### POPUP WINDOW */
function ecl_popup_window() 
{
    $('.popUp').click(function (e) {
        e.preventDefault();
        var targetHref = $(this).attr('href');
        window.open(targetHref,"_blank","height=640,width=700,toolbar=no,menubar=no,location=no,scrollbars=yes,status=no");
        // Return false to prevent the link click navigation occuring.
        return false;
    });
}
/* ##### ACCESSIBILITY */
// Font Size Buttons
function ecl_accessibilityFontSize()
{
    if ($(".fontSize").length > 0)
    {
        $(".fontSize").html('<a href="#" class="increase" alt="Increase font size" title="Increase font size"><span class="increaseIcon"></span></a><a href="#" class="decrease" alt="Decrease font size" title="Decrease font size"><span class="decreaseIcon"></span></a>');
        $(".fontSize .increase").click($.FontSizer.IncreaseSize);
        $(".fontSize .decrease").click($.FontSizer.DecreaseSize);
        var options = { min: 80, max: 130};
        $.FontSizer.Init(options);
    }
}
// Skip Content Focus
function ecl_skipContent()
{
    //$('.contentColumn a:first').focus();
    $('.accessibilityLinks a').click(function(){
        $($(this).attr('href')+' a:first').focus();
    });
}
/* ##### PAGE ANCHORS SMOOTH SCROLL */
function ecl_init_anchorSmoothScroll()
{
    // If this is IE6, do not smooth scroll (doesn't play nicely)
    if (!((jQuery.browser.msie) && (parseInt(jQuery.browser.version) == 6)))
    {
        var nodes = $('a[href*=#]');
        for(var i = 0; i < nodes.length; i++) 
        {
            if ($(nodes[i]).attr("href") != "#")
            {
                $(nodes[i]).click(function() 
                {
                    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
                    && location.hostname == this.hostname) {
                        var $target = $(this.hash);
                        $target = $target.length && $target
                        || $('[name=' + this.hash.slice(1) +']');
                        if ($target.length) {
                        var targetOffset = $target.offset().top;
                        $('html,body')
                        .animate({scrollTop: targetOffset}, 1000);
                         return false;
                        }
                    }
                });
            }
        }
    }
}
function ecl_init_externalLinkTarget()
{
    $("a[rel='external']").each(function(){
        $(this).append("<img src='/css/images/bg_link_external.gif' alt='external link icon' title='external link' height='9' width='9' class='externalLink' />").click(function(){
            window.open(this.href);
            return false;
        });                                     
    });
}
/* ########################################################################### *
/* ##### PRESENTATION
/* ########################################################################### */
/* ##### HOMEPAGE RANDOM IMAGE */
function ecl_randomImage()
{
    /*
        Setup the path and number of images that you want to display below. 
        Add the integer (between and including MAX_NUM and MIN_NUM set below
        to the end of the graphic file, between the prefix and suffix path strings.
        An image will then be selected and inserted at random. This is random and
        doesn't currently support remembering history for the users browser (ie. cookies).
    */
    var MAX_NUM = 2;
    var MIN_NUM = 1;
    var URL_PATH_PREFIX = 'media/images/img_randomPhoto_';
    var URL_PATH_SUFFIX = '.jpg';
    var IMG_WIDTH = "216";
    var IMG_HEIGHT = "209";
    var IMG_ALT = "";    
    
    if ($('#randomImage').length > 0)
    {        
        var HTML_STRING = '<img src="" width="' + IMG_WIDTH + '" height="' + IMG_HEIGHT + '" alt="' + IMG_ALT + '" />';
        var randomNumber = Math.floor(Math.random( ) * MAX_NUM - MIN_NUM + 1) + MIN_NUM;
        var randomImagePath = URL_PATH_PREFIX + randomNumber + URL_PATH_SUFFIX;
        
        $('#randomImage').html(HTML_STRING);
        $('#randomImage img').attr("src", randomImagePath);
    }
}
/* ##### TABLE ALTERNATE ROWS */
function ecl_tableAltRows()
{
    $('.ecl_tableOverRows tr').mouseover(function(){$(this).addClass('over');}).mouseout(function() {$(this).removeClass('over');});
    $('.ecl_tableAltRows tr:odd').removeClass('alt');
    $('.ecl_tableAltRows tr:even').addClass('alt');
    $('.ecl_tableAltRows tr').removeClass('last');
    $('.ecl_tableAltRows tr:first-child').addClass('first');    
    $('.ecl_tableAltRows tr:last-child').addClass('last');
}
/* ##### IE TWEAKS */
// Enable functionality if the user's browser is IE
function ecl_ieTweaks()
{    /*
    // If browser is IE6
    if ((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)==6)) 
    {
        ecl_input_padding();
        ecl_lists();
    }
    
    // If browser is IE7
    if ((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)==7)) 
    {
        ecl_lists();
    } */ 
    
    if(jQuery.browser.msie){
        if(parseInt(jQuery.browser.version)>5){
            ecl_input_padding();
            ecl_lists();
        }
    }
    else{
        ecl_input_padding();
        ecl_lists();
    }
}
// Adds a class to checkbox and radio buttons if IE
function ecl_input_padding()
{
    $('input[type=checkbox],input[type=radio]').addClass('nopad');
}
// Adds .first and .last classes to simulate :first :last pseudo classes on list items.
function ecl_lists()
{
    $('ul li:first-child').addClass("first");
    $('ul li:last-child').addClass("last");
}

/* ##### IFRAME RESIZE */
/* 
     NOTE: The iframeLoaded() function is called on the iframe onLoad event.
*/
function iframeLoaded()
{
    var applicationIframe = this.parent.document.getElementById("applicationIframe");
    var siteletIframe = this.parent.document.getElementById("siteletIframe");
    if (applicationIframe != null)
    {
        resizeIframe(applicationIframe);
    }
    
    if (siteletIframe != null)
    {
        resizeIframe(siteletIframe);
    }
    var IFRAME_WRAPPER_ELEMENT_ID = "contentWrapper";
    var NAV_COLUMN_ELEMENT_ID = "rightColumn";
    
    // Sets the iframe wrapper element to have a height greater than the nav column.
    var leftcol = $(NAV_COLUMN_ELEMENT_ID);
    if (leftcol != null)
    {
        var outer = $(IFRAME_WRAPPER_ELEMENT_ID);
        if (outer != null && leftcol.offsetHeight > outer.offsetHeight)
        {
            outer.style.height = leftcol.offsetHeight + 50 + "px";
        }
    }
    
    return true;
};
function resizeIframe(iframeRef)
{
    iframeRef.style.height = "auto";
    iframeRef.style.overflow = "hidden";
    pageDimensions = getPageDimensions(iframeRef.contentWindow);
    iframeRef.style.height = pageDimensions[1] + "px";
    return true;
};
function getPageDimensions(windowRef)
{
    var body = windowRef.document.getElementsByTagName("body")[0];
    var bodyOffsetWidth = 0;
    var bodyOffsetHeight = 0;
    var bodyScrollWidth = 0;
    var bodyScrollHeight = 0;
    var pageDimensions = [0, 0];
    if (typeof windowRef.document.documentElement != "undefined" && typeof document.documentElement.scrollWidth != "undefined")
    {
        pageDimensions[0] = windowRef.document.documentElement.scrollWidth;
        pageDimensions[1] = windowRef.document.documentElement.scrollHeight;
    }
    bodyOffsetWidth = body.offsetWidth;
    bodyOffsetHeight = body.offsetHeight;
    bodyScrollWidth = body.scrollWidth;
    bodyScrollHeight = body.scrollHeight;
    if (bodyOffsetWidth > pageDimensions[0])
    {
        pageDimensions[0] = bodyOffsetWidth;
    }
    if (bodyOffsetHeight > pageDimensions[1])
    {
        pageDimensions[1] = bodyOffsetHeight;
    }
    if (bodyScrollWidth > pageDimensions[0])
    {
        pageDimensions[0] = bodyScrollWidth;
    }
    if (bodyScrollHeight > pageDimensions[1])
    {
        pageDimensions[1] = bodyScrollHeight;
    }
    return pageDimensions;
}
/* ##### STYLE SHEET SWITCH */
/* 
     NOTE: The checkStyles() function is called in global presentation.
*/
function checkStyles(){
    $('.nav_top ul li:last').before('<li><a class="styleswitch" rel="WCAG-AAA" href="#">High contrast</a></li>');
    $('.styleswitch').click(function(){
        switchStyle(this.getAttribute("rel"));
        return false;
    });
    $('link[@rel*=style][title]').each(function(){this.disabled=true});
    var c = readCookie('style');
    if (c) switchStyle(c);
}
function switchStyle(style){
    $('link[@rel*=style][title]').each(function(){
        if (this.getAttribute('title') == style && this.disabled==true){
            this.disabled = false;
            createCookie('style', style, 365);
            $('a[@rel='+style+']').html('Standard contrast');
        }else{
            this.disabled = true;    
            eraseCookie('style');
            $('a[@rel='+style+']').html('High contrast');
        }
    });
}
// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Code Added By Joy Saha (CR # SCBT)
function detect_browser(){
    if(jQuery.browser.msie) alert('IE browser');
    else alert('Non IE browser');    
}
// Code Added By Joy Saha (CR # SCBT)

function eraseCookie(name)
{
    createCookie(name,"",-1);
}
// /cookie functions
// CR # 763
function checkFontStyles(){
    $('.nav_top ul li:last').before('<li><font size="-1"><a class="defaultFont" href="#">A</a></font> <font size="+0"><a class="largeFont" href="#">A</a></font> <font size="+1"><a class="largerFont" href="#">A</a></font></li>');
    
    $('.defaultFont').click(function(){
        switchFont(16);
        return false;
    });
    
    $('.largeFont').click(function(){
        switchFont(20);
        return false;
    });
    $('.largerFont').click(function(){
        switchFont(24);
        return false;
    });  
    var currentFontSize = $('html').css('font-size');
   
    var c = readCookie('fontSize');
       if (c) switchFont(c);
}
function switchFont(fontSize){
    $('html').css('font-size', parseInt(fontSize));
    var c = readCookie('fontSize');        
    if (c) {
        eraseCookie('fontSize');
    }
    createCookie('fontSize', fontSize, 365);
}
// CR # 763
//For Password Strength Check
function check_password_strength() {
    var lcStrength;
    var ucStrength;
    var numStrength;
    var scStrength;
    var lenStrength;
    $("input[type=password]").keyup(function() {
    /*contains lowercase characters*/
    if(this.value.match(/[a-z]+/)){
        lcStrength=1;
        $("span#passwordlc").removeClass("red").addClass("green");
        $("span#passwordlcok").removeClass("hidden");
    }
    else{
        lcStrength=0;
        $("span#passwordlc").removeClass("green").addClass("red");
        $("span#passwordlcok").addClass("hidden");
    }
        
    /*contains digits*/
    if(this.value.match(/[0-9]+/)){ 
        numStrength=1;
        $("span#passwordnum").removeClass("red").addClass("green");
        $("span#passwordnumok").removeClass("hidden");
    }
    else{
        numStrength=0;
        $("span#passwordnum").removeClass("green").addClass("red");
        $("span#passwordnumok").addClass("hidden");
    }
    /*contains uppercase characters*/
    if(this.value.match(/[A-Z]+/)){ 
        ucStrength=1;
        $("span#passworduc").removeClass("red").addClass("green");
        $("span#passworducok").removeClass("hidden");
    }
    else{
        ucStrength=0;
        $("span#passworduc").removeClass("green").addClass("red");
        $("span#passworducok").addClass("hidden");
    }
    /*contains special characters*/
    if(this.value.match(/[!@#\$%\^&\*\(\)\-_=\+\[\]\{\}.\?~\;\:\<\>\,|]+/)){ 
        scStrength=1;
        $("span#passwordsc").removeClass("red").addClass("green");
        $("span#passwordscok").removeClass("hidden");
    }
    else{
        scStrength=0;
        $("span#passwordsc").removeClass("green").addClass("red");
        $("span#passwordscok").addClass("hidden");
    }
    /*Password length check*/
    if(this.value.length >5 && this.value.length <33){
        lenStrength=1;
        $("span#passwordlen").removeClass("red").addClass("green");
        $("span#passwordlenok").removeClass("hidden");
    }
    else{
        lenStrength=0;
        $("span#passwordlen").removeClass("green").addClass("red");
        $("span#passwordlenok").addClass("hidden");
    }
    /*Finally, Password strength check*/
    /*if(lenStrength ==1 && (lcStrength+ucStrength+numStrength+scStrength)>2 ){
        $("span#passwordOK").removeClass("hidden");
        $("span#passwordNotOK").addClass("hidden");
    }
    else{
        $("span#passwordOK").addClass("hidden");
        $("span#passwordNotOK").removeClass("hidden");
    }*/

    if(lenStrength ==1 && (lcStrength+ucStrength+numStrength+scStrength)>2 && (lcStrength+ucStrength+numStrength+scStrength)<4){
        $("span#passwordw").addClass("hidden");
        $("span#passwordm").removeClass("hidden");
        $("span#passwords").addClass("hidden");
    }
    else if(lenStrength ==1 && (lcStrength+ucStrength+numStrength+scStrength)>3 ){
        $("span#passwordw").addClass("hidden");
        $("span#passwordm").addClass("hidden");
        $("span#passwords").removeClass("hidden");
    }
    else{
        $("span#passwordw").removeClass("hidden");
        $("span#passwordm").addClass("hidden");
        $("span#passwords").addClass("hidden");
    }

    });
}
// /For Password Strength Check
/* FIN */
