function isDigit(character)
{
  var value = false;

  if ((character >= '0') && (character <= '9'))
  {
    value = true;
  }

  return value;
}

//This function parses the URL in search of the geography
//and adds /shop/
function parseURL()
{
	URL = window.location;	
	words = new Array();
	words = URL.toString().split("/");
	if(words[3] == "webapp"){
	  geo="";
	}else{
	  geo = "/" + words[3] + "/" + words[4];
	}
	return geo;
}

function isLetter(character)
{
  var value = false;

  if (((character >= 'A') && (character <= 'Z')) || ((character >= 'a') && (character <= 'z')))
  {
    value = true;
  }

  return value;
}

function isSpace(character)
{
  var value = false;

  if ((character == ' ') || (character == '\r') || (character == '\n') || (character == '\t'))
  {
    value = true;
  }

  return value;
}

function isAlphaNumeric(string)
{
  var value = false;

  for (var i = 0; i < string.length; i++)
  {
    if (isDigit(string.charAt(i)) || isLetter(string.charAt(i)))
    {
      value = true;
    }
    else
    {
      value = false;
      break;
    }
  }

  return value;
}

function isBlank(string)
{
  var value = true;

  for (var i = 0; i < string.length; i++)
  {
    if (!isSpace(string.charAt(i)))
    {
      value = false;
      break;
    }
  }

  return value;
}

function isUnsignedInteger(string)
{
  var value = false;

  for (var i = 0; i < string.length; i++)
  {
    if (isDigit(string.charAt(i)))
    {
      value = true;
    }
    else
    {
      value = false;
      break;
    }
  }

  return value;
}

function nonBlankLength(string)
{
  var length = 0;

  for (var i = 0; i < string.length; i++)
  {
    if (!isBlank(string.charAt(i)))
    {
      length++;
    }
  }

  return length;
}

function isBetween(number, low, high)
{
  var value = false;

  if ((number >= low) && (number <= high))
  {
    value = true;
  }

  return value;
}

function setNVP(nvps, pSeparator, nvSeparator, name, value)
{
  var nvp = name + nvSeparator + value;
  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;
      }
      nvps = nvps.substring(0, offset) + nvp + nvps.substring(end, nvps.length);
    }
    else
    {
      nvps += pSeparator + nvp;
    }
  }
  else
  {
    nvps = nvp;
  }

  return nvps;
}

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 deleteNVP(nvps, pSeparator, nvSeparator, name)
{
  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)
      {
        if (offset > 0)
        {
          offset--;
        }
        end = nvps.length;
      }
      else
      {
        end++;
      }

      nvps = nvps.substring(0, offset) + nvps.substring(end, nvps.length);
      if (nvps.length == 0)
      {
        nvps = null;
      }
    }
  }

  return nvps;
}

function getURLValue(name)
{
  return getNVP(window.location.search, "&", "=", name);
}

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 deleteCookie(name)
{
  setCookie(name, "expired", (new Date()).toGMTString());
}

function setSubCookie(name, subName, subValue)
{
  if (subName == "storeName0" || subName == "storeName") 
  {
    subValue=escape(subValue);
  }
  setCookie(name, setNVP(getCookie(name), "|", ":", subName, subValue));
}

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

}

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

function deleteSubCookie(name, subName)
{
  var value = deleteNVP(getCookie(name), "|", ":", subName);

  if (getCookie(name) != value)
  {
    if (value == null)
    {
      deleteCookie(name);
    }
    else
    {
      setCookie(name, value);
    }
  }
}

function isCookieOn()
{
  var value = true;
  var shop_geo = parseURL();
  //Added shop_geo to reflect URL change to /shop/americas (etc)..
  setSubCookie("commerce", "cookie", "on");
  if (getSubCookie("commerce", "cookie") != "on")
  {
    window.location.replace(shop_geo+"/content/home/store_" + store + "/" + locale + "/cookie_error.html");
    value = false;
  }
  else
  {
    deleteSubCookie("commerce", "cookie");
  }

  return value;
}

