﻿/* ___________________________________________________

AjaxTree()
- creates a new "ajax tree" object with the following methods:

	AjaxTree.IsComplete = function()
	- returns true after content is loaded, otherwise false

	AjaxTree.LoadBranch = function(BranchID, LoadUrl)
	 - loads content into specified element
	 - Parameters:
		 --- BranchID: ID of Html element where the content should be inserted
		 --- LoadUrl: page (xml file) to generate the content


	AjaxTree.SwitchBranch = function(BranchID, LoadUrl, BranchRootID, OpenCssClass, CloseCssClass)
	 - checks if element specified by BranchID is already displayed; if so, hides it.  
	 - if not, loads content into the element (and displays the element).  
	 - If BranchRootID and Open/CloseCssClass are specified, then the css class of the BranchRootID element specified is changed
		(for changing display of a header, surrounding box, etc. depending whether the branch is open or closed)
	 - Parameters:
		 --- BranchID: ID of Html element where the content should be inserted
		 --- LoadUrl: page (xml file) to generate the content
		 --- RootBranchID: ID of 'Root' HTML element to use to change Css
		 --- OpenCssClass: Class to set RootBranchID element if open
		 --- CloseCssClass: Class to set RootBranchID element if closed


	AjaxTree.SwitchTab = function(LoadElementID, LoadUrl, DefaultTabID, TabID, OpenCssClass, CloseCssClass)
	 - loads content into specified element, switching tab styles.  
	 - Parameters:
		 --- LoadElementID: ID of Html element where the content should be inserted
		 --- LoadUrl: page (xml file) to generate the content
		 --- DefaultTabID: ID of the tab element in the group of tabs that is selected by default
		 --- TabID: ID of the tab being opened - this tab will be set to the Open class, others (including the default) will be set to the close class
		 --- OpenCssClass: Class to set tab that is being opend
		 --- CloseCssClass: Class to set other tabs
		 

	AjaxTree.submitForm(oForm, ActionUrl, SuccessBranchID, SuccessUrl, SuccessExecString, ErrorBranchID, ErrorExecString, SuccessRedirectUrl)
	 - submits a form using post method and subsequently loads appropriate content or runs appropriate code on success.
	 - success is determined by the presence of an element with ID '_ajax_response_success' in the response.
	 - Parameters:
		 --- oForm: object reference to the Html form
		 --- ActionUrl: page (xml file) that handles the form submission
		 --- SuccessBranchID (optional): ID of Html element on the original page where success content should be inserted on success
		 --- SuccessUrl (optional): page (xml file) to generate the success content to be inserted into SuccessBranchID element on success
		 --- SuccessExecString (optional): string containing javascript code to be run on success
		 --- ErrorBranchID (optional): ID of Html element on the original page where user error mesage should be inserted; error
				message must be returned in element with ID '_ajax_response_error'
		 --- ErrorExecString (optional): string containing javascript code to be run on error
		 --- SuccessRedirectUrl (optional): Url to redirect calling page to on success


	AjaxTree.submitUrl(ActionUrl, SuccessBranchID, SuccessUrl, SuccessExecString, SkipSuccessValidation, ErrorBranchID, ErrorExecString, SuccessRedirectUrl) 
	 - executes page specified, and subsequently loads appropriate content or runs appropriate code on success.
	 - success is determined by the presence of an element with ID '_ajax_response_success' in the response.
	 - Parameters:
		 --- ActionUrl: the page (xml file) to execute
		 --- SuccessBranchID (optional): ID of Html element on the original page where success content should be inserted on success
		 --- SuccessUrl (optional): page (xml file) to generate the success content to be inserted into SuccessBranchID element on success
		 --- SuccessExecString (optional): string containing javascript code to be run on success
		 --- SkipSuccessValidation (optional): if true, then success is always assumed (does not check for 'success' XML tag in the ActionUrl page results). default is false
		 --- ErrorBranchID (optional): ID of Html element on the original page where user error mesage should be inserted; error
				message must be returned in element with ID '_ajax_response_error'
		 --- ErrorExecString (optional): string containing javascript code to be run on error
		 --- SuccessRedirectUrl (optional): Url to redirect calling page to on success


	AjaxTree.ShowFormResults(oForm, LoadElementID, LoadUrl) 
	 - loads content into specified element, passing querystring parameters from form
	 - Parameters:
		 --- oForm: object reference to the Html form
		 --- LoadElementID: ID of Html element where the content should be inserted
		 --- LoadUrl: page (xml file) to generate the content


PrintElement(ElementID)
 - prints contents of Html element (browser opens print dialogue)
	 --- ElementID: ID of Html element to print

___________________________________________________ */


