var maxQuestions = 20;
var currentQuestion = 2;
var fromKeyPress=false;

document.onkeypress=detectspecialkeys

function detectspecialkeys(e){
	var evtobj=window.event? event : e
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
	//var actualkey=String.fromCharCode(unicode)
	//if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)

	if (evtobj.altKey && unicode==109){
		fromKeyPress=true;
		toggleNumberDetail(currentQuestion,8);
	}
}

function updatepage(str, location){
		if ( location == "") {
    	document.getElementById("result").innerHTML = str;
		} else {
			document.getElementById(location).innerHTML = str;
		}
}


// retrieve
function xmlhttpGet(strURL, location) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, location);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function xmlhttpGet2(strURL, location) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, location);
        }
    }
    self.xmlHttpReq.send("");
}


// this function POSTS data to the server
function xmlhttpPost(strURL, location) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, location);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function xmlhttpPost2(strURL, location, formName) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, location);
        }
    }
    self.xmlHttpReq.send(formData2QueryString(document.forms[formName]));
}


function getquerystring() {
    return formData2QueryString(document.forms['quizForm']);
}

function formData2QueryString(docForm) {

	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';

	for (i = 0; i < docForm.elements.length; i++) {

		formElem = docForm.elements[i];
		switch (formElem.type) {
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
				//alert(encodeURI(formElem.value));
				strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem.value) + '&'
				break;

			// select box has to be done differently for IE to work properly
			case 'select-one':
				//alert(formElem[formElem.selectedIndex].text);
				strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem[formElem.selectedIndex].value) + '&'
				break;

			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem.value) + '&'
				}
				break;

			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Continuing multiple, same-name checkboxes
					if (formElem.name == strLastElemName) {
						// Strip of end ampersand if there is one
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Append value as comma-delimited string
						strSubmitContent += ',' + encodeURIComponent(formElem.value);
					}
					else {
						strSubmitContent += formElem.name + '=' + encodeURIComponent(formElem.value);
					}
					strSubmitContent += '&';
					strLastElemName = formElem.name;
				}
				break;

		}
	}

	// Remove trailing separator
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	strSubmitContent += '&preview=' + encodeURIComponent("1");
	return strSubmitContent;
}


function turnONdiv(field) {
  // doesn't toggle, just turns ON a row, needs EXACT name passed
  body=document.getElementById(field);
  // To make tr tags disappear
  // we set display to none, as usual
  // to make them appear again
  // we set style to block for IE
  // but for firefox we use table-row
  try {
    body.style.display='table-row';
  } catch(e) {
    body.style.display = 'block';
  }
}

function turnOFFdiv(field) {
  // doesn't toggle, just turns OFF a row, needs EXACT name passed
  body=document.getElementById(field);
	body.style.display = 'none';
}

function toggleSingleDivv(field) {
  // toggle the display status of a single DIV
  body=document.getElementById(field);
  if (body) {
    if (body.style.display == 'none') {
    	// To make tr tags disappear
    	// we set display to none, as usual
    	// to make them appear again
    	// we set style to block for IE
    	// but for firefox we use table-row
    	try {
    	  body.style.display='table-row';
    	} catch(e) {
    	  body.style.display = 'block';
    	}
    }
    else {
    	body.style.display = 'none';
    }
  }
}
