/* 
WMS Lucrasoft Javascript Standaard functies
*/

function makeHttpRequest(url){ 
    if (window.XMLHttpRequest) { // Mozilla, Safari,... 
        http_request = new XMLHttpRequest(); 
        if (http_request.overrideMimeType) { 
            http_request.overrideMimeType('text/xml'); 
    } 
        } else if (window.ActiveXObject) { // IE 
            try { 
                http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
            } catch (e) { 
                try { 
                    http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
                } catch (e) {} 
            } 
        } 
    if (!http_request) { 
        alert('Helaas, jouw browser is te oud om deze pagina te kunnen laden.'); 
        return false; 
    } 
        http_request.open('GET', url, true); 
        http_request.send(null); 
}

function getField(fldName, strType) {
  var arrCheck = new Array;
  var blnFound = false;
  arrObjects = document.getElementsByTagName(strType.toUpperCase());

  for (i=0; i<arrObjects.length; i++) {
    if (arrObjects[i].id.split("_")[arrObjects[i].id.split("_").length - 1] == fldName) {
      blnFound = true;
      break;
    }
  }
  return arrObjects[i];
}

function PopUp(strPath) {
    window.open(strPath, 'WMSPOPUP', 'status=yes,resizable=yes,width=1024,height=764');
}

function MailTo(value) {
   if (value) {
		 var strTemp='';
		 for (i = value.length-1; i>=0; i--){
			 strTemp+=value.charAt(i);
		 }
		 strTemp = strTemp.split('|').join('@');
		 document.write('<a href=mailto:' + strTemp + '>' + strTemp + '</a>');
	 }
}

function Fade(p_objThis, p_intOp) {
    p_objThis.style.backgroundImage = 'url(./Images/bg/groupitem' + p_intOp + '.gif)'

}

function ChangeImage(p_strImage, p_pnlImageClientID, p_imgImageClientID) {
    document.getElementById(p_pnlImageClientID).style.backgroundImage = 'url(' + p_strImage + ')'
    document.getElementById(p_imgImageClientID).setAttribute('src', p_strImage)
}

function ChangeCSS(p_this, p_strClass) {
    p_this.className = p_strClass;
}

var lstImages = new Array;
var intSlideCounter = 0;
var intCurrentImage = 0;
var divImagesContainer = document.getElementById('divImagesContainer');
var intSlideSpace = 2;
var intPos = 0;
var intMiddle = 0;

function AddImage(p_strImage, p_strLink, p_strTitle) {
    lstImages[intSlideCounter] = new Array(3);
    lstImages[intSlideCounter][0] = p_strImage;
    lstImages[intSlideCounter][1] = p_strLink;
    lstImages[intSlideCounter][2] = p_strTitle;
    intSlideCounter = intSlideCounter + 1;
}

function SetSlide() {
    if (lstImages.length > 0) {
        document.getElementById('divSlide').style.display = 'block';
        divImagesContainer = document.getElementById('divImagesContainer')
        intMiddle = 0;
        var lstImg = new Array(lstImages.length);
        var intTotalCounter = 0;
        var blnLoop = true;
        while (blnLoop) {
            var i = 0;
            while (i < lstImages.length) {
                var divImageContainer = document.createElement('div');
                divImageContainer.className = 'divSlideImage';
                divImageContainer.style.backgroundImage = 'url(' + lstImages[i][0] + ')';
                divImageContainer.title = lstImages[i][2];
                intTotalCounter = intTotalCounter + 1;
                if (lstImages[i][1] == '') {
                } else {
                    divImageContainer.setAttribute('link', lstImages[i][1])
                    divImageContainer.onclick = function () { window.location = this.getAttribute('link'); }
                    divImageContainer.className = 'divSlideImageLinked';
                }
                divImagesContainer.appendChild(divImageContainer);
                i++;
            }
            if (intTotalCounter > 10 && intMiddle > 0) {
                blnLoop = false;
            }
            if (intMiddle == 0) {
                intMiddle = intTotalCounter * 92;
            }
        }
        if (lstImages.length > 0) {
            divImagesContainer.style.width = intTotalCounter * 92 + 'px';
            StartSlide();
        }
    }
}

function StartSlide() {
    divImagesContainer.style.marginLeft = intPos * -1 + 'px';
    intPos = intPos + intSlideSpace;
    if (intPos > intMiddle) {
        intPos = intPos - intMiddle;
    }
    setTimeout('StartSlide()', 40)
}

function ChangeSlideSpeed(p_intSpeed) {
    intSlideSpace = p_intSpeed;
}

