if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number

  if (ieversion==6){var ie6 = true; }
  if (ieversion==7){var ie7 = true; }
}
var access_level_user = "";
//assign behaviors to UI elements
var init = function(accessLevel){
	$("table").addClass("hasJS");
	//$("#utilities").show("slow");
	access_level_user = accessLevel;

	//if not logged in add login functionality to links
	if (access_level_subscribed.indexOf(access_level_user) == -1) {
		var loginLink = document.getElementById('loginLink');		
		var x = access_level_subscribed.indexOf(access_level_user);		
		attachShowSideBarClickEvent(loginLink);
		var customizeLink_view = document.getElementById('customizeLink_view');
		attachShowSideBarClickEvent(customizeLink_view);
	} else {
	
	}
	//POPUP - jw added div popup-content
	if(ie6){
	$("#container").after('<div id="popup" style="visibility:hidden;"><img class="top" style="filter:  progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/ie6/popup_top.png\',sizingMethod=\'crop\');"  src="../images/ie6/clear.gif" height="18" width="282" /><div class="body" style="background:none;  filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/ie6/popup_body.png\',sizingMethod=\'scale\');"><div id="popup-content"><div id="popup-content"></div></div><img class="bot" style="filter:  progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'../images/ie6/popup_bot.png\',sizingMethod=\'crop\');"  src="http://patientsafetymonitor-dev.hcpro.com/creative/site/images/clear.gif" height="23" width="282" /></div>');
	} else{
		
	$("#container").after('<div id="popup" style="display:none;"><img class="top" src="../images/popup_top.png" height="18" width="282" /><div class="body"><div id="popup-content"></div></div><img class="bot" src="../images/popup_bot.png" height="23" width="282" /></div>');
	}
	
	//Attach eventliteners to the calls if they contain data
	var rows=document.getElementById('tBody').rows;
	for ( var j=0; j<=rows.length-1; j++ ){
	   cells=rows[j].getElementsByTagName("TD");
		for (var i=0; i<cells.length; i++){
			if (cells[i].className == 'hasContent'){
				attachShowSideBarClickEvent (cells[i]);
			}
		} 	
	}

	//Add event handlers to nav links
	var customizeLink_goal = document.getElementById('customizeLink_goal');
	var customizeLink_state = document.getElementById('customizeLink_state');
	var customizeLink_view = document.getElementById('customizeLink_view');

	if (customizeLink_goal.addEventListener){ 
		customizeLink_goal.addEventListener("click", handleCustomizeLinkClick,false);
		customizeLink_state.addEventListener("click", handleCustomizeLinkClick,false);
		customizeLink_view.addEventListener("click", handleViewChangeLinkClick,false);
		
	} else if (customizeLink_goal.attachEvent) {
		customizeLink_goal.attachEvent ("onclick",handleCustomizeLinkClick,false);
		customizeLink_state.attachEvent ("onclick",handleCustomizeLinkClick,false);
		customizeLink_view.attachEvent ("onclick",handleViewChangeLinkClick,false);
	}

	//Search box
	$("input.initval").each(function(){
		hint = "Search the Crosswalk";
		$(this).attr("value", hint);
												   
	});
	
	$('.initval').focus(function() {
		if($(this).hasClass("initval")){
			$(this).attr("value", "").removeClass("initval");
		}
	});

	$("#search-container #search").blur(function() {
		currentval = $(this).attr("value");
		if( currentval == undefined || currentval == ""){
			hint = "Search the Crosswalk";
			$(this).addClass("initval").attr("value", hint);
		}
	});

	//Print an article
	$("#print").click(function(){
		window.print();
		return false;
	});

	$("#loading").hide();
	
} // END of Init 

