



function arizonaPopulateCourts(){
	var courtObj = document.getElementById('arizonacourt') ;	
	while (courtObj.options.length > 1 ) {
		courtObj.options[courtObj.options.length - 1] = null  ;
	}
	var courtIndex = document.getElementById("arizonacounty").selectedIndex ;
	if (courtIndex > 0 ) {
		courtObj.options[0].text = '< Select One >'	
		var courtsArr = document.getElementById("arizonacounty").options[courtIndex].getAttribute('courts').split(",") ;
		for (var i = 0 ; i < courtsArr.length ; i+=3) {
			courtObj.options[courtObj.options.length] = new Option (courtsArr[i] + ' - ' + courtsArr[i+2] + ' - $' + courtsArr[i+1] ,courtsArr[i] )
		}
	}else{ // no county selected
		courtObj.options[0].text = '< Select County First >'	
	}
	arizonaSetCoursePrice();
}


function arizonaSetCoursePrice (){
	var courtIndex = document.getElementById("arizonacourt").selectedIndex ;
	if (courtIndex) {
		var coursePrice = document.getElementById("arizonacourt").options[courtIndex].text.split('$')[1] ;
		$("#courtFee").html( coursePrice) ;
		var price = parseFloat(coursePrice) + parseFloat($("#courseFee").html()) + parseFloat ($("#stateSurcharge").html()) + parseFloat ($("#stateFee").html())
		if (price == parseInt (price)) {
			 price += '.00'
		}else if (price * 10 == parseInt (price * 10) ){
			price  = price.toString() + '0' ;
		}
		$("#totalCouseCost").html(price) ;
	}else{
		$("#courtFee").html('-') ;
		$("#totalCouseCost").html('-') ;
	}
}


