	function hide_all() {
	
		// we need to hide all printer pages
		
		for( i in total ) {

			eval_fix( "fo.getElementById('" + i + "').style.display = 'none';" );
		
		}
		
		// hide all other pages
		
		fo.getElementById("page").style.display = "none";
		fo.getElementById("page2").style.display = "none";
		fo.getElementById("selection").style.display = "none";
		fo.getElementById("start").style.display = "none";
		fo.getElementById("previous").style.display = "none";
		fo.getElementById("right").style.display = "none";
		fo.getElementById("reset").style.display = "none";
		fo.getElementById("printer_page").style.display = "none";
	
	}
	
	function check_status() {
	
		// when user first enters this page we want to see what they
		//previously entered and display it in the selection box
	
		var cookies = Array();
		var complete = false;
			
		cookies = read_cookie( 1 );
		
		var p = 0;
		var questions = Array();
		
		// loop through cookie data
		for( var i=0; i<cookies.length; i++ ) {
		
			// we only want the cookie associated with pages
			if( cookies[i].substring(0,5) == "page" + p ) {
			 
				// add answers to our questions array
				questions[p] = cookies[i].substring(6);
				
				p++;
								
			}
		
		}
		
		// now if we have multiple answers, we need to seperate them
		var answers = Array();
		
		for( var i=0; i<questions.length; i++ ) {
		
			var sel='';
			answers = questions[i].split("&");
			
			for( var p=0; p<answers.length; p++ ) {
			
				if( p == 0 ) {
					
					sel = eval_fix( "p" + i + "_q" + answers[p] + "_title;" );
				
				} else {
				
					sel += "," + eval_fix( "p" + i + "_q" + answers[p] + "_title;" );
				
				}
				
			}
			
			if( sel == "" ) {
			
				sel = "No Selection";
				
			}
			
			// add out text to the selection box
			fo.getElementById("sel" + i ).innerHTML = sel;
			sel="";
			
		}

		cookies = document.cookie.split(";");
		for( var i=0; i<cookies.length; i++ ) {
		
			if( trim( cookies[i].substring( 0,12 ) ) == "FINISH=TRUE" ) {
			
				// user has already completed this questionarre 
				// so we need to send them to the printer page
				step = total_pages;
				startHere(step);
				
			}
		
		}
		
	
	}
	
	function reset() {
	
		step = 0;
		
		var date = new Date();
		date.setTime( date.getTime() + ( -1*24*60*60*1000 ) );
		var expires = "; expires=" + date.toGMTString();
		var cookie_value = "";
		
		for( var i=0; i<total_pages; i++ ) {
		
			// delete the cookie
			fo.cookie = "page" + i + "=" + cookie_value + expires;
			
			// clear the selection box
			fo.getElementById("sel" + i ).innerHTML = "No Selection";
		
		}
		
		// delete the cookie
		fo.cookie = "FINISH=" + cookie_value + expires;
		
		startHere( step );
	
	}
	
	function startHere( p ) {
	
		step = p;
	
		hide_all();
	
		// display start page
		if( step == -1 ) {

			fo.getElementById("start").style.display = "block";
			fo.getElementById("right").style.display = "block";
		
		// display results page
		} else if( step == total_pages ) {
		
			var date = new Date();
			date.setTime( date.getTime() + ( 31*24*60*60*1000 ) );
			var expires = "; expires=" + date.toGMTString();
		
			// create cookie
			fo.cookie = "FINISH=TRUE" + expires;
		
			fo.getElementById("page2").style.display = "block";
			fo.getElementById("selection").style.display = "block";
			fo.getElementById("printer_page").style.display = "block";
			
			fo.getElementById("previous").style.display = "block";
			fo.getElementById("reset").style.display = "block";
			
			display_results();
		
		// show next set of questions
		} else {
		
			fo.getElementById("page").style.display = "block";
			fo.getElementById("selection").style.display = "block";

			fo.getElementById("previous").style.display = "block";
			fo.getElementById("right").style.display = "block";
			fo.getElementById("reset").style.display = "block";
			
			//set our variables
			el = fo.forms[0].elements;
			fetch_vars();
			fetch_saved_answers();
			add_selection();
			
		}
			
	}
	
	function newPage( d ) {
	
		if( step == -1 ) {
			
			step = 0;

			// send user
			startHere( step );
			
		} else {

			if( d == 0 ) {
	
				step--;
			
				// send user
				startHere( step );
				
			} else {
						
				var ok = validate();

				// did a user select an answer?
				if( ok ) {
				
					// let's make sure there's a next page!
					if( step < total_pages+1 ) {
			
						step++;
						
					}
	
					// send user
					startHere( step );
					
				} else {
				
					// display error message
					alert( "Please select an answer" );
					
				}
				
			}
			
		}
		
		// send user to the top of the page
		fo.getElementById("top").focus();
	
	}
	
	function imageswap( i, checked ) {
	
		if( checked == true ) {
	
			//image swap
			eval( "fo.getElementById('question" + i + "_select').innerHTML = '<a href=\"Javascript:clicked(" + i + ",false);\">Deselect</a>&nbsp;&nbsp;<input type=\"checkbox\" name=\"q" + i + "\" value=\"" + i + "\" onClick=\"clicked(" + i + ",true)\" />';" );
			
		} else {
		
			//image swap
			eval( "fo.getElementById('question" + i + "_select').innerHTML = '<a href=\"Javascript:clicked(" + i + ",false);\">Select</a>&nbsp;&nbsp;<input type=\"checkbox\" name=\"q" + i + "\" value=\"" + i + "\" onClick=\"clicked(" + i + ",true)\" />';" );
		
		}
	
	}
	
	function create_cookie( values ) {
	
		var cookie_value="";
	
		for( var i=0; i<values.length; i++ ) {
				
			if( i == 0 ) {
					
				cookie_value += values[i];
				
			} else {
			
				cookie_value += "&" + values[i];
				
			}
			
		}
		
		var date = new Date();
		date.setTime( date.getTime() + ( 31*24*60*60*1000 ) );
		var expires = "; expires=" + date.toGMTString();
		
		// create cookie
		fo.cookie = "page" + step + "=" + cookie_value + expires;
	
	}
	
	function add_selection() {
	
		// fetch values from array
		var sel="";
		var p=0;
		var cookies = Array();
		
		// loop through all elements
		for( var i=0; i<el.length; i++ ) {
		
			// if this element has been checked then we 
			// want to add it to the selection box
			if( el[i].checked == true ) {
		
				if( p == 0 ) {
				
					sel = fo.getElementById('question' + i + '_title').innerHTML;
					
					cookies[p] = i;
					
					p++;
				
				} else {
				
					sel += "," + fo.getElementById('question' + i + '_title').innerHTML;
					
					cookies[p] = i;
					
					p++;
				
				}
				
			}
		
		}
		
		if( sel == "" ) {
		
			sel = "No Selection";
			
		}
		
		// since the user selected these question(s) we will store
		// the question number(s) in a cookie
		create_cookie( cookies );
		
		// add out text to the selection box
		fo.getElementById("sel" + step ).innerHTML = sel;
	
	}
	
	function clicked( value, clicked ) {
	
		var selection = Array();
		var p = 0;
		
		if( !multi ) {
		
			// this means that only one answer is allowed so we need
			// to scan through every element and un-check them and only check
			// the one that was ticked! 

			for( var i=0; i<el.length; i++ ) {
					
				if( i != value ) {
				
					// image swap
					imageswap( i, false );
					
					// let's un-check the box the user did not tick!
					el[i].checked=false;
					
				}
				
			}
			
			//alert(clicked);
			//alert(el[value].checked);
			
			if( !clicked && !el[value].checked ) {
			
				// image swap
				imageswap( value, true );
					
				// let's check the box the user as ticked!
				el[value].checked=true;
			
			} else if( !clicked && el[value].checked ) {
			
				// image swap
				imageswap( value, false );
					
				// let's check the box the user as ticked!
				el[value].checked=false;
			
			} else if( clicked && el[value].checked ) {
				
				//image swap
				eval( "fo.getElementById('question" + value + "_select').innerHTML = '<a href=\"Javascript:clicked(" + value + ",false);\">Deselect</a>&nbsp;&nbsp;<input type=\"checkbox\" checked=\"checked\" name=\"q" + value + "\" value=\"" + value + "\" onClick=\"clicked(" + value + ",true)\" />';" );
			
			} else if( clicked && !el[value].checked ) {
			
				//image swap
				eval( "fo.getElementById('question" + value + "_select').innerHTML = '<a href=\"Javascript:clicked(" + value + ",false);\">Select</a>&nbsp;&nbsp;<input type=\"checkbox\" name=\"q" + value + "\" value=\"" + value + "\" onClick=\"clicked(" + value + ",true)\" />';" );
			
			}
			
					
		} else {
		
			// this means that more than one answer is allowed so we need
			// to check the one that was ticked! 
		
			// element was already checked so we will un-check it
			if( el[value].checked == true ) {
		
				if( !clicked ) {
				
					// image swap
					imageswap( value, false );
		
					// let's check the box the user as clicked!
					el[value].checked=false;
					
				} else {
				
					// image swap
					imageswap( value, true );
				
					// let's check the box the user as clicked!
					el[value].checked=true;
					
				
				}
			
			// element wasn't checked so we will check it
			} else {
			

				if( !clicked ) {
				
					// image swap
					imageswap( value, true );

					// let's check the box the user as clicked!
					el[value].checked=true;
					
				} else {
				
					 // image swap
					imageswap( value, false );
				
					// let's check the box the user as clicked!
					el[value].checked=false;
				
				}
				
			}
			
		}
		
		// add selection to selection box
		add_selection();
			
	}
	
	function trim( str ) {
	
  		return str.replace(/^\s*|\s*$/g,"");
		
	}
	
	function read_cookie( mass ) {
	
		var values = Array();
		var cookies = Array();
		
		values = fo.cookie.split( ";" );
		
		// loop through each cookie
		for( var i=0; i<values.length; i++ ) {
		
			// add cookie and it's data to our cookie array
			cookies[i] = trim( values[i] );
		
		}
		
		if( mass ) {
		
			return cookies;
			
		}
		
		var p = 0;
		var questions = Array();
		
		// loop through cookie data
		for( var i=0; i<cookies.length; i++ ) {
		
			// we only want the cookie associated with this page
			if( cookies[i].substring(0,5) == "page" + step ) {
			 
				// add answers to our questions array
				questions[p] = cookies[i].substring(6);
				
				p++;
								
			}
		
		}
		
		// now if we have multiple answers, we need to seperate them
		var answers = Array();
		
		for( var i=0; i<questions.length; i++ ) {
		
			answers = questions[i].split("&");
		
		}
		
		return answers;
	
	}
	
	function eval_fix( varname )  {
	
 		try {
				
			return eval( varname );
			
		} catch( e ) {
      
	  		return "";
		
		}
		
	}
	
	function fetch_saved_answers() {
	
		var answers = Array();
		answers = read_cookie();
		
		if( answers.length > 0 ) {
		
			// check all selected answers
			for( var i=0; i<answers.length; i++ ) {
			
				imageswap( answers[i], true );
				el[answers[i]].checked = true;
			
			}
			
		}
	
	}
	
	
	
	function validate() {
	
		var ok = false;
		var total = eval( "p" + step + "_total;" );
	
		for( var i=0; i<total; i++ ) {
		
			if( el[i].checked == true ) {
			
				ok = true;
				
			}
			
		}
		
		if( !ok ) {
		
			return false;
			
		} else {
		
			return true;
			
		} 
	
	}
	
	function fetch_vars() {
		
		// clear old data first!
		for( var i=0; i<4; i++ ) {
		
			eval( "fo.getElementById('question" + i + "_title').innerHTML = '';" );
			eval( "fo.getElementById('question" + i + "_subtitle').innerHTML = '';" );
			eval( "fo.getElementById('question" + i + "_image').innerHTML = '';" );
			eval( "fo.getElementById('question" + i + "_select').innerHTML = '';" );
			
		}
		
		multi = eval( "p" + step + "_multi;" );
		var title = eval( "p" + step + "_title;" );
		var sub_title = eval( "p" + step + "_subtitle;" );
		
		var ok0 = false;
		var ok1 = false;
		var ok2 = false;
		var ok3 = false;
		
		
		if( eval_fix( "p" + step + "_q0_title;" ) ) {
		
			var question0_title = eval( "p" + step + "_q0_title;" );
			var question0_subtitle = eval( "p" + step + "_q0_subtitle;" );
			var ok0 = true;
			
		}
		
		if( eval_fix( "p" + step + "_q1_title;" ) ) {
		
			var question1_title = eval( "p" + step + "_q1_title;" );
			var question1_subtitle = eval( "p" + step + "_q1_subtitle;" );
			var ok1 = true;
			
		}
		
		if( eval_fix( "p" + step + "_q2_title;" ) ) {
		
			var question2_title = eval( "p" + step + "_q2_title;" );
			var question2_subtitle = eval( "p" + step + "_q2_subtitle;" );
			var ok2 = true;
		
		}
		
		if( eval_fix( "p" + step + "_q3_title;" ) ) {
		
			var question3_title = eval( "p" + step + "_q3_title;" );
			var question3_subtitle = eval( "p" + step + "_q3_subtitle;" );
			var ok3 = true;
			
		}

		fo.getElementById('title').innerHTML = title
		fo.getElementById('subtitle').innerHTML = sub_title;
		
		if( ok0 ) {
		
			// Display question 1
			fo.getElementById('question0_title').innerHTML = question0_title
			fo.getElementById('question0_subtitle').innerHTML = question0_subtitle;
			
			if( eval_fix( "p" + step + "_q0_image;" ) ) {
			
				fo.getElementById('question0_image').innerHTML = "<img src=\"printerSelector/images/p" +  step + "_q0.jpg\" alt=\"ALT HERE!\" />";
				
			}
			
			fo.getElementById('question0_select').innerHTML = "<a href=\"Javascript:clicked(0, false);\">Select</a>&nbsp;&nbsp;<input type=\"checkbox\" name=\"q0\" value=\"0\" onclick=\"clicked(0,true)\" />";
			
		}
		
		if( ok1 ) {
		
			// Display question 2
			fo.getElementById('question1_title').innerHTML = question1_title
			fo.getElementById('question1_subtitle').innerHTML = question1_subtitle;
			
			if( eval_fix( "p" + step + "_q1_image;" ) ) {
			
				fo.getElementById('question1_image').innerHTML = "<img src=\"printerSelector/images/p" + step + "_q1.jpg\" alt=\"ALT HERE!\" />";
					
			}
			
			fo.getElementById('question1_select').innerHTML = "<a href=\"Javascript:clicked(1,false);\">Select</a>&nbsp;&nbsp;<input type=\"checkbox\" name=\"q1\" value=\"1\" onclick=\"clicked(1,true)\" />";
			
		}
		
		if( ok2 ) {
		
			// Display question 3
			fo.getElementById('question2_title').innerHTML = question2_title
			fo.getElementById('question2_subtitle').innerHTML = question2_subtitle;
			
			if( eval_fix( "p" + step + "_q2_image;" ) ) {
			
				fo.getElementById('question2_image').innerHTML = "<img src=\"printerSelector/images/p" + step + "_q2.jpg\" alt=\"ALT HERE!\" />";
				
			}
			
			fo.getElementById('question2_select').innerHTML = "<a href=\"Javascript:clicked(2, false);\">Select</a>&nbsp;&nbsp;<input type=\"checkbox\" name=\"q2\" value=\"2\" onclick=\"clicked(2, true)\" />";
			
		}
		
		if( ok3 ) {
		
			// Display question 4
			fo.getElementById('question3_title').innerHTML = question3_title
			fo.getElementById('question3_subtitle').innerHTML = question3_subtitle;
			
			if( eval_fix( "p" + step + "_q3_image;" ) ) {
			
				fo.getElementById('question3_image').innerHTML = "<img src=\"printerSelector/images/p" + step  + "_q3.jpg\" alt=\"ALT HERE!\" />";
				
			}
			
			fo.getElementById('question3_select').innerHTML = "<a href=\"Javascript:clicked(3, false);\">Select</a>&nbsp;&nbsp;<input type=\"checkbox\" name=\"q3\" value=\"3\" onclick=\"clicked(3, true)\" />";
			
		}
	
	}
	
	function sortNumber( a, b ) {

		return a - b;
		
	}
	
	function display_results() {
	
		var cookies = Array();
			
		cookies = read_cookie( 1 );
		
		var p = 0;
		var questions = Array();

		// loop through cookie data
		for( var i=0; i<cookies.length; i++ ) {
		
			// we only want the cookie associated with pages
			if( cookies[i].substring(0,5) == "page" + p ) {
			 
				// add answers to our questions array
				questions[p] = cookies[i].substring(6);
	//			alert(cookies[i].substring(6));
				p++;
								
			}
		
		}
		
		// now if we have multiple answers, we need to seperate them
		var answers = Array();
		var total = Array();
		// default values
		total['opera'] = 0;
		total['tempo'] = 0;
		total['avalon'] = 0;
		total['avalon_duo'] = 0;
		total['alto'] = 0;
		total['rio_2e'] = 0;
		total['tango_2e'] = 0;
		total['tango_L'] = 0;
		total['prima_2e'] = 0;
		
		for( var i=0; i<questions.length; i++ ) {
//			alert (questions[i]);
			var sel='';
			answers = questions[i].split("&");
			for( var p=0; p<answers.length; p++ ) {
				var q=0;
				q=answers[p];
				// scan through the values and add them
				total['opera'] += algo[i][q]['opera'];
//				alert (algo[i][q]['opera']);
				total['tempo'] += algo[i][q]['tempo'];
				total['avalon'] += algo[i][q]['avalon'];
				total['avalon_duo'] += algo[i][q]['avalon_duo'];
				total['alto'] += algo[i][q]['alto'];
				total['rio_2e'] += algo[i][q]['rio_2e'];
				total['tango_2e'] += algo[i][q]['tango_2e'];
				total['tango_L'] += algo[i][q]['tango_L'];
				total['prima_2e'] += algo[i][q]['prima_2e'];		
			}
			
		
		}
//		alert (total['opera']);
		var bestPrinter = new Array();
		var i = 0;
		
		// since associated arrays in Javascript are not really arrays
		// we need to take the data and place it in a real array so
		// we can use the sort() function
		for( x in total ) {
//		alert (total[x]);
			if( !isNaN( total[x] ) ) {
				
 				bestPrinter[i] = total[x];
//				alert ("i ... " + bestPrinter[i]);
 				i++;
				
			}
	 	
		}
		
		bestPrinter.sort( sortNumber );
		bestPrinter.reverse();
		
		var i = 0;
		var n = "";
		for( x in bestPrinter ) {
//		alert (bestPrinter[x]);
		if( !isNaN( bestPrinter[x])) {
			// let's make sure we are only displaying the appropriate amount
			if( i < printers_to_display ) {
			
				for( p in total ) {
//					alert(p +" ... " + total[p]);
					// compare our associated array with our new array
					if( total[p] == bestPrinter[x] && n != p && total[p] > 0) {
						
						n = p; // this gives us the name of the printer
						break;
					}
				
				}
			
				// display printer
//				alert(n);
				eval_fix( "fo.getElementById('" + n + "').style.display='block';" );
				
				i++;
				
			}
		}
		
		}
	
	}


	function getQueryVariable(variable) {

  		var query = window.location.search.substring(1);
		
  		var vars = query.split("&");
		
  		for( var i=0;i<vars.length;i++ ) {
		
			var pair = vars[i].split("=");
			
   			if ( pair[0] == variable ) {
			
      			return pair[1];
				
    		}
						
  		} 
		
		return -1;
  
	}
	
	function anc() {
	
		var an = fo.anchors;
	
		for( var i=0; i<an.length; i++ ) {
		
			an[i].href += r + "&L=" + language;
		
		}

	}

