/****************************************************************************************
Author:					Noah Bacon
Date Created:				2009.08.10
Last Modification Date:			2009.08.26
Company:					JAHL Datasystems
Site:						C&GG Beads
File name:					ToolBox.js

Overview:
	This file contains the client side code for handling the administrator's 
	ToolBox.  It uses AJAX for it's remote scripting calls.  If you don't know 
	AJAX go to www.w3schools.com and check it out.
****************************************************************************************/

function toolboxToggle() {
/***************************************************************************************************************
	toolboxToggle
	
	This toggles the session variable regarding the collapse state of the toolbox. The remote function 
	returns the value after the toggle.  The toolbox is then expanded or collapsed based on that value.
		
	parameters:
		void

	return:
		void
***************************************************************************************************************/
	var call = getXMLHttpRequest();// The request object to make the call
	call.open("GET","/_AJAX/ToolBox.asp?method=toolboxToggle",false);//standard synchronus ajax call
	call.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );//Defeats IE's aggressive caching.%(
	call.send(null);//Sends the request
	if (String(call.responseText) == "true") {//If the box needs to be collapse, we collapse it.
		document.getElementById("ToolBox_Body").style.display = "none";//Collapses the box
		document.getElementById("ToolBox_Collapse").innerHTML = "+";//Change the button icon to "+"
	} else {//If the box doesn't need to be collapsed, expand it.
		document.getElementById("ToolBox_Body").style.display = "block";//Expand the box
		document.getElementById("ToolBox_Collapse").innerHTML = "-";//Change the button icon to "-"
	}//The toolbox is now in it's propper expanded/collapsed state.
}

function toolboxHTML_LinkTo(select) {
/***************************************************************************************************************
	toolboxHTML_LinkTo
	
	Generates an HTML link to on of the pages on the site.  The result is dumped into the value attribute 
	of the element with id="ToolBox_Body_HTML_LinkTo" (idealy this will be a text box).  This is the 
	output element.
		
	parameters:
		select
			HTML DOM <select> object
			A select list that contains the pages to link to.  Each option in the select list will 
			have text and value.  The text will be used as the link text.  The value will be
			appended to "index.asp?method=" to be used as the target for the link. For example 
			<option value="faq">FAQ</option> would generate a link to the faq page (_pages/faq.asp).
			<a hreg="index.asp?method=faq">FAQ</a> is the link that would be generated.  You may 
			include a default option in the select list with a value of "choose".  When this option 
			is selected the output element is cleared.

	return:
		void
***************************************************************************************************************/
	var option = select.options[select.selectedIndex];//select is a DOM <select> object. option is the currently selected option.
	if (option.value == "choose") {//choose means they haven't chosen a page to link to.
		document.getElementById("ToolBox_Body_HTML_LinkTo").value="";//So we clear the output element.
	} else {//If they selected a page to link to
		var linkText = "<a href=\"index.asp?method="+option.value+"\">"+option.text+"</a>";//Generate an HTML link tag from the option they chose.
		document.getElementById("ToolBox_Body_HTML_LinkTo").value=linkText;//And dump it into the output element so they can copy it.
	}//The output element is now in it's proper state.
}

function toolboxHTML_imgTag(select) {
/***************************************************************************************************************
	toolboxHTML_imgTag
	
	Generates an HTML image tag to an image in the "_content/images/" directory.  The result is dumped into 
	the value attribute of the element with id="ToolBox_Body_HTML_imgTag" (idealy this will be a text box).
	This is the output element.
		
	parameters:
		select
			HTML DOM <select> object
			A select list that contains the pages to link to.  Each option in the select list will 
			have text and value.  The text will be used as the alternate text for the image.  The 
			value will be appended to "_content/images/" to be used as the source for the image. 
			For example the tag <option value="a.jpg">A Picture</option> would generate an image 
			tag for a.jpg in _content/images/ with "A Picture" as its alternate text. Ths would 
			look like <img src="_content/images/a.jpg" alt="A Picture">.  The select list may
			include a default option in the select list with a value of "choose".  When this option 
			is selected the output element is cleared.  The select list should include a refresh 
			option with a value of "refresh" if you want the user to check the directory and update 
			the select list. (Useful when the contents of the directory have changed.) The refresh 
			option does not affect the state of the output element.

	return:
		void
***************************************************************************************************************/
	var option = select.options[select.selectedIndex];//select is a DOM <select> object.  option is a DOM <option> of the currently selected option.
	if (option.value == "choose") {//Choose is the default option for the select list.
		document.getElementById("ToolBox_Body_HTML_imgTag").value="";//So we clear the output element.
	} else if (option.value == "refresh") {//Refresh means we look in the images folder (with an AJAX call) and get a new list of images.
		//select.innerHTML = "<<option value=\"choose\">Kerblah</option><option value=\"choose\">-- Choose an Image --</option><option value=\"refresh\">-- Refresh List --</option>"+getImagesOptionList();//gives the select list a new set of options based on the AJAX call.
		while(select.length > 2)
			select.remove(2);
		for (var names=getImagesList();names.length > 0;names.shift()) {
			option = document.createElement('option');
			option.text=names[0];
			option.value=names[0];
			select.add(option);
		}
	} else {//If they chose one of the images we'll generate a tag for it.
		var tagText = "<img src=\"_content/images/"+option.value+"\" alt=\""+option.text+"\">";//Generates and HTML image tag for the selected image.
		document.getElementById("ToolBox_Body_HTML_imgTag").value=tagText;//Dumps the tag into the output element.
	}//The output element and select list are now in their proper states.
}

function getImagesList() {
/***************************************************************************************************************
	getImagesList
	
	Gets a list of the images in the image folder. Returns it as an array.
	
	parameters:
		void

	return:
		array
		An array of the filenames (strings) of the images in (_content/images).
***************************************************************************************************************/
	var call = getXMLHttpRequest();//Creates the request object we will use to make the call.
	call.open("GET","/_AJAX/ToolBox.asp?method=getImagesList",false);//Standard synchronus AJAX call.
	call.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );//Defeats IE's aggressive caching.%(
	call.send(null);//Sends the request.
	return call.responseText.split(":");//The response is a ":" delimeted list.  The list is split into an array and returned.
}


