

function changeColor(obj, color) {
	if (obj) {
		obj.className = color;
	}
}
function gotoPage(url) {
	if (url) {
		document.location.href = url;
	}
}
function changeBackgroundColor(obj, color) {
	if (obj && color) {
		obj.style.backgroundColor = color;
	}
}

function changeCursor(obj, cursor) {
	if (obj && cursor) {
		obj.style.cursor = cursor;
	}
}
function checkCheckBox(number) {
	var counter = 0;
	for (var i = 1; i <= 5; i++) {
		var id = ("c" + i);
		var obj = document.getElementById(id);
		if (obj && obj.checked == true) {
			counter++;
		}
	}
	if (counter > 3) {
		var id = ("c" + number);
		document.getElementById(id).checked = false;
	}
}
function setActiveStyleSheet(title) {
	var i, a, main;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		var rel = a.getAttribute("rel");
		var t = a.getAttribute("title");
		if (rel.indexOf("style") != -1 && t) {
			a.disabled = true;
			if (a.getAttribute("title") == title) {
				a.disabled = false;
			}
		}
	}
}
function changeText(obj, text) {
	var label = document.getElementById("t2");
	var myText = document.createTextNode(text);
		label.replaceChild(myText, label.firstChild);
}

//product groep javascript funvties
var selectedList;
var availableList;




function selectNone(list1,list2){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}

function selectNone(list1,list2){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}

// Floating flash div
var globalSpeed;
	var flashWidth = 533;
	var flashHeight = 400;
	var globalMovie = 'home';

		// This is a javascript handler for the player and is always needed.
		function thisMovie(movieName) {
		  if(navigator.appName.indexOf("Microsoft") != -1) {
		  		alert(document[movieName]);
				return document[movieName];
			} else {
				return $(movieName);
			}
		}
		
		function loadFile(obj) { thisMovie("mpl").loadFile(obj); };

		
function openFloatingFlash(playlistFile) {
	try {
		globalSpeed = 512;
		globalMovie = 'home';
	
		createFlashPlayer('player', playlistFile, flashWidth, flashHeight);
		var floater = $('floatingFlash');
		var _left = ((document.body.scrollWidth / 2) - (flashWidth / 2)) + 'px';
		var _top = ((document.body.scrollHeight / 2) - 100 - (flashHeight / 2)) + 'px';
		Element.setStyle(floater, { left: _left, top: _top});
		toggleFlash();
	} catch(e) {}
}

function toggleFlash() {
	try {
		//new Effect.toggle('floatingFlash', 'blind');
		$('floatingFlash').toggle();
	} catch(e) {}
}

function createFlashPlayer(parentElement, fileName, _width, _height) {
	try {
		var so = new SWFObject('flash/mediaplayer.swf','mpl', _width, _height + 20, '8');
		so.addParam("allowfullscreen","true");
		so.addVariable("file", fileName);
		so.addVariable("enablejs","false");
		so.addVariable("javascriptid","mpl");
		so.addVariable("displayheight",_height);
		so.addVariable("displaywidth", _width);
		so.addVariable("frontcolor", "0x000000");
		so.addVariable("backcolor", "0xe4f2fc");
		so.addVariable("lightcolor", "0x10147f");
		so.addVariable("autoscroll", "false");
		so.addVariable("overstretch", "true");
		so.addVariable("showdigits", "true");
		so.addVariable("thumbsinplaylist", "true");
		so.addVariable("autostart", "true");
		so.addVariable('repeat','list');
		so.addVariable("shuffle", "false");
		so.addVariable("location", "flash/mediaplayer.swf");
		so.write(parentElement);
	} catch(e) {}
}

function stopMovie() {
	try {
		$('player').innerHTML = "";
	} catch (e) {
	}
}

// some variables to save
var currentPosition;
var currentVolume;
var currentItem;


// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { 
	try {
		thisMovie("mpl").sendEvent(typ,prm);
	} catch(e) {}
 };
 
function getUpdate(typ,pr1,pr2,pid) {
	return;
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; }
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(currentItem)",100); }
	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"<\/i>)";
	}
};

// These functions are caught by the feeder object of the player.

