/********************************************************************
/
/
/ 					liveSearch.js 
/ 					PUBLIC
/
/
*********************************************************************/

//Used to delay firing of AJAX on each and every keystroke, yuck
var count = 0;
function GrabSearchResultsPublic() 
{	
	var searchBox = document.getElementById("liveSearch");
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var liveSearchType = document.getElementById("liveSearchType");
	
	statusText.innerHTML = "Loading";
	
	//Nothing Entered
	if(searchBox.value == "") {
		searchResults.style.display = "none";
		document.getElementById("status").innerHTML = "";
		return 0;
	}
	
	//Too little entered
	if(searchBox.value.length < 2) {
		return 0;
	}
	//Too Much entered
	else if(searchBox.value.length > 30) {
		return 0;
	}
	
	//Give it a second
	//Used to delay firing of AJAX on each and every keystroke, yuck
	count = count + 1;
	setTimeout("GrabSearchResultsPublic_Send("+count+")", 400);
}

function GrabSearchResultsPublic_Send(currCount) {
	//Used to delay firing of AJAX on each and every keystroke, yuck
	if (currCount != count) 
		return 0;		
	else
		count = 0;
	
	var searchBox = document.getElementById("liveSearch");
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var liveSearchType = document.getElementById("liveSearchType");
	
	url = encodeURI("_ajax/xmlhttp_searchPublic.php?type=" + liveSearchType.value +"&search=" + searchBox.value);
	
	statusText.style.display = "block";
	
	GrabResults();
	//setTimeout('GrabResults()', 5);
	
	//Clear current result
	clearResultInfo();	
}



//Displays search results in LS_results div. 
//Used after user clicks on a specific result
function LS_resultInfo(type, id) {
	
	var LS_resultInfo = document.getElementById("LS_resultInfo");
	
	LS_resultInfo.innerHTML = "Loading";
	
	//write some custom php file
	url = encodeURI("_ajax/xmlhttp_searchPublicInfo.php?type=" + type + "&id=" + id);
	
	
	$('searchResults').style.display = 'none';
	LS_resultInfo.style.display = 'none';
	LS_resultInfo.innerHTML = xml_request_timed(url);
	new Effect.Appear('LS_resultInfo');
	
	//LS_resultInfo.style.display = "block";
	
	//Change color of selected item
	highlightSelected(type + id);
	//Jump up to anchor <a name ="topOfPAge">
	window.location.hash= "topOfPage";
	
	

} //end LS_display_info()


//Displays results for membership. 
function GrabMembershipPub(membership) {
	url = encodeURI("_ajax/xmlhttp_searchPublic.php?type=group&membership=" + membership);
	GrabResults();
	
	//Clear current result
	clearResultInfo();
	
} //end GrabMembershipPub()


//Sets search type (group or ip) 
function SetSearchPublic(type) {
	
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var liveSearchType = document.getElementById("liveSearchType");
	
	statusText.innerHTML = "Loading";
	liveSearchType.value = type;
	
	url = encodeURI("_ajax/xmlhttp_searchPublic.php?type=" + type);

	statusText.style.display = "block";
	
	GrabResults();
	
	//Change the Icon
	if (type == 'ip') type = 'location';
	displayActiveIconPublic(type);
	
} //end: SetSearchPublic()

//Styles Icon and Title for each search
function displayActiveIconPublic(selectedType) {
	
	//Find Total of selected entry
	//url = encodeURI("_ajax/xmlhttp_searchInternalMax.php?type=" + selectedType);
	//searchInternalMax = xml_request_timed(url);
	
	var iconDiv = document.getElementById("LS_objectTypeIcon");
	var name = selectedType.substring(0,1).toUpperCase() + selectedType.substring(1);
	iconDiv.innerHTML = '<img src="_images/icons/'  + selectedType + '.png" /> ' + name + ' search';	
	
} //end displayActiveIcon()


