
//###########################################################################################################

//###########################################################################################################


function createSlideShow()
{
	nomaShow = new jsSlideShow();
	nomaShow.name="nomaShow"
	slideShowWhole = document.getElementById("slideShowObject");
	slideShowLayers = slideShowWhole.getElementsByTagName("div");
	for(i=0;i<slideShowLayers.length;i++)
	{
		if(slideShowLayers[i].className=="F3_slideshowLayer" || slideShowLayers[i].className=="F3_slideshowBlankLayer")
		{
			nomaShow.addLayerToShow(slideShowLayers[i].id);
		};
	};
	nomaShow.resetShow();
	nomaShow.runShow();
};



function jsSlideShow()
{
	this.layers = new Array();
	this.current = 0;
	this.currentOpac = 10;
	this.stepValue = 0.5;
	this.holdVal = 100;
	this.holdValCounter = 0;
	this.name="";
};


function addLayerToShow(layer_name)
{
	this.layers.push(layer_name);
};

function resetShow()
{
	for(i=0;i<this.layers.length;i++)
	{
		
		targ = document.getElementById(this.layers[i]);
		setOpacity(targ,this.currentOpac);
	};
	this.current = 0;
	this.currentOpac = 10;
	this.holdValCounter=this.holdVal;
};

function runShow()
{
	
	this.interval = setInterval(proxyFunction,100);
};

function endShow()
{
	clearInterval(this.interval);
};

function change()
{
	//db = document.getElementById("debug");
	//db.value="this.current:"+this.current+"\nthis.currentOpac:"+this.currentOpac+"\nthis.holdValCounter"+this.holdValCounter;
	if(this.holdValCounter==this.holdVal)
	{
		targ = document.getElementById(this.layers[this.current]);
		this.currentOpac = this.currentOpac-this.stepValue;
		setOpacity(targ,this.currentOpac);
		if(this.currentOpac==0)
		{
			this.currentOpac=10;
			this.holdValCounter = 0;
			this.current++;
		};
		if(this.current==this.layers.length)
		{
		this.resetShow();
		//this.current=0;
		};
	} else {
		this.holdValCounter++;
	};
};

function proxyFunction()
{
nomaShow.change();
};

jsSlideShow.prototype.addLayerToShow = addLayerToShow;
jsSlideShow.prototype.resetShow = resetShow;
jsSlideShow.prototype.runShow = runShow;

jsSlideShow.prototype.endShow = endShow;
jsSlideShow.prototype.change = change;



function setOpacity(obj,val)
{
		
		obj.style.opacity = val/10;
		obj.style.filter = 'alpha(opacity=' + val*10 + ')';
};



//###########################################################################################################

//###########################################################################################################

testImage = new Image();

boxImagePadding=20;
var picHeight;
var picWidth;
var totalHeight;
var totalWidth;
var scrollDist;
var windowWidth;

var currentGalleryID;
var currentPictureID;

var activeLabel;
var activeWin = false;

galleryRegister = new Array();

function scanForGalleries()
{
	//this function	registers the picture grid so that...
	
	inc=0;
	
	mainStage = document.getElementById("F3_page_mainlow");
	collectDivs = mainStage.getElementsByTagName("div");
	for(i=0;i<collectDivs.length;i++)
	{
		if(collectDivs[i].className=="F3_picGrid")
		{
			//alert(collectDivs[i].className);
			registerGallery(collectDivs[i],inc);
			inc++;
		};
	};
}

function registerGallery(obj,gallID)
{

	tempObj = new Object();
	tempObj.id = obj.id;
	tempObj.pics = new Array();
	pics = obj.getElementsByTagName("div");
	
	testCount = 0;
	
	
	for(j=0;j<pics.length;j++)
	{
		
		if(pics[j].className=="F3_picGridImageHolder")
		{
			pics[j].id=obj.id+","+gallID+","+testCount;
			testCount++;
			
			picObj = new Object();
			
			picObj.id = pics[j].id
			picObj.src = pics[j].getAttribute("masterSrc")
			picObj.height = pics[j].getAttribute("masterHeight")
			picObj.width = pics[j].getAttribute("masterWidth")
			
			tempObj.pics.push(picObj);
		}
		
	}
	
	tempObj.picCount = testCount;
	
	galleryRegister.push(tempObj);
	
}