function addItem(obj,idx) { thisMovie("mpl").addItem(obj,idx); }
function removeItem(idx) { thisMovie("mpl").removeItem(idx); }
function getItemData(idx) {
	try {
		var obj = thisMovie("mpl").itemData(idx);
		var nodes = "";
		for(var i in obj) { 
			nodes += "<li>"+i+": "+obj[i]+"<\/li>"; 
		}
		//document.getElementById("data").innerHTML = nodes;
	} catch(e) {}
}


function setPlaylist(speed, _page,  e) {
	try {
		globalSpeed = speed;
		stopMovie();
		var fileName = "/episodes/playlist_" + _page + "_" + speed + ".xml";
		createFlashPlayer('player', fileName, flashWidth, flashHeight);
		globalMovie = _page;
	} catch(e) {}	
}

/********* Product group scripts **************/

function fixPaging(currentPage, formName){
	if (dirty) {
		if (!confirm('U heeft al 1 of meerdere artikelen ingevuld.\nAls u doorgaat zullen deze artikelen niet aan uw winkelwagen worden toegevoegd.')) {
			return false;
		}
	}
	var form = document.forms[formName];
	form.page.value = currentPage;
	postForm();
}

function updatePagesize(newValue) {
	$('myDataForm').pagesize.value = newValue;
}

function updatePhotoList(newValue) {
	$('myDataForm').photolist.value = newValue;
}


function sorting(columnName, newSortOrder, formName){
	var form = document.forms[formName];
	form.sortOrder.value = newSortOrder;
	form.sortColumn.value = columnName;
	form.mode.value = 'sort';
	postForm();
}


function updatePage(transport) {
	dirty = false;
	var theJSON = eval('('+transport.responseText+')');
	if(theJSON.navigationAttributes.length != 0) {
		writeFilters(theJSON);
	}
	$('myDataForm').initialize.value = 0;
	updateBrandSelection(theJSON.selectedDealers, theJSON.dealerCount);
	updateFilterSelection(theJSON.selectedFilters);
	writeProducts(theJSON.groupID, theJSON, $('products'));
	bottomResult = writeBottomTable(theJSON, $('products'), undefined, 2);
	topResult = writeBottomTable(theJSON, $('products'), "height: 55px;", 1);
    ieTableInnerHTML($('bottomTable'), bottomResult, 'bottomContainer');
    ieTableInnerHTML($('topTable'), topResult, 'topContainer');
}

function updateFilterSelection(selectedFilters) {
	$A(selectedFilters).each(
		function(e) {		
			$('filter-' + e.attributeId).value = e.value;
		}
	);
}

function updateBrandSelection(selectedDealers, numberOfDealers) {
	var count = 0;
	
	if (numberOfDealers != 0) {
		var dealers = document.getElementsByClassName("dealerCheckbox", $('dealerBox'));
		dealers.each(
			function(e) {
				var dealerName = $(e).id.split("_")[1];
				if ($A(selectedDealers).indexOf(dealerName) != -1) {
					e.checked = true;
					++count;
				} else {
					e.checked = false;
				}
			}
		);
	
		if (count == numberOfDealers || count == 0) {
			$("allDealersOn").checked = true;
		} else {
			$("allDealersOn").checked = false;
		}
	}
}

function toggleAllDealers(doRefresh) {
	var allBoxChecked = $F('allDealersOn');

	var dealers = document.getElementsByClassName("dealerCheckbox", $('dealerBox'));
	dealers.each(
		function(e) {
			e.checked = allBoxChecked;
		}
	);
	postForm();
}