//Fetches further information on sub obects (eg group -> location -> building)
function showSubInfo(type, id, count) {	
	var subInfoDiv = document.getElementById(type + id + "SubInfo" + count);
	if (alternateDiv(type + id + "SubInfo" + count) ) {
		url = encodeURI("_ajax/xmlhttp_searchPublicSubInfo.php?type=" + type + "&id=" + id);	
		subInfoDiv.innerHTML = xml_request_timed(url);
	}
} // end showSubInfo()

/********************************************************************
/
/
/ 					liveSearch.js 
/ 					INTERNAL
/
/
*********************************************************************/

function GrabSearchResultsInternal() 
{
	
	
	var searchBox = document.getElementById("liveSearch");
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var type = document.getElementById("liveSearchType");
	
	statusText.innerHTML = "Loading";
	
	//Nothing Entered
	if(searchBox.value == "") {
		searchResults.style.display = "none";
		statusText.innerHTML = "";
		return 0;
	}
	
	//Too little entered
	if(searchBox.value.length < 2) {
		statusText.innerHTML = "Type more than one letter.";
		return 0;
	}


	url = encodeURI("_ajax/xmlhttp_searchInternal.php?search=" + searchBox.value + "&type=" + type.value);
	
	
	setTimeout('GrabResults()', 5);
	

	statusText.style.display = "block";
		//Clear current result
	clearResultInfo();
}


//Displays results for membership. 
function GrabMembershipInt(membership) {
	
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var type = document.getElementById("liveSearchType");
	type.value = "group";
	
	
	//Style the div
	displayActive(membership);	
	
	url = encodeURI("_ajax/xmlhttp_searchInternal.php?membership=" + membership);
	GrabResults();
		
	//statusText.style.display = "block";
	
	//Clear current search info results
	clearResultInfo();
	//Change the Icon
	//displayActiveIcon('group');
	
	
	
} //end GrabMembershipPub()

//Displays results for CertStatus
function GrabLocationCertStatus (certstatus) {
	
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var type = document.getElementById("liveSearchType");
	type.value = "location";
		
//	displayActive(location);
	
	url = encodeURI("_ajax/xmlhttp_searchInternal.php?certstatus=" + certstatus);
	GrabResults();
	
	//clearResults();
	clearResultInfo();
 
//	var xml = xml_request(url);
 //   showResults.innerHTML = xml_request(url);
 //   showResults.style.display = "block";

}

function GrabDistrictGroups (districtId) {
	
	/*
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var type = document.getElementById("liveSearchType");
	type.value = "district";
	
	url = encodeURI("_ajax/xmlhttp_searchInternal.php?district=" + districtId);
	GrabResults();
	
	//clearResults();
	clearResultInfo();
	
	var type = document.getElementById("LS_resultInfo");
	type.display = "block";
	*/
	
	swapDiv("edit", "LS_resultInfo");
	
	var url = "_ajax/xmlhttp_searchInternal.php";
	var params = "district=" + districtId;
	var myAjax = new Ajax.Updater('searchResults', url, { method: 'post', parameters: params });
	
	var url = "_ajax/xmlhttp_searchInternalInfo.php";
	var params = "type=districts&id=" + districtId;
	var myAjax = new Ajax.Updater('LS_resultInfo', url, { method: 'post', parameters: params });


}

function GrabTms(tms) {
	
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	
	url = encodeURI("_ajax/xmlhttp_searchInternal.php?tms=" + tms);
	GrabResults();
	
	//clearResults();
	clearResultInfo();

}