/*
function ShowLoading(ElementID) {
	var myElement = document.getElementById(ElementID);
	myElement.style.display='visible';
}

function ShowLoadingLoop(myElement) {
	myElement.innerHTML='<span style="color:#aaaaaa;">Loading...</span>';
	
}
*/

var IFrameIDs = []; /*for resizing/loading of iFrames, if necessary*/

function PrintElement(ElementID) {

if (!document.getElementById('print_frame')) {
	try {
		document.body.innerHTML+='<iframe id="print_frame" style="height:0;" name="print_frame"></iframe>'; 
	}
	catch (e) {
		var wrappingDiv = document.createElement('div');
		wrappingDiv.innerHTML = '<iframe id="print_frame" name="print_frame"></iframe>';
		document.body.appendChild(wrappingDiv);
	}
}

        var printFrame = document.getElementById('print_frame');
        var doc = printFrame.contentDocument;
        if (doc == undefined || doc == null)
            doc = printFrame.contentWindow.document;
        doc.open();
        doc.write('<html><head><title>American Planning Association</title><link href="/WebInterface/inc/stylesheet.css" rel="stylesheet" type="text/css" /></head><body>' + document.getElementById(ElementID).innerHTML + '</body></html>');
        doc.close();
		try {
			document.print_frame.focus();
			document.print_frame.print();
		} catch(e) {
			window.frames['print_frame'].focus();
			window.frames['print_frame'].print();
}
}

function AjaxLoader()
{
	var xmlhttp, bComplete = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType("application/xml"); }  
	catch (e) { xmlhttp = false; }}}

  if (!xmlhttp) return null;

	this.IsComplete = function() {return bComplete;};

  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        if (sVars!='') xmlhttp.open(sMethod, sURL+"?"+sVars, true); else xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Content-Type", "application/xml");
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


/*________________________________________________________________________
 TREE VIEW CLASS
*/

