// this function dynamically sets the width on the <ul>'s for the
// intermediary pages so the drop-downs don't float over the footer
/*function sizeDropdowns () {
    var windowHeight = $(window).height();
    var footerHeight = 100;
    var headerHeight = 153;
    var contentPadding = 35;
    var navTopLevelOffset = 34;
    var itemsPerColumn = 3;
    var rowOffset = 44;
        
    contentHeight = windowHeight - footerHeight - headerHeight - contentPadding - navTopLevelOffset;
    maxChildren = '';
    
    
    $('.top_level>ul').each(function(i) {
        var thisIndex = i+1;
        var thisContentHeight = contentHeight;
        var thisRowOffset;
        
        //set row offsets
        if (thisIndex > 3) {
            var additionalRows = (thisIndex - itemsPerColumn) / itemsPerColumn;
            additionalRows = Math.ceil(additionalRows);
            thisRowOffset = rowOffset * additionalRows;
            thisContentHeight = thisContentHeight - thisRowOffset;
        }
        
        maxChildren = thisContentHeight / 24; //24 is height of a tags
        maxChildren = Math.floor(maxChildren);
        
        var $this = $(this);
        var itemCount = ($this.find('li').size());
        
        //get list item width, strip "px", and convert to integer
        var listItemWidth = $this.find('li:first').css('width');
        listItemWidth.replace("px", "");
        listItemWidth = parseInt(listItemWidth);
        
        if (itemCount > maxChildren) {
            var columnNum = Math.ceil(itemCount / maxChildren);
            $this.css('width',columnNum*listItemWidth);
        }
    });
}

THIS IS NO LONGER IN USE

*/

function returnNumber(variable) {
    variable = variable.replace("px", "");
    variable = parseInt(variable);
    return(variable);
}

$(function() {
    //breadcrumb separators
	$('.breadcrumb li:not(:last-child)').append(' > ');
    
    if ( ($('.breadcrumb').size() > 0) && ($('.fm_message').size() > 0) ) {
	    var contentWidth = $('#MainContainer').width();
    	var messageWidth = $('.fm_message').outerWidth() + 6;
    	var breadCrumbWidth = $('.breadcrumb').width();
    	var breadCrumbHeight = $('.breadcrumb').height();
    	var contentPadding = returnNumber($('.MainContent').css('padding-top'));
    	
    	if (breadCrumbWidth > (contentWidth - messageWidth) ) {
    	    var newBreadCrumbWidth = contentWidth - messageWidth - 5;
    	    $('.breadcrumb').css('width',newBreadCrumbWidth + 'px');
    	    var newBreadCrumbHeight = $('.breadcrumb').height();
    	    var heightDifference = newBreadCrumbHeight - breadCrumbHeight; 
    	    $('.MainContent').css('padding-top',(contentPadding + heightDifference) + 'px');
    	}
	}
	
    $('.product_list li').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });
    
	// equalize the height on account pages
	if ($("#MainContent ul.account_options").outerHeight() < $("#MainContent #account_form").outerHeight()) {
		$("#MainContent ul.account_options").height($("#MainContent #account_form").outerHeight())
	}
	
	// margin-top FIRST account options
	$("#MainContent ul.account_options li:first-child").css("margin-top","10px");
	
	if ($('table.exclusives').size() > 1) {
        $('table.exclusives:even').addClass('even');
    	$('table.exclusives:odd').addClass('odd');
	} else {
	    $('table.exclusives tr:even').addClass('even');
    	$('table.exclusives tr:odd').addClass('odd');
	}
	
	$('table.datagrid tr:even').addClass('even');
	$('table.datagrid tr:odd').addClass('odd');
	
	$('ul.product_list li.top_level').hover(function() {
	   
		var popupY = $(this).find('.cat_title').offset().top - $(window).scrollTop();//old->//$(this).position().top - $(window).scrollTop();
		var verticalMiddle = Math.floor($(window).height() / 2);
		
		var heightOfPopup = $(this).find('li.sub_level').parent().height();
		var windowHeight = $(window).height() + $(window).scrollTop();
		var dropDownYPlusHeight = $(this).find('.cat_title').offset().top + $(this).find('.cat_title').height();
		
		var popupLocationHeight = dropDownYPlusHeight + heightOfPopup;
   
		var topSpace = $(window).height() - (($(window).height() - popupY) + heightOfPopup);
		
		/*
		* If the popup is above the vertical half way point change css for the popup menu to go below the cat_title bar.
		* If the popup is above the vertical half way point but the menu is to long and goes past the browser then add the menu above the cat_title bar.
		* If the popup is above the vertical half way point and the menu is to long then set the height to add scrollbar and set it below cat_title.
		*/
		if(popupY < verticalMiddle) {
		   if(popupLocationHeight > windowHeight){
			   $(this).find('ul').css('top', null);
			   $(this).find('ul').css('bottom', '25px');
			   if(topSpace < 0){
					$(this).find('ul').css('height',24*5);
					$(this).find('ul').css('top', '120px');
			   		$(this).find('ul').css('bottom', null);
				}
		   }
		   if(popupLocationHeight < windowHeight){
			   $(this).find('ul').css('top', '120px');
			   $(this).find('ul').css('bottom', null);
		   }
		}
		/*
		* If the popup is below the vertical half way point change css for the popup menu to go above the cat_title bar.
		* If the popup is below the vertical half way point and is too high for the browser set the height to add scrollbar.
		*/
		if(popupY > verticalMiddle) {
		   $(this).find('ul').css('top', null);
		   $(this).find('ul').css('bottom', '25px');
		   if(topSpace < 0){
				$(this).find('ul').css('height',24*5);
			}
		}
		
		$(this).addClass('hover').css('z-index','10000');

	}, function() {
		$(this).removeClass('hover').css('z-index','1');
		$(this).find('ul').css('height',null);
	});
	
	$('.account input.clearandpopulate,.search input.clearandpopulate').each(function(index) {
	   if ($(this).attr('value') =='') {
	       var thisTitle = $(this).attr('title');
	       $(this).attr('value',thisTitle);
	   }
	});
	
	/*$('.product_list ul').each(function(index) {
	   var $this = $(this);
	   var childrenCount = $this.children('li.sub_level').size();
	   if (childrenCount > 15) {
	       $this.css('height',24*15);
	   }
	});*/
	
	
});