var timer; //used for the 'loading' box
// Position the tooltip.
var positionTooltip = function(event) {
		var tblWidth = $("table").width();
	$("#status").html(tblWidth);
		//****jw added - http://dev.jquery.com/ticket/1171
	//finds the x/y for IE
	if ( event.pageX == undefined && event.clientX != undefined ) {
		 var e = document.documentElement, b = document.body;
		 event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft);
		 event.pageY = event.clientY + (e.scrollTop || b.scrollTop);
	}
	var adjWidth =  (event.pageX + 26)  + 282; 
	
	if(adjWidth > tblWidth){
		var tPosX = event.pageX - 282;
	} else {		
		var tPosX = event.pageX + 10;
	}

	var tPosY = event.pageY + 30;

	$('#popup').css({top: tPosY, left: tPosX});
};
// Show (create) the tooltip.
var showTooltip = function(event) {	  
	timer = setTimeout("$('#popup').fadeIn('fast')", 750);
	positionTooltip(event);
};	

if(ie6){
	// Show (create) the tooltip.
	var showTooltip = function(event) {
	  
	timer = setTimeout("$('#popup').css('visibility','visible')", 750);
	positionTooltip(event);
	};
}

// Hide (remove) the tooltip.
var hideTooltip = function() {   

	if(timer){
		clearTimeout(timer);
	}	

	if(ie6){$('#popup').css("visibility", "hidden");}
	else{$('#popup').hide();}
	
};
/*
***********************************************************************
The commented code above is used to add RollOver functionality
**************************************************************************
*/

var sideBarOpen = false;
var showSideBarCount = 0;//set to 0 this is needed for an IE7 hack

// Show (create) the sidebar.
var showSidebar = function(event) {
  // only run if the sidebar doesn't exist  
  if( $("#crosswalk-detail").length < 1){
	// grab the last column in the table and remove it
	$('table tr').find("td:eq(3), th:eq(4)").css("display","none").removeClass("last");
	// move the scroll arrow 
	$('table th:eq(3) p').after('<a href="javascript:moveRightMulti();" id="goal-right">More Topics</a>');
	$('table th:eq(3)').addClass("last");		
	$('thead th').css("width", "24.92492%");

	insertSidebar();																																																																																																																																																																																																																																											
	// Bind click handleer to newly created Close button
	$("#close").click(function(){	
		closeSidebar();
		return false;
	});																																																																																																																																																																																																																																											
	// resize the table to make room for the sidebar
	// this code should run in all browsers - however in IE7 it must not run 
	// the first time the method is called and then should run each subsequent time the method is called
	if (!ie7 || showSideBarCount > 0){
		var tblWidth = $('table').width() - $("#crosswalk-detail").width();
		$("table, thead").width(tblWidth);		
		theadWidth = (tblWidth-3)/4;
		tbodyWidth = (tblWidth-3)/4;
		$("thead th").width(theadWidth);
		$("tbody td").width(tbodyWidth);
		$("tbody th").width(tbodyWidth-10);
	} 
	//position the sidebar
	sizeSidebar(event);
	showSideBarCount++;
	sideBarOpen = true;
  } 
  return false;
}

// Position and set the height of the sidebar
var sizeSidebar = function(){
	var docHeight = $(document).height() - 105;
	if(ie6){
		var docHeight = $(document).height() - 114;
	}
	$("#crosswalk-detail").css({height: docHeight});
}

function insertSidebar(){
	$("#container").after('<div id="crosswalk-detail"><div id="inner"></div></div>'); 
	populateSideBar();
}

function populateSideBar(){
	var x = document.getElementById('inner');
	if (access_level_subscribed.indexOf(access_level_user) == -1) {
		x.innerHTML='<a href="#" id="close">Close X</a><div class="member_signin"><h2>Member Login</h2><p>Please enter your username &amp; password below to access exclusive member features.</p><form><fieldset><p class="error-msg" id="login-error"></p><label for="member_login">Username</label><input id="member_login" class="field" value="" name="customer_login" type="text"><label for="member_password">Password</label><input id="member_password" class="field" value="" name="customer_password" type="password"><input id="member_login-button" src="../images/btn_member_login.gif" value="Log In" type="image" onclick="javascript:login();return false;"></fieldset></form><p>Not a member?  <br><a href="/membership">Click here to sign up now!</a></p></div>';	
	} else {		
		x.innerHTML='<a target="_blank" href="crosswalk_detail_print.cfm?" id="print" >Print</a><a href="" id="email" >E-Mail</a> <a href="#" id="close">Close X</a><div id="crosswalk-detail-content"><br><br><br>loading...</div>';		
	}
}

