var docLayers = (document.layers) ? true:false;
var ns6=document.getElementById&&!document.all

function ChangeStyleOfActiveMenuLink(menu_id)
{
	document.getElementById(menu_id).style.textDecoration="underline";
}

function ObjectPosition(obj) {
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return [curleft,curtop];
}

function adjDivPos(event,u,zip,address,el,rid)
{
	var x = document.getElementById("zipDiv");
	if (!event) {
	event = window.event;
	}
	
	posX = getMouseXPos(event)-30;
	/*posY = getMouseYPos(event)+50;*/
	posY = ObjectPosition(el)[1]+80;
	
	if(parseInt(posX) > 700) posX = getMouseXPos(event) - parseInt(x.style.width);
	
	x.style.top=posY+"px";
	x.style.left = posX+'px';
	
	document.getElementById("deliverytype").value = u;
	document.getElementById("f_address").value = address;
	document.getElementById("f_zip").value = zip;
	document.getElementById("restid").value = rid;
	/*document.getElementById("uri").value = u;*/
	x.style.display = "block";
	document.getElementById("f_address").focus();
	
	return false;
}

function getMouseXPos(e)
{
	if (document.layers||ns6) return parseInt(e.pageX+10);

	return (parseInt(event.clientX+10) + parseInt(document.body.scrollLeft));
}

function getMouseYPos(e) 
{
	if (document.layers||ns6) return parseInt(e.pageY);

	return (parseInt(event.clientY) + parseInt(document.body.scrollTop));
}

