function showme(id)
{
  //alert('Show me id ' + id);
	var att

	if  (eval('document.getElementById(\''+'div_'+id+'\')'))
	{

  	att = eval('document.getElementById(\''+'div_'+id+'\')').style.visibility
    //alert(eval('document.getElementById(\''+'div_'+id+'\')').id);

  	if (att == 'visible')
	  {	
		  eval('document.getElementById(\''+'div_'+id+'\')').style.height ="1px"
		  //eval('document.getElementById(\''+'tbl'+id+'\')').style.height ="1px"
		  eval('document.getElementById(\''+'div_'+id+'\')').style.visibility = "hidden"
		  eval('document.getElementById(\''+'div_'+id+'\')').style.position = "absolute"
		  eval('document.getElementById(\''+'div_'+id+'\')').style.display = "none"
		  eval('document.getElementById(\''+'lbl_'+id+'\')').innerText = '+'
  	}
	  else
  	{
	  	eval('document.getElementById(\''+'div_'+id+'\')').style.height ="100px"
		  //eval('document.getElementById(\''+'tbl'+id+'\')').style.height ="100px"
		  eval('document.getElementById(\''+'div_'+id+'\')').style.visibility = "visible"
		  eval('document.getElementById(\''+'div_'+id+'\')').style.position = "static"
		  eval('document.getElementById(\''+'lbl_'+id+'\')').innerText = '-'
		  eval('document.getElementById(\''+'div_'+id+'\')').style.display = "inline"	 
    }
	}
	//alert('done')
}

/*---------------------------------------------------------------------------*/

function btnOkClick()
{
	var valuestr;
	if (document.frm.txtQuestionText.value == '')
	{
		alert('Please Enter Question Text');
		return false;
	}
	if (document.frm.txtComparableText.value != '')
	{
		valuestr = document.frm.txtComparableText.value;
		if(valuestr.length > 400)
		{
			alert('The maximum length for Comparable Text is 400 characters.')
			return false;
		}
	}
	if (document.frm.txtLinkedText.value != '')
	{
		valuestr = document.frm.txtLinkedText.value;
		if(valuestr.length > 250)
		{
			alert('The maximum length for Linked Text is 250 characters.')
			return false;
		}
	}
	if (document.frm.txtLinkedDefinition.value != '')
	{
		valuestr = document.frm.txtLinkedDefinition.value;
		if(valuestr.length > 250)
		{
			alert('The maximum length forLinked Definition is 250 characters.')
			return false;
		}
	}

  if (eval('document.frm.txtAvgQuestionScore'))
  {
    if (document.frm.txtAvgQuestionScore.value == '' ||
	      isNaN(document.frm.txtAvgQuestionScore.value))
    {
	  	alert('Please enter a number for Average Question Score.')
		  return false;
  	}
	}
	
  if (eval('document.frm.txtBestInClassScore'))
  {
	  if (document.frm.txtBestInClassScore.value == '' ||
	      isNaN(document.frm.txtBestInClassScore.value))
	  {
		  alert('Please enter a number for Best In Class Score.')
		  return false;
	  }
	}

	if (parseInt(document.frm.NumberOfOption.value) == 0)
	{
		alert('Please enter at lease one Option');
		return false;
	}

  if (validateOptions() == false)
    return false
    
	document.frm.SubmitType.value='OK';
	document.frm.submit();
}

/*---------------------------------------------------------------------------*/

function validateOptions()
{
  var numopt
  
	numopt = parseInt(document.frm.NumberOfOption.value);
	for(i=1;i<=numopt;i++)
	{
		if (eval('document.frm.txtOptionName'+i).value == '')
		{
	  	alert('Please enter Option '+i+'.');
			return false;
			break;
		}
		if (eval('document.frm.txtOptionScore'+i)) // Some pages do not have scores
		{
  		if (eval('document.frm.txtOptionScore'+i).value == '')
	  	{
		  	alert('Please enter a score for Option '+i+'.');
			  return false;
			  break;	
	    }
		  else if (isNaN(eval('document.frm.txtOptionScore'+i).value))
		  {
			  alert('Please enter a number for score in Option '+i+'.');
			  return false;
			  break;						  					
		  }
		}
	}
}

