﻿var intOldRandomId = 9999;
var strMainPrintOrder = "";
var strMainUrlHeaderObjects = "";
var arrArticleKey = new Array();
var arrArticleVariation = new Array();
var arrArticleColors = new Array();
var arrArticleImages = new Array();
var arrArticleImagesId = new Array();
var arrArticleStockQuantity = new Array();
var arrArticleAddonNr = new Array();
var arrSelectTags = new Array();
var strLastCheckedImageSelector = "";
var strIdLayerCartAdd = "";
var strInfoMessageId = "";
var bolOverCart = false;
var bolOverCartAdd = false;

var intSpeedChangeImage = 20000;


var arrShipAddressElements = new Array();
var arrBillAdresssElements = new Array();

function showLoginForm()
{

    return false;
}

function change_mastercat_image(img, str) {
    var bild = document.getElementById("master_categorie_image_" + img);
    bild.src = str;
    return true;    
}

var intImageCount = 0;
var arrImages = new Array();
var arrImagesStartPage = new Array();
var arrImagesWebShop = new Array();

arrImagesStartPage.push("randomImage1.jpg");
arrImagesStartPage.push("randomImage2.jpg");

arrImagesWebShop.push("webshop/randomImage1.jpg");
arrImagesWebShop.push("webshop/randomImage2.jpg");


function initImage(pstrMode)
{
    if(pstrMode == "startPage")
        arrImages = arrImagesStartPage;
    else
        arrImages = arrImagesWebShop;

    imageId = 'headerFoto';
    image = document.getElementById(imageId);
    
    try
    {
        intOldRandomId = image.src.substring(image.src.indexOf(".jpg")- 1, image.src.indexOf(".jpg"));
        intOldRandomId--;
    }
    catch (err)
    {
    
    }
    
    setOpacity(image, 100);
    image.style.visibility = 'visible';
    window.setTimeout("fadeOut('"+imageId+"',100, 65, 5, true,'')", intSpeedChangeImage);   
}

function nextImage()
{
    // Zufallszahl bestimmen
    var intRandomId = Math.round(Math.random() * (arrImages.length - 1));
    
    imageId = 'headerFoto';
    image = document.getElementById(imageId);
    
    while(intOldRandomId == intRandomId)
        intRandomId = Math.round(Math.random() * (arrImages.length - 1));
        
    // Alte ID zwischenspeicher
    intOldRandomId = intRandomId;
        
    // Bild ändern
    image.src = strMainUrlHeaderObjects + arrImages[intRandomId];
    setOpacity(image, 0);
    fadeIn(imageId,0,65,5,true,'');
}

function setOpacity(obj, opacity)
{
    opacity = (opacity == 100)?99.999:opacity;

    // IE/Win
    obj.style.filter = "alpha(opacity:"+opacity+")";

    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity/100;

    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity/100;

    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity,intSpeed,intOpacityCount,bolNextImage)
{
    if(bolNextImage == undefined)
        bolNextImage = false;

    if (document.getElementById)
    {
        obj = document.getElementById(objId);
        if (opacity <= 100)
        {
            setOpacity(obj, opacity);
            opacity += intOpacityCount;
            window.setTimeout("fadeIn('"+objId+"',"+opacity+"," + intSpeed + "," + intOpacityCount + "," + bolNextImage.toString() + ", '')", intSpeed);
        }
        else if(bolNextImage == true)
        {
            window.setTimeout("fadeOut('"+objId+"',"+opacity+"," + intSpeed + "," + intOpacityCount + "," + bolNextImage.toString() + ", '')", intSpeedChangeImage);
        }
    }
}

function fadeOut(objId,opacity,intSpeed,intOpacityCount,bolNextImage,strNewImage)
{
    if(bolNextImage == undefined)
        bolNextImage = false;

    if (document.getElementById(objId))
    {
        obj = document.getElementById(objId);
        if (opacity >= 10 && strNewImage != "") 
        {
            setOpacity(obj, opacity);
            opacity = opacity - intOpacityCount;
            window.setTimeout("fadeOut('"+objId+"',"+opacity+"," + intSpeed + "," + intOpacityCount + "," + bolNextImage.toString() + ", '" + strNewImage + "')", intSpeed);
        }
        else if(strNewImage != undefined && strNewImage != "")
        {
            obj.src = strNewImage;
            window.setTimeout("fadeIn('"+objId+"',"+opacity+"," + intSpeed + "," + intOpacityCount + "," + bolNextImage.toString() + ", '')", intSpeed);
            //fadeIn(objId, opacity, intSpeed, intOpacityCount, bolNextImage);
        }
        else if(bolNextImage == true)
        {   
            if (opacity >= 10) 
            {
                setOpacity(obj, opacity);
                opacity = opacity - intOpacityCount;
                window.setTimeout("fadeOut('"+objId+"',"+opacity+"," + intSpeed + "," + intOpacityCount + "," + bolNextImage.toString() + ", '" + strNewImage + "')", intSpeed);
            }
            else
            {
                nextImage();
            }
        }
    }
}

