var WebPartElementID = "PrintContent";

function PrintWebPart() {
    var bolWebPartFound = false;
    if (document.getElementById != null) {
        //Create html to print in new window
        var PrintingHTML = '<HTML>\n<HEAD>\n';
        //Take data from Head Tag
        if (document.getElementsByTagName != null) {
            var HeadData = document.getElementsByTagName("HEAD");
            if (HeadData.length > 0)
                PrintingHTML += HeadData[0].innerHTML;
        }
        PrintingHTML += '\n</HEAD>\n<BODY>\n';
        var WebPartData = document.getElementById(WebPartElementID);
        if (WebPartData != null) {
            PrintingHTML += WebPartData.innerHTML;
            bolWebPartFound = true;
        }
        else {
            bolWebPartFound = false;
            alert('Cannot Find Web Part');
        }
    }
    PrintingHTML += '\n</BODY>\n</HTML>';
    //Open new window to print
    if (bolWebPartFound) {
        var PrintingWindow = window.open("", "PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");
        PrintingWindow.document.open();
        PrintingWindow.document.write(PrintingHTML);
        // Open Print Window
        PrintingWindow.print();
    }
}

var activeStyleSheet;
function setActiveStyleSheet(title) {
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
    this.title == title ? this.disabled = false : this.disabled = true;
    /*
    -- in comment because we removed Prototype ($)
    $('b_fontsmall', 'b_fontmedium', 'b_fontlarge').each(function(obj, index) {
    if (obj.id == "b_" + title) {
    obj.src = "/_layouts/images/BLG/" + obj.id + "_o.gif"
    obj.className += " selected"
    }
    else {
    obj.src = "/_layouts/images/BLG/" + obj.id + ".gif";
    obj.className = obj.className.replace(/ selected/ig, '');
    }
    });
    */
    chkImg(document.getElementById('b_fontsmall'), title);
    chkImg(document.getElementById('b_fontmedium'), title);
    chkImg(document.getElementById('b_fontlarge'), title);

    createCookie("fontSize", title, 365);
    activeStyleSheet = title;
}

function chkImg(obj, title) {
    if (obj.id == "b_" + title) {
        obj.src = "/_layouts/images/BLG/" + obj.id + "_o.gif"
        obj.className += " selected"
    }
    else {
        obj.src = "/_layouts/images/BLG/" + obj.id + ".gif";
        obj.className = obj.className.replace(/ selected/ig, '');
    }
}

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 expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}


function showOrHide(obj) {
    if (obj.style.display == "none") {
        obj.style.display = "block";
    }
    else {
        obj.style.display = "none";
    }
}


/* --------------------- EMAIL */

//set the aboslute position of the mail on page load
/* removed functionality from here as it was interfering with the swf
$(document).ready(function() {
    var top = document.getElementById("mainContentTD").getClientRects()[0].top;
    $("#div_stf").css({
        top: top
    });

});
*/
/*STF hint fields -------------------------------------------- */
function focusTB(ctr) {
    if (ctr.value == ctr.title) { ctr.value = ""; }
}

function blurTB(ctr) {
    if (ctr.value == "") { ctr.value = ctr.title; }
}

/* ----------------------------------------------------
toggleSTF, toggleThanks -------------------------------------------- */

function toggleSTF() {
    $("#div_stf").css("height", "250px").slideToggle('slow', function() { iframeHack(this); });
}

function toggleSTFThanks() {
    $("#div_stf_thanks").css("height", "250px").slideToggle('slow', function() { iframeHack(this); });
}

function toggleSTFError() {
    $("#div_stf_error").css("height", "250px").slideToggle('slow', function() { iframeHack(this); });
}




