var submit_formAnswer = false;
 function actionSubmitViewAnswer() {
 	if(submit_formAnswer) {
		document.form_start.submit();
	 	return ;
 	}
 	var location = "submitAnswer.php?" ;
 	
 	switch(question_type) {
 		case 1://Checkbox
 			passdata = $(':checkbox[checked]').serialize() ;
 			break;
 		case 2://Radio
 			passdata = $(':radio[checked]').serialize();
 			break;
 		case 3://Text Input
 			passdata = $(':text[name="answer"]').serialize();
 			break;
 		case 4://Textarea
 			passdata = $('#answer').serialize() ;
 			break;
 	}
 	
 	if(typeof($('#btnSkip') ) != "undefined" )
 		$('#btnSkip').attr({"disabled":"disabled"});
 	
	strSkip = '' ;
	if(typeof($(':input[name="skip"]') ) != 'undefined' && $(':input[name="skip"]').length == 1 )
 		strSkip = '&skip=' + $(':input[name="skip"]')[0].value ;
	
 	$.ajax({
 			type: "POST",
 			url: location + strSkip,
 			data: passdata,
 			dataType: "json",
 			success: function(res) {
 				if(res.message_type.indexOf("Success") != -1) {
	 				//$('#result_note').html(res);
	 				if(res.show== 3) {
		 				updateCheckboxStatus(res.data);
		 				updateNoteAnswers(res.data);
	 				}
	 				else if(res.show == 2) {
	 					updateNoteAnswersInCorrect(res.data);
	 				}
	 				else {
	 					document.form_start.submit();
	 					if(typeof($(':input[name="skip"]') ) != 'undefined' && $(':input[name="skip"]').length == 1 ){
	 						window.opener.refreshListPage(); 
	 						self.close();
	 					}
	 				}
	 				$('#btnStart').attr({value:"Next Question"});
	 				submit_formAnswer = true;
 				}
 				else {
 					if(res.redirect.indexOf("Go Next")!=-1) {
 						alert(res.message_content);
 						$('#btnStart').attr({value:"Next Question"});
	 					submit_formAnswer = true;
 					}
 					else if(res.redirect.indexOf("None")!=-1) {
 						alert(res.message_content);
 					}
 				}
 			}
 	});
 }
 
 function updateCheckboxStatus(obj) {
 	var ele ;
 	for(i=0;i<$(':checkbox').length;i++) {
 		input_value = $(':checkbox')[i].value ;
 		if(obj[input_value].true_false == 'true' ) {
 			$('#chk_' + input_value).css({border:"2px solid #0f0",float:"left"});
 		}
 		else {	
 			if( typeof($(':input[value="' + input_value + '"]:checked').val())!="undefined" ) {
 				$('#chk_' + input_value).css({border:"2px solid #f00",float:"left"});
 			}
 			/*else
 				$('#chk_' + input_value).css({border:"2px solid #0f0",float:"left"});*/
 		}	
 	}
 	/*for(key in obj) {
 		if(obj[key].true_false == 'true' ) {
 			
 			if( typeof($(':input[value="' + key + '"]:checked').val())!="undefined" ) {
 				$('#chk_' + key).css({border:"2px solid #00f",float:"left"});
 			}
 			else
 				$('#chk_' + key).css({border:"2px solid #f00",float:"left"});
 					
 		}
 	}*/
 }
 
 function updateNoteAnswers(obj) {
 	var strreplace = "<span>Note: </span><br><div style='float:left;padding-left:25px;'>" ;
 	strreplace += "<ul style='list-style-type: none;'>" ;
 	for(i=0;i<$(':checkbox').length;i++) {
 		answer_id =  $(':checkbox')[i].value;
 		strreplace += "<li ><b>" + obj[answer_id].answer + "</b>: "+  obj[answer_id].description + "</li>" ;
 		
 	}
 	strreplace += "</ul>" ;
 	$('#result_note').html(strreplace);
 }
 
 function updateNoteAnswersInCorrect(obj) {
 	var str_incorrect = "<span>Note: </span><br><div style='float:left;padding-left:25px;'>" ;
 	var update = 0;
 	str_incorrect +=  "<ul style='list-style-type: none;'>" ;
 	for(i=0;i<$(':checkbox').length;i++) {
 		input_value = $(':checkbox')[i].value ;
 		if(obj[input_value].true_false == 'true' ) {
 			$('#chk_' + input_value).css({border:"2px solid #0f0",float:"left"});
 			if(typeof($(':input[value="' + input_value + '"]:checked').val())=="undefined") {
 				update = 1;
 				str_incorrect += "<li ><b>" + obj[input_value].answer + "</b>: "+  obj[input_value].description + "</li>" ;
 			}
 		}
 		else {	
 			if( typeof($(':input[value="' + input_value + '"]:checked').val())!="undefined" ) {
 				$('#chk_' + input_value).css({border:"2px solid #f00",float:"left"});
 				update = 1;
 				str_incorrect += "<li ><b>" + obj[input_value].answer + "</b>: "+  obj[input_value].description + "</li>" ;
 			}
 			/*else
 				$('#chk_' + input_value).css({border:"2px solid #0f0",float:"left"});*/
 		}	
 	}
 	str_incorrect += "</ul>" ;
 	if(update==1)
 		$('#result_note').html(str_incorrect);
 	else 
 		document.form_start.submit();

 }
 
 function submitFormStart() {
 	for(i=0;i<document.form_start.choose_test.length;i++) {
	 	if(document.form_start.choose_test[i].checked) {
	 		document.form_start.submit();
	 		return;
	 	}
 	}
 	
 	alert('Please choose one exam');
 	return false;
 }

function clickOnIt(id) {
 	if(typeof( array_selected) != "object" ) {
 		alert('Error!');
 		return ;
 	}
	if(array_selected.indexOf(id) == -1)
		array_selected[array_selected.length] = id;
	else {
		array_selected.splice(array_selected.indexOf(id), 1);
	}
 }
 
 function clearArraySelected() {
 	array_selected.length = 0;
 	$('#showCorrected>*').css({'background-color':'','color':''});
	$('#showInCorrected>*').css({'background-color':'','color':''});
	$('#showAll>*').css({'background-color':'','color':''});
 }
 
 function actionSkipQuestion(qid) {
 	//document.form_start.skipId = qid;
 	document.form_start.submit();
 	
 }
 
function popitup(url, title, height, width) { 	
	 newwindow=window.open(url,title,'height='+height+',width='+width+',resizable=0'); 	
	 
	 if(newwindow!=null)
	 	newwindow.focus();
	 /*
	 if (window.focus) {newwindow.focus()} 	
	 return false;*/ 
 }
function exitExam(){
	var str = "Are you sure you would like to exit this exam?" ;
	if(confirm(str))
		document.location = 'examHistory.php';	
	return false;
}

function updatePhoto2User(photoId) {
	var location = 'updatePhotos2User.php?' ;
	location += 'pid=' + photoId + '&nsPhoto=' + nsPhoto + '&step=' + step ;
	$.ajax({
 			type: "POST",
 			url: location,
 			dataType: "json",
 			success: function(res) {
 				if(res.message_type.indexOf("Success") != -1) {
	 				//$('#photo_' + photoId).html(res.message_content);
	 				var type = res.message_content.toLowerCase();
	 				var tmpNewImage = HTTP_LOCATION + '/images/' + type + '_photo_btn.jpg';
	 				$('img#photo_' + photoId).attr("src",tmpNewImage) ;
 				}
			}
 	});
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