/*---------------------------------------------------------------------------*/

function ClearTextBox(name)
{
	//alert(name);
	eval('document.frm.'+name).value = '';
}

/*---------------------------------------------------------------------------*/

function CloseMe(rdST)
{
	var path,selectedsid,selectedqid;
	selectedsid = document.frm.selectedsid.value;
	selectedqid = document.frm.selectedqid.value;
	path = 'FormulateQuestion.asp?rdST='+rdST+'&selectedsid='+selectedsid+'&selectedqid='+selectedqid
    //alert(path)
	opener.window.location=path;
	self.close();
}

/*---------------------------------------------------------------------------*/

function CloseMeId(rdST, selectedType, selectedId)
{
	var path,selectedsid,selectedqid;
	//selectedsid = document.frm.selectedsid.value;
	//selectedqid = document.frm.selectedqid.value;
	path = 'FormulateQuestion.asp?rdST='+rdST+'&selectedtype='+selectedType+'&selectedid='+selectedId
    //alert(path)
	opener.window.location=path;
	self.close();
}

/*---------------------------------------------------------------------------*/

function CloseWindow(rdST)
{
	var path
	path = 'FormulateQuestion.asp?rdST='+rdST
	opener.window.location=path;
	self.close();
}

/*---------------------------------------------------------------------------*/

function fnOpen(URL,iHeight)
{
   var sFeatures='dialogHeight:' + iHeight + 'px;status=no;help=no;';
   //alert('asdfasdf');
   
   window.showModalDialog(URL,'',sFeatures);
   window.location.reload(true);
   //window.location = 'FormulateQuestion.asp';
}

/*---------------------------------------------------------------------------*/

/*function NewWindow(mypage,myname,w,h,scroll)
{
	var win = null;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=no'
	
	win = window.open(mypage,myname,settings)
}*/

var STid;
STid= '';


/*--------------------------------------------------------------------------*/

var globalType; // Indicates the type of item selected(Section, Topic, Sub Topic, Question)
var globalId;   // The unique id of the selected item

globalType = "";
globalId   = "";

/*--------------------------------------------------------------------------*/

function toggleMenu(strType)
{
    if ((strType == 'Topic' || strType == 'SubTopic') && globalType != strType)
    {
      var topicEl,subTopicEl
      //alert('get elbyid');
      topicEl = document.getElementById("divTopic");
      subTopicEl = document.getElementById("divSubTopic");
      if (strType == 'Topic')
      { 
        topicEl.style.display = 'inline';
        subTopicEl.style.display = 'none';
      }
      if (strType == 'SubTopic')
      {
        //alert('set display');
        subTopicEl.style.display = 'inline';
        topicEl.style.display = 'none';
      }
    }
}

/*--------------------------------------------------------------------------*/

function setValue(strType, strId)
{
  //alert('setValue -> type : ' + strType + ' id : ' + strId);

  if (eval("document.frm.chk_" + strType + "_" + strId).checked == true)
  {  
    // If an item was previously selected, then unselect it
    if (globalId != "" && globalId != strId)
    {
      eval("document.frm.chk_" + globalType + "_" + globalId).checked = false;
    }
      
    toggleMenu(strType);
    
    globalType = strType;
    globalId   = strId;
  
    document.frm.selectedType.value = strType;
    document.frm.selectedId.value = strId;
    
  }
  else
  {
    globalType = "";
    globalId   = "";
  
    document.frm.selectedType.value = "";
    document.frm.selectedId.value = "";
  }
}

/*--------------------------------------------------------------------------*/

