var current_question = 1;
var total_questions = 0;
var mode = "show";

function checkQuizcreatorAnswer(e) {
	var answered = false;
	
	jQuery("#question-" + current_question + " .answer").each(function(i) {
		if(this.checked) {
			answered = true;
			return true;
		}
	});
	
	return true;
}

function nextQuizcreatorQuestion(e) {
	if(!checkQuizcreatorAnswer(e)) return;
	
	jQuery("#question-" + current_question).hide();
	current_question++;
	jQuery("#question-" + current_question).show();
	
	if(total_questions <= current_question) {
		jQuery("#next-question").hide();
		jQuery("#action-button").show();
	}
}

// This part is used only if the answers are show on a per question basis.
function showQuizcreatorAnswer(e) {
	if(!checkQuizcreatorAnswer(e)) return;
	
	if(mode == "next") {
		mode = "show";
		
		jQuery("#question-" + current_question).hide();
		current_question++;
		jQuery("#question-" + current_question).show();
	
		jQuery("#show-answer").val("Show Answer");
		return;
	}
	
	mode = "next";
	
	jQuery(".correct-answer-label.label-"+current_question).addClass("correct-answer");
	jQuery(".answer-"+current_question).each(function(i) {
		if(this.checked && this.className.match(/wrong\-answer/)) {
			var number = this.id.toString().replace(/\D/g,"");
			if(number) {
				jQuery("#answer-label-"+number).addClass("user-answer");
			}
		}
	});
	
	if(total_questions <= current_question) {
		jQuery("#show-answer").hide();
		jQuery("#action-button").show();
	} else {
		jQuery("#show-answer").val("Next >");
	}
}


function stopwatch(hour,min,sec) {
	sec=parseInt(sec)-1;
	
	if(hour>=1 && min == 1 && sec == 0){
		hour=parseInt(hour)-1;
		min = 59;
	}
		
	if(sec == 59){
		min=parseInt(min)-1;
	}
	
	if(min==0){
		min = 60;
	}
	
	if(sec==0){
		sec = 60;
	}
	
   document.getElementById('clock').innerHTML = ((hour<=9) ? "0"+hour : hour) + " : " + ((min==60) ? "00" : ((min<=9) ? "0" + min : min)) + " : " + ((sec==60) ? "00" : ((sec<=9) ? "0" + sec : sec));

	
  if (hour==0 && min==0 && sec==60) {
	alert("Oops...Time over!!!");
	jQuery("#hdnAction").val('submitQuiz');
	jQuery(".quiz-form").submit();
   window.clearTimeout(SD);
   return true; }
   
	SD=window.setTimeout(function(){stopwatch(hour,min,sec);}, 1000);
}

function startquiz(hour, min, sec){
    //if(validateEmailForm()){
		jQuery("#startquiz").hide();
		jQuery("#quizDiv").show();
		if(hour==0 && min==0 && sec==0){
		}else{

		  jQuery('#countdownMsg').show();
                  jQuery('#significantShort1').countdown({until:'+'+hour+'h +'+min+'m +'+sec+'s', format: 'HMS', onExpiry: liftOff});
		}
		quizcreatorInit();
    //}	
}

function valid(){

   
    var email1 = document.getElementById('teacher_email').value;
    var subject = document.getElementById('subject').value;
    var messag = document.getElementById('message').value;
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(email1=="" || email1=="Enter Email"){
 		alert('Please enter some email address!');
		document.getElementById(email1).focus();
		return false;
	}
	
	if(reg.test(trim(email1)) == false) {
		alert('Invalid Email Address: '+email1);
		document.getElementById(email1).focus();
		return false;
	}
       if(subject==""){
                  alert('Please enter Subject');
		  document.getElementById(subject).focus();
		   return false;
         }

}



function liftOff(){
	alert("Oops...Time over!!!");
	jQuery("#hdnAction").val('submitQuiz');
	jQuery(".quiz-form").submit();
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function validateEmailForm(){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var to = document.getElementById('to').value;
	var subject = document.getElementById('subject').value;
	var message = document.getElementById('message').value;
	var error;
	var counter = 0;
	if(to==""){
		alert('Please enter some email address!');
		document.getElementById('to').focus();
		return false;
	}
	
	var address = to.split(',');
	var count = address.length;
	
	if(address[count-1]==''){
		count = count-1;
	}
	
	for(i=0;i<count;i++){
		
		if(reg.test(trim(address[i])) == false) {
			error+= 'Invalid Email Address: '+address[i]+'\n';
			counter++;
		}
	}
	
	if(counter>0){
		alert(error);
		document.getElementById('to').focus();
		return false;
	}
	
	
	
	if(subject==""){
		alert('Please enter some subject for email!');
		document.getElementById('subject').focus();
		return false;
	}
	
	document.getElementById('email_ids').value=to;
	document.getElementById('email_subject').value=subject;
	document.getElementById('email_message').value=message;
	return true;

}

function checkEmail(email,id){
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(email==""){ 
		alert('Please enter some email address!');
		document.getElementById(id).focus();
		return false;
	}
	
	if(reg.test(trim(email)) == false) {
		alert('Invalid Email Address: '+email);
		document.getElementById(id).focus();
		return false;
	}
	var email_ids = jQuery('#email_ids').val();
	email_ids = email+","+email_ids;
	jQuery('#email_ids').val(email_ids);
	return true;
}

function addEmail(){
	var counter = jQuery('#email_counter').val();
	counter = parseInt(counter)+1;
	var html = '<div><input type="text" name="teacher_email[]" id="teacher_email_'+counter+'" value="Enter Email" onfocus="this.value=\'\'" onblur="checkEmail(this.value, this.id);" /></div>';
	jQuery("#addEmail").append(html);
	jQuery('#email_counter').val(counter);
	
}

function emailResult(){
	jQuery("#divResults").hide("slide", { direction: "up" }, 1000);
	jQuery("#divEmail").show("slow");
	jQuery("#sub").hide();
	
	
}

function showEmailDiv(){
	jQuery("#divEmail").slideDown('slow');
}

function quizcreatorInit() {
	jQuery("#question-1").show();
	total_questions = jQuery(".quizcreator-question").length;
	
	if(total_questions == 1) {
		jQuery("#action-button").show();
		jQuery("#next-question").hide();
	} else {
		jQuery("#action-button").hide();
		jQuery("#next-question").show();
		jQuery("#next-question").click(nextQuizcreatorQuestion);
		
	}
}

//jQuery(document).ready(quizcreatorInit);