function writeFilters(theJSON){
	var navigationAttributes = theJSON.navigationAttributes;
	var isSelected = false;
	var selectElement ="";
	var optionElement ="";
	var tdElement ="";
	var trElement ="";
	var tableElement ="";
			
    for(var i=0; i<navigationAttributes.length; i++){
        var nAtt = navigationAttributes[i];
        if(nAtt && nAtt.valueList != undefined){
            if(nAtt.attribute.attribute != "Merk"){
           		tdElement = writeElement("td", nAtt.attribute.attribute + (nAtt.attribute.unit.length > 0 ? " (" + nAtt.attribute.unit + ")" : "") , "label", "vertical-align:middle");
                optionElement += writeElement("option", "- Alles weergeven -", undefined, undefined, "value=''");
                for(var j =0; j<nAtt.valueList.length; j++){
                    var attributeID = nAtt.attribute.attributeID;
                    var sO = document.getElementById("filter-"+attributeID);
                    var nVl = nAtt.valueList[j];
                    if(sO != undefined){
                        if(sO.value == nVl.value){
                           isSelected = true;
                        }
                    }
                    if(isSelected){
		                optionElement += writeElement("option", nVl.valueDisplayString, undefined, undefined, "value='"+ nVl.value +"' selected='selected'");
	                }else{
	                	optionElement += writeElement("option", nVl.valueDisplayString, undefined, undefined, "value='"+ nVl.value + "'");
	                }
	                isSelected = false;
                }
       			//if (nAtt.valueList.length > 0) {
	                selectElement = writeElement("select", optionElement, undefined, "border:1px solid #0054A3", "name='filter("+ nAtt.attribute.attributeID +")' id='filter-"+ nAtt.attribute.attributeID +"' onchange='javascript:filteronly();'");
	                tdElement += writeElement("td", selectElement);
	                trElement += writeElement("tr", tdElement);
	            //}
				optionElement="";
                
            }
        }
    }
    
    tableElement = writeElement("table", trElement, "filters", undefined, "id='filters'");
    if ($('filterContainer')) {
		$('filterContainer').innerHTML = tableElement;
	}
}

function writeProducts(groupID, theJSON, productTableBody) {
	var pagge = theJSON.paging.page;
	if(pagge == undefined){
		pagge = 1;
	}
	
	var bovenGrens = theJSON.paging.pagesize * pagge;
	var onderGrens= bovenGrens - theJSON.paging.pagesize;
	
	var products = theJSON.products;
	var result = "";
	result += writeHeader(theJSON);
	for (i = 0; i <= bovenGrens; i++) {
		if(products[i] != undefined){
				result += writeProduct(theJSON, products[i], i);
		}
	}
	var tempTable = writeElement("table", result, "order_products", undefined, "id=\"products\"");
	$('tablecontainer').innerHTML = tempTable;
}

function writeHeader(theJson){
    var dealers = (theJson.dealerCount > 0);
	var photo = theJson.paging.photolist;
	var columns = "";
	var sortOrder = theJson.sorting.sortNewOrder;
	var sortColumn = theJson.sorting.sortColumn;
	var element ='';
	if(sortOrder == 1){
	  element += writeElement("img", undefined, undefined, undefined, "src='images/layout/arrow-up.gif' alt='Oplopend'"); 
	}else{
	  element += writeElement("img", undefined, undefined, undefined, "src='images/layout/arrow-down.gif' alt='Aflopend'"); 
	}
	
	columns += writeElement("th", undefined);
	if (photo == "photo"){
	    columns += writeElement("th", undefined, undefined, "width='10%';");
	    columns += writeElement("th", undefined, "border");
	}		 
	columns += writeElement("th", ("Artikelnr.") + (sortColumn == "ProductID" ? element : ""), undefined, undefined, "colspan=\"2\" onclick='sorting(\"ProductID\",\""+ sortOrder +"\",\"myDataForm\")' onmouseover=\"changeCursor(this,'pointer')\" onmouseout=\"changeCursor(this,'default')\" ");
	columns += writeElement("th", undefined, "border");
	columns += writeElement("th", ("Omschrijving") + (sortColumn == "Description" ? element : ""), undefined, "text-align:left", "colspan=\"3\" onclick='sorting(\"Description\","+ sortOrder +",\"myDataForm\")' onmouseover=\"changeCursor(this,'pointer')\" onmouseout=\"changeCursor(this,'default')\" "); 
	columns += writeElement("th", undefined, "border");
	columns += writeElement("th", ("Specificatie") + (sortColumn == "Specification" ? element : ""), undefined, "text-align:left", "colspan=\"2\" onclick='sorting(\"Specification\","+ sortOrder +",\"myDataForm\")' onmouseover=\"changeCursor(this,'pointer')\" onmouseout=\"changeCursor(this,'default')\" "); 
	columns += writeElement("th", undefined, "border");
	columns += writeElement("th", ("Eenheid") + (sortColumn == "Unit" ? element : ""), undefined, "text-align:left", "onclick='sorting(\"Unit\","+ sortOrder +",\"myDataForm\")' onmouseover=\"changeCursor(this,'pointer')\" onmouseout=\"changeCursor(this,'default')\" "); 
	columns += writeElement("th", undefined, "border");
	columns += writeElement("th", ("Bruto excl.") + (sortColumn == "Price" ? element : ""), undefined, "text-align:right", "onclick='sorting(\"Price\",\""+ sortOrder +"\",\"myDataForm\")' onmouseover=\"changeCursor(this,'pointer')\" onmouseout=\"changeCursor(this,'default')\" "); 
	columns += writeElement("th", undefined, "border");
	columns += writeElement("th", ("Korting") + (sortColumn == "Discount" ? element : ""), undefined, "text-align:left", "onclick='sorting(\"Discount\","+ sortOrder +",\"myDataForm\")' onmouseover=\"changeCursor(this,'pointer')\" onmouseout=\"changeCursor(this,'default')\" ");
	if (  theJson.orderfast == true ) {
		columns += writeElement("th", undefined, "border");
		columns += writeElement("th", ("Bestel snel") , undefined, "text-align:left","colspan=\"1\"");
	}

	return writeElement("tr", columns);
}

