/********************************************************************
*-------------------------------------------------------------------
* Licensed Materials - Property of Shared Vision Group
*
* WebSphere Commerce
*
* (c) Copyright IBM Corp. 2007
*
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
*-------------------------------------------------------------------*/

/************************************************************
*
* Search Result Flow
*
*********************************************************************/

dojo.require("dojo.lfx.*");
dojo.require("dojo.undo.browser");
     
ProductReviewFlowJS={
		ajaxURL : "UNDEFINED",	
		catalogSearchResultURL : "UNDEFINED",
		categoryName : "UNDEFINED",
		initialURL : "UNDEFINED"		
}

		
var waiting = false;
var loaded= false;
function openReviewProfile(divId, contentId){
    var content = dojo.widget.byId(divId);
    content.show();
    var htmlContent = dojo.byId(contentId);
    content.setContent('');
    if(htmlContent) 
    	content.setContent(htmlContent.innerHTML);
	return false;
}
//id = id to add to N param
//widget = the widget that is making the request
//ind = add/remove switch for id (1=add, 0=remove);
function refreshHandler(widget,url)
{

	waiting=true;
	cursor_wait();
	dojo.lfx.fadeOut('searchResultsDiv', 800).play();
	var pURL="";
	
		 
		 
	//unescape all url's
	url = unescape(url);
	
	if (url.indexOf("ReviewEngineDisplay") == -1)
		 pURL =  ProductReviewFlowJS.ajaxURL +'&'+ url;	
	
	if (pURL.indexOf("?") == -1)
			pURL = pURL+"?" ;
	
	
	//if this is a category page- refresh the category
	while (pURL.indexOf('&amp;') > -1)
			pURL = pURL.replace('&amp;','&');
		
	
		  
	
		
	
		if (pURL.indexOf("fromWidget") == -1)
			pURL = pURL + "&fromWidget="+widget;	

		
		
		
	// alert(pURL);
	   
	    
	   
		dojo.byId('queryString').innerHTML = pURL;
	    
	
	    // get the content div inside the popup
	   var content = dojo.widget.byId('navEngine');   
	   
	   content.setUrl(pURL);  
	   cursor_clear(); 
	   return;
  	
   
  
}  	 

  

function updateBreadCrumbList(breadCrumbArray,selectedURLArray,selectedIdArray,selectedDisplayNameArray)
{
	var bcDiv ="";;

	for (x in breadCrumbArray)
	{
	   
	   
		 bcDiv = bcDiv+breadCrumbArray[x];	
		 //update the url's for all that are current selected
		//alert(selectedTypeArray[x]);
		if (selectedTypeArray[x] == 'showonly')
		{
			//alert(selectedIdArray[x]);
			if (dojo.byId(selectedIdArray[x]))
				attachCheckBoxEvent2(selectedURLArray[x],dojo.byId(selectedIdArray[x]));			
		} else if (selectedTypeArray[x] == 'starRating')
		{
			//update the histogram to 100%
			if (dojo.byId(selectedIdArray[x]+'_img'))
			{
				attachLinkEvent(selectedURLArray[x],dojo.byId(selectedIdArray[x]+'_img'));
				dojo.byId(selectedIdArray[x]+'_img').style.width = "0px";
				dojo.byId(selectedIdArray[x]+'_img').style.padding = "0px 0px 0px 80px";
				//dojo.byId(selectedIdArray[x]+"_display").innerHTML = "0";
				
			}
		} else if (selectedTypeArray[x] == 'checkbox')
		 {
		 		attachLinkEvent(selectedURLArray[x],dojo.byId(selectedIdArray[x]));	
		 	
		 	//update displayname count if applicable
		 //	if (selectedDisplayNameArray[x])
		 //		dojo.byId(selectedIdArray[x]).innerHTML = selectedDisplayNameArray[x];
		}
		 	
		 		 	
		 
	}
	//alert("done");
	  
	
	dojo.byId("breadCrumbDiv").innerHTML = bcDiv;

					 
	
}
//need this in a separate function for IE
function attachLinkEvent(aURL,thisDiv) {
    thisDiv.onclick = function() {
        checkBoxSelect(aURL,thisDiv);
        return false;
    };   
}