function jSearch() {
  self.location='/cgi-bin/mvi.exe/CATALOG.BROWSE?searchtext=' + escape(document.mainForm.searchtext.value);
  return;
}
function loginUser() {
  var userid = document.loginForm.userid.value;
  var password = document.loginForm.password.value;
  var url = '/cgi-bin/mvi.exe/CUST.LOGIN?userid=' + userid + "&password=" + password;
  if (userid == "") {
    alert("Please enter a valid userid."); 
	document.loginForm.userid.focus();
	return false;
  }
  if (password == "") {
    alert("Please enter a valid password.");
	document.loginForm.password.focus();
	return false;
  }
  //var x = window.open( url, 'loginWindow', 'width=500,height=400,scrollbars=no,');
  return true;
}
function prodjump() {
  var catid = document.jumpForm.jumpcat[document.jumpForm.jumpcat.selectedIndex].value;

  self.location='/cgi-bin/mvi.exe/CATALOG.BROWSE?catid=' + catid;
  return;
}
function topMenu( b_name ) {
  switch (b_name) {
    case 'button1':
      document.location = '/cgi-bin/mvi.exe/LOAD.STATIC?sid=specialfurniture';
    break;
    case 'button2':
      document.location = '/cgi-bin/mvi.exe/CATALOG.BROWSE?catid=1*1200001';
    break;
    case 'button3':
      document.location = '/cgi-bin/mvi.exe/CATALOG.BROWSE?catid=2*1400020';
    break;
    case 'button4':
      document.location = '/cgi-bin/mvi.exe/CATALOG.BROWSE?catid=2*1400021';
    break;
    case 'button5':
      document.location = '/cgi-bin/mvi.exe/CATALOG.BROWSE?catid=1*1200002';
    break;
    case 'button6':
      document.location = '/cgi-bin/mvi.exe/CATALOG.BROWSE?catid=2*1400012';
    break;
    case true:
      alert( b_name );
    break;
  }
}
function validateUSNumber( item ) { 
 var CleanedString=""; 
 var index = 0; 
 var LimitCheck; 
 var InitialString = item.value

 if (item.value == '') return true;

 //Get the length of the inputted string, to know how many characters to check
 LimitCheck = InitialString.length;
 
 //Walk through the inputted string and collect only number characters, appending them to CleanedString
 while (index != LimitCheck) { 
  if (isNaN(parseInt(InitialString.charAt(index)))) { } 
  else { CleanedString = CleanedString + InitialString.charAt(index); } 
  index = index + 1; 
 }
 
 //If CleanedString is exactly 10 digits long, then format it and allow form submission
 if (CleanedString.length == 10) { 
  item.value = CleanedString.substring(0,3) + "-" + CleanedString.substring(3,6) + "-" + CleanedString.substring(6,10); 
 }
 
 //If CleanedString is not 10 digits longs, show an alert and cancel form submission
 else { 
  CleanedString = InitialString; 
  alert("Please enter only your ten digit phone number.");
  if (document.all) { 
    item.focus();
    item.select();
  }
 return false
 } 
} 
function viewOrder( type, options, orderid ) {
  var url='/cgi-bin/mvi.exe/VIEW.ORDER?type=' + type + '&options=' + options + '&orderid=' + orderid;
  var x = window.open( url, 'orderWindow', 'width=700,height=380,scrollbars=yes');
}
function refreshOpener() {
  if (opener) {
    opener.history.go(0);
    window.focus();
  }
  return;
}
function addItemToQuickList( item, qty ) {
  var url='/cgi-bin/mvi.exe/QLIST.ADD?item=' + item + '&qty=' + qty;
  var x = window.open( url, 'QuickWindow', 'width=400,height=400,scrollbars=no');
}
function stext() {
  if (document.mainForm) {
    if (document.mainForm.searchtext) {
      if (document.mainForm.searchtext.value == '') {
        document.mainForm.searchtext.value = 'Keyword, Part #, UPC';
      }
    }
  }
}
function stextCheck() {
      if ( document.mainForm.searchtext.value == 'Keyword, Part #, UPC') {
        document.mainForm.searchtext.value = ''; 
      }
}
function maintainWebUser( edituser, cid ) {
  var url='/cgi-bin/mvi.exe/WEB.USER.MAINT?edituser=' + edituser + '&cid=' + cid;
  var x = window.open( url, 'userMaintWindow', 'width=500,height=380,scrollbars=no');
}
function taxExemptCheck(fieldObject, nextObject) {

  if (fieldObject.value != '') {
    if (confirm("NOTE: In order to be considered tax exempt, all of the addresses that you ship orders to from this web site must be tax exempt.  Are you sure that you are tax exempt?")) {
      nextObject.focus();
    } else {
      fieldObject.value = '';
      nextObject.focus();
    }
  }

}
function showCCAlert() {
  if (document.regform) {
    if (document.regform.terms) {
      if(document.regform.terms.checked = true) {
        if (confirm("WARNING: Even though you are using a credit card to process this order, it will still show up on your Company's statement.  If you would like to place a personal order, please create a personal account.")) {

         } else {
           document.regform.terms.checked = false;
         }
      }
    }
  }
  return;
}