function writeProduct(theJSON, product, index) {
	var groupID = theJSON.groupID;
	var dealersPresent = (theJSON.dealerCount > 0);
	var photoList = theJSON.paging.photolist;
	var columns = "";
		columns += writeElement("td", "<input type='checkbox' id='c" + index + "' name='productID' value='" + product.productID + "' onclick=\"checkCheckBox(" + index + ", 'message')\" style='border:none;'/>", "border3", "padding-right: 2px; padding-left: 5px; vertical-align: middle");
	if (photoList == "photo"){
	    columns += writeElement("td", "<img id='foto" + product.productID + "' src=\"static/images/product/small/" + product.thumbnail + "\" border='0' width='50' height='50' alt='" + product.description + " " + product.specification + "' title=''/>", 'border3', undefined, "onclick=\"gotoPage('" + product.url + "')\"");
	    columns += writeElement("td", undefined, "border3");
	}		    
	columns += writeElement("td", product.productID, undefined, undefined, "colspan=\"2\" onclick=\"gotoPage('" + product.url + "')\"");
	columns += writeElement("td", undefined, "border3");
	if(product.discription != NaN){
	columns += writeElement("td", product.description + (product.showPriceOnRequestText != null ? '<br/><span style="color: red">' + product.showPriceOnRequestText + '</span>' : ""), undefined, "text-align:left", "colspan=\"3\" onclick=\"gotoPage('" + product.url + "')\"");	
	}else{
	columns += writeElement("td", "$nbsp;", undefined, "text-align:left", "colspan=\"3\" onclick=\"gotoPage('" + product.url + "')\"");
	}
	columns += writeElement("td", undefined, "border3");
	if( product.specification != NaN){
	columns += writeElement("td", product.specification, undefined, "text-align:left", "colspan=\"2\" onclick=\"gotoPage('" + product.url + "')\"");
	}else{
	columns += writeElement("td", "$nbsp;", undefined, "text-align:left", "colspan=\"2\" onclick=\"gotoPage('" + product.url + "')\"");
	}
	columns += writeElement("td", undefined, "border3");
	columns += writeElement("td", product.unit, undefined, "text-align:left onclick=\"gotoPage('" + product.url + "')\"");
	columns += writeElement("td", undefined, "border3");
	
	if (product.showPrice == "False") {
		columns += writeElement("td", product.grossPrice, undefined, "text-align:right; padding-right: 1em; color: #999; font-weight:bold", " title='De prijs van dit product is op aanvraag' colspan='5' onclick=\"gotoPage('" + product.url + "')\"");
	} else {
		columns += writeElement("td", product.grossPrice, undefined, "text-align:right", "onclick=\"gotoPage('" + product.url + "')\"");
		columns += writeElement("td", undefined, "border3");
		columns += writeElement("td", product.discount, undefined, "text-align:right; padding-right:1em;", "onclick=\"gotoPage('" + product.url + "')\"");
		if (  theJSON.orderfast == true ) {
			columns += writeElement("td", undefined, "border3");
			columns += writeElement("td", "<input type='text' onchange='dirty = false' style='text-align: center;width: 52px' name='amount" + product.productID +"'>", "border3", "vertical-align: middle");
		}
	}

	return writeElement("tr", columns, "product", undefined, "id='t" + index + "' onmouseover=\"changeBackgroundColor(this,'#C7E3F8');changeCursor(this,'pointer')\" onmouseout=\"changeBackgroundColor(this,'transparent');changeCursor(this,'default')\"");
	}