function initGUI(){
	
	// show / hide course outline
	$("#navlink_outline").click(function(){
		try{
			if ($("#box_outline_shadow").css('display') == 'none' ){
				showCourseOutline()
			}else{ 
				hideCourseOutline()
			}
			return false;
		}catch(e){}
	});
	initOutlineMenu();
	//show hide mynotes
	$("#navlink_notes").click(function(){
		try{
			if ($("#box_mynotes_shadow").css('display') == 'none' ){
				showMyNotes()
			}else{ 
				hideMyNotes()
			}
			return false;
		}catch(e){}
	});
	// school name shadow
	$("#header_logo_text").clone().addClass("header_logo_text_shadow").insertBefore("#header_logo_text") ;

	// if we are in course section do not let them navigate away
	try {
	if (gentleunload) { 
		window.onbeforeunload = confirmExit ;
	}	
	}catch(e){}
 	 
 	 if ($("#mcform").length){
		// member contact form
		$("#mcform").yav({
			errorClass:"fielderror", 
			errorTag:"div", 
			errorPosition:"parent().prepend" 
		},{
			inputclasserror: "inputerror" 
		}); 
	}
	
 	 if ($("#authnetform").length){
		// authnet checkout
		$("#authnetform").yav({
			errorClass:"fielderror", 
			errorTag:"div", 
			errorPosition:"parent().prepend" ,
			custom: {
				ccexpiry: function(return_value){
					var sel_year = $('#cc_year').val();
					var sel_month = $('#cc_month').val();
					if (sel_year && sel_month) {
						$("#cc_month").removeClass('inputerror');		
						$("#cc_year").removeClass('inputerror');		
						return_value.msg = 'Specify expiration date'  
						return null;
					}else{				
						$("#cc_month").addClass('inputerror');		
						$("#cc_year").addClass('inputerror');		
						return_value.msg = 'Specify expiration date'  
						return return_value ;
					}		
				},
				phone: function (return_value){
					var val1 = $("#phone_1").val() ;
					var val2 = $("#phone_2").val() ;
					if ( val1.match (/^[0-9]{3}$/) && val2.match (/^[0-9]{7}$/)  ){
						$("#phone_1").removeClass("inputerror");
						$("#phone_2").removeClass("inputerror");
						return null ;
					}else{
						$("#phone_1").addClass("inputerror");
						$("#phone_2").addClass("inputerror");
						return_value.msg = 'Enter phone number'  
						return return_value;
					}
				}		
			},
			onOk: function(form){ 
				// hide submit button to prevent duplicate submits
				$(form).unbind('submit');
				$(form).bind('submit' , function(){return false;})
				$("fieldset.submit").hide();
			} 
		},{
			inputclasserror: "inputerror" 
		}); 
	}
	
	if ($("#regform").length){
		// registration / login form
		$("#regform").yav({
			errorClass:"fielderror", 
			errorTag:"div", 
			errorPosition:"parent().prepend" ,
			custom: {
				dayphone: function (return_value){
					var val1 = $("#phone_1").val() ;
					var val2 = $("#phone_2").val() ;
					if ( val1.match (/^[0-9]{3}$/) && val2.match (/^[0-9]{7}$/)  ){
						$("#phone_2").removeClass("inputerror");
						return null ;
					}else{
						$("#phone_2").addClass("inputerror");
						return_value.msg = 'Enter phone number'  
						return return_value;
					}
				},
				evphone: function (return_value){
					var val1 = $("#eveningphone_1").val() ;
					var val2 = $("#eveningphone_2").val() ;
					if ( ( val1  == '' && val2 == '' ) || (val1.match (/^[0-9]{3}$/) && val2.match (/^[0-9]{7}$/))){
						$("#eveningphone_2").removeClass("inputerror");
						return null ;
					}else{
						$("#eveningphone_2").addClass("inputerror");
						return_value.msg = 'Enter phone number'  
						return return_value;
					}
				},
				birthdate: function (return_value) {
					var sel_year = $('#birthday_year').val();
					var sel_month = $('#birthday_month').val();
					var sel_day = $('#birthday_day').val();
					if (sel_year && sel_month && sel_day) {
						var bday=new Date();
						bday.setFullYear(sel_year,sel_month -1 ,sel_day ) ;
						var dday = new Date();
						dday.setYear(dday.getYear() - 18) ;
						if (bday>dday){ // not 18 yet
							$("#guardian_info").show();
							$("#gname").addClass('required');		
							$("#gemail").addClass('email');		
							$("#gemail").addClass('required');		
							$("#grelationship").addClass('required');		
							$("#glicense").addClass('required');		
							$("#glicenseexparation_year").addClass('licenseexpiration');
						}else{
							$("#guardian_info").hide();
							$("#gname").removeClass('required');		
							$("#gemail").removeClass('email');		
							$("#gemail").removeClass('required');		
							$("#grelationship").removeClass('required');		
							$("#glicense").removeClass('required');		
							$("#glicenseexparation_year").removeClass('licenseexpiration');
						}	
						$('#birthday_month').removeClass('inputerror')
						$('#birthday_day').removeClass('inputerror')
						return null;
					}else{
						$('#birthday_month').addClass('inputerror')
						$('#birthday_day').addClass('inputerror')
						return_value.msg = 'Specify your birth date'  
						return return_value				
					}
				},
				licenseexpiration: function(return_value){
					var sel_year = $('#glicenseexparation_year').val();
					var sel_month = $('#glicenseexparation_month').val();
					var sel_day = $('#glicenseexparation_day').val();
					if (sel_year && sel_month && sel_day) {
						$("#glicenseexparation_month").removeClass('inputerror');		
						$("#glicenseexparation_day").removeClass('inputerror');		
						return null;
					}else{				
						$("#glicenseexparation_month").addClass('inputerror');		
						$("#glicenseexparation_day").addClass('inputerror');		
						return_value.msg = 'Specify expiration date'  
						return return_value ;
					}		
				},
				studentinfo : function(return_value){
					if ($("#isstudent:checked").length) {
						$("#mschool").addClass('required');		
						$("#grade").addClass('required');		
						$("#student_info").show() ;
					}else{
						$("#mschool").removeClass('required');		
						$("#grade").removeClass('required');		
						$("#student_info").hide() ;
					}
					return null ;
				},
				password2 : function(return_value){
					if ($("#password1").val() == $("#password2").val() ) {
						$("#password2").removeClass('inputerror');		
						return null;
					}else{
						$("#password2").addClass('inputerror');		
						return_value.msg = 'Passwords do not match'  
						return return_value				
					}
					return null ;
				},
				password3 : function(return_value){
					if ($("#password").val() == $("#password2").val() ) {
						$("#password2").removeClass('inputerror');		
						return null;
					}else{
						$("#password").addClass('inputerror');		
						return_value.msg = 'Passwords do not match'  
						return return_value				
					}
					return null ;
				},
				email2 : function(return_value){
					if ($("#email").val() == $("#email2").val() ) {
						$("#email2").removeClass('inputerror');		
						return null;
					}else{
						$("#email").addClass('inputerror');		
						return_value.msg = 'Emails do not match'  
						return return_value				
					}
					return null ;
				}
			}
		},{
			inputclasserror: "inputerror" 
		}); 
	}

	if ($("#regform2").length){
		// additional registration form
		$("#regform2").yav({
			errorClass:"fielderror", 
			errorTag:"div", 
			errorPosition:"parent().prepend" ,
			custom: {
				courtdate: function(return_value){
					if ( $("#Court_ordered_completion_date").val().match(/^[0-9]{2}[-/][0-9]{2}[-/][0-9]{4}$/)  ) {
						$("#Court_ordered_completion_date").removeClass('inputerror');		
						return null;
					}else{
						$("#Court_ordered_completion_date").addClass('inputerror');		
						return_value.msg = 'Invaid date format'  
						return return_value				
					}
					return null ;
				},
				nameanddeptofthecourt:	function(return_value){
					if ( $("#Name_and_department_of_the_court").val()) {
						$("#Name_and_department_of_the_court").removeClass('inputerror');		
						if ( $("#Name_and_department_of_the_court").val() == 'other' ){
							if ( $("#Name_and_department_of_the_court_other").val()) {
								$("#Name_and_department_of_the_court_other").removeClass('inputerror');		
							}else{
								$("#Name_and_department_of_the_court_other").addClass('inputerror');		
								return_value.msg = 'Required filed'  
								return return_value				
							}				
						}else{ 
							return null;
						}
					}else{
						$("#Name_and_department_of_the_court").addClass('inputerror');		
						return_value.msg = 'Required filed'  
						return return_value				
					}		
				}
			}
		},{
			inputclasserror: "inputerror" 
		}); 
	}

	// setup idverification setup if required
	if ($("#idverifysetup").length) {
		$("#idverifysetup").bind ('submit' , function () {
			return false ;// kill off form
		})
		// hook up action on save button
		$("#save_idquestion").click (function(){
			var qcount = parseInt($("#idverify_count").html());
			var question  = $("#idverify_question").val();
			var answer = $.trim($("#idverify_answer").val()) ;
			if (!question){
				alert ('Please select a question') ;
				return false ;
			}	
			if (!answer){
				alert ('Please enter your answer') ;
				$("#idverify_answer").focus();
				return false ;
			}
			if (answer.length <= 1) {
				alert ('Answer should be more then single character') ;
				$("#idverify_answer").focus();
				return false ;
			}
			// make sure answer is not already used
			var error = false ;
			$.each($("#idverifysetup :input[type=hidden]")	,function(k,elem){
				if($(elem).attr('name').indexOf ('idq_') === 0 ) {
					if ($(elem).val() == answer){
						alert ('Each question should have different answer')
						$("#idverify_answer").focus();
						error = true ;
						return false ;
					}
				}
			});		
			if (error) return false ;			
			// add question and answer to the list				
			var inputelem = $(document.createElement('input')).attr('type' , 'hidden' ).attr('name' , 'idq_' + question ).val(answer).appendTo($("#idverifysetup")) ;			
			
			// move this block to new li
			var newli = $(document.createElement('LI')).html('<span>' + $("#idverify_question option[value="+question+"]").text() +'</span>&nbsp;&nbsp;&nbsp;&nbsp; <span><b>'+answer+'</b></span>').insertBefore($(this).parent()) ;
			// unset question and answer
			$("#idverify_question").val(0)
			$("#idverify_answer").val('')
			// remove question from the list
			$("#idverify_question option[value="+question+"]").remove();				
			if($("#idverifysetup li").length > qcount) {
				// remove this li
				$(this).parent().remove();
				// show save option
				$("#idverify_save").click(function(){
					$("#idverifysetup").unbind ('submit');
					$("#idverifysetup").submit();
					return false ;
				});
				$("#idverify_save").show();
			}

			return false ;
		});
	}

} // end of init

