
function stringIsBlank ( str ) {
	var blankRE=/^[\s]+$/
	if ( str == "" || blankRE.test(str) ) {
		return true;
	} else {
		return false;
	}
}

	
	/*
	 * update categories drop down based on category Id
	 */
	function updateCategories(catGrpXml) {
		var catGrp = catGrpXml.getElementsByTagName("categoryGroup")[0];
		//document.frmLoad.cat.options.length = 0;
		
		 var categoryComboBox = $("categoryId");
		 
		 
		//addOption(document.frmLoad.cat, "--Select Category--", "");
		var categories = catGrp.getElementsByTagName("category");
		for (var i = 0 ; i < categories.length ; i++) {

			var category = categories[i];
			var id = category.getElementsByTagName("id")[0].firstChild.nodeValue;
			var name = category.getElementsByTagName("name")[0].firstChild.nodeValue;
			var option = addOption(categoryComboBox,name,id);
				
			if ( this["categoryIdValue"] ) {
			   if ( option.value == this["categoryIdValue"] ) {
				  option.selected = true;
			   }
			}	
		}
		
		m_categoryIdOfCurrentQuestion = null;
	}
		
	function getCategoriesAjaxCall(catGrpId)  {
	
	// alert ( "GROUP ID: " + catGrpId );
	
	    var strURL = "ajaxserver.htm"; //  + ( "?uniqueCallId=" + getNextUniqueCallId () );
		
		var xmlHttpReq = false;
		var self = this;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		self.xmlHttpReq.open('POST', strURL, true);
		self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReq.onreadystatechange = function() {

			if (self.xmlHttpReq.readyState == 4) {
				
				updateCategories(self.xmlHttpReq.responseXML); 
				
				// alert ( " RESP: " + self.xmlHttpReq.responseText );
			}
		}

		
		self.xmlHttpReq.send("objectType=CATEGORYGROUP&action=GET&objectId="+catGrpId);
		
	}
		
				
   function getCategories ( categoryGroupId ) {
      

	   $("categoryGroupId").style.backgroundColor = "#ffffff";
	   

       try {
	     $("categoryId").options.length = 0;
		 addOption ( $("categoryId"), "- Category -", 0 );
	   } catch ( exc ) {
		   alert ( "EXC: " + exc );
	   }
	  //}
	  if ( categoryGroupId != 0 ) {
	     getCategoriesAjaxCall ( categoryGroupId );
	  }
	  
	  // document.frmLoad.cat.options.length = 0;
   }
   
    function categorySelected () {
      $("categoryId").style.backgroundColor = "#ffffff";
   }  
   
	function addOption(selectbox,text,value){
		 var optn = document.createElement("OPTION");
		 
		 optn.text = text;
		 optn.value = value;

		 
		 var len = selectbox.options.length;
		 selectbox.options[len] = optn;
				 
		 return optn;
	}
	
	
	
	
	
	function answerTypeSelected ( questionType ) {
	
	// alert ( "QUESTION TYPE: " + questionType );
	
	   switch ( questionType ) {
	      case "binary":
		      $("questionDto.type").selectedIndex = 0;
		     $("binaryAnswers").style.display = "block";
			 $("freeTextAnswers").style.display = "none";
			 $("multipleChoiceAnswers").style.display = "none";
		     break;
			 
	      case "free text":
		     $("questionDto.type").selectedIndex = 1;
		     $("binaryAnswers").style.display = "none";
			 $("freeTextAnswers").style.display = "block";
			 $("multipleChoiceAnswers").style.display = "none";		  
		     break;
			 
	      case "single choice":
		     $("questionDto.type").selectedIndex = 2;
		     $("binaryAnswers").style.display = "none";
			 $("freeTextAnswers").style.display = "none";
			 $("multipleChoiceAnswers").style.display = "block";		  
		     break;
	   }
	  // alert ( "QUESTION TYPE: " + questionType );
	}
	   
   
    function previewQuestion () {
	  //alert ( "PREVIEW" );
	   var dataIsValid = questionDataIsValid ();
	   
	   if ( dataIsValid ) {
	      previewUnsaved ();
	   }
	}


   
    function submitQuestion () {
	  // alert ( "SUBMIT" );
	   var dataIsValid = questionDataIsValid ();
	   
	   if ( dataIsValid ) {
	      submitForm ();
	   }
	}

    function questionDataIsValid () {
	   
	   var dataIsValid = false;
	   
	   $("errorMessage").innerHTML = "";  
	   $("videoLinkError").style.display = "none"; 
	   $("keywordsError").style.display = "none"; 
	   $("questionTextError").style.display = "none"; 
	   $("categoryError").style.display = "none"; 

       // THIS IS WRONG BUT WORKS FOR NOW!!!
	   $("questionDto.url").style.border = "1px"; 
	   $("questionDto.url").style.padding = "2px";

	   $("questionDto.keywords").style.border = "1px"; 
	   $("questionDto.keywords").style.padding = "2px";

	   $("questionDto.questionText").style.border = "1px"; 
	   $("questionDto.questionText").style.padding = "2px";

       $("freeTextAnswer").style.border = "1px";
       $("freeTextAnswer").style.padding = "2px";

       $("questionDto.data01").style.border = "1px solid black";
       $("questionDto.data01").style.padding = "1px";

       $("questionDto.data02").style.border = "1px solid black";
       $("questionDto.data02").style.padding = "1px";

       $("questionDto.data03").style.border = "1px solid black";
       $("questionDto.data03").style.padding = "2px";

       $("questionDto.data04").style.border = "1px solid black";
       $("questionDto.data04").style.padding = "2px";
	   

	   
	   $("categoryGroupId").style.backgroundColor = "#ffffff";

	   $("categoryId").style.backgroundColor = "#ffffff";
	 
		  
       var videoLinkValue = $("questionDto.url").value;
       var keywordsValue = $("questionDto.keywords").value;
       var questionTextValue = $("questionDto.questionText").value;
       var categoryGroupValue = $("categoryGroupId").value;
       var categoryValue = $("categoryId").value;
       var answerTypeValue = $("questionDto.type").value;
	   var freeTextCorrectAnswerValue = $("freeTextAnswer").value;
       var choice1Value = $("questionDto.data01").value;
	   var choice2Value = $("questionDto.data02").value;
	   var choice3Value = $("questionDto.data03").value;
	   var choice4Value = $("questionDto.data04").value;
       var correctChooseOneChoiceValue = $("singleChoiceAnswer").value;
	   

	   
       if ( videoLinkValue == "" ) {
	      $("errorMessage").innerHTML = "Please enter a URL for the video"; 
		  $("questionDto.url").style.border = "4px solid red";
		  $("questionDto.url").style.padding = "3px";
	      return false;
	   }	 
	   

	   if ( keywordsValue == "" ) {
	      $("errorMessage").innerHTML = "Please enter keywords for the question"; 
		  $("questionDto.keywords").style.border = "4px solid red";
		  $("questionDto.keywords").style.padding = "3px";
	      return false;	   
	   }
	 
	   if ( questionTextValue == "" ) {
	      $("errorMessage").innerHTML = "Please enter the question text"; 
		  $("questionDto.questionText").style.border = "4px solid red";
		  $("questionDto.questionText").style.padding = "3px";
	      return false;	   
	   }	   
	   
	   
	   if ( categoryGroupValue == 0 ) {
	      $("errorMessage").innerHTML = "Please select a category group for the question"; 
		  $("categoryGroupId").style.backgroundColor = "#ff0000";
	      return false;	   	   
	   }
	   

	   if ( categoryValue == 0 ) {
	      $("errorMessage").innerHTML = "Please select a category for the question"; 
		  $("categoryId").style.backgroundColor = "#ff0000";
	      return false;	   	   
	   }
	   	   	   
		   


       switch ( answerTypeValue ) {
	      case "binary":
			 break;
			 
	      case "free text":
		     if ( stringIsBlank (freeTextCorrectAnswerValue) ) {  //  if ( freeTextCorrectAnswerValue == "" ) {
	            $("errorMessage").innerHTML = "Please enter the correct answer"; 
		        $("freeTextAnswer").style.border = "4px solid red";
		        $("freeTextAnswer").style.padding = "3px";
	            return false;
	         }
			 break;
			 
	      case "single choice":
		  
		      var answerErrorMessage = null;
			  
	          switch ( correctChooseOneChoiceValue ) {
			     case "0":
				    if ( stringIsBlank (choice1Value) ) { //choice1Value == "" ) {
					   answerErrorMessage = "Please specify a valid choice as the correct answer";
					}
				    break;
					
			     case "1":
				    if ( stringIsBlank (choice2Value) ) { // choice2Value == "" ) {
					   answerErrorMessage = "Please specify a valid choice as the correct answer";
					}				 
				    break;
					
			     case "2":
				    if ( stringIsBlank (choice3Value) ) { // choice3Value == "" ) {
					   answerErrorMessage = "Please specify a valid choice as the correct answer";
					}				 
				    break;
					
			     case "3":
				    if ( stringIsBlank (choice3Value) ) { // choice4Value == "" ) {
					   answerErrorMessage = "Please specify a valid choice as the correct answer";
					}				 
				    break;
			  }
			  
			  if ( answerErrorMessage == null ) {
			     var numChoicesSpecified = 0;
				 if ( !stringIsBlank (choice1Value) ) { numChoicesSpecified++; }
				 if ( !stringIsBlank (choice2Value) ) { numChoicesSpecified++; }
				 if ( !stringIsBlank (choice3Value) ) { numChoicesSpecified++; }
				 if ( !stringIsBlank (choice4Value) ) { numChoicesSpecified++; }
				 if ( numChoicesSpecified < 2 ) {
				     answerErrorMessage = "Please specify at least 2 choices for the question";
				 }
			  }
			  
			  if ( answerErrorMessage ) {
	            $("errorMessage").innerHTML = answerErrorMessage;
		        $("questionDto.data01").style.border = "4px solid red";
		        $("questionDto.data01").style.padding = "3px";	
				$("questionDto.data02").style.border = "4px solid red";
		        $("questionDto.data02").style.padding = "3px";	
				$("questionDto.data03").style.border = "4px solid red";
		        $("questionDto.data03").style.padding = "3px";	
				$("questionDto.data04").style.border = "4px solid red";
		        $("questionDto.data04").style.padding = "3px";	
				
				return false;
			  }
			  		 
			 break;
	   }
	   
	   dataIsValid = true;
	   
	   return dataIsValid;
	}
	
	   
	    function hidePreviewAndSubmitButtons () {
			var previewQuestionButton = document.getElementById ( "previewQuestionButton" );
			var submitQuestionButton = document.getElementById ( "submitQuestionButton" );
			
			previewQuestionButton.style.display = "none";
			submitQuestionButton.style.display = "none";
		}
		
		function showPreviewAndSubmitButtons () {
			var previewQuestionButton = document.getElementById ( "previewQuestionButton" );
			var submitQuestionButton = document.getElementById ( "submitQuestionButton" );
			
			
			previewQuestionButton.style.display = "inline";
			submitQuestionButton.style.display = "inline";			
		}
		
		function hideClearAndEmailButtons () {
			var newQuestionButton = document.getElementById ( "newQuestionButton" );
			var emailQuestionButton = document.getElementById ( "emailQuestionButton" );
			
			newQuestionButton.style.display = "none";
			emailQuestionButton.style.display = "none";			
		}
		
		function showClearAndEmailButtons () {
			var newQuestionButton = document.getElementById ( "newQuestionButton" );
			var emailQuestionButton = document.getElementById ( "emailQuestionButton" );
			
			newQuestionButton.style.display = "inline";
			emailQuestionButton.style.display = "inline";				
		}		
		
		function hideSuccessMessageFromBackEnd () {
			var successMessageFromBackEnd = document.getElementById ( "successMessageFromBackEnd" );
			successMessageFromBackEnd.style.display = "none";
		}
		
		function showSuccessMessageFromBackEnd () {
			var successMessageFromBackEnd = document.getElementById ( "successMessageFromBackEnd" );
			successMessageFromBackEnd.style.display = "block";
		}		
		
		function createNewQuestion () {
			hideSuccessMessageFromBackEnd ();
			var categoryGroupId = document.getElementById ( "categoryGroupId" );
			categoryGroupId.selectedIndex = 0;
			
	
	       $("questionDto.url").value = "";
           $("questionDto.keywords").value = "";
           $("questionDto.questionText").value = "";
           $("questionDto.type").value = "";
	       $("freeTextAnswer").value = "";
           $("questionDto.data01").value = "";
	       $("questionDto.data02").value = "";
	       $("questionDto.data03").value = "";
	       $("questionDto.data04").value = "";
       
	
			showPreviewAndSubmitButtons ();
			hideClearAndEmailButtons ();			
		}
		
		function  submitForm(){
		
			document.submitQuestionForm.submit();
		}

		function previewUnsaved(){
			document.submitQuestionForm.requestType.value = "previewUnsaved";
			submitForm();
		}
		
		
		
			   
