
window.onload = init;

var onLoads = new Array();

function updateBasketCount(count)
    { 
        var basketText = document.getElementById('ctl00_ucBasket_tmNoArticle1');
        var firstword;
        if( basketText == null )
        {
            // We have items in basket so count is in separate object.
            firstword = document.getElementById('ctl00_ucBasket_lblArticleQuantity1');
            firstword.innerHTML = String((Number(firstword.innerHTML) + Number(count)));
            document.getElementById('ctl00_ucBasket_lblArticleQuantity2').innerHTML = firstword.innerHTML;
        }
        else
        {
            // No items in basket so modify "No articles" string.
            firstword = basketText.innerHTML.substring(0, basketText.innerHTML.indexOf(' '));
            if( isNaN(firstword) )
            {
                firstword = count;
            }
            else
            {
                firstword = String(Number(firstword) + Number(count));
            }

            basketText.innerHTML = firstword + basketText.innerHTML.substring(basketText.innerHTML.indexOf(' '), basketText.innerHTML.length);
            document.getElementById('ctl00_ucBasket_tmNoArticle2').innerHTML = basketText.innerHTML;
        }
        
        // re-initializing the basket
        aB.startHeight = 121;
        aB.fps = aB.startHeight / 9;
        aB.currHeight = 0;
        document.getElementById('shoping_container').style.height = "30px";
        document.getElementById('shoping_container').style.top = "0px";
        document.getElementById('testButton').onclick = aB.start;
        document.getElementById('testButton2').onclick = aB.start;
        aB.close();
        
        // setting the focus on the basket
        document.getElementById('testButton2').focus();
        //window.scrollTo(window.top.screenX,window.top.screenY);
        
        // slide-open the basket
        aB.start();
        
        // close the basket after aB.autoCloseInterval milisecond
        setTimeout('closeBasketIfOpen()',aB.autoCloseInterval);
         
    }    

function asyncEndRequest(sender, args)
{ 
    // re-initializing the basket
    aB.getBasketHeight();
}

// Function to the basket only if it is open.   
function closeBasketIfOpen()
{
	//  basket is open when currentHeight is less than minHeight
	if(aB.minHeight < aB.currHeight)
	    { 
	        aB.start(); 
	        return true; 
	    }
}
		
    

function init() {
	for (var i = 0; i < onLoads.length; i++) {
		eval(onLoads[i]);
	}
}

function addOnLoad(func) {
	onLoads[onLoads.length] = func;
}


// Global vars
var leftMenuLevel1Id = "level1";
var leftMenuLevel2Class = "level2";

// Set onclicks
addOnLoad("setOnClick()");

function setOnClick(){
	document.getElementById("departments").onclick = changeTab;
	document.getElementById('your-pages').onclick = changeTab;
	document.getElementById('support').onclick = changeTab;
	document.getElementById('login').onclick = changeTab;
}

// Fetch the height of shoping_container and close the basket. and open if product is added to it.

addOnLoad("aB.getBasketHeight()");