function checkCategorySelect(form)
{
  var returnValue = false;

  if (form.categoryId.selectedIndex != 0)
  {
    form.action = 'CategoryDisplay';
    returnValue = true;
  }
  else 
  {
    alert(MESSAGE_NO_SELECTION);
  }

  return returnValue;
}

function bottomSelectHandler(select) 
{
  if (select.selectedIndex > 0) 
  {
    if (select.selectedIndex != (select.length - 1))
    {
      if (select.options[select.selectedIndex].value.substring(0, 11) == 'javascript:') 
      {
        eval(select.options[select.selectedIndex].value.substring(11, select.options[select.selectedIndex].value.length));
      }
      else 
      {
        window.location.href = select.options[select.selectedIndex].value;
      }
    }

    select.selectedIndex = 0;
  }
}

function validateItemListQuantities(form)
{
  var count = 0;
  var returnValue = true;

  for (var i = 7; i < form.elements.length; i+= 2)
  {
    if (form.elements[i].value.length > 0)
    {
      if (isUnsignedInteger(form.elements[i].value))
      {
        if (form.elements[i].value > 0)
        {
          count++;
        }
      }
      else
      {
        alert(MESSAGE_INVALID_QUANTITY);
        form.elements[i].select();
        form.elements[i].focus();
        returnValue = false;
        break;
      }
    }
  }
   
  if ((returnValue == true) && (count == "0"))
  {
    alert(MESSAGE_NO_QUANTITY);
    returnValue = false;
  }

  return returnValue;
}

function toHexSourceId0()
{
  var sourceId0 = getURLValue("sourceid0");
  var hexSourceId0 = null;

  if (sourceId0 == null)
  {
    sourceId0 = getCookie("sourceid0");
  }

  if (sourceId0 != null)
  {
    hexSourceId0 = sourceId0.substring(0, sourceId0.length - 10);
  }

  return hexSourceId0;
}

var quantity;

function saveQuantity(quantityField)
{
  quantity = quantityField.value;
}

function checkQuantity(quantityField)
{
  var value = true;

  if (!isUnsignedInteger(quantityField.value))
  {
    quantityField.value = quantity;
    alert(MESSAGE_SHOPCART_INVALID_QUANTITY + quantity);
    value = false;
  }

  return value;
}

var imageSource;

function swapImage(imageName, newImageSource)
{
  imageSource = document.images[imageName].src;
  document.images[imageName].src = newImageSource;
}

function restoreImage(imageName)
{
  document.images[imageName].src = imageSource;
}

var defaultStoreIdCookie;
var catalogIdCookie;
var storeIdCookie;
var langIdCookie;
var dualCurrIdCookie;
var storeCookie;
var localeCookie;
var storeNameCookie;
var storeURLCookie;
var isSetGroupNavigationCookie;

function setGroupNavigationCookie(defaultStoreId, catalogId, storeId, langId, dualCurrId, store, locale, storeName, storeURL)
{
  if (isSetGroupNavigationCookie)
  {
    if (getSubCookie("commerce", "defaultStoreId") != defaultStoreId)
    {
      setSubCookie("commerce", "defaultStoreId", defaultStoreId);
    }
    if (getSubCookie("commerce", "catalogId") != catalogId)
    {
      setSubCookie("commerce", "catalogId", catalogId);
    }
    if (getSubCookie("commerce", "storeId") != storeId)
    {
      setSubCookie("commerce", "storeId", storeId);
    }
    if (getSubCookie("commerce", "langId") != langId)
    {
      setSubCookie("commerce", "langId", langId);
    }
    if (getSubCookie("commerce", "dualCurrId") != dualCurrId)
    {
      setSubCookie("commerce", "dualCurrId", dualCurrId);
    }
    if (getSubCookie("commerce", "store") != store)
    {
      setSubCookie("commerce", "store", store);
    }
    if (getSubCookie("commerce", "locale") != locale)
    {
      setSubCookie("commerce", "locale", locale);
    }
    if (getSubCookie("commerce", "storeName") != storeName)
    {
      setSubCookie("commerce", "storeName", storeName);
    }
    if (getSubCookie("commerce", "storeURL") != storeURL)
    {
      setSubCookie("commerce", "storeURL", storeURL);
    }
  }
}