function AjaxTree() {

	var VisibleBranches = new Array();
	var VisibleTabs = new Array();

	var MyLoader = new AjaxLoader();

	var LoadingHTML = '<div class="AjaxLoading">...Loading...</div>';
	
	this.IsComplete = function() {return MyLoader.IsComplete();};

	this.SwitchBranch = function(BranchID, LoadUrl, BranchRootID, OpenCssClass, CloseCssClass) {
//		alert(document.getElementById('scrollchecker').scrollTop + ',' + document.getElementById('loadcontent').scrollHeight);
//		document.getElementById('loadcontent').style.height = document.getElementById('loadcontent').offsetHeight;
		if (!VisibleBranches[BranchID] || document.getElementById(BranchID).style.display=='none') VisibleBranches[BranchID]=false;

		if (BranchRootID) {
			if (VisibleBranches[BranchID]==true) document.getElementById(BranchRootID).className=CloseCssClass; else document.getElementById(BranchRootID).className=OpenCssClass;
		}

		if (VisibleBranches[BranchID]==true) {
			if (document.getElementById(BranchID)) {
				document.getElementById(BranchID).style.display = 'none';
				document.getElementById(BranchID).innerHTML = '';
			} else alert('Element does not exist! : ' + BranchID);
		} else {
			//document.getElementById(BranchID).innerHTML = LoadingHTML;
			this.LoadBranch(BranchID, LoadUrl);
			document.getElementById(BranchID).style.display = 'block'; 
		}

		VisibleBranches[BranchID]=!VisibleBranches[BranchID];
	};


	this.ShowFormResults = function(oForm, LoadElementID, LoadUrl) {
		this.LoadBranch(LoadElementID, LoadUrl + '?' + this.getFormValues(oForm));
	};


	this.SwitchTab = function(LoadElementID, LoadUrl, DefaultTabID, TabID, OpenCssClass, CloseCssClass) {

		document.getElementById(DefaultTabID).className=CloseCssClass;
		if (VisibleTabs[DefaultTabID]) document.getElementById(VisibleTabs[DefaultTabID]).className=CloseCssClass;
		document.getElementById(TabID).className=OpenCssClass;

		VisibleTabs[DefaultTabID] = TabID;

		this.LoadBranch(LoadElementID, LoadUrl);

	};

	
	this.LoadBranchSeries = function(BranchID, LoadUrl, NextLoadBranchSeries) {
		this.LoadBranch = function() { 
			var MyCopyXml;
			if (NextLoadBranchSeries) {
				MyCopyXml = new CopyXmlSeries(BranchID, NextLoadBranchSeries);
			} else {
				MyCopyXml = new CopyXml(BranchID);
			}
			ShowLoading();
			MyLoader.connect(LoadUrl, 'GET', '', MyCopyXml.CopyToElement);
		}; 
		return this;
	}


	function CopyXmlSeries(MyBranchID, MyNextLoadBranchSeries) {
		this.CopyToElement = function(oXML) {
			try {
				document.getElementById(MyBranchID).innerHTML=oXML.responseText; 
				HideLoading();
			}
			catch (e) {
			  // IE fails unless we wrap the string in another element.
			  var wrappingDiv = document.createElement('div');
			  wrappingDiv.innerHTML = oXML.responseText;
			  document.getElementById(MyBranchID).appendChild(wrappingDiv);
			}
			MyNextLoadBranchSeries.LoadBranch();
		};
		return this;
	}

	this.LoadBranch = function(BranchID, LoadUrl, NextLoadBranchSeries) {
			var MyCopyXml;
			if (NextLoadBranchSeries) {
				MyCopyXml = new CopyXmlSeries(BranchID, NextLoadBranchSeries);
			} else {
				MyCopyXml = new CopyXml(BranchID);
			}
		ShowLoading();
		MyLoader.connect(LoadUrl, 'GET', '', MyCopyXml.CopyToElement);
	};

	function CopyXml(MyBranchID) {
		this.CopyToElement = function(oXML) {
			try {
				document.getElementById(MyBranchID).innerHTML=oXML.responseText; 
				HideLoading();
			}
			catch (e) {
			  // IE fails unless we wrap the string in another element.
			  var wrappingDiv = document.createElement('div');
			  wrappingDiv.innerHTML = oXML.responseText;
			  document.getElementById(MyBranchID).appendChild(wrappingDiv);
			}
		};
		return this;
	}

	function OnSubmitXmlForm(me, MyBranchID, MyLoadUrl, SuccessExecString, ErrorBranchID, ErrorExecString, SuccessRedirectUrl) {

		this.NSResolver = function(prefix) {
			if(prefix == 'html') {return 'http://www.w3.org/1999/xhtml'; }
			else if(prefix == 'webdata') {return 'http://xml.planning.org/namespace/webdata';}
			else if(prefix == 'dataviewrequest') {return 'http://xml.planning.org/namespace/dataviewrequest';}
			else  {return 'http://xml.planning.org/namespace/dataviewrequest';}
		}

		this.OnSubmit = function(oXML) {
			if (oXML) {
				if (oXML.responseXML) {

				var IsSuccess= false;

				try {
					if (oXML.responseXML.selectSingleNode("//*[@id='_ajax_response_success']")) {IsSuccess=true;}
				} catch(e) {
					try {
						if (oXML.responseXML.getElementById('_ajax_response_success')) {IsSuccess=true;}
					}  catch(e) {alert('There is an error.'); IsSuccess=false;}
				}

				//if (oXML.responseXML.evaluate("//*[@id='w_ajax_response_success']", document, null, XPathResult.ANY_TYPE, null)) {alert('hi!!');}

					if (IsSuccess) {
						if (MyBranchID) {
							me.LoadBranch(MyBranchID, MyLoadUrl);
							document.getElementById(MyBranchID).style.display='block';
						}
						if (SuccessExecString) {
							eval(SuccessExecString);
						}
						if (SuccessRedirectUrl) document.location.href=SuccessRedirectUrl;
					} else {
						try {
							if (oXML.responseXML.selectSingleNode("//*[@id='_ajax_response_error']") && document.getElementById(ErrorBranchID)) {
							  var wrappingDiv = document.createElement('div');
							  wrappingDiv.innerHTML = oXML.responseXML.selectSingleNode("//*[@id='_ajax_response_error']").xml;
							  document.getElementById(ErrorBranchID).innerHTML='';
							  document.getElementById(ErrorBranchID).appendChild(wrappingDiv);
							  document.getElementById(ErrorBranchID).style.display='block';
							} else alert('There was a problem submitting the form.');
						} catch(e) {
							if (oXML.responseXML.getElementById('_ajax_response_error') && document.getElementById(ErrorBranchID)) {
								document.getElementById(ErrorBranchID).innerHTML='';
								document.getElementById(ErrorBranchID).appendChild(document.importNode(oXML.responseXML.getElementById('_ajax_response_error'), true));
								document.getElementById(ErrorBranchID).style.display='block';
							} else alert('There was a problem submitting the form.');
						}
						if (ErrorExecString) {
							eval(ErrorExecString);
						}
					}
				} else alert('Error: not XML response');
			} else alert('Error: no XML object');
		};
		HideLoading();
		return this;
	}

	function OnSubmitXmlForm_SkipValidation(me, MyBranchID, MyLoadUrl, SuccessExecString, SuccessRedirectUrl) {
		this.OnSubmit = function(oXML) {
			if (oXML) {
				if (oXML.responseXML) {
					//alert(oXML.responseText);
					if (MyBranchID) me.LoadBranch(MyBranchID, MyLoadUrl);
					document.getElementById(MyBranchID).style.display='block';
					if (SuccessExecString!=null) {
						eval(SuccessExecString);
					}
					if (SuccessRedirectUrl) document.location.href=SuccessRedirectUrl;
				} else alert('not XML resposne!');
			} else alert('no XML object!');
		};
		HideLoading();
		return this;
	}


	this.submitForm = function(oForm, ActionUrl, SuccessBranchID, SuccessUrl, SuccessExecString, ErrorBranchID, ErrorExecString, SuccessRedirectUrl) {
		//alert(this.getFormValues(oForm));
		var MyOnSubmitXmlForm;
		//if (SuccessExecString==null) {
		//MyOnSubmitXmlForm = new OnSubmitXmlForm(this, SuccessBranchID, SuccessUrl, null);
		//		} else {
			ShowLoading();
			MyOnSubmitXmlForm = new OnSubmitXmlForm(this, SuccessBranchID, SuccessUrl, SuccessExecString, ErrorBranchID, ErrorExecString, SuccessRedirectUrl);
		//		}
		MyLoader.connect(ActionUrl, 'POST', this.getFormValues(oForm), MyOnSubmitXmlForm.OnSubmit);
		//alert(this.getFormValues(oForm));
		return false;
	};

	this.submitUrl = function(ActionUrl, SuccessBranchID, SuccessUrl, SuccessExecString, SkipSuccessValidation, ErrorBranchID, ErrorExecString, SuccessRedirectUrl) {
		//alert(this.getFormValues(oForm));
		ShowLoading();
		if (SkipSuccessValidation==null || SkipSuccessValidation==false) {
			var MyOnSubmitXmlForm = new OnSubmitXmlForm(this, SuccessBranchID, SuccessUrl, SuccessExecString, ErrorBranchID, ErrorExecString, SuccessRedirectUrl);
			MyLoader.connect(ActionUrl, 'GET', '', MyOnSubmitXmlForm.OnSubmit);
		} else {
			var MyOnSubmitXmlForm = new OnSubmitXmlForm_SkipValidation(this, SuccessBranchID, SuccessUrl, SuccessExecString, SuccessRedirectUrl);
			MyLoader.connect(ActionUrl, 'GET', '', MyOnSubmitXmlForm.OnSubmit);
		}
	};

	this.getFormValues = function(oForm) {

		var myDataBuilder = new FormDataBuilder();

		function FormDataBuilder() {
			var formValues = new Array();

			this.addNameValue = function(name, value) {
				if(value==null) value='';
				if (formValues[name]) formValues[name] = formValues[name] + ',' + value;
				else formValues[name] = value; 
			}

			this.addElementValue = function(oElement) {
				this.addNameValue(oElement.name, escape(oElement.value));
			};

			this.addElementOptionValue = function(oElement) {
				this.addNameValue(oElement.name, escape(oElement.options[oElement.selectedIndex].value));
			};

			this.addElementMultiValue = function(oElement) {
				for (var i = 0; i < oElement.options.length; i++) { 
					if (oElement.options[i].selected) this.addNameValue(oElement.name, escape(oElement.options[i].value));
				}
			};

			this.strOutput = function() {
				var myOutput = '';
				for (var i in formValues) {
					myOutput += i + '=' + formValues[i] + '&';
				}
				myOutput = myOutput.substr(0,(myOutput.length - 1));
				return myOutput;
			};

			return this;
		}		

		for (var i = 0; i < oForm.elements.length; i++) {
			var thisElement = oForm.elements[i];

			switch(thisElement.type)  {
				case 'text':
					myDataBuilder.addElementValue(thisElement);
					break;
				case 'hidden':
					myDataBuilder.addElementValue(thisElement);
					break;
				case 'password':
					myDataBuilder.addElementValue(thisElement);
					break;
				case 'textarea':
					myDataBuilder.addElementValue(thisElement);
					break;
				case 'radio':
					if (thisElement.checked) myDataBuilder.addElementValue(thisElement);
					break;
				case 'checkbox':
					if (thisElement.checked) myDataBuilder.addElementValue(thisElement);
					break;
               case 'select-one':
					myDataBuilder.addElementOptionValue(thisElement);
                    break;
               case 'select-multiple':
					myDataBuilder.addElementMultiValue(thisElement);
                    break;
           }

       }
		return myDataBuilder.strOutput();
    };

	return this;
}


