//////////////////////////////////////////////////
//  Global variables which can be used 
//  in any place.
//////////////////////////////////////////////////
ns = navigator.appName == "Netscape";

//////////////////////////////////////////////////
// Setting a layout of generic frame
//////////////////////////////////////////////////

function setLayout() {
   var downRgnHeight = 120; // Please adjust it, when bottom size is changed.
   var upRgnHeight;
   var LayoutBGColor = "#FFFFFF";
   var LayoutWidth = "992px";
   
   var gTable = document.getElementById('GlobalTable');
   var contentCell = document.getElementById('GlobalContentCell');
   gTable.style.width = LayoutWidth;
   if (navigator.appName == 'Netscape') {
   	upRgnHeight = window.innerHeight - downRgnHeight ;
   	document.getElementById('GlobalNavCell').style.height = upRgnHeight ;
	contentCell.style.height = upRgnHeight - 20;
   } else {   	
   	gTable.style.height = "100%";
   }
   gTable.style.background = LayoutBGColor;
   gTable.style.padding = "0px";
   gTable.style.margin = "0px";
   document.body.style.margin = "0";
}


//////////////////////////////////////////////////
// Refresh window for shop-in-shop periodically
//////////////////////////////////////////////////

var timerID = null;
var timerRunning = false;
//var intervalSec = 5; // interval second which should be changed in accordence with customer's request.

function stopclock() {
	if (timerRunning) clearInterval(timerID);
	timerRunning = false;
}

function setRefresh() {
	// Make sure the clock is stopped;
	stopclock();
	timerID = setInterval("top.location.reload();",1000 * 60 * 3);
	timerRunning = true;
}
function startLoading() {
	//DO NOT REMOVE
        //if (window.parent.SIS_frame != null)
	//	window.parent.SIS_frame.loading();
}

function stopLoading() {
	//DO NOT REMOVE
       // if (window.parent.SIS_frame != null)
	//	window.parent.SIS_frame.loaded();
}

//////////////////////////////////////////////////
// Image Rollover Functionalities
//////////////////////////////////////////////////
function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
  }

  function MM_preloadImages() { //v3.0
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  }

  function MM_findObj(n, d) { //v4.0
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n); return x;
  }
  function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
     if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
  }
  
//////////////////////////////////////////////////
// Open popup window 
//////////////////////////////////////////////////
function openPopup(url, winName, popWidth, popHeight, popMenu, popScroll, popStatus, isCenter, isResizable) {
    // popStatus is a new parameter; it is optional so that old calls will not cause an error
    if (isCenter) {
      x = Math.floor((screen.width - popWidth)/2);
	    y = Math.floor((screen.height - popHeight )/2);
	  } else {
	    x = 25;
	    y = 140;
	  }
    var statusBar = (popStatus == null) ? popScroll : popStatus;
    var isResizable = (isResizable == null) ? "no" : isResizable;
    var ns = navigator.appName == "Netscape";
    if (popStatus != null && popStatus == "yes") {

      // 'fix' for Mac IE's problem when including a status bar in the pop up window
      // - the browser chops off the bottom 18 pixels of the window to accomodate for the
      //   the status bar, and so we add 18 pixels to the height
      isMac = (navigator.appVersion.indexOf("Mac") == -1) ? false : true;
      if (!ns && isMac) { popHeight = String(Number(popHeight) + 18); }
    }

    if(win) {
      win.close();
      setTimeout('openPopup(url, winName, popWidth, popHeight, popMenu, popScroll, popStatus, isCenter)',1000);
      return;
    }

    if ((navigator.appVersion.indexOf("MSIE") != -1)){
      var win=window.open(url, winName,'width=' + popWidth + ',height=' + popHeight + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ',menubar='+ popMenu + ',scrollbars=' + popScroll + ',resizable='+isResizable +',status=' + statusBar + '');
    }
    else {
      var win=window.open(url, winName,'width=' + popWidth + ',height=' + popHeight + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ',toolbar='+ popMenu + ',scrollbars=' + popScroll + ',resizable='+isResizable +',status=' + statusBar + '');
    }

    if (win.opener == null) win.opener = self;

    if ((navigator.appVersion.indexOf("MSIE 4.01") != -1)){
      return;
    }
    else {
      win.focus();
    }
}