function navigateTo(defaultStoreId, catalogId, storeId, langId, dualCurrId, store, locale, storeName, storeURL)
{
  setSubCookie("commerce", "defaultStoreId0", defaultStoreIdCookie);
  setSubCookie("commerce", "catalogId0", catalogIdCookie);
  setSubCookie("commerce", "storeId0", storeIdCookie);
  setSubCookie("commerce", "langId0", langIdCookie);
  setSubCookie("commerce", "dualCurrId0", dualCurrIdCookie);
  setSubCookie("commerce", "store0", storeCookie);
  setSubCookie("commerce", "locale0", localeCookie);
  setSubCookie("commerce", "storeName0", storeNameCookie);
  setSubCookie("commerce", "storeURL0", storeURLCookie);

  setSubCookie("commerce", "defaultStoreId", defaultStoreId);
  setSubCookie("commerce", "catalogId", catalogId);
  setSubCookie("commerce", "storeId", storeId);
  setSubCookie("commerce", "langId", langId);
  setSubCookie("commerce", "dualCurrId", dualCurrId);
  setSubCookie("commerce", "store", store);
  setSubCookie("commerce", "locale", locale);
  setSubCookie("commerce", "storeName", storeName);
  setSubCookie("commerce", "storeURL", storeURL);
}

function navigateBack()
{
  var returnURL = getSubCookie("commerce", "storeURL0");

  setSubCookie("commerce", "defaultStoreId", getSubCookie("commerce", "defaultStoreId0"));
  setSubCookie("commerce", "catalogId", getSubCookie("commerce", "catalogId0"));
  setSubCookie("commerce", "storeId", getSubCookie("commerce", "storeId0"));
  setSubCookie("commerce", "langId", getSubCookie("commerce", "langId0"));
  setSubCookie("commerce", "dualCurrId", getSubCookie("commerce", "dualCurrId0"));
  setSubCookie("commerce", "store", getSubCookie("commerce", "store0"));
  setSubCookie("commerce", "locale", getSubCookie("commerce", "locale0"));
  setSubCookie("commerce", "storeName", getSubCookie("commerce", "storeName0"));
  setSubCookie("commerce", "storeURL", getSubCookie("commerce", "storeURL0"));

  deleteSubCookie("commerce", "defaultStoreId0");
  deleteSubCookie("commerce", "catalogId0");
  deleteSubCookie("commerce", "storeId0");
  deleteSubCookie("commerce", "langId0");
  deleteSubCookie("commerce", "dualCurrId0");
  deleteSubCookie("commerce", "store0");
  deleteSubCookie("commerce", "locale0");
  deleteSubCookie("commerce", "storeName0");
  deleteSubCookie("commerce", "storeURL0");

  window.location.href = returnURL;
}