/* ----------------------------------------------------
validateSTF ----------------------------------------- */
function validateSTF() {

    $("#sender_name").removeClass("error");
    $("#friends_name").removeClass("error");
    $("#friends_email").removeClass("error");

    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var senderName = $.trim($("#sender_name").val());
    var friendName = $.trim($("#friends_name").val());
    var friendEmail = $.trim($("#friends_email").val());
    var sendComment = $.trim($("#sendComments").val());

    var valid = true;
    if (senderName == "" || senderName == $("#sender_name").attr("title")) {
        valid = false;
        $("#sender_name").addClass("error");
    }

    if (friendName == "" || friendName == $("#friends_name").attr("title")) {
        valid = false;
        $("#friends_name").addClass("error");
    }

    if (friendEmail == "" || friendEmail == $("#friends_email").attr("title") ||
		!filter.test(friendEmail)) {
        valid = false;
        $("#friends_email").addClass("error");
    }

    if (valid) {
        sendEmail(senderName,
	    	friendName,
		    friendEmail,
		    sendComment,
		    location.href);
    }
    else
    { $("#inputErrors").css("display", "block"); }

}




/* ----------------------------------------------------
sendEmail ------------------------------------------- */

function sendEmail(fromName, toName, toEmail, comments, url) {
    var soapEnv =
        "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
          <soap:Body> \
            <Send xmlns='http://BLGCanada.com/IT'> " +
              "<fromName>" + fromName + "</fromName>" +
              "<toName>" + toName + "</toName>" +
              "<toEmail>" + toEmail + "</toEmail>" +
              "<url>" + url + "</url>" +
              "<comments>" + comments + "</comments>" +
            "</Send> \
          </soap:Body> \
        </soap:Envelope>";

    $.ajax({
        type: "POST",
        url: "/_vti_bin/blg/sendemail.asmx",
        dataType: "xml",
        data: soapEnv,
        contentType: "text/xml; charset=\"utf-8\"",
        success: function(xml) {
            toggleSTF();
            if (xml.getElementsByTagName("SendResult")[0].text == "true")
                toggleSTFThanks();
            else
                toggleSTFError();
        },
        customError: function(msg) {
            alert('fail');
            toggleSTF();
            toggleSTFError();
        }
    });
}

function iframeHack(target) {

    if ($.browser.msie) {
        $('select').toggle();
        obj = document.getElementsByName("filter")[0];
        ie6 = (window.navigator.appVersion.search("MSIE 6") != -1)
        if (obj && ie6) {

            if ((obj.style.visibility == "visible") || (obj.style.visibility == "")) {
                obj.style.visibility = "hidden";
            }
            else
                if (obj.ishide != "true" && obj.style.visibility == "hidden") {
                obj.style.visibility = "visible"

            }

        }
    } else {
        var heightiframe = parseInt($(target).css("padding-top").replace("px", "")) + parseInt($(target).css("margin-top").replace("px", "")) + parseInt($(target).css("margin-bottom").replace("px", "")) + parseInt($(target).css("height").replace("px", "")) + parseInt($(target).css("borderBottomWidth"));
        $("#iframe").css("height", heightiframe).toggle();
    }
}

function getImageFromXML(xmlFile) {
    var theImages = new Array();
    var xmlDoc = null;
    if (window.ActiveXObject) {// code for IE
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    }
    else if (document.implementation.createDocument) {// code for Mozilla, Firefox, Opera, etc.
        xmlDoc = document.implementation.createDocument("", "", null);
    }
    else {
        alert('Your browser cannot handle this script');
    }
    if (xmlDoc != null) {
        xmlDoc.async = false;
        xmlDoc.load(xmlFile);
        var x = xmlDoc.getElementsByTagName("image");

        for (var i = 0; i < x.length; i++) {
            theImages[i] = x[i].getElementsByTagName("url")[0].childNodes[0].nodeValue
        }
        return theImages;
    }
}

function randomImage(theImages) {
    var j = 0
    var p = theImages.length;
    var preBuffer = new Array()
    for (i = 0; i < p; i++) {
        preBuffer[i] = new Image()
        preBuffer[i].src = theImages[i]
    }
    var whichImage = Math.round(Math.random() * (p - 1));

    var img = document.getElementById("header_flash_holder");
    img.innerHTML = "<img src='" + theImages[whichImage] + "' width='754' height='120' alt='In a changing world, the right answers make all the difference.' />";
}
	