//**************Function Starts

function AddToValueList(ListBoxName,TextFieldName,HiddenTextBox){
//Adds a new item in the Listbox.
	var NewText = document.getElementById(TextFieldName).value;
	var ListOption = document.getElementById(ListBoxName);
	//First checks if the Text to assingn is not empty.
	if(trim(NewText) != ""){
		var ListLen = ListOption.options.length;
		//Checks to see if the Current No. of options is 
		//zero than the Index of the Item to add will be zero
		if(ListLen == 0){ListLen = 0}
		ListOption.options[ListLen] = new Option(NewText,NewText,false,false);//Adds a new Item to the List
		ListOption.options[ListLen].selected=true;
		UpdateHiddenText(ListBoxName,HiddenTextBox)//Updates the items if List as a # seperated text.
	}
}// End of the AddToList funtion

//**************Function Ends

//**************Function Starts

function AddToValueListcomma(ListBoxName,TextFieldName,HiddenTextBox){
//Adds a new item in the Listbox.
	var NewText = document.getElementById(TextFieldName).value;
	var ListOption = document.getElementById(ListBoxName);
	//First checks if the Text to assingn is not empty.
	if(trim(NewText) != ""){
		var ListLen = ListOption.options.length;
		//Checks to see if the Current No. of options is 
		//zero than the Index of the Item to add will be zero
		if(ListLen == 0){ListLen = 0}
		ListOption.options[ListLen] = new Option(NewText,NewText,false,false);//Adds a new Item to the List
		ListOption.options[ListLen].selected=true;
		UpdateHiddenTextcomma(ListBoxName,HiddenTextBox)//Updates the items if List as a # seperated text.
	}
}// End of the AddToList funtion

//**************Function Ends

//**************Function Starts







function AddToKeyValueList(ListBoxName,KeyTextFieldName,ValueTextFieldName,HiddenTextBox){
//Adds a new item in the Listbox.
	var NewKey = document.getElementById(KeyTextFieldName).value;		
	var NewText = document.getElementById(ValueTextFieldName).value;
	var ListOption = document.getElementById(ListBoxName);
	//First checks if the Text to assingn is not empty.
	if(trim(NewKey) != "" && trim(NewText) != ""){
		if(ListOption.options.length > 0){
			for(var i=0;i<ListOption.options.length;i++){
				var skey = ListOption.options[i].text;
				skey = skey.substring(0,skey.indexOf("->"));
				if(skey == NewKey){
					alert("The Key is already in Use.");
					return;
				}
			}
		}
		var ListLen = ListOption.options.length;
		var ItemText = NewKey + "->" + NewText;
		//Checks to see if the Current No. of options is 
		//zero than the Index of the Item to add will be zero
		if(ListLen == 0){ListLen = 0}
		ListOption.options[ListLen] = new Option(ItemText,ItemText,false,false);//Adds a new Item to the List
		ListOption.options[ListLen].selected=true;
		UpdateHiddenText(ListBoxName,HiddenTextBox)
	}
}// End of the AddToList funtion

//**************Function Ends

//**************Function Starts
function AddToKeyValueListNew(ListBoxName,KeyTextFieldName,ValueTextFieldName1,ValueTextFieldName2,ValueTextFieldName3,HiddenTextBox){
//Adds a new item in the Listbox.
	var NewKey = document.getElementById(KeyTextFieldName).value;
	if(document.getElementById(ValueTextFieldName1).value.length==0||document.getElementById(ValueTextFieldName2).value.length==0||document.getElementById(ValueTextFieldName3).value.length==0)
	{
	 alert("empty");
	 return;
	}
	var NewText1 ="entityid ="+ document.getElementById(ValueTextFieldName1).value;
    var NewText2 = "noofyears ="+document.getElementById(ValueTextFieldName2).value;
	var NewText3 = "expirydate ="+document.getElementById(ValueTextFieldName3).value;
    var NewText=NewText1+","+NewText2+","+NewText3;
	var ListOption = document.getElementById(ListBoxName);

	//First checks if the Text to assingn is not empty.
	if(trim(NewKey) != "" && trim(NewText) != ""){
		if(ListOption.options.length > 0){
			for(var i=0;i<ListOption.options.length;i++){
				var skey = ListOption.options[i].text;
				skey = skey.substring(0,skey.indexOf("->"));
				if(skey == NewKey){
					alert("The Key is already in Use.");
					return;
				}
			}
		}
		var ListLen = ListOption.options.length;
		var ItemText = NewKey + "->" + NewText;
		//Checks to see if the Current No. of options is 
		//zero than the Index of the Item to add will be zero
		if(ListLen == 0){ListLen = 0}
		ListOption.options[ListLen] = new Option(ItemText,ItemText,false,false);//Adds a new Item to the List
		alert(ItemText);
		ListOption.options[ListLen].selected=true;
		UpdateHiddenText(ListBoxName,HiddenTextBox)
	}
}