var closeSidebar = function(event){
	sideBarOpen = false;
	// move the scroll arrow 
	$('table th:eq(3) a').remove('#goal-right');
	$('table th:eq(3)').removeClass("last");
	$('table tr').find("td:eq(3), th:eq(4)").css("display","table-cell").addClass("last");	
	$('table th:eq(4) p').after('<a href="javascript:moveRightMulti();" id="goal-right">More Topics</a>');

	var horizMaxPos = totalCols-gridColCount;
	if (horizPos >= (horizMaxPos)) {
		goalRightHideDisplay();
	} else {
		goalRightShowDisplay();
	}

	var tblWidth = $('table').width() + $("#crosswalk-detail").width();
	$("table, thead").width(tblWidth);
	theadWidth = (tblWidth-4)/5;
	tbodyWidth = (tblWidth-4)/5;
	$("thead th").width(theadWidth);
	$("tbody td").width(tbodyWidth);
	$("tbody th").width(tbodyWidth-10);	
	//sizeTable(event);
	$('#crosswalk-detail').remove();
	$('table').css("width", "100%");
	return false;
}

if(ie6 || ie7){
	var closeSidebar = function(event){
		sideBarOpen = false;
		// move the scroll arrow 
		$('table th:eq(3) a').remove('#goal-right');
		$('table th:eq(3)').removeClass("last");
		$('table tr').find("td:eq(3), th:eq(4)").css("display","inline").addClass("last");
		$('table th:eq(4) p').after('<a href="javascript:moveRightMulti();" id="goal-right">More Topics</a>');

		var horizMaxPos = totalCols-gridColCount;
		if (horizPos >= (horizMaxPos)) {
			goalRightHideDisplay();
		} else {
			goalRightShowDisplay();
		}


		var tblWidth = $('table').width() + $("#crosswalk-detail").width();
		$("table, thead").width(tblWidth);
		theadWidth = (tblWidth-4)/5;
		tbodyWidth = (tblWidth-4)/5;
		$("thead th").width(theadWidth);
		$("tbody td").width(tbodyWidth);
		$("tbody th").width(tbodyWidth-10);
		//sizeTable(event);
		$('#crosswalk-detail').remove();
		$('table').css("width", "100%");
		return false;
	}
}
var sizeTable = function(){
	var docWidth  =  $(document).width();
	var tblWidth  =  $('table').width();
	var sideWidth =  $("#crosswalk-detail").width();
	var theadWidth;
	var tbodyWidth;

	if( $("#crosswalk-detail").length > 0){
		tblWidth = docWidth - sideWidth;
		theadWidth = (tblWidth-3)/4;
		tbodyWidth = (tblWidth-3)/4;
	} else {
		tblWidth = docWidth;
		theadWidth = (tblWidth-4)/5;
		tbodyWidth = (tblWidth-4)/5;
	}
				
	$("table, thead").width(tblWidth);
	$("thead th").width(theadWidth);
	$("tbody td").width(tbodyWidth);
	$("tbody th").width(tbodyWidth-10);
}

$(window).resize(function(event){		 
	if( $("#crosswalk-detail").length > 0){	
		sizeSidebar(event);
		sizeTable(event);			
	} else {
		sizeTable(event);
	}
	if( $("#customize-container").css("display") == "block"){
		var docHeight = $(document).height() - 105;
				if(ie6){
				var docHeight = $(document).height() - 114;
				}
			closeSidebar();
			$("#customize-container").css({height: $(document).height()}).show();
			$("#customize").css({height: docHeight});
			$("#narrow_by").hide();	
	}
});

