// Reports Functions

function runScripts(theResponse) {			
	// contentElem is the HTML element you've loaded via ajax
	if (!theResponse)
		theResponse = 'ajaxContent';
		
	scriptx = document.getElementById(theResponse).getElementsByTagName("script");

	// Load scripts into new array because 
	// scriptx seems to change as scripts execute, at least in some browsers
	scripts = new Array();
	for (var idx=0; idx<scriptx.length; idx++) {
		scripts[idx] = scriptx[idx].text;
	}

	// execute each script in turn
	for(idx=0; idx<scripts.length; ++idx) {
		if (scripts[idx].length!=0) {
			try {
				// create a function for the script & execute it
				f = new Function(scripts[idx]);
				f();
			} catch(se) {
				alert("Script error (" + se.message + "):\n"+scripts[idx]);
			}
		}
	}
}

//Ajax loadThis function
var xmlhttp = new Array();
var i = 0;

function loadThis(url, destination, functocall, showLoadAnimation, ajaxWidth, animation, action) {
		
	animationArray = new Array();
	animationArray[1] = 'ajax_iamloading.gif';
	animationArray[2] = 'ajax-loader-bar.gif';
	animationArray[3] = 'ajax-loader-flower.gif';

	if(!ajaxWidth) 	ajaxWidth = 400;
	if(!animation) 	animation = 1;
	if(!action) 	action = 'replace';
	
	if (!destination) {
		destination = "ajaxContent";
		if(!document.getElementById("ajaxContainer")) {		
				
			winHeightD	= $(document).height(); 

			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				winWidth = window.innerWidth;
				winHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				winWidth = document.documentElement.clientWidth;
				winHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				winWidth = document.body.clientWidth;
				winHeight = document.body.clientHeight;
			}

			sWinHeight = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
				sWinHeight = window.pageYOffset;
			} else if( document.body &&  document.body.scrollTop  ) {
				//DOM compliant
				sWinHeight = document.body.scrollTop;
			} else if( document.documentElement && document.documentElement.scrollTop  ) {
				//IE6 standards compliant mode
				sWinHeight = document.documentElement.scrollTop;
			} 
			winHeightD = winHeightD-5;

			$(document.body).append('<div id="coverImage" style="height:'+winHeightD+'px"></div>');
	
			//$(document.body).append('<div id="ajaxContainer" style="z-index:20; display:none; width:'+ajaxWidth+'px"><div id="ajaxInnerContainer"><img src="images/ajax_left-corner.png" alt="" border="0" class="png" width="16px" id="ajaxContainer_c_1" /><img src="images/ajax_right-corner.png" alt="" border="0" class="png" width="16px" id="ajaxContainer_c_2" /><div id="ajaxContainer_top" class="png"></div><div id="ajaxContainer_left" class="png"><div id="ajaxContentBox" class="png"><div id="ajaxContent" class="png" style="width:'+(ajaxWidth-30)+'px"></div></div></div><div id="ajaxContainer_bottom" class="png"></div><img src="images/ajax_left-corner-b.png" class="png" alt="" border="0" width="16px" id="ajaxContainer_c_3" /><img src="images/ajax_right-corner-b.png" alt="" border="0" class="png" width="16px" id="ajaxContainer_c_4" /><a href="javascript:" onclick="closeAjax();" id="ajaxClose_btn" title="close"><img src="images/ajax_close-big.png" border="0" alt="Close" class="png" width="48px" height="48px" /></a></div></div>');
			$(document.body).append('<div id="ajaxContainer" style="z-index:20; display:none; width:'+ajaxWidth+'px"><div id="ajaxContent" class="png" style="width:'+(ajaxWidth-4)+'px"></div><a href="javascript:" onclick="closeAjax();" id="ajaxClose_btn" title="close"><img src="images/ajax_close-big.png" border="0" alt="Close" class="png" width="48px" height="48px" /></a></div>');

		}
	} else {
		if (showLoadAnimation) {
			// show loading image
			destHight = ($('#'+destination).height())-2;
			$('#'+destination).empty();
			if(animation==1) {
				$('#'+destination).append('<div ID="ajaxLoader"><div ID=ajaxLoadIcon style="background:url(images/'+animationArray[animation]+') center center no-repeat;"></div></div><div style="clear:both"></div>');
			} else {
				$('#'+destination).append('<div ID=ajaxLoadIcon style="padding:0; height:'+destHight+'px; background:url(images/'+animationArray[animation]+') center center no-repeat;"></div><div style="clear:both"></div>');	
			}
			
		}
	}
	
	xmlhttp[i]=null
	if (functocall=="undefined") functocall = "";
	if (destination=="undefined") destination = "";
	if (window.XMLHttpRequest) {
		xmlhttp[i] = new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		xmlhttp[i] = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	if (xmlhttp[i]!=null) {
		var currentTime = new Date();
		xmlhttp[i].onreadystatechange=new Function("loaded("+i+",'"+destination+"','"+functocall+"','"+action+"')")
		xmlhttp[i].open("GET",url,true)
		xmlhttp[i].send(null)
	} else {
		alert("Your browser does not support AJAX")
	}
	i=i+1
	
}

function loaded(b,destination,functocall,action) {

	if (xmlhttp[b].readyState==4 || xmlhttp[b].readyState=="complete") {
		
		if (xmlhttp[b].status == 200) {
			if (destination != "undefined" && destination != "" && destination!="hidden" && destination != null && xmlhttp[b].responseText!=''){ 
				//document.getElementById(destination).innerHTML = xmlhttp[b].responseText;
				if(action=='replace') {
					$('#'+destination).html(xmlhttp[b].responseText);
				} else if(action=='append') {
					$('#'+destination).append(xmlhttp[b].responseText);
				} else if(action=='prepend') {
					$('#'+destination).prepend(xmlhttp[b].responseText);
				}
				
				adjustAjax();
			}
			if (functocall!="undefined" && functocall!="") {
				
				if (functocall.indexOf('(') > -1)
					eval(functocall);
				else 
					eval(functocall+'()');
			}
		} else {
			//alert("Problem retrieving data")
			//alert(xmlhttp[b].status)
		}
	}
}

function adjustAjax() {
	
	winHeightD	= $(document).height(); 
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}

	sWinHeight = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		sWinHeight = window.pageYOffset;
	} else if( document.body &&  document.body.scrollTop  ) {
		//DOM compliant
		sWinHeight = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop  ) {
		//IE6 standards compliant mode
		sWinHeight = document.documentElement.scrollTop;
	} 
	
	acWidth		= $('#ajaxContainer').width();
	acHeight	= $('#ajaxContainer').height();
	
	pHeight = (winHeight/2)-(acHeight/2)+sWinHeight;
	pWidth = (winWidth/2)-(acWidth/2);
	
	$('#ajaxContainer').css('top',pHeight+'px');
	$('#ajaxContainer').css('left',pWidth+'px');
	$('#ajaxContainer').css('z-index','100');
	
	$('#ajaxContainer').fadeIn("fast");		
}

function closeAjax() {
	$('#ajaxContainer').fadeOut("fast",function callback() {$('#ajaxContainer').remove()});
	$('#coverImage').fadeOut("fast",function callback() {$('#coverImage').remove()});
}