function confirmExit (){
	// disabled for now
	//return 'Navigating away from this page will loose all unsaved progress' ;
}

function initOutlineMenu(){
	$("a.expand_unit_list").click(function(){
		var uid = this.id.replace('anc_expand_unit_list_' , '') ;
		if ($('#unitlist_'+ uid ).css("display") == 'none' ) {
			$('#anc_expand_unit_list_' + uid + " img")[0].src = $('#anc_expand_unit_list_' + uid + " img")[0].src.replace ('expand' , 'collapse'); 
			$('#unitlist_'+ uid ).slideDown('fast');
		}else{
			$('#anc_expand_unit_list_' + uid + " img")[0].src = $('#anc_expand_unit_list_' + uid + " img")[0].src.replace ('collapse','expand'); 
			$('#unitlist_'+ uid ).slideUp('fast');
		}
		return false;
	});
}
function expandAllModules (){
	$.each($("a.expand_unit_list"),function(i,obj){
		var uid = obj.id.replace('anc_expand_unit_list_' , '') ;
		//alert (uid)
		if($("#anc_expand_all_modules").html().indexOf('Expand all') > -1){
			$('#unitlist_'+ uid ).show();
			$('#anc_expand_unit_list_' + uid + " img")[0].src = $('#anc_expand_unit_list_' + uid + " img")[0].src.replace ('expand' , 'collapse'); 
		}else{
			$('#unitlist_'+ uid ).hide();
			$('#anc_expand_unit_list_' + uid + " img")[0].src = $('#anc_expand_unit_list_' + uid + " img")[0].src.replace ('collapse','expand'); 
		}
	});
	if($("#anc_expand_all_modules").html().indexOf('Expand all') > -1){
		$("#anc_expand_all_modules").html('Collapse all') ;
	}else{
		$("#anc_expand_all_modules").html('Expand all') ;
	}
}

