var fontSize;
var _parent = '';
var img_loaded = false;
var tot_time = 0;
var school_name = "";
var _stu_name = '';
var _class = '';
var _roll = '';
var _exam = '';
var _time = '';

var radioValue = '';
function getPDF()
{
	var html = String($('#pdfPreview').html()).replace(/(<img\b[^<>]*[^<>\/])>/ig, "$1 />");
	var htmlstr = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /></head><body>" + html + "</body></html>";
	shah.getPDF(htmlstr);
}

function printCommand(a)
{
	console.log(a);
	checkParentlastScreen();
	if(a == 's')
	{	
		$(_parent).find('.question-answer').css('display','none');
		$(_parent).find('.question-title').find('table tr:last').css('display','none');	
	}
	setFontSize(a);
	window.print();
	resetFontSize(a)
}

function export_word(a)
{	
	
}

function printCopy(a, radioValue){
		
	checkParentlastScreen()
	radioValue = 'pdf';
	if(a == 's'){
		if(radioValue == null){
			alert('Please select output format');
		}
		else
		{
			if(radioValue == 'word') showFontDialogBox('export_word','s');
			else showFontDialogBox('printStudent','s');
		}
	}

	if(a == 't'){
		if(radioValue == null){
			alert('Please select output format');
		}
		else
		{
			if(radioValue == 'word') showFontDialogBox('export_word','t');
			else showFontDialogBox('printTeacher','t');
		}
	}
}

function showFontDialogBox(fun_name, fun_arg)
{
	fontSize = 12;
	export_output(fun_name, fun_arg);
}

function createFontDialogBox()
{
	var $userInfo = '';
	$userInfo = $('<div class = "userInfo" id="userInfo"></div>');
	$userInfo.append('<div id="form-shcool"><label>School with location:</label> <input type="text" name="school" id="school" style="margin-top: 10px;margin-left: 10px;" /></div>');
 	$userInfo.append('<div id="form-subject"><label>Subject:</label> <input type="text" name="subject" id="subject" /></div>');
	$userInfo.append('<div id="form-class"><label>Class & Section:</label> <input type="text" name="_class" id="_class" style="margin-top: 10px;margin-left: 10px;" /></div>');
	$userInfo.append('<div id="form-class"><label>Examination:</label> <input type="text" name="_exam" id="_exam" style="margin-top: 10px;margin-left: 10px;" /></div>');
	$userInfo.append('<div id="form-class"><label>Student Name:</label> <input type="text" name="_name" id="_name" style="margin-top: 10px;margin-left: 10px;" /></div>');
	$userInfo.append('<div id="form-time"><label>Time:</label> <input type="text" name="time" id="time" style="margin-left: 12px; margin-top: -3px;" value='+window.tgduration  + " (Mins) "+' readonly /> <span id="compulsary">*</span></div>');
	$userInfo.append('<div id="form-class"><label>Roll Number:</label> <input type="text" name="_roll" id="_roll" style="margin-top: 10px;margin-left: 10px;" /></div>');
	
	$userInfo.append('<button id="closepopupBtn" class="" title="Close" onclick="closePopupFromPreview()"><i class="fa fa-times"></i></i></button>');
	$userInfo.append('<div class = "_laststep" id="form-btn"><button id="showPrevBtn" class="sm_button" onclick="lastStep()"  >Preview</button></div>');
	
	if($(_parent).find('.userInfo').length)
	{
		$(_parent).find('.userInfo').remove();
	}
	
	$(_parent).prepend($userInfo);
	//window.print();
	//$(_parent).find("#info").show();
}

function lastStep()
{
	school_name = $('#school').val();
	subject_name = $('#subject').val();
	_stu_name = $('#_name').val();
	_class = $('#_class').val();
	_roll = $('#_roll').val();
	_exam = $('#_exam').val();
	_time = $('#time').val();
	
	if(_time == "")
	{
		showMsgBox("Please enter time.");
		return;
	}
	
	/* if(subject_name == "")
	{
		showMsgBox("Please provide subject.");
		return;
	}
 */	
		
	$('#activityArea').hide();
	//loadImage();
	closePopupFromPreview();
	checkParentlastScreen();
	
	//$(_parent).find('.stud_word').prop('checked', true);
	showPreview();
}