var handleCustomizeLinkClick = function(){
	if (access_level_subscribed.indexOf(access_level_user) >= 0) {
		closeSidebar();
		var docHeight = $(document).height();
		//IE6 does not like document.height!!
		//if(window.addEventListener) {
		if(!ie6) {
			docHeight = docHeight - 105;
			$("#customize-container").css({height: $(document).height()}).show();
			$("#customize").css({height: docHeight});
		}
		else {
			docHeight = docHeight - 114;
			$("#customize-container").css({clientHeight: $(document).height()}).show();			
			$("#customize").css({clientHeight: docHeight});
		}
		$("#narrow_by").hide();	
	} else {
		showSidebar();	
		$("#narrow_by").show();
	}
	return false;
}

var handleViewChangeLinkClick = function(){
	if (access_level_subscribed.indexOf(access_level_user) >= 0) {
		closeSidebar();
		toggleView();	
	} else {
		showSidebar();	
	}
	return false;

}
////***********************************************************////
////***********************************************************////
var horizPos=1;
//create psm_crosswalk_nav_cfc
//CONFIG!!!
var cfc =new psm_crosswalk_config();
var objConfig = cfc.getConfig();
var gridColCount = objConfig.GRIDCOLS-1; //-1 because the last col moves to the first onclick
var totalCols = objConfig.TOTALCOLS;
//access level necessary to view content
var access_level_subscribed = objConfig.ACCESS_LEVEL_SUBSCRIBED;
var isCustomView = objConfig.ISCUSTOMVIEW;

var getUserCustomization = function(){
	var custom =new psm_custom_builder();
	custom.setCallbackHandler(handleCustomization);
	custom.setErrorHandler(handleAjaxError);
	custom.userAuthenticated();		

}
var handleCustomization = function(res){
	emptySelectBox('org1');
	emptySelectBox('org2');
	populateSelectBox('org1', res.Y.NOTSELECTED);
	populateSelectBox('org2', res.Y.SELECTED);

	emptySelectBox('goal1');
	emptySelectBox('goal2');
	populateSelectBox('goal1', res.X.NOTSELECTED);
	populateSelectBox('goal2', res.X.SELECTED);
}
var populateSelectBox = function (selectBox, data){
	for (i=0; i<data.length; i++) {
		addOption(selectBox, data[i].TEXT, data[i].VALUE);
	}	
}
var emptySelectBox = function(selectBox){
	var i;
	var selObj = document.getElementById(selectBox);
	for(i=selObj.options.length-1;i>=0;i--){
		selObj.remove(i);
	}
}
//adds options from a select box
var addOption = function(selectBox,text,value ){
	var selObj = document.getElementById(selectBox);
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selObj.options.add(optn);
}

var moveLeftMulti = function(){
	//moves 1st col on left to last col on right
	horizPos-=(gridColCount);
	if (horizPos<1){
		horizPos = 1;	
	}	
	changeContent();
	changeColHeaders();
}

var isCurrentlyLoading = 0;

var moveRightMulti = function(){
	if (!isCurrentlyLoading){
		//moves last col on right to 1st col on left
		horizPos+=(gridColCount);
		if (horizPos > (totalCols-gridColCount)) {
			horizPos = totalCols-gridColCount;	
		}
		isCurrentlyLoading = 1;
		changeContent();
		changeColHeaders();
	}
}																																																																																																																																																																																																																																																								

var displayLoadingBox = function(){
	var h = $(document).height();
	h = h/2;
	$("#loading").css({top: h, marginTop: "-50px" });
	$("#loading").show();	
}

var changeContent = function(){	
	//loading box
	displayLoadingBox();
	// create an instance of the proxy. 
	var e = new psm_crosswalk_nav();
	totalCols = e.getColCount();
	// Setting a callback handler for the proxy automatically makes
	// the proxy's calls asynchronous.
	e.setCallbackHandler(handleContent);
	e.setErrorHandler(handleAjaxError);
	e.getContent(horizPos);	
}

var changeViewChangeButton = function(){
	var lnk =document.getElementById('customizeLink_view');
	var txt = "Custom View";
	if(isCustomView){
		txt = "Standard View";
	}	
	lnk.innerHTML=txt;
}