function showCourseOutline(){
	hideCourseOutline();
	// if they are on course_outline page this should not be allowed
	if ($('#course_disclaimer').length || $("#idverifysetup").length || $("#regform2").length ||  $("#arizonadocform1").length || $("#arizonadocform2").length) {
		return false;
	}
//	if (window.location.toString().indexOf('/course_outline') > -1 ) {
//		return false;
//	}
	$("#box_outline_shadow").show();
	$("#box_outline").show();
	var json = $.getJSON("content/page/course_outline/rpc/42", function(data){
		var str = data.content ;
		$("#box_outline_content").html(str) ;
		//$("#box_outline_loader").css('visibility' , 'hidden' ) ;
		$("#box_outline_loader img").attr('src' , 'images/loading_blank.gif' ) ;
		initOutlineMenu();
	});
	checkJSON (json,'showcourseoutline',"content/page/course_outline/" , '' );
}

function hideCourseOutline(){
	if ($("#box_mynotes_shadow").css('display') != 'none' ){ 
		hideMyNotes()
	}
	$("#box_outline_shadow").hide();
	$("#box_outline").hide();
	//$("#box_outline_loader").css('visibility' , 'visible' ) ;
	$("#box_outline_loader img").attr('src' , 'images/loading.gif' ) ;
	$("#box_outline_content").html('') ;
}

function stopStatusUpdates(stopSends){
	if (stopSends){
		if (status_timeout) clearTimeout(status_timeout)
	}
	$("#top_navigation_2_b").hide();
}


