var con;
var sspanic = false;
var url;
var sl_table;
var sl_tableDiv;
var sl_tableForm;
var sl_itemNumber;
var clicked_id;
var css_clicked = "shoppingListElementClicked";
var txtAddList = "";
var uoMeasure;
var sl_nunits;
var sl_nunits_element;

function getShoppingListNames(random, ident, txtAddNewList, unitOfMeasureRi, type) {
	txtAddList = txtAddNewList;
	uoMeasure = unitOfMeasureRi;
	sl_nunits = trimSpaces(document.getElementById(random).value);
	if((sl_nunits.search("\\D")) !=-1){
		alert("default to 1: "+sl_nunits);
		sl_nunits = 1;
	}
	/*test for pushbox (item_type4)*/
	if(type==4){ 
		sl_tableDiv = 'shoppingListN'+ident+'pushBox';
	} else {
		sl_tableDiv = 'shoppingListN'+ident;
	}
	if(getInternetExplorerVersion() == 8) {
		document.getElementById(sl_tableDiv).setAttribute('class','ShoppingListElementBody');
	}else if(getInternetExplorerVersion == 7) {
		document.getElementById(sl_tableDiv).setAttribute('className','ShoppingListElementBody');
	}else {
		document.getElementById(sl_tableDiv).className = 'ShoppingListElementBody';
	}
	var spec = 'getShoppingListNames'; 
	sl_itemNumber = ident;	//ident.substring(0,(ident.search("\\D")-1)); Does not work for Finland: get itemnumber from "FIE88888segnapagina" and "1420024M�rkflikar"
	if(!con) {
		getConnection();
	}
	if(con) {
		try {
			url = "ePortal/Ajax?action=shoppinglist&spec="+escape(spec);
			con.open("GET", url, true);
			con.onreadystatechange = handleChange;
			con.send(null);
		}catch (e2) {
			if(sspanic) {
				alert('fail2 ' + e2.toString());
			}
		}
	}else {
		if(sspanic) {
			alert('fail: '+con);
		}
	}
}

function handleClick(clicked_ri, clicked_number, clicked_uomeasure) {
	var clickSpec = "addItemToShoppingList";
	var cnumb = clicked_number.toString();
	var cri = clicked_ri.toString();
	clicked_id = cnumb.concat(cri);
	if(!con) {
		getConnection();
	}
	if(con) {
		try {
			clr = document.getElementById(clicked_id);
			if(clr.firstElementChild) {
				clr.firstElementChild.onclick="";
				clr.className = css_clicked;
			} else if(clr.children[0]) {
				clr.children[0].onclick="";
				clr.setAttribute('class', css_clicked);
				clr.style.color = '#aaaaaa';
			}
			url = "ePortal/Ajax?action=shoppinglist&spec="+escape(clickSpec)+"&listri="+escape(clicked_ri)+"&inumber="+escape(clicked_number)+"&nunits="+escape(sl_nunits)+"&uomeasure="+escape(clicked_uomeasure);
			con.open("GET", url, true);
			con.onreadystatechange = handleClickChange;
			con.send(null);
		}catch (e4) {
			if(sspanic) {
				alert('fail4 ' + e4.toString());
			}
		}
	}else {
		if(sspanic) {
			alert('fail: '+con);
		}
	}
}
function handleClickChange() {
	var cId = clicked_id;
	if(con.readyState == 4) { //complete
		if(con.status == 200) { //status = ok
			try {
				document.getElementById(clicked_id).firstElementChild.onclick="";
				document.getElementById(clicked_id).className = css_clicked;
			}catch(e5){
				if(sspanic) {
					alert('fail5' + e5.toString());
				}
			} finally {
				con = null;
			}
		}
	}
}
function handleChange() {
	if(con.readyState == 4) { //complete
		if(con.status == 200) { //status = ok
			try {
				sl_tableElement = document.getElementById(sl_table);
				sl_tableForm = document.getElementById(sl_tableForm);
				sl_tableDiv = document.getElementById(sl_tableDiv);
				var nameAndRiArray = con.responseText.split("|");
				var listName;
				var listRi;
				//nuke existing divs
				if(sl_tableDiv.hasChildNodes){
					while (sl_tableDiv.hasChildNodes()) {
						sl_tableDiv.removeChild(sl_tableDiv.lastChild);
					}
				}
				//generate first entry in list
				var linkRow = document.createElement("div");
				//linkRow.id = "";
				linkRow.className = "ShoppingListElementBodyElement";
				linkRow.innerHTML = "<div><a href='/web/ePortal/ctrl?action=showshoppinglistlist&spec=copyToList&row=1&itemNumber_1="+sl_itemNumber+"&qty_1=1&unitOfMeasure_1="+uoMeasure+"'>"+txtAddList+"</a></div>"
				sl_tableDiv.appendChild(linkRow);
				//generate rest of list
				for(var i=0;i<nameAndRiArray.length;i++){
					listName = nameAndRiArray[i].substring(0,nameAndRiArray[i].indexOf('?'));
					listRi = nameAndRiArray[i].substring(nameAndRiArray[i].indexOf('?')+1,nameAndRiArray[i].length);
					listName = trimSpaces(listName);
					listRi = trimSpaces(listRi);
					var row = document.createElement("div");
					row.id = sl_itemNumber+listRi;
					row.className = "ShoppingListElementBodyElement";
					row.innerHTML = "<div onClick='handleClick("+listRi+",&#34;"+sl_itemNumber+"&#34;,"+uoMeasure+");'>"+listName+"</div>"
					sl_tableDiv.appendChild(row);
				}
				sl_tableDiv.style.height = '200px';
			}catch(e3) {
				if(sspanic) {
					alert('fail3' + e3.toString());
				}
			} finally {
				con = null;
			}
			
		}
	}
}

function getConnection() {
	try {
		con = new XMLHttpRequest();
	}catch(e) {
		if(sspanic) {
			alert('fail '+ e.toString());
		}
	}
}
function trimSpaces(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}