function writeElement(elementName, value, clazz, style, attributes) {
	var result = "<" + elementName;
	result += (clazz ? " class='" + clazz + "'" : "");
	result += (style ? " style='" + style + "'" : "");
	result += (attributes ? " " + attributes : "");
	result += ">" + (value ? value : "") + "</" + elementName + ">";
	return result;
}

function writeBottomTable(theJSON, productTable, styleItems, sequence){
    var bottomTable = document.getElementById('bottomTable');
    var rijen =elementA=elementB=columns='';
    var page = theJSON.paging.page;
    var photo =theJSON.paging.photolist;
    var pagesize = theJSON.paging.pagesize;
    var originalPageSize = theJSON.paging.originalPagesize;
    
   var elementA = "";
   var elementB = "";
   var columns ="";
    columns += writeElement('td', '&nbsp;');
   var rijen = "";
    elementB = "";
    elementB += writePaging(theJSON);
    elementB += writePagesize(photo, originalPageSize);
    columns = writeElement('td', elementB, 'paging');
    elementA = writeElement('font', 'weergave', undefined, 'text-align: right;');
    elementA += writeElement('input', undefined, undefined, 'border: none; vertical-align: middle;', (photo == 'photo' ? 'checked = "checked"' : '') + ('id="photo" value="photo" type="radio" name="photolistController' + sequence + '" onclick="updatePhotoList(this.value);postForm();return false;"'));
    elementA += writeElement('label', 'foto', undefined, undefined, 'for="photo"');
    elementA += writeElement('input', undefined, undefined, 'border: none; vertical-align: middle;', (photo == 'photo' ? '' : 'checked = "checked"') +  ('id="nophoto" value="nophoto" type="radio" name="photolistController' + sequence + '" onclick="updatePhotoList(this.value);postForm();return false;"'));
    elementA += writeElement('label', 'lijst', undefined, undefined, 'for="nophoto"');
    elementB = writeElement('span', elementA, 'page', 'padding-left: 5px;');
    columns += writeElement('td', elementB, 'paging', 'text-align: right;');
    rijen += writeElement('tr', columns);
    return writeElement("table", rijen, "buttonlinks", styleItems, "id=\"bottomTable\"");
}

function writePaging(theJSON){
	var isLastPage = theJSON.paging.isLastPage;
	var isFirstPage =theJSON.paging.isFirstPage;
	var page = theJSON.paging.page;
	var lastPage = theJSON.paging.lastPage;
	var pages = theJSON.paging.pages;
	var element ='';
	var result = '';
	if(page != undefined) {
	
	    if(!isFirstPage){
	       element = writeElement('img', undefined, undefined, "vertical-align: middle;", "alt='pijl links' src='images/RightCorner.gif'")
	       result += writeElement('a', element, undefined, 'color: #024E9A; font-style: italic;', "href='#nogo' onclick='fixPaging("+(page-1)+", \"myDataForm\");return false;'");
	    }
	    
	    if(lastPage >= 11){
	        if (page >= 7) {
	            if(page == 7) {
	                result += writeElement('a', ' 1', undefined, 'color: #024E9A; font-style: italic;', "href='#nogo' onclick='fixPaging(4, \"myDataForm\");return false;'");
	                page = 8
	            }else {
	                result += writeElement('a',' 1...' , undefined, 'color: #024E9A; font-style: italic;', "href='#nogo' onclick='postForm();return false;'");
	            }
	        }
		}
		
	    for(var i=0; i < pages.length;i++) {
	        if(pages[i] != 0){
	            if(page != pages[i]){
	                element = writeElement('a', pages[i] + ' ' , undefined, 'color: #024E9A; font-style: italic;', "href='#nogo' onclick='fixPaging(" + pages[i] + ", \"myDataForm\");return false;'");
	                result += writeElement('span', element, 'page');
	            }
	            if(page == pages[i]){
	                result += writeElement('span', pages[i] + ' ', 'chosenPage');
	            }
	        }
	    }
	    
	    if(lastPage >= 11){
	      if(page <= (lastPage - 6)){
	        if(page != (lastPage - 6)){
	            result += writeElement('a', "... ", undefined, 'color: #024E9A; font-style: italic;',"href='#nogo' onclick='fixPaging("+page+", \"myDataForm\");return false;'");
	        }else{
	            result += writeElement('a', lastPage , undefined, 'color: #024E9A; font-style: italic;', "href='#nogo' onclick='fixPaging("+lastPage+", \"myDataForm\");return false;'");
	        }
	      }
	  }
	  
	  if(!isLastPage) {
	    element = writeElement('img', undefined, undefined, "vertical-align: middle;", "alt='pijl rechts' src='images/LeftCorner.gif'")
	    result += writeElement('a', element, undefined, 'color: #024E9A; font-style: italic;', "href='#nogo' onclick='fixPaging("+(page + 1)+", \"myDataForm\");return false;'");
	  }
	}
	return result;
}

