
// Core SmartAssist code

NS6=false;
IE4=(document.all);
if (!IE4) { NS6 = (document.getElementById);}
NS4=(document.layers);
layerheight = 181;
floatY = 1000;
firstLoad = true;
deployed = false;
window.onresize = winResize;
lastY = -1;
adjust();
smartAssistDiv = "smartassist";  // smartassist module name


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 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 = "";
if(getSubCookie("commerce", "catalogId") == "-840"){
  SO_cntry = "us";
}else{
  SO_cntry = "ca";
}
var SO_lang = "" ;
if(getSubCookie("commerce", "langId") == "-1" || getSubCookie("commerce", "langId") == "124"){
  SO_lang = "en";
}
if(getSubCookie("commerce", "langId") == "-125"){
  SO_lang = "fr";
}



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

function adjust() {
	if (deployed) {
	
		if ((NS4) || (NS6)) {
		
			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 (IE4){
		
			visHeight = document.documentElement.clientHeight;
			scrollPos = 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 ((NS4) || (NS6)) {
		document.getElementById(smartAssistDiv).style.visibility = "hidden";
		document.cookie = "psam=;path=/; domain=ibm.com;";
	}
	if (IE4){
		document.all[smartAssistDiv].style.visibility = "hidden";
		document.cookie = "psam=;path=/; domain=ibm.com;";
	}
	
	var spoof = "http://www-132.ibm.com/shop/americas/webapp/wcs/stores/servlet/default/OrderItemDisplay/"+SO_cntry+"/"+SO_lang+"/garsmartofferclose.pdf?ca=icueoos&tactic=icue0w0w01";
	sa_onclick(spoof);
}

function showIt() {
	if ((NS4) || (NS6)) {
		document.getElementById(smartAssistDiv).style.visibility = "visible";
		
	
	}
	if (IE4){
		document.all[smartAssistDiv].style.visibility = "visible";
	}

        var spoof = "http://www-132.ibm.com/shop/americas/webapp/wcs/stores/servlet/default/OrderItemDisplay/"+SO_cntry+"/"+SO_lang+"/garsmartofferopen.pdf?ca=icueoos&tactic=icue0w0w01";
	sa_onclick(spoof);
}



// Analyze the shopping cart and show module if ICUE part is out of stock
if(ICUE_insufficient_inventory()){
       deployModule();
}




// Deploy the module if a problem is sensed
function deployModule() {
 
       
        smartAssistDiv = "smartassist";
      	deployed = true;
	showIt();
	// commented via Raymond's direction
	//window.SA_Message="ca=openpsamoffer";
	
	adjust();
    
}

// search CCE cart for ICUE out of stock parts (adestefa@us.ibm.com)
function ICUE_insufficient_inventory(){ 
   
   // get error em element 
   var em = document.getElementsByTagName("em")[0];
   // only if it exsits
   if(em){
    if(em.firstChild){
    
     // get the text
     var str = em.firstChild.nodeValue;
     if(str){
        // only errors about inventory count
        if(str.indexOf("insufficient inventory") != -1){
      
        // get list of out of stock part numbers from msg
        var parts = str.substring(str.indexOf("\n")+1).split(",");
        
           // loop each product sku
           for(var i=0;i<parts.length;i++){
        
               // filter ICUE parts only by matching first character
               if(parts[i].substring(0,1) == "A" || parts[i].substring(0,1) == "T"||
                  parts[i].substring(0,1) == "S" || parts[i].substring(0,1) == "M"||
                  parts[i].substring(0,1) == "R"){
                  return true;
               }
       
           }
          return false;
        }
     } 
   }else{
     return false;
   }
 }  
}   




