﻿

var requests = new Array();
/*
Object definition to hold data relating to an ajax call.
This allows multiple calls to be madeo n the page and tracked in the above array.
*/
function ajaxTracker(caller) {    
    this.Caller = caller;

    if (window.XMLHttpRequest) { // Not IE 
        this.Request = new XMLHttpRequest();
        this.Request.onreadystatechange = processStateChange;
    }
    else if (window.ActiveXObject) { // IE
        this.Request = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

/* 
    Function to make an ajax call 
    Notes : Pass the html element that wishes to raise the call and the call url 
            the call will be made and the calling element will have its "innerhtml"
            property set to the value of the result in the call.
*/
function ajaxCall(callingObject, url) {
    var reqTracker = new ajaxTracker(callingObject);

    if (reqTracker.Request) {
        reqTracker.Request.onreadystatechange = processStateChange;
        try {
            reqTracker.Request.open("GET", url, true);
            reqTracker.Request.send();
            requests.push(reqTracker);
        }
        catch (e) {
            alert('Erm ... woops ...\n' + e);
        }
    }
}

/* 
    Function to handle results from an ajax call 
    Note : Basically when an ajax call is reponded to it handles that responce checking 
           the status of all calls and acting on them too if required.
*/
function processStateChange() {
    for (callNo in requests) {
        if (requests[callNo].Request.readyState == 4 || requests[callNo].Request.readyState == "complete") {
            // Complete
            if (requests[callNo].Request.status == 200) { // OK response
                requests[callNo].Caller.innerHTML = requests[callNo].Request.responseText;
            }
            else {
                // uh oh, think it failed
                requests[callNo].Caller.innerHtml = "<span class='error'>failed.</span>";
            }
        }
    }
}

/* 
    Renders a series of links useful for rss feed data 
    The links allow users to pull rss feeds in to their own feed readers.
*/
function RenderFeedClientLinks(FeedURL)
{
    document.write('<a href="http://www.bloglines.com/sub/' + FeedURL + '">' +
      '<img height="18" width="91" vspace="3" alt="bloglines" src="/images/icons/bloglines.gif" /></a>');
    document.write('<a href="http://www.feedzilla.com/mini/default.asp?ref=bbc&amp;url=' + FeedURL + '">' +
      '<img height="22" width="93" vspace="3" alt="feedzilla" src="/images/icons/feedzilla.gif" /></a>');
    document.write('<a href="http://add.my.yahoo.com/rss?url=' + FeedURL + '">' +
      '<img height="17" width="91" vspace="3" alt="my yahoo" src="/images/icons/myyahoo.gif" /></a>');
    document.write('<a href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=' + FeedURL + '">' + 
      '<img height="17" width="91" vspace="3" alt="newsgator" src="/images/icons/newsgator.gif" /></a>');
    document.write('<a href="http://www.live.com/?add=' + FeedURL + '">' +
      '<img height="17" width="91" vspace="3" alt="Microsoft Live" src="/images/icons/windowslive.gif" /></a>');

    document.write('<p><a href="' + FeedURL + '">Click Here</a> to see the feed xml.</p>');
}

// Produces Lightbox to show dialogs ...
// to create a lightbox based dialog simply pass the full url 
// and the size of the dialog to this function.
function ShowDialog(url, height, width) {
    try {
        // get the window dimensions for positioning the lightbox 
        var winWidth = document.all ? document.body.clientWidth : window.innerWidth;
        var winHeight = document.all ? document.body.clientHeight : window.innerHeight;
        // calculate the actual position of the lightbox
        var xpos = (winWidth - width) * 0.5;
        var ypos = (winHeight - height) * 0.5;
        
        // prepare the dialog container
        var dialog = document.getElementById('Dialog');
        if (dialog != null) {
            //set the position of the dialog
            dialog.style.left = xpos;
            //dialog.style.top = ypos;

            // populate the dialog with the content specified by the url ...
            dialog.src = url;
            //ajaxCall(dialog, url);

            // show the dialog elements
            document.getElementById('DialogBackground').style.visibility = 'visible';
            dialog.style.visibility = 'visible';
            dialog.style.width = width + "px";
            dialog.style.height = height + "px";
        }
    }
    catch (err) {
        alert('Couldnt open dialog:\n' + err);
    }
}

function CloseDialog(postback) {
    // hide the dialog elements
    parent.document.getElementById('DialogBackground').style.visibility = 'hidden';
    parent.document.getElementById('Dialog').style.visibility = 'hidden';
    return postback;
}

var panelstate = false;

function PrepareContent() {
    var frameList = document.getElementsByTagName('iframe');

    for (var i = 0; i < frameList.length; i++) {
        var frame = frameList[i];
        var contentStore = document.getElementById(frame.id + 'ContentInput');
        if (contentStore != null) {
            contentStore.value = frame.contentWindow.document.body.innerHTML
        }
    }
}

function OpenEditor() {
    window.open(document.getElementsByTagName('base').item(0).href + "Editor.aspx", "CMSEditor", "toolbar=0, location=0, resizable=1, height=1000, width=1300, top=20, left=200");
}


function TransferHTMLToContentArea(source) {
    try {
        // prepare helper class
        var qs = new Querystring();
        // get the target element
        var target = window.parent.document.getElementById(qs.params['Ref']);
        
        // copy the source HTML to the target iframe body tag 
        target.contentDocument.body.innerHTML = source.innerText;
        // and close :)
        CloseDialog();
    }
    catch(err)
    {
        alert(err);
    }
}

function Querystring(qs) {
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); 
	
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
