﻿var zitemanAjaxSyncResult = '';
function zitemanAjaxSyncRequest(sParam)
{
    var zitemanHttpRequest;
    if (typeof(XMLHttpRequest)!='undefined')
    {
        zitemanHttpRequest = new XMLHttpRequest();
        if (zitemanHttpRequest.overrideMimeType)
        {
            zitemanHttpRequest.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            zitemanHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                zitemanHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!zitemanHttpRequest)
    {
        return false;
    }
    // Get solution path
    zitemanAjaxSyncResult = '';
    var sPath = '' + document.location.href;
    sPath = 'http://' + sPath.split('/')[2] + '/ajaxsend.asp';
    if (sParam != '')
    {
        sPath += '?' + sParam;
    }
    
    zitemanHttpRequest.open('GET', sPath, false);
     zitemanHttpRequest.onreadystatechange=function() {
      if (zitemanHttpRequest.readyState==4) {
       zitemanAjaxSyncResult = zitemanHttpRequest.responseText;
      }
     }
    zitemanHttpRequest.send(null);
    return zitemanAjaxSyncResult;
}

function zitemanGetNamedPage(sId)
{
    var sConvertedName = 'page.asp';
    if (bUseZitemanNamedPages)
    {
        sConvertedName = zitemanAjaxSyncRequest('function=translatepagename&id=' + sId);
        if (sConvertedName.indexOf('?') > 0)
        {
            sConvertedName = sConvertedName.split('?')[0];
        }
    }
    return sConvertedName;
}

function zitemanAjaxRequest(sResultID, sParam)
{
    var zitemanHttpRequest;
    if (typeof(XMLHttpRequest)!='undefined')
    {
        zitemanHttpRequest = new XMLHttpRequest();
        if (zitemanHttpRequest.overrideMimeType)
        {
            zitemanHttpRequest.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            zitemanHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                zitemanHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!zitemanHttpRequest)
    {
        return false;
    }
    // Get solution path
    var sPath = '' + document.location.href;
    sPath = 'http://' + sPath.split('/')[2] + '/ajaxsend.asp';
    if (sParam != '')
    {
        sPath += '?' + sParam;
    }
    zitemanHttpRequest.onreadystatechange = function() { zitemanHandleAjaxResponse(zitemanHttpRequest, sResultID); };
    zitemanHttpRequest.open('GET', sPath, true);
    zitemanHttpRequest.send(null);
    return;
}

function zitemanHandleAjaxResponse(zitemanHttpRequest, sResultID)
{
    if (zitemanHttpRequest.readyState == 4)
    {
        if (zitemanHttpRequest.status == 200)
        {
            // all is well
            var strResult = zitemanHttpRequest.responseText;
            var oResult = document.getElementById(sResultID);
            if (oResult)
            {
                oResult.setAttribute('soap', 'done');
                oResult.innerHTML = strResult;
            }
        } else {
            // Fejl i serverResponse
        }
    } else {
        // handle other status
    }
}

function zitemanFindProductLineVareID(sComboName)
{
	var aVariant = new Array();
	var aVariantOldSelect = new Array();
	var aCount = new Array();
	var oElements = document.getElementsByName(sComboName);
	var oElementsHidden = document.getElementsByName(sComboName + '_hidden');
	var sCheckValue = '';
	for (var x=0; x<oElements.length; x++)
	{
	    var sCheckValue = oElementsHidden[x].value;
	    var sBaseSelected = oElements[x].getAttribute('currentSelected');
		for (var i=0; i<oElements[x].length; i++)
		{
			if (oElements[x][i].selected == true)
			{
				var strId = oElements[x].id;
				strId = strId.split('_')[strId.split('_').length-1];
				var strValue = oElements[x][i].value;
				
				/* CHECK FOR VARENR - VARIANT */
				var aValue = strValue.split('##');
				if (typeof(aVariant[strId]) == 'undefined')
				{
					aCount[aCount.length] = strId;
					aVariant[strId] = aValue[1];
					aVariantOldSelect[strId] = sBaseSelected;
				}
			}
		}
	}
	
	/* FIND KORREKT VARE NUMMER */
	var aControlArray = new Array();
	var aCheck = sCheckValue.split('¤**¤');
	for (var x=0; x<aCheck.length; x++)
	{
	    var aTmp = aCheck[x].split(':**:');
	    aControlArray[x] = new Array();
	    aControlArray[x]['vareid'] = aTmp[1];
	    aControlArray[x]['varenr'] = aTmp[2];
	    aControlArray[x]['check'] = new Array();
	    var aTmp2 = aTmp[0].split('/**/');
	    for (var y=0; y<aTmp2.length; y++)
	    {
	        var aTmp3 = aTmp2[y].split('**');
		    if (aTmp3[0] != '')
		    {
		        aControlArray[x]['check'][aTmp3[0]] = aTmp3[1];
		    }
	    }
	}
	
	var strVariants = '';
	var sFound = '';
	var sFoundOld = '';
	var aFoundList = new Array();
	for (var x=0; x<aCount.length; x++)
	{
	    var sFound = '';
	    var bIsNew = (aVariant[aCount[x]] == aVariantOldSelect[aCount[x]])?false:true
        for (var run=0; run<aControlArray.length; run++)
        {
           // alert(aControlArray[run]['check'][aCount[x]] + ':' + aVariant[aCount[x]] + ':' + aCount[x] + ':' + run);
            if (aControlArray[run]['check'][aCount[x]] == aVariant[aCount[x]])
            {
                sFound += bIsNew + ',' + run + ',';
            }
        }
        /*alert('PRE:' + sFound + ':' + ((aVariant[aCount[x]] == aVariantOldSelect[aCount[x]])?'OLDselected':'NEW'));*/
        aFoundList[aFoundList.length] = sFound;
        if (sFound != sFoundOld && x > 0 && sFound != '')
        {
            var aFound = sFound.split(',');
            var aFoundOld = sFoundOld.split(',');
            var sTmpFound = sFound;
            sFound = '';
            for (var b=0; b<aFoundOld.length; b+=2)
            {
                var bInUse = false;
                for (var n=0; n<aFound.length; n+=2)
                {
                    if (aFoundOld[b+1] == aFound[n+1] && aFoundOld[b+1] != '' && typeof(aFoundOld[b+1]) != 'undefined')
                    {
                        var bIsNewCheck = (aFoundOld[b]=='true' || aFound[n] == true)?true:false;
                        sFound += bIsNewCheck + ',' + aFoundOld[b+1] + ',';
                        bInUse = true;
                    } 
                }
                if (aFoundOld[b] == 'true' && bInUse==false && aFoundOld[b+1] != '' && typeof(aFoundOld[b+1]) != 'undefined')
                {
                    sFound += aFoundOld[b] + ',' + aFoundOld[b+1] + ',';
                }
            }
            
            for (var n=0; n<aFound.length; n+=2)
            {
                if (aFound[n] == 'true' && aFound[n+1] != '' && typeof(aFound[n+1]) != 'undefined')
                {
                    if (sFound.indexOf(aFound[n+1]+',') != -1)
                    {
                        sFound += aFound[n] + ',' + aFound[n+1] + ',';
                    }
                }
            }
                
            if (sFound == '' && bIsNew)
            {
                sFound = sTmpFound;
            }
        }
        sFoundOld = sFound;
	}
	/* KAN DER FINDES ET VALID KRYSPRODUKT */
	var iValidProd = -1;
	for (var x=0; x<aFoundList.length; x++)
	{
	    if (aFoundList[x] != '' && iValidProd == -1)
	    {
	        var aTmp1 = aFoundList[x].split(',');
	        for (var y=0; y<aTmp1.length; y+=2)
	        {
	            if (iValidProd == -1)
	            {
	                var bValidId = true;
	                var sCheck = ''+aTmp1[y+1];
		            if (sCheck != 'undefined')
		            {
	                    for (var x1=0; x1<aFoundList.length; x1++)
	                    {
	                        if (x1 != x && aFoundList[x1] != '')
	                        {
	                            var aTmp2 = aFoundList[x1].split(',');
	                            var bFoundInSplit = false;
	                            for (var y1=0; y1<aTmp2.length; y1+=2)
	                            {
	                                if (aTmp2[y1+1] == sCheck)
	                                {
	                                    bFoundInSplit = true;
	                                }
	                            }
	                            if (!bFoundInSplit)
	                            {
	                                bValidId = false;
	                            }
	                        }
	                    }
	                    if (bValidId)
	                    {
	                        iValidProd = sCheck
	                    }
	                }
	            }
	        }
	    }
	}
	
	/*alert('done:' + aFoundList + ':' + iValidProd);*/
	var sProduct = '';
	if (iValidProd != -1)
	{
	    sProduct = aControlArray[iValidProd]['vareid'] +':**:'+ aControlArray[iValidProd]['varenr'];
	} else {
    	if (sFound.indexOf(',') != -1)
	    {
	        sProduct = aControlArray[sFound.split(',')[1]]['vareid'] +':**:'+ aControlArray[sFound.split(',')[1]]['varenr'];
	    }
	}
    return sProduct;
}

function zitemanMakeAjaxRequest(sResultID, sFlowID, sComboName)
{
	/* CHECK FOR PRODUCTLINE VARIANTER */
	var sProduct = zitemanFindProductLineVareID(sComboName);
	if (sProduct != '')
	{
	    var aProduct = sProduct.split(':**:');
        
        if (strExtraParamString.indexOf('showproduct') != -1)
        {
            /* PAGE RELOAD */
            // Get solution path
            var sPath = '' + document.location.href;
            sPath = 'http://' + sPath.split('/')[2] + '/page.asp?objectid=' + currentShowPageID + '&dataowner=webshopproduct&dataaction=showproduct&dataactionitem=' + aProduct[0];
            document.location.replace(sPath);
        } else {
            /* AJAX */
	        var sParam = 'function=getproduct&flow=' + sFlowID + '&id=' + aProduct[0];
            zitemanAjaxRequest(sResultID, sParam);
        }
    }
    return;
}

var oImageMapShow = null;
var bBuilding = false;
function zitemanImagemapRemove()
{
    if (oImageMapShow)
    {
        oImageMapShow.style.display = 'none';
        oImageMapShow = null;
    }
    return;
}

function zitemanImagemapShow(evt, sImageMap, sCount, sDesignTag)
{
    if (!bBuilding)
    {
        zitemanImagemapRemove();
        /* VIS DESIGN TAG */
        evt = (evt) ? evt : ((window.event) ? window.event : "");
        if (window.event) {
            var oElem = evt.srcElement;
            while ('' + oElem.className == '')
            {
                var oElem = oElem.parentElement;
            }
            var sClass = oElem.className;
        } else {
            var oElem = evt.target;
            while ('' + oElem.className == '')
            {
                var oElem = oElem.parentNode;
            }
            var sClass = oElem.className;
        }
        var oDivShow = document.getElementById('designtag_show_' + sImageMap + '_' + sCount);
        if (!oDivShow)
        {
            var oDivShow = document.createElement('DIV');
            oDivShow.setAttribute('id', 'designtag_show_' + sImageMap + '_' + sCount);
            oDivShow.style.display = 'none';
            oDivShow.style.position = 'absolute';
            oDivShow.style.top = evt.clientY + document.body.scrollTop;
            oDivShow.style.left = evt.clientX + document.body.scrollLeft;
            oDivShow.className = sClass;
            
            document.body.appendChild(oDivShow);
            
            zitemanImagemapBuild(sImageMap, sCount, sDesignTag)
        }
        
        if (oDivShow)
        {
            /* SHOW IT */
            document.body.appendChild(oDivShow);
            oDivShow.style.display = 'block';
            oDivShow.style.left = evt.clientX + document.body.scrollLeft;
            oDivShow.style.top = evt.clientY + document.body.scrollTop;
            oImageMapShow = oDivShow;
        }
    }
    return;
}

function zitemanImagemapBuild(sImageMap, sCount, sDesignTag)
{
    /* VIS DESIGN TAG */

    /* Hent DESIGN TAG GRUND FORM */
    bBuilding = true;
    var oDiv = document.getElementById('designtag_' + sDesignTag);

    if (!oDiv)
    {
        var oDiv = document.createElement('DIV');
        oDiv.setAttribute('id', 'designtag_' + sDesignTag);
        oDiv.style.display = 'none';
        
        document.body.appendChild(oDiv);
        var sParam = 'function=getdesigntag&id=' + sDesignTag + '&flow=';
        zitemanAjaxRequest('designtag_' + sDesignTag, sParam);
        
        setTimeout('zitemanImagemapBuild("' + sImageMap + '", "' + sCount + '", "' + sDesignTag + '")',10);
        return;
    }
    if (oDiv.getAttribute('soap') != 'done')
    {
        setTimeout('zitemanImagemapBuild("' + sImageMap + '", "' + sCount + '", "' + sDesignTag + '")',10);
        return;
    }
            
    /* udskift indhold */
    var oDivContent = document.getElementById('designtag_text_' + sDesignTag);
    if (!oDivContent)
    {
        var oDivContent = document.createElement('DIV');
        oDivContent.setAttribute('id', 'designtag_text_' + sDesignTag);
        oDivContent.style.display = 'none';
        document.body.appendChild(oDivContent);
    }

    if (oDivContent.getAttribute('startsoap') != 'true')
    {
        oDivContent.setAttribute('soap', '');
        oDivContent.setAttribute('startsoap', 'true');

        var sParam = 'function=getdesigntag_content&id=' + sImageMap + '&flow=' + sCount;
        zitemanAjaxRequest('designtag_text_' + sDesignTag, sParam);
        setTimeout('zitemanImagemapBuild("' + sImageMap + '", "' + sCount + '", "' + sDesignTag + '")',10);
        return;
    }
    
    if (oDivContent.getAttribute('soap') != 'done')
    {
        setTimeout('zitemanImagemapBuild("' + sImageMap + '", "' + sCount + '", "' + sDesignTag + '")',10);
        return;
    }
    /* vis design tag */
    oDivContent.setAttribute('startsoap', '');

    var oDivShow = document.getElementById('designtag_show_' + sImageMap + '_' + sCount);
    var sShowHTML = oDiv.innerHTML;
    var aContent = new Array();
    for (var x=0; x<oDivContent.childNodes.length; x++)
    {
        if (oDivContent.childNodes[x].nodeType != 3)
        {
            aContent[aContent.length] = x;
        }
    }
    sShowHTML = sShowHTML.replace('designtag_text', oDivContent.childNodes[aContent[2]].innerHTML);
    
    var sImg = '';

    if (oDivContent.childNodes[aContent[0]].innerHTML != '')
    {
        var sHeight = '-1';
        var sWidth = '-1';
        var sSize = '';
        /* BYG IMAGE */
        var oDivs = oDiv.getElementsByTagName('DIV');
        for (var x=0; x<oDivs.length; x++)
        {
            if (oDivs[x].innerHTML == 'designtag_img')
            {
                var sHeight = oDivs[x].getAttribute('imgheight');
                var sWidth = oDivs[x].getAttribute('imgwidth');
            }
        }
        if (sWidth != '-1' && sHeight != '-1')
        {
            sSize = ' width="' + sWidth + '" height="' + sHeight + '"';
        }
        var sAlt = oDivContent.childNodes[aContent[1]].innerHTML;
        sAlt = sAlt.replace('"', '');
        var sImg = '<img src="' + oDivContent.childNodes[aContent[0]].innerHTML + '" border="0" alt="' + sAlt + '" ' + sSize + '/>';
    }
    sShowHTML = sShowHTML.replace('designtag_img', sImg);
    oDivShow.innerHTML = sShowHTML
    oDivShow.style.display = 'block';
    oImageMapShow = oDivShow;
    document.body.appendChild(oDivShow);
    bBuilding = false;
    return;
}