//Changes what the live search is for
function liveSearchType(selectedType) {
	var searchBox = document.getElementById("liveSearch");
	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");	
	var type = document.getElementById("liveSearchType");
	var name = document.getElementById("liveSearchTypeName");
	
	//Style the div
	displayActive(selectedType);	
	
	//Display search type
	name.innerHTML = selectedType + " Search";
	
	searchBox.value = '';
	searchResults.innerHTML = '';
	statusText.innerHTML = 'Idle';
	type.value = selectedType;
	
	
	//Clear current search info results
	clearResultInfo();	
	
	//Display some results
	LS_idleSearch();
	
	//If Selected Type = group, display Membership
	if (selectedType == 'group')
		$('groupMembership').style.display = 'block';
	else
		$('groupMembership').style.display = 'none';
		
	//If Selected Type = location, display Certification
	if (selectedType == 'location') 
	      $('certStatus').style.display = 'block';
	else
		  $('certStatus').style.display = 'none';	
	
  //If selected Type = disttict, display District	
    if (selectedType == 'district')
		$('districtList').style.display = 'block';
	else
		$('districtList').style.display = 'none';
	
	if (selectedType == 'endpoint')
		$('tmsList').style.display = 'block';
	else
		$('tmsList').style.display = 'none';
}

//Styles the links to show which is selected
function displayActive(selectedType) {
	
	var currentType = document.getElementById("LSType");
	var oldType = document.getElementById(currentType.value);
	oldType.style.background = "#FFFFFF";
	
	
	currentType.value = selectedType;	
	var newType = document.getElementById(selectedType);	
	newType.style.background = "#999999";	
	
	//Change the Icon
	displayActiveIcon(selectedType);
} //end displayActive()

//Styles Icon and Title for each search
function displayActiveIcon(selectedType) {
	
	//Find Total of selected entry
	url = encodeURI("_ajax/xmlhttp_searchInternalMax.php?type=" + selectedType);
	searchInternalMax = xml_request(url);
	
	var iconDiv = document.getElementById("LS_objectTypeIcon");
	var name = selectedType.substring(0,1).toUpperCase() + selectedType.substring(1);
	iconDiv.innerHTML = '<img src="../_images/icons/'  + selectedType + '.png" /> ' + name + " Search (" + searchInternalMax +")";	
	
} //end displayActiveIcon()

//Returns a generic search for the given type
function LS_idleSearch() {

	var searchResults = document.getElementById("searchResults");
	var statusText = document.getElementById("status");
	var type = document.getElementById("liveSearchType");
	
	
	statusText.innerHTML = "Loading";
	

	url = encodeURI("_ajax/xmlhttp_searchInternal.php?idle=true&type=" + type.value);
	
	statusText.style.display = "block";
	
	setTimeout('GrabResults()', 5);
} //end LS_idleSearch()


//Displays search results in LS_results div. 
//Internal version displays ALL FIELDS and has an edit button
function LS_resultInfoInternal(type, id) {
	swapDiv("edit", "LS_resultInfo");
	
	var LS_resultInfoDiv = document.getElementById("LS_resultInfo");	
	LS_resultInfoDiv.innerHTML = "Loading";
	
	//write some custom php file
	url = encodeURI("_ajax/xmlhttp_searchInternalInfo.php?type=" + type + "&id=" + id);
	
	//header = "<h2>" + type.substring(0,1).toUpperCase() + type.substring(1, type.length - 1) + " Information</h2>";
	
	//new Effect.Fade('LS_resultInfo');
	LS_resultInfoDiv.style.display = 'none';
	
	LS_resultInfoDiv.innerHTML = xml_request_timed(url);
	new Effect.Appear('LS_resultInfo');
	
	//Change color of selected item
	highlightSelected(type + id);
	//Jump up to anchor <a name ="topOfPAge">
	window.location.hash= "topOfPage";

} //end LS_display_info()

//Edits an object in the LS_results div. 
//Places edit functions in an iFrame, then reloads it
function LS_editInfo(type, id) {
	swapDiv("LS_resultInfo", "edit"); 
	
	var editFrame = document.getElementById("editFrame");
	editFrame.src="_ajax/xmlhttp_editInternal.php?type=" + type + "&id=" + id;
	editFrame.location.reload(true);
} //end LS_editInfo()

