////// global variables ///////////////
var currentID = 0;
var isLoaded = false;
var callListener = false;
var isReset = false;
var currentID;
var allQuest = new Array();
window.dhtmlHistory.create();

//////////////////////////////////////


window.onload = function() {
	initQuestion();
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(myHistoryListener);

	var currID = getStorageValue("currID");
	if(currID && currID != ""){
		currentID = parseInt(currID);
	}
	else{
		setCurrentID(0);
	}
	displayCurrentQuest();
	hideIt("busyIcon");
	showIt("nextButton");
};

var myHistoryListener = function(newLocation, historyData) {

	if(callListener){
		callListener = false;
		return;
	}

	isLoaded = true;

	if(typeof newLocation == 'undefined'){
		setCurrentID(0);
	}
	else if(newLocation == ''){
		setCurrentID(0);
	}
	else{

		var currTemp = getStorageValue("currID");
		if(parseInt(newLocation) < parseInt(currTemp)){
			doBack();
			return;
		}
		else if(parseInt(newLocation) > parseInt(currTemp)){
			showNextQuest();
		}
		else{
			currentID = currTemp;
		}
	}
	displayCurrentQuest();
}


function showNextQuest(){
	var succeedFlag = validQuestion(currentID);

	if(succeedFlag){
		hideIt("errormsg");
		var temp = currentID +1;

		if(temp>=allQuest.length){
			return checkDyZip(getQuestValue(currentID));
		}
		else{
			setCurrentID(temp);
			callListener = true;
			dhtmlHistory.add(""+currentID, currentID);
		}
	}
	else{
		showIt("errormsg");
	}
	displayCurrentQuest();
}



function showPreQuest(){
	callListener = true;
	history.back();
	doBack();
}


function doBack(){

	var currTemp = getStorageValue("currID");
	if(currTemp && currTemp!=""){
		currTemp = parseInt(currTemp) - 1;

		if(currTemp <0){
			currTemp = 0;
		}
		currentID = currTemp;
		addhistoryStorage("currID", ""+currentID);
	}

	hideIt("errormsg");
	displayCurrentQuest();
}


function validQuestion(givenid){
	if(givenid ==0){		//check subject mulit-select
		return validateSubArea(document.UserForm.SUBJECT);
	}
	else{
		return isOk(givenid);
	}
}
function isOk(givenid){
	return getQuestValue(givenid) ==""? false: true;
}

function checkDyZip(value){

	if(value.length ==5){
		if(checkzip(value) == 0){
			showIt("errormsg");
			return false;
		}
		else{
			hideIt("errormsg");
			showIt("busyIcon");
			hideIt("nextButton");
			document.UserForm.submit();
			startAnimation();

		}
	}
	hideIt("errormsg");
	return true;
}

function startAnimation(){
	var atag = document.getElementById("busyimage");
	if(atag){
		atag.src = "images/busy.gif";
	}
}

function initQuestion(){
	allQuest.length = 0;
	allQuest.push("0");
	allQuest.push("1");
	allQuest.push("2");
	allQuest.push("3");
	allQuest.push("4");
	allQuest.push("5");
	allQuest.push("6");
	allQuest.push("7");
	allQuest.push("8");
	allQuest.push("9");
}
function displayCurrentQuest(){
	var i;
	for(i = 0; i < allQuest.length; i++){
		if(i == currentID){
			showIt("b"+allQuest[i]);
			addhistoryStorage(""+i, "");
			addhistoryStorage("currID", ""+i);
			if(i==allQuest.length-1){
				document.UserForm.ZIP.focus();
			}
			continue;
		}
		hideIt("b"+allQuest[i]);
	}

	if(currentID >0 && currentID <=9){
		showIt("backButton");
	}
	else{
		hideIt("backButton");
	}
	setBarPercent();
	gotSubject(0);
}

function gotSubject(subid){

	var sel_area = document.UserForm.SUBJECT;
	for(var i = 0; i<sel_area.length; i++){
		if(sel_area[i].checked){
			if(sel_area[i].value == subid){
				return true;
			}
		}
	}
	return false;
}


///////////////////common function ////////////////////////////
function setBarPercent(){
	var value2 = parseInt(currentID*100/allQuest.length);
	var atag = document.getElementById("progBar");
	if(atag){
		atag.style.width =value2+"%";
	}
}

function validateSubArea(sel_area){
	for(var i = 0; i<sel_area.length; i++){
		if(sel_area[i].checked){
			return true;
		}
	}
	return false;
}

function setCurrentID(id){
	currentID = id;
	var strID = ""+currentID;
	addhistoryStorage("currID", strID);
}

function addhistoryStorage(key, val){
	historyStorage.put(key, val);
}
function rmAllhistoryStorage(){
	historyStorage.reset();
}
function rmhistoryStorage(key){
	if(containStorage(key)){
		historyStorage.remove(key);
	}
}
function containStorage(key){
	return historyStorage.hasKey(key);
}

function getStorageValue(key){
	return historyStorage.get(key);
}

function isQuestionInOrder(givenid){
	return currentID == givenid? true: false;
}

function isEmptyValue(givenid){
	return getQuestValue(givenid) ==""? true: false;
}

function getQuestValue(givenid){
	var atag = document.getElementById("q"+givenid);
	if(atag){
		return atag.value;
	}

	return "";
}

function changeQuestDisplay(currid, nextid){
	hideIt("b"+currid);
	showIt("b"+nextid);
	hideIt("errormsg");
	currentID = nextid;
}


function getIntValue(givenStr){
	var retid = 0;
	if(givenStr){
		retid = parseInt(givenStr);
		if( isNaN(retid)){
			retid = 0;
		}
	}
	return retid;
}




///////////// print log block ////////////////////
var linecount = 0;
function log(msg) {
}
function clearLog(msg) {
}