function changeColHeaders(){
	displayLoadingBox();
	// create an instance of the proxy. 
	var e = new psm_crosswalk_nav();
	// Setting a callback handler for the proxy automatically makes
	// the proxy's calls asynchronous.
	e.setCallbackHandler(handleColHeaders);
	e.setErrorHandler(handleAjaxError);
	e.getColHeaders(horizPos);	
}

var handleColHeaders = function(res2){
	var rows=document.getElementById('tHead').rows;
	var cell=rows[0].cells;
	cell[1].innerHTML= formatColHeader(res2[0][0],res2[0][1]);
	cell[2].innerHTML= formatColHeader(res2[1][0],res2[1][1]);
	if (!sideBarOpen){
		cell[3].innerHTML= formatColHeader(res2[2][0],res2[2][1]);
		cell[4].innerHTML= formatColHeader(res2[3][0],res2[3][1]);
		cell[4].innerHTML+="<a href='javascript:moveRightMulti();' id='goal-right'>More Topics</a></th>";
	} else {
		cell[3].innerHTML= formatColHeader(res2[2][0],res2[2][1]);
		cell[3].innerHTML+="<a href='javascript:moveRightMulti();' id='goal-right'>More Topics</a></th>";
		cell[4].innerHTML= formatColHeader(res2[3][0],res2[3][1]);
	}
}

var formatColHeader = function(desc,subdesc){
	//formats the col header according to PSM rules - if the col header
	//	contains a "/" it split it to 2 lines and disregards the subdescription
	//	(if it exists)
	var n = desc.indexOf("/");
	var str = "";
	if (n > 0){
		var strDesc1 = desc.substring(0,n+1);
		var strDesc2 = desc.substring(n+1,desc.length);
		str = "<p>" + strDesc1 + "<br>" + strDesc2 + "</p>";
	} else {
		str = "<p>" + desc + "<em>" + subdesc + "</em></p>";
	}
	return str;
}

var buildTableRow = function(row,index) {
	row.appendChild(document.createElement('TH'));
	row.appendChild(document.createElement('TD'));
	row.appendChild(document.createElement('TD'));
	row.appendChild(document.createElement('TD'));
	row.appendChild(document.createElement('TD'));
	if(!(index % 2)){
		//add class 'odd' to row if applicable
		row.setAttribute('className','odd');
		row.setAttribute('class','odd');
	}	
}

//attaches eventhandlers and javascript functions based on 
//	user access level to each cell as it is created
var buildTableCell = function(res,cell) {	
	if (res.HASCONTENT){
		attachShowSideBarClickEvent(cell);
		if (access_level_subscribed.indexOf(access_level_user) >= 0) {
			cell.innerHTML="<a href='##' onClick='javascript:getDetail(\"" + res.ROW  + "\",\"" + res.COL + "\");'>" + res.CONTENT +"</a>";
		} 
		else {	
			cell.innerHTML="<a href='##' onClick='javascript:getDetailAfterLogin(\"" + res.ROW  + "\",\"" + res.COL + "\");'>" + res.CONTENT +"</a>";
		}
	}
	else {
		detachShowSideBarClickEvent(cell);
		cell.innerHTML=res.CONTENT;
	}
}

var attachEvents = function(obj) {
	if (obj.addEventListener){ 
		obj.addEventListener("click", showSidebar,false);
		
	} else if (obj.attachEvent) {
		obj.attachEvent ("onclick",showSidebar,false);
	}
	return obj;
}

var detachEvents = function(obj) {
	if (obj.removeEventListener){ 
		obj.removeEventListener("click", showSidebar,false);
	} else if (obj.attachEvent) {
		obj.detachEvent ("onclick",showSidebar);
	}
	return obj;
}