function changeArticleImageNow(pstrImageName)
{
    document.getElementById("imgArticle").src = pstrImageName;
}

function preloadImages()
{
    var objImage = new Image();
    var intKey = 0;
    
    while(intKey < arrArticleImages.length)
    {
        objImage = new Image();
        objImage.src = arrArticleImages[intKey];
        intKey++;
    }
}

function showFullScreenImage()
{
    var strOldUrl = document.getElementById("imgArticle").src.toString();
    var strNewUrl = strOldUrl.substring(0, strOldUrl.lastIndexOf("/"));
    var strImage = strOldUrl.substring(strOldUrl.lastIndexOf("/"));
    var strNewImage = strImage.substring(0, strImage.lastIndexOf("_")) + "_3" + strImage.substring(strImage.lastIndexOf("."));
    
    strNewImage = strNewUrl + strNewImage;
    
    document.getElementById("layFullScreenImage").style.display = "inline";
    document.getElementById("cboQuantity").style.visibility = "hidden";
    document.getElementById("imgFullSize").src = strNewImage;
    
    if(document.getElementById("cboSizeVariation"))
        document.getElementById("cboSizeVariation").style.visibility = "hidden";
        
        
    showOverlay();
        
    return false;
}

function hideFullScreenImage()
{
    document.getElementById("layFullScreenImage").style.display = "none";
    document.getElementById("cboQuantity").style.visibility = "visible";
    
    if(document.getElementById("cboSizeVariation"))
        document.getElementById("cboSizeVariation").style.visibility = "visible";
        
    hideOverlay();
}

function closeCart()
{
    if(bolOverCart == false)
        document.getElementById('plnCartItems').style.display = 'none';
}

function closeCartAdd(pobjObject)
{
    if(bolOverCartAdd == false && document.getElementById(strIdLayerCartAdd) != undefined)
        document.getElementById(strIdLayerCartAdd).style.display = 'none';
}

function enableShipAddress()
{
    var intCount = 1;
    
    while(intCount < arrShipAddressElements.length)
    {
        document.getElementById(arrShipAddressElements[intCount]).disabled = false;
        intCount++;
    }
}

function disableShipAddress()
{
    var intCount = 1;
    
    while(intCount < arrShipAddressElements.length)
    {
        document.getElementById(arrShipAddressElements[intCount]).disabled = true;
        intCount++;
    }
}

function shipAddressHandler()
{        
    if(document.getElementById(arrShipAddressElements[0]).checked == false)
        enableShipAddress();
    else
        disableShipAddress();
}

function initSettingsHandler()
{
    shipAddressHandler();
}


function showOverlay()
{
    if(document.getElementById("overlay") != null)
        document.getElementById("overlay").style.display = "block";
        
    fadeOutSelectTags();
}

function hideOverlay()
{
    if(document.getElementById("overlay") != null)
        document.getElementById("overlay").style.display = "none";
        
    fadeInSelectTags();
}

function printFullScreenImage(pstrUrl)
{
    var objWindow = null;
    
    // Öffnen des Fenster nur bei Artikel ID
    if(!document.getElementById("txtArticleVariation") && !document.getElementById("cboSizeVariation"))
        objWindow = window.open(pstrUrl + "?articleId=" + document.getElementById("txtArticleId").value + "&image=" + document.getElementById("imgFullSize").src, "ImagePrintOut", "width=755,height=625,left=100,top=100");
    
    // Öffnen des Fenster nur bei Farbenauswahl
    if(document.getElementById("txtArticleVariation") && !document.getElementById("cboSizeVariation"))
        objWindow = window.open(pstrUrl + "?articleId=" + document.getElementById("txtArticleId").value + "&articleVariationId=" + document.getElementById("txtArticleVariation").value + "&image=" + document.getElementById("imgFullSize").src, "ImagePrintOut", "width=755,height=625,left=100,top=100");
    
    // Öffnen des Fenster nur bei Farbenauswahl und Grössenauswahl
    if(document.getElementById("txtArticleVariation") && document.getElementById("cboSizeVariation"))
        objWindow = window.open(pstrUrl + "?articleId=" + document.getElementById("txtArticleId").value + "&articleVariationId=" + document.getElementById("txtArticleVariation").value + "&sizeVariationId=" + document.getElementById("cboSizeVariation").value + "&image=" + document.getElementById("imgFullSize").src, "ImagePrintOut", "width=755,height=625,left=100,top=100");
        
    // Öffnen des Fenster nur bei Farbenauswahl und Grössenauswahl
    if(!document.getElementById("txtArticleVariation") && document.getElementById("cboSizeVariation"))
        objWindow = window.open(pstrUrl + "?articleId=" + document.getElementById("txtArticleId").value + "&sizeVariationId=" + document.getElementById("txtArticleVariation").value + "&image=" + document.getElementById("imgFullSize").src, "ImagePrintOut", "width=755,height=625,left=100,top=100");
        
    objWindow.focus();
}

