//
//  functions.js
//
//  A suite of functions used by the Angry Koala
//  report surfer site and probably a few others
//  in the future.
//
//  Shaun Bliss   11/12/08
//
//

// Pseudo Global Variables
var imagePath = '/Images/AKGraphics/';
var loginPage = '/Login.htm';


//
//  Sets up a cross browser routine to find object
//  easily
//
document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
}; 

//  
//  formatLinkFromTag
//
//  This function takes a WebApp tag like {tag_name} and
//  strips out the url from the href.  It then formats a new
//  anchor tag using the parameter htmlStr as the object
//  to be given a the tag's url href.
//
//  This is partcularly useful when trying to change a tag
//  to an image link instead of the default text.
//
//  mustBeLoggedIn if set to 1 means the link will only 
//  display if the user is logged in
//
//  altHtml will be displayed if the user is not logged in
//  (and mustBeLoggedIn is set to 1)
//
//
function formatLinkFromTag(tag, htmlStr, mustBeLoggedIn, altHtml) {

    //var tag = '<a href="http://www.blissweb.com/">Testing</a>';

    // extract the url from the tag if present
    // 
    if (tag!='') {

	// process a valid tag
	var href = new Array();
	href = tag.split('href');
	if (href[1]==null) { href=tag.split('src'); }
	var quotes = new Array();
	quotes = href[1].split('"');
	url = quotes[1];

	// reformat the anchor
	link='<a href="'+ url +'">'+htmlStr+'</a>';

    } else {

	link='';
    }

    // if must be logged in, check it,
    // if not logged in don't display anything
    //
    if (mustBeLoggedIn=="1") {
        if (loggedin!="1") {
            link=altHtml;
        }
    }

    // write the new link to the page
    // 
    document.write(link);

}

//  This will be passed in two pieces of html
//  and it will write one of them to the document
//  depending on whether the user is logged in or
//  not
//
function loginSwitch(doLoggedIn, doNotLoggedIn) {

    if (loggedin=="1") {
        document.write(doLoggedIn);
    } else {
        document.write(doNotLoggedIn);
    }

}

//  A simple, yet, very important function which tests to see
//  if a tag is blank or not.  If it is not, it will output
//  the htmlstr passed in.  This is used by the average rating
//  system.
//
function ifNotBlank(test, htmlStr) {

    if (test!='') {
        document.write(htmlStr);
    }
}



//  This changes the menu from insecure mode to
//  secure mode.  It should remove all the lock
//  icons, change the reportlibrary link and
//  change the Sign In link.
//
function secureMenuCheck() {

    if (loggedin=="1") {

	//  Remove lock icons
	var elements = new Array();
	elements = document.getElementsByClassName('locked-menu');
	for (var i = 0; i < elements.length; i++) {
	    elements[i].firstChild.style.backgroundImage = "none";
	}

	//  Change report library link to secure - not necessary anymore
	//elements = new Array();
	//elements = document.getElementsByClassName('library');
	//elements[0].firstChild.href = "/ReportLibrarySecure.htm";

	//  Change sign in link to sign out
	elements = new Array();
	elements = document.getElementsByClassName('signin');
	elements[0].firstChild.href = "/LogOutProcess.aspx";
	elements[0].firstChild.innerHTML = "Sign Out";
    }

}

//  Globals needed for PopBox
popBoxWaitImage.src = "AKScripts/Popbox/images/spinner40.gif";
popBoxRevertImage = "AKScripts/Popbox/images/magminus.gif";
popBoxPopImage = "AKScripts/Popbox/images/magplus.gif";

//  Generate a preview image tag which will have the PopBox 
//  hooks to zoom in on an image whilst staying on the page
//
function getPreviewImageTag(imageUrl) {
    var imageUrlCopy = imageUrl;
    var imageStemBits = imageUrlCopy.split("/");
    var imageFile = imageStemBits[imageStemBits.length-1];
    var imagePathBits = imageUrlCopy.split(imageFile);
    var imagePath = imagePathBits[0];
    var tnImageUrl = imagePath + "tn_" + imageFile;
    var ftdImageUrl = imagePath + "ftd_" + imageFile;
    var imageTag = '<IMG alt="Click Image to Enlarge" ' + 'id="'+ tnImageUrl+'" pbsrcNL="'+imageUrl+'" src="'+ tnImageUrl
//	+ '" onclick="Pop(this,50,\'PopBoxImageLarge\');" />';
	+ '" onclick="PopEx(this,null,null,0,null,50,\'PopBoxImageLarge\');" />';
    document.write(imageTag);
}


