// JavaScript Document
function Gallery()
{
	//variables
	var self = this;
	var categoryPath;
	var galleryPath;
	var xmlhttp;
	var xmlhttp2;
	
	var chkArray = new Array();
	var selectedArray = new Array();
	
	//this function fires when the page is loaded
	Gallery.prototype.populateGallery = function(){
		self.categoryPath = document.getElementById('categoryDiv');
		self.buildCategoryList();		
	}
	
	Gallery.prototype.buildCategoryList = function()
	{
		xmlhttp = this.getXmlHttpObject();
		if (xmlhttp==null)
  		{
  			alert ("Browser does not support HTTP Request");
  			return;
  		}
		var url="scripts/GetCategories.php";
		url=url+"?sid="+Math.random();
		xmlhttp.onreadystatechange=this.stateChanged;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);		
	}
	
	Gallery.prototype.stateChanged = function()
	{
		if (xmlhttp.readyState==4)
		{
			//BF had to add the code below because of an AJAX asynchonous IE error ('The data necessary to complete this operation is not yet available.")
			//I saved the link in my Javascript favorites
			if (typeof(xmlhttp.responseText) == "unknown")
			{
				self.categoryPath.innerHTML = "";
			}
			else
			{
				self.categoryPath.innerHTML = xmlhttp.responseText;
				self.checkClick();
			}							
		}
	}
	
	Gallery.prototype.getXmlHttpObject = function()
	{
		if (window.XMLHttpRequest)
  		{
  			// code for IE7+, Firefox, Chrome, Opera, Safari
  			return new XMLHttpRequest();
 		 }
		if (window.ActiveXObject)
  		{
  			// code for IE6, IE5
  			return new ActiveXObject("Microsoft.XMLHTTP");
  		}
		return null;
	}
	
	Gallery.prototype.checkClick = function()
	{
		chkArray = [];	
		selectedArray = [];
		
		var collection = document.getElementsByName('display');
		var i;
		var valueChosen;
		for (i = 0; i < collection.length; i++)
		{
			if(collection[i].checked)
			{
				valueChosen = collection[i].value;
				break;
			}
		}
		
		//this works because right now checkboxes are only input on page
		chkArray = document.getElementsByTagName('input');
		
		for(i = 0; i < chkArray.length; i++)
		{
			//ignore the values for the radio buttons
			if (chkArray[i].value != 'filter' && chkArray[i].value != 'all')
			{
				if(valueChosen == 'all')
				{
					selectedArray.push(chkArray[i].value);
				}
				else
				{
					if (chkArray[i].checked)
					{
						selectedArray.push(chkArray[i].value);
					}	
				}	
			}							
		}
		
		if(valueChosen == 'all')
		{
			for(i = 0; i < chkArray.length; i++)
			{
				if (chkArray[i].checked)
				{
					chkArray[i].checked = false;
				}	
			}
			
			document.getElementById("radAll").checked = 'checked';
		}
		else
		{
			if(selectedArray.length < 1)
			{
				alert('Please select the categories you would like to view.');
				return;
			}
		}
		
		
		self.displaySelectedList();
	}
	
	Gallery.prototype.displaySelectedList = function()
	{
		//build string to add to URL for list out of selected array
		var strList = "";
		
		for (i = 0; i < selectedArray.length; i++)
		{
			if (i == selectedArray.length - 1)
			{
				strList = strList + selectedArray[i];
			}
			else
			{
				strList = strList + selectedArray[i] + "_";
			}
		}
		
		xmlhttp2 = this.getXmlHttpObject();
		if (xmlhttp2==null)
  		{
  			alert ("Browser does not support HTTP Request");
  			return;
  		}
		var url="scripts/PopulateDisplay.php";
		url=url+"?list=" + strList;
		url=url+"&sid="+Math.random();
		
		xmlhttp2.onreadystatechange=this.stateChanged2;
		xmlhttp2.open("GET",url,true);
		xmlhttp2.send(null);		
	}
	
	Gallery.prototype.stateChanged2 = function()
	{
		if (xmlhttp.readyState==4)
		{
			self.galleryPath = document.getElementById('galleryDiv');	
			//BF had to add the code below because of an AJAX asynchonous IE error ('The data necessary to complete this operation is not yet available.")
			//I saved the link in my Javascript favorites
			if (typeof(xmlhttp2.responseText) == "unknown")
			{
				self.galleryPath.innerHTML = "";
			}
			else
			{
				self.galleryPath.innerHTML = xmlhttp2.responseText;	
			}						
		}
	}
	
	Gallery.prototype.checkSelected = function()
	{
		chkArray = [];	
		isChecked = false;
		
		//this works because right now checkboxes are only input on page
		chkArray = document.getElementsByTagName('input');
		
		for(i = 0; i < chkArray.length; i++)
		{
			if(chkArray[i].type == "checkbox")
			{
				if (chkArray[i].checked)
				{
					isChecked = true;
				}	
			}					
		}
		
		if(isChecked)
		{
			document.getElementById("radFilter").checked = "checked";
		}
		else
		{
			document.getElementById("radAll").checked = "checked";	
		}
	}
	
	Gallery.prototype.allClicked = function()
	{
		if(document.getElementById("radAll").checked)
		{
			chkArray = [];	
				
			chkArray = document.getElementsByTagName('input');
		
			for(i = 0; i < chkArray.length; i++)
			{
				if(chkArray[i].type == "checkbox")
				{
					chkArray[i].checked = false;
				}					
			}
		}	
		
	}
}