function GoTo(sec){
	stopStatusUpdates(true);
	hideCourseOutline();	
	//return false;
	$('body').css('cursor','wait');
	var requrl = '';
	if (sec == 'finaltest') {
		requrl = "content/page/course_finaltest/rpc/42" ;
	}else if (sec == 'envirotestfail' || sec == 'envirotestintro' || sec == 'finaltestintro'  || sec =='envirotest') {
		requrl =  "content/page/course_arizonadef_"   + sec ;
	}else{
		requrl = sec.indexOf('test') == -1 ? "content/page/course_lesson/lesson/"+sec+"/rpc/42" : "content/page/course_test/test/"+sec.toString().replace('test.','')+"/rpc/42" ;
	}
	var json = $.getJSON(requrl , function(data){
		$("#main").html(data["content"]);
		// break unallowed links
		$("a.nextlink_disabled").bind('click', function(){
			//alert ('no can do');
			return false;
		});
		
		$('body').css('cursor','default');
		initFlowPlayer();
		window.scrollTo (0,0) ;
		// start progress tracking
		try {
			if (data['status'].indexOf(',') > - 1  ) {
				// there is time tracking
				var status_parts = data['status'].split (',');
				$("#status_1").text( status_parts[0] + ' time');
//				$("#status_3").text( status_parts[1] + ' / ' + status_parts[2] + ' minutes' );
				$("#status_3").text( timeFormat(status_parts[1]) + ' / ' + timeFormat(status_parts[2])  );
				var ratio = Math.round((status_parts[1] / status_parts[2] ) * 100 ,0) ;
				if (ratio > 100) ratio = 100 ;
				var bar_width =  parseInt($("#status_2").css('width')) * ratio / 100 -2  ;
				if (bar_width < 0 || !bar_width ) bar_width = 0 ;
				$("#progress_bar").width (bar_width ) ; 
				$("#progress_bar").text(ratio + '%' )
				//$("#progress_bar").css('color' , (ratio  < 50 ) ? '#000' : '#fff' );
				if (ratio || ratio === 0) {
					$("#top_navigation_2_b").show();
				}else{
					$("#top_navigation_2_b").hide();
				}
				statusUpdate(sec)
			}else{
				stopStatusUpdates(false);
			}
		}catch(e){
			stopStatusUpdates(true);
		};
	});
	if (timeout_time  > 0) 	logoutTimer();// every time this request is made restart timeout timer
	checkJSON (json,'goto',requrl , '');
}

function logoutTimer(){
	try {clearTimeout(timeout_timer)}catch(e){};// stop current timer if one exits
	timeout_timer = window.setTimeout("forceLogout()",timeout_time*60 *1000);
}

var logout_pending ;
function forceLogout(){
	window.scrollTo(0,0)
	$("#box_timeout").show();
	logout_pending = setTimeout ("doLogout()" , 2 * 60 * 1000) ; // 2 minutes grace time
}

function hideLogout (){
	try {clearTimeout(logout_pending)}catch(e){};
	$("#box_timeout").hide();
	logoutTimer();
}
function doLogout (){
	window.location = '/course/content/page/userfront_logout/';
}


var status_timeout ; 
function statusUpdate(sec){
	if (sec.toString().indexOf('.') > -1 || true) { 
		try {
			if (sec.toString().indexOf('test.') > -1){
				$.get ("content/page/course_test/test/"+sec.toString().replace('test.','')+"/su/42" , function (data) {
					if (data == 'ok'  ) { 
						// notthing to do, this is server thing only
					}else if(data == 'duplicatelogin'){
						alert ('Duplicate login detected. Please close all browser windows and login again.') ;
					}				
					if (status_timeout) clearTimeout(status_timeout)
					status_timeout  = setTimeout ("statusUpdate('"+sec+"')" , 60000 );
				});
			}else{
				$.get ("content/page/course_lesson/lesson/"+sec+"/su/42" , function (data) {
					if (data == 'ok' || data == 'doupdate'  ) { // move progress bar
						try {	
						//var state = $.trim($("#status_3").text().replace ('minutes' , '' ) ).split ( ' / ' );
						var state_parts = $.trim($("#status_3").text()).split ( ' / ' );
						var state = new Array () ;
						var tmp_s = state_parts[0].split(':') ;
						tmp_s[0] = tmp_s[0].replace ( /^0+/ , '' ) ;
						tmp_s[1] = tmp_s[1].replace ( /^0+/ , '' ) ;
						if (!tmp_s[0]) tmp_s[0] = 0 ;
						if (!tmp_s[1]) tmp_s[1] = 0 ;
						state[0] = parseInt(tmp_s[0]) * 60 + parseInt(tmp_s[1])
						var tmp_s = state_parts[1].split(':') ;
						tmp_s[0] = tmp_s[0].replace ( /^0+/ , '' ) ;
						tmp_s[1] = tmp_s[1].replace ( /^0+/ , '' ) ;
						if (!tmp_s[0]) tmp_s[0] = 0 ;
						if (!tmp_s[1]) tmp_s[1] = 0 ;
						state[1] = parseInt(tmp_s[0]) * 60 + parseInt(tmp_s[1])
						var new_state = parseInt(state[0]) + 1 ;
						if (new_state > state[1]) new_state = state[1] ;
//						$("#status_3").text($("#status_3").text().replace (state[0] + " / " +state[1] , new_state + " / " + state[1]) )
						$("#status_3").text($("#status_3").text().replace (timeFormat(state[0]) + " / " +timeFormat(state[1]) , timeFormat(new_state) + " / " + timeFormat(state[1])) )
						var ratio = Math.round ((new_state / state[1]) * 100 , 0)
						if (ratio > 100) ratio = 100 ;
						$("#progress_bar").width ( parseInt($("#status_2").css('width')) * ratio / 100 -2 ) ; 
						$("#progress_bar").text(ratio + '%' )
						//$("#progress_bar").css('color' , (ratio  < 50 ) ? '#000' : '#fff' );
						if (data == 'doupdate') {
							// enable disabled next links
							$("a.nextlink_disabled").unbind('click');
							$("a.nextlink_disabled").removeClass('nextlink_disabled').addClass('nextlink') ;
						}	
						}catch (e){}
					}else if(data == 'duplicatelogin'){
						alert ('Duplicate login detected. Please close all browser windows and login again.') ;
					}else if (data.indexOf('arizonadefkick') === 0 ) {
						GoTo (data.replace ( 'arizonadefkick:' ,  '' ) ) ;
						//return false ;
					}				
					if (status_timeout) clearTimeout(status_timeout)
					status_timeout  = setTimeout ("statusUpdate('"+sec+"')" , 60000 );
				});
			}
		}catch (e){
			stopStatusUpdates(true);
		}
	}else{
		// hide status bar and stop any updates
		stopStatusUpdates(false);
	}
}

