//Mediafly functions
function getPlayer(_id) {
    var player = document.getElementsByName(_id)[0];
    if (!player)
        player = document.getElementById(_id);
    return player;
}

function CreateMediaflySwf(divId, width, height, params, attributes, flashvars, swfPath, swfFileName, playerDivId) {
    if (swfobject.hasFlashPlayerVersion("9.0.115")) {
        swfobject.embedSWF(swfPath + swfFileName, divId, width, height, "9.0.115", swfPath + "expressInstall.swf", flashvars, params, attributes);
    }
    else {
        var divObj = document.getElementsByName(divId)[0];
        if (!divObj)
            divObj = document.getElementById(divId);
        divObj.innerHTML = "Flash version 9.0.115 or higher required.<br/><a target='_blank' href='http://get.adobe.com/flashplayer/'>Download Latest Flash Player</a>";        
    }
}

function CreateMediaflyRolloverSwf(rolloverSwfFilename) {
    //created this object in javascript to get around IE7.0 Activation bug
    var rootDir = location.protocol + "//" + location.host;
    var assetsUrl = rootDir + "/_layouts/1033/MediaflyAssets";

    var flashvars = {};
    flashvars.AssetUrl = assetsUrl;
    flashvars.TargetFrameOverride = ".remaxtest.net/:_parent,.remax.net/:_parent";
    
    var params = {};
    params.allowScriptAccess = "always";
    params.allowFullScreen = "false";
    params.quality = "high";
    params.wmode = "transparent";
    params.allowScriptAccess = "always";

    var attributes = {};
    attributes.id = "MflyRollover";
    attributes.name = "MflyRollover";
    attributes.align = "middle"

    swfobject.embedSWF(rolloverSwfFilename,
        "divMediaflyRolloverFlashPlayer",
        "100%",
        "100%",
        "9.0.115",
        "/_layouts/1033/REMAX/expressInstall.swf",
        flashvars,
        params,
        attributes);
}
//End Mediafly functions

function GetGACode() {
    var gaCode;
    switch (window.location.host) {
        case 'remax-dev.remaxtest.net':
        case 'remax-test.remaxtest.net':
            gaCode = 'UA-30723-31';
            break;
        case 'uat.remax.net':
        case 'public-uat.remax.net':
        case 'remax-uat.remax.net':
            gaCode = 'UA-30723-30';
            break;
        case 'www.remax.net':
        case 'public.remax.net':
        case 'remax-prod.remax.net':
            gaCode = 'UA-30723-25';
            break;
        case 'www.joinremax.com':
            gaCode = 'UA-30723-33';
            break;
         case 'joinremaxdev.joinremax.com':
         case 'joinremaxtest.joinremax.com':
         case 'joinremaxdev.joinremax.ca':
         case 'joinremaxtest.joinremax.ca':
            gaCode = 'UA-30723-34';
            break;
       case 'www.joinremax.ca':
            gaCode = 'UA-30723-35';
            break;
        default:
            gaCode = 'UA-30723-31';
    }
    //alert('Take this out.  Testing only.  GA code: ' + gaCode);
    return gaCode;
}

// these are all the functions that run in the masterpages ONLOAD
function RMXonLoad() {
    //fixflash();
    updateNav();
}

//this function will add the home image to the home tab, and write sub-headings under each top navigation item
function updateNav() {
    var titleArray = new Array();
    var titleIndex = 0;
    var tdArray = document.getElementById('zz1_TopNavigationMenu').getElementsByTagName("td");  // get all top navigation elements
    for (x in tdArray) { // for each td element
        if ((tdArray[x].title != '') && (tdArray[x].title != null)) {  // if there is a title value
            titleArray.push(tdArray[x].title); // add the title to the array of titles
        }
    }
    var linkArray = document.getElementById('zz1_TopNavigationMenu').getElementsByTagName("a"); // get all top navigation links
    for (y in linkArray) {
        if (linkArray[y].className != 'remax-subheadings') {
            var linkArr = linkArray[y].innerHTML;
            if (linkArr != null) {  // if the link has text
                if (linkArray[y].className.search(/FlyOuts/) == '-1') {  // if the link is not a drop-down navigation option
                    if (linkArray[y].innerHTML == 'Home') {  // If text = Home, do not write a sub-heading
                        var newNav = linkArray[y].parentNode;
                        linkArray[y].style.width = '105px';
                        linkArray[y].style.padding = '0px 0px 5px 0px';
                        var thisClass = linkArray[y].className;
                        var homeImg = '_layouts/images/REMAX/remax-home_house.png';
                        var writeNav = linkArray[y].parentNode.innerHTML;
                        writeNav = writeNav.substr(0, writeNav.search('>Home'));
                        writeNav = writeNav + '><img src="' + homeImg + '" border="0" style="padding:0px; margin: 0px; position: relative; bottom:-4px; left:-5px;">Home</a>';
                        newNav.innerHTML = writeNav;
                    }
                    else {  // if the link is a top navigation link other than Home, write the title out of the array as a sub-heading
                        var newNav = linkArray[y].parentNode
                        var writeNav = linkArray[y].parentNode.innerHTML;
                        writeNav = writeNav + '<span class="remax-subheadings"><a class="remax-subheadings">' + titleArray[titleIndex] + '</a></span>'
                        newNav.innerHTML = writeNav;
                        titleIndex++
                    }
                }
            }
        }
    }
}