function buttonClick(mypage,myname,w,h,scroll, actionType, action,numberofST)
{

  //alert('type :' + globalType + '  id : '+ globalId );
  //alert('validateAction : ' + validateAction(action, actionType));
  
  if (validateAction(action, actionType) == false)
    return;
  
  
	mypage = mypage + '&STid=' + globalId;

//  alert(mypage);  
	NewWindow(mypage,myname,w,h,scroll);

}

/*--------------------------------------------------------------------------*/

function buttonClickNew(mypage,myname,w,h,scroll, actionType, action,numberofST)
{

  //alert('type :' + globalType + '  id : '+ globalId );
  //alert('validateAction : ' + validateAction(action, actionType));
  
  if (validateAction(action, actionType) == false)
    return;
  
  
	mypage = mypage + '&type=' + globalType + '&id=' + globalId;

  //if (globalType == 'Question' && action == 'Add')
 // {
    
      
//  }
// alert(mypage);  
	NewWindow(mypage,myname,w,h,scroll);

}

/*--------------------------------------------------------------------------*/

function validateAction(strAction, strActionType)
{
  if (strAction == 'Add')
    return true;
    
  if ((globalType != strActionType) || (globalId == ""))
  {
		alert('Please Select a ' + strActionType + '!');
		return false;
	}
  
//	if (strAction == 'Edit'|| strAction == 'Delete' || strAction == 'Attach' || strAction == 'Preview')
//	{
//		if (globalId == "")
//		{
//			alert('Please Select a ' + strType + '!');
//			return false;
//		}
//	}

	if (strAction == 'Delete' && getQuestionCount() > 0)
	{
		alert(strType + ' with Questions cannot be deleted.');
		return false;
	}
	
	return true;

}


/*--------------------------------------------------------------------------*/

function getQuestionCount()
{
  // Returns the number of questions attached to the item.
  if (globalType == "Question" || globalType == "SubTopic")
    return 0;
    
  intQuestionCount = parseInt(eval('document.getElementById(\''+'NumQ_'+ globalType + "_" + globalId +'\')').value);

  //alert("QuestionCount = " + intQuestionCount);
  
  return (intQuestionCount);
}

/*--------------------------------------------------------------------------*/

function VaildOneCheck(numberofST,action,ST)
{
	var i;
	var count = 0;
	var NumQ;
	for(i = 1 ; i<=numberofST;i++)
	{
        //alert('1');
		if (eval('document.getElementById(\''+'CKST'+i+'\')').checked)
		{
			STid = eval('document.getElementById(\''+'CKST'+i+'\')').value;
			NumQ = parseInt(eval('document.getElementById(\''+'NumQ'+i+'\')').value);
			count++;
		}
	}
    //alert('2');
	if (action == 'Edit'|| action == 'Delete' || action == 'Attach' || action == 'Preview')
	{
		if (count == 0 || count > 1)
		{
			alert('Please Select a '+ST+'!');
			return false;
		}
	}
	else
	{
		if(count>1)
		{
			alert('Please Select a '+ST+'!');
			return false;
		}
	}
	
    //alert('3');
	if (action == 'Delete' && NumQ > 0)
	{
		alert(ST+' with Questions cannot be deleted.');
		return false;
	}
	return true;
}

