img1 = new Image(); img1.src = 'images/XMG1p1.jpg';
img2 = new Image(); img2.src = 'images/XMG1p2.jpg';
img3 = new Image(); img3.src = 'images/XMG1p3.jpg';
img4 = new Image(); img4.src = 'images/XMG1p4.jpg';
img5 = new Image(); img5.src = 'images/XMG1p5.jpg';

// http://www.cryer.co.uk/resources/javascript/script19_fade_image.htm#self
// Opacity and Fade in script.
// Script copyright (C) 2008 http://www.cryer.co.uk/.
// Script is free to use provided this copyright header is included.
function SetOpacity(object,opacityPct)
{
  // IE.
  object.style.filter = 'alpha(opacity=' + opacityPct + ')';
  // Old mozilla and firefox
  object.style.MozOpacity = opacityPct/100;
  // Everything else.
  object.style.opacity = opacityPct/100;
}
function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
  var element=document.getElementById(id);
  var opacity = element.style.opacity * 100;
  var msNow = (new Date()).getTime();
  opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity<0) 
    SetOpacity(element,0)
  else if (opacity>100)
    SetOpacity(element,100)
  else
  {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
  }
}
function FadeIn(id)
{
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',400," + startMS + ",0,100)",1);
}

function FadeOut(id)
{
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',200," + startMS + ",100,0)",1);
}


function FadeInImage(foregroundID,newImage,backgroundID)
{
  var foreground=document.getElementById(foregroundID);
  if (backgroundID)
  {
    var background=document.getElementById(backgroundID);
    if (background)
    {
      background.style.backgroundImage = 'url(' + foreground.src + ')';
      background.style.backgroundRepeat = 'no-repeat';
    }
  }
  SetOpacity(foreground,0);
  foreground.src = newImage;
  if (foreground.timer) window.clearTimeout(foreground.timer); 
  var startMS = (new Date()).getTime();
  foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',300," + startMS + ",0,100)",10);
}


function showPic(num) {
	if(document.getElementById('photo').src.match('p'+num)) {return;} 
	document.getElementById('th1').style.borderColor='#FFF';
	document.getElementById('th1').style.background='#AAA';
	document.getElementById('th2').style.borderColor='#FFF';
	document.getElementById('th2').style.background='#AAA';
	document.getElementById('th3').style.borderColor='#FFF';
	document.getElementById('th3').style.background='#AAA';
	document.getElementById('th4').style.borderColor='#FFF';	
	document.getElementById('th4').style.background='#AAA';
	document.getElementById('th5').style.borderColor='#FFF';	
	document.getElementById('th5').style.background='#AAA';
	document.getElementById('th'+num).style.borderColor='#FF0000';	
	document.getElementById('th'+num).style.background='#FF0000';
	filename ='images/XMG1p' + num + '.jpg';
	FadeOut('photo');	
	CheckOut();
	}

function CheckOut() {
	// has the photo faded out yet?
	if(document.getElementById('photo').style.opacity=='0') {
		document.getElementById('photo').src=filename;
		FadeIn('photo');
		return;
		}
	window.setTimeout('CheckOut();', 100);
	}

function ccCalc(which) {
	// which = cc: upstream concentration, ao: aerosol output, q: flow rate

	var target = which+'Result';
	document.getElementById(target).innerHTML = 'wait...';

	var getString = 'r=' + which + 
				'&ao1=' + document.getElementById('ao1').value + 
				'&q1=' + document.getElementById('q1').value +
				'&cc2=' + document.getElementById('cc2').value +
				'&q2=' + document.getElementById('q2').value +
				'&ao3=' + document.getElementById('ao3').value +
				'&cc3=' + document.getElementById('cc3').value;

	var link = 'ccCalc.php?'+getString;

	dLoadBegin(link,target);

	var hideDiv = which+'CalcBut';
	var showDiv = which+'Result';
	document.getElementById(hideDiv).style.display='none';
	document.getElementById(showDiv).style.display='block';
	}

function resetCalc(which) {
	var showDiv = which+'CalcBut';
	var hideDiv = which+'Result';
	document.getElementById(showDiv).style.display='block';
	document.getElementById(hideDiv).style.display='none';
	document.getElementById(hideDiv).innerHTML = '';
	}


// DYNAMIC CONTENT LOADER -------------------------------------------------------------------------
function dLoadFinish(req,sourceFile,targetElement) {
	if (req.readyState == 4) { 
		if (req.status == 200 || request.status == 304) { 
			document.getElementById(targetElement).innerHTML = req.responseText;
			// Now, run any scripts that may have come in with this new HTML
			if(document.getElementById('dScripts')) {
				eval(document.getElementById('dScripts').innerHTML);
				}
			}
		else if(request.status == 404){
			alert("File not found on server.");
			document.getElementById(targetElement).innerHTML = 'error';
			}

		else {
			alert("Server Error.  Please try again later.");
			document.getElementById(targetElement).innerHTML = 'error';
			}

	}
}
function dLoadContinue(sourceFile,targetElement) {
	var req;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Error initializing XMLHttpRequest!");
	}

	if (req !== undefined) {
		req.onreadystatechange = function() {dLoadFinish(req,sourceFile,targetElement);};
		req.open("GET",sourceFile,true);
		req.send("");
	}
}

function dLoadBegin(sourceFile,targetElement) {
	dLoadContinue(sourceFile,targetElement);
	return false;
}
// End Dynamic Content Loader  -------------------------------------------------------