function showPrevNext()
{
	document.getElementById("F3_picLayerNext").style.height="24px";
	document.getElementById("F3_picLayerNext").style.width="24px";
	document.getElementById("F3_picLayerNext").style.overflow="hidden";
	document.getElementById("F3_picLayerPrevious").style.height="24px";
	document.getElementById("F3_picLayerPrevious").style.width="24px";
	document.getElementById("F3_picLayerPrevious").style.overflow="hidden";
	document.getElementById("F3_picLayerSep").style.height="24px";
	document.getElementById("F3_picLayerSep").style.width="6px";
	document.getElementById("F3_picLayerSep").style.overflow="hidden";
}

function hidePrevNext()
{
	document.getElementById("F3_picLayerNext").style.height="1px";
	document.getElementById("F3_picLayerNext").style.width="1px";
	document.getElementById("F3_picLayerNext").style.overflow="hidden";
	document.getElementById("F3_picLayerPrevious").style.height="1px";
	document.getElementById("F3_picLayerPrevious").style.width="1px";
	document.getElementById("F3_picLayerPrevious").style.overflow="hidden";
	document.getElementById("F3_picLayerSep").style.height="1px";
	document.getElementById("F3_picLayerSep").style.width="1px";
	document.getElementById("F3_picLayerSep").style.overflow="hidden";
}


function explodePic(obj)
{
		hidePrevNext();
		if(activeWin)
		{
			galleryImageExit();
			
		};
		mstrHeight = obj.getAttribute("masterHeight");
		mstrWidth = obj.getAttribute("masterWidth");
		mstrSrc = obj.getAttribute("masterSrc");
		//alert(mstrSrc);
		galleryloadImage(obj.getAttribute("masterSrc"),obj.getAttribute("masterHeight"),obj.getAttribute("masterWidth"),obj.id);
		activeWin = true;

};

function relayloadImage(obj)
{
		showPrevNext();
		if(activeWin)
		{
			galleryImageExit();
			
		};
		galleryloadImage(obj.getAttribute("masterSrc"),obj.getAttribute("masterHeight"),obj.getAttribute("masterWidth"),obj.id);
		activeWin = true;
}

function galleryImageNext(inc)
{	
	showPrevNext();
	whatIsTheMin = 1;
	whatIsTheMax = galleryRegister[currentGalleryID].picCount-1;
	
	whatIsTheProposedID = currentPictureID+inc;
	
	//alert(whatIsTheProposedID);
	
	if(whatIsTheProposedID==-1)
	{
		newPic = whatIsTheMax;
	} else if (whatIsTheProposedID>whatIsTheMax){
		newPic = 0;
	} else {
		newPic = whatIsTheProposedID;
	}

	//alert(newPic);

	
	newId = document.getElementById(galleryRegister[currentGalleryID].id+","+currentGalleryID+","+newPic);
	relayloadImage(newId);
}



function galleryloadImage(imageSrc,imageHeight,imageWidth,objId)
{
	//alert(imageSrc);
	
	dec = objId.split(',');
	//alert(dec[1]);
	currentGalleryID = parseInt(dec[1]);
	currentPictureID = parseInt(dec[2]);
	
	activeLabel = document.getElementById(objId).getElementsByTagName("img")[0].getAttribute("alt");
	//alert(activeLabel);
	//alert(imageSrc);alert(imageHeight);alert(imageWidth);
	 
	picHeight = parseInt(imageHeight);
	picWidth = parseInt(imageWidth);
	
	scrollDist = document.body.scrollTop;
	//alert(scrollDist);
	windowWidth = document.body.clientWidth;
	//alert(windowWidth);
	

	
	totalHeight = parseInt(boxImagePadding) + parseInt(imageHeight);
	totalWidth = parseInt(boxImagePadding) + parseInt(imageWidth);
	
	xPos = Math.floor((windowWidth/2) - (totalWidth/2));
	yPos = scrollDist+30;
	
	
	//show the loading window.
	showHideGalleryLayer(true,parseInt(totalHeight),parseInt(totalWidth),parseInt(xPos),parseInt(yPos));
	
	//get the image
	testImage.src = imageSrc;
	testImage.onload = galleryImageLoaded;
	
	//load the image into the window

}