function OpenDefaultPage(mypage,myname,w,h,scroll,ST,action,numberofST)
{
  var i;
  var count = 0;
  var id;
  var Flag;
  
  id = '';
  Flag = '';

  if (numberofST == 0 && action == 'Add')  
  {
	  mypage = mypage + '&id='+id+'&DefaultFG=';
	  NewWindow(mypage,myname,w,h,scroll);
  }
  else
  {	
	  if (numberofST == 0)
	  {
		  alert('Cannot open default question page!');
		  return false;
	  }
	  else
	  {
		  if (globalType == 'Section')
   		{ 
   		  id = globalId;
   		  count = 1;
   		  Flag = eval('document.getElementById(\''+'HasDefault_'+id+'\')').value;
   		}
  
		  //for(i = 1 ; i<=numberofST;i++)
		  //{
  		//	if (eval('document.getElementById(\''+'CKST'+i+'\')').checked)
	  	//	{
		  //		id = eval('document.getElementById(\''+'CKST'+i+'\')').value;
			// 	Flag = eval('document.getElementById(\''+'HasDefault'+i+'\')').value;
			//	  count++;
			//  }
		  //}

  		//if(count>1) 
	  	//{
		  //	alert('Please Select a '+ST+'!');
			//  return false;
		  //}
		  
		  if (count == 0 && action == 'Add')
		  {
			  mypage = mypage + '&id='+id+'&DefaultFG='+Flag;
			  //alert(mypage);
			  NewWindow(mypage,myname,w,h,scroll);
			  return true;
		  }
		  
		  if(count == 0 && action == 'default')
	  	{
		  	alert('Please Select a '+ST+'!');
			  return false;
		  }
		  
		  if (Flag == 0 && action =='default')
		  {
			  alert('The selected section already has a default Question Format.');
			  return false;
		  }
		  
		  mypage = mypage + '&id='+id+'&DefaultFG='+Flag;
		  NewWindow(mypage,myname,w,h,scroll);
	  }
  }
}



function VaildEDQuestionClick(mypage,myname,w,h,scroll,ST,action,numberofST,numberofq)
{
	
	if(numberofST >0)
	{
		/*if (numberofST == 1)
		{
			STid = document.getElementById('CKST1').value;
			mypage = mypage + '&STid='+STid;
			NewWindow(mypage,myname,w,h,scroll);
		}
		else
		{*/
			if( VaildOneCheck(numberofST,action,ST))
			{
				mypage = mypage + '&STid='+STid;
				//alert(mypage);
				NewWindow(mypage,myname,w,h,scroll);
			}
			else
			{
				return false;
			}
		//}
	}
	
	
}

function CheckMandatory()
{
	if(document.frm.txtMandatory.value != '')
	{
		var i,Found,value;
		Found = false;
		for(i = 1; i<= parseInt(document.frm.NumberOfOption.value);i++)
		{
			value = eval('document.frm.txtOptionName'+i).value;
			if (document.frm.txtMandatory.value == value)
			{
				Found = true;
			}
		}
			if(!Found)
			{
				alert('Please enter a vaild Question Option.');
				document.frm.txtMandatory.value = '';
				return false
			}
	}
}

function isEmpty(FormName,FieldName,LableName)
{
	var str;
	str = 'document.'+FormName+'.'+FieldName;
	if(eval(str).value == '')
	{
		alert('Please enter '+LableName+'.');
		return false;
	}
	return true;
	
}
function btnDeleteClick()
{
	var oRow,oCell,counter;
				
	if (document.frm.OptionClickValue.value != '')
	{
		var arrOpt = new Array;
			
		var selectedOpt,Totalcount;
		selectedOpt = parseInt(document.frm.OptionClickValue.value);
					
		Totalcount = parseInt(document.frm.NumberOfOption.value);
		//alert(selectedOpt);
		//alert(Totalcount);
		var count;
		count=0;
		for (i=1;i<=Totalcount;i++)
		{	
			if (i != selectedOpt)
			{
				count++;
				arrOpt[count+","+"0"] = '<p class="copy"><font color="#0066FF"><label onmouseover="this.style.cursor=\'hand\'" onclick="javascript:PopUpInfo(\''+count+'\');">'+count+'</label></font></p>';
				arrOpt[count+","+"1"] = '<p class="copy">'+eval('document.getElementById(\''+'txtOptionName'+i+'\')').value+'</p>'+'<input type="hidden" name="txtOptionName'+count+'" id="txtOptionName'+count+'" value="'+eval('document.getElementById(\''+'txtOptionName'+i+'\')').value+'" >';
				arrOpt[count+","+"2"] = '<p class="copy">'+eval('document.getElementById(\''+'txtGifName'+i+'\')').value+'<input type="hidden" name="txtGifName'+count+'" id="txtGifName'+count+'" value="'+eval('document.getElementById(\''+'txtGifName'+i+'\')').value+'" >'; 
				arrOpt[count+","+"3"] = '<p class="copy"><textarea name="txtComment'+count+'" class="field" rows="4" cols="50">'+eval('document.getElementById(\''+'txtComment'+i+'\')').value+'</textarea></p>';
			}
			
		}
			
		var k ;
		k = Totalcount
		for(i=1;i<=Totalcount; i++)
		{
			tblOption.deleteRow(k);
			k--;
		}
		document.frm.NumberOfOption.value = 0;
		
		if (count >0 )
		{	
			for (i=1; i<=count; i++)
			{
				oRow = tblOption.insertRow();
				for (j=0; j<4; j++)
				{
					oCell = oRow.insertCell();
					oCell.align = "left";
					oCell.bgColor = "white";
					oCell.innerHTML = arrOpt[i + "," + j];
					//alert(arrOpt[i + "," + j]);
				}
			}
			
		}
		document.frm.txtOption.value = '';
		document.frm.txtGif.value = '';
		document.frm.txtComment.value = '';
		document.frm.OptionClickValue.value='';
		document.frm.NumberOfOption.value = count;
	}
}

