var isFirefox = typeof InstallTrigger !== 'undefined';

var preservemyHTML = "";

function addToTestAuto(selectedQuestions) 
{
	resetSummary();

	testBank = [];
	
	for(var i=0; i<selectedQuestions.length; i++) 
	{
		testBank.push(String(selectedQuestions[i]));
	}
	
	checkParent();
	
	//$(_parent).find('#pdfPreview').empty();
	used_Save_Print = false;
	testBank = uniqueArray(testBank);
	
	selectChapters = [];

	fetch_Data();
	ModifyUniqueTypeArr(uniquetype);
	
	setFilterArr();
	
	/* if($('#pdfPreview #unseencomprehension').length > 0) {
		$('#pdfPreview #unseencomprehension').detach().insertAfter('#pdfPreview #userInfo');
	} */
	
	setNumbering();
	addMarksInputEvent();
	showCart();

	//toggleAnswers();
	
	/* jQuery('#sContainIn .topTitlez').each(function(e)
	{
		if(!jQuery(this).find('.question-parent').length)
			jQuery(this).remove();
	}); */
	
/* 	if(document.getElementById("student-version-m").checked == true) {
		$('.marksDiv').hide();
	} */	
	
	$("#auto-level-1").hide();
	$("#level2").show();
	
	setHeightFix();
	prepareSummary();
	disableReplaceBtn();
}

function autoNewQuestion() {
	$("#aft-validation").hide();
	$("#insertNewQuestion input[type='text']").val('');
	$("#insertNewQuestion").fadeIn();

	var questionChap = allQuestions().distinct('chapter');
	
	var chapterHTML = "";
	var titleHTML = "";
	
	for(var i=0; i<questionChap.length; i++) {
		chapterHTML += '<option value="' + questionChap[i] + '">' + questionChap[i] + '</option>'
	}
	
	$("#afm-chap").html(chapterHTML).trigger('change');
	
	var typeHTML = "";
	for(var i=0; i<All_types.length; i++)
	{
		if(typeof All_types[i] === "undefined")
		{
			console.log(i, ' check...');
		}
		else 
		{
			typeHTML += '<option value="' + All_types[i] + '">' + All_types[i] + '</option>'
		}		
	}
	
	$("#afm-type").html(typeHTML).trigger('change');
	
	var _arrrr = [];
	
	for(var i=0; i < questions.length; i++)
	{
		_arrrr.push(questions[i].question.title)	
	}
	
	_arrrr = uniqueArray(_arrrr);
	
	for(var i=0; i<_arrrr.length; i++) {
		titleHTML += '<option value="' + _arrrr[i] + '">' + _arrrr[i] + '</option>'
	}
		
	$("#afm-question-title").html(titleHTML).trigger('change');
	
	var questionOutcome = allQuestions().distinct('outcome');
	var outcomeHTML = "";
	for(var i=0; i<questionOutcome.length; i++) {
		outcomeHTML += '<option value="' + questionOutcome[i] + '">' + questionOutcome[i] + '</option>'
	}
	$("#aft-outcome").html(outcomeHTML); 
	
	$('.alert_test').html();
	$('.alert_test').html(saved_alert);
}

function switchScreenAuto(ele)
{
	if(used_Save != true)
	{
		//console.log('saved')
		$(".auto-level-page").not("#auto-level-1").hide();
		$("#auto-level-1").show();
		$('#auto-page2').show();
		showAutoPage = true;		
		setHeightFix();
	}
	else
	{
		//console.log('unsaved')
		$(".auto-level-page").not("#auto-level-1").hide();
		$("#test_list").show();
		showsavedTests();
		used_Save = false;
	}
}