function writePagesize(photo, pagesize){
	var elementA ="";
	var result= "";
	result += writeElement('span', 'toon ', 'page', 'padding-left: 15px;');
	elementA += writeElement('option', '25', undefined, undefined, 'value="25"' +(pagesize == '25' ? ' selected="selected"': ''));    
	elementA += writeElement('option', '50', undefined, undefined, 'value="50"' +(pagesize == '50' ? ' selected="selected"': ''));    
	elementA += writeElement('option', '75', undefined, undefined, 'value="75"' +(pagesize == '75' ? ' selected="selected"': ''));    	    
	elementA += writeElement('option', '100', undefined, undefined, 'value="100"' +(pagesize == '100' ? ' selected="selected"': ''));    
	elementA += writeElement('option', '150', undefined, undefined, 'value="150"' +(pagesize == '150' ? ' selected="selected"': ''));        
	elementA += writeElement('option', 'alle', undefined, undefined, 'value="1500"' +(pagesize == '1500' ? ' selected="selected"': ''));
	result += writeElement('select', elementA, undefined, 'vertical-align:middle;', 'name="pagesizeController" onchange="updatePagesize(this.value);postForm();return false;"');
	result += writeElement('span', 'producten per keer', 'page', 'padding-left: 5px;');
	
	return result;
}

function ieTableInnerHTML(target, newTable, divId) {
	var theDiv = $(divId);
	if(target != null){
	Element.remove(target);
	}
	theDiv.innerHTML = newTable;
}	


function updateFailed() {
	// do nothing
}


/********** Product page **********/

function keycheck(obj){
	var nummer = document.onkeyup = alertkey;
	alert(nummer);
	}

	function alertkey(e, obj) {
	  if( !e ) {
	    if( window.event ) {
	      e = window.event;
	    } else {
	      return;
	    }
	  }
	  
	  if( typeof( e.keyCode ) == 'number'  ) {
	    //DOM
	    e = e.keyCode;
	  } else if( typeof( e.which ) == 'number' ) {
	    //NS 4 compatible
	    e = e.which;
	  } else if( typeof( e.charCode ) == 'number'  ) {
	    //also NS 6+, Mozilla 0.9+
	    e = e.charCode;
	  } else {
	    //total failure, we have no way of obtaining the key code
	    return;
	  }
	  
	  
	  if(e == 38 || e == 40){
	    if(e == 38){
	        var up = parseInt(obj.id.substring(1,(obj.id.length)));
	        for(var i=0; i < 10; i++){
	            up -= 1;
	            var box =document.getElementById('a' + up);
	            if(box != null){
	                box.focus();
	                i = 11;
	            }
	        }
	    } else if(e == 40){
	        var down = parseInt(obj.id.substring(1,(obj.id.length)));
	        for(var i=0; i<10; i++){
	            down += 1;
	            var box2 =document.getElementById('a' + down);
	            if(box2 != null){
	                box2.focus();
	                i=11;
	           }
	        }
	    } 
	  }
	}
	
