﻿// Core Javascript for www.lawcareers.net
// Copyright 2009 and onwards Globe Business Publishing

// Utility functions

function encodeMyHtml(text) {

 
    text = text.replace(/\&/g, '&' + 'amp;');
    text =text.replace(/</g, '&' + 'lt;');
    text = text.replace(/>/g, '&' + 'gt;');
    text = text.replace(/\'/g, '&#39;');
    
    
    text = text.replace(/\"/g, '&' + 'quot;');



   return text;
}



function SetupPage() {
    document.frmPageForm.TitleTextBox.focus();
}


// re encode " ' " Characcter
$(document).ready(function() {

    $('.Note').each(function() {
        $(this).html($(this).html().replace(/\&amp;apos/gi, '&#39;'));

      //  alert($(this).html());
    });

});



function ToggleDisplay(x) {

    var item = document.getElementById(x);
    if (item.style.display == "none") {
        item.style.display = "";
    } else {
        item.style.display = "none";
    }
}



function Togglevisible(x, visible) {

    var item = document.getElementById(x);
    item.style.display = visible;
}








function ClearTextBox(x) {
    var item = document.getElementById(x);
    item.value = "";


}



function ShowAddNote(selectionref) {
    
    if ($('#spanaddnote' + selectionref).val() != undefined)
        ToggleDisplay('spanaddnote' + selectionref); 

}





function GetDirections(from, to) {
    window.location = 'Default.aspx?from=' + from + '&to=' + to;
}





function GetDistance(from, to) {

    var map;
    var directions  ="from: " + from + " to: " + to
    var gDir = new GDirections();
    var directionsPanel;


    map = new GMap2(document.getElementById("_ctl0__ctl0_MainContentPlaceholder_MainContentPlaceholder_GoogleMap1"));
           // map.setCenter(new GLatLng(42.351505,-71.094455), 15); 

       directionsPanel = document.getElementById("route");
           directions = new GDirections(map, directionsPanel);
           directions.load(directions);
    
          distance = directions.getDistance(directions); // ====> for getting 
           //distance
           alert("value :" + distance); // displaying the distance 
   






}
    



function OpenContext(Button, ContextMenu, ClearBox, Display, ManageUrl, SaveUrl, Ref, RefVal) {
 
    // Show menu when #myDiv is clicked
    $(Button).contextMenu(Button, { menu: ContextMenu },
					function(action, el, pos) {

                        if (action.match(/AddNote/ig) == 'AddNote') {

					    
					        ClearTextBox(ClearBox);
					        ToggleDisplay(Display);

					    }

					    if (action.match(/Manage/ig) == 'Manage') {
					        window.location = ManageUrl;

					    }

					    if (action.match(/Remove/ig) == 'Remove') {
					        PageMethod(SaveUrl, 'Remove', [Ref, RefVal]);

					    }




					});
}






   

   
 function UpdateContextMenuCss(){

// update context menu when the window is resized

// get each context menu
     $(".contextMenu").each(function() {


        // if the context menu is displayed
         if ($(this).css("display") != 'none') {

            // get the associated button
             var button = '#buOptionsfi' + $(this).attr("id").substring(8, 9);
             //  alert(button);
             
             // get button position
             var pos = $(button).position();
             
             // get width of context menu
             var width = $(this).width();

               // update contxt menu position
             $(this).css({
                 "position": "absolute",
                 left: (pos.left - width) - 5,
                 top: pos.top
             });

         }
     });             
                    

   
 };  


// jQuery Context Menu Plugin
//
// Version 1.00
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
//
// Visit http://abeautifulsite.net/notebook/80 for usage and more information
//
// Terms of Use
//
// This software is licensed under a Creative Commons License and is copyrighted
// (C)2008 by Cory S.N. LaViska.
//
// For details, visit http://creativecommons.org/licenses/by/3.0/us/
//
if (jQuery) (function() {
    $.extend($.fn, {

        contextMenu: function(Button, o, callback) {


            if (o.isContextMenu == undefined) { o.isContextMenu = true; }



            // Defaults

            if (o.menu == undefined) return false;
            if (o.inSpeed == undefined) o.inSpeed = 50;
            if (o.outSpeed == undefined) o.outSpeed = 25;
            // 0 needs to be -1 for expected results (no fade)
            if (o.inSpeed == 0) o.inSpeed = -1;
            if (o.outSpeed == 0) o.outSpeed = -1;
            // Loop each context menu
            $(this).each(function(e) {
                var el = $(this);
                var offset = $(el).offset();
                // Add contextMenu class                
                $('#' + o.menu).addClass('contextMenu');
                // Simulate a true right click                

                //$(this).mouseup(function(e) {
                //not fireing as button has allready clicked
                var evt = e;

                //$(this).mouseup(function(e) {
                var srcElement = $(this);
                //$(this).unbind('mouseup');
                $(this).unbind('mouseout');

                var buttonID = 1;
                if (o.isContextMenu) {
                    buttonID = 2;
                } else {
                    buttonID = 0;
                }

                if (evt.button != buttonID) {
                    // Hide context menus that may be showing
                    $(".contextMenu").hide();
                    // Get this context menu
                    var menu = $('#' + o.menu);

                    if ($(el).hasClass('disabled')) return false;

                    // Detect mouse position
                    var d = {}, x, y;
                    if (self.innerHeight) {
                        d.pageYOffset = self.pageYOffset;
                        d.pageXOffset = self.pageXOffset;
                        d.innerHeight = self.innerHeight;
                        d.innerWidth = self.innerWidth;

                    } else if (document.documentElement &&
								document.documentElement.clientHeight) {
                        d.pageYOffset = document.documentElement.scrollTop;
                        d.pageXOffset = document.documentElement.scrollLeft;
                        d.innerHeight = document.documentElement.clientHeight;
                        d.innerWidth = document.documentElement.clientWidth;
                    } else if (document.body) {
                        d.pageYOffset = document.body.scrollTop;
                        d.pageXOffset = document.body.scrollLeft;
                        d.innerHeight = document.body.clientHeight;
                        d.innerWidth = document.body.clientWidth;

                    }
                    (e.pageX) ? x = e.pageX : x = e.clientX + d.scrollLeft;
                    (e.pageY) ? y = e.pageY : y = e.clientY + d.scrollTop;


                    // get item position and display content menu
                            var pos = $(Button).position();
                            var width = $('#' + o.menu).width();
                    
                    // Show the menu

                    $(document).unbind('click');

                    $(menu).css({ top: pos.top, left: (pos.left - width) - 5 }).fadeIn(o.inSpeed);

               



                    // Hover events
                    $(menu).find('A').mouseover(function() {
                        $(menu).find('LI.hover').removeClass('hover');
                        $(this).parent().addClass('hover');
                    }).mouseout(function() {
                        $(menu).find('LI.hover').removeClass('hover');
                    });

                    // Keyboard
                    $(document).keypress(function(e) {
                        switch (e.keyCode) {
                            case 38: // up
                                if ($(menu).find('LI.hover').size() == 0) {
                                    $(menu).find('LI:last').addClass('hover');
                                } else {
                                    $(menu).find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
                                    if ($(menu).find('LI.hover').size() == 0) $(menu).find('LI:last').addClass('hover');
                                }
                                break;
                            case 40: // down
                                if ($(menu).find('LI.hover').size() == 0) {
                                    $(menu).find('LI:first').addClass('hover');
                                } else {
                                    $(menu).find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
                                    if ($(menu).find('LI.hover').size() == 0) $(menu).find('LI:first').addClass('hover');
                                }
                                break;
                            case 13: // enter
                                $(menu).find('LI.hover A').trigger('click');
                                break;
                            case 27: // esc
                                $(document).trigger('click');
                                break
                        }
                    });

                    // When items are selected
                    $('#' + o.menu).find('A').unbind('click');
                    $('#' + o.menu).find('LI:not(.disabled) A').click(function() {
                        $(document).unbind('click').unbind('keypress');
                        $(".contextMenu").hide();
                        // Callback
                        if (callback) callback($(this).attr('href').substr(1), $(srcElement), { x: x - offset.left, y: y - offset.top, docX: x, docY: y });

                   

                        return false;
                    });

                    // Hide bindings
                    setTimeout(function() { // Delay for Mozilla
                        $(document).click(function() {
                            $(document).unbind('click').unbind('keypress');
                            $(menu).fadeOut(o.outSpeed);
                            return false;
                        });
                    }, 0);
                }
                //}); //here
                //});

                // Disable text selection
                if ($.browser.mozilla) {
                    $('#' + o.menu).each(function() { $(this).css({ 'MozUserSelect': 'none' }); });
                } else if ($.browser.msie) {
                    $('#' + o.menu).each(function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
                } else {
                    $('#' + o.menu).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
                }
                // Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
                $(el).add('UL.contextMenu').bind('contextmenu', function() { return false; });

            });
            return $(this);
        },

        // Disable context menu items on the fly
        disableContextMenuItems: function(o) {
            if (o == undefined) {
                // Disable all
                $(this).find('LI').addClass('disabled');
                return ($(this));
            }
            $(this).each(function() {
                if (o != undefined) {
                    var d = o.split(',');
                    for (var i = 0; i < d.length; i++) {
                        $(this).find('A[href="' + d[i] + '"]').parent().addClass('disabled');

                    }
                }
            });
            return ($(this));
        },

        // Enable context menu items on the fly
        enableContextMenuItems: function(o) {
            if (o == undefined) {
                // Enable all
                $(this).find('LI.disabled').removeClass('disabled');
                return ($(this));
            }
            $(this).each(function() {
                if (o != undefined) {
                    var d = o.split(',');
                    for (var i = 0; i < d.length; i++) {
                        $(this).find('A[href="' + d[i] + '"]').parent().removeClass('disabled');

                    }
                }
            });
            return ($(this));
        },

        // Disable context menu(s)
        disableContextMenu: function() {
            $(this).each(function() {
                $(this).addClass('disabled');
            });
            return ($(this));
        },

        // Enable context menu(s)
        enableContextMenu: function() {
            $(this).each(function() {
                $(this).removeClass('disabled');
            });
            return ($(this));
        },

        // Destroy context menu(s)
        destroyContextMenu: function() {
            // Destroy specified context menus
            $(this).each(function() {
                // Disable action
                $(this).unbind('mousedown').unbind('mouseup');
            });
            return ($(this));
        }

    });
})(jQuery);




// Top nav search --------------------------------------------------------


function LoginSubmit(event) {
    if (event.keyCode == 13) {
        $("#buLogin").click();
    }
}


 



function SearchBoxClick(box, hide) {
    hide.addClass("HiddenFeatures");
    box.focus();
}

function SearchBoxBlur(box, hide) {
    setTimeout(function() {
        box.value = '';
        hide.removeClass("HiddenFeatures");
    }
    , 200);
}

function LoginBoxBlur(box, hide) {
    if(box.value == '')
    {
        setTimeout(function() {
            box.value = '';
            hide.removeClass("HiddenFeatures");
        }
        , 200);
    }
}

function SearchBoxOnKeyDown(event, box, searchurl) {
    if (event.keyCode == 13) {

        var searchterm = '';

        if (box.value)
            searchterm = box.value;
        document.location = searchurl + "?s=" + escape(searchterm);
        
    }
}

function SearchBoxGo(box, searchurl) {

    var searchterm = '';

    if (box.val() != null)
        searchterm = box.val();
    document.location = searchurl + "?s=" + escape(searchterm);

}

// End top nav search --------------------------------------------------------


// Timeline
function TogglePostGrade(x) {

    if (x != "on") {
        Togglevisible('divYear', '');
        Togglevisible('divPostBar', 'none');
        Togglevisible('divPostSol', 'none');

    }
    else {
        Togglevisible('divYear', 'none');

        if ($("input[@name='route']:checked").val() == "Solicitor") {
            Togglevisible('divPostSol', '');
            Togglevisible('divPostBar', 'none');

        } else {
            Togglevisible('divPostBar', '');
            Togglevisible('divPostSol', 'none');
        }

        if ($("input[@name='route']:checked").val() == "Don't know yet") {
            Togglevisible('divPostBar', 'none');
            Togglevisible('divPostSol', 'none');
        }

    }

}

// end timeline



// Solicitor / barrister / course detail page

// Partial page updates ---------------------------------------------
//  Must have a script manager on page for this to work.

var result;
var MainArea;


// Fill a div with the content from a url
function FillAreaFromUrl(url, resultarea, TabDiv) {

    Resetstyle();

    setActivetab(TabDiv);

    result = document.getElementById(resultarea);

    MainArea = document.getElementById("MainPage");

    MainArea.style.display = 'none';

    $('#' + resultarea).load(url);
}

function setActivetab(tab) {

    $("#" + tab).parents("li:first").attr('class', 'ActiveTab');

}

function Resetstyle() {



    $("#OverViewLinkTab").parents("li:first").attr('class', 'TabMenuTabs');
    $("#NewsLinkTab").parents("li:first").attr('class', 'TabMenuTabs');
    $("#BurningLinkTab").parents("li:first").attr('class', 'TabMenuTabs');
    $("#DiaryLinkTab").parents("li:first").attr('class', 'TabMenuTabs');
    $("#DealsLinkTab").parents("li:first").attr('class', 'TabMenuTabs');
    $("#FeaturedLawyerLinkTab").parents("li:first").attr('class', 'TabMenuTabs');
    $("#LCNFocusTab").parents("li:first").attr('class', 'TabMenuTabs');

}



// Callback
function OnWebRequestCompleted(executor, eventArgs) {
    if (executor.get_responseAvailable()) {

        var resdata = executor.get_responseData();

        result.innerHTML = resdata;

    }

}
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


// End partial page updates -----------------------------------------
function ResetPanelUpdates() {
    MainArea = document.getElementById("MainPage");

    MainArea.style.display = '';

    result = document.getElementById("ResultsArea");

    result.innerHTML = "";

    Resetstyle();

    setActivetab("OverViewLinkTab");

}

// end solicitor / barrister / course detail page





//   LCN POPUP


//Note: needs jquery.

var popupStatus = 0;
function TogglePopup() {
    if (popupStatus == 0) {

        var totalNum = 2;
        var ran = Math.random();
        var rndNum = Math.floor(Math.random() * totalNum);


        if (window.XMLHttpRequest) {
            // IE 7, mozilla, safari, opera 9
            $("#imgRegister").attr("src", "/images/MyLCN/Register_" + rndNum + ".png");
        } else {
            $("#imgRegister").attr("src", "/images/MyLCN/Register_" + rndNum + "_ie6.png");
        }

        CenterPopup()
        $(".popupbg").css({ "opacity": "0.7" });
        $(".SearchGo").fadeOut("normal");


        $(".popupbg").fadeIn("normal");
        $(".popup").fadeIn("normal");
        popupStatus = 1;
    } else {
        $(".popupbg").fadeOut("normal");
        $(".popup").fadeOut("normal");
        $(".popupClearOnClose").hide();
        $(".SearchGo").fadeIn("normal");

        popupStatus = 0;
    }
}

function CenterPopup() {

    $(".popup").center();

    $(".popupbg").css({  //only need force for IE6
        "height": $(document).height(),
        "width": $(document).width()
    });
    
}

   $(window).resize(function() {
       if ($(".popup").is(":visible")) {
           CenterPopup();
         
       }
   });

   $(window).scroll(function() {
  
       if ($(".popup").is(":visible")) {
           CenterPopup();
          
       }

   });
   
   
   
   $(document).ready(function() {
       $('.popupclose, .showpopup').click(function() { TogglePopup(); });

       if ($(".popup").length > 0 && getCookie('LCNRegisteredPopup') == '') {
           // pop up
           TogglePopup();
       }

       $('.LCNRegisteredPopup').click(function() {

           if (getCookie('LCNRegisteredPopup') == '') {
               setCookie('LCNRegisteredPopup', "true", 30);
           }
       });


   });




// END  LCN POPUP

    jQuery.fn.center = function() {
        this.css("position", "absolute");
        this.css("top",      ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px"); this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
        return this;
    }