/*
@author	Johan
*/
function clearSearchFields()
{
	clearBox('proSearch');
	clearBox('modSearch');
	clearBox('refSearch');
}



function clearBox(id)
{
	document.getElementById(id).value = '';
}



function highlight(id)
{
	var type = id.substring(0, 1);
	var listId, searchId;

	clearSearchFields()

	switch (type)
	{
		case 'p':
		{
			listId='producers';
			document.getElementById("proSearch").value=document.getElementById(id).firstChild.nodeValue;
		}
		break;
		case 'm':
		{
			listId='models';
			document.getElementById("modSearch").value=document.getElementById(id).firstChild.nodeValue;
		}
		break;
		case 'a':
		{
			listId='articles';
			if(isSet(document.getElementById(id).firstChild.firstChild))
			{
				document.getElementById("refSearch").value=document.getElementById(id).firstChild.firstChild.nodeValue;
			}
			else
			{
				document.getElementById("refSearch").value=document.getElementById(id).firstChild.nodeValue;
			}
		}
		break;
	}
	var items = document.getElementById(listId).getElementsByTagName('li');
	for (var i = 0; i < items.length; i++)
	{
		items[i].style.backgroundColor = '#ffffff';
	}
	document.getElementById(id).style.backgroundColor = '#cccccc';
}



