//FUNCTIONS FOR ADDRESS BOOK CHECKBOXES, ETC.
function checkallboxes() {
	if (document.ContactForm.all.checked) {
		xxx = true;
	} else { 
		xxx = false; 
	}
	for (i=0; i < document.forms["ContactForm"].length; i++) {
		document.forms["ContactForm"].elements[i].checked = xxx;
	}
}


function contactName(Number) {
	document.EditContact.contactID.value = Number;
	document.EditContact.submit();
}


function groupName(Number, Name) {
	document.EditGroup.groupID.value = Number;
	document.EditGroup.groupName.value = Name;
	document.EditGroup.submit();
}



//VERIFICATION FUNCTION
function verify(formName) {
	if (formName == "contacts") {
		msg = "Are you sure you want to delete the selected contacts?";
	}
	if (formName == "groups") {
		msg = "Are you sure you want to delete the selected groups?";
	}
	return confirm(msg);
}



//FUNCTIONS FOR SELECT BOXES, ETC.
function copyToList(from,to) {
	//create 2 lists
	fromList = eval('document.getElementById("' + from + '")');
	toList = eval('document.getElementById("' + to + '")');
	
	//clear initial text from toList
	if (toList.options.length > 0 && toList.options[0].value == 'temp') {
		toList.options.length = 0;
	}
	
	var alreadythere = false;
	var sel = false;
	
	for (i=0;i<fromList.options.length;i++) {
		var current = fromList.options[i];
		if (current.selected) {
			sel = true;
			//give error if user selects initial toList text
			if (current.value == 'temp') {
				alert ('You cannot move this text!');
				return;
			}
			
			//create selected item in toList if it's not already there
			txt = current.text;
			val = current.value;
			for (a=0;a<toList.options.length;a++) {
				var toOptionVal = toList.options[a];
				if (val == toOptionVal.value) {
					var alreadythere = true;
				}
			}
			if (!alreadythere) {
				toList.options[toList.length] = new Option(txt,val);
			}
			
			//remove selected item from fromList
			if (from == "chosen") {
				fromList.options[i] = null;
				i--;
			}
		}
	}
	if (!sel) alert ('You haven\'t selected any contacts.');
}


function allSelect() {
	List = document.getElementById("chosen");
	if (List.length && List.options[0].value == 'temp') return;
	for (i=0;i<List.length;i++) {
		List.options[i].selected = true;
	}
}


function addSelectionsToParent() {

	var recipients = opener.document.myform.recipientemails;
	
	for (n=0; n<2; n++) {	
		
		var groupsObj = document.getElementById("groups");
		var contactsObj = document.getElementById("contacts");
		
		var groupvalue = groupsObj.options[groupsObj.selectedIndex].value;	
		
		//Loop through the group list
		for (i=0; i<groupsObj.options.length; i++) {
		
			//If a group is selected...
			if (groupsObj.options[i].selected) {
				//Detect if there are any contacts selected
				if (contactsObj.selectedIndex == -1) {
					var nocontacts = true;
				}
				
				//Look through contacts form
				for (n=0; n<contactsObj.options.length; n++) {
					//variable to check if email is already in list
					var matchstr = recipients.value.indexOf(contactsObj.options[n].value);
					
					//If a contact is selected and the email is not in the list...
					if (contactsObj.options[n].selected && matchstr == -1) {
						//add email to recipient list
						if (recipients.value=="") {
							recipients.value+=contactsObj.options[n].value;
						} else {
							recipients.value+=", " + contactsObj.options[n].value;
						}
					}
					
					if (nocontacts == true && matchstr == -1) {
						//add email to recipient list
						if (recipients.value=="") {
							recipients.value+=contactsObj.options[n].value;
						} else {
							recipients.value+=", " + contactsObj.options[n].value;
						}
					}
				}
			} else {
				for (n=0; n<contactsObj.options.length; n++) {
					
				}
			}
		}
	}
	opener.document.myform.recipientemails.focus();
}


function updateContactList() {
	
	var groupObj = document.getElementById("groups");
	var contactObj = document.getElementById("contacts");
	
	var contactsDisplayArray = new Array();
	
	var dup = "";
	
	if (groupObj.selectedIndex == -1) {
		contactObj.options.length = 0;
		return;
	} else {
		//Clear the contact list
		contactObj.options.length = 0;
		
		//Loop through the group list
		for (i=0, a=0; i<groupObj.options.length; i++) {
			//If a group is selected...
			if (groupObj.options[i].selected) {
				choice = groupObj.options[groupObj.selectedIndex].value;
				
				if (choice == "All") {
					for (n=0, a=a; n<allcontacts.length; n++, a++) {
						contactDupMark = allcontacts[n].contactDupMark;
						contactEmail = allcontacts[n].contactEmail;
						contactName = allcontacts[n].contactFirstName + ' ' + allcontacts[n].contactLastName;

						if (contactDupMark == "Y") {
							contactName = contactName + ' (' + contactEmail + ')';
						}
						
						contactObj.options[n] = new Option(contactName, contactEmail);
						/*
						contactLastName = allcontacts[n].contactLastName;
						contactFirstName = allcontacts[n].contactFirstName;
						contactEmail = allcontacts[n].contactEmail;
						contactsDisplayArray[a] = new Array(contactLastName, contactFirstName, contactEmail);
						*/
					}
				} else {
					//Place contacts from selected groups in an array
					var selectedGroup = groups[i - 1];
					for (n=0, a=a; n<selectedGroup.length; n++, a++) {
						contactLastName = selectedGroup[n].contactLastName;
						contactFirstName = selectedGroup[n].contactFirstName;
						contactEmail = selectedGroup[n].contactEmail;
						contactDupMark = selectedGroup[n].contactDupMark;
						contactsDisplayArray[a] = new Array(contactLastName, contactFirstName, contactEmail, contactDupMark);
					}	
				}
			}
			//sort contacts array
			contactsDisplayArray.sort();
			
			//put contacts array in list
			for (n=0, a=0; n<contactsDisplayArray.length; n++, a++) {
				var contactDupMark = contactsDisplayArray[n][3];
				var contactName = contactsDisplayArray[n][1] + ' ' + contactsDisplayArray[n][0];
				var contactEmail = contactsDisplayArray[n][2];
				
				if (n+1<contactsDisplayArray.length) { 
					var nextContactEmail = contactsDisplayArray[n+1][2];
				} else {
					nextContactEmail = "";
				}
				
				if (contactEmail != nextContactEmail) {
					if (contactDupMark == "Y") {
						contactName = contactName + ' (' + contactEmail + ')';
					}
					contactObj.options[a] = new Option(contactName, contactEmail);
				}
			}
		}
	}
	//contactObj.focus();
}