function changeArticleImage(pstrNewHexValue, pstrColorName, pstrId)
{
    var intKey = 0;
    var strUrl = document.getElementById("imgArticle").src.toString();
    var bolFoundImage = false;
    
    if(document.getElementById("lnkImage_" + pstrId))
    {
        if(document.getElementById(strLastCheckedImageSelector))
            document.getElementById(strLastCheckedImageSelector).style.backgroundColor = "#999999";
            
        document.getElementById("lnkImage_" + pstrId).style.backgroundColor = "#00ADEF";
    }

    if(document.getElementById("txtSelectedColor"))    
        document.getElementById("txtSelectedColor").value = pstrNewHexValue;
    
    if(document.getElementById("cboSizeVariation"))
        changeSizeAndColor();

    strLastCheckedImageSelector = "lnkImage_" + pstrId;
    
    while(intKey < arrArticleColors.length)
    {
        if((arrArticleColors[intKey] == pstrNewHexValue || pstrNewHexValue == "") && (strUrl != arrArticleImages[intKey] && arrArticleImagesId[intKey] == pstrId.toString()) && arrArticleImages[intKey] != "")
        {
            fadeOut("imgArticle", 100, 15, 10, false, arrArticleImages[intKey]);
            
            if(document.getElementById('layColorChoice'))
                document.getElementById('layColorChoice').innerHTML = pstrColorName;
            
            if(!document.getElementById("cboSizeVariation"))
            {
                document.getElementById("txtArticleVariation").value = arrArticleVariation[intKey];
                document.getElementById("layArticleAddonNr").innerHTML = arrArticleAddonNr[intKey];
            }
            return false; 
        }  
        
        // Ist ein grössen auswahl dabei?
        if(document.getElementById("cboSizeVariation"))
        {
            if(arrArticleVariation[intKey] == document.getElementById("cboSizeVariation").value && arrArticleColors[intKey] == pstrNewHexValue)
            {
                fadeOut("imgArticle", 100, 15, 10, false, arrArticleImages[intKey]);
                
                if(document.getElementById('layColorChoice'))
                    document.getElementById('layColorChoice').innerHTML = pstrColorName;
                
                if(!document.getElementById("cboSizeVariation"))
                {
                    document.getElementById("txtArticleVariation").value = arrArticleVariation[intKey];
                    document.getElementById("layArticleAddonNr").innerHTML = arrArticleAddonNr[intKey];
                }
                return false; 
            }    
        }  
        
        if(arrArticleColors[intKey] == pstrNewHexValue)
        {
            if(document.getElementById('layColorChoice'))
                document.getElementById('layColorChoice').innerHTML = pstrColorName;
            
            if(!document.getElementById("cboSizeVariation"))
            {
                document.getElementById("txtArticleVariation").value = arrArticleVariation[intKey];
                document.getElementById("layArticleAddonNr").innerHTML = arrArticleAddonNr[intKey];
            }
            bolFoundImage = true;   
        }
            
        intKey++;   
    }
    
    // Kein passendes Bild gefunden
    if(bolFoundImage == false && document.getElementById('layColorChoice'))
        document.getElementById('layColorChoice').innerHTML = pstrColorName + " (Leider kein Bild vorhanden)";
        
    return false;
}

function changeSize()
{
    var intKey = 0;
    
    if(document.getElementById('layColorChoice'))
    {
        changeSizeAndColor();
    }
    else
    {
        while(intKey < arrArticleAddonNr.length)
        {
            if(arrArticleVariation[intKey] == document.getElementById("cboSizeVariation").value)
            {
                document.getElementById("txtArticleVariation").value = arrArticleVariation[intKey];
                document.getElementById("layArticleAddonNr").innerHTML = arrArticleAddonNr[intKey];
                return false; 
            }    
                
            intKey++;
        }
    }
    
    return false;
}

