function message(headTxt, msg) {
	alert(headTxt + "\n\n" + msg);		
}
function selectAllCheckboxes(selectAllCheckbox) {
	len = document.forms[2].elements.length;
	var i=0;
	while(i!=len) {
		if (document.forms[2].elements[i].type=='checkbox' && document.forms[2].elements[i].name != eval("selectAllCheckbox.name"))	
			document.forms[2].elements[i].checked = selectAllCheckbox.checked;
		i++;
	}
}
function toggleCheckboxes(checked,name) {
	len = document.forms[2].elements.length;
	var i=0;
	while(i!=len) {
		if (document.forms[2].elements[i].type=='checkbox' && document.forms[2].elements[i].name == eval("name"))	
			document.forms[2].elements[i].checked = checked;
		i++;
	}
}
function toUpperCase(inputElement) {
    if (inputElement.value.length > 0) inputElement.value = inputElement.value.toUpperCase();
}
function confirmSignoff(headtxt, href) {
	if (confirm(headtxt + "!")) {
       	top.location.replace(href);
   	}
}
function languageSwitch(href,code,warningText) {
	if (!top.window.blockedLanguageSwitch || confirm(warningText)) {
		top.location.replace(href+'?TO_LANGUAGE='+code);
	}
}
function confirmation(text,href) {
	if (confirm(text)) {
       	top.home.location.replace(href);
   	}
}
function confirmCardInactivation(text,actionName,checkbox) {

	if (confirm(text)) {
		submitLinkOnce(actionName,checkbox.value);
	} else {
      	checkbox.checked = !checkbox.checked;
    }
        
}
function confirmCardActivation(text,cardInfo,actionName,checkbox) {

    text = text + "\n" + cardInfo
	cn = prompt(text,"");
	if (cn == null) {
		checkbox.checked = !checkbox.checked;
	} else {
		submitLinkOnce(actionName,cn);
    }
        
}
function showWindow(jspName) {
	if (top.window.infoWindow != null && !top.window.infoWindow.closed) {
		top.window.infoWindow.close(); 
	}
	if (navigator.userAgent.indexOf("MSIE")!=-1) {
		top.window.infoWindow = window.open(jspName, "NetStore", "left=500,top=200,height=400,width=600,toolbar=no,menubar=no,location=no,status=yes,scrollbars=yes,resizable=yes");
		top.window.infoWindow.focus();
    } else {
		top.window.infoWindow = window.open(jspName, "NetStore", "screenX=500,screenY=200,height=400,width=600,toolbar=no,menubar=no,location=no,status=yes,scrollbars=yes,resizable=yes");
		top.window.infoWindow.focus();
    }
}
function showWindow(jspName,anchorName,windowName,x,y,h,w) {

	if (top.window.infoWindow != null && !top.window.infoWindow.closed) {
		top.window.infoWindow.close(); 
	}
       str = ",height=" + h + ",width=" + w + ",toolbar=no,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes";
	if (navigator.userAgent.indexOf("MSIE")!=-1) {
    	str = "left=" + x + ",top=" + y + str;		
    } else {
    	str = "screenX=" + x + ",screenY=" + y + str;
    }
     
    if (anchorName != null) {
       	jspName = jspName+"#"+anchorName;
    }
	top.window.infoWindow = window.open(jspName, windowName, str);
	top.window.infoWindow.focus();
}
function showCartWindow(jspName,anchorName,windowName,x,y,h,w) {

	if (top.window.cartWindow != null && !top.window.cartWindow.closed) {
		top.window.cartWindow.close(); 
	}
       str = ",height=" + h + ",width=" + w + ",toolbar=no,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes";
	if (navigator.userAgent.indexOf("MSIE")!=-1) {
    	str = "left=" + x + ",top=" + y + str;		
    } else {
    	str = "screenX=" + x + ",screenY=" + y + str;
    }
     
    if (anchorName != null) {
       	jspName = jspName+"#"+anchorName;
    }
	top.window.cartWindow = window.open(jspName, windowName, str);
	top.window.cartWindow.focus();
}
var listName = "";
var itemKey = null;
var action = "";
function openShoppingListWindow(actionValue, key, jspName,windowName,x,y,h,w) {
	itemKey = key;
	action = actionValue;
	if (top.window.shoppingListWindow != null && !eval("typeof(top.window.shoppingListWindow)!=\"undefined\"") && !top.window.shoppingListWindow.closed) {// if mywindow has been opened, and mywindow hasn't already been closed
		top.window.shoppingListWindow.close(); //close it in order to re-assign properties
	}
       str = ",height=" + h + ",width=" + w + ",toolbar=no,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes";
	if (navigator.userAgent.indexOf("MSIE")!=-1) {
    	str = "left=" + x + ",top=" + y + str;		
    } else {
    	str = "screenX=" + x + ",screenY=" + y + str;
    }
    top.window.shoppingListWindow=open(jspName, windowName, str);
   	if (top.window.shoppingListWindow.opener == null) {
		 top.window.shoppingListWindow.opener = self;
  	}
	top.window.shoppingListWindow.focus() 
}
function submitEnter(button,ev) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (ev) 
		keycode = ev.which;
	else 
		return true;
	
	if (keycode == 13) {
		submitButtonOnce(button);
		return false;
	} else
		return true;
}
function submitLinkOnEnter(action,value,ev) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (ev) 
		keycode = ev.which;
	else 
		return true;
	
	if (keycode == 13) {
		submitLinkOnce(action,value);
		return false;
	} else
		return true;
}
function updateDataFromShoppingListWindow() {

	if(document.forms[2].IBS_LISTSELECTION_VALUE != null) {		
		document.forms[2].IBS_LISTSELECTION_VALUE.value = escape(listName);
	}
	
	if (top.window.shoppingListWindow != null) {
		top.window.shoppingListWindow.close();
	}
	submitLinkOnce(action, itemKey); 
}
function disableAllButtons() {
    var totalFormsNumber = document.forms.length;
    if (totalFormsNumber > 0) {
        for (var i=0; i<totalFormsNumber; i++) {
            var form = document.forms[i];
            for (var j=0; j<form.elements.length; j++) {
                var formElement = form.elements[j];
		if (formElement.type == "submit" || formElement.type == "button") {
                    formElement.disabled = true;
                }
            }
        }
    }
}
function disableActions() {
    if (navigator.userAgent.indexOf("Mac") < 0) {
		document.body.style.cursor = 'wait';		
	}
}
function enableActions() {
    if (navigator.userAgent.indexOf("Mac") < 0) {
		document.body.style.cursor = 'default';		
	}
}
var loaded = false;
var blocked = false;
var activeHref = null;
var loops = 0;
function invokeLink(href) {
  if(!blocked) {
	activeHref = href;
	blocked = true;
    invokeLinkWhenLoaded();
  }
}
function invokeLinkWhenLoaded() {
  if (loaded) {
  	if (navigator.userAgent.indexOf("Mac") < 0) {
  		document.body.style.cursor = 'wait';
  	}
	disableAllButtons();	
	window.location.href = activeHref;
  } else {
     loops++;
     if (loops < 720) {
     	setTimeout("invokeLinkWhenLoaded()",250);	
     } else {
        blocked = false;
		alert('Not ready loading page!');
     }
  }
}
var sbmAction = null;
var sbmValue = null;
var sbmQty = null;
var sbmUnit = null;
var sbmAlias = null;
function submitLinkOnce(action,value) {
   if(!blocked) {
	blocked = true;
        sbmAction = action;
        sbmValue = value;
       	submitLinkWhenLoaded();
   }
}
function submitLinkWhenLoaded() {
    if(loaded) {
    	if (navigator.userAgent.indexOf("Mac") < 0) {
    		document.body.style.cursor = 'wait';
    	}
		disableAllButtons();
		document.forms[2].IBS_LINKSELECTION_ACTION.name= sbmAction;
		document.forms[2].IBS_LINKSELECTION_VALUE.value= sbmValue;
		document.forms[2].submitPopupQty.value= sbmQty;
		document.forms[2].submitPopupUnit.value= sbmUnit;
		document.forms[2].submitPopupAlias.value= sbmAlias;
		document.forms[2].submit();
		if (navigator.userAgent.indexOf("MSIE")!=-1
			&& navigator.userAgent.indexOf("Mac") < 0){
			document.forms[2].IBS_LINKSELECTION_VALUE.value= "";
		} 
    } else {
        loops++;
        if (loops < 720) {
	   		setTimeout("submitLinkWhenLoaded()",250);
        } else {
        	blocked = false;
	   		alert('Not ready loading page!');
        }
    } 
}
var sbmButton = "";
function submitButtonOnce(button) {
   if(!blocked) {
		blocked = true;
        sbmButton = button.name;
        submitButtonWhenLoaded();
   }
}
function submitButtonWhenLoaded() {
    if(loaded) {
    	if (navigator.userAgent.indexOf("Mac") < 0) {
    		document.body.style.cursor = 'wait';
    	}
        disableAllButtons();
		document.forms[2].IBS_SUBMIT_ACTION.name= sbmButton;
		document.forms[2].submit();
    } else {
        loops++;
        if (loops < 720) {
	    	setTimeout("submitButtonWhenLoaded()",250);
        } else {
        	blocked = false;
	   		alert('Not ready loading page!');
        }
    } 
}
function submitenter(button,e) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	else 
		return true;
	if (keycode == 13) {
		submitButtonOnce(button);
		return false;
	} else
		return true;
}
var prevAction = "";
var sbmResAction = null;
var sbmResCol = null;
var sbmResSeq = null;
function submitResequenceFormWhenLoaded() {
   if(loaded) {
   		if (navigator.userAgent.indexOf("Mac") < 0) {
   			document.body.style.cursor = 'wait';
   		}
		disableAllButtons();
		document.forms[2].IBS_RESEQUENCE_ACTION.name= sbmResAction;
		document.forms[2].IBS_RESEQUENCE_COLUMN.value= sbmResCol;
		document.forms[2].IBS_RESEQUENCE_SEQUENCE.value= sbmResSeq;
		document.forms[2].submit();
    } else {
        loops++;
        if (loops < 720) {
	   		setTimeout("submitResequenceFormWhenLoaded()",250);
        } else {
        	blocked = false;
	   		alert('Not ready loading page!');
        }
    } 
}
function submitResequenceForm(action,column,sequence) {
   if(!blocked) {
		if (prevAction != action) {
			blocked = true;		
			prevAction = action;		
			sbmResAction = action;
			sbmResCol = column;
			sbmResSeq = sequence;
			submitResequenceFormWhenLoaded();
		}
   }
}
function toggleResequenceImage(imageName,src) {
	if (document.images) {
	       document[imageName].src = src;
	}
} 
var listName = "";
var itemKey = null;
var action = "";
var remoteWindow = null;
function openRemoteWindow(actionValue, key, jspName,windowName,x,y,h,w) {
	itemKey = key;
	action = actionValue;
	if (remoteWindow != null && !remoteWindow.closed) {// if mywindow has been opened, and mywindow hasn't already been closed
		remoteWindow.close(); //close it in order to re-assign properties
	}
       str = ",height=" + h + ",width=" + w + ",toolbar=no,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes";
	if (navigator.userAgent.indexOf("MSIE")!=-1) {
    	str = "left=" + x + ",top=" + y + str;		
    } else {
    	str = "screenX=" + x + ",screenY=" + y + str;
    }
    remoteWindow=open(jspName, windowName, str);
   	if (remoteWindow.opener == null) {
		 remoteWindow.opener = self;
  	}
	remoteWindow.focus() 
}
function openPopup(url,width,height) 
{
	openPopup(url,width,height, null) 
}

