var ns4=document.layers;
var ie4=document.all;
var ns6=document.getElementById&&!document.all;
var dragswitch=0;
var nsx;
var nsy;
var nstemp;
var objPopup = null;
var intWidth = 400;
var intHeight = 577;
var posLeft = 0;
var posRight = 0;
var strAgent = navigator.userAgent.toLowerCase();
var strGet;
var http = getHTTPObject();
var arrTemp = new Array;
var arrData = new Array;

function doShow(wat) {
  njaa = document.getElementById('div' + wat);
  njaa.className = 'carton';
}
function doHide(wat) {
  njaa = document.getElementById('div' + wat);
  njaa.className = 'cart';
}

function knopShow(wat) {
  var njaa = document.getElementById(wat);
  njaa.className = 'cartonKnop';
}

function knopHide(wat) {
  var njaa = document.getElementById(wat);
  njaa.className = 'cart';
}
function savePos(x) {
	var intPos;
	if (x) {
	  intPos = 0;
	} else {
    intPos = document.getElementById('content').scrollTop;
	}
  document.getElementById('fldPos').value = intPos;
}
function htmlEncode(source, display, tabs) {
	function special(source)	{
		var result = '';
		for (var i = 0; i < source.length; i++) {
			var c = source.charAt(i);
			if (c < ' ' || c > '~') {
				c = '&#' + c.charCodeAt() + ';';
			}
			result += c;
		}
		return result;
	}
	
	function format(source) {
		tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
		
		var lines = source.split(/\r\n|\r|\n/);
		
		for (var i = 0; i < lines.length; i++) {
			var line = lines[i];
			var newLine = '';
			for (var p = 0; p < line.length; p++) {
				var c = line.charAt(p);
				if (c === '\t') {
					var spaces = tabs - (newLine.length % tabs);
					for (var s = 0; s < spaces; s++) {
						newLine += ' ';
					}
				} else {
					newLine += c;
				}
			}

      newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
			lines[i] = newLine;
		}
		
		var result = lines.join('<br />');
		result = result.replace(/  /g, ' &nbsp;');
		return result;
	}

	var result = source;
	
	result = result.replace(/\&/g,'&amp;');
	result = result.replace(/\</g,'&lt;');
	result = result.replace(/\>/g,'&gt;');
	
	if (display) {
		result = format(result);
	}	else {
		result = result.replace(new RegExp('"','g'), '&quot;');
	}

	result = special(result);
	return result;
}
function drag_dropns(name){
	if (!ns4)
	return
	temp=eval(name)
	temp.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP)
	temp.onmousedown=gons
	temp.onmousemove=dragns
	temp.onmouseup=stopns
}

function gons(e){
	temp.captureEvents(Event.MOUSEMOVE)
	nsx=e.x
	nsy=e.y
}

function dragns(e){
	if (dragswitch==1){
		temp.moveBy(e.x-nsx,e.y-nsy)
		return false
	}
}

function stopns(){
	temp.releaseEvents(Event.MOUSEMOVE)
}

function drag_drop(e){
	if (ie4&&dragapproved){
		crossobj.style.left=tempx+event.clientX-offsetx
		crossobj.style.top=tempy+event.clientY-offsety
		return false
	}
	else if (ns6&&dragapproved){
		crossobj.style.left=tempx+e.clientX-offsetx+"px"
		crossobj.style.top=tempy+e.clientY-offsety+"px"
		return false
	}
	
	document.getElementById('contents').className = 'alphaOff';
}

function initializedrag(e){
	crossobj=ns6? document.getElementById("popup") : document.all.popup
	var firedobj=ns6? e.target : event.srcElement
	var topelement=ns6? "html" : document.compatMode && document.compatMode!="BackCompat"? "documentElement" : "body"
	while (firedobj.tagName!=topelement.toUpperCase() && firedobj.id!="pop_drag"){
		firedobj=ns6? firedobj.parentNode : firedobj.parentElement
	}
	
	document.getElementById('contents').className = 'alphaOn';

	if (firedobj.id=="pop_drag"){
		offsetx=ie4? event.clientX : e.clientX
		offsety=ie4? event.clientY : e.clientY
		
		tempx=parseInt(crossobj.style.left)
		tempy=parseInt(crossobj.style.top)
		
		dragapproved=true
		document.onmousemove=drag_drop
	}
}

document.onmouseup=new Function("dragapproved=false")

function hidebox(){
	crossobj=ns6? document.getElementById("popup") : document.all.popup
	if (ie4||ns6)
	crossobj.style.visibility="hidden"
	else if (ns4)
	document.popup.visibility="hide"
}

if (strAgent.indexOf("msie") != -1) {
  strAgent = 'IE';
} else {
  strAgent = 'FF';
}



