// JavaScript Document


// inArray = Function to check if a passed value is in an array
Array.prototype.inArray = function(value){
	// Returns true if the passed value is found in the
	// array. Returns false if it is not.
	var i;
	for (i=0; i < this.length; i++) {
		// use === to check for Matches. ie., identical (===),
		if (this[i] === value) {
			//alert(value+' found in '+this+' Array');
			return true;
		};
	};
	//alert(value+' not found in '+this+' Array');
	return false;
};
// getID = Returns the array id of the passed value
Array.prototype.getID = function(value){
	
	var i;
	for (i=0; i < this.length; i++) {
		// use === to check for Matches. ie., identical (===),
		if (this[i] === value) {
			//alert(value+' found in '+this+' Array');
			return i;
		};
	};
	//alert(value+' not found in '+this+' Array');
	return false;
};
//removeItem = Removes item (string or number) from an array
Array.prototype.removeItem = function(itemToRemove) {
	var j = 0;
	while (j < this.length) {
   	if (this[j] == itemToRemove) {
      	this.splice(j, 1);
      } else { 
			j++; 
		};
	};
   return this;
};
//removeId = removes item matching idToRemove from array
Array.prototype.removeId = function(idToRemove) {
	var j = 0;
	while (j < this.length) {
   	if (this[j] == this[idToRemove]) {
      	this.splice(j, 1);
      } else { 
			j++; 
		};
	};
   return this;
};

searchTagger = function(myTaggerName, mySearchForm, mySelect, myTagArea, mySearchIds, mySearchNames){
	this.myTaggerName = myTaggerName;
	this.mySearchForm = mySearchForm; //variable to store name of search form
	this.mySelect = mySelect; //variable to store reference to select menu
	this.myTagArea = myTagArea; // variable to store div id that holds the tags
	this.mySearchIds = mySearchIds; //hidden field to store selected Id's posted from form
	this.mySearchNames = mySearchNames; //hidden field to store selected names's posted from form
	this.myIdArray = new Array(); //array to hold selected id's
	this.myNameArray = new Array();  //array to hold selected names's	
};

searchTagger.prototype.makeTag = function(searchId){
	//alert('make Tag Function: id = '+searchId+' array value = '+this.myIdArray.getID(searchId));
	var currentRef = this.myIdArray.getID(searchId);
	var currentTagId = this.myIdArray[currentRef];
	var currentTagName = this.myNameArray[currentRef];
	//alert('makeTag: currentTagId: '+currentTagId+' currentTagName: '+currentTagName+' currentRef: '+currentRef);	
	var tagArea = document.getElementById(this.myTagArea);
	var newDiv = document.createElement('div');// create a new span	
	var spanName = (this.myNameArray[currentRef]+'_'+this.myIdArray[currentRef]);// set the name of the the new span (name_id)	
	
	newDiv.setAttribute('id',this.myIdArray[currentRef]);// set the id of the span to the new name
	newDiv.setAttribute('class','searchTag');// set the class of the span to choice
	// set the text for the span and add the remove handler to the onclick
	
	newDiv.innerHTML = '<a href=\'#\' onclick=\''+this.myTaggerName+'.removeTag("'+this.myNameArray[currentRef]+'", "'+this.myIdArray[currentRef]+'")\'>'+this.myNameArray[currentRef]+' <img src="/img/icons/remove_me.gif" width="16" height="16" align="absbottom" border="0" alt="remove '+this.myNameArray[currentRef]+'"/></a>';
	tagArea.appendChild(newDiv);// add the Div to the document	

};

searchTagger.prototype.addAll = function(searchSelect){
	for (var i=0; i<searchSelect.length; i++){
		//alert(searchSelect.options[i].text+" "+searchSelect.options[i].value)
		var myIndex = i;// get selected index of select	
		var myTagName = searchSelect.options[myIndex].text;// get selected name	
		var myTagId = searchSelect.options[myIndex].value;// get selected value
		//alert('ProcessClick: searchName = '+myTagName+'; searchId = '+myTagId+';');
		if(myTagId !== '*') {
			if (this.myIdArray.inArray(myTagId) == false) {
			//alert('ProcessClick: searchId '+myTagId+' not found in array. adding: '+myTagId+' to myIdArray');
				this.myIdArray.push(myTagId);
				addedID = true;
			}else{
				addedID =false;
			};
			if (this.myNameArray.inArray(myTagName) == false) {
				//alert('ProcessClick: searchName '+myTagName+' not found in array.');
				this.myNameArray.push(myTagName);
				addedName = true;
			}else{
				addedName = false;
				//return;
			};
			//alert('this.myIdArray array count = '+this.myIdArray.length);
			if(addedID == true && addedName == true){
				this.makeTag(myTagId);
				this.applyChanges();
			};
		};
	};
};