function searchValue(e, type, searchString, more)
{
	if(!isSet(searchString))
	{
		searchString="";
	}
	if(!isSet(more))
	{
		more=0;
	}
	var container="";
	switch(type)
	{
		case "Prod": container='producers';break;
		case "Series":
		case "Model":
		{
			container='models';
			if(searchString==xmlDoc.getElementsByTagName("modelexample")[0].childNodes[0].nodeValue)
			{
				searchString="";
			}
			var advanced=document.getElementById('advancedBox').getElementsByTagName('input');
			var technology=0;
			var device=0;
			for(var i=0;i<advanced.length;i++)
			{
				if(advanced[i].checked==true)
				{
					if(advanced[i].name=="technology")
					{
						technology=advanced[i].value;
					}
					else if(advanced[i].name=="devise")
					{
						device=advanced[i].value;
					}
				}
			}
		}
		break;
		case "Art": container='articles';break;
	}
	var searchingText=xmlDoc.getElementsByTagName("searching")[0].childNodes[0].nodeValue;
	document.getElementById(container).innerHTML="<li>"+searchingText+" \""+searchString+"\"...</li>";

	if(e==0 || keyCheck(e) || more==1)
	{
	  if (!window.JMB)
		{
			window.JMB = {};
		}
		if (window.JMB.timeoutId)
		{
			window.clearTimeout(window.JMB.timeoutId);
		}
		switch(type)
		{
			case "Prod":
			case "Art":window.JMB.timeoutId = window.setTimeout(function () { loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=search'+ type +'&value=' + searchString+'&more='+more, 'json', postSearch); }, 777);break;
			case "Series":
			case "Model":window.JMB.timeoutId = window.setTimeout(function () { loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=search'+ type +'&value=' + searchString+'&more='+more+'&tech=' + technology+'&dev=' + device, 'json', postSearch); }, 777);break;
		}
	}
}



function postSearch(data)
{
	document.getElementById(data.container).innerHTML=data.string;
	if(isSet(data.action))
	{
		switch(data.action.substring(0,2))
		{
			case "0p":break;
			case "1p":loadProd(data.action.substring(1));break;
			case "0m":break;
			case "1m":loadModel(data.action.substring(1));break;
			case "0a":;break;
			case "1a":loadArt(data.action.substring(1));break;
			case "1b":loadBruneau("a"+data.action.substring(2), data.cat);break;
		}
	}
}



function keyCheck(key)
{
	var keyNum;
	if(window.event) // IE
	{
		keyNum = key.keyCode;
	}
	else if(key.which) // Netscape/Firefox/Opera
	{
		keyNum = key.which;
	}
	if(keyNum==8 || (keyNum>45 && keyNum<91) || (keyNum>96 && keyNum<112) || (keyNum>185 && keyNum<223))
	{
		return true;
	}
	else
	{
		return false;
	}
}



function loadProd(id)
{
	if(isSet(document.getElementById(id)))
	{
		highlight(id);
		var brand=document.getElementById(id).innerHTML;
		var searchingModels=xmlDoc.getElementsByTagName("searchingModels")[0].childNodes[0].nodeValue;
		var searchingArticles=xmlDoc.getElementsByTagName("searchingArticles")[0].childNodes[0].nodeValue;
		document.getElementById('models').innerHTML="<li>"+searchingModels+" \""+brand+"\"...</li>";
		document.getElementById('articles').innerHTML="<li>"+searchingArticles+" \""+brand+"\"...</li>";

		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selModel&id=' + id, 0, 'models');
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selArt&id=' + id, 0, 'articles');
	}
}



function loadModel(id)
{
	if(isSet(document.getElementById(id)))
	{
		highlight(id);
		var model=document.getElementById(id);
		var favorite=model.getElementsByTagName('img')[0];
		if(isSet(favorite))
		{
			model.removeChild(favorite);
		}
		model=model.innerHTML;
		var loadingArticles=xmlDoc.getElementsByTagName("loadingModArticles")[0].childNodes[0].nodeValue;
		var searchingBrands=xmlDoc.getElementsByTagName("searchingBrands")[0].childNodes[0].nodeValue;
		var searchingArticles=xmlDoc.getElementsByTagName("searchingArticles")[0].childNodes[0].nodeValue;
		document.getElementById('contentContainer').innerHTML="<li>"+loadingArticles+" \""+model+"\"...</li>";
		document.getElementById('producers').innerHTML="<li>"+searchingBrands+" \""+model+"\"...</li>";
		document.getElementById('articles').innerHTML="<li>"+searchingArticles+" \""+model+"\"...</li>";
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=viewCart&id=' + id + '&idType=mod&view=' + document.getElementById("view").value + "&comp=" + document.getElementById("comp").value + "&info=" + document.getElementById("info").value, 0, 'contentContainer');
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selProd&id=' + id, 0, 'producers');
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selArt&id=' + id, 0, 'articles');
	}
}



function loadArt(id)
{
	if(isSet(document.getElementById(id)))
	{
		highlight(id);
		var article=document.getElementById(id).innerHTML;
		var loadingArticles=xmlDoc.getElementsByTagName("loadingModArticles")[0].childNodes[0].nodeValue;
		var searchingBrands=xmlDoc.getElementsByTagName("searchingBrands")[0].childNodes[0].nodeValue;
		var searchingModels=xmlDoc.getElementsByTagName("searchingModels")[0].childNodes[0].nodeValue;
		document.getElementById('contentContainer').innerHTML="<li>"+loadingArticles+" \""+article+"\"...</li>";
		document.getElementById('producers').innerHTML="<li>"+searchingBrands+" \""+article+"\"...</li>";
		document.getElementById('models').innerHTML="<li>"+searchingModels+" \""+article+"\"...</li>";
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=viewCart&id=' + id + '&idType=art&view=' + document.getElementById("view").value + "&comp=" + document.getElementById("comp").value + "&info=" + document.getElementById("info").value, 0, 'contentContainer');
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selProd&id=' + id, 0, 'producers');
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selModel&id=' + id, 0, 'models');
	}
}



function loadBruneau(id, cat)
{
	if(isSet(cat))
	{
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=viewCart&id=' + id + '&idType=art&view=' + document.getElementById("view").value + "&comp=" + document.getElementById("comp").value + "&info=" + document.getElementById("info").value + "&cat=" + cat, 0, 'contentContainer');
	}
	else
	{
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=viewCart&id=' + id + '&idType=art&view=' + document.getElementById("view").value + "&comp=" + document.getElementById("comp").value + "&info=" + document.getElementById("info").value, 0, 'contentContainer');
	}
	loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selProd&id=' + id, 0, 'producers');
	loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=selModel&id=' + id, 0, 'models');
}



window.onload=function ()
{
	init();
	document.getElementById('producers').onclick = function (e)
	{
		if (!e)
		{
			var e = window.event;
		}
		e.cancelBubble = true;
		if (e.stopPropagation)
		{
			e.stopPropagation();
		}
		var target = e.target || e.srcElement;
		if(target.tagName!="UL" && target.className!="more")
		{
			loadProd(target.id);
		}
	}
	document.getElementById('models').onclick = function (e)
	{
		if (!e)
		{
			var e = window.event;
		}
		e.cancelBubble = true;
		if (e.stopPropagation)
		{
			e.stopPropagation();
		}
		var target = e.target || e.srcElement;

		if(target.tagName!="UL" && target.tagName!="IMG" && target.className!="more")
		{
			if(isSet(target.id))
			{
				loadModel(target.id);
			}
			else
			{
				searchValue(0, "Series", target.innerHTML);
			}
		}
	}
	document.getElementById('articles').onclick = function (e)
	{
		if (!e)
		{
			var e = window.event;
		}
		e.cancelBubble = true;
		if (e.stopPropagation)
		{
			e.stopPropagation();
		}
		var target = e.target || e.srcElement;

		if(target.tagName=="SPAN")
		{
			target=target.parentNode;
		}
		if(target.tagName!="UL" && target.className!="more")
		{
			loadArt(target.id);
		}
	}
}



function changeView(id)
{
	var view=document.getElementById("view").value;
	if(id=='showComp' || id=='showInfo' || view!=id)
	{
		var button = document.getElementById(id);
		if (button.src.match('Active') == null)
		{
			var source=button.src.replace('.png', 'Active.png');
		}
		else
		{
			var source=button.src.replace('Active', '');
		}
		switch (id)
		{
			case 'listView':
			case 'picView':
			{
				button.setAttribute( 'src', source );
				document.getElementById("view").value=id;
				loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=viewCart&id=' + document.getElementById("selectedId").value + '&idType=' + document.getElementById("selectedIdType").value + '&view=' + id + "&comp=" + document.getElementById("comp").value + "&info=" + document.getElementById("info").value, 0, 'contentContainer');
			}
			break;
			case 'showComp':
			{
				if(view=='listView')
				{
					if(isSet(document.getElementById('listContent')))
					{
						button.setAttribute( 'src', source );
						var table = document.getElementById('listContent');
						var allTableRows=table.getElementsByTagName('tr');
						if (document.getElementById('comp').value==1)
						{
							document.getElementById('comp').value=0;
							button.onmouseover=function () {overlib("<p style='text-align:left;padding:5px;'>"+xmlDoc.getElementsByTagName('showComp')[0].childNodes[0].nodeValue+"</p>", OFFSETX, 25, FGCOLOR, "#CCFFFF", HAUTO, VAUTO)};
							for (var i=0; i<allTableRows.length; i++)
							{
								if(allTableRows[i].className=="compatible")
								{
									allTableRows[i].style.display="none";
								}
							}
						}
						else
						{
							document.getElementById('comp').value=1;
							button.onmouseover=function () {overlib("<p style='text-align:left;padding:5px;'>"+xmlDoc.getElementsByTagName('showCompActive')[0].childNodes[0].nodeValue+"</p>", OFFSETX, 25, FGCOLOR, "#CCFFFF", HAUTO, VAUTO)};
							for (var i=0; i<allTableRows.length; i++)
							{
								if(allTableRows[i].className=="compatible")
								{
									allTableRows[i].style.display="table-row";
								}
							}
						}
					}
				}
				else
				{
					if(isSet(document.getElementById('rightContent')) && isSet(document.getElementById('leftContent')))
					{
						button.setAttribute( 'src', source );
						var rightContent = document.getElementById('rightContent');
						var leftContent = document.getElementById('leftContent');
						if (document.getElementById('comp').value==1)
						{
							document.getElementById('comp').value=0;
							button.onmouseover=function () {overlib("<p style='text-align:left;padding:5px;'>"+xmlDoc.getElementsByTagName('showComp')[0].childNodes[0].nodeValue+"</p>", OFFSETX, 25, FGCOLOR, "#CCFFFF", HAUTO, VAUTO)};
							rightContent.style.display = 'none';
							leftContent.style.width = '740px';
						}
						else
						{
							document.getElementById('comp').value=1;
							button.onmouseover=function (){overlib("<p style='text-align:left;padding:5px;'>"+xmlDoc.getElementsByTagName('showCompActive')[0].childNodes[0].nodeValue+"</p>", OFFSETX, 25, FGCOLOR, "#CCFFFF", HAUTO, VAUTO)};
							if(navigator.userAgent.toLowerCase().indexOf('msie') != -1)
							{
								rightContent.style.display = 'block';
							}
							else
							{
								rightContent.style.display = 'table';
							}
							leftContent.style.width = '575px';
						}
					}
				}
			}
			break;
			case 'showInfo':
			{
				button.setAttribute( 'src', source );
				if(view=='listView')
				{
					var table=document.getElementById('listContent');
				}
				else
				{
					var table=document.getElementById('leftContent');
				}
				var allSpans=table.getElementsByTagName('span');
				if (document.getElementById('info').value==1)
				{
					document.getElementById('info').value=0;
					button.onmouseover=function(){overlib("<p style='text-align:left;padding:5px;'>"+xmlDoc.getElementsByTagName('showInfo')[0].childNodes[0].nodeValue+"</p>", OFFSETX, 25, FGCOLOR, "#CCFFFF", HAUTO, VAUTO)};
					for (var i=0; i<allSpans.length; i++)
					{
						if(allSpans[i].className=="extraInfo")
						{
							allSpans[i].style.display="none";
						}
					}
				}
				else
				{
					document.getElementById('info').value=1;
					button.onmouseover=function(){overlib("<p style='text-align:left;padding:5px;'>"+xmlDoc.getElementsByTagName('showInfoActive')[0].childNodes[0].nodeValue+"</p>", OFFSETX, 25, FGCOLOR, "#CCFFFF", HAUTO, VAUTO)};
					for (var i=0; i<allSpans.length; i++)
					{
						if(allSpans[i].className=="extraInfo")
						{
							allSpans[i].style.display="inline";
						}
					}
				}
			}
			break;
		}
	}
}


/*JBEG: OPTION NOT AVAILABLE ANYMORE
function requestCartridge(id)
{
	loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=requestCart&id=' + id, 0, 'contentContainer');
}



function sendRequest()
{
	if(isSet(document.getElementById("mailContent")) && isSet(document.getElementById("mailContent").value))
	{
		if(isSet(document.getElementById("firm")) && isSet(document.getElementById("firm").value))
		{
			if(isSet(document.getElementById("email")) && validEmail(document.getElementById("email").value))
			{
				if(isSet(document.getElementById("telNr")) && isSet(document.getElementById("telNr").value))
				{
					var varString="func=sendRequest&content="+encodeURIComponent(document.getElementById("mailContent").value)+"&firm="+encodeURIComponent(document.getElementById("firm").value)+"&email="+encodeURIComponent(document.getElementById("email").value)+"&telNr="+encodeURIComponent(document.getElementById("telNr").value);
					if(isSet(document.getElementById("lastName")) && isSet(document.getElementById("lastName").value))
					{
						varString+="&lastName="+encodeURIComponent(document.getElementById("lastName").value);
					}
					if(isSet(document.getElementById("firstName")) && isSet(document.getElementById("firstName").value))
					{
						varString+="&firstName="+encodeURIComponent(document.getElementById("firstName").value);
					}
					if(isSet(document.getElementById("userId")) && isSet(document.getElementById("userId").value))
					{
						varString+="&userId="+encodeURIComponent(document.getElementById("userId").value);
					}
					loadFile('post', '/includes/cartridgeFuncAjax.php', varString, 'text',function (data) {startAlert(data);});
					hideNseek('requestCartridge');
				}
				else
				{
					startAlert(27);
				}
			}
			else
			{
				startAlert(4);
			}
		}
		else
		{
			startAlert(26);
		}
	}
	else
	{
		startAlert(25);
	}
}



function saveRequestData(field, value)
{
	loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=saveRequestData&field='+field+'&fieldValue='+value);
}
*/


/*
action='del' || 'add'
type='printer' || 'supply'
*/
function changeFavorite(action, type, id)
{
	loadFile('post', '/includes/cartridgeFuncAjax.php', 'func='+action+capitalize(type)+'Favorite&id='+id);
/*CHANGES THE FAVORITES LIST*/
	var div='fav'+capitalize(type);
	if(isSet(document.getElementById(div)))
	{
		document.getElementById(div).style.display='none';
	}
	displayFavorites(type);
	if(type=='printer')
	{
		if(isSet(document.getElementById('favSupply')))
		{
			document.getElementById('favSupply').style.display='none';
		}
		displayFavorites('supply');
	}
/*END CHANGES THE FAVORITES LIST*/
/*CHANGES THE MODELBOX*/
	if(type=='printer')
	{
		var mId='m'+id;
		var actionToDo='';
		switch(action)
		{
			case 'del':actionToDo='add';break;
			case 'add':actionToDo='del';break;
		}
		if(isSet(document.getElementById(mId)))
		{
			document.getElementById(mId).getElementsByTagName('img')[0].src="/images/general/buttons/"+actionToDo+"Favorites.png";
			document.getElementById(mId).getElementsByTagName('img')[0].alt=xmlDoc.getElementsByTagName(actionToDo+"Fav")[0].childNodes[0].nodeValue;
			document.getElementById(mId).getElementsByTagName('img')[0].onclick=function(){changeFavorite(actionToDo, type, id)};
			document.getElementById(mId).getElementsByTagName('img')[0].onmouseover=function(){return overlib(xmlDoc.getElementsByTagName(actionToDo+"Fav")[0].childNodes[0].nodeValue, OFFSETX, 25, FGCOLOR, "#CDDDEE", HAUTO, VAUTO)};
		}
	}
/*END CHANGES THE MODELBOX*/
/*CHANGES THE CARTRIDGE DETAILBOX*/
	var view=document.getElementById('view').value;
	if(isSet(view) && isSet(document.getElementById('selectedId')))
	{
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=viewCart&id=' + document.getElementById("selectedId").value + '&idType=' + document.getElementById("selectedIdType").value + '&view=' + view + "&comp=" + document.getElementById("comp").value + "&info=" + document.getElementById("info").value, 0, 'contentContainer');
	}
/*END CHANGES THE CARTRIDGE DETAILBOX*/
}



function displayFavorites(type)
{
	var div='fav'+capitalize(type);
	if(isSet(document.getElementById(div)) && document.getElementById(div).style.display=='block')
	{
		document.getElementById(div).style.display='none';
	}
	else
	{
		loadFile('post', '/includes/cartridgeFuncAjax.php', 'func=displayFavorites&type='+type, 'text', div);
		document.getElementById(div).style.display='block';
	}
}