function changeSizeAndColor()
{
    var intKey = 0;
    var strUrl = document.getElementById("imgArticle").src.toString();
    var bolFoundImage = false;  
    var arrKey = new Array();
    
    while(intKey < arrArticleKey.length)
    {
        arrKey = arrArticleKey[intKey].split("|@|");
        
        if(arrKey[1] == document.getElementById("txtSelectedColor").value && arrKey[2] == document.getElementById("cboSizeVariation").options[document.getElementById("cboSizeVariation").selectedIndex].text)
        {
            document.getElementById("txtArticleVariation").value = arrKey[0];
            document.getElementById("layArticleAddonNr").innerHTML = arrArticleAddonNr[intKey];
        }
                
        intKey++;
    }
}

function printOrder()
{
    var objWindow = window.open(strMainPrintOrder, "OrderPrintOut", "width=755,height=570,left=100,top=100,scrollbar=yes");
    objWindow.focus();
}

function hideInfoMessage()
{
    hideOverlay();
    
    if(document.getElementById(strInfoMessageId))
        document.getElementById(strInfoMessageId).style.display = "none";

    return false;
}

function fadeOutSelectTags()
{
    var strElements = "";
    var strType = "";
    var strBrowserType = getBrowserType();

    if(strBrowserType == "IE6" || strBrowserType == "ie5" || strBrowserType == "ie5.5" || strBrowserType == "ie5m")
    {
        for(var intCount = 0; intCount < document.forms[0].length; intCount++)
        {
            if((document.forms[0].elements[intCount].type == "select-one" || document.forms[0].elements[intCount].type == "select-multiple") && document.forms[0].elements[intCount].style.display != "none")
            {
                arrSelectTags.push(document.forms[0].elements[intCount].id);
                document.forms[0].elements[intCount].style.visibility = "hidden";
            }
            
            strType += document.forms[0].elements[intCount].type + "\r\n";
        }
    }
}

function fadeInSelectTags()
{
    for(var intCount = 0; intCount < arrSelectTags.length; intCount++)
    {
        if(arrSelectTags[intCount] != "" && arrSelectTags[intCount] != undefined && document.getElementById(arrSelectTags[intCount]))
            document.getElementById(arrSelectTags[intCount]).style.visibility = "visible";
    }
}

function getBrowserType()
{
    var strBrowserType = "";
    if(document.ids) 
        strBrowserType = 'nc4';
    else if(document.all && !document.getElementById) 
        strBrowserType = 'ie4';
    else if(window.opera && !document.createElement) 
        strBrowserType = 'op5';
    else if(window.opera && window.getComputedStyle)
    {
        if(document.createRange) 
            strBrowserType = 'op8';
        else if(window.navigate) 
            strBrowserType = 'op7.5';
        else
            strBrowserType = 'op7.2';
    }
    else if(window.opera && document.compatMode)
        strBrowserType = 'op7';
    else if(window.opera && document.releaseEvents)
        strBrowserType = 'op6';
    else if(document.contains && !window.opera)
        strBrowserType = 'kq3';
    else if(window.pkcs11&&window.XML)
        strBrowserType = 'f15';
    else if(window.getSelection && window.atob)
        strBrowserType = 'nn7';
    else if(window.getSelection && !document.compatMode)
        strBrowserType = 'nn6';
    else if(window.clipboardData && document.compatMode)
        strBrowserType = window.XMLHttpRequest? 'IE7' : 'IE6';
    else if(window.clipboardData )
    {
        strBrowserType = 'ie5';
        if(!document.createDocumentFragment)
            strBrowserType += '.5';
        if(document.doctype && !window.print)
            strBrowserType += 'm';
    }
    else if(document.getElementById && !document.all)
        strBrowserType = 'op4';
    else if(document.images && !document.all)
        strBrowserType = 'nn3';
    else if(document.clientWidth && !window.RegExp)
        strBrowserType = 'kq2';
    else
        strBrowserType = '???';
        
    return strBrowserType;
}

function getPageWidth()
{
    arrData = getPageSize();
    return arrData[0];
}

function getPageHeight()
{
    arrData = getPageSize();
    return arrData[1];
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showCart()
{
    if(document.getElementById(strIdLayerCartAdd))
        document.getElementById(strIdLayerCartAdd).style.display = 'none';

    document.getElementById('plnCartItems').style.display = 'inline'
}
