        function getElementSafe(id)
            {
	            var retval;
	            if(document.getElementById){
		            retval = document.getElementById(id);
	            }
	            else if(document.all){
		            retval = document.all[id];
	            }
	            else if(document.layers)
	            {
		            retval = document.layers[id];
	            }
	            return retval;
            }
            
        function popUp(url, width, height, menubar, name, top, left) 
		{
			var strName
			if(!top)
			{
				if(height)
					top = (screen.height / 2 - height / 2);
				else
					top = 50;
			}
			if(!left)
			{
				if(width)
					left = (screen.width / 2 - width / 2);
				else
					left = 100;
			}
			if(name) strName = name; else strName = "popup"
			if(menubar)
				window.open(url,strName,'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);
			else
			{
				window.open(url,strName,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);
			}
		}
		
		function popUpDrilldown(url,name, width, height)
		{
			if (width == null)
				width = 800;
			if (height == null)
				height = 600;
			popUp(url, width, height, false, name);
		}
        
        
        
        function openModalPopup(productid)
        {
            var popup = $find('modalPopupExtenderBehavior');
            popup.set_dynamicContextKey(productid);
            popup.add_populated(populatedHandler); 
            popup.show();
        }
        
        function populatedHandler(sender, args)
        {
            var popupContent = $get('ctl00_ContentPlaceHolder1_pnlPopup').innerText;
            var result=popupContent.split('|##|')
            $get('ctl00_ContentPlaceHolder1_pnlPopup').style.width = result[1];
        }
        
        function openMenuPopup()
        {
            var hovermenu = $find('hoverMenuCategoryBehavior');
            
            hovermenu.set_dynamicContextKey(1);
            hovermenu.showPopup();
        }
        function closeMenuPopup()
        {
            var popup = $find('hoverMenuCategoryBehavior');
            
            popup.hidePopup();
        }
        
        function closeModalPopup(extendername)
        {
            var popup = $find(extendername);
            
            popup.hide();
        }
        
        function printModalPopup(productId)
        {
            popUpDrilldown('PrintProduct.aspx?productid=' + productId,'Edit', 550);
        }
            
        function openLogin()
        {
            var popup = $find('loginPopupExtenderBehavior');
            popup.show();

        }

        var currentRedirectPage = "";

        function redirectLogin() {
            window.location = currentRedirectPage;

        }
        
        function addItem(productId)
        {
            Labs.Web.Services.ServiceMethods.SaveCartItem(productId, getElementSafe('tbQty').value, addItemSucceededCallback);
        }
        
        // This is the callback function that
        // processes the Web Service return value.
        function addItemSucceededCallback(result)
        {
            alert(result);
            closeModalPopup('modalPopupExtenderBehavior');
        }
        
        function validateLogin(url) {
            currentRedirectPage = url;
            Labs.Web.Services.ServiceMethods.ValidateLogin(url, validateSucceededCallback);
        }
        
        function validateSucceededCallback(result) {
            if (result == "false") // user not logged in
                openLogin();
            else
                window.location = result;
        }

        
        function setCookie(c_name,value,expiredays)
            {var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
            document.cookie=c_name+ "=" +escape(value)+
            ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
        } 
        
        
        
        function getCookie(c_name)
        {
        if (document.cookie.length>0)
          {
          c_start=document.cookie.indexOf(c_name + "=");
          if (c_start!=-1)
            { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
            } 
          }
        return "";
        } 
        
        function searchProducts(fieldName)
        {				
	        if (document.forms[0].elements[fieldName] != null && document.forms[0].elements[fieldName].value != "")
	        {
		        var SearchTerm = document.forms[0].elements[fieldName].value;
		        document.location = "SearchReturn.aspx?keyword=" + SearchTerm;																			
		        
	        }

        }

        function KeyPressFunction(buttonType,e)
        {

	        if(document.all) 
	        { 
		        var e = window.event.keyCode; 
	        }
	        else
	        { 
		        var e = e.which; 
	        } 
	        if (e == 13) 
	        { 
		        
	            searchProducts(buttonType);
	            event.returnValue = false;
	            return false;
		       
	        }
        } 

        function ShowCatalogSimplified(CatalogOID, ticket) 
        {

        ShowCatalog(CatalogOID,ticket,"ViewCatalog.aspx?Left=1:amp:Right=-1:amp:CatalogOID=" + CatalogOID);
        } 

        function ShowCatalog(CatalogOID, Ticket, ShowPage)
        {

        var width = 975;
        var height = 686;
        var property = "top=0, left=0, resizable=1, width=" + width + ", height=" + height;
        var wincatalog = window.open("http://www.activemerchandiser.com/CatalogView.aspx?CatalogOID=" + CatalogOID + "&SupplierID=401148&ShowPage=" + ShowPage + "&Password=" + Ticket + "&Rand=" + Math.random(), 'CatalogViews', property);
        wincatalog.focus();
        }