// this function will open the urls on the home page for (Training on demand and Tidbits)
function openRMXUrl(link) {
    if (link == 'TidbitsLink') { var val = document.getElementById("TidbitsLink").innerHTML }
    if (link == 'TrainingOnDemandLink') { var val = document.getElementById("TrainingOnDemandLink").innerHTML }
    if (val.indexOf('http') == -1) {  // url is not fully qualified
        if (val.indexOf('&nbsp;') == -1) {// for mac browsers
            var valParse = val.substring(0, val.length - 1);
        }
        else { var valParse = val.substring(0, val.indexOf('&nbsp;')) };
        var valUrl = location.protocol + '//' + location.hostname + valParse;
        window.location.replace(valUrl);
    }
    else {
        var urlParse = val.slice(val.indexOf('="') + 2);
        var urlParse2 = urlParse.substring(0, urlParse.indexOf('"'));
        window.location.replace(urlParse2);
    }
}

//the next two functions are called from the Training Calendar dispform.aspx page and redirects the user to the course detail page
function loadRMXCourseDetail() {
    if (window.location.search != null) { // if there are not query string parameters, do not run this code.
        var aArray = new Array();
        aArray = document.getElementsByTagName("A");  // get all the links on the page
        var courseTitle = checkRMXCourseLink(aArray); // this function is checking for a link to the training catalog dispform.aspx.  if this link exists, the calendar item has a link to a course in the course catalog and the user should be directed to the course detail page
        if (courseTitle != false) {
            var str = "http://" + location.host + "/Learn/Pages/CourseDetail.aspx?Filter1Value=" + courseTitle;
            location.replace(str); // directs the user to the course detail page and passes the course detail in the Query string
        }
        else {
            document.getElementById("Wp_Zone").style.display = "block";
            document.getElementById("Loading_Msg").style.display = "none";
        }
    }
}

function checkRMXCourseLink(aArray) {
    for (x in aArray) {
        var aHref = aArray[x].href;
        if (aHref != null) {
            if (aHref.search(/Learn_TrainingCatalog\/DispForm.aspx/i) != -1) {
                var qTitle = aArray[x].innerHTML;     // grabs the title of the course in the course catalog that is associated with this calendar item
                return (qTitle);
            }
        }
    }
    return false;
}

//the next two functions are used in the Downloads library to hide fields from users who should not access them
function hideafield(fname) {
    var str = 'FieldName="' + fname + '"'
    var arr = document.getElementById("formTbl").getElementsByTagName("TR");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].innerHTML.indexOf(str) != -1) {
            arr[i].style.display = "none";
        }
    }
}

function showafield(fname) {
    var str = 'FieldName="' + fname + '"'
    var arr = document.getElementById("formTbl").getElementsByTagName("TR");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].innerHTML.indexOf(str) != -1) {
            arr[i].style.display = "block";
        }
    }
}

//this function is used on the "My Region" pages
function regionRedirect() {
    if (location.search == '') {
        var regLink = document.getElementById('RegionLink').innerHTML;
        if (regLink.indexOf('http') == -1) window.location = regLink; //url is not fully qualified, open in existing window
        else window.open(regLink);  // url is to external site, open in new window
    }
}