//Adds a room to the building object selected from the LS_results div. 
//Puts the create room form in an iFrame, then reloads it
function LS_addRoom(id) {
	swapDiv("LS_resultInfo", "edit"); 
	
	var editFrame = document.getElementById("editFrame");
	editFrame.src="_ajax/xmlhttp_objectCreateSelect.php?selected=true&type=room&buildingId=" + id;
	editFrame.location.reload(true);
} //end LS_editInfo()


//After a delete, hides + clears the iframe, and brings ls_resultInfo back into being
function deletedObject(object) {
	setTimeout("deletedObject_c()",2100);		
} //end deletedObject
function deletedObject_c() {
	swapDiv("LS_resultInfo", "edit");
	var editFrame = document.getElementById("editFrame");
	editFrame.src="";
	var LS_resultInfo = document.getElementById("LS_resultInfo");	
	LS_resultInfo.innerHTML = "";
	
	//spit out some results
	LS_idleSearch();
} //end deletedObject_c()


//After an edit or room add, refreshes info payne
function editedObject(type, id) {
	//alert("ok");
	setTimeout("editedObject_c('" + type +"' , '" + id + "')",2100);		
} //end deletedObject
function editedObject_c(type, id) {
	swapDiv("LS_resultInfo", "edit");
	var editFrame = document.getElementById("editFrame");
	editFrame.src="";
	
	//refresh this side
	LS_resultInfoInternal(type, id);
} //end deletedObject_c()

//Group -> view detailed location information

function alternateLocation(showDivId, iconId, plus, minus) {
	

	var div = document.getElementById(showDivId);
	var icon = document.getElementById(iconId);
	var icon_src = '';
	(div.style.display == "none") ? icon_src=minus : icon_src=plus;
	//document['plus'].src= icon_src;
	icon.src = icon_src;
	//alert(icon_src);
	
	 
	alternateDiv(showDivId);
} //end alternateLocation()

//Fetches further information on sub obects (eg group -> location -> building)
function showSubInfoInternal(type, id, count) {	
	var subInfoDiv = document.getElementById(type + id + "SubInfo" + count);
	if (alternateDiv(type + id + "SubInfo" + count) ) {
		url = encodeURI("_ajax/xmlhttp_searchInternalSubInfo.php?type=" + type + "&id=" + id);	
		subInfoDiv.innerHTML = xml_request_timed(url);
	}
} // end showSubInfo()


/********************************************************************
/
/
/ 					liveSearch.js 
/ 					Other Functions used all over the place
/
/
*********************************************************************/

/*
*	These functions help to edit the notes
*/
function locationNotesEdit(id) {
	//http://wiki.script.aculo.us/scriptaculous/show/Draggable
	//onclick="new Effect.Fold(this); window.setTimeout('Effect.Appear(\'demo-effect-fold\', {duration:.3})',2500);"
	
	var locationNotesEdit = 'locationNotesEdit' + id;
	//alert(locationNotesEdit);
	//Put in form
	url = encodeURI("_ajax/xmlhttp_locationNote.php?id=" + id);
	$(locationNotesEdit).style.display="block";
	//new Effect.SlideDown(locationNotesEdit);
	$(locationNotesEdit).innerHTML = xml_request_timed(url);
	//Display Form
	
	new Draggable(locationNotesEdit, {});	
}

function locationNotesEditClose(id) {
	var locationNotesEdit = 'locationNotesEdit' + id;
	new Effect.SlideUp(locationNotesEdit);
	//var kill = new Draggable(locationNotesEdit, {});
	//kill.destroy();
	
}

//Submit changes
function locationNotesEditSubmit(id) {
	var locationNotes = 'locationNotes' + id;
	var notes =  $(locationNotes).value;
	var locationNotesShow = 'locationNotesShow' + id;
	
	
	url = encodeURI("_ajax/xmlhttp_locationNote.php?insert=true&notes=" + notes + "&id=" + id);
	$(locationNotesShow).innerHTML = xml_request_timed(url);
	new Effect.Highlight(locationNotesShow);
	window.setTimeout('locationNotesEditClose(\''+id+'\')', 2500);
	//locationNotesEditClose(id);	
}