function fadeGreen(ElementID, hex, attempt) { 

	if (attempt==null) attempt=1;

	if (document.getElementById(ElementID)) {
		if (hex==null) hex=256;

		//document.getElementById(ElementID).style.color = '#000000';

		if (hex>=0) {
			document.getElementById(ElementID).style.color = 'rgb('+hex+',200,'+hex+')';
			hex-=22;
			testimonial_fadein=true;
			setTimeout('fadeGreen("' + ElementID + '",' + hex + ')',5); 
		 } 
	} else {if (attempt<10) setTimeout('fadeGreen("' + ElementID + '", null, ' + (attempt + 1) + ');', 500);}
/*
	else if (hex>=200)  {
		hex-=11; 
		testimonial_fadein=false;
		setTimeout('fadetext()',8000); 
	} else if (testimonial_fadein==true)  {
		hex+=11; 
		document.getElementById('testimonial' + testimonial_counter).style.color= 'rgb('+hex+','+hex+','+hex+')';
		setTimeout('fadetext()',20); 
	} else {
		hex-=11; 
		document.getElementById('testimonial' + testimonial_counter).style.color= 'rgb('+hex+','+hex+','+hex+')';
		setTimeout('fadetext()',20); 
	}*/

}

function resizeIframe(frameid){
	var iframehide="yes";
	var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
	var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 ;//extra height in px to add to iframe in FireFox 1.0+ browsers

	var currentfr=document.getElementById(frameid);

	currentfr.height=0;
	
	if (currentfr && !window.opera){
		currentfr.style.display="block"
		if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
		currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
		else if (currentfr.document && currentfr.Document.body.scrollHeight) //ie5+ syntax
		currentfr.height = currentfr.contentWindow.document.body.scrollHeight;
//		if (currentfr.addEventListener)
//			currentfr.addEventListener("load", readjustIframe, false)
//		else if (currentfr.attachEvent){
//			currentfr.detachEvent("onload", readjustIframe) // Bug fix line
//			currentfr.attachEvent("onload", readjustIframe)
//		}
	}
}