//need this in a separate function for IE
function attachDisabledLinkEvent(thisDiv) {
    thisDiv.onclick = function() {
    	doNothing(); 
    	return false;
    };   
   
}


function attachCheckBoxEvent2(aURL,thisDiv) {
    thisDiv.onclick = function() {      
        showOnlySelect(aURL,thisDiv);        
    };      
    thisDiv.disabled = false;     
}  
function attachShowOnlyEventDisabled(thisDiv) {
    thisDiv.disabled = true;       
    return false;           
}

function updateSearchResults(results) 
{
	//dojo.html.setDisplay('searchResultsDiv',false);
	//alert("updateSearchResults");
	if (dojo.byId('searchResultsDiv'))
	{
		dojo.lfx.fadeIn('searchResultsDiv', 800).play();
		  
 	}
 	
 	dojo.byId('searchResultsDiv').innerHTML = results;
}  
 
function updateCustyReviewResults(results) 
{
//	alert("updateCustyReviewResults");
	if (dojo.byId('searchCustyResultsDiv'))
	{
	dojo.lfx.fadeIn('searchCustyResultsDiv', 600).play();
	
 	}
 	dojo.byId('searchCustyResultsDiv').innerHTML = results;  
}        
    
function updateSearchTotal(total)  
{
//alert("updateSearchTotal");
  dojo.byId('search-results-count').innerHTML = "Your search returned "+total+" results.";
}

function updateClearAllLinks(categoryURL, brandURL)
{
	//alert("updateClearAllLinks");
	if (dojo.byId('categoryClear'))
		dojo.byId('categoryClear').href = "javascript:clearThisWidget('"+categoryURL+"')";

	if (dojo.byId('brandClear'))
		dojo.byId('brandClear').href = "javascript:clearThisWidget('"+brandURL+"')";
}

function updateTotalSearchResults(results,showallurl) 
{
  // alert(results);
  //update show all url
  //alert("updateTotalSearchResults");
  	dojo.byId('ShowAllURLDiv').innerHTML = showallurl;
    
 	dojo.byId('searchTotalResultsDiv').innerHTML = results; 


// 	alert("y");
}

function updateSortListResults(results) 
{

    dojo.byId('updateSortListResultsDiv').innerHTML = results;   
} 

function updatePaginationSet(results) 
{

	//  alert("updatePaginationSet");
	//do not display just a 1 for one page
	if (results == "1")
		results ="";
 	dojo.byId('paginationResultsDiv').innerHTML = results;  

}


function updateQueryString(qs)
{
	dojo.byId('queryString').innerHTML = qs;
}


function updateGenderWidget(genderIdArray,genderURLArray,genderDisplayArray)
{

  if (dojo.byId('ReviewerGenderDiv'))
  {
	var elem = document.getElementById('ReviewerGenderDiv').getElementsByTagName('div');	
	
	var currSel = dojo.byId('currentSelections').innerHTML;

	for(var i=0;i<elem.length;i++){
	
		var e = elem[i];		
		var keepEnabled = false;
		var listId = e.getAttribute("id");		
		
		var objId = dojo.byId(listId);
		
		for (x in genderIdArray)
		{		    
			var updateId = genderIdArray[x];
			var updateobjId = dojo.byId(updateId);
			var updateURL = genderURLArray[x];
			var updateDisplay = genderDisplayArray[x];
			if (updateId == listId)
			{
				//keep clickable
				keepEnabled = true;
				//update display and enable				
				dojo.byId(updateId).innerHTML = updateDisplay;
				attachLinkEvent(updateURL,dojo.byId(updateId));
				dojo.byId(updateId).className = "svg_HtmlCheckbox svg_HtmlCheckboxOff";				
			} 
		
		}
	
		//disable the checkbox if not returned in the results
		if (!keepEnabled)
		{
			//if this is a current selection do not disabled it.	
			if (currSel.indexOf(listId) == -1)
			{ 
			 
				var disableThis = objId.innerHTML;
				var removeChar = disableThis.indexOf("(");					
				attachLinkEvent('javascript:;',dojo.byId(listId));
				objId.className ="svg_HtmlCheckbox svg_HtmlCheckboxDisabledOff";				
				if (removeChar != -1)
					objId.innerHTML = disableThis.substring(0,removeChar); 
				
			}
		} 
		
		
	}
  } //end if ReviewerGenderDiv does not exist
 
}



