// xFade.js \\

var safetycatch=0;
var adddiv="dont";
var opacity="";

function xFade(filetoload, wheretoloadit, paragraph_count, action) { // needs a safety-catch \\
	
	if (paragraph_count) {
		filetoload=filetoload+"&adddiv="+adddiv; // do! or dont : controls display of the 'add paragraph' link \\
	}
	
	if (safetycatch<1) {
		paragraph_count=paragraph_count*1;
		if (paragraph_count) {
			
			new_wheretoloadit="paragraph"+paragraph_count;
			newdiv="<div id='"+new_wheretoloadit+"' class='paragraph clearboth'></div>";
			
			if (!document.getElementById(new_wheretoloadit)) {
				document.getElementById('paragraphs').innerHTML = document.getElementById('paragraphs').innerHTML+newdiv;
				// at this point we want to prevent the last added paragraph from showing the add-div link \\
				adddiv="dont";
			}
			
			hideme="hideme"+paragraph_count;
			if (document.getElementById(hideme)) {
				//fes(hideme,100,0,5);
				//alert(hideme);
				document.getElementById(hideme).style.display="none";
			}
		}
		
		if (filetoload && document.getElementById(wheretoloadit)) {
			fade(wheretoloadit,100,0,5,filetoload);
		}
		else {
			alert('the target element, (`'+wheretoloadit+'`) does not exist');
		}
	}
}

//////////////      i          a        z          n
function fade(wheretoloadit,startingopacity,endingopacity,increment,filetoload) { // replaces fes() \\

	if (safetycatch<1) {
		safetycatch=1;

		fs = increment;
		fc = 0;
		fa = (startingopacity - endingopacity) / fs;
		so=startingopacity;
		
		if (filetoload) {
			ft = setInterval("controlfade('"+wheretoloadit+"','"+filetoload+"')", 50);
		}
		else {
			ft = setInterval("controlfade('"+wheretoloadit+"')", 50);
		}
	}
	
}

function fesf(i, a, z, n,filetoload) {
	fes(i, a, z, n, "1",filetoload);
}


// CONTROL FADE \\

function controlfade(whattofade,filetoload) { // replaces fe() \\
	//alert(filetoload);
	fc++; // goes from 1 to 5 (normally)
	
	if (fa < 0) { setopacity(Math.abs(fc * fa), whattofade); }
	else { setopacity(so - (fc * fa), whattofade); }
	
	if (fc == 100) {
		alert('hi');
	}
	//alert(fc);
	if (fc == fs) {
		clearInterval(ft);
		safetycatch=0;

		if (filetoload=="fudgeit") {
			document.getElementById(whattofade).innerHTML = document.getElementById('fudgepacket').innerHTML;
			fade(whattofade, 0, 100, 10);
		}
		else if (filetoload) {
			loadXMLDoc(filetoload, whattofade,'fesr');
			//setTimeout("fesr('"+i+"',0,100,5)",200);
			//fesr(i,0,100,5);
		}
	}
}


function fadeandgo(whattofade,startingopacity,endingopacity,increment) {
	if (safetycatch<1) {
		safetycatch=1;

		fs = increment;
		fc = 0;
		fa = (startingopacity - endingopacity) / fs;
		so=startingopacity;
		
		zaptimer = setInterval("fadethenzap('"+whattofade+"')", 50);
	}
}

function fadethenzap(whattofade) { // replaces fe() \\

	fc++; // goes from 1 to 5 (normally)
	
	if (fa < 0) { setopacity(Math.abs(fc * fa), whattofade); }
	else { setopacity(so - (fc * fa), whattofade); }
	
	if (fc == 100) {
		alert('hi');
	}

	if (fc == fs) {
		clearInterval(zaptimer);
		safetycatch=0;
		document.getElementById(whattofade).style.visibility='hidden';
		document.getElementById(whattofade).style.display='none';
	}
}


// SET OPACITY \\

function setopacity(targetopacity, whattofade) { // replaces so() \\
	var objectopacity = document.getElementById(whattofade).style;
	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) { opacity = 99.999; }
	}
	objectopacity.filter = "alpha(opacity=" + targetopacity + ")";
	objectopacity.KhtmlOpacity = (targetopacity / 100); 
	objectopacity.MozOpacity = (targetopacity / 100);
	objectopacity.opacity = (targetopacity / 100);
}


function fesr(i, a, z, n) {
	fes(i, a, z, n, "-1");
}

function loadXMLDoc(filetoload, wheretoloadit,fesr) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.filename=filetoload;
		req.open("GET", filetoload, true);
		req.target=wheretoloadit;
		req.send(null);
		req.fesr=fesr;
	}
	// branch for IE/Windows ActiveX version
	else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", filetoload, true);
			req.send();
			req.fesr=fesr;
		}
	}
}

function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			document.getElementById(req.target).innerHTML = req.responseText;
			if (req.fesr) {
				fade(req.target,0,100,5);
			}
		}
		else {
			alert("There was a problem retrieving the file: "+req.filename);
		}
	}
}

function ajaxform(thisform,formhandler,div,action) {
	if (div && document.getElementById(div)) {
		//alert("form handler: "+formhandler);
	}
	else {
		alert(div+" does not exist");
	}
	if (action=="addform") { adddiv="do!"; }
	
	// Set up data variable
	var formdata = "";

	// Set up Ajax request variable
	try { xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Error: Could not load page."); }

	// Loop through form fields
	for (i=0; i < thisform.length; i++) {
		//Build Send String
		if (thisform.elements[i].type == "text") { //Handle Textbox's
			formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		}
		else if (thisform.elements[i].type == "textarea") { //Handle textareas
			formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		}
		else if (thisform.elements[i].type == "checkbox"){ //Handle checkbox's
			formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
		}
		else if (thisform.elements[i].type == "radio"){ //Handle Radio buttons
			if (thisform.elements[i].checked==true){
				formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
			}
		}
		else {
			//finally, this should theoretically this is a select box.
			formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
		}
	}

	//Send Ajax Request
	xmlhttp.onreadystatechange = function() {
		//Check page is completed and there were no problems.
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
			//What to do once the form is submitted - to inform the user.
			document.getElementById('fudgepacket').innerHTML = xmlhttp.responseText;
			//fes(i, a, z, n, zi,filetoload,reponse)
			fade(div,100,0,5,'fudgeit');
			//var action="document.getElementById("+div+").innerHTML = "+xmlhttp.responseText;
			//setTimeout(action,50);
		 }
	}
	
	//Make connection
	xmlhttp.open("POST", formhandler);
	
	//Set Headers
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	//Send data
	xmlhttp.send(formdata);
	
	//stops form from submitting normally
	return false;
}

// #### getElementsByClassName #### \\
/* document.getElementsByClassName = function(cl) { */
/* 	var retnode = []; */
/* 	var myclass = new RegExp('\\b'+cl+'\\b'); */
/* 	var elem = this.getElementsByTagName('*'); */
/* 	for (var i = 0; i < elem.length; i++) { */
/* 		var classes = elem[i].className; */
/* 		if (myclass.test(classes)) retnode.push(elem[i]); */
/* 	} */
/* 	return retnode; */
/* }; */

function xFadeChain (filetoload, wheretoloadit, filetoloadb, wheretoloaditb) {
	xFade(filetoload, wheretoloadit);
	//alert(filetoloadb +"-"+ wheretoloaditb)
	var setTImeoutAction="xFade("+filetoloadb+", "+wheretoloaditb+")"
	setTimeout("xFade('"+filetoloadb+"', '"+wheretoloaditb+"')",2000);
}
