// ----------------------------------------------------------
// Recommend.js
//
// Author        : Thomas E. George
// Creation Date : March 5, 2009
//
// History       : 03/05/2009 - TEG - created
//              
// ----------------------------------------------------------

var xmlHttp = null;

// user agent
var _uagtre_ = navigator.userAgent.toLowerCase();

var iconElem    = null;
var iconAction1 = null;
var iconAction2 = null;
var explElem    = null;
var explPID     = null;
var fblElem     = null;
var fblPID      = null;
var fblCustID   = null;
var ajaxAction  = null;
var ajaxCursorElem   = null;
var ajaxCursor       = null;
var ajaxCursDocB     = null;
function ShowElem(elem)
{
	elem.style.visibility = "visible";
	elem.style.display    = "inline";
}

function HideElem(elem)
{
	elem.style.visibility = "hidden";
	elem.style.display    = "none";
}

// e.g., tagName = "a", subStr = "#ajax"
function FixFallbackElements(tagName, subStr)
{
	var rc = false;
	
	try
	{
		if(_bActivateRA && (tagName != "") && (subStr != ""))
		{
			var elems = document.getElementsByTagName(tagName);

			if(elems != null)
			{
				var elem = null;
				var href = null;
				//var className = null;
				
				for(var i = 0; i < elems.length; i++)
				{
					elem = elems[i];
					href = elem.href;
					//className = elem.className;
					
					if(href && (href != null))
					{
						if(href.indexOf(subStr) >= 0)
						{
							elem.href = subStr;
							rc = true;
						}
					}
				}
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "FixFallbackElements()");
	}
	finally
	{
	}
	
	return rc;
}

function initAJAXReco()
{
	try
	{
		if(_bActivateRA)
		{
			var rc = FixFallbackElements("a", "#ajax");
		}
	}
	catch(e)
	{
	    ProcessException(e, "initAJAXReco()");
	}
	finally
	{
	}
}

function doRecommend(element, PID, custID, action1, action2)
{
	var rc = true;
	
	try
	{
		iconElem    = null;
		iconAction1 = null;
		iconAction2 = null;
		
		if(_bActivateRA)
		{
			if((element != null) && (PID != null) && (custID != null) && (action1 != null) && (action2 != null))
			{	
				iconElem    = element;
				iconAction1 = action1;
				iconAction2 = action2;
				
				var eID = iconElem.id;
				//var ePID = eID.replace(/re/gi, "");
				
				//createXMLHttpRequest();
				xmlHttp = CreateXMLHttpRequestObject();
					
				if(xmlHttp != null)
				{
					// show wait cursor
					toggleCursor(element, 'wait', true); //element
					
					xmlHttp.onreadystatechange = callbackRE;
    
					var url = "/AjaxServer.asp?PID=" + escape(PID) + "&CustID=" + escape(custID) + "&Action=" + escape(action1);
						        
					xmlHttp.open("GET", url, true);
					xmlHttp.send(null);
					
					rc = false;
				}
				else
				{
					ProcessException(null, "Recommend.js::doRecommend()", "CreateXMLHttpRequestObject() failed --- Cannot process.");
				}
			}
			else
			{
				ProcessException(null, "Recommend.js::doRecommend()", "1 or more invalid passed-in parameters --- Cannot process.");
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "doRecommend()");
	}
	finally
	{
	}
	
	return rc;
}

function doRecoExpl(element, PID, custID, config, action)
{
	var rc = true;
	
	try
	{
		if(_bActivateRA)
		{
			if((element != null) && (PID != null) && (custID != null) && (config != null) && (action != null))
			{	
				explElem   = element;
				explPID    = PID;
				ajaxAction = action;
				
				var eID = explElem.id;
				//var ePID = eID.replace(/x/gi, "");
				//var ePID = ePID.replace(/re/gi, "");
				
				//createXMLHttpRequest();
				xmlHttp = CreateXMLHttpRequestObject();
					
				if(xmlHttp != null)
				{
					// show wait cursor
					toggleCursor(element, 'wait', true); //element
					
					xmlHttp.onreadystatechange = callbackREX;
    
					var url = "/AjaxServer.asp?PID=" + escape(PID) + "&CustID=" + escape(custID) + "&Config=" + escape(config) + "&Action=" + escape(action);
						        
					xmlHttp.open("GET", url, true);
					xmlHttp.send(null);
					
					rc = false;
				}
				else
				{
					ProcessException(null, "Recommend.js::doRecoExpl()", "CreateXMLHttpRequestObject() failed --- Cannot process.");
				}
			}
			else
			{
				ProcessException(null, "Recommend.js::doRecoExpl()", "1 or more invalid passed-in parameters --- Cannot process.");
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "doRecoExpl()");
	}
	finally
	{
	}
	
	return rc;
}

function doRecoFBL(element, PID, custID, feedBackCode, action)
{
	var rc = true;
	
	try
	{
		if(_bActivateRA)
		{
			if((element != null) && (PID != null) && (custID != null) && (feedBackCode != null) && (action != null))
			{	
				fblElem    = element;
				fblPID     = PID;
				fblCustID  = custID;
				ajaxAction = action;
				
				var eID = fblElem.id;
				//var ePID = eID.replace(/o/gi, "");
				//var ePID = eID.replace(/n/gi, "");
				//var ePID = ePID.replace(/re/gi, "");
				
				//createXMLHttpRequest();
				xmlHttp = CreateXMLHttpRequestObject();
					
				if(xmlHttp != null)
				{
					// show wait cursor
					toggleCursor(element, 'wait', true); //element
					
					xmlHttp.onreadystatechange = callbackREFB;
    
					var url = "/AjaxServer.asp?PID=" + escape(PID) + "&CustID=" + escape(custID) + "&FBC=" + escape(feedBackCode) + "&Action=" + escape(action);
						        
					xmlHttp.open("GET", url, true);
					xmlHttp.send(null);
					
					rc = false;
				}
				else
				{
					ProcessException(null, "Recommend.js::doRecoFBL()", "CreateXMLHttpRequestObject() failed --- Cannot process.");
				}
			}
			else
			{
				ProcessException(null, "Recommend.js::doRecoFBL()", "1 or more invalid passed-in parameters --- Cannot process.");
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "doRecoFBL()");
	}
	finally
	{
	}
	
	return rc;
}

function callbackRE()
{
	try
	{
		if(xmlHttp != null)
		{
			if(xmlHttp.readyState == _nRequestReadyState_Complete_)
			{
			    if(xmlHttp.status == _nHttpServerStatus_OK_)
			    {
					var sContentType = "";
					
					if(xmlHttp.responseText.toLowerCase().indexOf(_sXmlDocTag) >= 0)
					{
						sContentType = "text/xml";
					}
					else
					{
						sContentType = "text/html";
					}
					
					if(sContentType == "text/xml")
					{
						var xmlResults = null;
						
						if(reParseXMLText(xmlHttp))
						{
							var sXHRR = cleanXMLDocText(xmlHttp.responseText);
							
							xmlResults = ParseXMLTextToXMLDoc(sXHRR);
						}
						else
						{
							xmlResults = xmlHttp.responseXML;
						}
	
						toggleRecommend(xmlResults);
			        }
			    }
						
				// remove wait cursor
				toggleCursor(ajaxCursorElem, null, true); //element
			}
		}
		else
		{
			ProcessException(null, "Recommend.js::callbackRE()", "xmlHttp is null - CreateXMLHttpRequestObject() failed --- Cannot process.");
		}
	}
	catch(e)
	{
	    ProcessException(e, "callbackRE()");
	}
	finally
	{
	}
}
        
function toggleRecommend(xmlResults)
{
	var rc = false;
	
	try
	{
		if(xmlResults != null)
		{
			if((iconElem != null) && (iconAction2 != null))
			{
				var PID      = xmlResults.getElementsByTagName("PID")[0].firstChild.data;
				var CustID   = xmlResults.getElementsByTagName("CustID")[0].firstChild.data;
				var IconPath = xmlResults.getElementsByTagName("IconPath")[0].firstChild.data;
				var Action   = xmlResults.getElementsByTagName("Action")[0].firstChild.data;
				
				if(IconPath.length > 0)
				{        
					// icon
					if(iconElem.src)
					{
						iconElem.src = IconPath;
					}
					else
					{
						iconElem.innerHTML = ((IconPath == "images/icon-mlt.gif") ? "Include" : "Exclude");
					}
					iconElem.onclick = function() {doRecommend(this, PID, CustID, iconAction2, iconAction1);}
					
					// NO GOOD in IE (works in Firefox and K-Meleion)
					//iconElem.setAttribute("onclick", "doRecommend(this, " + PID + ", " + CustID + ", '" +  iconAction2 + "', '" + iconAction1 + "')");
					
					// other elements
					var exrSpanElem = document.getElementById("exrSpan" + PID + "re"); // <span>
					showHideSpan(exrSpanElem, (Action == 'RECOINCL'));
					
					var babSpanElem = document.getElementById("babSpan" + PID + "re"); // <span>
					showHideSpan(babSpanElem, (Action != 'RECOINCL'));
										
					rc = true;
				}
			}
			else
			{
				ProcessException(null, "Recommend.js::toggleRecommend()", "Invalid 'iconElem' and/or 'iconAction2' --- Cannot process.");
			}
		}
		else
		{
			ProcessException(null, "Recommend.js::toggleRecommend()", "Invalid passed-in parameter 'xmlResults' --- Cannot process.");
		}
	}
	catch(e)
	{
	    ProcessException(e, "toggleRecommend()");
	}
	finally
	{
	}
	
	return rc;
}

function changeCursor(element, cursorName)
{
	try
	{
		if((element != null) && (cursorName != null) && (cursorName.length > 0))
		{
			//alert(element.style.cursor);
				
			//if((_uagtre_.indexOf("opera") >= 0) && (cursorName.indexOf("pointer") = 0)) //indexOf throws exception here in opera 9.64
			//if(((String(_uagtre_)).indexOf("opera") >= 0) && ((String(cursorName)).indexOf("pointer") = 0)) //indexOf throws exception here in opera 9.64
			if(_uagtre_.toLowerCase().substring(0, 5) == "opera") //&& (cursorName.toLowerCase() == "pointer"))
			{
				//alert("opera pointer");
				//var body = document.getElementsByTagName("body");
				//if(body != null)
				//{
				//}

				element.style.cursor = cursorName; //not working here in opera 9.64 (none of this works) 
				
				//if(cursorName.toLowerCase() == "pointer")
				//{
				//	cursorName = "hand";
				//}
				
				//iconElem = element;
				//setTimeout("'" + "iconElem.style.cursor=" + cursorName + "'", 5); // delay it for the likes of Opera
				//setTimeout("'" + 'element.setAttribute("style", "cursor:' + cursorName + '")' + "'", 5);
			}
			else
			{
				element.style.cursor = cursorName;
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "changeCursor()");
	}
	finally
	{
	}
}

function toggleCursor(element, cursorName, doDocBody)
{
	try
	{
		if(element != null)
		{
			if((cursorName != null) && (cursorName.length > 0))
			{
				ajaxCursorElem       = element;
				ajaxCursor           = element.style.cursor; 
				element.style.cursor = cursorName;
				
				if(doDocBody && (document.body != null))
				{
					ajaxCursDocB               = document.body.style.cursor;
					document.body.style.cursor = cursorName;
				}
				else
				{
					ajaxCursDocB = null;
				}
			}
			else
			{
				if(ajaxCursor != null)
				{
					element.style.cursor = ajaxCursor;
					ajaxCursor           = null;
				}
				
				if(doDocBody && (ajaxCursDocB != null))
				{
					document.body.style.cursor = ajaxCursDocB;
					ajaxCursDocB               = null;
				}
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "toggleCursor()");
	}
	finally
	{
	}
}

function showHideSpan(element, bHide)
{
	try
	{
		if(element != null)
		{
			var sVisVal = '';
			if(bHide)
			{
				// hidden
				if(!_bStyleDispBNRA)
				{
					sVisVal = 'hidden';
				}
				else
				{
					sVisVal = 'none';
				}
			}
			else
			{
				// visible
				if(!_bStyleDispBNRA)
				{
					if(_isIE)
					{
						sVisVal = 'visible';
					}
					else
					{
						sVisVal = ''; // visible
					}
				}
				else
				{
					sVisVal = 'block';
				}
			}
						
			if(!_bStyleDispBNRA)
			{
				if(_isIE)
				{
					element.style.visibility = sVisVal;
				}
				else
				{
					element.style.setProperty('visibility', sVisVal, null);
				}
			}
			else
			{
				if(_isIE)
				{
					element.style.visibility = 'visible';
				}
				else
				{
					element.style.setProperty('visibility', '', null);
				}
				element.style.display = sVisVal;
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "showHideElem()");
	}
	finally
	{
	}
}

function callbackREX()
{
	try
	{
		if(xmlHttp != null)
		{
			if(xmlHttp.readyState == _nRequestReadyState_Complete_)
			{
			    if(xmlHttp.status == _nHttpServerStatus_OK_)
			    {
					var sContentType = "";
					
					if(xmlHttp.responseText.toLowerCase().indexOf(_sXmlDocTag) >= 0)
					{
						sContentType = "text/xml";
					}
					else
					{
						sContentType = "text/html";
					}
					
					if(sContentType == "text/xml")
					{
						var xmlResults = null;
						
						if(reParseXMLText(xmlHttp))
						{
							var sXHRR = cleanXMLDocText(xmlHttp.responseText);
							
							xmlResults = ParseXMLTextToXMLDoc(sXHRR);
						}
						else
						{
							xmlResults = xmlHttp.responseXML;
						}
	
						toggleShowExplReco(xmlResults);
			        }
			    }
						
				// remove wait cursor
				toggleCursor(ajaxCursorElem, null, true); //element
			}
		}
		else
		{
			ProcessException(null, "Recommend.js::callbackREX()", "xmlHttp is null - CreateXMLHttpRequestObject() failed --- Cannot process.");
		}
	}
	catch(e)
	{
	    ProcessException(e, "callbackREX()");
	}
	finally
	{
	}
}

function toggleShowExplReco(xmlResults)
{
	var rc = false;
	
	try
	{
		if(xmlResults != null)
		{
			if((explElem != null) && (explPID != null) && (ajaxAction != null))
			{
				var PID      = null;
				var LineItem = null;
				
				var explState = explElem.innerHTML;
					
				explElem.innerHTML = ((explState == "Show Explanation") ? "Hide Explanation" : "Show Explanation");
				//explElem.onclick   = function() {doRecoExpl(this, PID, CustID, Config, Action);}
					
				// togggle none/block on the display span
				var bBlock = (explState == "Show Explanation");
					
				var exprSpanElem = document.getElementById("exprSpan" + explPID + "re"); // <span>
				
				if(bBlock)
				{
					var explPIDS = xmlResults.getElementsByTagName("PID");
					var explLITS = xmlResults.getElementsByTagName("LineItem");
					
					if(explPIDS.length > 0)
					{
						var iHTML = ""
						
						iHTML += "<font style='font-size:12px;color:black;font-weight:bold;'>Recommended because you purchased:</font>";
						
						iHTML += "<ul>"; 
						
						for(i = 0; i < explPIDS.length; ++i)
						{
							PID      = explPIDS[i].firstChild.data;
							LineItem = explLITS[i].firstChild.data;
								
							if(LineItem.length > 0)
							{
								iHTML += ("<li><font style='font-size:12px;color:black;'>" + LineItem + "</font></li>");
							}
						}
						
						iHTML += "</ul>";
						
						exprSpanElem.innerHTML = iHTML;
					}
				}
					
				blockNoneSpan(exprSpanElem, bBlock);
					
				rc = true;
			}
			else
			{
				ProcessException(null, "Recommend.js::toggleShowExplReco()", "Invalid 'explElem', 'explPID', and/or 'ajaxAction' --- Cannot process.");
			}
		}
		else
		{
			ProcessException(null, "Recommend.js::toggleShowExplReco()", "Invalid passed-in parameter 'xmlResults' --- Cannot process.");
		}
	}
	catch(e)
	{
	    ProcessException(e, "toggleShowExplReco()");
	}
	finally
	{
	}
	
	return rc;
}

function blockNoneSpan(element, bBlock)
{
	try
	{
		if(element != null)
		{
			var sVisVal = '';
			if(bBlock)
			{
				// show
				sVisVal = 'block';
			}
			else
			{
				// hide
				sVisVal = 'none';
			}
						
			if(_isIE)
			{
				element.style.visibility = 'visible';
			}
			else
			{
				element.style.setProperty('visibility', '', null);
			}
			element.style.display = sVisVal;
		}
	}
	catch(e)
	{
	    ProcessException(e, "showHideElem()");
	}
	finally
	{
	}
}

function callbackREFB()
{
	try
	{
		if(xmlHttp != null)
		{
			if(xmlHttp.readyState == _nRequestReadyState_Complete_)
			{
			    if(xmlHttp.status == _nHttpServerStatus_OK_)
			    {
					var sContentType = "";
					
					if(xmlHttp.responseText.toLowerCase().indexOf(_sXmlDocTag) >= 0)
					{
						sContentType = "text/xml";
					}
					else
					{
						sContentType = "text/html";
					}
					
					if(sContentType == "text/xml")
					{
						var xmlResults = null;
						
						if(reParseXMLText(xmlHttp))
						{
							var sXHRR = cleanXMLDocText(xmlHttp.responseText);
							
							xmlResults = ParseXMLTextToXMLDoc(sXHRR);
						}
						else
						{
							xmlResults = xmlHttp.responseXML;
						}
	
						procFBLReco(xmlResults);
			        }
			    }
						
				// remove wait cursor
				toggleCursor(ajaxCursorElem, null, true); //element
			}
		}
		else
		{
			ProcessException(null, "Recommend.js::callbackREFB()", "xmlHttp is null - CreateXMLHttpRequestObject() failed --- Cannot process.");
		}
	}
	catch(e)
	{
	    ProcessException(e, "callbackREFB()");
	}
	finally
	{
	}
}

function procFBLReco(xmlResults)
{
	var rc = false;
	
	try
	{
		if(xmlResults != null)
		{
			if((fblElem != null) && (fblPID != null) && (fblCustID != null) && (ajaxAction != null))
			{
				var fblInnerHTML = fblElem.innerHTML;
				
				//fblHTML.innerHTML = ((fblHTML == sLinkText) ? "" : "");
					
				var fblXmlPID   = xmlResults.getElementsByTagName("PID")[0].firstChild.data;
				var fblFBReason = xmlResults.getElementsByTagName("FBReason")[0].firstChild.data;
					
				if(fblFBReason.length > 0)
				{
					var sOtherLinkText = ""
					if(fblFBReason.toLowerCase() == "o") // own it
					{
						sOtherLinkText = "Not Interested";
					}
					else if(fblFBReason.toLowerCase() == "n") // not interested
					{
						sOtherLinkText = "Own It";
					}
				
					//"<a href='#ajax' id=z" + fblPID + "re onclick='doRecoFBL(this, " + fblPID + ", " + fblCustID + ', "Z", "RECOFBL"' + ");'>" + fblInnerHTML + "</a>";
					var fblRestoreElem = document.getElementById("z" + fblPID + "re");
					
					if(fblRestoreElem != null)
					{
						if(fblFBReason.toLowerCase() != "z")
						{
							////fblRestoreElem.innerHTML = "Restore";
							//if(_isIE)
							//{
							//	fblRestoreElem.style.visibility = 'visible';
							//}
							//else
							//{
							//	fblRestoreElem.style.setProperty('visibility', '', null);
							//}
							fblRestoreElem.style.display = 'inline'; //'block';
						}
						else
						{
							////fblRestoreElem.innerHTML = "";
							//if(_isIE)
							//{
							//	fblRestoreElem.style.visibility = 'hidden';
							//}
							//else
							//{
							//	fblRestoreElem.style.setProperty('visibility', 'hidden', null);
							//}
							fblRestoreElem.style.display = 'none';
						}
					}
					
					//var fblOwnItElem  = document.getElementById("o" + fblPID + "re");
					//var fblNotIntElem = document.getElementById("n" + fblPID + "re");
					//
					//if((fblOwnItElem != null) && (fblNotIntElem != null))
					//{
					//	if(fblFBReason.toLowerCase() == "o")
					//	{
					//		fblOwnItElem.outerHTML  = "<font id=o" + fblPID + "re></font>"; //" + fblInnerHTML + "
					//		fblOwnItElem.innerHTML  = fblInnerHTML;
					//		
					//		fblNotIntElem.outerHTML = "<a href='#ajax' id=n" + fblPID + "re onclick='doRecoFBL(this, " + fblPID + ", " + fblCustID + ', "N", "RECOFBL"' + ");'></a>"; //" + sOtherLinkText + "
					//		fblNotIntElem.innerHTML = sOtherLinkText;
					//	}
					//	else if(fblFBReason.toLowerCase() == "n")
					//	{
					//		fblNotIntElem.outerHTML = "<font id=n" + fblPID + "re></font>"; //" + fblInnerHTML + "
					//		fblNotIntElem.innerHTML = fblInnerHTML;
					//		
					//		fblOwnItElem.outerHTML  = "<a href='#ajax' id=o" + fblPID + "re onclick='doRecoFBL(this, " + fblPID + ", " + fblCustID + ', "O", "RECOFBL"' + ");'></a>"; //" + sOtherLinkText + "
					//		fblOwnItElem.innerHTML  = sOtherLinkText;
					//	}
					//}
					
					var fblFBTextElem = document.getElementById("fb" + fblPID + "re"); // <font>
					
					if(fblFBTextElem != null)
					{
						var iHTML = ""
							
						fblFBTextElem.innerHTML = "You clicked " + fblInnerHTML; //+ "'";
						//fblElem.innerHTML = iHTML;
					}
				}
					
				rc = true;
			}
			else
			{
				ProcessException(null, "Recommend.js::procFBLReco()", "Invalid 'fblElem', 'fblPID', and/or 'ajaxAction' --- Cannot process.");
			}
		}
		else
		{
			ProcessException(null, "Recommend.js::procFBLReco()", "Invalid passed-in parameter 'xmlResults' --- Cannot process.");
		}
	}
	catch(e)
	{
	    ProcessException(e, "procFBLReco()");
	}
	finally
	{
	}
	
	return rc;
}

// onload handler
function addLoadEvent(func)
{
	try
	{
		if(func)
		{
			var oldonload = window.onload;

			if(typeof window.onload != 'function')
			{
				window.onload = func;
			}
			else
			{
				window.onload =
				function()
				{
					if(oldonload)
					{
						oldonload();
					}

					func();
				}
			}
		}
	}
	catch(e)
	{
	    ProcessException(e, "addLoadEvent()");
	}
	finally
	{
	}
}

// onload call
//(function()
//{
//	addLoadEvent(initAJAXReco);
//})()