function updateReviewerAgeWidget(idArray,URLArray,displayArray)
{

  if (dojo.byId('ReviewerAgeDiv'))
  {
	var elem = document.getElementById('ReviewerAgeDiv').getElementsByTagName('div');	
	
	var currSel = dojo.byId('currentSelections').innerHTML;

	for(var i=0;i<elem.length;i++){
	
		var e = elem[i];		
		var keepEnabled = false;
		var listId = e.getAttribute("id");		
		
		var objId = dojo.byId(listId);
		
		for (x in idArray)
		{		    
			var updateId = idArray[x];
			var updateobjId = dojo.byId(updateId);
			var updateURL = URLArray[x];
			var updateDisplay = displayArray[x];
			if (updateId == listId)
			{
				//keep clickable
				keepEnabled = true;
				//update display and enable				
				dojo.byId(updateId).innerHTML = updateDisplay;
				attachLinkEvent(updateURL,dojo.byId(updateId));
				dojo.byId(updateId).className = "svg_HtmlCheckbox svg_HtmlCheckboxOff";				
			} 
		
		}
	
		//disable the checkbox if not returned in the results
		if (!keepEnabled)
		{
			//if this is a current selection do not disabled it.	
			if (currSel.indexOf(listId) == -1)
			{ 
			 
				var disableThis = objId.innerHTML;
				var removeChar = disableThis.indexOf("(");					
				attachLinkEvent('javascript:;',dojo.byId(listId));
				objId.className ="svg_HtmlCheckbox svg_HtmlCheckboxDisabledOff";				
				if (removeChar != -1)
					objId.innerHTML = disableThis.substring(0,removeChar); 
				
			}
		} 
		
		
	}
  } //end if ReviewerAgeDiv does not exist
 
}


function updateReviewerExperienceLevelWidget(idArray,URLArray,displayArray)
{

  if (dojo.byId('ExpLevelDiv'))
  {
	var elem = document.getElementById('ExpLevelDiv').getElementsByTagName('div');	
	
	var currSel = dojo.byId('currentSelections').innerHTML;

	for(var i=0;i<elem.length;i++){
	
		var e = elem[i];		
		var keepEnabled = false;
		var listId = e.getAttribute("id");		
		
		var objId = dojo.byId(listId);
		
		for (x in idArray)
		{		    
			var updateId = idArray[x];
			var updateobjId = dojo.byId(updateId);
			var updateURL = URLArray[x];
			var updateDisplay = displayArray[x];
			if (updateId == listId)
			{
				//keep clickable
				keepEnabled = true;
				//update display and enable				
				dojo.byId(updateId).innerHTML = updateDisplay;
				attachLinkEvent(updateURL,dojo.byId(updateId));
				dojo.byId(updateId).className = "svg_HtmlCheckbox svg_HtmlCheckboxOff";				
			} 
		
		}
	
		//disable the checkbox if not returned in the results
		if (!keepEnabled)
		{
			//if this is a current selection do not disabled it.	
			if (currSel.indexOf(listId) == -1)
			{ 
			 
				var disableThis = objId.innerHTML;
				var removeChar = disableThis.indexOf("(");					
				attachLinkEvent('javascript:;',dojo.byId(listId));
				objId.className ="svg_HtmlCheckbox svg_HtmlCheckboxDisabledOff";				
				if (removeChar != -1)
					objId.innerHTML = disableThis.substring(0,removeChar); 
				
			}
		} 
		
		
	}
  } //end if ExpLevelDiv does not exist
 
}


