////// 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 = getHistoryID(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);
	}
}
//////////////////////////////////////
function pageInit(){
	initMoreLinkPg();
	/*******  Even listener *******/
	$("select").change(showNextQuest);
	initForm();
	$.historyInit(pageload, "jquery_history.html");
	if(currentID==0){
		$.historyLoad(getHistoryName(1));
	}
}

function initForm(){
	allQuest.length = 0;
	$("div[id^='quest']").each(function (){
		allQuest.push(""+allQuest.length);
	});
	reValue();
	populateSubject("");
	changeLoadImage(false);
}
function showNextQuest(){
	if(isOpera && bwsHack){
		bwsHack = false;
		return true;
	}
	if(!validQuestion(getCurrentID(), true)){
		if($("input[id^='var']:visible").attr("name") == "ZIP"){
			$("input[name='ZIP']").focus();
		}
		return true;
	}
	if(getCurrentID()+1 <= allQuest.length){
		$.historyLoad(getHistoryName(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 itxt= "";
	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{
			itxt = i+" is empty";
			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);
		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);
		var maintype2digit = maintype.substring(0, 2);
		for(var i = 0; i<subList.length; i++){
			if(subList[i].startsWith(maintype2digit)){
				counter++;
				document.UserForm.SUBJECTAREA.options[counter] = new Option(
					""+subList[i].substring(5), subList[i].substring(0,4));
			}
		}
		var oldval = $("#HIDESUB").val();
		if( oldval != ""){
			setSelectMulitViewValue("SUBJECTAREA", oldval);
		}
		else{
			setSelectMulitViewValue("SUBJECTAREA", maintype);
		}

		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(id){
	if(id>1)
		$("#backButton").show();
	else
		$("#backButton").hide();

	showErr(false);
}
function showErr(flag){
	if(flag)
		$("#err").show();
	else
		$("#err").hide();
}
/* Deprecated Method - Not used anymore */
function showErrDeprecated(flag){
	$(".errmsg").hide();
	$("[id^='var']").removeClass("errWarn");
	$("div[id^='quest']").find(".questText label").css({"color": "#000000"});
	if(flag){
		$("div[id^='quest']:visible").find(".questText label").css({"color": "#FF0000"});
		$("div[id^='quest']:visible").find("[id^='var']").addClass("errWarn");
		$(".errmsg").show();
	}

}

function finalCheck() {
	//alert("finalcheck call!!");
	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();
	}
}

function getHistoryName(id){

	return ""+id;

	// if(id == allQuest.length){
		// return "OOOOO";	 //value=31
	// }
	/*******************************************
	 * DON'T MANUALLY EDIT THE FOLLOWING CODES
	 *******************************************/
	
	/*
	switch(id){
		case 1:   return "0000O";
		case 2:   return "000O0";
		case 3:   return "000OO";
		case 4:   return "00O00";
		case 5:   return "00O0O";
		case 6:   return "00OO0";
		case 7:   return "00OOO";
		case 8:   return "0O000";
		case 9:   return "0O00O";
		case 10:  return "0O0O0";
		case 11:  return "0O0OO";
		case 12:  return "0OO00";
		case 13:  return "0OO0O";
		case 14:  return "0OOO0";
		case 15:  return "0OOOO";
		case 16:  return "O0000";
		case 17:  return "O000O";
		case 18:  return "O00O0";
		case 19:  return "O00OO";
		case 20:  return "O0O00";
		default:  return "00000";	//value=0
	}
	*/
}

function getHistoryID(txt){
	for(var i=1; i<=allQuest.length; i++){
		if(getHistoryName(i) == txt){
			return i;
		}
	}
	return 1;
}



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);
}
