// Core SmartAssist code

raqNS6=false;
raqIE4=(document.all);
if (!raqIE4) { raqNS6 = (document.getElementById);}
raqNS4=(document.layers);
layerheight = 370;
floatY = 1000;
firstLoad = true;
deployed = false;
window.onresize = winResize;
lastY = -1;
adjust();
smartAssistDiv = "smartICUEdiv";  // smartassist module name



// MD5
if(typeof(sa_onclick)!="function"){
	window.document.write("<script type=\"text/javascript\" language=\"JavaScript\" src=\"//www.ibm.com/common/stats/stats.js\"><\/script>");
}



function getNVP(nvps, pSeparator, nvSeparator, name) {
  var value = null;
  var offset = -1;
  var end = -1;

  if (nvps != null)
  {
    name += nvSeparator;
    offset = nvps.indexOf(name);
    if (offset >= 0)
    {
      end = nvps.indexOf(pSeparator, offset);
      if (end < 0)
      {
        end = nvps.length;
      }
      value = nvps.substring(offset + name.length, end);
    }
  }

  return value;
}
function setCookie(name, value)
{
  document.cookie = name + "=" + value + "; path=/; domain=.ibm.com" + (arguments.length == 3 ? ("; expires=" + arguments[2]) : "");
}
function getCookie(name)
{
  return getNVP(document.cookie, ";", "=", name);
}

 function getSubCookie(name, subName)
{	
  if (subName == "storeName0" || subName == "storeName")
  {
    return unescape(getNVP(getCookie(name), "|", ":", subName));
  }else{
    return getNVP(getCookie(name), "|", ":", subName);
  }

}

// find CCE country and language from cookie
// use this in the spoof URL to Surfaid
var SO_cntry = "-";
var SO_lang = "-" ;
if(getSubCookie("commerce", "catalogId") != null){
  if(getSubCookie("commerce", "catalogId") == "-840"){
    SO_cntry = "us";
  }else{
    SO_cntry = "ca";
  }
  if(getSubCookie("commerce", "langId") == "-1" || getSubCookie("commerce", "langId") == "124"){
    SO_lang = "en";
   }
  if(getSubCookie("commerce", "langId") == "-125"){
   SO_lang = "fr";
  }
}

var REGURLS = new Array();

REGURLS[0]="categoryId=2576395";
REGURLS[1]="categoryId=2576396";
REGURLS[2]="categoryId=2576397";
REGURLS[3]="categoryId=4611686018425049002";
// look for RAQ cookie
var seen = getCookie("smartICUE");
 if(seen == null){
  str = "ICUE TEST\n";
  for(var i=0;i<REGURLS.length;i++){
   str += "PATTERN:"+REGURLS[i]+" RESULT:"+location.href.indexOf(REGURLS[i])+"\n";
    if(location.href.indexOf(REGURLS[i]) != -1){
      deployModule();
    }
  } 
  //alert(str)
 }else{
 // alert("SmartRAQ already seen by user and closed or call to action followed")
 } 


function winResize() {
	firstLoad = true;
	adjust();
}

function adjust() {
	if (deployed) {
	
		if ((raqNS4) || (raqNS6)) {
		 	visHeight = window.innerHeight;
			scrollPos = window.pageYOffset;
			modPos = document.getElementById(smartAssistDiv).offsetTop - scrollPos;
	                 
			// check to see if we are in range and if we need to bother
			if (modPos < 0 || modPos + layerheight > visHeight || firstLoad) {
				lastY = visHeight - layerheight + scrollPos;
				
				document.getElementById(smartAssistDiv).style.top = lastY + "px";
				firstLoad = false;
			}
		}
		if (raqIE4){
		        visHeight = document.documentElement.clientHeight;
			scrollPos = document.body.scrollTop || document.documentElement.scrollTop;
			modPos = document.all[smartAssistDiv].style.posTop;
		        // check to see if we are in range and if we need to bother
			//if (scrollPos > modPos || modPos + layerheight > scrollPos + visHeight || firstLoad) {
				lastY = visHeight - layerheight + scrollPos;
				document.all[smartAssistDiv].style.posTop = lastY;
				firstLoad = false;
			//}
		} 
		setTimeout('adjust()',500);
	}
}

function closeIt() {

       if ((raqNS4) || (raqNS6)) {
		document.getElementById(smartAssistDiv).style.visibility = "hidden";
		document.cookie = "psam=;path=/; domain=ibm.com;";
	}
	if (raqIE4){
		document.all[smartAssistDiv].style.visibility = "hidden";
		document.cookie = "psam=;path=/; domain=ibm.com;";
	}
	setCookie("smartRAQ",1,"");
	var spoof = "http://www.ibm.com/raqsmartofferclose.pdf?ca=M712101b01&url="+escape(document.location.href);
	sa_onclick(spoof);
}

function showIt() {
	if ((raqNS4) || (raqNS6)) {
		document.getElementById(smartAssistDiv).style.visibility = "visible";
		
	
	}
	if (raqIE4){
		document.all[smartAssistDiv].style.visibility = "visible";
	}

       var spoof = "http://www.ibm.com/raqsmartofferopen.pdf?ca=M712101b01&url="+escape(document.location.href);
	sa_onclick(spoof);
}

function follow(){
setCookie("smartICUE",1,"");


}




// Deploy the module if a problem is sensed
function deployModule() {
    deployed = true;
	/** update 6/8/09 wait a min before showing */
	setTimeout(showIt, 60000);
	// commented via Raymond's direction
	//window.SA_Message="ca=openpsamoffer";
	
	adjust();
}