function getRunReportBtn(reportLink, btnSize) {

//    var reportLink = "http://run.reportsurfer.com/Reports/Pages/Report.aspx?ItemPath=%2fSQL+Server+2005%2fReport+Pack%2fGet+More+Charts%2fMovingAverage";
//    var reportLink = "%2fSQL+Server+2005%2fReport+Pack%2fGet+More+Charts%2fMovingAverage";

    var reportPath = '';
    var reportServer = "http://run.reportsurfer.com/";
  //  var runCommand = "run_report.htm?report=" +
	//             "/Reports/Pages/Report.aspx%3fItemPath=";
    var runCommand = "run_report.htm?report=" +
	             "/ReportServer?";
    var noToolbar = "";  //  default on - to turn off "&rs:Toolbar=None";

    var runBtnImage = "run_report_btn.gif";

    if (btnSize!="") {
        runBtnImage = "run_report_btn_" + btnSize + ".jpg";
    }

    var runBtnImageUrl = '<img src="' + imagePath + runBtnImage + '" />';


    var reportLinkBits = reportLink.split("ItemPath=");
    if (reportLinkBits[1]==null) {
	// assume its just path
	reportPath=reportLink;
    } else {
	// assume its full url 
	reportPath=reportLinkBits[1];
    }
    var runUrl = reportServer + runCommand + reportPath + noToolbar;

    var runReportBtnHtml='<a target="_blank" href="' + runUrl + '">' + runBtnImageUrl + "</a>"
    document.write(runReportBtnHtml);	

}


function getFavoriteReportBtn(favoriteLink, btnSize) {

//    var favoriteLink = '<a class="favoritelink" href="/FavoriteProcess.aspx?A=Remove&amp;OID=130469&OTYPE=35">akremove</a>';
//    var favoriteLink = '<a class="favoritelink" href="/FavoriteProcess.aspx?A=Remove&amp;OID=130469&OTYPE=35">akadd</a>';
    var removeImageName = "remove_from_favorites_btn_" + btnSize + ".jpg";
    var addImageName = "add_to_favorites_btn_" + btnSize + ".jpg";
    var imgStartHtml = '<img src="' + imagePath; 
    var imgEndHtml = '" />'; 
    favoriteLink = favoriteLink.replace("akremove", imgStartHtml + removeImageName + imgEndHtml);
    favoriteLink = favoriteLink.replace("akadd", imgStartHtml + addImageName + imgEndHtml);

    var loginLink = '<a href="' + loginPage + '">' + 
   	            imgStartHtml + addImageName + imgEndHtml + "</a>";

    if (loggedin=="1") {   
        favoriteHtml = favoriteLink;	
    } else {
        favoriteHtml = loginLink;	
    }

    document.write(favoriteHtml);	

}


function getEditReportBtn(editLink, btnSize) {

//    var editLink='<img src="blah/blah/blah" />';
//    var editLink='';
    var editImageName = 'edit_report_btn_' + btnSize + ".jpg";
    var imgStartHtml = '<img src="' + imagePath; 
    var imgEndHtml = '" />'; 
    var editImageUrl = imgStartHtml + editImageName + imgEndHtml;

    formatLinkFromTag(editLink, editImageUrl, "1", '');

}    


function getDownloadReportBtn(downloadLink, btnSize) {

//    var downloadLink= '<a href="/Reports/BubbleChart.zip" target="_blank">BubbleChart.zip</a>';

    var downloadImageName = 'download_report_btn_' + btnSize + ".jpg";
    var imgStartHtml = '<img src="' + imagePath; 
    var imgEndHtml = '" />'; 
    var downloadImageUrl = imgStartHtml + downloadImageName + imgEndHtml;

    var loginPage = "Login.htm";
    var loginUrl = '<a href="' + loginPage + '">' + 
   	                         downloadImageUrl + "</a>";
    formatLinkFromTag(downloadLink, downloadImageUrl, "1", loginUrl);

}    

function getEmbedCode() {

    var embedCode = '<input type="input" size=40 onClick="this.select();" value=\'' +
                    '<a href="http://www.reportsurfer.com/_webapp_131799/Fields_in_Page_Header">' +
                    'Report Title Report at ReportSurfer</a>\' />';
    document.write(embedCode);
}






