/**
 * This file needs to be cleaned up and better documented.
 */

function Breakout(){
	if (top.location != location) {
		top.location.href = document.location.href ;
	}
}

/**
 * Used by security-report.
 */
function CalendarPopup(PG_FormName, PG_FieldName){
    var SomeURL = '/global/php/popupcal.php?FormName=' + PG_FormName + '&FieldName=' + PG_FieldName;
    var CalendarWindow;
    CalendarWindow  = window.open(
        SomeURL,
        'CalendarPopup',
        'width=260,height=300,top=65,left=150,scrollbars=yes,resizable=yes'
    );
    CalendarWindow.focus();
}

function decodeContact(anchor) {
    href = anchor.getAttribute("href");
    
    if (href.search(/@/) != -1) {
        // already done
        return false;
    }
    
    // remove the rewrite
    clean = href.replace(/.*\/global\/rw\/contact\//, 'mailto:');
    // remove the checksum, if present
    if (clean.search(/\+/) != -1) {
        clean = clean.replace(/\+.*/, '');
    }
    if (clean.search(/\//) != -1) {
        clean = clean.replace(/\//, '@');
    } else {
        clean = clean + '@mcgill.ca';
    }
    anchor.setAttribute("href", clean);
    
    // remove triggers so we don't do this again
    anchor.onclick = null;
    anchor.onmouseover = null;
    
    return false;
}

/**
 * Convenience function to set cookies
 */
function setCookie(name, value, expires, path, domain, secure) {   
    var val = escape(value);
    document.cookie= name + "=" + val +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

/**
 * Convenience function to get cookies
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function NewPopup(SomeURL, WindowName, WindowWidth, WindowHeight){
    var PopupWindow;
    PopupWindow  = window.open(
        SomeURL,
        WindowName,
        'width='+WindowWidth+',height='+WindowHeight+',top=65,left=150,scrollbars=yes,resizable=yes'
    );
    PopupWindow.focus();
}

function VariablePopup(SomeURL, WindowHeight, WindowWidth){
    NewPopup(SomeURL, 'Popup', WindowWidth, WindowHeight);
}

function ShowPopup(SomeURL){
    VariablePopup(SomeURL, 350, 500);
}

function ChangeParent(SomeURL){
    if(window.opener != null){
        window.parent.opener.location = SomeURL;
    }
    else{
        window.location = SomeURL;
    }
}

function ChangeLanguage(Language){
    document.cookie = ('GLBLanguage=' + Language + '; path=/; domain=.mcgill.ca; expires=');
    window.location.reload();
}

function ChangeAdminLang(toLang) {
    if (!toLang) {
        currentLang = getCookie('WPSAdminLang');
        if (!currentLang) currentLang = 'en';
        if (currentLang == 'fr') {
            toLang = 'en';
        } else {
            toLang = 'fr';
        }
    }
    setCookie('WPSAdminLang', toLang, null, '/admin/', '.mcgill.ca', true);
    window.location.reload();
}

function GetAdminLang() {
    lang = getCookie("WPSAdminLang");
    if (lang != 'en' && lang != 'fr') lang = "en";
    return lang;
}


function ChangeTextSize(NewSize){
    document.cookie = ('GLBTextSize=' + NewSize + '; path=/; expires=');
    window.location.reload();
}

function EditPeople(){
    document.cookie = ('EditPeople=EditPeople; path=/; domain=.mcgill.ca; expires=');
    window.location.reload();
}    

function AdminDirectory(DirName){
    document.cookie = ('AdminFocus=' + DirName + '; path=/; domain=.mcgill.ca; expires=');
    if(location.hostname != ''){
        var MyHostname = location.hostname;
    }
    else{
        var MyHostname = 'home.mcgill.ca';
    }
    window.location = 'https://'+location.hostname+'/admin/site/';
}

function GetModuleName(SomeString){
    Start = SomeString.indexOf('$');
    End = SomeString.indexOf('-');
    ModName = SomeString.substring(Start + 1, End);  
    return ModName;
}    

function TurnOn(ImageName){
    if(document.getElementById){
        document.getElementById(ImageName).src = eval(ImageName + '_On.src');
    }
}

function TurnOff(ImageName){
    if(document.getElementById){
        document.getElementById(ImageName).src = eval(ImageName + '_Off.src');
    }
}

/**
 * Used by AccordionMenu blocks.
 */
function AccordionMenu(object) {
    if (object.parentNode.parentNode.className == 'app_block app_askus') {
        linknodes = object.parentNode.getElementsByTagName("dl");
    }
    else {
        linknodes = object.parentNode.getElementsByTagName("ul");
    }
    // iterate through all submenus and collapse open ones
    for (i=0; i<linknodes.length; i++) {
        linknode = linknodes[i];
        titlenode = linknode.parentNode;
        if (linknode.className == "child") {
            // if the parent node is us, activate it
            if (titlenode == object) {
                linknode.className = "child active";
                titlenode.className = "active";
            }
        }
        else {        
            // deactivate it
            linknode.className = "child";
            titlenode.className = "";
            // if the click was on the same node, return
            if (titlenode == object) {
                return;
            }
        }
    }
}

/**
 * Used by the new-design CMS hovers.
 */
function ToggleCMSHover(object, show) {
    cmsDiv = object.getElementsByTagName('div')[0];
    if (show) {
        object.className += " outlined";
        cmsDiv.className += " displayed";
    }
    else {
        object.className = object.className.replace(/ outlined/, "");
        cmsDiv.className = cmsDiv.className.replace(/ displayed/, "");
    }
}

/**
 * Used to trigger the new-design CMS action.
 */
function PerformCMSAction(object, action) {
    cmsForm = object.parentNode;
    cmsForm.CMSAction.value = action;
    cmsForm.submit();
}

/**
 * Used to trigger keystroke-based editing of page areas.
 */
function ShortcutEdit(event, object) {
    if (event.shiftKey) {
        cmsForm = object.getElementsByTagName("form")[0];
        cmsForm.CMSAction.value = "edit";
        cmsForm.submit();
    }
}

function PrintPreview() {
    styleDefault = document.getElementById('stylesheet_default');
    stylePrint = document.getElementById('stylesheet_print');
    currentStyleHref = styleDefault.href;
    otherStyleHref = stylePrint.href;
    styleDefault.href = otherStyleHref;
    stylePrint.href = currentStyleHref;
}

var ImAPopup = 0;

/**
 * Returns the current host's language
 */
function getCurrentLang() {
    if (location.host.match(/www/)) {
        return 'en';
    } else if (location.host.match(/francais/)) {
        return 'fr';
    } else {
        if (lang = getCookie('GLBLanguage')) {
            return lang;
        } else {
            return 'en';
        }
    }
}