var aB = {
	minHeight: 29,
	currHeight: null,
	startHeight: null,
	interval: 40,
	autoCloseInterval: 5000,
	running: null,
	fps: null,
	timer: null,
	
    getBasketHeight: function(){
        if(document.getElementById('mast_body_wrapper')){
            aB.startHeight = document.getElementById('shoping_container').offsetHeight;
            aB.fps = aB.startHeight / 9;
            aB.currHeight = 0;
            document.getElementById('shoping_container').style.height = "30px";
            document.getElementById('shoping_container').style.top = "0px";
            document.getElementById('testButton').onclick = aB.start;
            document.getElementById('testButton2').onclick = aB.start;
            aB.close();
        }
    },
	start: function(){
		if(aB.minHeight < aB.currHeight){
			if(aB.running == null){
				aB.timer = setInterval(aB.close, aB.interval);
			}else{
				return false;
			}
		}
		else if(aB.minHeight >= aB.currHeight){
                if(aB.running == null){
				aB.timer = setInterval(aB.open, aB.interval);
			}else{
				return false;
			}
		}
		return false;
	},
	stop: function(){
		clearInterval(aB.timer);
		aB.running = null;
	},
	close: function(){
		aB.running = 1;
		aB.currHeight -= aB.fps;
		document.getElementById('mast_body_wrapper').style.visibility = "hidden";
		
        if(parseInt(aB.currHeight - 3) > 0)
    		document.getElementById('shoping_container').style.height = parseInt(aB.currHeight - 3) +"px";
    	else
    	    document.getElementById('shoping_container').style.height = "0px";
		if(aB.currHeight <= aB.minHeight){
			document.getElementById('shoping_container').style.height = "27px";
			aB.stop();
		}
	},
	open: function(){
        aB.running = 1;
		aB.currHeight += aB.fps;
		document.getElementById('shoping_container').style.height = aB.currHeight+"px";
        if(aB.currHeight >= aB.startHeight){
			document.getElementById('mast_body_wrapper').style.visibility = "visible";
			document.getElementById('shoping_container').style.height = aB.startHeight - 5 +"px";
			aB.stop();
		}
	}
	
}

function goHome(url){
    window.location.href = url;   
}

function delayLinkSetup(){
    var allLinks = document.getElementsByTagName("a");
    for(i = 0; i < allLinks.length; i++){
        if(allLinks[i].onclick == null)
            allLinks[i].onclick = delayLink;
    } 
}
function delayLink(){
    var delay = 0;
    window.setTimeout("function(){return true;}", delay);
}