//////////////////////////////////////////////////
// Return current year
//////////////////////////////////////////////////
function getCurrentYear() {
	return new Date().getFullYear();
}


//////////////////////////////////////////////////
// Set the field value 
//////////////////////////////////////////////////
function setFieldVal(f, v, t, d) {
  // where f is Field name, v is Value to be set and t is field type, d is default value.
  // Set the default value
  if (v == null || v == 'null') { 
    v = d;
  }
  // get a object using Field name
  var fields = document.getElementsByName(f);
  // Set the value into the feild object
  for (var i = 0 ; i < fields.length;i++){
    if (t == "r") { // Radio Button
	    if (fields[i].value == v) {
	      fields[i].checked = true;
	    }
	  } else if (t=='s'){// selection
	    var fOptions = fields[i].options;
	    for (var j = 0 ; j < fOptions.length; j++){
	      if (fOptions[j].value == v) {
	        fOptions[j].selected =  true;
	      }
	    } // end for
	  } else if (t=='c'){ // Check box
	    if (fields[0].value == v) {
	      fields[0].checked = true;
	    } 
	  } else {
	    fields[0].value = v;
	  }// end if 
  } // end for
}

//////////////////////////////////////////////////
// set different colors to even rows 
// and odd rows in a table. 
//////////////////////////////////////////////////
function setGridTableColor(oName, startRow, numberOfRow, oddColor, evenColor) {
  // where oName is table object ID
  // 0 base counting, if End row is -1, it means End of table.
  
  var tbl = getFirstRowElement(document.getElementById(oName));
  var maxCnt;
  var tmp = '';
  var rowNumber = 1;
  if ( numberOfRow == -1 || numberOfRow == null || numberOfRow == "" || numberOfRow > tbl.childNodes.length) {
    maxCnt = tbl.childNodes.length;
  } else {
    maxCnt = startRow + numberOfRow;
  }
  for (var i = startRow; i < maxCnt ; i++){
     trTag = tbl.childNodes[i];
     if (trTag.nodeName.toUpperCase() =='TR') {
		trTag.bgColor = ((rowNumber%2) == 1)? oddColor:evenColor;
    	rowNumber++;
    }
  }
}

function getFirstRowElement(node) {
	var tag = node.nodeName;
	if (tag.toUpperCase() =='TBODY') return node;
	if (node.childNodes.length <= 0) return null;
	for (var i = 0 ; i < node.childNodes.length; i++) {
		var trNode = getFirstRowElement(node.childNodes[i]);
		if (trNode != null) return trNode;
	}
}

//////////////////////////////////////////////////
//  This function, when set to true, always 
//  guarantees the iframe html targets are 
//  always set to parent
//////////////////////////////////////////////////
function setHtmlTargets(modeFlag) {
	if(modeFlag){
		var linksWithTarget = document.links;
		var formsWithTarget = document.forms;
		var targetName = "_parent";
		for (var i = 0 ; i < linksWithTarget.length; i++)
			linksWithTarget[i].target = targetName;		
		for (var i = 0 ; i < formsWithTarget.length; i++) 
			formsWithTarget[i].target = targetName;
	}
}
//This Function reset the HREF target if the site is in dealer mode
function setTargets(isShopperMode,isCallCenterMode,isShopInShopMode) {
	if(isCallCenterMode || isShopInShopMode){	
		var linksWithTarget = document.links;
		var formsWithTarget = document.forms;
		var targetName="main_frame";
		for (var i = 0 ; i < linksWithTarget.length; i++)
			linksWithTarget[i].target = targetName;		
		for (var i = 0 ; i < formsWithTarget.length; i++) 
			formsWithTarget[i].target = targetName;
	}
	
}

//////////////////////////////////////////////////
// Set the layout of table with pagenation. 
//////////////////////////////////////////////////
function setPageTitleBar ( leftMargin, rightMargin, contentWidth, cellHeight, cellColor, titleBarId) {
    if (titleBarId == null || titleBarId == "") {
      titleBarId = "TitleBar";
    }//end if
    
    var titleBar = document.getElementById(titleBarId);
	
    if (titleBar != null) {
      var titleBarCell = titleBar.childNodes;
      var titleBarWidth = [leftMargin, contentWidth, rightMargin];
      
      titleBar.bgColor = cellColor;
      titleBar.height = cellHeight;
  
      //set width and height
      for (var i = 0; i < titleBarCell.length ; i++) {
        titleBarCell[i].width = titleBarWidth[i];
      }
    } // end if
}// end function

