var flagtest = location.href.indexOf('http://10.16.132.178/')>-1? true: false;
var xmlhttp;
var objhtml;
var objimg;
MAXPRODUCT = 5;

function doGetHTML(url, id, imgid){
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp!=null){
		objhtml = document.getElementById(id);
		objimg = document.getElementById(imgid);
		if (objhtml!=undefined){
			xmlhttp.onreadystatechange = readyHandler;
			xmlhttp.open('GET', url, true);
			xmlhttp.send(null);
		}		
	}
}

function readyHandler(){
	if(xmlhttp.readyState==4){
		xhrhtml = xmlhttp.responseText;
		resulthtml = xhrhtml.split('</body>')[0];
		arrtmp = resulthtml.split('<body');
		if (arrtmp.length==2){
			arrtmp2 = arrtmp[1].split('>');
			resulthtml = '';
			for (var i=1;i<arrtmp2.length;i++){
				resulthtml += (i>1? '>': '') + arrtmp2[i];
			}
		}
		objhtml.innerHTML = resulthtml;
		setImgTitle(xhrhtml);
	}
}

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		return new XMLHttpRequest();
  	}else if (window.ActiveXObject){
		// code for IE6, IE5
  		return new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return null;
}

function setImgTitle(html){
	var strtitle = '';
	arrclass = ['brand','price'];
	for (var i=0;i<arrclass.length;i++){
		arrtmp = html.split('<div class="'+arrclass[i]+'">');
		if (arrtmp.length==2){
			strtitle += (strtitle!=''?' ':'') + arrtmp[1].split('</div>')[0];
		}
	}
	objimg.title = utf8Decode(strtitle);
}

function utf8Decode(input){
	var result = '';
	var character = '';
	var isUTF8 = false;
	arrtmp = input.split(';');
	for (var i=0;i<arrtmp.length;i++){
	
		character = '';
		isUTF8 = false;
		var tmpresult = '';
		arrtmp2 = arrtmp[i].split('&#');
		tmpresult += arrtmp2[0];
		if (arrtmp2.length>1){
			if (!isNaN(arrtmp2[1])){
				intChar = parseInt(arrtmp2[1]);
				if ((intChar+'')==arrtmp2[1]){
					isUTF8 = true;
				}
			}
			
			start=1;
			if (isUTF8){
				tmpresult += String.fromCharCode( intChar );
				start=2;
			}

			for (var j=start;j<arrtmp2.length;j++){
				tmpresult += (j>0? '&#': '') + arrtmp2[j];
			}
			
		}	

		result += tmpresult + (((i<arrtmp.length-1) && !isUTF8)? ';': '' );
	
	}
	return result;
}


function doGetXML(id){
	pcode = getURLParam('pcode');
	if (pcode!=''){
		url = location.protocol+'//'+location.host+'/images/bannerxml/'+pcode+'.xml';
		xmlhttp = GetXmlHttpObject();
		if (xmlhttp!=null){
			objhtml = document.getElementById(id);
			if (objhtml!=undefined){
				xmlhttp.onreadystatechange = readyHandlerXML;
				xmlhttp.open('GET', url, true);
				xmlhttp.send(null);
			}		
		}
	}
}

function readyHandlerXML(){
	if(xmlhttp.readyState==4){
		var flagTruncate = getURLParam('truncate')=='1'? true: false;
		tmp = getURLParam('maxchar');
		var maxchar = parseInt(tmp!=''? tmp: '20');
		fontsize = getURLParam('fontsize');
		htmlstyle = fontsize!=''? 'style="font-size:'+fontsize+'pt;" ': '';
		
		var htmlresult = '';
		arrTag = ['thumbnaillink','productlink','merk','type','pricelink','prijsmin','prijsmax'];
		arrTagvalue = new Array();
		xhrxml = xmlhttp.responseText;
		arrproduct = xhrxml.split('</productdetails>');
		//alert(arrproduct.length);
		if (arrproduct.length>1){
			for (var i=0;i<arrproduct.length && i<MAXPRODUCT;i++){
				arrTagvalue.length=0;
				for (var j=0;j<arrTag.length;j++){
					arrTagvalue[arrTagvalue.length] = getTagvalue(arrproduct[i], arrTag[j])					;
				}
				htmlresult += '<li>';
				htmlresult += '<div class="productbox"><div class="imagebox">';
				htmlresult += '<a href="'+arrTagvalue[1]+'"><img class="imgproduct" alt="" src="'+arrTagvalue[0]+'" border="0" /></a>';
				htmlresult += '</div><div class="descriptionbox">';
				
				description = arrTagvalue[2]+' '+arrTagvalue[3];
				htmlresult += '<a href="'+arrTagvalue[1]+'" class="title" target="_blank"'+(flagTruncate? ' title="'+description.replace(/\\/g,'\\\\')+'"': '')+ ' ' + htmlstyle + '>';
				htmlresult += (flagTruncate && description.length>maxchar? (description.substring(0, maxchar-1)+'...'): description)+'</a><br />';
				htmlresult += '<a href="'+arrTagvalue[4]+'" class="prices" target="_blank">Van &euro; '+arrTagvalue[5]+' tot &euro; '+arrTagvalue[6]+'</a>';
				htmlresult += '</div></div>';
				htmlresult += '</li>';
			}
		}
		if (htmlresult!=''){
			htmlresult = '<ul>' + htmlresult + '</ul>';
			if (flagtest){
				//alert(htmlresult);
			}
		}
		objhtml.innerHTML = htmlresult;
	}
}

function getTagvalue(xml, tag){
	var result = '';
	arrtmp1 = xml.split('</'+tag+'>');
	if (arrtmp1.length>=2){
		arrtmp2 = arrtmp1[0].split('<'+tag+'>');
		if (arrtmp2.length==2){
			result = arrtmp2[1];
		}
	}
	return result;
}

function getURLParam(param){
	search = unescape(location.search);
	arrtmp1 = search.split('&');
	for (var i=0;i<arrtmp1.length;i++){
		arrtmp2 = arrtmp1[i].split(param+'=');
		if (arrtmp2.length>=2){
			return arrtmp2[arrtmp2.length-1];
		}
	}
	return '';
}