/********************************************************************
/
/
/ 					liveSearch.js 
/ 					Helper
/
/
*********************************************************************/

function GrabResults() {	
	var searchResults = document.getElementById("searchResults");	
	searchResults.innerHTML = xml_request_timed(url);
	searchResults.style.display = "block";
}


//Clears the contents of a div
function clearResult() {
	var result = 	document.getElementById('searchResults');
	result.style.display = 'none';
		
	clearResultInfo();
}

//Clears the contents of the 'LS_resultInfo' div
function clearResultInfo() {
	var resultInfo = 	document.getElementById('LS_resultInfo');
	resultInfo.style.display = 'none';
	
	var edit = 	document.getElementById('edit');
	edit.style.display = 'none';	
}

//Hides the first dive, shows the second
//Eventually we'll integrate scriptaculous into this
function swapDiv(hideDivId, showDivId) {
	var hideDiv = document.getElementById(hideDivId);
	var showDiv = document.getElementById(showDivId);
	
	if (hideDiv.style.display != 'none') {
		hideDiv.style.display = 'none'
	}
	
	if (showDiv.style.display == 'none') {
		showDiv.style.display = 'block'
	}

} //end swapDiv()


//Hides the first dive, shows the second
//Eventually we'll integrate scriptaculous into this
//returns true if now visible, false is now hidden
function alternateDiv(showDivId) {
	var showDiv = document.getElementById(showDivId);
	
	if (showDiv.style.display == 'none') {
		showDiv.style.display = 'block';
		return true;
	}
	
	else if (showDiv.style.display != 'none') {
		showDiv.style.display = 'none';
		return false;
	}

} //end swapDiv()

//highlights the selected div, stores it in window object
function highlightSelected(selected) {
	//store that item into the window object, for when a second item is clicked
	// on more about this http://snook.ca/archives/javascript/global_variable/
	if (typeof window.selectedResult != "undefined") {
		prev_selected = window.selectedResult;
		$(prev_selected).style.background = window.selectedResult_background;		
	}
	window.selectedResult = selected;
	window.selectedResult_background = $(selected).style.background;	
	$(selected).style.background= '#BEE4FC';
	

	
} //end highlightSelected()

//returns a random string, need this to break IE's nasty habit of caching ajax calls (post, or otherwise)
function uniqueStamp() {
	//Break IE Caching nonsense
	var randomnumber=Math.floor(Math.random()*21);
	return "&unique=" + randomnumber;	
} //end uniqueStamp()


//Carries out an xml request, but displayes time elapsed in the 'status' div
function xml_request_timed(url) {
	var start = new Date(); //Time
	var status = document.getElementById("status");
	
	var xml = xml_request(url);
	
	end = new Date();	
	elapsed = end.getTime() - start.getTime();
	status.innerHTML = "Done (" + elapsed / 1000 + " sec)";
	
	return xml;
} //end xml_request_timed()
	



/*****************************************
/ 			AJAX
/ 			Mozilla: XMLHttpRequest()
/			IE: ActiveXObject("Microsoft.XMLHTTP")
******************************************/
function xml_request(url) {
  req = xml_http_request_object();
  req.open("GET", url + uniqueStamp() , false);
  req.send(null);
    
	try { // code for Mozilla, etc.
  		response = req.responseText;		
	} catch(e) {		
		try {// code for IE
		response = req.responseText;
		} catch(E) {
			response = "error";
		}
	}
	return response;

} //end: xml_request()


function xml_http_request_object() {
	var xmlhttp = null;

	// code for Mozilla, etc.
	try {
	  xmlhttp = new XMLHttpRequest();
	} catch(e) {
		try {
			// code for IE
			  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E) {
				xmlhttp = false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	 return xmlhttp;
} //end: xml_http_request_object()