function bookmarksite(url,title)
{
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

function ChangeLogos(page)
{
	var url = 'dispatch/changelogos.php?page='+page;
	
	var myAjax = new Ajax.Updater('logos',url,{ method: 'get' });
}

function CheckInput(el, action, text)
{
	if(action=='onfocus')
	{
		if(el.value==text)
			el.value='';
	}
	
	if(action=="onblur")
	{
		if(el.value.length==0)
			el.value=text;
	}
}

function ShowErrorMessage(id)
{
	if($(id).style.display=='none')
		Effect.toggle(id,'slide',{duration:0.5});
}

function HideErrorMessage(id)
{
	if($(id).style.display!='none')
		Effect.toggle(id,'slide',{duration:0.5});
}

function ShowHideDiv(id, time)
{
	if(!time || time=="undefined")
	{
		var time = 0.5;
	}
	
	Effect.toggle(id,'slide',{duration:time});
}

function ChangeImage(elementID, checkElementID, firstImage, secondImage)
{
	if($(checkElementID).style.display=="none")
	{
		src = secondImage;
	}
	else
	{
		src = firstImage;
	}
	$(elementID).src = src;
}

function ChangeTitle(elementID, firstTitle, secondTitle)
{
	if($(elementID).innerHTML==firstTitle)
	{
		$(elementID).innerHTML = secondTitle;
	}
	else
	{
		$(elementID).innerHTML = firstTitle;
	}
}
	
/**************************SOME STANDART FUNCTIONS*********************************/
function Document_Click(e)
{
	if (!e) var obj = window.event.srcElement;
	else var obj = e.target;
	while (obj.nodeType != 1)
	{
		obj = obj.parentNode;
	}
	if (obj.tagName == 'DIV') return;
	
	if(typeof g_oMenu != 'undefined' && g_oMenu)
	{
		g_oMenu.style.display = "none"
		g_oMenu = null;
		document.onclick = null;
	}
	else return;
}

function HideMenu(div)
{
	if(typeof g_oMenu != 'undefined' && g_oMenu && (div.id != g_oMenu.id))
	{
		g_oMenu.style.display = "none"
		g_oMenu = null;
		document.onclick = null;
	}
	else return;
}

function switchdiv(div_1, div_2)
{
	if (document.getElementById)	{
	    if(!document.getElementById(div_1)) return ;
	    if(!(document.getElementById(div_1).style)) return ;
	    if(!(document.getElementById(div_1).style.display)) return ;

		var state_1 = document.getElementById(div_1).style.display;
		if(state_1=="none") {
        		document.getElementById(div_1).style.display="block";
        		document.getElementById(div_2).style.display="none";
	     }
	    if(state_1=="block") {
        		document.getElementById(div_2).style.display="block";
        		document.getElementById(div_1).style.display="none";
	     }
	}
	else if (document.all)	{
	    if(!document.all[div_1]) return ;
	    if(!(document.all[div_1].style)) return ;
	    if(!(document.all[div_1].style.display)) return ;

		var state_1 = document.all[div_1].style.display;
		if(state_1=="none") {
		        document.all[div_1].style.display = "block";
		        document.all[div_2].style.display = "none";
		}
		if(state_1=="block") {
        		document.getElementById(div_1).style.display="none";
        		document.getElementById(div_2).style.display="block";
	     }
    }
}

function showhide()
{
	var nArguments = arguments.length;
	
	function __showhide_one_argument(div)
	{
		HideMenu(document.getElementById(div));
		state = document.getElementById(div).style.display;
		g_oMenu = document.getElementById(div);
		
		if(state == "none")
		{
			g_oMenu.style.display="block";
			g_oMenu.style.visibility = 'visible';
			g_oMenu = document.getElementById(div);
			setTimeout("document.onclick = Document_Click",100);
		}
		else
		{
			g_oMenu.style.display="none";
			g_oMenu.style.visibility = 'visible';
			g_oMenu = null;
		}
		g_oMenu = document.getElementById(div);
		
	}
	
	function __showhide_two_arguments(div,ensurevis)
	{
//		HideMenu(document.getElementById(div));
		state = document.getElementById(div).style.display;
		g_oMenu = document.getElementById(div);
		
		if(state == "none") { g_oMenu.style.display="block";}
		else { g_oMenu.style.display = "none";}
		g_oMenu.style.visibility = 'visible';
	}
		
	if(nArguments == 1) return __showhide_one_argument(arguments[0]);
	else if(nArguments == 2) return __showhide_two_arguments(arguments[0],arguments[1]);
	else return false;
}

// Multiple combo functions =============================

function moveUpList(listField)
{
	if ( listField.length == -1) {  // If the list is empty
	  alert("There are no values which can be moved!");
	} else {
	  var selected = listField.selectedIndex;
	  if (selected == -1) {
	     alert("You must select an entry to be moved!");
	  } else {  // Something is selected
	     if ( listField.length == 0 ) {  // If there's only one in the list
	        alert("There is only one entry!\nThe one entry will remain in place.");
	     } else {  // There's more than one in the list, rearrange the list order
	        if ( selected == 0 ) {
	           alert("The first entry in the list cannot be moved up.");
	        } else {
	           // Get the text/value of the one directly above the hightlighted entry as
	           // well as the highlighted entry; then flip them
	           var moveText1 = listField[selected-1].text;
	           var moveText2 = listField[selected].text;
	           var moveValue1 = listField[selected-1].value;
	           var moveValue2 = listField[selected].value;
	           listField[selected].text = moveText1;
	           listField[selected].value = moveValue1;
	           listField[selected-1].text = moveText2;
	           listField[selected-1].value = moveValue2;
	           listField.selectedIndex = selected-1; // Select the one that was selected before
	        }  // Ends the check for selecting one which can be moved
	     }  // Ends the check for there only being one in the list to begin with
	  }  // Ends the check for there being something selected
	}  // Ends the check for there being none in the list
}

//Hopefully the code makes sense. It is commented pretty well, so that should help. For moving down in the list, the code is quite similar:

function moveDownList(listField)
{
	if ( listField.length == -1) {  // If the list is empty
	  alert("There are no values which can be moved!");
	} else {
	  var selected = listField.selectedIndex;
	  if (selected == -1) {
	     alert("You must select an entry to be moved!");
	  } else {  // Something is selected
	     if ( listField.length == 0 ) {  // If there's only one in the list
	        alert("There is only one entry!\nThe one entry will remain in place.");
	     } else {  // There's more than one in the list, rearrange the list order
	        if ( selected == listField.length-1 ) {
	           alert("The last entry in the list cannot be moved down.");
	        } else {
	           // Get the text/value of the one directly below the hightlighted entry as
	           // well as the highlighted entry; then flip them
	           var moveText1 = listField[selected+1].text;
	           var moveText2 = listField[selected].text;
	           var moveValue1 = listField[selected+1].value;
	           var moveValue2 = listField[selected].value;
	           listField[selected].text = moveText1;
	           listField[selected].value = moveValue1;
	           listField[selected+1].text = moveText2;
	           listField[selected+1].value = moveValue2;
	           listField.selectedIndex = selected+1; // Select the one that was selected before
	        }  // Ends the check for selecting one which can be moved
	     }  // Ends the check for there only being one in the list to begin with
	  }  // Ends the check for there being something selected
	}  // Ends the check for there being none in the list
}

function allSelect(el)
{
	for (i=0;i<el.length;i++) el.options[i].selected = true;
}

// End Multiple combo fncs

function SwichImgs(img,src)
{
	document.getElementById(img).src = src;
}

function openInNewWin(sUrl, sTitle, sWidth, sHeight)
{
  oNewWin = window.open(sUrl, sTitle, "width="+sWidth+",height="+sHeight+",resizable");
  oNewWin.focus();
}

