////// global variables ///////////////
var currentID = 0;
var allQuest = new Array();
var bwsHack = false;
var isOpera = $.browser.opera;

function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			hash = encodeURIComponent(hash);
		}
		var loc = getInt(hash);
		var cur = getCurrentID();
		
		if(loc > cur){
			var curtemp = cur;
			while(curtemp < loc){
				if(buildAndValidQuest(curtemp)){
					curtemp++;
				}
				else{
					break;
				}
			}		
			
			if(curtemp < loc){
				if($.browser.msie) {
					setCurrent(1);				
				}
				else{
					setCurrent(curtemp);
				}
				return;
			}
			
		}
		if(loc >1 && (cur==0 || cur==allQuest.length)){
			if(""== $("#HIDESUB").val()){
				bwsHack = true;
			}
			populateLicense($("#HIDESUB").val());
		}
		
		if(loc == allQuest.length-1 && "" == $("#HIDESUB").val()){
			if(loc < cur){
				loc--;
			}
			else{
				loc++;
			}
		}		
		setCurrent(loc);				
	}
}
//////////////////////////////////////
$(document).ready(function() {
	/*******  Even listener *******/
	$("select").change(showNextQuest);
	initForm();
	$.historyInit(pageload, "jquery_history.html");
	if(currentID==0){
		$.historyLoad(""+1);
	}
});
function initForm(){
	allQuest.length = 0;
	$("div[id^='quest']").each(function (){
		allQuest.push(""+allQuest.length);
	});
	//licRNnum = getInt($("select[name='HASRNLICENSE']").attr("id").replace(/[^\d]/g, ""));
	//licTCnum = getInt($("select[name='HASTEACHCERT']").attr("id").replace(/[^\d]/g, ""));
	reValue();
	populateSubject("");
	changeLoadImage(false);
}
function showNextQuest(){
	if(isOpera && bwsHack){
		bwsHack = false;
		return true;
	}
	if(!validQuestion(getCurrentID(), true)){
		return true;
	}
	if(getCurrentID()+1 <= allQuest.length){
		$.historyLoad(""+(getCurrentID()+1));
	}
}
function showPreQuest(){
	history.back();
}


function buildAndValidQuest(id){
	if(id==1){
		populateSubject($("#var1").val());
	}
	else if(id==2){
		populateLicense(null);
	}
	else if(id>=allQuest.length-1){
		storeLicVal();
	}
	return validQuestion(id, false);
}

function validQuestion(id, isSub){
	var isOk = true;
	for(var i = 1; i<=id && i <= allQuest.length; i++){
		var val = $("#var"+i).val();
		if(val !=""){
			if(isSub && i==allQuest.length){
				if(!checkDyZip()){
					isOk = false;
				}
			}
		}
		else{
			isOk = false;
			break;
		}
	}
	showErr(!isOk);
	return isOk;
}
function setCurrent(val){
	if(val <1){
		val =1;
	}
	if(val <= allQuest.length){
		currentID = val;
		displayCurQuest(val);
	}
}
function displayCurQuest(id){
	if(id <= allQuest.length){
		$("div[id^='quest']").hide();
		$("div[id='quest"+id+"']").show();
		$("#var"+id).focus();
		showBackButton((id >1)? true: false);
		setBarPercent(id-1);
	}
}
function getCurrentID(){
	if(currentID == allQuest.length-2){
		if(""== $("#LicLabel").text()){
			return currentID+1;
		}
	}
	return currentID;
}