function AddToKeyOfKeyValueList(ListBoxName,KeyTextFieldName,ValueTextFieldNames,HiddenTextBox)
{
//Adds a new item in the Listbox.
//ValueTextFieldName1,ValueTextFieldName2,ValueTextFieldName3
	var NewKey = document.getElementById(KeyTextFieldName).value;

	var valueTextFieldArr = ValueTextFieldNames.split('|');
	var NewText = "";
	for(var cnt=0;cnt<valueTextFieldArr.length;cnt++)
	{				
		var keyValue = valueTextFieldArr[cnt].split('~');
		if(document.getElementById(keyValue[1]).value.length==0)
		{
		 alert("empty");
		 return;
		}
		NewText = NewText + keyValue[0]+"=" + document.getElementById(keyValue[1]).value;
		if(cnt < valueTextFieldArr.length-1)
			NewText = NewText +",";
	}
	if(NewText.length == 0)
	{
		alert("empty");
		return;
	}

	var ListOption = document.getElementById(ListBoxName);

	//First checks if the Text to assingn is not empty.
	if(trim(NewKey) != "" && trim(NewText) != "")
	{
		if(ListOption.options.length > 0)
		{
			for(var i=0;i<ListOption.options.length;i++)
			{
				var skey = ListOption.options[i].text;
				skey = skey.substring(0,skey.indexOf("->"));
				if(skey == NewKey){
					alert("The Key is already in Use.");
					return;
				}
			}
		}
		var ListLen = ListOption.options.length;
		var ItemText = NewKey + "->" + NewText;
		//Checks to see if the Current No. of options is 
		//zero than the Index of the Item to add will be zero
		if(ListLen == 0){ListLen = 0}
		ListOption.options[ListLen] = new Option(ItemText,ItemText,false,false);//Adds a new Item to the List
		alert(ItemText);
		ListOption.options[ListLen].selected=true;
		UpdateHiddenText(ListBoxName,HiddenTextBox)
	}
}

function AddToKeyValueListNewZone(ListBoxName,KeyTextFieldName,ValueTextFieldName1,ValueTextFieldName2,ValueTextFieldName3,HiddenTextBox){
//Adds a new item in the Listbox.
	var NewKey = document.getElementById(KeyTextFieldName).value;
	if(document.getElementById(ValueTextFieldName1).value.length==0||document.getElementById(ValueTextFieldName2).value.length==0||document.getElementById(ValueTextFieldName3).value.length==0)
	{
	 alert("empty");
	 return;
	}
	var NewText1 ="domainname ="+ document.getElementById(ValueTextFieldName1).value;
    var NewText2 = "noofyears ="+document.getElementById(ValueTextFieldName2).value;
	var NewText3 = "noofrecords ="+document.getElementById(ValueTextFieldName3).value;
    var NewText=NewText1+","+NewText2+","+NewText3;
	var ListOption = document.getElementById(ListBoxName);

	//First checks if the Text to assingn is not empty.
	if(trim(NewKey) != "" && trim(NewText) != ""){
		if(ListOption.options.length > 0){
			for(var i=0;i<ListOption.options.length;i++){
				var skey = ListOption.options[i].text;
				skey = skey.substring(0,skey.indexOf("->"));
				if(skey == NewKey){
					alert("The Key is already in Use.");
					return;
				}
			}
		}
		var ListLen = ListOption.options.length;
		var ItemText = NewKey + "->" + NewText;
		//Checks to see if the Current No. of options is 
		//zero than the Index of the Item to add will be zero
		if(ListLen == 0){ListLen = 0}
		ListOption.options[ListLen] = new Option(ItemText,ItemText,false,false);//Adds a new Item to the List
		alert(ItemText);
		ListOption.options[ListLen].selected=true;
		UpdateHiddenText(ListBoxName,HiddenTextBox)
	}
}