function updateStarRatingWidget(idArray,URLArray,displayArray,valueArray,remainderArray)
{

  if (dojo.byId('StarRatingDiv'))
  {
	var elem = document.getElementById('StarRatingDiv').getElementsByTagName('img');	
	
	var currSel = dojo.byId('currentSelections').innerHTML;

	for(var i=0;i<elem.length;i++){
	
		var e = elem[i];		
		var keepEnabled = false;
		var listId = e.getAttribute("id");		
		
		var objId = dojo.byId(listId);
		
		for (x in idArray)
		{		    
			var updateId = idArray[x]+"_img";
			var updateIdDisp = idArray[x]+"_display";
			var updateobjId = dojo.byId(updateId);
			var updateURL = URLArray[x];
			var updateDisplay = displayArray[x];
			var updateValue = valueArray[x];
			var updateRemainder= remainderArray[x];
			
			
			if (updateId == listId)
			{
				//keep clickable
				keepEnabled = true;
				//alert(updateURL);
				//update display and enable			
				dojo.byId(updateId).style.width = updateRemainder+"px";
				dojo.byId(listId).style.padding = "0px 0px 0px "+updateValue+"px";		
				if (updateURL == '')
				{
					attachDisabledLinkEvent(dojo.byId(updateId));
					dojo.byId(listId).className = "";
				} else 
				{
					dojo.byId(listId).className = "svg_pointer";		
					attachLinkEvent(updateURL,dojo.byId(updateId));
				}
				dojo.byId(updateIdDisp).innerHTML = updateDisplay;
				//dojo.byId(updateId).className = "svg_HtmlCheckbox svg_HtmlCheckboxOff";				
			} 
		
		}
	
		//disable the checkbox if not returned in the results
		if (!keepEnabled)
		{
			//if this is a current selection do not disabled it.	
			//alert(listId);
			//alert("curr-"+currSel);
			if (currSel.indexOf(listId.substring(0,listId.length-4)) == -1)
			{ 
			 

				attachDisabledLinkEvent(dojo.byId(listId));
				dojo.byId(listId).style.width = "80px";
				dojo.byId(listId).style.padding = "0px 0px 0px 0px";
				dojo.byId(listId).className = "";
				dojo.byId(listId.substring(0,listId.length-4)+"_display").innerHTML = "0";
				
				//objId.className ="svg_HtmlCheckbox svg_HtmlCheckboxDisabledOff";		
				
			}
		} 
		
		
	}
  } //end if ExpLevelDiv does not exist
 
}

function updateShowOnlyWidget(showOnlyIdArray,showOnlyURLArray,showOnlyDisplayArray)
{

     
     
	var elem = document.getElementById('ShowOnlyDiv').getElementsByTagName('input');	
	
	var currSel = dojo.byId('currentSelections').innerHTML;

	for(var i=0;i<elem.length;i++){
	
		var e = elem[i];		
		var keepEnabled = false;
		var listId = e.getAttribute("id");		
		if (listId != null)
		{
			var objId = dojo.byId(listId);
		
			for (x in showOnlyIdArray)
			{		    
				var updateId = showOnlyIdArray[x];
				var updateobjId = dojo.byId(updateId);
				var updateURL = showOnlyURLArray[x];
				var updateDisplay = showOnlyDisplayArray[x];
				
				if (updateId == listId)
				{
				
				
					//keep clickable
					keepEnabled = true;
					//update display and enable		
					
					attachCheckBoxEvent2(updateURL,updateobjId);
				//	dojo.byId(updateId).disabled = false;
					//alert("re-enble");
					break;					
				} 
			
			}
		
			//disable the checkbox if not returned in the results
			if (!keepEnabled)
			{
			    
				//if this is a current selection do not disabled it.	
				
				if (currSel.indexOf(listId) == -1)
				{ 			
											
					attachShowOnlyEventDisabled(dojo.byId(listId));
														
					
				} 
			} 
		}
		
		
	}
  
 
}

 
function doNothing()
{

  	return;
}
function wipeWidget(elId){
			
	if (dojo.html.isShowing(elId))
	{
		dojo.lfx.wipeOut(elId, 300).play();
	} 
		else
	{
		dojo.lfx.wipeIn(elId, 300).play();
	}				
}	
function clearThisWidget(url)
{
	if (!waiting && loaded)
	{
		waiting = true;	
 		refreshHandler("clearwidget",url);
 	}
}
function checkBoxSelect(url,obj)
{
	
	if (!waiting && loaded)
	{
		waiting = true;
		var thismenu = dojo.byId(obj);

	 	var thisclass = thismenu.className;
	 	
	 	if (thisclass == 'svg_HtmlCheckbox svg_HtmlCheckboxOff')
	 	{
	 		thismenu.className = 'svg_HtmlCheckbox svg_HtmlCheckboxOn';
	 	} 
	 	else if (thisclass == 'svg_HtmlCheckbox svg_HtmlCheckboxOn')
	 	{
	 		thismenu.className = 'svg_HtmlCheckbox svg_HtmlCheckboxOff';
	 	} else
	 	{
	 		//do nothing
	 	}
 	    
 		//refresh the engine	
 		 thisjob = setTimeout("refreshHandler('check','"+url+"')",1);
 		
 		
 	}

} 


    
function removeFromSearch(url,id)
{	         
 				
   
 	if (!waiting && loaded)
    {
    	
	    var updateId = unescape(id);
	    //alert(updateId);
	    //alert(dojo.byId(id).checked);
	    if (dojo.byId(id) && dojo.byId(id).checked)
	    {
		    if (dojo.byId(id).checked)
		    	dojo.byId(id).checked = false;
		    else
		    	dojo.byId(id).checked = true;
	    }
 		refreshHandler("remove",url);
 	}
 				
}