function checkGroupNavigation(defaultStoreId, catalogId, storeId, langId, dualCurrId, store, locale, storeName, storeURL)
{
  defaultStoreIdCookie = getSubCookie("commerce", "defaultStoreId");
  catalogIdCookie = getSubCookie("commerce", "catalogId");
  storeIdCookie = getSubCookie("commerce", "storeId");
  langIdCookie = getSubCookie("commerce", "langId");
  dualCurrIdCookie = getSubCookie("commerce", "dualCurrId");
  storeCookie = getSubCookie("commerce", "store");
  localeCookie = getSubCookie("commerce", "locale");
  storeNameCookie = getSubCookie("commerce", "storeName");
  storeURLCookie = getSubCookie("commerce", "storeURL");
  isSetGroupNavigationCookie = true;
  var url = window.location.href;

  if ((storeIdCookie != null) &&
      ((url.indexOf("CategoryDisplay") > 0) || (url.indexOf("ProductDisplay") > 0) || (url.indexOf("PromotionDisplay") > 0)))
  {
    if (storeId == storeIdCookie)
    {
      var pageURL = getSubCookie("commerce", "pageURL");
      if (pageURL != null)
      {
        deleteSubCookie("commerce", "pageURL");
      }
    }
    else
    {
      if (storeId == defaultStoreId)
      {
        var pageURL = getSubCookie("commerce", "pageURL");
        if (pageURL != null)
        {
          deleteSubCookie("commerce", "pageURL");
          navigateTo(defaultStoreId, catalogId, storeId, langId, dualCurrId, store, locale, storeName, storeURL);
        }
        else
        {
          if (defaultStoreId == defaultStoreIdCookie)
          {
            setSubCookie("commerce", "pageURL", url);
            url = setNVP(url, "&", "=", "catalogId", catalogIdCookie);
            url = setNVP(url, "&", "=", "storeId", storeIdCookie);
            url = setNVP(url, "&", "=", "langId", langIdCookie);
            url = setNVP(url, "&", "=", "dualCurrId", dualCurrIdCookie);
            isSetGroupNavigationCookie = false;
            window.location.replace(url);
          }
          else
          {
            navigateTo(defaultStoreId, catalogId, storeId, langId, dualCurrId, store, locale, storeName, storeURL);
          }
        }
      }
      else
      {
        navigateTo(defaultStoreId, catalogId, storeId, langId, dualCurrId, store, locale, storeName, storeURL);
      }
    }  
  }
}

function alertMultiCarts()
{
  if (getSubCookie("commerce", "multiCarts") == null)
  {
    alert(MESSAGE_MULTI_CARTS);
    setSubCookie("commerce", "multiCarts", "true");
  }
}

function popup(url) 
{
  var windowObject;
  var name = "popup";
  var width = 313;
  var height = 450;

  if (arguments.length == 3)
  {
    height = arguments[1];
    width = arguments[2];
  }
  else if (arguments.length == 4)
  {
    height = arguments[1];
    width = arguments[2];
    name = arguments[3];
  }
 if (width < 313)
  {
    width = 313;
  }
  else if (width > 760)
  {
    width = 760;
  }
  if (height < 300)
  {
    height = 300;
  }
  else if (height > 570)
  {
    height = 570;
  }
  //o = "width="+width+",height="+height+",resizable=1,status=0,left=0,top=0,menubar=0,scrollbars=1,toolbar=0,location=0,directories=0";
  if ((name == "chat") || (name == "callMe") || (name == "auto") || (name == "autoError"))
  {
    windowObject = window.open(url, name, "height=" + height + ",innerHeight=" + height + ",width=" + width + ",innerWidth=" + width + ",screenX=0,screenY=0,scrollbars=no,resizable=no");
  }
  else
  {
    if ((name == "FormatPrint") || (name == "_EstimatePrint1"))
    {
      windowObject = window.open(url, name, "height=" + height + ",innerHeight=" + height + ",width=" + width + ",innerWidth=" + width + ",screenX=0,screenY=0,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes");
    } else {
      windowObject = window.open(url, name, "height=" + height + ",innerHeight=" + height + ",width=" + width + ",innerWidth=" + width + ",screenX=0,screenY=0,scrollbars=yes,resizable=yes");
    }
  }
  windowObject.focus();
}






// tracCode (1x)
// v1.1 max length patch
function setTrackingCode(){
var delim = "-";
  // read url trac param
  var tracVal = getURLValue("trac");
    if ((tracVal != "") && (tracVal != null)){
     var c = getSubCookie("commerce","keyCode");
        if(tracVal.length > 11) {
            tracVal = tracVal.substring(0,11);
        }    
       // cookie is empty
       if((c == "") || (c == null)){ 
            // fail safe max length       
           if(tracVal.length <= 110){
              setSubCookie("commerce","keyCode",tracVal);
            }
       // cookie has value
       }else{
           // no keycode, no comma, but content 
            if((c.indexOf(".") == -1) && (c.indexOf(delim) == -1) && (c.length > 0 )){
                tracVal = c + delim + tracVal + delim; 
               // Keycode found with single trac code
               }else if((c.indexOf(".") != -1) && (c.indexOf(delim) == -1) && (c.substring(c.indexOf(".")).length > 1)){
                tracVal = c + delim + tracVal + delim;		 
            }else{
              tracVal = c + tracVal + delim;		 
             }
         //alert("js.js testing -\n" + tracVal);
          if(tracVal.length <= 110){
          setSubCookie("commerce","keyCode",tracVal);                
         }
       } 
    }  
 } 