function AddToKeyValueListcomma(ListBoxName,KeyTextFieldName,ValueTextFieldName,HiddenTextBox){
//Adds a new item in the Listbox.
	var NewKey = document.getElementById(KeyTextFieldName).value;		
	var NewText = document.getElementById(ValueTextFieldName).value;
	var ListOption = document.getElementById(ListBoxName);
	//First checks if the Text to assingn is not empty.
	if(trim(NewKey) != "" && trim(NewText) != ""){
		if(ListOption.options.length > 0){
			for(var i=0;i<ListOption.options.length;i++){
				var skey = ListOption.options[i].text;
				skey = skey.substring(0,skey.indexOf("->"));
				if(skey == NewKey){
					alert("The Key is already in Use.");
					return;
				}
			}
		}
		var ListLen = ListOption.options.length;
		var ItemText = NewKey + "->" + NewText;
		//Checks to see if the Current No. of options is 
		//zero than the Index of the Item to add will be zero
		if(ListLen == 0){ListLen = 0}
		ListOption.options[ListLen] = new Option(ItemText,ItemText,false,false);//Adds a new Item to the List
		ListOption.options[ListLen].selected=true;
		UpdateHiddenTextcomma(ListBoxName,HiddenTextBox)
	}
}

function RemoveFromList(ListBoxName,HiddenTextBox){
	//Removes a Item from the ListBox
	var ListOption = document.getElementById(ListBoxName);
	var ListLen = ListOption.selectedIndex;
	//Checks to see if the a Item is actually selected to Remove
	if(ListLen >= 0){
		ListOption.options[ListLen] = null;//Removes the Selected Item
		ListOption.selectedIndex = -1;//Sets the current selection to None in the Listbox
		UpdateHiddenText(ListBoxName,HiddenTextBox)//Updates the items if List as a # seperated text.
	}
}// End of the RemoveFromList funtion

//**************Function Ends

//**************Function Starts
function RemoveFromListcomma(ListBoxName,HiddenTextBox){
	//Removes a Item from the ListBox
	var ListOption = document.getElementById(ListBoxName);
	var ListLen = ListOption.selectedIndex;
	//Checks to see if the a Item is actually selected to Remove
	if(ListLen >= 0){
		ListOption.options[ListLen] = null;//Removes the Selected Item
		ListOption.selectedIndex = -1;//Sets the current selection to None in the Listbox
		UpdateHiddenTextcomma(ListBoxName,HiddenTextBox)//Updates the items if List as a # seperated text.
	}
}

function removecomma(HiddenTextBox)
{
 // var HiddenBox = document.getElementById(HiddenTextBox)
 //    if()

}	
function UpdateHiddenText(ListBoxName,HiddenTextBox){
	//The purpose of this function is to update the 
	//a hiddent text field with a '#' seperated representation
	//of the Listbox items, which is used to access at Server side.
	var ListBox = document.getElementById(ListBoxName)
	var HiddenBox = document.getElementById(HiddenTextBox)
	if(ListBox.options.length > 0){
		HiddenBox.value = "";
		for(var i=0;i<ListBox.options.length;i++){
			HiddenBox.value = HiddenBox.value + ListBox.options[i].text + "#";
		}
	}
}
function UpdateHiddenTextcomma(ListBoxName,HiddenTextBox){
	//The purpose of this function is to update the 
	//a hiddent text field with a '#' seperated representation
	//of the Listbox items, which is used to access at Server side.
	var ListBox = document.getElementById(ListBoxName)
	var HiddenBox = document.getElementById(HiddenTextBox)
	if(ListBox.options.length > 0){
		HiddenBox.value = "";
		for(var i=0;i<ListBox.options.length;i++){
			HiddenBox.value = HiddenBox.value + ListBox.options[i].text + ",";
		}
	}
}

//**************Function Ends

//**************Function Starts

function trim(inputString) {
// Removes leading and trailing spaces from the passed string. Also removes
// consecutive spaces and replaces it with one space. If something besides
// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

//**************Function Ends