/*---------------------------------------------------------------------------*/

function btnSaveClick()
{
	var oRow,oCell,counter;
	if (document.frm.txtOption.value == '' )
	{
		alert('Please enter Option Name');
		return false;
	}
	
	if (document.frm.txtScore.value == '')
	{
		alert('Please enter a score.');
		return false;
  }
	else if (isNaN(document.frm.txtScore.value))
	{
		alert('Please enter a number for score.');
		return false;
	}
	
	if (document.frm.txtComment.value != '')
	{
		valuestr = document.frm.txtComment.value;
		if(valuestr.length > 1000)
		{
			alert('The maximum length for Option Comments is 1000 characters.')
			return false;
		}
	}
				
	if (document.frm.OptionClickValue.value != '')
	{
		var arrOpt = new Array;
					
		var selectedOpt,Totalcount;
		selectedOpt = document.frm.OptionClickValue.value;
		//alert('selectedOpt:'+selectedOpt);
		Totalcount = document.frm.NumberOfOption.value;
		//alert('Totalcount:'+Totalcount);
		for (i=1;i<=Totalcount;i++)
		{	
			arrOpt[i+","+"0"] = '<p class="copy"><font color="#0066FF"><label onmouseover="this.style.cursor=\'hand\'" onclick="javascript:PopUpInfo(\''+i+'\');">'+i+'</label></font></p>';
			if (i == selectedOpt)
			{
				arrOpt[i+","+"1"] = '<p class="copy">'+document.frm.txtOption.value+'</p>'+'<input type="hidden" name="txtOptionName'+i+'" id="txtOptionName'+i+'" value="'+document.frm.txtOption.value+'" >';
				arrOpt[i+","+"2"] = '<p class="copy">'+document.frm.txtScore.value+'</p>'+'<input type="hidden" name="txtOptionScore'+i+'" id="txtOptionScore'+i+'" value="'+document.frm.txtScore.value+'" >';
				arrOpt[i+","+"3"] = '<p class="copy">'+document.frm.txtGif.value+'</p>'+'<input type="hidden" name="txtGifName'+i+'" id="txtGifName'+i+'" value="'+document.frm.txtGif.value+'" >';
				arrOpt[i+","+"4"] = '<p class="copy"><textarea name="txtComment'+i+'" class="field" rows="4" cols="50">'+document.frm.txtComment.value+'</textarea></p>';
			}
			else
			{
				arrOpt[i+","+"1"] = '<p class="copy">'+eval('document.getElementById(\''+'txtOptionName'+i+'\')').value+'</p>'+'<input type="hidden" name="txtOptionName'+i+'" id="txtOpitonName'+i+'" value="'+eval('document.getElementById(\''+'txtOptionName'+i+'\')').value+'" >';
				arrOpt[i+","+"2"] = '<p class="copy">'+eval('document.getElementById(\''+'txtOptionScore'+i+'\')').value+'</p>'+'<input type="hidden" name="txtOptionScore'+i+'" id="txtOptionScore'+i+'" value="'+eval('document.getElementById(\''+'txtOptionScore'+i+'\')').value+'" >';
				arrOpt[i+","+"3"] = '<p class="copy">'+eval('document.getElementById(\''+'txtGifName'+i+'\')').value+'<input type="hidden" name="txtGifName'+i+'" id="txtGifName'+i+'" value="'+eval('document.getElementById(\''+'txtGifName'+i+'\')').value+'" >'; 
				arrOpt[i+","+"4"] = '<p class="copy"><textarea name="txtComment'+i+'" class="field" rows="4" cols="50">'+eval('document.getElementById(\''+'txtComment'+i+'\')').value+'</textarea></p>';
			}
		}
					
		var k ;
		k = Totalcount
		for(i=1;i<=Totalcount; i++)
		{
			tblOption.deleteRow(k);
			k--;
		}
		document.frm.NumberOfOption.value = 0;
					
					
			for (i=1; i<=Totalcount; i++)
			{
				oRow = tblOption.insertRow();
				for (j=0; j<=4; j++)
				{
					oCell = oRow.insertCell();
					oCell.align = "left";
					oCell.bgColor = "white";
					oCell.innerHTML = arrOpt[i + "," + j];
				}
			}
			document.frm.txtOption.value = '';
			document.frm.txtGif.value = '';
			document.frm.txtComment.value = '';
			document.frm.OptionClickValue.value='';
			document.frm.NumberOfOption.value = Totalcount;
					
	}
	else
	{
		oRow = tblOption.insertRow();
		var Option = new Array;
		counter = ++document.frm.NumberOfOption.value;
		Option["0"] = '<p class="copy"><font color="#0066FF"><label onmouseover="this.style.cursor=\'hand\'" onclick="javascript:PopUpInfo(\''+counter+'\');">'+counter+'</label></font></p>';
		Option["1"] = '<p class="copy">'+document.frm.txtOption.value+'</p>'+'<input type="hidden" name="txtOptionName'+counter+'" id="txtOptionName'+counter+'" value="'+document.frm.txtOption.value+'" >';
		Option["2"] = '<p class="copy">'+document.frm.txtScore.value+'</p>'+'<input type="hidden" name="txtOptionScore'+counter+'" id="txtOptionScore'+counter+'" value="'+document.frm.txtScore.value+'" >';
		Option["3"] = '<p class="copy">'+document.frm.txtGif.value+'</p>'+'<input type="hidden" name="txtGifName'+counter+'" id="txtGifName'+counter+'" value="'+document.frm.txtGif.value+'" >';
		Option["4"] = '<p class="copy"><textarea name="txtComment'+counter+'" class="field" rows="4" cols="50">'+document.frm.txtComment.value+'</textarea></p>';
		for (i=0; i<=4; i++)
		{
			oCell = oRow.insertCell();
			oCell.align = "left";
			oCell.innerHTML = Option[i];
			oCell.bgColor = "white";
		}
				
		document.frm.txtOption.value = '';
		document.frm.txtScore.value = '';
		document.frm.txtGif.value = '';
		document.frm.txtComment.value = '';
		//alert(document.frm.NumberOfOption.value)

	}
}
function CheckMaxLength(FormName,FieldName,LabelName,Maxlength)
{
	var str;
	var value;
	str = 'document.'+FormName+'.'+FieldName;
	if(eval(str).value != '')
	{
		value = eval(str).value;
		if(value.length > parseInt(Maxlength))
		{
			alert('The Max Length for '+LabelName+' is '+Maxlength+' characters.');
			eval(str).value = value.substr(0,1000);
			return false;
		}
	}
	return true;
}
function OptionEnter()
{
	var i,Totalcount;
	Totalcount = parseInt(document.frm.NumberOfOption.value);
		
		for (i=1;i<=Totalcount;i++)
		{		
		}
}
