// JavaScript Document
var IE = document.all?true:false
var thisID = '0'

function aspHtmlDecode(s) {
	// Functie op een string gecodeerd in asp met server.htmlencode te decoderen.
	// Versie 1.0 (07-03-2007)
	s = s.replace(/&lt;/g,"<");
	s = s.replace(/&gt;/g,">");
	s = s.replace(/&quot;/g,"\"");
	s = s.replace(/&amp;/g,"&");
	return s;
}

function GetHttpRequest() { // Functie die http_request vult met een XmlHttp object, geeft false terug als er een fout optreedt.
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 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('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	return http_request
}

function quickList(id) {
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	var code = ''
	if(document.getElementById('code'+id)) code = document.getElementById('code'+id).value
	var aantal = ''
	if(document.getElementById('aantal'+id)) aantal = document.getElementById('aantal'+id).value

	//alleen wanneer ingevuld...
	if((code!='' && aantal!='') || id==0) { 
		var url = "xml/quickList.xml.asp?id="+id+"&code="+code+"&aantal="+aantal
		http_request.onreadystatechange = function() { GetResultQuickList(http_request); };
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}

function GetResultQuickList(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if(!IE) http_request.responseXML.normalize() 
			
			var xmldoc = http_request.responseXML;

			var id_node = xmldoc.getElementsByTagName('id').item(0);
			thisID = id_node.firstChild.data
			var nextID = parseInt(id_node.firstChild.data)+1

			var content_node = xmldoc.getElementsByTagName('content').item(0);
			if(content_node.firstChild) {
				document.getElementById('c_articles').innerHTML = content_node.firstChild.data

//				var cont = document.getElementById('c_articles')
//				var e_code = document.createElement('input');
//				e_code.setAttribute('type','text'); 
//				e_code.setAttribute('name','code'+thisID);
//				e_code.setAttribute('id','code'+thisID);
//				e_code.setAttribute('title','code'+thisID);
//				e_code.style.width = '100px';
//
//				var e_aantal = document.createElement('input');
//				e_aantal.setAttribute('type','text'); 
//				e_aantal.setAttribute('name','aantal'+thisID);
//				e_aantal.setAttribute('id','aantal'+thisID);
//				e_aantal.onblur = function om(){quickList(nextID2=thisID)}
//				e_aantal.style.width = '50px';
//				cont.innerHTML += '<div id="c_art_'+nextID+'"><table cellpadding="0" cellspacing="0" border="0">' +
//													'<tr>' +
//													'<td width="20"><span id="cont_nr_'+thisID+'">' + nextID + '</span>.</td>' + 
//													'<td width="120" id="cont_code_'+thisID+'"></td>' + 
//													'<td width="300" id="cont_aantal_'+thisID+'"></td>' +
//													'</tr>' +
//													'</table></div>';
//				document.getElementById('cont_code_'+thisID).appendChild(e_code);
//				document.getElementById('cont_aantal_'+thisID).appendChild(e_aantal);

				setTimeout(function () { 
				if (document.getElementById('code'+thisID)) {
					window.focus()
					document.getElementById('code'+thisID).focus()
				}
				; }, 500);				
			}
		}
		
	}
}

function updateArticle(id) {
	var http_request = GetHttpRequest();
	if(!http_request) return false; // Kan geen ajax component aanmaken.

	var code = ''
	if(document.getElementById('code'+id)) code = document.getElementById('code'+id).value
	var aantal = ''
	if(document.getElementById('aantal'+id)) aantal = document.getElementById('aantal'+id).value

	var url = "xml/updateArticle.xml.asp?id="+id+"&code="+code+"&aantal="+aantal
	//window.open(url)
	//http_request.onreadystatechange = function() { GetResultQuickList(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function submitField(e, id){
	var k;
	document.all ? k = e.keyCode : k = e.which;
	if (k == 13) {
		quickList(id)
		return false
	}else{
		return ((k > 47 && k < 58 || k == 44 || k == 46 || k == 0));
	}
}

function isNumberQuick(e,id) {
	var k;
	document.all ? k = e.keyCode : k = e.which;
	if (k == 13) {
		if(document.getElementById(id)) document.getElementById(id).focus();
	}
}

function isNumberGo2Next(e,id) {
	var k;
	document.all ? k = e.keyCode : k = e.which;
	if (k == 13) {
		if(document.getElementById('dummy_field')) document.getElementById('dummy_field').focus();
		if(document.getElementById(id)) document.getElementById(id).focus();
	}
	if(id.indexOf('articleid')>-1){ //inkooporder inputveld, bij quickorder moet wel een '-' geaccepteerd worden (GPS)
		return ((k > 47 && k < 58 || k == 44 || k == 46 || k == 0));
	}else{
		return ((k > 47 && k < 58 || k == 44 || k == 45 || k == 46 || k == 0));
	}
}

function checkQuickList() {
	document.getElementById('frmQuick').submit()
}