function GetYPSubmenu(){
 if(document.getElementById('submenu_yp'))
        return document.getElementById('submenu_yp');
    else
        return document.getElementById('submenu_yp_loggedin');
}
// function to return the div containing login submenu. 
// the id of login submenu will be different depending on the login status of the user.
function GetLoginSubmenu(){
    // if the user is logged in
    if(document.getElementById('submenu_login_loggedin'))
        return document.getElementById('submenu_login_loggedin');
    // if the user is not logged in
    else
        return document.getElementById('submenu_login');
}
// Show / hide
function hideAll(){
	GetYPSubmenu().style.display = "none";
	document.getElementById('submenu_support').style.display = "none";
	GetLoginSubmenu().style.display = "none";
	document.getElementById('submenu_departments').style.display = "none";
}
function showAll(){
	GetYPSubmenu().style.display = "block";
	document.getElementById('submenu_support').style.display = "block";
	GetLoginSubmenu().style.display = "block";
	document.getElementById('submenu_departments').style.display = "block";
}
// this function will change the css class of the login tab to make is look selected
function selectLoginTab()
{
    // selecting the login tab when user is logged in 
    if(document.getElementById("login").className == "logOutTab"){document.getElementById("login").className = "logOutTabSelected" }
    // selecting the login tab when user is logged out
    else if(document.getElementById("login").className == "logInTab"){document.getElementById("login").className = "logInTabSelected" }
}
// this function will change the css class of the login tab to make is look not selected
function deSelectLoginTab()
{
    // De-selecting the login tab when user is logged in 
    if(document.getElementById("login").className == "logOutTabSelected"){document.getElementById("login").className = "logOutTab" }
    // De-selecting the login tab when user is logged in 
    else if(document.getElementById("login").className == "logInTabSelected"){document.getElementById("login").className = "logInTab" }
}
// Show section
function showDepartments(toggleTabActiveView){
	document.getElementById('submenu_departments').style.display = 'block';
	GetYPSubmenu().style.display = 'none';
	GetLoginSubmenu().style.display = "none";
	document.getElementById('submenu_support').style.display = 'none';

	if(document.getElementById('departments').className == "active" && toggleTabActiveView){
		document.getElementById('departments').className = "";
	}else{
		document.getElementById('departments').className = "active";
	}
	document.getElementById('support').className = "";
	document.getElementById('your-pages').className = "";
	deSelectLoginTab();
	return false;
}
function showYP(toggleTabActiveView){
	document.getElementById('submenu_departments').style.display = 'none';
	document.getElementById('submenu_support').style.display = 'none';
	GetLoginSubmenu().style.display = "none";
	GetYPSubmenu().style.display = 'block';

	document.getElementById('departments').className = "";
	document.getElementById('support').className = "";
	deSelectLoginTab();
	if(document.getElementById('your-pages').className == "active" && toggleTabActiveView){
		document.getElementById('your-pages').className = "";
	}else{
		document.getElementById('your-pages').className = "active";
	}
	return false;
}
// function is called when user clicks on login tab. the login tab is displayed while all the other tabs are hidden
function showLogin(toggleTabActiveView){
	// hidding the departments tab
	document.getElementById('submenu_departments').style.display = 'none';
	// hidding the support tab
	document.getElementById('submenu_support').style.display = 'none';
	// hidding the your-pages tab
	GetYPSubmenu().style.display = 'none';
	// displaying the login tab
	GetLoginSubmenu().style.display = "block";
    
    // removing the active class from the department tab
	document.getElementById('departments').className = "";
	// removing the active class from the Support tab
	document.getElementById('support').className = "";
	// removing the active class from the your-pages tab
	document.getElementById('your-pages').className = "";
	
	// selecting the login tab button if it is not selected already. other Deselcting it
	if((document.getElementById('login').className == "logOutTabSelected" || document.getElementById('login').className == "logInTabSelected" )&& toggleTabActiveView){
		deSelectLoginTab();
	}else{
		selectLoginTab();
	}
	return false;
}
function showSupport(toggleTabActiveView){
	document.getElementById('submenu_departments').style.display = 'none';
	document.getElementById('submenu_support').style.display = 'block';
	GetYPSubmenu().style.display = 'none';
	GetLoginSubmenu().style.display = "none";

	document.getElementById('departments').className = "";
	document.getElementById('your-pages').className = "";
	deSelectLoginTab();
	if(document.getElementById('support').className == "active" && toggleTabActiveView){
		document.getElementById('support').className = "";
	}else{
		document.getElementById('support').className = "active";
	}
	return false;
}
// Change tab
function changeTab(){
    toggleTabActiveView = true; //Toggle tab active view only when you are not expanding the whole menybar and the tab is already active
	if(this.id == "departments"){
		if(document.getElementById('departments').className == "active"){
			showDepartments(toggleTabActiveView);
		}
		else{
			showDepartments(toggleTabActiveView);
		}
	}
	else if(this.id == "your-pages"){
		if(document.getElementById('your-pages').className == "active"){
			showYP(toggleTabActiveView);
		}
		else{
			showYP(toggleTabActiveView);
		}
	}
	else if(this.id == "login"){
		if(document.getElementById('login').className == "logOutTabSelected" || document.getElementById('login').className == "logInTabSelected" ){
			showLogin(toggleTabActiveView);
		}
		else{
			showLogin(toggleTabActiveView);
		}
	}
	else if(this.id == "support"){
		if(document.getElementById('support').className == "active"){
			showSupport(toggleTabActiveView);
		}
		else{
			showSupport(toggleTabActiveView);
		}
	}
}

// Mouseover
addOnLoad('myMouseOver()');

function myMouseOver(){
	var allTrs = document.getElementsByTagName('tr');
	for(var i = 0; i < allTrs.length; i++){
		if(allTrs[i].className == "jsMouseOver"){
			allTrs[i].onmouseover = overColor;
			allTrs[i].onmouseout = outColor;			
		}
	}
	function overColor(){
	 	this.style.background = "#ccc";
	}
	function outColor(){
	 	this.style.background = "none";
	}
}
// Adjust the height of elements with class 'content_no_menu'
// + The regular content div

addOnLoad('cnmAdjust()');