function autoNewQuestionAdd() {
	
	$("#aft-validation").hide();
	
	var formObj = {
		type: document.getElementById('afm-type'),
		title: document.getElementById('afm-type'),
		question: document.getElementById('afm-question'),
		answer: document.getElementById('afm-answer'),
		marks: document.getElementById('afm-marks'),
		difficulty: document.getElementById('aft-difficulty'),
		chapter: document.getElementById('afm-chap')
	}
	
	var titleHelper = document.getElementById('afm-type-custom');
	
	var showMsg = function(msg) {
		$("#aft-validation").text(msg).show();
	}
	
	if(formObj.title.value == "others" && titleHelper.value == "") {
		showMsg("Please type title");
		formObj.title.focus();
		return false;
	}

	if(formObj.title.value == "others") {
		formObj.title = titleHelper;
	}
	
	if(formObj.question.value == "") {
		showMsg("Please type question");
		formObj.question.focus();
		return false;
	}
	
	if(formObj.answer.value == "") {
		showMsg("Please type answer");
		formObj.answer.focus();
		return false;
	}
	
	if(formObj.marks.value == "") {
		showMsg("Please insert marks.");
		formObj.marks.focus();
		return false;
	}
	
	if(isNaN(formObj.marks.value)) {
		showMsg("Please insert valid marks in integer.");
		formObj.marks.focus();
		return false;
	}
		
	var newId = String(allQuestions().count() + 1);
	
	var newTitle = "";
	
	var formattedObj = {
		'class': All_class[0],
		'chapter': formObj.chapter.value,
		'neworbook': 'Custom Other',
		'topic': formObj.chapter.value,
		'subject': All_subject[0],
		'type': formObj.title.value,
		'question': {
			'title': newTitle,
			'rubric': formObj.question.value.replace(/\r\n|\r|\n/g,"<br />"),
			'option': [],
			'image': ''
		},
		'answer': [formObj.answer.value],
		'mark': formObj.marks.value,
		'outcome': 'others',
		'difficulty': formObj.difficulty.value,
		'category':"Applying",
		'id': parseInt(newId)
	}
	
	if(document.getElementById('afm-image').files.length > 0) {
		formattedObj['question']['image'] = inputToURL(document.getElementById('afm-image'));
	}
	
	allQuestions.insert(formattedObj);
	
	testBank.push(newId);
		
	if(tgType == 'manual') addToTest();
	else addToTestAuto(testBank);
		
	dismissPopupInsertion();
	showMsgBox("New question has been added.");
	return false;
}

function dismissPopupInsertion() {
	$("#insertNewQuestion").hide();
	$("#insertNewQuestion input[type='text'], #insertNewQuestion textarea").val('');
	$("#new-question-insertion-form")[0].reset();
}

function inputToURL(inputElement) {
    var file = inputElement.files[0];
    return window.URL.createObjectURL(file);
}

var style = '<style>\
				.question-parent {\
				clear: both;\
				margin: 0px;\
				padding: 6px 0px 6px 0px;\
				position: relative;\
				margin-top: 7px;min-height: 22px;}\
				.question-parent.selection{\
				}\
				.numbering {float: left;}\
				.marksDiv{float: right; widht: 25px;}\
			</style>';

function printStudent(a) 
{
	//$("#previewHTML .question-answer").hide();
	if(typeof appjs != "object") {
		if(isFirefox) {
			printCommand(a);
		} else {
			printCommand(a);
		}
	} else {
		if(preservemyHTML == "") {
			preservemyHTML = $(_parent).find("#pdfPreview").html();
		}
		$(_parent).find("#pdfPreview").html(preservemyHTML);
		//$("#previewHTML .question-answer").remove();
	}
}

function printTeacher(a) {
	//$("#previewHTML .question-answer").show();
	if(typeof appjs != "object") {
		if(isFirefox) {
			printCommand(a);
		} else {
			printCommand(a);
		}
	} else {
		if(preservemyHTML == "") {
			preservemyHTML = $(_parent).find("#pdfPreview").html();
		}
		$(_parent).find("#pdfPreview").html(preservemyHTML);
	}
}

window.onafterprint = function () {
	$("#css-responsive").attr('href', 'style/responsive.css');
}

window.onbeforeprint = function () {
	$("#css-responsive").attr('href', '');
}