function openPopup(url,width,height,args) 
{
	var left, top;
	left = screen.width / 2 - width / 2;
	top = screen.height / 2 - height / 2;
	var features;
	if (window.showModalDialog) {
		features = "dialogWidth:" + width + "px; dialogHeight:" + height + "px; dialogLeft:" + left + "; dialogTop:" + top +
						  "; directories:no; location:no; menubar:no; resizable:no; status:no; titlebar:no; toolbar:no";
		return showModalDialog(url, args, features);
	} else {
	  	features =  "height=" + height + ",width=" + width + ",directories=no,menubar=no,resizable=no,status=no,toolbar=no,scrollbars=no,modal=yes";
	  	return window.open(url,'PopUp',features);
	}
}
function fillDay(dayString)
{
	returnValue = dayString;
	window.close();
}
function submitQuickSearchEnter(action,value,ev) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (ev) 
		keycode = ev.which;
	else 
		return true;
	if (keycode == 13) {
		submitQuickSearch(action,value);
		return false;
	} else
		return true;
}
function submitQuickSearch(action,value) {
	document.forms[1].IBS_LINKSELECTION_ACTION.name= action;
	document.forms[1].IBS_LINKSELECTION_VALUE.value= value;
	document.forms[1].submit();
	if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Mac") < 0){
		document.forms[1].IBS_LINKSELECTION_VALUE.value= "";
	} 
}
function submitSignonHoverEnter(action,value,ev) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (ev) 
		keycode = ev.which;
	else 
		return true;
	
	if (keycode == 13) {
		submitSignonHover(action,value);
		return false;
	} else
		return true;
}
function submitSignonHover(action,value) {
	document.forms[0].IBS_LINKSELECTION_ACTION.name= action;
	document.forms[0].IBS_LINKSELECTION_VALUE.value= value;
	document.forms[0].submit();
	if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Mac") < 0){
		document.forms[0].IBS_LINKSELECTION_VALUE.value= "";
	} 
}
function submitSignonHoverButtonOnce(button) {
   if(!blocked) {
		blocked = true;
        sbmButton = button.name;
        submitSignonHoverButtonWhenLoaded();
   }
}
function submitSignonHoverButtonWhenLoaded() {
    if(loaded) {
    	if (navigator.userAgent.indexOf("Mac") < 0) {
    		document.body.style.cursor = 'wait';
    	}
        disableAllButtons();
		document.forms[0].IBS_SUBMIT_ACTION.name= sbmButton;
		document.forms[0].submit();
    } else {
        loops++;
        if (loops < 720) {
	    	setTimeout("submitButtonWhenLoaded()",250);
        } else {
        	blocked = false;
	   		alert('Not ready loading page!');
        }
    } 
}
function autoTab(input, e) {
  isNN = (navigator.appName.indexOf("Netscape")!=-1);
  len = input.getAttribute("maxlength");
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}
function submitPopupLinkOnEnter(action,value,qty,unit,alias,ev) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (ev) 
		keycode = ev.which;
	else 
		return true;
	
	if (keycode == 13) {
		submitPopupLinkOnce(action,value,qty,unit,alias);
		return false;
	} else
		return true;
}
function submitPopupLinkOnce(action,value,qty,unit,alias) {
   if(!blocked) {
		blocked = true;
        sbmAction = action;
        sbmValue = value;
		sbmQty = qty;
		sbmUnit = unit;
		sbmAlias = alias;
       	submitLinkWhenLoaded(qty,unit,alias);
   }
}
function buyAndUpdate(object, action, value, targetSuccess, successLoad, event) {
	if (event != null) {
		var keycode;
		if (window.event) 
			keycode = window.event.keyCode;
		else if (ev) 
			keycode = event.which;
		else 
			return true;
		if (keycode != 13)
			return true;
	}
	document.forms[2].IBS_LINKSELECTION_VALUE.value= value;
	var servlet = object.form.action;
	var form = "#" + object.form.name;
	var data = $(form).serialize();
	data = data + '&' + action + '=' + value;
	$.post(servlet, data, function(response, status, xhr) {
		$(targetSuccess).load(successLoad, function(response, status, xhr) {
			if (status == 'error') {
				alert('error processing AJAX response');
			}
		});
		$(form).find('input[type=text]').val(''); 
		if (response.length == 0) {
		} else {
			response+= "<br/><p class=\"closeModal\"><input class=\"nyroModalClose\" type=\"button\" value=\"Close\"></p>";
			$.fn.nyroModalManual({
				content: response,
				zIndexStart: 10000,
				minHeight: 100
			});
		}
	});
	return false;
}
function performAction(servlet, action, value, targetSuccess, successLoad, displayType) {
	(function($) { 
		$.fn.center = function(options) { 
			return this.each(function(index) { 
				if (index == 0) { 
					var $this = $(this); 
					var offset = $this.parent().offset().left;
					$this.css({ 
						position: 'fixed', 
						marginTop: '0',
						top: (($(window).height() - $this.height()) / 2) + 'px',
			                  left: ((($(window).width() - offset - $this.width()) / 2) + offset) + 'px'
					}); 
				} 
			}); 
		}; 
	})(jQuery);

	document.forms[2].IBS_LINKSELECTION_VALUE.value= value;
	var data = action + '=' + value;
	$(targetSuccess).empty().html('<center><img src="/IBSStaticResources/NS_resources/ajax-loader.gif" /><center>');
	$.post(servlet, data, function(response, status, xhr) {
		$(targetSuccess).load(successLoad, function(response, status, xhr) {
			if (status == 'error') {
				alert('error processing AJAX response');
			} else {
				if (action == 'ACTION_NEXT' || action == 'ACTION_SEARCH' || action == 'ACTION_DISPLAY_STYLE') {
					refreshPriceAndBuy(displayType);
					expandable();
				}
			}
		});
		if (response.length == 0) {
		} else {
			response+= "<br/><p class=\"closeModal\"><input class=\"nyroModalClose\" type=\"button\" value=\"Close\"></p>";
			$.fn.nyroModalManual({
				content: response,
				zIndexStart: 10000,
				minHeight: 100
			});
		}
	});
}
function refreshPriceAndBuy(displayType) {

	$('#catalogue-details .priceAndBuy').each(function(i) {
		$(this).empty().html('<center><img src="/IBSStaticResources/NS_resources/ajax-loader-small.gif" /></center>');
		var data = 'ACTION_PRICING=';
		var item = $(this).attr('id');
		data = data + '&KEY_ITEM=' + item.substring(7);
		data = data + '&INDEX=' + (i+1);
		if (displayType != null) {
			data = data + '&DISPLAY_TYPE=' + displayType;
		}
		$(this).load('AJAXProcessor', data, function(response, status, xhr) {
			if (status == 'error') {
				alert('error processing AJAX response');
			}
		});
	});
	
}
function performSimpleAction(object, action, value) {
	document.forms[2].IBS_LINKSELECTION_VALUE.value= value;
	var servlet = object.form.action;
	var form = "#" + object.form.name;
	var data = $(form).serialize();
	data = data + '&' + action + '=' + value;
	$.post(servlet, data, function(response, status, xhr) {
		if (status == 'error') {
			alert('error processing AJAX response');
		}
	});
}
function performSignonSearch(servlet, action, value, target) {
	document.forms[2].IBS_LINKSELECTION_VALUE.value= value;
	var form = '#Signon';
	var data = $(form).serialize();
	data = data + '&' + action + '=' + value;
	$(target).load(servlet, data, function(response, status, xhr) {
		if (status == 'error') {
			alert('error processing AJAX response');
		}
	$(function() {
		var projects = [];
		var isFirst = true;
		$('#CustomerList tr').each(function () {
			var tablerow = $(this);
			var rowData = {};
			if (!isFirst) {
				$('td', tablerow).each(function () {
					var td = $(this);
					var className = td.attr('class');
					if (className != '') {
						rowData[className] = td.html();
					}
				});
				projects.push(rowData);
			} else {
				isFirst = false;
			}
		});
		$( "#QRY_Customer" ).autocomplete({
			minLength: 0,
			source: projects,
			focus: function( event, ui ) {
				$( "#QRY_Customer" ).val( ui.item.name );
				return false;
			},
			select: function( event, ui ) {
				$( "#QRY_Customer" ).val( ui.item.value );
				invokeLink($("#Signon").attr("action") + '?ACTION_SELECT_SALES_REP='+ui.item.value);
				return false;
			}
		})
		.data( "autocomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( "<a><span class='CustomerLabel'>" + item.name + " (<b>" + item.value + "</b>)</span><br>" + item.label + "</a>" )
				.appendTo( ul );
		};
	});
	});
}
function addPoints(object, action, value, cart, points, event) {
	if (event != null) {
		var keycode;
		if (window.event) 
			keycode = window.event.keyCode;
		else if (ev) 
			keycode = event.which;
		else 
			return true;
		if (keycode != 13)
			return true;
	}
	object.disabled=true;
	document.forms[2].IBS_LINKSELECTION_VALUE.value= value;
	var servlet = object.form.action;
	var form = "#" + object.form.name;
	var data = $(form).serialize();
	data = data + '&' + action + '=' + value;
	$(form).find('input[type=text]').val(''); 
	$.post(servlet, data, function(response, status, xhr) {
		object.disabled=false;
		if (status == 'error') {
			alert('error processing AJAX (points add) response');
		}
		$("#pointsHistory").prepend(response);
		$("#shoppingCart").load(cart, function(response, status, xhr) {
			if (status == 'error') {
				alert('error processing AJAX (cart update) response');
			}
		});
			$("#yourPoints").load(points, function(response, status, xhr) {
				if (status == 'error') {
					alert('error processing AJAX (points update)response');
				}
			});
	});
}
function expandable() {
	$('div.expandable').expander({
		slicePoint: 290,
		widow: 8,
		userCollapseText: '[less]',
		expandText: '[more]'
	});
}