var attachShowSideBarClickEvent = function(obj) {
	/************************************************************************
	Attach click event that calls the showSideBar() function
	**************************************************************************
	*/
	if (obj.addEventListener){ 
		obj.addEventListener("click", showSidebar,false);
		
	} else if (obj.attachEvent) {
		obj.attachEvent ("onclick",showSidebar,false);
	}
	return obj;
}
var detachShowSideBarClickEvent = function(obj) {
	if (obj.removeEventListener){ 
		obj.removeEventListener("click", showSidebar,false);
	} else if (obj.attachEvent) {
		obj.detachEvent ("onclick",showSidebar);
	}
	return obj;
}
// Callback function to handle the results returned by the
// getEmployees function and populate the drop-down list.
var handleContent = function(res) {
	var count = document.getElementById('tBody').rows.length;
	var rows=document.getElementById('tBody').rows;

for ( var i=0; i<=res.length-1; i++ ){		
		if (i >= count){
			//Add tds if recordset being visualized has more records than the previous one
			// this happens when user goes from customized data subset to data superset
			//builds a new row with 5 cells
			var newRow=document.getElementById('tBody').insertRow(i);
			buildTableRow(newRow,i);
		}
		
		//1st cell is row header <TH>
		//set row id to the first cell in row 
		rows[i].setAttribute('id',res[i][0].TOPIC);
		var cell=rows[i].cells;		
		cell[0].innerHTML=res[i][0].CONTENT;
		//Loop through other cells - populate them and add/remove eventlisteners
		for ( var j=1; j<=cell.length-1; j++ ){
			buildTableCell(res[i][j],cell[j]);
		}
	}

	//removes extra tds if recordset being visualized has less records than the previous one
	//this happens when user goes from data superset to cusomtized dataset
	if (i < count){
		for ( var n=count-1; n>=i; n-- ){
			document.getElementById('tBody').deleteRow(n);
		}
	}

	$("#loading").hide();
	isCurrentlyLoading = 0;
	
	if (horizPos<=1){
		$("#goal-left").hide("slow");
	} else {
		$("#goal-left").show("slow");
	}

	var horizMaxPos = totalCols-gridColCount;
	if (horizPos >= (horizMaxPos)) {
		goalRightHideDisplay();
	} else {
		goalRightShowDisplay();
	}
}
var goalRightShowDisplay=function(){
	$("#goal-right").show("slow");
}
var goalRightHideDisplay=function(){
	$("#goal-right").hide("slow");
}
var toggleView=function(){
	cfc.setCustomView(isCustomView);	//session.grid_custom_view and rebuilds row and col arrays
	horizPos=1
	//update public var isCustom
	if(!isCustomView){
		isCustomView = 1;
	} else {
		isCustomView = 0;
	}
	loadCurrentView();
}
var closeCustomView=function(){
	$("#customize-container").hide();	
	$("#narrow_by").show();	
}
var loadCustomView=function(){
	isCustomView = 1;
	loadCurrentView();
}
var loadStandardView=function(){
	isCustomView = 0;
	loadCurrentView();
}
var loadCurrentView=function(){	
	$("#customize-container").hide();
	$("#narrow_by").show();	
	changeContent();
	changeColHeaders();	
	changeViewChangeButton();  //btnViewChange
}
var openEmailForm=function(id,col_topic){
	$("#email-form").show();
	$("#form_content_id").val(id);
	$("#form_col_topic_id").val(col_topic);
}
//used to poulate Detail window after login
var sRow = "";
var sCol = "";
var bDisplayDetail = 0;
//sets vars so that the code will know which Detail 
//	to display after a user has successfully logged on
//	This method is only called when a user is not yet logged in
var getDetailAfterLogin= function(row,col){
	sRow = row;
	sCol = col;
	bDisplayDetail = 1;
}

var getDetail = function(row,col){
	// create an instance of the proxy. 
	var e = new psm_crosswalk_nav();
	// Setting a callback handler for the proxy automatically makes
	// the proxy's calls asynchronous.
	e.setCallbackHandler(handleDetail);
	e.setErrorHandler(handleAjaxError);
	e.getDetail(row,col);
}