// this script should overwrite the default one provided by SharePoint to expand and contract data views, which does not work in firefox
function ExpGroupBy(formObj) {
    if ((browseris.w3c) && (!browseris.ie)) {
        document.all = document.getElementsByTagName("*");
    }
    docElts = document.all;
    numElts = docElts.length;
    images = formObj.getElementsByTagName("IMG");
    img = images[0];
    srcPath = img.src;
    index = srcPath.lastIndexOf("/");
    imgName = srcPath.slice(index + 1);
    if (imgName == 'plus.gif') {
        fOpen = true;
        displayStr = "";
        img.src = '/_layouts/images/minus.gif';
    }
    else {
        fOpen = false;
        displayStr = "none";
        img.src = '/_layouts/images/plus.gif';
    }
    oldName = img.name;
    img.name = img.alt;
    img.alt = oldName;
    spanNode = img;
    while (spanNode != null) {
        spanNode = spanNode.parentNode;
        if (spanNode != null && spanNode.id != null && spanNode.id.length > 5 && spanNode.id.substr(0, 5) == "group") break;
    }
    parentNode = spanNode;
    while (parentNode != null) {
        parentNode = parentNode.parentNode;
        if (parentNode != null && parentNode.tagName == "TABLE") break;
    }
    lastNode = null;
    if (parentNode != null) {
        lastNode = parentNode.lastChild;
        if (lastNode != null && lastNode.tagName == "TBODY") lastNode = lastNode.lastChild;
        if (lastNode != null && lastNode.tagName == "TR" && lastNode.lastChild != null) lastNode = lastNode.lastChild;
    }
    for (var i = 0; i < numElts; i++) {
        var childObj = docElts[i];
        if (childObj == spanNode) break;
    }
    ID = spanNode.id.slice(5);
    for (var j = i + 1; j < numElts; j++) {
        var childObj = docElts[j];
        if (childObj.id.length > 5 && childObj.id.substr(0, 5) == "group") {
            curID = childObj.id.slice(5);
            if (curID <= ID) return;
        }
        parentNode = childObj;
        while (parentNode != null) {
            parentNode = parentNode.parentElement;
            if (parentNode == spanNode) break;
        }
        if (parentNode == spanNode) continue;
        if (childObj != img && childObj.tagName == "IMG" && childObj.src && childObj.src.slice(childObj.src.length - 25) == '/_layouts/images/plus.gif') {
            childObj.src = '/_layouts/images/minus.gif';
            oldName = childObj.name;
            childObj.name = childObj.alt;
            childObj.alt = oldName;
        }
        if (childObj.tagName == spanNode.tagName && childObj.id != "footer") {
            childObj.style.display = displayStr;
        }
        if ((childObj.tagName == "TABLE" && lastNode == null) || childObj == lastNode) break;
    }
}

//this script fixes the infinite loop error in the Core.js file when using a doctype in the master page and trying to use "Edit in Datasheet"
function GCComputeSizing(GCObject) {
    if (TestGCObject(GCObject)) {
        var fBIDI = (document.documentElement.currentStyle.direction == "rtl");
        var lGCWindowWidth = document.documentElement.scrollWidth;
        var lGCWindowHeight = (document.documentElement.scrollHeight > document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.documentElement.scrollHeight;

        var lGCObjectOffsetLeft = 0;
        var lGCObjectOffsetTop = 0;
        if (fBIDI) {
            lGCObjectOffsetLeft = -180;
            lGCObjectOffsetTop = 120;
        }
        else {
            lGCObjectOffsetLeft = 32;
            lGCObjectOffsetTop = -2;
        }
        var lGCObjectWalker = GCObject.parentElement;
        while (lGCObjectWalker != document.body) {
            lGCObjectOffsetLeft += lGCObjectWalker.offsetLeft;
            lGCObjectOffsetTop += lGCObjectWalker.offsetTop;
            lGCObjectWalker = lGCObjectWalker.offsetParent;
            if (fBIDI)
                if (lGCObjectWalker.offsetLeft > 0)
                break;
        }
        lGCObjectOffsetLeft += GCObject.parentElement.offsetLeft;
        lGCObjectOffsetTop += GCObject.parentElement.offsetTop;
        glGCObjectHeight = lGCWindowHeight - lGCObjectOffsetTop;
        if (glGCObjectHeight > lGCWindowHeight)
            glGCObjectHeight = lGCWindowHeight
        if (glGCObjectHeight < cGCMinimumHeight) f
        glGCObjectHeight = cGCMinimumHeight;
        if (fBIDI) {
            glGCObjectWidth = lGCWindowWidth + lGCObjectOffsetLeft;
        }
        else
            glGCObjectWidth = lGCWindowWidth - lGCObjectOffsetLeft;
        if (glGCObjectWidth > lGCWindowWidth)
            glGCObjectWidth = lGCWindowWidth;
        if (glGCObjectWidth < cGCMinimumWidth)
            glGCObjectWidth = cGCMinimumWidth;
    }
}

function HandleDisabledCookies(redirectPage) {
    document.cookie = "testcookie";
    cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    
    if (!cookieEnabled) {
        window.location.href = redirectPage;
    }
}

//this script is used for the list driven drop menus
function FP_jumpMenu(el,frm,sel) {//v1.0

var href=el.options[el.selectedIndex].value; if(sel) el.selectedIndex=0;

if('_new'==frm) open(href); else eval(frm+".location='"+href+"'");

}