function ShowLoading() {
	if (document.getElementById('_ajax_ShowLoading')) {
		document.getElementById('_ajax_ShowLoading').style.top=document.body.scrollTop+8;
		document.getElementById('_ajax_ShowLoading').style.visibility='visible';
	}

}

function HideLoading() {
	if (document.getElementById('_ajax_ShowLoading')) document.getElementById('_ajax_ShowLoading').style.visibility='hidden';
}

function readjustIframe(loadevt) {
var crossevt=(window.event)? event : loadevt
var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
if (iframeroot)
resizeIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url){
if (document.getElementById)
document.getElementById(iframeid).src=url
}

function resizeIFrameCaller() {
	var dyniframe=new Array()
	for (i=0; i<IFrameIDs.length; i++){
	if (document.getElementById)
		resizeIframe(IFrameIDs[i]);
		//reveal iframe for lower end browsers? (see var above):
		/* IS THIS NECESSARY...?? */
		if ((document.all || document.getElementById)){
			var tempobj=document.all? document.all[IFrameIDs[i]] : document.getElementById(IFrameIDs[i]);
			tempobj.style.display='block';
		}
	}
}

function resizeIFramesOnLoad(myIFrameIDs) {
	IFrameIDs=myIFrameIDs;
	if (window.addEventListener)
	window.addEventListener('load', resizeIFrameCaller, false)
	else if (window.attachEvent)
	window.attachEvent('onload', resizeIFrameCaller)
	else
	window.onload=resizeIFrameCaller;
}

function findPosition(obj) {	
	var curleft = curtop = 0;	
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;	
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}