var handleDetail = function(res){
	var x = document.getElementById('crosswalk-detail-content');
	x.innerHTML="<h2>" + res.ROW_DESCRIPTION + "</h2>" +
				"<h3>" + res.COL_DESCRIPTION + "</h3>" + 
				"<p>" + res.BODY_SHORT + "</p>";
	if (res.BODY_LONG.length) {
		x.innerHTML += "<p>" + res.BODY_LONG + "</p>";
	}
	if (res.LINK.length) {
		x.innerHTML += "<p>Original Source:<br>" +
				 "<a target='_blank' href=\'" +  res.LINK+ "\'>" +
				"View the Full Regulation &raquo;</a></p>";	
	}
	x.innerHTML+="<br><br>";
	//change content_id in urls for print functionality
    document.getElementById('print').href = "/crosswalk-print/" + res.CONTENT_ID +"/" 
		+ res.COL_TOPIC;
	//change content_id in urls for email functionality
	var sEmailHref = "javascript:openEmailForm(" + res.CONTENT_ID + ",'" + res.COL_TOPIC + "')";
	document.getElementById('email').href = sEmailHref;

}
/*-----------------------*/

var cancelCustomerTopics = function(){
	var obj = new psm_custom_builder();
	obj.setCallbackHandler(handleCancelSuccess);
	obj.setErrorHandler(handleAjaxError);
	obj.cancelCustomerTopics();
}

var saveCustomerTopics = function(){
	selectedItems = getSelectBoxItems();
	// create an instance of the proxy. 
	var obj = new psm_custom_builder();
	obj.setCallbackHandler(handleSaveSuccess);
	obj.setErrorHandler(handleAjaxError);
	obj.saveCustomerTopics(selectedItems);
}
//Gets all selected items in a org2 and goal2 select boxes
var getSelectBoxItems = function(){
	var selArray = new Array();
	var selObj_Y = document.getElementById('org2');
	var selObj_X = document.getElementById('goal2');
	var i;
	var count = 0;
	for (i=0; i<selObj_Y.options.length; i++) {
		selArray[count] = selObj_Y.options[i].value;
		count++;
	}	
	for (i=0; i<selObj_X.options.length; i++) {
		selArray[count] = selObj_X.options[i].value;
		count++;
	}	
	return selArray;
}

var handleSaveSuccess = function(res){
	repopulateSelectBoxes(res);
	
	if(!isCustomView){
		cfc.setCustomView();//called from crosswalk.js
	} 
	loadCustomView();
	$("#narrow_by").show();
}
var handleCancelSuccess = function(res){
	//alert('handleCancelSuccess');
	repopulateSelectBoxes(res);
	$("#customize-container").hide();
	$("#narrow_by").show();	
}
var repopulateSelectBoxes = function(res){
	//removeAllOptions('org1');
	removeAllOptions('org2');
	for (i=0; i<res.Y.SELECTED.length; i++) {
		addOption('org2', res.Y.SELECTED[i].TEXT, res.Y.SELECTED[i].VALUE);
	}
	for (i=0; i<res.Y.NOTSELECTED.length; i++) {
		addOption('org1', res.Y.NOTSELECTED[i].TEXT, res.Y.NOTSELECTED[i].VALUE);
	}	

	removeAllOptions('goal1');
	removeAllOptions('goal2');

	for (i=0; i<res.X.SELECTED.length; i++) {
		addOption('goal2', res.X.SELECTED[i].TEXT, res.X.SELECTED[i].VALUE);
	}
	for (i=0; i<res.X.NOTSELECTED.length; i++) {
		addOption('goal1', res.X.NOTSELECTED[i].TEXT, res.X.NOTSELECTED[i].VALUE);
	}		
}

//clears options from a select box
var removeAllOptions = function(selectBox){	
	var i;
	var selObj = document.getElementById(selectBox);
	for(i=selObj.options.length-1;i>=0;i--){
		selObj.remove(i);
	}
}

//adds options from a select box
var addOption = function(selectBox,text,value ){
	var selObj = document.getElementById(selectBox);
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selObj.options.add(optn);
}