//////////////////////////////////////////////////
// Making a return URL for login, 
// member-signin and so on.
//////////////////////////////////////////////////

function getParamStr(formObj, viewName, defaultParams, excludeParams) {
	var coll = formObj.elements;
	var separator = (viewName.indexOf('?') >= 0) ? '&' : '?';
	paramStr = viewName + (defaultParams == '' ? '' : separator + defaultParams);
	
	// add all field values
	for (var i = 0; i < coll.length ; i++){
		if (coll[i].type == 'radio' || coll[i].type == 'checkbox'){
			if (coll[i].checked) paramStr = paramStr.concat('&', coll[i].name ,'=', coll[i].value);
		} else if (coll[i].type == 'select-one' || coll[i].type == 'select-multi' ) {
			if (coll[i].selected) paramStr = paramStr.concat('&', coll[i].name ,'=', coll[i].value);
		} else {
			if (coll[i].value != '' && !isExcludeParam(coll[i].name, excludeParams)) paramStr = paramStr.concat('&', coll[i].name ,'=', coll[i].value);
		}
	}
	return paramStr.replace(/&/g, '%26').replace(/\?/g, '%3F');
}

function isExcludeParam(fName, excludeList) {
	if (excludeList.toString().indexOf(fName) >= 0 ) return true;
	return false; 
}

//////////////////////////////////////////////////
// Set Inner frame style for static HTML files
// It has been out of date. 
// It may be able to be deleted.
//////////////////////////////////////////////////
var count = 0;
var win = null;
var regExp = null;
 
var OBJ_TYPE_IMG = 'I';
var OBJ_TYPE_LINK = 'L';
var OBJ_TYPE_EMBED = 'E';
var OBJ_TYPE_FORM = 'F';
var OBJ_TYPE_IFRAME = 'M';

 
var typeArray = [
					[document.images, OBJ_TYPE_IMG],
					[document.links, OBJ_TYPE_LINK],
					[document.embeds, OBJ_TYPE_EMBED],
					[document.forms, OBJ_TYPE_FORM],
					[document.getElementsByTagName('IFRAME'), OBJ_TYPE_IFRAME]
				];

