// Strip leadin, trailing and multiple spaces.
String.prototype.Trim = function () 
{
	return this.replace(/^\s+|\s+$/g,'').replace(/\s{2,}/g, ' '); 
}

function addItemToQuickList( item, qty ) {
	var url='quicklistAdd.php?productID=' + item + '&qty=' + qty;
	var x = window.open( url, 'QuickWindow', 'width=400,height=400,scrollbars=no');
}

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=BaseURL+'viewOrder.php?type=' + type + '&options=' + options + '&orderid=' + orderid;
	var x = window.open( url, 'orderWindow', 'width=800,height=550,scrollbars=1,resizable=1');
}

function removeItem( id ) {
	if (confirm('Really remove this category from the database?')) {
		document.rmItemForm.remove.value = "Remove";
		document.rmItemForm.submit();
	} else {
		return false;
	}
}

/* provides hover support for li elements in #categorylisting */
function startList() {
	if (document.all&&document.getElementById("categorylisting")) {
		navRoot = document.getElementById("categorylisting");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" hover";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" hover", "");
				}
			}
		}
	}
}

function fixFFlayout()
{
	if(!document.all){
		if(document.getElementById("content").offsetWidth>"825"){
			document.getElementById("container").style.width=document.getElementById("container").offsetWidth+180+"px";
			document.getElementById("container").style.maxWidth=document.getElementById("container").offsetWidth+180+"px";
			document.getElementById("maincontent").style.width=document.getElementById("content").offsetWidth+"px";
			document.getElementById("maincontent").style.maxWidth=document.getElementById("content").offsetWidth+"px";
		}
	}

}

window.onload= function() {
	startList();
	fixFFlayout();
}

/* Functions for pseudo "combo-box".
   Requires a text type input element (txtElmnt), 
   a select element (selElmnt),
   and a hidden type input element (hidElmnt).
*/
function createSelection(txtElmnt, posStart, posEnd) {
   if (txtElmnt.createTextRange) {
      var selRange = txtElmnt.createTextRange();
      selRange.collapse(true);
      selRange.moveStart('character', posStart);
      selRange.moveEnd('character', posEnd);
      selRange.select();
   } else if (txtElmnt.setSelectionRange) {
      txtElmnt.setSelectionRange(posStart, posEnd);
   } else if (txtElmnt.selectionStart) {
      txtElmnt.selectionStart = posStart;
      txtElmnt.selectionEnd = posEnd;
   }
}

function ehComboTextChanged(evt, txtElmnt, hidId) {
   var hidElmnt;

   hidElmnt = document.getElementById(hidId);

   //if (txtElmnt.value.toUpperCase() == 'NONE') {
   //   txtElmnt.value = 'NONE';
   //   hidElmnt.value = '';
   //} else {
      hidElmnt.value = txtElmnt.value;
   //}
}

function ehComboTextKeyDown(evt, txtElmnt, selId) {
   var selElmnt;

   selElmnt = document.getElementById(selId);

   switch (evt.keyCode) {
      case 40:
         selElmnt.focus();
         break;
      case 38:
         selElmnt.focus();
         break;
   }
}

function ehComboTextKeyUp(evt, txtElmnt, selId, hidId) {
   var hidElmnt;
   var result = true;
   var selElmnt;

   selElmnt = document.getElementById(selId);
   hidElmnt = document.getElementById(hidId);

   var txtVal = txtElmnt.value.toLowerCase();
   var txtValLen = txtVal.length;

   if ((evt.keyCode >= 65 && evt.keyCode <= 90) || (evt.keyCode == 32)) {
      for (var i = 0; i < selElmnt.length; i++) {
         var opt = selElmnt.options[i];
         if (opt.label.substr(0, txtValLen).toLowerCase() == txtVal) {
            if (txtElmnt.value != opt.label) {
               hidElmnt.value = opt.value;
               txtElmnt.value = opt.label;
               createSelection(txtElmnt, txtValLen, txtElmnt.value.length);
               result = false;

               selElmnt.selectedIndex = i;
            }

            break;
         }
      }
   }

   evt.returnValue = result;
   return result;
}

function ehComboSelectChanged(evt, selElmnt, txtId, hidId) {
   var txtElmnt;
   var hidElmnt;

   txtElmnt = document.getElementById(txtId);
   hidElmnt = document.getElementById(hidId);

   txtElmnt.value = selElmnt.options[selElmnt.selectedIndex].label;
   hidElmnt.value = selElmnt.options[selElmnt.selectedIndex].value;
   txtElmnt.focus();
   createSelection(txtElmnt, 0, 0);
}

function ehComboSelectKeyUp(evt, selElmnt, txtId) {
   var txtElmnt;

   txtElmnt = document.getElementById(txtId);

   txtElmnt.focus();
}