function checkJSON (json,funcname,args1,args2){
	json.onreadystatechange = function(){
		if (json.readyState==4){
			if (json.responseText.indexOf('{') !== 0) {
				// let's log this
				var p = $.post('/course/content/page/userfront_logerror',{func:funcname,response:json.responseText,status:json.status,url:location.href,dbgc:dbgc,args1:args1,args2:args2},
					function(data){
						window.location  = '/course/content/page/userfront_error/code/Error:JSON:' + funcname +':'+data;
					});
			}
		}
	};
}

function showMyNotes() {
	if ($('#course_disclaimer').length || $("#idverifysetup").length || $("#idverifysubmit").length ||  $("#arizonadocform1").length || $("#arizonadocform2").length ) {
		return false;
	}
	hideMyNotes();
	$("#mynotesupdated").val("") ;
	$("#box_mynotes_shadow").show();
	$("#box_mynotes").show();
	var json = $.getJSON("content/page/course_mynotes/rpc/42", function(data){
		$("#box_mynotes_content").val(data.content);
		//$("#box_mynotes_loader").css('visibility' , 'hidden' ) ;
		$("#box_mynotes_loader img").attr('src' , 'images/loading_blank.gif' ) ;
		initMyNotes();
		mynotes_timer = window.setInterval ("saveMyNotes()",2000);
	});
	checkJSON (json,'mynotes',"content/page/course_mynotes/",'');
	$("#box_mynotes_content").keyup(function(){
		$("#mynotesupdated").val("1") ;
	});
	// add clear button
	$("#box_mynotes_clear").click(function(){
		$("#box_mynotes_content").val('');
		$("#mynotesupdated").val('1');
	});
}

var mynotes_timer = '' ;
function initMyNotes(){
//	if (window.location.toString().indexOf('/course_mynotes') > -1 ) {
//		return false;
//	}
}

function saveMyNotes(){
	if ($("#mynotesupdated").val()){
		$.post("content/page/course_mynotes/rpc/save", {'notes': $("#box_mynotes_content").val()} );
		$("#mynotesupdated").val("") ;
	}
}
function hideMyNotes(){
	if ($("#box_outline_shadow").css('display') != 'none' ){ 
		hideCourseOutline();	
	}
	$("#box_mynotes_shadow").hide();;
	$("#box_mynotes").hide();
	window.clearInterval(mynotes_timer) ;
	//$("#box_mynotes_loader").css('visibility' , 'visible' ) ;
	$("#box_mynotes_loader img").attr('src' , 'images/loading.gif' ) ;
	$("#box_mynotes_content").val('') ;
}