function galleryImageLoaded()
{

imageObj = document.getElementById("F3_picLayerImage");
statusObj = document.getElementById("picMessageStatus");
messageObj = document.getElementById("picMessage");


imageObj.src = testImage.src;
imageObj.height = picHeight - boxImagePadding;
imageObj.width = picWidth - boxImagePadding;


imageObj.style.visibility = "visible";
messageObj.innerHTML = activeLabel;
statusObj.innerHTML = "Photo: ";
};

function galleryImageExit()
{

imageObj = document.getElementById("F3_picLayerImage");
statusObj = document.getElementById("picMessageStatus");
messageObj = document.getElementById("picMessage");

imageObj.style.visibility = "hidden";
statusObj.innerHTML = '<img src="../../../../../images/F3_ajax-loader_w16_h16.gif" height="16" width="16"/>';
messageObj.innerHTML = "";
showHideGalleryLayer(false,1,1,0,0);
activeWin = false;
};

function showHideGalleryLayer(x,height,width,xpos,ypos)
{
	layerObj = document.getElementById("F3_picLayer");
	controlObj = document.getElementById("F3_picLayerControl");
	imageObj = document.getElementById("F3_picLayerPic");
	if(x){
		layerObj.style.visibility="visible";
		createDropShadow(ypos,xpos,height+40,width+40,5);

	} else {
		layerObj.style.visibility="hidden";
		removeDropShadow(5);
	};
		layerObj.style.height=height+40;
		layerObj.style.width=width+40;
		controlObj.style.width=width+40;
		
		layerObj.style.top=ypos;
		layerObj.style.left=xpos;
		
		/*imageObj.style.height=height+40;*/
		imageObj.style.width=width+40;
		imageObj.style.paddingTop=10;
		
		/*imageObj.style.marginTop = 20;
		imageObj.style.marginBottom = 20;
		imageObj.style.marginLeft = 20;
		imageObj.style.marginRight = 20;*/
	
};


function createDropShadow(yposRoot,xposRoot,height,width,count)
{
	for(i=0;i<count;i++)
	{
		newDiv = document.createElement("div");
		newDiv.className = "F3_picLayerShadow";
		newDiv.style.top = i+yposRoot;
		newDiv.style.left = i+xposRoot;
		newDiv.style.height = height;
		newDiv.style.width = width;
		newDiv.id="shadow"+i;
		document.body.appendChild(newDiv);
		
	};
	
};


function removeDropShadow(count)
{
	
	for(i=0;i<count;i++)
	{

			var d = document.body; 
			var d_nested = document.getElementById("shadow"+i); 
			var throwawayNode = d.removeChild(d_nested);
		
	};
	
};








SyncLoaderStack = new Array();
SyncLoaderCount = 0;


function checkSyncLoader(nid)
{
	//alert(nid);
	stackObj = SyncLoaderStack[nid]
	//document.getElementById(stackObj.returnDivID).innerHTML+=stackObj.checkCount+","
	
	if(stackObj.checkCount<=stackObj.maxCheckCount)
	{
	//still within time
	if(stackObj.xmlObject==null)
			{
				//xml is still loading
				stackObj.xmlObjectLoaded = false;
			} else {
				//xml is loaded
				stackObj.xmlObjectLoaded = true;
				if(stackObj.xslObject==null)
				{
					//xsl is still loading
					stackObj.xslObjectLoaded = false;
				} else {
					//xsl is loaded
					stackObj.xslObjectLoaded = true;
					//both xml and xsl are loaded - do the xform
					if(stackObj.dataDisplayed)
					{
					//data is displayed - do nothing	
					} else {
					stackObj.xformNow();
					//alert("transform now");
					stackObj.dataDisplayed = true;
					clearInterval(stackObj.interval);
					}
				};
			};
			stackObj.checkCount++;
	} else {
		//time is out!
		clearInterval(stackObj.interval);
		if(stackObj.xmlObject==null)
		{
		document.getElementById(stackObj.returnDivID).innerHTML = "There has been a fault retrieving the xml object from "+	stackObj.xmlObjectFile+"<br/>";
		}
		if(stackObj.xslObject==null)
		{
		document.getElementById(stackObj.returnDivID).innerHTML = "There has been a fault retrieving the xsl object from "+	stackObj.xslObjectFile+"<br/>";
		};
	};
};