setTrackingCode();

// PCD popup 
function openWindow(url,w,h,shortcut){
		 if ( shortcut )
		 {
		 		 url += "?shortcut=" + shortcut + "&";
		 		 newWindow=window.open(url,'thewindow','width=' + w + ',height=' + h);  newWindow.focus();
		 } else {
		 		 newWindow=window.open(url,'thewindow','width=' + w + ',height=' + h);  newWindow.focus();
		 }
		 }
		 
// print PCD flash tour popup link
// 3 types (spec,series,tab)
function visualTour(type,url){ 
        var str= "";
        var shop_geo = parseURL();
        // arrow and tour link at bottom of model specs page
        // Added shop_geo to reflect URL change to /shop/americas(etc)...
        if(type == "spec"){
           str = '<td><a href="javascript:openWindow('+url+',640,480)"><img src="'+shop_geo+'/content/product_images/en_US/ar_blue.gif" alt="" height="7" width="7" border="0" /></a></td>';
           str += '<td nowrap="nowrap">&nbsp;<span class="small"><a href="javascript:openWindow(\''+url+'\',640,480)" style="color: #03689A" style="text-decoration: none">Visual tour</a></span></td>';
           str += '<td><img src="'+shop_geo+'/content/misc_images/c.gif" alt="" height="1" width="20" border="0" /></td>';
        // series 3D tour link under series image
        }else if(type == "series"){
           if(arguments[2]){
           str = '<table border="0" cellspacing="0" cellpadding="0"><tr>';
           str += '<td><a href="javascript:openWindow(\''+url+'\',640,480)"><img src="'+shop_geo+'/content/misc_images/3d_tour.gif" alt="" height="15" width="16" border="0" /></a></td>';
           str += '<td>&nbsp;<a href="javascript:openWindow(\''+url+'\',640,480)" class="small" style="text-decoration: none">'+arguments[2]+' Series 3D tour</a></td></tr></table>';
          }else{alert("you must provide series letter as 3rd argument to visualTour function")}
          
        // visual tour as tab link
        }else if(type == "tab"){
           str = ' <table class="v14-list-spacing" border="0" cellpadding="0" cellspacing="0"><tr class="bullet-list"><td><img src="//www.ibm.com/i/v14/bullets/dbl-bullet.gif" alt="" height="8" width="2" /></td>';
           str +=    '<td><a class="v14-text-tab-unselect-link" href="javascript:openWindow(\''+url+'\',640,480)">Visual tour</a></td></tr></table>';
		   
	// visual tour as tab1 link
        }else if(type == "tab1"){
           str +=    '<td><a class="v14-text-tab-unselect-link" href="javascript:openWindow(\''+url+'\',640,480)">Visual tour</a></td>';
	// visual tour as tab2 link
        }else if(type == "tab2"){
           str +=    '<td><a class="v14-text-tab-unselect-link" href="javascript:openWindow(\''+url+'\',640,480)">Tour d&acute;horizon</a></td>';
       }
        document.write(str);
     }
     
function submitForm(submitAction, theForm){
 document.getElementById("submitAction").value = submitAction;
 if(theForm != null){
   document[theForm].submit();
 }else if (document.getElementById("actionFormName") != null && document[document.getElementById("actionFormName").value] != null){
   document[document.getElementById("actionFormName").value].submit();
 }else{
   document.forms[0].submit();
 }
} 

function clearSubmitAction(){
  if(document.getElementById("submitAction") != null){
    document.getElementById("submitAction").value = "";
  }
}
    