/*
addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
http://simon.incutio.com/archive/2004/05/26/addLoadEvent
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* Add more childitems to a page :
UrlList[parent]=new Array("child1","child2") etc.
please note that all names  are LowerCase
*/

var UrlList = new Object();
UrlList["orderlist.aspx"]= new Array("ordersheet.aspx,myaccount.aspx,changepassword.aspx");
UrlList["carselect.aspx"]= new Array("carselect.aspx","catalog.aspx,productsheet.aspx");



function getPageFromUrl(url)
{
	var urlParts = url.split('/');
	var pageParts = urlParts[urlParts.length-1].split('?');
	var currentPage = pageParts[0].toLowerCase();
	return currentPage;
}
	


//this handles the topnav...
addLoadEvent(function() {	
	if (!document.getElementById("nav")) return;
	var url = location.href.toLowerCase();
	var nav = document.getElementById("nav").getElementsByTagName("A");
	for(i=0; i < nav.length; i++) 
	{
		
		var CurNavPage = getPageFromUrl(nav[i].href.toLowerCase())	
		if ((url.indexOf(nav[i].href.toLowerCase()) > -1) && (url.length <= nav[i].href.length))
		{
			if (nav[i].parentNode.nodeName == "LI")	nav[i].parentNode.className += " active";			
		}
		else
			if(UrlList[CurNavPage]!=null)
			{
				var sChildUrls = UrlList[CurNavPage].join(',');
				var currentPage = getPageFromUrl(url);
				if(sChildUrls.indexOf(currentPage)>-1 && nav[i].parentNode.nodeName == "LI")
				{
					nav[i].parentNode.className += " active";
				}
			}
	}
});

//add and remove " hover" to the class.
function hover(ob){
	ob.className+=' hover';
}
function unhover(ob){
	rExp = / hover/gi;
	ob.className = ob.className.replace(rExp, '')
}

// Handles automatic pop-ups in the search bar
var lastSearchSelectedIndex = 0;
addLoadEvent(function() {
	lastSearchSelectedIndex = getSearchSelectedIndex;
});
function getSearchSelectedIndex(searchDropDownID){
	var searchDDL = document.getElementById( ( searchDropDownID != null ? searchDropDownID : 'Search1_searchtype'));
	if( searchDDL != null )
		return searchDDL.selectedIndex;
	else
		return 0;
}
function searchChanged(biltorvetValue, bilsynValue, searchDropDownID) {
	var searchDDL = document.getElementById(searchDropDownID);
	if( searchDDL != null )
	{	
		// Handle popup events
		switch(parseInt(searchDDL[getSearchSelectedIndex(searchDropDownID)].value))
		{
			case biltorvetValue:
				// Spawn pop-up
				searchPopup("http://www.biltorvet.dk/", "biltorvet");
				// Set the dropdown list back to its previous position
				searchDDL.selectedIndex = lastSearchSelectedIndex;
				break;
			case bilsynValue:
				// Spawn pop-up
				searchPopup("http://147.29.112.249/discoverer5i/topti", "bilsyn");
				// Set the dropdown list back to its previous position
				searchDDL.selectedIndex = lastSearchSelectedIndex;
				break;
		}
	
		// Remember this as the last index
		lastSearchSelectedIndex = searchDDL.selectedIndex;
	}
}
function searchPopup(url, name){
	window.open(url, name);
}

function setLoginPageFocus(userCtlID, passwordCtlID, searchRegId)
{
	var boxCustomerNumber = document.getElementById(userCtlID);
	var boxPassword = document.getElementById(passwordCtlID);
	var boxSearch = document.getElementById('ctl00$Search1$searchValue');
	
	if( boxCustomerNumber != null && typeof(boxCustomerNumber) != "undefined" && boxCustomerNumber.value == "" )
	{
		boxCustomerNumber.focus();
	}
    else if (boxPassword != null && typeof (boxPassword) != "undefined") {
		boxPassword.focus();
    }
    else if (boxSearch != null && typeof (boxSearch) != "undefined") {
        boxSearch.focus();
    }

    var searchReg = document.getElementById('ctl00$ContentPlaceHolder1$SearchRegNr$searchReg');
    
    if (searchReg != null)
    {
        searchReg.focus();
    }

    var passwordTop = document.getElementById('ctl00$TopTabs1$tabMenuRepeater$ctl07$CostumerNavigation2$LoginSmall1$password');
    if (passwordTop != null)
        passwordTop.focus();
    
    
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


//
// Keyboard shortcut code
//

// Add the hook at load time
addLoadEvent(function() {
	document.onkeyup = KeyCheck;
});

// Define supported keyboard shotcuts
var key_sc = new Array();
key_sc[113] = function() { location.href='SwitchCustomer.aspx' }; // F2

// The method doing the checking
function KeyCheck(e)
{
	// Cross browser compatibility
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	
	// Call method for this key, if defined
	if( typeof(key_sc[KeyID]) == 'function' )
		key_sc[KeyID]();
}


//
// Basket UI handling code
//

// A single method to handle it all
function basketSendOrderDisplay()
{
	basketHideButtons();
	basketDisplayLoadScreen();
}

// Hide the buttons in the basket while the order is committing
function basketHideButtons()
{
	basketHideElmIfExist('btnEmptyBasket1');
	basketHideElmIfExist('BasketButtonRow1_tblButtons');
	basketHideElmIfExist('BasketButtonRow2_tblButtons');
}
function basketHideElmIfExist(elmId)
{
	var elm = document.getElementById(elmId);
	if (elm != null)
	{
	    if( typeof(elm) != 'undefined' && typeof(elm.style) != 'undefined' )
		    elm.style.display = 'none';
	}
}

// Show the load "screen"
function basketDisplayLoadScreen()
{
	var elm = document.getElementById('tblBasketWaitBox');
	if (elm != null)
	{
	    if( typeof(elm) != 'undefined' && typeof(elm.style) != 'undefined' )
	    {
		    elm.style.display = 'block';
	    }
	}
}


//var message="Sorry, that function is disabled.\n\nContents & Graphics Copyright İyour name\nOur work is not Public Domain, and should NOT be taken from this site."; // Message for the alert box
//var message="Contents & Graphics Copyright www.meca.se "+String.fromCharCode(169)+"2007 MECA."; // Message for the alert box
/*
var message=""
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
*/
