/*###############################################################
#	Broken Image Fixer v1										#
#	used on http://dev.bcct.ca/									#
#	Author: Lee Beckman (BCCT - Junior Developer)				#
#	SRS: O:\Lee Beckman\Broken_Image_Fixer_Design.doc			#
#																#
#	Last Updated: Jan 16th, 2008								#
###############################################################*/

//Uses DOM to get a list of images on pages and AJAX calls to check if their src's are valid
var myreq;	
var imgs;
var width;
var height;
function fiximages()
{
	imgs = document.getElementsByTagName("img");
	for(i = 0; i < imgs.length; i++) {									
		if (window.XMLHttpRequest)
			myreq = new XMLHttpRequest();
		else if (window.ActiveXObject)
			myreq = new ActiveXObject('Microsoft.XMLHTTP');
			
		width = imgs[i].width;
		height = imgs[i].height;
		myreq.open("get", imgs[i].src, false);
		myreq.send(null); 
		//Used by to check if src loaded and to replace src on failure
		if(myreq.readyState == 4)
		{
			if(myreq.status != 200)
			{
				imgs[i].src = "/Images/brokenimg.gif";
				if (width != 0 && height != 0) {
					imgs[i].style.width = width;
					imgs[i].style.height = height;
				}
			}
		}
	}
}

//Used to add an event checking for broken images after the page as loaded without displacing other onload events
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(fiximages);