function updateCurrentSelections(str)
{
	dojo.byId("currentSelections").innerHTML = str;

} 

function updateWaitBackStatus()
{
	//set back status...

	waiting = false;


}

function cursor_hand() {

	document.body.style.cursor = 'pointer';
	document.body.style.cursor = 'hand'; 
}  

function cursor_clearer() {

	document.body.style.cursor = 'default';		
} 

function displayAllCount(){
	 
   if (!waiting && loaded)
   {
		var url = dojo.byId('ShowAllURLDiv').innerHTML;
		while (url.indexOf('&amp;') > -1)
			url = url.replace('&amp;','&');
		
	 	refreshHandler("searchSize",url);
 	}
  
}
function refreshDisplayCount(fromThis)
{
	
  if (!waiting && loaded)
   {
	var selectBox = fromThis;
	var user_input = selectBox.options[selectBox.selectedIndex].value

 	refreshHandler("searchSize",user_input);
 }

}
function refreshSortList(fromThis)   
{
if (!waiting && loaded)
   {
	var selectBox = fromThis;
	var user_input = selectBox.options[selectBox.selectedIndex].value

 	refreshHandler("sortList",user_input);
 	}

}
function showOnlySelect(url,thisBox)
{
  
  if (!waiting && loaded)
  {
  
   refreshHandler("showOnlySelect",url);
	
  } else
  {
   if (dojo.byId(thisBox).checked)
    	dojo.byId(thisBox).checked = false;
    else
    	dojo.byId(thisBox).checked = true;    
    	
   }

	

} 

function refreshPagination(url)
{ 
	if (!waiting && loaded)
		refreshHandler("pagination",url);
	return;
} 


var pane = null;

var myLoadMessage = {
		show: function(event){
			event.preventDefault();			
		}
	}

// addOnLoad closure, so we dont have to call dojo.widget.byId multiple times
dojo.addOnLoad(function(){
    pane = dojo.widget.byId("navEngine");
    dojo.event.connect(pane, "onDownloadStart", myLoadMessage, "show");   
   
});


// historyTracker for back/forward info
function historyTracker(data){
    //alert("storing: "+(data.xml||data));
    this.data = data;
}
historyTracker.prototype.back = function(){
 //   alert("back: "+(this.data.xml||this.data));
    pane.setContent(this.data, true);
}
historyTracker.prototype.forward = function(){
  //  alert("forward: "+(this.data.xml||this.data));
    pane.setContent(this.data, true);
}

// push setContent calls to history
function pushToHistory(data, dontLog){
    // dont log the calls made by historyTracker
 //   alert(dontLog);
    if(dontLog){
        return;
    }
    // store data in a new history object
    var histObj = new historyTracker(data);
    dojo.undo.browser.addToHistory(histObj);
}

function dumpData( type, data, evt ) {
    var msg = "Data dump:\n" + data;
    //alert( msg );
     var histObj = new historyTracker(data);
     dojo.undo.browser.setInitialState(histObj);
}

dojo.addOnLoad(function(){
if (ProductReviewFlowJS.initialURL != 'UNDEFINED')
{
	dojo.io.bind({
	    url: ProductReviewFlowJS.initialURL,
	    load: dumpData,
	    mimetype: "text/html"
	});
}
dojo.html.setVisibility('searchResultsDiv', true);
loaded=true;
		});