var cnmAdjust = function() {
	var cnmDiv = null, cDiv = null, adspotsDiv = null;
	
	divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className == 'content_no_menu') { cnmDiv = divs[i]; }
		if (divs[i].className == 'content') { cDiv = divs[i]; }
		if (divs[i].className == 'ad_spots') { adspotsDiv = divs[i]; }
	}
	
	if ((cnmDiv && adspotsDiv) && cnmDiv.offsetHeight < adspotsDiv.offsetHeight) {
		cnmDiv.style.height = (adspotsDiv.offsetHeight-146)+'px'
	}
	
	if ((cDiv && adspotsDiv) && cDiv.offsetHeight < adspotsDiv.offsetHeight) {
		cDiv.style.height = (adspotsDiv.offsetHeight-150)+'px'
	}
}

// Slideshow for product pages (only works if popup.js is linked on the page in question)

addOnLoad('commonSlideshow.init()');

var commonSlideshow = {
	products: new Array(),
	
	init: function() {
		if (typeof(Popup) != 'undefined') {
			// Check for product boxes
			var divs = document.getElementsByTagName('div');
			for (var i = 0; i < divs.length; i++) {
				if (divs[i].className.match(/^product_box/)) {
					commonSlideshow.products.push(divs[i]);
				}
			}
			
			// Continue if we found any product boxes
			if (commonSlideshow.products.length > 0) {
				// Add an onclick event to all product images
				for (var i = 0; i < commonSlideshow.products.length; i++) {
					var image;
					if (image = commonSlideshow.products[i].getElementsByTagName('img').item(0)) {
						image.productNum = i;
						image.onclick = commonSlideshow.show;
					}
				}
			}
		}
	},
	
	show: function() {
		// Copy the contents of the corresponding product box into the popup
		if (Popup.readyToRun(true)) {
			nodes = this.parentNode.childNodes;
			content = Popup.popupContentObject;
			
			// First, remove anything already in the popup
			content.innerHTML = '';
			
			// Populate the popup
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].nodeName != '#text') {
					content.appendChild(nodes[i].cloneNode(true))
				}
			}
			
			// Edit the path of the image, and remove its onclick property
			if (image = content.getElementsByTagName('img').item(0)) {
				image.removeAttribute('productNum');
				image.removeAttribute('onclick');
				image.src = image.src.replace('-medium.', '-large.')
			}
			
			// Show the popup
			Popup.show(true);
		}
	}
}

var loginForm = {
    successfulLoginDelay: 3000,
    logoutCountDownSeconds: 60,
    
    SuccessfulLogin: function(){
        showYP(false);
    },
	ShowLogoutWarning: function(){
	    showLogin(false);
	    
	    if(document.getElementById('autoLogoutText') && document.getElementById(ContinueLoggedInButtonId)){
	        document.getElementById('autoLogoutText').style.display = 'block';
	        document.getElementById(ContinueLoggedInButtonId).style.display = 'block';
	        window.focus();
        }
	    if(document.getElementById('logoutInfoLoggedOn2')) document.getElementById('logoutInfoLoggedOn2').style.display = 'none';
	    if(document.getElementById('logoutInfoUserName2')) document.getElementById('logoutInfoUserName2').style.display = 'none';
	    //if(document.getElementById('logoutButton')) document.getElementById('logoutButton').style.display = 'none';

	    loginForm.StartLogoutCountDown();
	},
	StartLogoutCountDown: function(){
	    if(document.getElementById('autoLogoutSeconds'))
	        document.getElementById('autoLogoutSeconds').innerHTML = loginForm.logoutCountDownSeconds--;
        var t = setTimeout("loginForm.StartLogoutCountDown()",1000); 
        if(loginForm.logoutCountDownSeconds<0)
        {
            clearTimeout(t);
            window.location.href = '/default.aspx?logout=1&auto=1';
        }
	},
	ShowLoginForm: function(){
        if(document.getElementById('autoLoggedOutInfo_LoginTab'))
            document.getElementById('autoLoggedOutInfo_LoginTab').style.display = 'none';
        if(document.getElementById('notLoggedInFormA_LoginTab') && document.getElementById('notLoggedInFormB_LoginTab')){
            document.getElementById('notLoggedInFormA_LoginTab').style.display = 'block';
            document.getElementById('notLoggedInFormB_LoginTab').style.display = 'block';
        }
	}
}