function take_print()
{
	checkParentlastScreen();
	
	radioValue = $(_parent).find('.left_btns').find("input[name='output']:checked").val();
	console.log(radioValue);
	if(include_ans) printCopy('t', radioValue);
	else printCopy('s', radioValue);
}

function export_output(fun_name, fun_arg)
{	
	if(fun_arg == null)	window[fun_name]();
	else window[fun_name](fun_arg);
}

function SetUserInfoForPrint(ele)
{
	var school = school_name;
	var subject = subject_name;
	var name = _stu_name;	
	var class_t = _class;	
	var roll = _roll;	
	var exam = _exam;	
	var time = _time;
	
	//var set = ele.find('#userInfo').find('#set').val();	
	//time = time.replace(' (Mins)', '');
	
	var totalMarks = 0;
	
	$(_parent).find('.tot_marks').each(function (i, e)
	{
		totalMarks += Number($(this).text().replace('[Marks: ', '').replace(']', ''));
	})
	
	var $table = $('<table></table>');
	$table.append("<tr><td></td><td></td><tr>");
	$table.append("<tr><td></td><td></td><tr>");
	$table.append("<tr><td></td><td></td><tr>");

	var schoolobj = '<td align="left" valign="top" width="30%"><label class="topHeadLable">School with location</label><label class="colon">:</label> <span id="text-school">' + school + '</span></td>';
	var midSpace = '<td width="37%"></td>';
	var subjectobj = '<td align="left" valign="top" width="30%"><label class="topHeadLable topHeadLableRight">Subject</label><label class="colon">:</label> <span id="text-subject">' + subject + '</span></td>';
	var classobj = '<td align="left" valign="top" width="120pt"><label class="topHeadLable">Class & Section</label><label class="colon">:</label> <span id="text-class_t">' + class_t + '</span></td>';
	var examobj = '<td align="left" valign="top" width="120pt"><label class="topHeadLable topHeadLableRight">Examination</label><label class="colon">:</label> <span id="text-exam">' + exam + '</span></td>';
	var stunameobj = '<td align="left" valign="top" width="30%"><label class="topHeadLable">Student Name</label><label class="colon">:</label> <span id="text-stuname">' + name + '</span></td>';
	var timeobj = '<td align="left" valign="top" width="120pt"><label class="topHeadLable topHeadLableRight">Time</label><label class="colon">:</label><span id="text-time">' + convertMinsToHrsMins(window.tgduration) + '</span></td>';
	var rollobj = '<td align="left" valign="top" width="120pt"><label class="topHeadLable">Roll Number</label><label class="colon">:</label><span id="text-roll">' + roll + '</span></td>';
	var marksobj = '<td align="left" valign="top" width="120pt"><label class="topHeadLable topHeadLableRight">Total Marks</label><label class="colon">:</label><span id="text-marks">'+totalMarks+'</span></td></tr>';
	
	setTimeout(function () 
	{ 
		var tablestr = '';
	 
		if(userImageLogo != "")
		{
			tablestr = '<div align="center" class="logo_file">' + userImageLogo + '</div><table width="100%">\
						<tr>\
							<td width="100%" align="left" valign="top" colspan="2"><table width="100%"><tr>' + schoolobj + midSpace + subjectobj + '</tr><tr>' + classobj + midSpace + examobj + '</tr><tr>' + stunameobj + midSpace + timeobj +  '</tr><tr>' + rollobj + midSpace + marksobj + '</tr></table></td>\
						</tr>\
					</table><hr>';			
		}
		else 
		{
			tablestr = '<table width="100%">\
						<tr>\
							<td width="100%" align="left" valign="top" colspan="2"><table width="100%"><tr>' + schoolobj + midSpace + subjectobj + '</tr><tr>' + classobj + midSpace + examobj + '</tr><tr>' + stunameobj + midSpace + timeobj +  '</tr><tr>' + rollobj + midSpace + marksobj + '</tr></table></td>\
						</tr>\
					</table><hr>';
		}  
		
		$(ele).find('#previewHTML').prepend(tablestr);
	},500);
}

