var IsApartmentCoordinatorInitialIndex;
var HaveApartmentAlreadyInitialIndex;
var HAAValue;
function AccountInfo_AssignInitialValues(HAAInitial, IACInitial, HAAInitialValue){
	IsApartmentCoordinatorInitialIndex = IACInitial;
	HaveApartmentAlreadyInitialIndex= HAAInitial;
	HAAValue = HAAInitialValue;

}
function AccountInfo_CheckApartmentCoordinator(){

	if(document.search_quick.ApartmentCoordinator.value == "true"){
		document.search_quick.RoommatesCanFacilitate.disabled = false;
		document.search_quick.RoommatesLookingFor.disabled = false;
	}
	else if(document.search_quick.ApartmentCoordinator.value == "false"){
		document.search_quick.RoommatesCanFacilitate.disabled = true;
		document.search_quick.RoommatesLookingFor.disabled = true;
	}
}

function AccountInfo_CheckLinkedRoommates(SiteURL, RequestorUserID, HaveApartmentAlready){
	var CoordinatorChangeWarningMessage;
	CoordinatorChangeWarningMessage = '';
	var TargetRequest = '';
	TargetRequest = SiteURL + 'roommates/unlinkallroommates.asp?ubuid=' + RequestorUserID;

	if(document.search_quick.IsLinkedToAGroup.value == "True"){
		if(document.search_quick.AuxIsAparmentCoordinator.value == "True"){
			CoordinatorChangeWarningMessage = 'You are an apartment coordinator and have roommates linked to you!\nYou need to unlink them in order to continue with your changes.\nWould you like to unlink them now?';
		}
		else{
			CoordinatorChangeWarningMessage = 'You are linked to an apartment coordinator already!\nYou need to unlink yourself in order to continue with your changes.\nWould you like to unlink yourself now?';
		}
		if(confirm(CoordinatorChangeWarningMessage)){
			AccountInfo_UnlinkUser(TargetRequest);
		}
		else{
			document.search_quick.ApartmentCoordinator.selectedIndex = IsApartmentCoordinatorInitialIndex;
			document.search_quick.HaveApartmentAlready[HaveApartmentAlreadyInitialIndex].checked = true;
			HAAValue = document.search_quick.HaveApartmentAlready[HaveApartmentAlreadyInitialIndex].value; 
		}
	}

}
function AccountInfo_CheckHaveApartment(HaveApartmentAlready){
	var HAA = HAAValue;

	if((HAA) == 1 || (HAA) == 0){
		document.search_quick.ApartmentCoordinator.disabled = false;
		document.search_quick.RoommatesCanFacilitate.disabled = false;
		document.search_quick.RoommatesLookingFor.disabled = false;
		AccountInfo_CheckApartmentCoordinator();
	}
	else{	
		document.search_quick.ApartmentCoordinator.selectedIndex = 0;
		document.search_quick.ApartmentCoordinator.disabled = true;
		document.search_quick.RoommatesCanFacilitate.disabled = true;
		document.search_quick.RoommatesLookingFor.disabled = true;
	}

}
function AccountInfo_CheckRoommatesLookingFor(){
	if(document.search_quick.RoommatesAlreadyFound.value > document.search_quick.RoommatesLookingFor.value){
		alert('You have more linked roommates than the number of roommates you are looking for.\nIn order to successfully edit this field you have to unlink the necessary roommates first.');
		document.search_quick.RoommatesLookingFor.selectedIndex = document.search_quick.RoommatesAlreadyFound.value;
	}
	if((document.search_quick.RoommatesCanFacilitate.value - 1) < document.search_quick.RoommatesLookingFor.value){
		document.search_quick.RoommatesCanFacilitate.selectedIndex = document.search_quick.RoommatesLookingFor.value;
	}
}

function Roommates_UnlinkRoommate(ApartmentSeekerUserID, UnlinkedByUserID, ApartmentCoordinatorUserID){
	
	document.UnlinkRoommate.asuid.value = ApartmentSeekerUserID;
	document.UnlinkRoommate.acuid.value = ApartmentCoordinatorUserID;
	document.UnlinkRoommate.ubuid.value = UnlinkedByUserID;
	
	if(ApartmentSeekerUserID == UnlinkedByUserID){
		if(confirm("Are you sure you want to unlink yourself from this group?")){
			document.UnlinkRoommate.submit();
		}
	}
	else{
		if(confirm("Are you sure you want to unlink this roommate?")){
			document.UnlinkRoommate.submit();
		}
	}
}

function LinkedRoommatesRequestAnswer(LinkedRoommateRequestID, LinkRequestAnswer){
	if(document.LinkedRoommateRequestAnswer != null){
		if(LinkedRoommateRequestID != '' && LinkRequestAnswer != ''){
			document.LinkedRoommateRequestAnswer.lrid.value = LinkedRoommateRequestID;
			document.LinkedRoommateRequestAnswer.ra.value = LinkRequestAnswer;
			document.LinkedRoommateRequestAnswer.submit(); 	
		}
		else{
			alert('An error ocurred while trying to perform save your answer, please contact Customer Service for help.');
		}
	}
	else{
		alert('Your link request was sent to the apartment coordinator already.\n You may get a answer very soon.');
	}
}

var xmlhttp;
				
function AccountInfo_UnlinkUser(TargetRequest){
	
	xmlhttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null){

		//alert(TargetRequest);
		xmlhttp.onreadystatechange=AccountInfo_UnlinkUser_StateChange;
		xmlhttp.open("GET",TargetRequest,true);			
		xmlhttp.send(null);							

	}
	else{
		alert("An error ocurred while trying to unlink you from your roommates group.\nPlease try to unlink yourself by using the \"roommates\" link on your navigation bar.");
	}
}
function AccountInfo_UnlinkUser_StateChange(){
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4){
		// if "OK"
		//alert(xmlhttp.status);
		if (xmlhttp.status==200){
			alert("Sucessfully unlinked!");
			document.search_quick.IsLinkedToAGroup.value = "False";
		}else{
			alert("An error ocurred while trying to unlink you or your roommates.\nYou may need to unlink yourself manually from the group.");
			//document.getElementById("xmlResponseText").innerHTML = "Problem retrieving XML data";
			if(document.search_quick.ApartmentCoordinator.value == "true"){
				document.search_quick.ApartmentCoordinator.selectedIndex = 1;	
			}
			else{
				document.search_quick.ApartmentCoordinator.selectedIndex = 0;
			}
		}
	}
}
				
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