function showbox(value){
	//if (objPopup == null) {
    objPopup = document.getElementById('popup');
    if (document.body.clientWidth) {
      posLeft = document.body.clientWidth / 2;
      posTop = document.body.clientHeight / 2;
    } else {
      posLeft = window.outerWidth / 2;
      posTop = window.outerHeight / 2;
    }
    
    //posTop = window.screenTop + (posTop - (intHeight / 2))
    //posLeft = window.screenLeft + (posLeft - (intWidth / 2))
    //posTop = window.screenTop;
    //posLeft = window.screenLeft;
    
    //window.status = 'screen.height = ' + screen.height + ' || document.body.clientHeight = ' + document.body.clientHeight + ' || window.screenTop = ' + window.screenTop;
    
  	//posLeft = (screen.width - intWidth) / 2;
    //posTop = (screen.height - intHeight) / 2;
		//objPopup.style.left = posLeft +'px';
		//objPopup.style.top = posTop - 100 +'px';
		posTop = posTop - (intHeight / 2);
		if ((posTop - 50) > 0) {
		  posTop -= 50;
		}  
		
		objPopup.style.top = posTop + 'px';
		objPopup.style.left = posLeft - (intWidth / 2) + 'px';
		
		
	//}
	
	objPopup.style.visibility="visible";
	
	getData(value);
	return void(null);
}

function handle_onreadystatechange(e) {
  if (strAgent == 'IE') {
	 	if (frmMain.readyState=='complete') {
      initialize();
  	}
	} else {
    initialize();
	}
}

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function getData(value) {
  strGet = 'productdetails.aspx?q=' + escape(value);

	http.open("GET", strGet, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			parseData(http.responseText);
		}
	}
	http.send('');
}

function parseData(value) {
	arrTemp = value.split('|');
	arrData = new Array(arrTemp.length);
	
	for (i=0; i<arrTemp.length; i++) {
		arrData[i] = new Array(1);
		arrData[i][0] = arrTemp[i].split('=')[0];
		arrData[i][1] = arrTemp[i].split('=')[1];
	}

  fillDivs();
}

function pCase(value) {
  var strReturn;
	strReturn = value.substring(0,1).toUpperCase();
	strReturn += value.substring(1).toLowerCase();
  return strReturn;
}

function fillDivs() {
	// ok, ik had direct div's kunnen vullen. 
	// Maar vanwege manipulatie string toch maar handmatig.
	
	var objDiv;
	var strDivName = '';
	var strDivValue = '';
	
	var strProduct = '';
	var strPacking = '';
	var strName = '';
	var strVolume = '';
	var strProducttype = '';
	var dblPrice;
	var strBrand = '';
	var strName1 = '';
	var strSexe = '';
	var strDescription = '';
	var strImagefile = '';
	var strImagefilelarge = '';
	var strNotitie = '';
	
	for (i=0; i<arrData.length; i++) {
		strDivName = 'div' + pCase(arrData[i][0]);
		strDivValue = arrData[i][1];
		
		switch (strDivName) {
			case 'divProduct': { strProduct = strDivValue; break; }
			case 'divPacking': { strPacking = strDivValue; break; }
			case 'divName': { strName = strDivValue; break; }
			case 'divVolume': { strVolume = strDivValue; break; }
			case 'divProducttype': { strProducttype = strDivValue; break; }
			case 'divPrice': { dblPrice = parseFloat(strDivValue); break; }
			case 'divBrand': { strBrand = strDivValue; break; }
			case 'divName1': { strName1 = strDivValue; break; }
			case 'divSexe': { strSexe = strDivValue; break; }
			case 'divDescription': { strDescription = strDivValue; break; }
			case 'divImagefile': { strImagefile = strDivValue; break; }
			case 'divNotitie': { strNotitie = strDivValue; break; }
		}
 	}
  if (strSexe !== undefined) {
		if (parseInt(strSexe) == -1) {
			strSexe = ' voor haar';
		} else if (parseInt(strSexe) == 1) {
			strSexe = ' voor hem';
		}
	}
	
	var strTitle = '<b>' + htmlEncode(strBrand, false) + '</b> - ' + htmlEncode(strName, false);
  document.getElementById('pop_title').innerHTML = strTitle;
	var strInner = '';

	  strInner = '<div id="pop_foto"></div>';
 
  strInner += '<div id="pop_description">' + AddBrakes(strNotitie) + '</div>';
	strInner += '<div id="pop_footer">';
	strInner += '<div id="pop_footer_type">' + htmlEncode(strDescription, false) + strSexe + '</div>';
	strInner += '<div id="pop_footer_volume">' + htmlEncode(strVolume, false) + '</div>';
	strInner += '</div>';
	document.getElementById('pop_inner').innerHTML = strInner;

	var MyImage = new Image();
	strImagefile = strImagefile.replace(/%3D/g, '=');
	MyImage.src = strImagefile;

	document.getElementById('pop_foto').appendChild(MyImage);
}

function AddBrakes(ReplaceStr) {
    
    var SplittedString = ReplaceStr.split("&lt;br /&gt;")
    var TotalString = "";
    var ReturnString = "";
    for(i = 0; i < SplittedString.length; i++) {
        TotalString += SplittedString[i] + "<br />";
    }
    var SplittedString = TotalString.split("\"")
    if (SplittedString.length > 1) {
        for(i = 0; i < SplittedString.length; i++) {
            ReturnString += SplittedString[i] + "&quot;";
        }
    }
    else {
        ReturnString = SplittedString;
    }
    return ReturnString;
}