/********* Home ***************/
	function showQuestion(transport) {
		var theJSON = eval('('+transport.responseText+')');
		if (!theJSON.poll) {
			var doneDiv = document.createElement('div');
			doneDiv.appendChild(document.createTextNode("U heeft alle beschikbare vragen al beantwoord. Bedankt voor uw medewerking."));
			$('pollContainer').innerHTML = "";
			$('pollContainer').appendChild(doneDiv);
			return;
		}
		
		// Question
		var questionDiv = document.createElement('div');
		questionDiv.className = 'question';
		questionDiv.appendChild(document.createTextNode(theJSON.poll.question));
		
		// Answers
		var answerContainer = document.createElement('div');
		answerContainer.id = 'pollAnswers';
		var answers = $A(theJSON.poll.answers);
		answers.each(function (e) { createAnswer(answerContainer, e) });
		
		// Other question
		var backDiv = document.createElement('div');
		backDiv.onclick = getQuestion;
		backDiv.className = 'button other';
		backDiv.appendChild(document.createTextNode('Andere vraag'));		
		
		$('pollForm').questionID.value = theJSON.poll.pollID;
		$('pollContainer').innerHTML = "";
		$('pollContainer').appendChild(questionDiv);
		$('pollContainer').appendChild(answerContainer);
	//		$('pollContainer').appendChild(backDiv);
	}
	
	function createAnswer(answerContainer, answer) {
		var divElement = document.createElement('div');
		divElement.className = 'answer';
		
		// Radio button
		// IE6 doesn't allow the name of radio buttons to be set through the DOM
		var radioElement = Try.these (
			function() { 
				var result = document.createElement('<input name="pollAnswer">'); 
				if (result == undefined) throw "bad IE6!"; return result;
			},
			function() { 
				var result = document.createElement('input');
				result.name = "pollAnswer";
				return result;
			}
		);
		radioElement.type = 'radio';
		radioElement.value = answer.answerID;
		radioElement.id = 'answer' + answer.answerID;
		radioElement.onclick = submitAnswer;
		
		// Label
		var labelElement = document.createElement('label');
		labelElement.setAttribute('for', radioElement.id);
		labelElement.appendChild(document.createTextNode(answer.answer));
		
		divElement.appendChild(radioElement);
		divElement.appendChild(labelElement);
		answerContainer.appendChild(divElement);
	}
	
	function submitAnswer() {
		pageTracker._trackPageview('/poll/vraag-' + $('pollForm').questionID);
		
		new Ajax.Request('pollSubmit.do', {
			parameters: $('pollForm').serialize(true),
		    // onSuccess: showResult,
		    onSuccess: getQuestion,
		    onFailure: updateFailed
		});
	}	
	
	function createAnswerResult(answerContainer, answer) {
		var divElement = document.createElement('div');
		divElement.className = 'answer';
		divElement.appendChild(document.createTextNode(answer.votesPercentage + " " ));		
		divElement.appendChild(document.createTextNode(answer.answer.answer));
		answerContainer.appendChild(divElement);
	}	

	function showResult(transport) {
		var theJSON = eval('('+transport.responseText+')');
		$('pollContainer').innerHTML = "";
		
		// Question
		var questionDiv = document.createElement('div');
		questionDiv.className = 'question';
		questionDiv.appendChild(document.createTextNode(theJSON.pollResult.question.question));

		// Answers
		var answerContainer = document.createElement('div');
		answerContainer.id = 'pollAnswers';
		var answers = $A(theJSON.pollResult.answers);
		answers.each(function (e){ createAnswerResult(answerContainer, e) });
		
		// Total votes
		var totalVotesDiv = document.createElement('div');
		totalVotesDiv.className = 'total';
		totalVotesDiv.appendChild(document.createTextNode("Stemmen: " + theJSON.pollResult.totalVotes));
		
		// Other question
		var backDiv = document.createElement('div');
		backDiv.onclick = getQuestion;
		backDiv.className = 'button other';
		backDiv.appendChild(document.createTextNode('Andere vraag'));
		
		$('pollContainer').appendChild(questionDiv);
		$('pollContainer').appendChild(answerContainer);
		$('pollContainer').appendChild(totalVotesDiv);
		$('pollContainer').appendChild(backDiv);
	}