function setFontSize(a)
{
	$(_parent).find(".questionsSS").attr('width', '78%');
	
	$(_parent).find(".question-title").css('width', '100%');
	$(_parent).find(".data_table").css('width', '450px');
	
	$(_parent).find(".sequence").attr('width', '8%');
	$(_parent).find(".quest, .imgss, .opts").attr('width', '78%');
	$(_parent).find(".mks").attr('width', '22%');
	
	$(_parent).find(".aligntop").find("table td").css({
		'overflow': 'hidden',
		'word-break': 'break-all'
	})
	
	$(_parent).find("#previewHTML").css('font-size', (fontSize) + 'pt');
	$(_parent).find("table td").css('font-size', (fontSize) + 'pt');
	$(_parent).find("table th").css('font-size', (fontSize) + 'pt');
		
	$(_parent).find('.question-parent').find("table td").css(
	{
		'padding':'5px 15px 4px 5px'
	});
	
	/* $(_parent).find('.question-parent').find("table tr").css({
		'page-break-inside': 'avoid',
		'page-break-after':'avoid'
	}); */
	
	//$('.main_Div_table').css('padding', '0px');
	
	$(_parent).find(".marksDiv").css('display', 'none');
	$(_parent).find(".sequence").css('vertical-align', 'top');
	$(_parent).find("._markss").css('vertical-align', 'top');	
	
	//$( ".main_Div_table" ).next().not("._edit_cat_title").remove();
	
	$(_parent).find('.options').css('display', 'none');
	$(_parent).find('.question-answer').css('display', 'none');
	
	$(_parent).find('.options').find('table').css('display', 'none');
	$(_parent).find('.question-answer').find('table').css('display', 'none');
	
	$(_parent).find('.data_table').attr('border', 1);
	
	if(a == 's') 
	{
		$(_parent).find('.question-title').find('table tr:last').css('display','none');	
	}
	else 
	{
		$(_parent).find('.question-title').find('table tr:last').css('display','table-row');
	}
	
	$(_parent).find('.logo_file').css({
		
		'text-align':'center',
		'display':'block',
		'position':'relative'
	});
	
	$(_parent).find('.align_right').css('text-align','right');
	
	$('.previewNumbering').css('text-align','center');
	if(radioValue == "word") $('.previewNumbering_heading').css('text-align','left');
	else $('.previewNumbering_heading').css('text-align','center');
	
	
}

function resetFontSize(a)
{	
	$(_parent).find("#pdfPreview").find('#userInfo').remove();
	$(_parent).find("#pdfPreview").find('.logo_file').remove();
	
	$(_parent).find(".questionsSS").attr('width', '100%');
	
	$(_parent).find(".data_table").css('width', 'auto');
	
	$(_parent).find(".sequence").attr('width', '40pt');
	$(_parent).find(".quest, .imgss, .opts").attr('width', '92%');
	$(_parent).find(".mks").attr('width', '25pt');
	
	//$(_parent).find("#pdfPreview").append('<div class="summary-selection">' + summary + '</div>');
	
	$(_parent).find(".question-title").css('width', '65%');
	
	//$(_parent).find(".question-title").find( "table:first" ).attr('width', '');
	$(_parent).find("#pdfPreview").css('font-size', '');
	$(_parent).find("table td").css('font-size', '');
	$(_parent).find("table th").css('font-size', '');
	
	//$('.question-parent').css('padding','7px 55px 6px 6px');
	$(_parent).find('.question-parent').find("table td").css('padding', '');
	
	$('.main_Div_table').css('padding', '5px 0px');
	$('.previewNumbering').css('width','');
	
	/* $(_parent).find('.question-parent').find("table tr").css({
		'page-break-inside': 'auto',
		'page-break-after':'auto'
	}); */
	
	$(_parent).find('.data_table').removeAttr('border');
	
	//$(_parent).find('.options').css('display', 'block');
	$('._markss p').css('display', 'none');
	
	if(showMarks || window.MarksMode) $(_parent).find(".marksDiv").css('display', 'block');
	if(!showMarks) $(_parent).find(".marksDiv").css('display', 'none');
	
	if(include_ans || window.Show_Ans) $(_parent).find('.question-title').find('table tr:last').css('display','table-row');
	else $(_parent).find('.question-title').find('table tr:last').css('display','none');
	
	$('.previewNumbering_heading').css('text-align','center');
}