function populateLicense(subcode){
	if(subcode ==null){
		subcode = document.UserForm.SUBJECTAREA.value;
	}
	if(subcode != ""){	
		$("select[name='HASRNLICENSE']").hide();
		$("select[name='HASTEACHCERT']").hide();
		$("#LicLabel").text("");
		document.UserForm.HASRNLICENSE.length=0;
		document.UserForm.HASTEACHCERT.length=0;

		$("#HIDESUB").val(subcode);

		if(subcode == "HN06"){
			$("#LicLabel").text("Do you possess a RN License?");
			document.UserForm.HASRNLICENSE.options[0] = new Option("Yes", "Yes");
			document.UserForm.HASRNLICENSE.options[1] = new Option("No", "No");		
			var oldval = $("#HIDELICRN").val();
			if( oldval != ""){
				setFormValue("HASRNLICENSE", oldval);
			}
			$("select[name='HASRNLICENSE']").show();
		}
		else if(subcode.startsWith("ED")){
			$("#LicLabel").text("Do you possess a Teaching Certificate?");
			document.UserForm.HASTEACHCERT.options[0] = new Option("Yes", "Yes");
			document.UserForm.HASTEACHCERT.options[1] = new Option("No", "No");
			var oldval = $("#HIDELICTC").val();
			if( oldval != ""){
				setFormValue("HASTEACHCERT", oldval);
			}
			$("select[name='HASTEACHCERT']").show();
		}
	}
}
function populateSubject(maintype){
	if(maintype ==""){
		maintype = $("select[name='SUBJECTIDS'] :selected").val();
	}
	if(maintype != ""){
		var counter	= 0;
		document.UserForm.SUBJECTAREA.length=0;
		document.UserForm.SUBJECTAREA.options[0] = new Option("General", maintype+"00");
		for(var i = 0; i<subList.length; i++){
			if(subList[i].startsWith(maintype)){
				counter++;
				document.UserForm.SUBJECTAREA.options[counter] = new Option(
					""+subList[i].substring(5), subList[i].substring(0,4));
			}
		}

		var oldval = $("#HIDESUB").val();
		if( oldval != ""){
			setFormValue("SUBJECTAREA", oldval);
		}

		var dySubtext =$("select[name='SUBJECTIDS'] :selected").text();
		if(dySubtext != ""){
			$("#dySubtext").text(" in "+dySubtext);
		}
	}
}

function storeLicVal(){	
	var subcode = document.UserForm.SUBJECTAREA.value;
	if(subcode == "HN06"){	
		$("#HIDELICRN").val($("select[name='HASRNLICENSE']").val());
	}
	else if(subcode.startsWith("ED")){
		$("#HIDELICTC").val($("select[name='HASTEACHCERT']").val());
	}
}
///////////////////UI function ////////////////////////////
function setBarPercent(val){
	$("#progBar").css({"width": parseInt((val)*100/allQuest.length)+"%"});
}

function showBackButton(flag){
	if(flag)
		$("#backButton").show();
	else
		$("#backButton").hide();

	showErr(false);
}
function showErr(flag){
	if(flag)
		$("#err").show();
	else
		$("#err").hide();
}
function finalCheck() {
	checkDyZip();
	return false;
}
///////////////////common function ////////////////////////////
function getInt(val){
	return getIntByDef(val, 0);
}
function getIntByDef(val, def){
	var intval = parseInt(val, 10);
	if(isNaN(intval)){
		return def;
	}
	return intval;
}
function ajaxSuccessZip(xml){
	var isZip = $(xml).find('isZip').text();
	changeLoadImage(false);
	if(isZip =="true"){
		showErr(false);
		document.UserForm.submit();
	}
	else{
		showErr(true);
		$("input[name='ZIP']").focus();
	}
}

function checkDyZip(){
	if(isDoingZip){
		return true;
	}

	var isOk = false;
	var value = $("input[name='ZIP']").val();
	if(value.length ==5){
		if(checkzip(value) == 1){
			isOk = true;
			changeLoadImage(true);
			ajaxZip("zip="+value);
		}
		if(!isOk){
			showErr(true);
		}
	}
	return isOk;
}


function changeLoadImage(flag){
	if(flag){
		$("input[name='ZIP']").attr('disabled', 'disabled');
		$("#zipload").show();
	}
	else{
		$("input[name='ZIP']").attr('disabled', '');
		$("#zipload").hide();
	}
}

var debcount =0;
/************** debug function *******************/
function printLog(txt){
	// $("#log11").html($("#log10").html());
	// $("#log10").html($("#log9").html());
	// $("#log9").html($("#log8").html());
	// $("#log8").html($("#log7").html());
	// $("#log7").html($("#log6").html());
	// $("#log6").html($("#log5").html());
	// $("#log5").html($("#log4").html());
	// $("#log4").html($("#log3").html());
	// $("#log3").html($("#log2").html());
	// $("#log2").html($("#log1").html());
	// $("#log1").html((debcount++)+"  "+txt);
}