var intTimer = 50;
var intOrigImageWidth = 0;
var intOrigImageHeight = 0;
var intImageToWidth = 20;
var intImageToHeight = 20;
var objImage = null;
var imgImageX = null;
var imgImageY = null;
var divShopX = null;
var divShopY = null;
var intStep = null;
var intSteps = 15;
var btnDoTheClick = null;

function AddToCartSingle(p_strArtCode) {
    document.getElementById('tbSingleProduct').value = p_strArtCode;
    document.getElementById('btnSingleOrder').click();
}

function AddToCart(p_imgImage, p_strArtCode) {
    intStep = 1;
    btnDoTheClick = document.getElementById('btnSingleOrder'); //  p_btnDoTheClick;
    document.getElementById('tbSingleProduct').value = p_strArtCode
    if (intOrigImageWidth == 0) {
        intOrigImageWidth = p_imgImage.width;
        intOrigImageHeight = p_imgImage.height;
        intImageToHeight = Math.abs(Math.round(p_imgImage.height / (p_imgImage.width / intImageToWidth)));
    }
    if (p_imgImage.height == 0) {
        intImageToHeight = 20;
    }
    var p_divShop = document.getElementById('divShopcartV2');
    imgImageX = p_imgImage.offsetLeft;
    imgImageY = p_imgImage.offsetTop;
    divShopX = p_divShop.offsetLeft;
    divShopY = p_divShop.offsetTop;
    if (imgImageX == 0 || imgImageY == 0) {
        var imgImage = p_imgImage;
        while (imgImage != null) {
            imgImageX += imgImage["offsetLeft"];
            imgImage = imgImage.offsetParent;
        }
        imgImage = p_imgImage;
        while (imgImage != null) {
            imgImageY += imgImage["offsetTop"];
            imgImage = imgImage.offsetParent;
        }
    }
    if (imgImageX == 0 || imgImageY == 0) {
        var imgImage = p_imgImage;
        while (imgImage != null) {
            imgImageX += imgImage["offsetLeft"];
            imgImage = imgImage.offsetParent;
        }
        imgImage = p_imgImage;
        while (imgImage != null) {
            imgImageY += imgImage["offsetTop"];
            imgImage = imgImage.offsetParent;
        }
    }

    if (divShopY == 0 || divShopX == 0) {
        var divShop = p_divShop;
        while (divShop != null) {
            divShopX += divShop["offsetLeft"];
            divShop = divShop.offsetParent;
        }
        divShop = p_divShop;
        while (divShop != null) {
            divShopY += divShop["offsetTop"];
            divShop = divShop.offsetParent;
        }
    }

    if (imgImageX == 0 || imgImageY == 0) {
        imgImageY = Math.abs(p_imgImage.getAttribute('Posy'));
        if (typeof window.innerWidth != 'undefined') {
            imgImageX = window.innerWidth;
        } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
            imgImageX = document.documentElement.clientWidth;
        } else {
            imgImageX = document.getElementsByTagName('body')[0].clientWidth;
        }

        imgImageX = ((Math.abs(imgImageX) - 956) / 2) + 250;

    }

    divShopX += 30;
    objImage = document.getElementById('imgToShopAnimation');

    objImage.src = p_imgImage.src;
    objImage.style.left = imgImageX + 'px';
    objImage.style.top = imgImageY + 'px';
    objImage.style.display = 'block';
    ChangePosition();
}

function ChangePosition() {
    if (intOrigImageWidth == 0) {
        intOrigImageWidth = 40;
        intOrigImageHeight = 40;
    }
    objImage.style.left = Math.round(imgImageX + (((divShopX - imgImageX) / intSteps) * intStep)) + 'px';
    objImage.style.top = Math.round(imgImageY + (((divShopY - imgImageY) / intSteps) * intStep)) + 'px';
    objImage.style.width = intOrigImageWidth - Math.round(((intOrigImageWidth - intImageToWidth) / intSteps) * intStep) + 'px';
    objImage.style.height = intOrigImageHeight - Math.round(((intOrigImageHeight - intImageToHeight) / intSteps) * intStep) + 'px';

    if (intStep < intSteps) {
        intStep = intStep + 1;
        setTimeout('ChangePosition()', intTimer);
    } else {
        objImage.style.display = 'none';
        btnDoTheClick.click();
    }
}

function AddToCartNoImage(p_strArtCode) {
    btnDoTheClick = document.getElementById('btnSingleOrder');
    document.getElementById('tbSingleProduct').value = p_strArtCode;
    btnDoTheClick.click();
}