searchTagger.prototype.processClick = function(searchSelect){
	var myIndex = searchSelect.selectedIndex;// get selected index of select	
	var myTagName = searchSelect.options[myIndex].text;// get selected name	
	var myTagId = searchSelect.options[myIndex].value;// get selected value
	//alert('ProcessClick: searchName = '+myTagName+'; searchId = '+myTagId+';');
	if(myTagId !== '*') {
		if (this.myIdArray.inArray(myTagId) == false) {
		//alert('ProcessClick: searchId '+myTagId+' not found in array. adding: '+myTagId+' to myIdArray');
	
			this.myIdArray.push(myTagId);
		}else{
			return;
		};
		if (this.myNameArray.inArray(myTagName) == false) {
			//alert('ProcessClick: searchName '+myTagName+' not found in array.');
			this.myNameArray.push(myTagName);
			//alert('ProcessClick: searchId '+myTagId+' not found in array. adding: '+myTagName+' to myNameArray');
		}else{
			return;
		};
		//alert('this.myIdArray array count = '+this.myIdArray.length);
	
		this.makeTag(myTagId);
		this.applyChanges();
	};
};
searchTagger.prototype.removeAll = function(){
	for (var i = 0; i < this.myNameArray.length; i = 0){ 
   	//alert('current loop: '+i);
		//alert('array length: '+this.myNameArray.length)
		//alert('removing tag: '+this.myIdArray[i]+' - '+this.myNameArray[i]);
		this.removeTag(this.myNameArray[i], this.myIdArray[i]); 
	};
};

searchTagger.prototype.removeGroup = function(searchSelect){
	for (var i=0; i<searchSelect.length; i++){
   	var myIndex = i;// get selected index of select
		var myTagName = searchSelect.options[i].text;// get selected name	
		var myTagId = searchSelect.options[i].value;// get selected value
		//alert('myTagId: '+myTagId);
		if (this.myIdArray.inArray(myTagId) == true){
			if(myTagId !== '*') {
				this.removeTag(myTagName, myTagId); 
			};
		};
	};
};
searchTagger.prototype.removeTag = function(myTagName, myTagId){
	var myTagId = myTagId;
	var myTagName = myTagName;
	//alert('removeBoroughTag: searchName='+searchName+' searchId='+searchId);
	//alert('remove borough Tag: searchName='+searchName+' searchId ='+searchId+' borough id in array? '+myBoroughId.inArray(searchId)+' data in myBoroughId - '+myBoroughId);
	if (this.myIdArray.inArray(myTagId) == true){
		//alert(searchId+' is in the myBoroughId array');
		// remove item from array
		this.myIdArray.removeItem(myTagId);
	};
	if (this.myNameArray.inArray(myTagName) == true){
		//alert(searchName+' is in the myBoroughNames array');
		this.myNameArray.removeItem(myTagName);
	};
	var d = document.getElementById(this.myTagArea);
	//alert('my TagArea: '+d);
	var olddiv = document.getElementById(myTagId);
	//alert('olddiv: '+olddiv);
	d.removeChild(olddiv);	
	//alert('items in array '+myBoroughId.length+' values: '+myBoroughId);
	this.applyChanges();
};

searchTagger.prototype.init = function(){
	//alert('searchTagger initialised: mySearchForm = '+this.mySearchForm+' mySelect = '+this.mySelect+' myTagArea = '+this.myTagArea+' mySearchIds = '+this.mySearchIds+' mySearchNames = '+this.mySearchNames);
	// check to see if any choices have been made
	//alert(this.mySearchIds);
	var idBox = document.getElementById(this.mySearchIds);
	//alert(idBox.value);
	myString = idBox.value;
	if (myString == 'undefined'){
		myString = '';
	};
	if(myString != ''){
		this.myIdArray = myString.split(",");
	}else{
		return;
	};
	var nameBox = document.getElementById(this.mySearchNames);
	myString = nameBox.value;
	if(this.mySearchNames != ''){
		this.myNameArray = myString.split(",");
	}else{
		return;
	};	
	//loop through selected tags and make tags
	for(i=0; i < this.myIdArray.length; i++){
		this.makeTag(this.myIdArray[i]);
	};
	//alert('myIdArray length: '+ this.myIdArray.length+' contains IDs: '+this.myIdArray+' names: '+this.myNamesArray);
};

searchTagger.prototype.applyChanges = function(){
	//alert('no of Cats selected: '+myCatId.length);
	var myString ='';
	//alert('apply changes: array length = '+this.myIdArray.length);
	for(i=0; i < this.myIdArray.length; i++){
		if (i === 0){
			myString = this.myIdArray[i];
			//alert(i+' add myString: '+myString);
		};
		if (i > 0){
			myString = (myString+','+this.myIdArray[i]);
			//alert(i+' add myString: '+myString);
		};
	};
	//alert(this.mySearchIds);
	var idBox = document.getElementById(this.mySearchIds) 
	idBox.value	= myString;
	//alert(document.searchForm.catSet.value);
	for(i=0; i < this.myNameArray.length; i++){
		if (i === 0){
			myString = this.myNameArray[i];
			//alert(i+' add myString: '+myString);
		};
		if (i > 0){
			myString = (myString+','+this.myNameArray[i]);
			//alert(i+' add myString: '+myString);
		};
	};
	var nameBox = document.getElementById(this.mySearchNames) 
	nameBox.value	= myString;
};