var userFound = 0;

function openLdapSearch(source) {
	// Open the search window.
	if(document.getElementById && document.getElementById(source + 'LastName')) {
		lastName = document.getElementById(source + 'LastName').value
		if(lastName == "") {
			alert("Please type the last name to search for before pressing Search.")
		} else {
			winWidth = 600
			winHeight = 500
			winLeft = (screen.availWidth / 2) - (winWidth / 2)
			winTop = (screen.availHeight / 2) - (winHeight / 2)
			openURL = './ldapUserSearch.php?jsPrefix=' + source + '&filter=[sn=' + lastName + ']'
			windowFeatures = 'width=' + winWidth + ',height=' + winHeight + ',left=' + winLeft + ',top=' + winTop + ',toolbar=0,status=0,location=0,menubar=0,scrollbars=1'
			window.open(openURL, 'ldapSearch', windowFeatures)
		}
	}
}

function openTicketUserSearch(source) {
	// Open the search window.
	if(document.getElementById && document.getElementById(source)) {
		lastName = document.getElementById(source).value
		if(lastName == "") {
			alert("Please type the last name to search for before pressing Search.")
		} else {
			winWidth = 700
			winHeight = 500
			winLeft = (screen.availWidth / 2) - (winWidth / 2)
			winTop = (screen.availHeight / 2) - (winHeight / 2)
			openURL = './TicketUserSearch.php?filter=[sn=' + lastName + ']'
			windowFeatures = 'width=' + winWidth + ',height=' + winHeight + ',left=' + winLeft + ',top=' + winTop + ',toolbar=0,status=0,location=0,menubar=0,scrollbars=1'
			window.open(openURL, 'ldapSearch', windowFeatures)
		}
	}
}

function transferValuesToParent(columnsArray, valuesArray, source) {
	// Transfer the values to the parent window.
	if(window.opener) {
		var counter
		for(counter = 0; counter < columnsArray.length; counter++) {
			if(window.opener.document.getElementById && window.opener.document.getElementById(columnsArray[counter])) {
				window.opener.document.getElementById(columnsArray[counter]).value = valuesArray[counter]
			}
		}
		userFound = 1;
		self.close();
		window.opener.focus();
		if(source == "primaryContact") {
			window.opener.roomReservationForm.secondaryContactLastName.focus();
		}
		if(source == "secondaryContact") {
			window.opener.roomReservationForm.eventName.focus();
		}
	}
}

function unloadLdapSearch(source) {
	// On unload set the focus to the correct field if the user was not found.
	if(userFound == 0) {
		window.opener.focus();
		if(source == "primaryContact") {
			window.opener.roomReservationForm.primaryContactName.focus();
		}
		if(source == "secondaryContact") {
			window.opener.roomReservationForm.secondaryContactName.focus();
		}
	}
}