function setStaticPart(){
	if (count++ > 0) return;
	var divObj = document.getElementById('staticHtmlPart');
	regExp = eval('/' + document.location.pathname.substring(0, document.location.pathname.lastIndexOf('/')).replace(/\//g, '\\/') +'/i');
	if (baseTarget.charAt(baseTarget.length - 1) == '/')
		baseTarget = baseTarget.substring(0, baseTarget.length - 1);
	// search images in this document
	for (var i = 0; i < typeArray.length ; i ++) {
		changeUriPath(typeArray[i][0],typeArray[i][1]);
	}
	
	
}

function isStaticPart(o) {
	var p = o.parentNode;
	if (o.tagName.toUpperCase() == 'BODY') return false;
	if (p.id == 'staticHtmlPart') return true;
	if (isStaticPart(p)) return true;
	return false;	
}

function changeUriPath(o, objType) {
	if (o !=null) {
		for (var i =0 ; i < o.length ; i ++) {
			if (isStaticPart(o[i]))  {
				switch (objType) {
					case OBJ_TYPE_IMG:
						o[i].src = o[i].src.replace(regExp, baseTarget);
						break;
					case OBJ_TYPE_LINK:
						if (o[i].href.toLowerCase().indexOf('javascript:') == 0) break;
						o[i].href = linkTarget + o[i].href.replace(regExp, baseTarget);
						break;
					case OBJ_TYPE_EMBED:
						
						if (o[i].src.indexOf('http://') == 0) break;
						if (o[i].src.indexOf('/') == 0) 
							o[i].src = baseTarget + o[i].src;
						else  
							o[i].src = baseTarget + '/' + o[i].src;
						break;
					case OBJ_TYPE_FORM:
						if (o[i].action.indexOf('http://') == 0) break;
						if (o[i].action.indexOf('/') == 0) 
							o[i].action = baseTarget + o[i].action;
						else  
							o[i].action = baseTarget + '/' + o[i].action;
						break;
					case OBJ_TYPE_IFRAME:
						if (o[i].src.indexOf('http://') == 0) break;
						if (o[i].src.indexOf('/') == 0) 
							o[i].src = 'http://' + o[i].src;
						else  
							o[i].src = 'http://' + baseTarget + '/' + o[i].src;
						break;
				}
			}
		}
	}
}

function debug(parentTree, o) {
	for(var i in o) {
		var curObj = parentTree + '.' + i;
		if (i !='outerHTML' && i.length > 1) {
			win.document.writeln(curObj + ' = ');
			var obj = eval(curObj + ';');
			if (obj != null) win.document.writeln(obj);
			win.document.writeln('<br>');
			//if (obj != null && obj != undefined) debug(curObj, eval(curObj));
		}
	}
}

function setCartDisplay(){
	var scItems=getCookie('WCS_ITEMS');
	var scTotal=getCookie('WCS_TOTAL');
	if(scItems!=null) document.getElementById('itemDisplay').innerHTML=scItems;
	if(scTotal!=null) document.getElementById('totalDisplay').innerHTML=scTotal;
}

function getItemDisplay(){
	var scItems=getCookie('WCS_ITEMS');
	if(scItems!=null) return scItems;
	return 0;
}

function resetCartDisplay(){
	value="";
	document.cookie = "WCS_ITEMS=;expires=Thu, 01-Jan-70 00:00:01 GMT" + ";path=/";
	document.cookie = "WCS_TOTAL=;expires=Thu, 01-Jan-70 00:00:01 GMT" + ";path=/";
}

function updateItemDisplay(value){
	document.cookie = "WCS_ITEMS=" +escape(value) + ";path=/";
	// document.getElementById('itemDisplay').innerHTML=value;
}

function updateTotalDisplay(value) {
    document.cookie = "WCS_TOTAL=" +escape(value) + ";path=/";
    // document.getElementById('totalDisplay').innerHTML=value;
}

function resetLastVisitedModel() {
	document.cookie = "WCS_LAST_VISITED_MODEL=;expires=Thu, 01-Jan-70 00:00:01 GMT" + ";path=/";
}

function updateLastVisitedModelvalue(value){
    // alert(value);
	document.cookie = "WCS_LAST_VISITED_MODEL=" +escape(value) + ";path=/";
	// document.getElementById('itemDisplay').innerHTML=value;
}
	
function getLastVisitedModel(){
	var lastVisitedModel=getCookie('WCS_LAST_VISITED_MODEL');
	// alert("inside: " + lastVisitedModel);
	return lastVisitedModel;
}	

function resetLastVisitedProductId() {
	document.cookie = "WCS_LAST_VISITED_PRODUCT_ID=;expires=Thu, 01-Jan-70 00:00:01 GMT" + ";path=/";
}

function updateLastVisitedProductId(value){
    // alert(value);
	document.cookie = "WCS_LAST_VISITED_PRODUCT_ID=" +escape(value) + ";path=/";
	// document.getElementById('itemDisplay').innerHTML=value;
}
	
function getLastVisitedProductId(){
	var lastVisitedProductId=getCookie('WCS_LAST_VISITED_PRODUCT_ID');
	// alert("inside: " + lastVisitedModel);
	return lastVisitedProductId;
}	

function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function goTop(toplink)
{
  if (toplink == null) toplink ='top';
  if (toplink.substring(0,1) =='#') toplink=toplink.substring(1);
  var urlStr = window.location.pathname + window.location.search + '#' + toplink;
  window.location = urlStr;
}

function callStatic(urlStr)
{
  var urlStr = '/commerce/servlet/StaticView?storeId=10001&catalogId=10001&contentpage=' + urlStr;
  var currentHostname = window.location.hostname;
  urlStr = 'http://' + currentHostname + urlStr;
  window.location = urlStr;
}

function getFormattedNumber(num, isEngLang) {
   if (isEngLang) {
    sep = ","; 
    decpoint =".";
  } else {
    sep = " "; 
    decpoint =",";
  }

  num = num.toString();
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";
  if (typeof(x) != "undefined") {
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    if (typeof(y) != "undefined" && y.length > 0)
      x += "" ;
  }
  return x;
}


function searchKeyword(strTemp) {
    strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,"");
    document.forms.PerformSearchTop.keyword.value=strTemp;
    document.PerformSearchTop.submit();
}

//////////////////////////////////////////////////
// The End of the JS
//////////////////////////////////////////////////
