// Javascript for Rolodex search form
var paths = {
	rolodex : "/careerservices/customcf/embeddedApps/Rolodex/"
};


var responseSuccess = function(o){ 

	ILR.animation.fadeOutIn(o.responseText, "xhrResponseContainer");
	// Load the JavaScript for the search results 
	ILR.util.scriptLoader.loadScriptsOnce([paths.rolodex + "searchRolodex/js/showSearchResults.js"]);	
}; 


var responseFailure = function(o){ 

    alert("Transaction failed.  Error: " + o.status + " " + o.statusText);
    showSearchForm();
};

var callback = { 

	success : responseSuccess, 
	failure : responseFailure

}; 

function initRolodexSearch() {

	var dsConfig = setACDataSourceConfig(),
		cnDS,
		cnAutoComp,
		orgDS,
		orgAutoComp;
		
	// This fixes a bug that only surfaces under the following conditions: 
	// (1) Disable Javascript. (2) Search. (3) Select "refine search" - this sets the query string 
	// "?refine=1" in the url. (4) Refresh the page - now the defaultValue of any non-empty form fields
	// is set to the current value. (5) Enable Javascript. (6) Run a search. (7) Select "new search" or 
	// refresh the page. Since the defaultValue is set, the previous values that were set as defaultValues
	// reappear in the form. Admittedly an arcane sequence, it should be avoided if only to save future
	// debuggers. 
	if (/refine=[^&]*/.test(location.search)) {
		location.search = location.search.replace(/refine=[^&]*/, "");
	}
	// RY 5/29/07 Using above version instead, since this wipes out showDumpStruct in url
	// Test for (location.search.length > 1) instead of (location.search), since in IE6 and Opera, 
	// when location.search is set to "", it retains the "?" and thus is never empty, so the 
	// page keeps reloading in an infinite loop.
    //	if (location.search.length > 1) {
    //		location.search = "";
    //	}

	/* Set up autocomplete fields */
					 	
	// Contact name autocomplete
	cnDS = new YAHOO.widget.DS_XHR(paths.rolodex + "autocomplete/acContactNames.cfm", ["\n"], dsConfig);				 	
	cnAutoComp = new YAHOO.widget.AutoComplete("contactName", "contactName_acResults", cnDS);
	setDefaultACVals(cnAutoComp, "contactName", true);

	// Organization name autocomplete	
	// Use newline as record delimiter because organization names can contain comma
	orgDS = new YAHOO.widget.DS_XHR(paths.rolodex + "autocomplete/acOrgNames.cfm", ["\n"], dsConfig);
	orgAutoComp = new YAHOO.widget.AutoComplete("orgName", "orgName_acResults", orgDS);
	setDefaultACVals(orgAutoComp,"orgName", true);
			
			
	/* Event handlers for searching/browsing cards */
	
	// The search form submit button
	YAHOO.util.Event.addListener("submit", "click", searchRolodex);	
	// The "browse all cards" link
	YAHOO.util.Event.addListener("browse", "click", browseAllCards);		
}


// Event handler for the "browse all cards" link
function browseAllCards(event) {

	// Reset all form fields, in case the user entered input before clicking the browse link
	//clearForm("card_search");  RY change to built-in Form.reset()
	document.getElementById("card_search").reset();
	// Do the search
	searchRolodex(event);
}

// Event handler for the search form submission
function searchRolodex(event) {

	var url;
	
	/* Cancel the default form submission action */
	YAHOO.util.Event.stopEvent(event);	

	/* Modify the display of existing elements */	
	// Remove the form background color from the page title 
	YAHOO.util.Dom.removeClass("pageHeader", "formHeader"); 

	// Hide the intro text and search form
	// This doesn't work in author mode. Need to hide the enclosing div that has class="page" instead.
	// YAHOO.util.Dom.addClass("searchCards", "hidden");
	YAHOO.util.Dom.addClass(getAncestor( { el: "searchCards", tagname: "div", classname: "page" }), "hidden");

	
	// Display the new/refine search links
	YAHOO.util.Dom.removeClass("linksToForm", "hidden");
	
	/* Make the Ajax request */
	// Build the url string: destination server page + url query string
	// This is now done with YAHOO.util.Connect.setForm()
	// var url = paths.rolodex + "searchRolodex/showSearchResults.cfm?" + serializeFormVals("card_search");
	url = paths.rolodex + "searchRolodex/showSearchResults.cfm";
	// NB event.target not supported in IE, at least version 6
	if (YAHOO.util.Event.getTarget(event).id == "browse") {
		url += "?SubmitForm=Browse";
	}
	YAHOO.util.Connect.setForm(document.getElementById("card_search"));
	// Initiate the asynchronous transaction using YUI Connection Manager
	YAHOO.util.Connect.asyncRequest("GET", url, callback, null);
	
	ILR.animation.showActivityIndicator("/images/animations/activityIndicators/indicator_big_b31b1b.gif", "xhrResponseContainer");
}

// Restore the search form with its previous values. Used as onclick event for "Refine search" link,
// and to redisplay the form in case of a transaction failure.
function showSearchForm(event) {

	// Delete the search results div
	document.getElementById("xhrResponseContainer").innerHTML = "";
	
	// Re-class the page title as a formHeader
	YAHOO.util.Dom.addClass("pageHeader", "formHeader");
	
	// Unhide the intro text and form
	//YAHOO.util.Dom.removeClass("searchCards", "hidden");
	// This doesn't work in author mode. Need to hide the enclosing div with class="page" instead.
	YAHOO.util.Dom.removeClass(getAncestor( { el: "searchCards", tagname: "div", classname: "page" }), "hidden");
	
	// Prevent the link from being followed
	// NB event is undefined if we get here from responseFailure  
	if (event) {
		YAHOO.util.Event.stopEvent(event);
	}

	// Fade in the search form
	YAHOO.widget.Effects.Appear("card_search", ILR.animation.defaultFadeOpts);	

}

// Restore the search form, clearing previous values. On click behavior for the "New search" link.
function showClearedSearchForm(event) {

	//clearForm("card_search");  RY change to built-in Form.reset()
	document.getElementById("card_search").reset();
	showSearchForm(event);
}

YAHOO.util.Event.addListener(window, "load", initRolodexSearch); 