﻿//Copyright WebMail Wou3, Inc. 2007-2010
Type.registerNamespace('WebMail2');
WebMail2.ListBase=function
(
	ListDivID,
	SiblingValueControlIDs,
	SelectedValuesControlID,
	UnSelectedColorBack,
	SelectedColorBack,
	SelectedValueAttribute,
	CheckBoxControlID,
	ListTabIndex,
	ToolbarObject
) 
{
	this._ListDiv=document.getElementById(ListDivID);
	this._SiblingValueControl=document.getElementById(SiblingValueControlIDs);
	this._SelectedValuesControl=document.getElementById(SelectedValuesControlID);
	
	this._SelectedColorBack=SelectedColorBack;
	this._UnSelectedColorBack=UnSelectedColorBack;
	this._SelectedValueAttribute=SelectedValueAttribute;
	//To keep the selected row (highlighted) and checkbox in synch, LinkControlIDSearch will store the portion of 
	//the anchor's uniquID to replace with the value stored in the CheckBoxIDReplace.
	//LinkControlIDSearch should always equal the LinkButton's ID as defined in the HTML in the Repeater
	//CheckBoxIDReplace should always equal the checkBox's ID as defined in the HTML in the Repeater
	this._CheckBoxControlID=CheckBoxControlID;
	this._ListTabIndex=ListTabIndex;
	this._ToolbarObject=ToolbarObject;

	this._LastSelected=null;
	
	this._ImgUp='/Images/icon_arrow_up.gif';
	this._ImgDown='/Images/icon_arrow_dn.gif';
}
WebMail2.ListBase.prototype=
{

getSelectedItemsArray:function()
{
	var anchorIds=this._SelectedValuesControl.value.split(FORMAT_ARRAY_ITEM_SEPERATOR);
	return anchorIds;
},

getLastSelected:function()
{
	var anchorIds=this.getSelectedItemsArray();
	
	//if the last selected is not null, make sure it is still in the list
	if(this._LastSelected!=null)
	{
		//if the lastSelected.id is not in the list of selected objects, set it to null
		if(Array.indexOf(anchorIds, this._LastSelected.id)<0)
			this._LastSelected=null;
	}
	
	if(this._LastSelected==null)
	{
		for(var i=anchorIds.length-1; i>=0; i--)
		{
			var objSelected=null;
			try{objSelected=document.getElementById(anchorIds[i]);}
			catch(ex){}
			
			if(objSelected!=null)
			{
				this._LastSelected=objSelected;
				break;
			}
			else
			{
				Array.removeAt(anchorIds,i);
				this._SelectedValuesControl.value=anchorIds.join(FORMAT_ARRAY_ITEM_SEPERATOR);
			}
		}
	}
	
	return this._LastSelected;
},

//checks the list of selected values and updateds the selected values list
//if the objects currently in the seleted values list are not found, the are removed from the list
//Also check the _lastSelected object.  if it is not found, _LastSelected is set to null
validateSelectedList:function()
{
	var anchorIds=this.getSelectedItemsArray();

	for(var i=(anchorIds.length-1); i>=0; i--)
	{
		if(anchorIds[i].length>0)
		{
			var objSelected=document.getElementById(anchorIds[i]);
			if(objSelected!=null)
			{
				var divMain=document.getElementById(objSelected.getAttribute(ATT_LINE_DIV_ID));
				if(divMain!=null)
				{
					//if the className==the selectedColorBack add the item's id to the SelectedValues hidden control
					if(divMain.className!=this._SelectedColorBack)
						divMain.className=this._SelectedColorBack;
					divMain=null;
				}
			}
			else
				Array.removeAt(anchorIds, i);
		}
	}
	
	//if the last selected is not null, make sure it is still in the list
	if(this._LastSelected!=null)
	{
		//if the lastSelected.id is not in the list of selected objects, set it to the 
		//first item in the array.
		if(Array.indexOf(anchorIds, this._LastSelected.id)<0)
		{
			if(anchorIds.length>0)
				this._LastSelected=document.getElementById(anchorIds[0]);
			else
				this._LastSelected=null;
		}
	}
	
	this._SelectedValuesControl.value=anchorIds.join(FORMAT_ARRAY_ITEM_SEPERATOR);
	return anchorIds;
},

//returns the objects that are currently selected
//if clearSelected==true, change all selected items to their original color
//if clearSelected==false, keep all selected items in their selected state
//if hideSelections==true, the divs will have their hidden value set to none
getSelectedValues:function(clearSelected, hideSelections, seperatorCharacter)
{
	var anchorIds=this.getSelectedItemsArray();
	var divMain;
	strSelectedValues="";
	for(var i=(anchorIds.length-1); i>=0; i--)
	{
		if(anchorIds[i].length>0)
		{
			var objSelected=document.getElementById(anchorIds[i]);
			if(objSelected!=null)
			{
				divMain=document.getElementById(objSelected.getAttribute(ATT_LINE_DIV_ID));
				//if the className==the selectedColorBack add the item's id to the SelectedValues hidden control
				if(divMain.className==this._SelectedColorBack)
				{
					strSelectedValues=strSelectedValues.concat(seperatorCharacter).concat(objSelected.getAttribute(this._SelectedValueAttribute));
					if(hideSelections)
						divMain.style.display='none';
				}
				//set the div objects in the anchor
				if(clearSelected)
					this.setSelected(objSelected, false);
			}
		}
	}
	if(strSelectedValues.length>0)
		strSelectedValues=strSelectedValues.substring(seperatorCharacter.length);
		
	return strSelectedValues;
},
//////////////////////////////////////////////////////////////////////////////////////
//gets the selected values in an array
//////////////////////////////////////////////////////////////////////////////////////
getSelectedValuesArray:function(clearSelected, hideSelections, seperatorCharacter)
{
	var selectedValues=this.getSelectedValues(clearSelected, hideSelections, seperatorCharacter);

	if(selectedValues.length>0)
	{
		selectedValues=selectedValues.split(FORMAT_ARRAY_ITEM_SEPERATOR);
		if((selectedValues.length>0) && (selectedValues[0].length==0))
			Array.removeAt(selectedValues,0);
		if((selectedValues.length>0) && (selectedValues[selectedValues.length-1].length==0))
			Array.removeAt(selectedValues,selectedValues.length-1);
		return selectedValues;
	}
	else
		return new Array();
},
//////////////////////////////////////////////////////////////////////////////////////
//after updating the list from a callback gets the current selected items
//and sets each item as selected if it is still in the current list of selected
//items
//The _SiblingValueControl and the MessageList 
//must be populated before this method is called.
//////////////////////////////////////////////////////////////////////////////////////
updateSelectedValuesPostCallback:function(selectedValues, siblingAnchorIDs)
{
	this._SelectedValuesControl.value="";
	
	if((selectedValues.length>0) && (siblingAnchorIDs.length>0))
	{
		var lastSelected=this.getLastSelected();
		
		var selectedValuesArray=selectedValues.split(FORMAT_ARRAY_ITEM_SEPERATOR);
		var siblingAnchorIDsArray=siblingAnchorIDs.split(FORMAT_ARRAY_ITEM_SEPERATOR);
		
		for(var i=0; i<siblingAnchorIDsArray.length; i++)
		{
			var objListItem=document.getElementById(siblingAnchorIDsArray[i]);
			
			if(objListItem!=null)
			{
				//gets the line's actual value (contactID, MessageID, etc.
				//The actual value is always stored in the SelectedValue attribute
				var objValue=objListItem.getAttribute(this._SelectedValueAttribute);

				//checks the selectedValues array to the value stored in the SelectedValue Attribute
				//if it is found, set the item as selected
				if(Array.indexOf(selectedValuesArray, objValue, 0)>=0)
					this.setSelected(objListItem, true);
			}
		}
		
		//reset LastSelected if there was a valid value when before resetting the selected items.
		if(lastSelected!=null)
		{
			var anchorIds=this.getSelectedItemsArray();
		
			//if the lastSelected.id is not in the list of selected objects, set it to null
			if(Array.indexOf(anchorIds, lastSelected.id)>=0)
				this._LastSelected=null;
			
			this._LastSelected=lastSelected;
		}
	}
},

selectAll:function(markAsASelected)
{
	setCheckBoxValue(this._CheckBoxControlID,markAsASelected);
	var anchorIds=this._SiblingValueControl.value.split(FORMAT_ARRAY_ITEM_SEPERATOR);
	
	for(var i=0; i<anchorIds.length; i++)
	{
		if(anchorIds[i].length>0)
		{
			var objSelected=document.getElementById(anchorIds[i]);
			
			if(objSelected!=null)
			{
				if(true==markAsASelected)
					this.setSelected(objSelected, true);
				else
					this.setSelected(objSelected, false);
			}
		}
	}
},

//finds the control whose name is stored in this._SelectedValuesControlID, and stores
//the current list of selections in the hidden control
//strSelectedObjectID is a new values to be stored as selected
addSelectedID:function(strSelectedObjectID)
{
	var anchorIds=this.getSelectedItemsArray();
	if(Array.contains(anchorIds, strSelectedObjectID)==false)
	{
		Array.add(anchorIds, strSelectedObjectID);
		this._SelectedValuesControl.value=anchorIds.join(FORMAT_ARRAY_ITEM_SEPERATOR);
		return true;
	}
	return false;
},
removeSelectedID:function(strSelectedObjectID)
{
	var anchorIds=this.getSelectedItemsArray();
	var index=Array.indexOf(anchorIds,strSelectedObjectID);
	if(index>=0)
	{
		Array.removeAt(anchorIds, index);
		this._SelectedValuesControl.value=anchorIds.join(FORMAT_ARRAY_ITEM_SEPERATOR);
		return true;
	}
	return false;
},

//Gets the value of the the currently selected item and clears all other items
// function name was ChangeChildColorsDoubleClick
//objSelected is the HTML item associated with this method.
//This should only select the objSelected and no other items.  If any others are
//still highlighted, they should not be any longer.
selectCurrentOnly:function(objSelected)
{
	this.getSelectedValues(true, false, FORMAT_ARRAY_ITEM_SEPERATOR)
	this.setSelected(objSelected, true);
	//setting the last parameter to null means all nextItems and previousItems are selected.

	return objSelected.getAttribute(this._SelectedValueAttribute);
},

//deslects all previous items (items above the currently selected item) 
//count is the number of additional next items to select or deselct. 
//if count is null or <=0 no additional items will be selected
setPreviousItems:function(objSelected, select, count)
{
	if(objSelected==null)
		return;
	var id;
	//search the hdnSiblingAnchorIds object to find the prev id
	if(id.length>0)
	{
		var sibling=document.getElementById(id);
		if(sibling!=null)
		{
			this.setSelected(sibling, true==select);
			if(count==null) 
				this.setPreviousItems(sibling,select, null);
			else if (Number(count)>=1)
				this.setPreviousItems(sibling,select, Number(count)-1);
		}
	}
},

//this method exclusivly selects the nth item above the current item.  
//if index is 10 and the first item in the list is currently selected, the 11th item would the ben selected
setPreviousXthItem:function(objSelected, index)
{
	if((objSelected==null) || (Number(index)<=0))
		return;
	
	var id;
	//search the hdnSiblingAnchorIds object to find the prev index'th id
	var sibling=null;
	if(id.length>0)
		sibling=document.getElementById(id);
	 else
		this.selectCurrentOnly(objSelected);
		
	if(sibling!=null)
	{
		if(Number(index)==1)
		{
			this.selectCurrentOnly(sibling);
		}
		else
			this.setPreviousXthItem(sibling, Number(index)- 1);
	}
},

//slects all of the next items (items below the currently selected item) 
//count is the number of additional next items to select or deselct. 
//if count is null or <=0 no additional items will be selected
setNextItems:function(objSelected, select, count)
{
	if(objSelected==null)
		return;
	var id;
	//search the hdnSiblingAnchorIds object to find the next id
	if(id.length>0)
	{
		var sibling=document.getElementById(id);
		if(sibling!=null)
		{
			this.setSelected(sibling, true==select);

			if(count==null) 
				this.setNextItems(sibling,select, null);
			else if (Number(count)>=1)
				this.setNextItems(sibling,select, Number(count)-1);
		}
	}
},

//this method exclusivly selects the nth item below the current item.  
//if index is 10 and the first item in the list is currently selected, the 11th item would the ben selected
setNextXthItem:function(objSelected, index)
{
	if((objSelected==null) || (Number(index)<=0))
		return;
	
	var id;
	//search the hdnSiblingAnchorIds object to find the next index ids
	var sibling=null;
	if(id.length>0)
		sibling=document.getElementById(id);
	else
		this.selectCurrentOnly(objSelected);
		
	if(sibling!=null)
	{
		if(Number(index)==1)
		{
			this.selectCurrentOnly(sibling);
		}
		else
			this.setNextXthItem(sibling, Number(index)- 1);

	}
},
toggleSelectedControl:function(objSelected)
{
	if (document.getElementById(objSelected.getAttribute(ATT_LINE_DIV_ID)).className==this._SelectedColorBack)
		this.setSelected(objSelected, false);
	else
		this.setSelected(objSelected, true);
	
	return false;
},
 
click:function(linkObj, evt)
{
	hideListContextMenu();
	hideCalendarContextMenu();

	try{page.resetLastClicked();}catch(e){}
	
	setCheckBoxValue(this._CheckBoxControlID, false);
	if (evt!=null) 
	{
		if(((evt.ctrlKey) && (!g_IsSafariBrowser)) || ((evt.altKey) && (g_IsSafariBrowser)))
		{
			this.toggleSelectedControl(linkObj);
		}
		else
		{
			this.selectCurrentOnly(linkObj);
		}
	}
	else
	{
		this.selectCurrentOnly(linkObj);
	}
	if(evt!=null)
	{
		evt.cancelBubble=true;
		evt.returnValue=false;
	}
},

checkBoxClick:function(chkBox,linkID)
{
	try{page.resetLastClicked();}catch(e){}
	
	var link=document.getElementById(linkID);
	var div=document.getElementById(link.getAttribute(ATT_LINE_DIV_ID));
	//un check the box if the div can not be found
	if(div==null)
	{
		chkBox.checked=!chkBox.checked;
		return;
	}
	if(chkBox.checked==true)
	{
		this.addSelectedID(linkID);
		this.setSelected(link,true);
		div.className=this._SelectedColorBack;
	}
	else
	{
		this.removeSelectedID(linkID);
		div.className=this._UnSelectedColorBack;
		setCheckBoxValue(this._CheckBoxControlID, false);
	}
},

//uses selected object which should always be a LinkButton or checkbox.
//Then uses the unique portion of the selected LinkButton.ID and replaces it with the unique portion of the CheckBox.ID
//The result should be the unique id of the correllating CheckBox in the Selected Row or Selected LinkButton
setSelected:function(objSelected, IsSelected)
{
	if(objSelected==null)
		return;
	
	var objDiv=document.getElementById(objSelected.getAttribute(ATT_LINE_DIV_ID));
	if(objDiv==null)
		return;
		
	var chkBox=document.getElementById(objSelected.getAttribute(ATT_LINE_CHECKBOX_ID));
	if(chkBox!=null)
		chkBox.checked=IsSelected;
	
	if(IsSelected)
	{
		objDiv.className=this._SelectedColorBack;
		this._LastSelected=objSelected;
		this.addSelectedID(objSelected.id);
	}
	else
	{
		objDiv.className=this._UnSelectedColorBack;
		if((this._LastSelected!=null) && (this._LastSelected.id==objSelected.id))
			this._LastSelected=null;
		this.removeSelectedID(objSelected.id);
	}
},

onKeyUpCapture:function(evt)
{
	/*
	enter=13
	down=40
	up=38
	pageup=33
	pagedown=34
	*/
	if(page.getSelectedTabIndex()==this._ListTabIndex)
	{
		switch(evt.keyCode)
		{
			case 46:
				//del=46
				if((!evt.altKey) && (!evt.shiftKey) && (!evt.ctrlKey))
					this.deleteItems();
				break;
			
			case 13:
				//enter=13
				if((!evt.altKey) && (!evt.shiftKey) && (!evt.ctrlKey))
					this.view();
				break;
			
			/*
			case 65:
				//A=65
				if(evt.altKey)
				{
					this.selectAll(true);
				}
			
			case 40:
				//down=40
				if(this._LastSelected==null)
				{
						this.selectFirstItem();
						return;
				}
				if (evt.shiftKey)
				{
					this.setNextItems(this._LastSelected, true, 1);
				}
				else
				{
					this.setNextXthItem (this._LastSelected, 1);
				}
				break;
			
			case 38:
				//up=38
				if(this._LastSelected==null)
				{
						this.selectFirstItem();
						return;
				}
				if (evt.shiftKey)
					this.setPreviousItems(this._LastSelected, true, 1);
				else
					this.setPreviousXthItem (this._LastSelected, 1);
				break;
			
			case 33:
				//pageup=33
				if(this._LastSelected==null)
				{
						this.selectFirstItem();
						return;
				}
				if (evt.shiftKey)
				{
					this.setPreviousItems(this._LastSelected, true, 10);
				}
				else
					this.setPreviousXthItem (this._LastSelected, 10);
				break;
			
			case 34:
				//pagedown=34
				if(this._LastSelected==null)
				{
						this.selectFirstItem();
						return;
				}
				if (evt.shiftKey)
					this.setNextItems(this._LastSelected, true, 10);
				else
					this.setNextXthItem (this._LastSelected, 10);
				break;
			*/
		}
		evt.returnValue=false;
		evt.cancelBubble=true;
	}
},
set_SibilingControls:function(ControlIDs)
{
	this._SiblingValueControl.value=ControlIDs;
},
handleMenuClick:function(source) 
{
	//handleMenuClick is used by the context popup menu.
	hideListContextMenu();
	switch(source.id)
	{
		case 'listContextMenu1':
			this.selectAll(true);
			break;
		case 'listContextMenu2':
			this.selectAll(false);
			break;
		case 'listContextMenu4':
			page.Nav.GoNextPage(1);
			break;
		case 'listContextMenu3':
			page.Nav.GoPrevPage(1);
			break;
	}
	
}

}
WebMail2.ListBase.registerClass("WebMail2.ListBase");