function scanForAsyncLoader()
{
	pageContent = document.getElementById("F3_page_mainlow");
	allDivsIn_pageContent=pageContent.getElementsByTagName("div");
	
	for(u=0;u<allDivsIn_pageContent.length;u++)
	{
		if(allDivsIn_pageContent[u].className=="F3_asyncLoader")
		{
			tObj=allDivsIn_pageContent[u];
			//alert(tObj.id);
			temp = new AsyncRequest(tObj.id,tObj.getAttribute("xmlSrc"),tObj.getAttribute("xslSrc"),SyncLoaderCount);
			SyncLoaderStack.push(temp);
			SyncLoaderCount++;
		}
	}

	
}

AsyncRequest.prototype.xformNow = xformNow;

function xformNow()
{
	if (window.ActiveXObject)
	{
		resultDocument=this.xmlObject.transformNode(this.xslObject);
		document.getElementById(this.returnDivID).innerHTML=resultDocument;
  	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
 	{
 	xsltProcessor=new XSLTProcessor();
  	xsltProcessor.importStylesheet(this.xslObject);
	resultDocument = xsltProcessor.transformToFragment(this.xmlObject,document);
	document.getElementById(this.returnDivID).innerHTML="";
	document.getElementById(this.returnDivID).appendChild(resultDocument);
  	}
	//document.getElementById(this.returnDivID).innerHTML = xformdata;
}

function AsyncRequest(objectID,objectXML,objectXSL,stackID)
{
	
	//alert("AsyncRequest:"+stackID);
	this.returnDivID = objectID;
	this.stackID = stackID;
	this.dataDisplayed = false;
	
	this.checkCount = 0;
	this.maxCheckCount = 200;
	
	
	this.xmlObjectFile = objectXML;
	this.xmlObjectLoaded = false;
	this.xmlObject = null;
	sendRequest(this.xmlObjectFile,stackID,"xmlObject");
	
	
	this.xslObjectFile = objectXSL;
	this.xslObjectLoaded = false;
	this.xslObject = null;
	sendRequest(this.xslObjectFile,stackID,"xslObject");
	
	this.interval = setInterval("checkSyncLoader("+stackID+")",500);
	//t=setTimeout(this.interval = setInterval(checkSyncLoader(stackID),500),1000);
	
}



function sendRequest(url,stackID,assignTo,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		//callback(req);
		eval("SyncLoaderStack[stackID]."+assignTo+"=req.responseXML");
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}


function loadDetail(cuid,name)
{
		newXML = "http://www.facingafrica.org/FA08/asp/sahara_marathon_proxy.asp?cuid="+cuid;
		newXSL = "http://www.facingafrica.org/FA08/xsl/special/sahara_marathon_content_single.xsl";
		tObj = document.getElementById("syncLoadFundsRaised");
		tObj.innerHTML = '<img src="/FA08/images/F3_ajax-loader_w16_h16.gif"/><br/>'+name+"<br/>Loading Individual Data...";
		temp = new AsyncRequest(tObj.id,newXML,newXSL,1);
		SyncLoaderStack.push(temp);
		SyncLoaderCount++;
}



//######################## FORM FUNCTIONS ##################################//




ResetForm = function()
{
	if(document.getElementById("donations"))
	{
	document.getElementById("M_as_part_of_spons_no").checked=true;
	DefaultSponsorshipStatus = false;
	CheckSponsorshipInterval = setInterval("CheckSponsorshipStatus()",500);
	CheckDonationInterval = setInterval("CheckDonationAmount()",500);
	}
}

CheckSponsorshipStatus = function()
{
	XactiveTable = document.getElementById("activeTable");
	XinactiveTable = document.getElementById("inactiveTable");
	XactiveInsertionPoint = document.getElementById("activeInsertionPoint");

	if(document.getElementById("M_as_part_of_spons_yes").checked)
	{
		if(DefaultSponsorshipStatus==false)
		{
			//there is a change - open it.
			DefaultSponsorshipStatus = true;
			insertedElement_1 = XactiveTable.insertBefore(document.getElementById("transRow1").cloneNode(true), XactiveInsertionPoint);
			insertedElement_2 = XactiveTable.insertBefore(document.getElementById("transRow2").cloneNode(true), XactiveInsertionPoint);
			insertedElement_3 = XactiveTable.insertBefore(document.getElementById("transRow3").cloneNode(true), XactiveInsertionPoint);
			insertedElement_4 = XactiveTable.insertBefore(document.getElementById("transRow4").cloneNode(true), XactiveInsertionPoint);
			insertedElement_5 = XactiveTable.insertBefore(document.getElementById("transRow5").cloneNode(true), XactiveInsertionPoint);
			insertedElement_6 = XactiveTable.insertBefore(document.getElementById("transRow6").cloneNode(true), XactiveInsertionPoint);
		}
	} else {
		if(DefaultSponsorshipStatus==true)
		{
			//there is a change - close it.
			//alert("move to inactive");
			DefaultSponsorshipStatus = false;
			removedElement_1 = XactiveTable.removeChild(document.getElementById("transRow1"));
			removedElement_2 = XactiveTable.removeChild(document.getElementById("transRow2"));
			removedElement_3 = XactiveTable.removeChild(document.getElementById("transRow3"));
			removedElement_4 = XactiveTable.removeChild(document.getElementById("transRow4"));
			removedElement_5 = XactiveTable.removeChild(document.getElementById("transRow5"));
			removedElement_6 = XactiveTable.removeChild(document.getElementById("transRow6"));
			}
	}
}




function CheckDonationAmount()
{
	UF_amount = document.getElementById("amount");
	UF_amount_value = parseFloat(UF_amount.value);
	UF_currency = document.getElementById("currency");
	UF_currency_value = UF_currency.options[UF_currency.selectedIndex].value;
	
	//alert(UF_amount_value);
	
	willThisBeOk = true;
	friendlyErrorMessage="";
	if(isNaN(UF_amount_value))
	{
	willThisBeOk = false;
	friendlyErrorMessage+="Please enter a valid amount.\n";
	}
	
	if(UF_amount_value<5)
	{
	willThisBeOk = false;
	friendlyErrorMessage+="The amount may not be less than 5.00 "+UF_currency_value+"\n";
	}
	
	donateNoteD = document.getElementById("donateNote");
	
	if(!willThisBeOk)
	{
	donateNoteD.innerHTML = friendlyErrorMessage;
	} else {
	donateNoteD.innerHTML = "&nbsp;";
	}
}

function CustomValidation()
{

willThisBeOk = true;
friendlyErrorMessage = "There were the following issues with your submission:\n";

UF_amount = document.getElementById("amount");
UF_giftaid = document.getElementById("M_giftaid_unconfirmed");
UF_currency = document.getElementById("currency");


UF_amount_value = parseFloat(UF_amount.value);
UF_giftaid_value = UF_giftaid.checked;
UF_currency_value = UF_currency.options[UF_currency.selectedIndex].value;

//alert(UF_amount_value+", "+UF_giftaid_value); //use this to check the submission


if(isNaN(UF_amount_value))
{
willThisBeOk = false;
friendlyErrorMessage+="You have not entered a valid donation amount.\n";
}

if(UF_amount_value<5)
{
willThisBeOk = false;
friendlyErrorMessage+="The donation amount may not be less than 5.00 "+UF_currency_value+"\n";
}

if(UF_giftaid_value)
{
willThisBeOk = false;
friendlyErrorMessage+="You must confirm whether you are a UK taxpayer for GIFT AID.";
}





if(willThisBeOk)
{
	return true;
} else {
	alert(friendlyErrorMessage);	
	return false;
};





}