function submitTest(finaltest){
	// did they answer to all questions ?
	var error , ch;
	var submition = new Array();
	$("#testform > ol > li").each(function(k,v){
		// at least one radio input here should be checked
		ch = false ;
		$(v).find(":radio").each (function(kk,vv){
			if (vv.checked) {
				submition[submition.length] = vv.name.replace('q','') + ':' + vv.value
				ch = true ;
			}
		});
		if (!ch) {
			alert ('Please answer question '  + (k + 1) )
			error = true ;
			return false;
		}
	});
	if (error) return false;
	// we have submition ready 
	$('body').css('cursor','wait');
	var json = $.getJSON("content/page/course_"+  ((finaltest != undefined) ? 'final' : ''  ) +"testscore/answers/"+submition+"/rpc/42" , function(data){
		$("#main").html(data["content"]);
		$('body').css('cursor','default');
		window.scrollTo (0,0) ;
	});
	checkJSON (json,'testscore',"content/page/course_"+  ((finaltest != undefined) ? 'final' : ''  ) +"testscore/answers/"+submition , '');
	return false;
}

function submitEnviroTest(){
	// did they answer to all questions ?
	var error , ch;
	var submition = new Array();
	$("#testform > ol > li").each(function(k,v){
		// at least one radio input here should be checked
		ch = false ;
		$(v).find(":radio").each (function(kk,vv){
			if (vv.checked) {
				submition[submition.length] = vv.name.replace('q','') + ':' + vv.value
				ch = true ;
			}
		});
		if (!ch) {
			alert ('Please answer question '  + (k + 1) )
			error = true ;
			return false;
		}
	});
	if (error) return false;
	// we have submition ready 
	$('body').css('cursor','wait');
	var json = $.getJSON("content/page/course_envirotestscore/answers/"+submition+"/rpc/42" , function(data){
		$("#main").html(data["content"]);
		$('body').css('cursor','default');
		window.scrollTo (0,0) ;
	});
	checkJSON (json,'testscore',"content/page/course_envirotestscore/answers/"+submition , '');
	return false;
}

function submitIDverification(allowBlanks){
	// did they answer to all questions ?
	var error ;
	var submition = new Array();
	submition[0]='idverify_setup=42';
	$("#idverifysubmit > ol > li input").each(function(k,v){
		if (!allowBlanks) {
			if (!$(v).val()) {
				alert ('Please answer question '  + (k + 1) )
				error = true ;
				$(v).focus();
				return false;
			}
		}
		submition[submition.length] =  $(v).attr('name')  + '=' + escape($(v).val())  ;
	});
	submition = submition.join ('&')
	if (error) return false;
	var reqstr = '';
	try {
		var reqstr = $("#idverifysubmit").attr('action').split("/")[4] ;
		$('body').css('cursor','wait');
		var json = $.post( $("#idverifysubmit").attr('action') ,  submition   , function(data){
			$("#main").html(data["content"]);
			$('body').css('cursor','default');
			window.scrollTo (0,0) ;
			if (data['statusarizonadef']) {
				GoTo(reqstr);
			}
		},'json');
		checkJSON (json,'idverification',$("#idverifysubmit").attr('action'),submition);
	}catch(e){};
	$('body').css('cursor','default');
	return false;
}


function timeFormat (numMinutes){
	var hours = Math.floor (numMinutes / 60 )
	var minutes = numMinutes % 60 
	if (minutes.toString().length < 2 ) minutes = '0' + minutes.toString ();
	return hours.toString()  + ':' + minutes.toString();
}


function initFlowPlayer (){
	$.each($("a[id^=flowplayer-]"),function(){
		$(this).html('').css ('display' , 'block') ;
		if ($(this).css('width').toString().indexOf('width')  == -1 ) {
			$(this).css('width' , '455px' ) ;
			$(this).css('height' , '400px' ) ;
			
		}
		
		flowplayer( $(this).attr('id') ,  '/course/js/flowplayer-3.2.2.swf' ,{
			clip :{
				autoPlay:false,
				autoBuffering: true
			}
		}) ;

	})
}