var inactivityStartTime = new Date();
addOnLoad("autoLogout.StartCountDown();");

var autoLogout = {
    checkInterval: 500,
    logoutWarningDelay: 1740000,
    
    StartCountDown: function(){
        //Variable IsLoggedIn is Registered in TopMenu
        if (typeof IsLoggedIn != 'undefined' && IsLoggedIn) {
            window.setTimeout("autoLogout.CheckInactivityWarning();", autoLogout.checkInterval);
        }
    },
	CheckInactivityWarning: function(){
	    var now = new Date();
	    if((now.getTime() - inactivityStartTime.getTime()) > autoLogout.logoutWarningDelay)
	    {
	        if (!kml)
	        {
	            //Show warning info
	            loginForm.ShowLogoutWarning();
	        } else
	        {
	            ContinueSession();
	            inactivityStartTime = new Date();
	            window.setTimeout("autoLogout.CheckInactivityWarning();", autoLogout.checkInterval);
	        }
	    }
	    else
	    {
            window.setTimeout("autoLogout.CheckInactivityWarning();", autoLogout.checkInterval);
        }
	}
}

function ContinueSession()
{
    var url = '/default.aspx';
    var xmlHttp = getXmlHttpObject();
    if (!xmlHttp)
        return;

    xmlHttp.open('POST', url, true);
    xmlHttp.send('');
}

function toggleElementDisplay(elementId)
{
    var element = document.getElementById(elementId);
    if(element != null)
    {
        if(element.style.display == 'none')
        {
            element.style.display = '';
        }else{
            element.style.display = 'none';
        }
    }
}

function validatelimit(obj,length)
     {    
        if(this.id) obj = this;       
            if(obj.value.length > 300)
            {
               obj.value = obj.value.substring(length,0); 
               return false;
            }
            else
            {
              return true;
            }
      }   


function ToggleCheckBoxes(current, idList)
{
    if (idList && current)
    {
        if (current.checked)
        {
            for (id in idList)
            {
                var elem = $get(idList[id]);
                elem.checked = false;
            }
        }
    }
}

function ToggelCheckBox(current, other)
{
    if (other)
    {
        var checkBoxArray = new Array();
        checkBoxArray[0] = other;
        ToggleCheckBoxes(current, checkBoxArray);
    }
}

function printContent(contentId, width, height) {
    var popup = window.open('', 'Print', 'width=' + width + ',height=' + height + ',top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=no');
    popup.document.body.innerHTML = $('#' + contentId).html();
    popup.document.close(); popup.print(); popup.close();
}

function popupContent(url, width, height) {
    var popup = window.open('', 'popup', 'width=' + width + ',height=' + height + ',top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=yes,location=no');
    var result;
    $.ajax({
        url: url,
        type: "GET",
        processData: false,
        dataType: "html",
        timeout: 10000,
        success: function(response) {
            result = response;
        },
        error: function() {
            alert('error!');
            return;
        },
        async: false
    });
    popup.document.write('<html><head><title>Jotex</title></head><body>' + result + '</body></html>');
}

function registerForNewsletter(email) {

    var result = false;
    var url = "/Services/Customer.svc/RegisterForNewsLetter/" + email + "/json";
    $.ajax({
        url: url,
        type: "GET",
        processData: false,
        timeout: 10000,
        success: function(response) {
            result = (response == 'true');
        },
        error: function(request, status, errorThrown) {
            result = false;
        },
        async: false
    });
    return result;
}

function ResetScrollPosition() {

    var scrollX = document.getElementById('__SCROLLPOSITIONX');
    var scrollY = document.getElementById('__SCROLLPOSITIONY');

    if (scrollX && scrollY) {
        scrollX.value = 0;
        scrollY.value = 0;
    }
}