var ischanged;//This variable used as flag to check whether user clicked submit button or not
function initialize_ischanged()//this function triggered when body load event
{
	ischanged=false;
	//alert(ischanged);
}
function check_buttonpressed_ischanged()//this function invoked when submit button  get clicked.
{
	ischanged=true;
	//alert("ischanged is true");	
}
//following function called when clicked any links in  menu.php file, to check whether user clicked submit button or not
function check_ischanged()
{
	//var form_action=function() { window.location.href='modify_groupit.php'; }
  	if(ischanged==false)
	{
		if(confirm("Save changes?\nOk will save changes.\nCancel will not save and changes will be lost."))
		{ 
		window.document.getElementById("save").click();
		//window.location.href=src.href;
		return false;
		}
    		else
		 return true;
	}
}

//function for checking maximum length of card information textarea



function validation(myform)
{
//Checking for minimum two category with help of hidden textbox
var no = document.getElementsByName("add_more_cards");
if(no.length>=2)
document.getElementById('txt_hidden_min_category').value=no.length;

var count=document.getElementById("txt_hidden_count").value;
		for(i=1;i<=count;i++)//This for-loop for avoiding white spaces. if white space only occured in textarea  without any characters 
		{	
				category_id="category_name"+i;
				
				if(document.getElementById(category_id))
				{ 	
		
					var str=document.getElementById(category_id).value;
					var pattern_white_space=/^\s{1,4}/g;
					//var no_white_spaces=str.match(pattern_white_space);
					var pattern_any_characters=/\w+/g;
					//var no_characters=str.match(pattern_any_characters)
					if(str.match(pattern_white_space) && !str.match(pattern_any_characters)) 
					document.getElementById(category_id).value="";
								
					
				}
				
				
		}


}
//Following function triggered when onkeypress event occured after typing the  category name in  textarea
function validate_characters_limit(src,e)//For maximum limit of the characters in the category name
{
//alert("category validations");
var i,no_line_break;
var str=document.getElementById(src).value;

if(window.event) // For IE
                 keynum = e.keyCode;
        else if(e.which)//For Netscape/firefox/opera
       		keynum=e.which;

			if(str!="")
			{
			//alert(str.length);
						/*var pattern=/\n/g;
						if(str.match(pattern))
						{
							no_line_break=str.match(pattern);
						//alert("no of enter"+no_line_break.length);
							if(no_line_break.length>1)
							{	
								//alert("Not allowed to type more than 2 lines for Category Name");
								//document.getElementById(src).value="";
								//str="";
								document.getElementById(src).value=str.substring(0,44);
								document.getElementById(src).value.focus;
								//temp="document.groupit."+src;
                                				//temp1=temp+".focus();";
								//eval(temp1);
								//alert(temp1);
								if(keynum==8)//8 is ASCII  value for Backspace key
									{		
									return true;
									}
								str="";
								return false;                     
							}
						}*/
						if(str.length>= 44)
						{
							//alert("Not allowed to type more characters for Category Name");
							//document.getElementById(src).value="";
							//temp="document.groupit."+src;
                                				//temp1=temp+".focus();";
                                				//eval(temp1);
							document.getElementById(src).value=str.substring(0,44);
							document.getElementById(src).value.focus;
							if(keynum==8)//8 is ASCII  value for Backspace key
								{		
								return true;
								}
							return false;
							
						}
			//document.groupit.category_name2.focus();
			}
}
//categorytables(table name from 2 to 8) are dynamically created and also inserted into  div tag (divCategory) when following function is called.
function addCategory()
{
    
	
	var no = document.getElementsByName("add_more_cards");

	
	if(no.length>=8)//8--->indicate totaly 8 'input' html element with Name 'add_more_cards' available inside div tag(divCategory) when user clicking 8th attempt on 'add category' button 
	{
		 alert("Not allowed to add more than 8 categories");
		 return false;
	}
	else	 
	{
		if ( typeof addCategory.cat_rownumber == 'undefined' )
		{
	             var x=parseInt(document.getElementById("txt_hidden_count").value);
				 addCategory.cat_rownumber=x+1;
			addCategory.unique_id=0;
				 
		}		 
				
	tabBody1=document.createElement("Table");
	tabBody1_body=document.createElement("Tbody");
	//tabBody1.border="1";
	
	tabBody1.id="categorytable"+addCategory.cat_rownumber;
	tabBody1.name="categorytable"+addCategory.cat_rownumber;
	if(browser_type=="firefox")
		{
			tabBody1.setAttribute("onmouseover","display_color(this.id);"); 
			tabBody1.setAttribute("onmouseout","hide_color(this.id);"); 
			
		}
		else
		{
			tabBody1.onmouseover=function (){ display_color(this.id);}	
			tabBody1.onmouseout=function (){ hide_color(this.id);}	
		}
	
	tabBody1_body.id="categorytable_body"+addCategory.cat_rownumber;
	//tabBody1.style.width="100em"

	row0=document.createElement("TR");
	cell0=document.createElement("TD");
	tabBody0=document.createElement("Table");
	tabBody0_body=document.createElement("Tbody");
	//tabBody0.style.width="100em"
	
	row01=document.createElement("TR");
	cell01=document.createElement("TD");
	tabBody_card=document.createElement("TABLE");//this table contains textarea elements only
	tabBody_card.id="cardstable"+addCategory.cat_rownumber;
	tabBody_card_body=document.createElement("TBODY");
	tabBody_card_body.id="cardstable_body"+addCategory.cat_rownumber;
	//tabBody_card_body.style.width="100em"
	//cell0.appendChild(tabBody0);
	//row0.appendChild(cell0); 
	//alert(tabBody1.innerHTML);
	row1=document.createElement("TR");
	row2=document.createElement("TR");
	row3=document.createElement("TR");
	row4=document.createElement("TR");
	
	cell1 = document.createElement("TD");
	cell1.style.width="150em";
	
	cell2 = document.createElement("TD");
	cell2.style.width="1em";
	
	cell2_1=document.createElement("TD"); //for category name instruction
	cell2_1.style.width="1100em";
	
	
	cell3 = document.createElement("TD");
	cell3.style.width="6em";
	cell4 = document.createElement("TD");
	cell4.style.width="4em";
	
	cell4_1 = document.createElement("TD");//for delete button instruction
	cell4_1.style.width="74em";
	
	
	cell5 = document.createElement("TD");
	cell5.style.width="6em";
	cell6= document.createElement("TD");
	
	cell6.style.width="4em";
	//for category facts instruction
	cell6_1= document.createElement("TD");
	cell6_1.style.width="74em";
	
	cell7 = document.createElement("TD");

	cell7.style.width="20em";
	//cell8 = document.createElement("TD");
	
	//cell8.style.width="10em";
	//for card information instruction
	cell9 = document.createElement("TD");
	cell9.colSpan="2";
		
	
	textnode1=document.createElement("Label");
	//textnode1.id="cat_name"+addCategory.cat_rownumber;
	textnode1.innerHTML="<font color=#CC6666 ><b>"+"Category Name";
	
	
	textnode2=document.createElement("Textarea");
	textnode2.name="category_name"+addCategory.cat_rownumber;
	textnode2.id="category_name"+addCategory.cat_rownumber;
	textnode2.style.width="190px";
	textnode2.rows="2";
	textnode2.style.overflow="hidden";
	var browser_type=getbrowsertype();
		if(browser_type=="firefox")
		{
			textnode2.setAttribute("onkeypress","return validate_characters_limit(this.id,event);"); 
		}
		else
		{
			var jsfunction = function() {return validate_characters_limit(this.id,event); };
			textnode2.onkeypress=jsfunction;	
			
		}
	textnode2_1=document.createElement("Label");	
	textnode2_1.innerHTML="Enter or edit the name of the category here"+"<br>"+ "Add more categories by clicking the Add another category button after you enter card information below";
			 	 
	textnode3=document.createElement("label");
	textnode4_1=document.createElement("label");//for delete button instruction
//	textnode3.style.visibility="none";
	
	var textnode4_name="delete_category"+addCategory.cat_rownumber;
	var textnode4_id="delete_category"+addCategory.cat_rownumber;
	
	
	
	var browser_type=getbrowsertype();
	if(browser_type=="firefox")
	{
		textnode4=document.createElement("Input");
		textnode4.name="delete_category"+addCategory.cat_rownumber;
		textnode4.id="delete_category"+addCategory.cat_rownumber;
		textnode4.type="button";
		textnode4.value="Delete Category & Question";
		textnode4.style.width="192px";
		textnode4.setAttribute("onClick","delete_category_fun(this);"); 
		//for delete button instruction
		textnode4_1.innerHTML="Click this button to delete this category and all questions belonging to this category."
		
	}
	else
	{
		textnode4=document.createElement("<Input type='button' style='width:194px' value='Delete Category & Question' name="+textnode4_name + " id="+textnode4_id+ ">");
		textnode4_1.innerHTML="Click this button to delete this category and all questions belonging to this category.";
		textnode4.onclick=function (){ delete_category_fun(this);}	
		
	}
	
	
	textnode5=document.createElement("Label");
	//textnode5.id="cat_facts"+addCategory.cat_rownumber;
	textnode5.innerHTML="<font color=#CC6666 ><b>"+"Category Facts";
	
	textnode6=document.createElement("Textarea");
	textnode6.name="category_facts"+addCategory.cat_rownumber;
	textnode6.rows="5";
	textnode6.style.overflow="auto";
	textnode6.style.height="85px";
	
	textnode6.style.width="192px";
	//for category facts instructions
	textnode6_1=document.createElement("Label");
	textnode6_1.innerHTML="Enter information here that your students should know about this category.";
	
	
	textnode7=document.createElement("Label");
	//textnode7.id="card_info";
	textnode7.innerHTML="<font color=#CC6666 ><b>"+"Card Information";
	
	//textnode8=document.createElement("Label");
	//for card information instruction
	textnode9=document.createElement("Label");
	//textnode7.id="card_info";
	textnode9.innerHTML="Enter information for the cards belonging to this category in the boxes below."+"<br>"+"Add more cards as needed by clicking the Add more cards button.";
	
			 
	cell1.appendChild(textnode1);
	cell2.appendChild(textnode2);
	cell2_1.appendChild(textnode2_1);
	
	cell3.appendChild(textnode3);
	cell4.appendChild(textnode4);
	cell4_1.appendChild(textnode4_1);
	
	cell5.appendChild(textnode5);
	cell6.appendChild(textnode6);
	cell6_1.appendChild(textnode6_1);// for category facts instructions
	cell7.appendChild(textnode7);
	//cell8.appendChild(textnode8);
    cell9.appendChild(textnode9);
	//cell3.appendChild(textnode2);
	
	row1.appendChild(cell1);
	row1.appendChild(cell2);
	row1.appendChild(cell2_1);
	
	row2.appendChild(cell3);
	row2.appendChild(cell4);
	row2.appendChild(cell4_1);
	
	row3.appendChild(cell5);
	row3.appendChild(cell6);
	row3.appendChild(cell6_1);
	row4.appendChild(cell7);
	//row4.appendChild(cell8);
	row4.appendChild(cell9);
	//tabBody.appendChild(row);	
	//tabBody1.appendChild(row0);
	tabBody0_body.appendChild(row1);	 
	tabBody0_body.appendChild(row2);
	tabBody0_body.appendChild(row3);
	tabBody0_body.appendChild(row4);
	tabBody0.appendChild(tabBody0_body);
	cell0.appendChild(tabBody0);
	row0.appendChild(cell0);
	tabBody1_body.appendChild(row0);
	
	//document.getElementById("divCategory").innerHTML=document.getElementById("divCategory").innerHTML+document.getElementById("divCategory").innerHTML;
		
	//tabBody1=document.getElementById("cardstable");
	
		row=document.createElement("TR");
		cell7 = document.createElement("TD");
		cell7.id="td_default_no"+eval(addCategory.unique_id++);
		 cell7.style.width="25em";
		cell8= document.createElement("TD");
		cell8.id="td_default_no"+eval(addCategory.unique_id++);
		 cell8.style.width="25em";
		cell9 = document.createElement("TD");
		cell9.id="td_default_no"+eval(addCategory.unique_id++);
		 cell9.style.width="25em";
		cell10 = document.createElement("TD");
		cell10.id="td_default_no"+eval(addCategory.unique_id++);
		 cell10.style.width="25em";
	
			 textnode7=document.createElement("Textarea");
			 textnode7.name="textarea"+addCategory.cat_rownumber+"[]";
			 textnode7.style.width="170px";
			 
			 textnode7.rows="4";
			 textnode7.style.overflow="hidden";
			  if(browser_type == "firefox")
				{  
					textnode7.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() {return alertMsgLength(this,this.parentNode.id,event); };
					textnode7.onkeypress=jsfunction;
					
				}
			  
			 textnode8=document.createElement("Textarea"); 
			 textnode8.name="textarea"+addCategory.cat_rownumber+"[]";
			 textnode8.style.width="170px";
			  textnode8.rows="4";
			  textnode8.style.overflow="hidden";
			  if(browser_type == "firefox")
				{  
					textnode8.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() {return alertMsgLength(this,this.parentNode.id,event); };
					textnode8.onkeypress=jsfunction;
					
				}
			 textnode9=document.createElement("Textarea"); 
			 textnode9.name="textarea"+addCategory.cat_rownumber+"[]";
			 textnode9.style.width="170px";
			  textnode9.rows="4";
			  textnode9.style.overflow="hidden";
			  if(browser_type == "firefox")
				{  
					textnode9.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() {return alertMsgLength(this,this.parentNode.id,event); };
					textnode9.onkeypress=jsfunction;
					
				}
			 textnode10=document.createElement("Textarea"); 
			 textnode10.name="textarea"+addCategory.cat_rownumber+"[]";
			 textnode10.style.width="170px";
			  textnode10.rows="4";
			  textnode10.style.overflow="hidden";
				if(browser_type == "firefox")
				{  
					textnode10.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() {return alertMsgLength(this,this.parentNode.id,event); };
					textnode10.onkeypress=jsfunction;
					
				}
			cell7.appendChild(textnode7);
			cell8.appendChild(textnode8);
			cell9.appendChild(textnode9);
			cell10.appendChild(textnode10);
			/* Following lines appending textareas into a row*/
			 row.appendChild(cell7);
			 row.appendChild(cell8);
			 row.appendChild(cell9);
			 row.appendChild(cell10);
			
			
			 tabBody_card_body.appendChild(row);//appending a row into tbody which contains textareas only
			tabBody_card.appendChild(tabBody_card_body);//appending tbody into table
			 cell01.appendChild(tabBody_card);
			 row01.appendChild(cell01);
			 tabBody1_body.appendChild(row01);
			 
			 
			 row02=document.createElement("TR");
			 cell02=document.createElement("TD");
			 cell02.align="center";
			 var browser_type=getbrowsertype();
	if(browser_type=="firefox")
	{
		 addcard=document.createElement("Input");
			 addcard.type="button";
			 addcard.value="Add More Cards";
			 addcard.id="add_more_cards";
			 addcard.name="add_more_cards";
			 addcard.align="center";
			var temp="addRow(this,"+addCategory.cat_rownumber+");";
			 addcard.setAttribute("onClick", temp); 
			  temp="";
	}
	else
	{  
		addcard=document.createElement("<Input type='button' value='Add More Cards' name='add_more_cards'  align='center' id='add_more_cards'>");
		var id_no=addCategory.cat_rownumber;
		addcard.onclick=function (){ addRow(this,id_no) }	
		//alert(addCategory.cat_rownumber);
		//alert( addcard.outerHTML);
		
	}
					
			// row_no=addCategory.cat_rownumber;
			// addcard.onclick="addRow(this,'rownumber')";

			//alert(temp);
			//addcard.onClick=function() {addRow(this,addCategory.cat_rownumber)}
			
			 cell02.appendChild(addcard);
			 row02.appendChild(cell02);
			 tabBody1_body.appendChild(row02);
			tabBody1.appendChild(tabBody1_body);
			 	
			 categorydiv=document.getElementById("divCategory");
			
			 categorydiv.appendChild(tabBody1);
			
			
			 //alert(document.getElementById("aa").outerHTML);
			document.getElementById("txt_hidden_count").value=addCategory.cat_rownumber;
//var a="categorytable"+addCategory.cat_rownumber;
//alert(document.getElementById(a).innerHTML);
			//var x="categorytable"+addCategory.cat_rownumber;
			//alert(x);
 //alert(document.getElementById("categorytable"+addCategory.cat_rownumber).innerHTML);

addCategory.cat_rownumber=addCategory.cat_rownumber+1;
			
			 //alert(tabBody1.innerHTML);
			 
								
		 }
		
		 
} //End of the addCategory() function
function addRow(src,rownumber)
{
		var id="cardstable_body"+rownumber;
if(typeof addRow.id == 'undefined')
	addRow.id=1;
		tabBody=document.getElementById(id);
                row=document.createElement("TR");
		cell1 = document.createElement("TD");
	
		cell1.id="td_seq_no"+eval(addRow.id++);

		cell2 = document.createElement("TD");
		cell2.id="td_seq_no"+eval(addRow.id++);
		
		cell3 = document.createElement("TD");
		cell3.id="td_seq_no"+eval(addRow.id++);
	
		cell4 = document.createElement("TD");
		cell4.id="td_seq_no"+eval(addRow.id++);
		
		
		
			 textnode1=document.createElement("Textarea");
			 textnode1.name="textarea"+rownumber +"[]";
			textnode1.id="textarea"+rownumber +"[]";
			 textnode1.style.width="170px";
			 textnode1.style.height="63px";
	 		// textnode1.rows="4";
			textnode1.style.overflow="hidden";
			var browser_type=getbrowsertype();
			if(browser_type == "firefox")
				{  
					textnode1.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() { return alertMsgLength(this,this.parentNode.id,event); };
					textnode1.onkeypress=jsfunction;
					
				}
			 			
			  
			 textnode2=document.createElement("Textarea"); 
			 textnode2.name="textarea"+rownumber +"[]";
			textnode2.id="textarea"+rownumber +"[]";
			 textnode2.style.width="170px";
			 textnode2.style.height="63px";
			 //textnode2.rows="4";
			 textnode2.style.overflow="hidden";
			if(browser_type == "firefox")
				{  
					textnode2.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() { return alertMsgLength(this,this.parentNode.id,event); };
					textnode2.onkeypress=jsfunction;
					
				}
			 			
			 
			 
			 textnode3=document.createElement("Textarea"); 
			 textnode3.name="textarea"+rownumber +"[]";
			textnode3.id="textarea"+rownumber +"[]";
			 textnode3.style.width="170px";
			 textnode3.style.height="63px";
			 //textnode3.rows="4";
			 textnode3.style.overflow="hidden";
			 if(browser_type == "firefox")
				{  
					textnode3.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() { return alertMsgLength(this,this.parentNode.id,event); };
					textnode3.onkeypress=jsfunction;
					
				}
			 			
			 
			 textnode4=document.createElement("Textarea"); 
			 textnode4.name="textarea"+rownumber +"[]";
			textnode4.id="textarea"+rownumber +"[]";	
			 textnode4.style.width="170px";
			 textnode4.style.height="63px";
			// textnode4.rows="4";
			textnode4.style.overflow="hidden";
			if(browser_type == "firefox")
				{  
					textnode4.setAttribute("onKeyPress","return alertMsgLength(this,this.parentNode.id,event)"); 
				}
				else
				{
					var jsfunction = function() { return alertMsgLength(this,this.parentNode.id,event); };
					textnode4.onkeypress=jsfunction;
					
				}
			 			
			
			cell1.appendChild(textnode1);
			cell2.appendChild(textnode2);
			cell3.appendChild(textnode3);
			cell4.appendChild(textnode4);
			
			 row.appendChild(cell1);
			 row.appendChild(cell2);
			 row.appendChild(cell3);
			 row.appendChild(cell4);
			 tabBody.appendChild(row);
			
			
			//alert("CARD TABLE "+rownumber+document.getElementById("cardstable"+rownumber).innerHTML);
}//end of addrow() function
function delete_category_fun(src)			 	
{
//alert(src);
	var index;
	if(confirm("Are you sure to delete category"))
	{
	var delete_button_id=src.id;
	var str=delete_button_id;
	var pattern=/\d{1,2}$/;
	//var p=/\d/g.exec(str);
	var p=str.match(pattern);
	var tableid="categorytable"+p; 
	var table_id=document.getElementById(tableid);
	var div_id=document.getElementById("divCategory");
	div_id.removeChild(table_id);
	//alert("Category deleted");
	document.getElementById('txt_hidden_min_category').value="";
	}

}
function getbrowsertype()
{
		var BrowserAgent;
		BrowserAgent=navigator.userAgent;
		
		var BrowserType="";
		try
		{							
			if((BrowserAgent.indexOf("Firefox/1")!=-1) || (BrowserAgent.indexOf("Firefox/2")!=-1) || (BrowserAgent.indexOf("Firefox/3")!=-1))
			{
			BrowserType="firefox";

			}
			else
			{
			BrowserType="others";
			}
			
		}
		catch(Exception)
		{
		BrowserType="others";
		}
		return BrowserType;
						
}
//Following function for checking maximum characters length  for card informations
function alertMsgLength(src,parent,e)
{  
	//alert(e);
	if(window.event) // For IE
                 keynum = e.keyCode;
        else if(e.which)//For Netscape/firefox/opera
       		keynum=e.which;
 	//alert(keynum);
	
	var str=document.getElementById(parent).getElementsByTagName("textarea");
	//alert(str[0].value);
	var strMsg;
	strMsg=str[0].value;
	
	//alert(strMsg[2]);
	//document.getElementById("txtHint").style.display="none";
	//alert(document.getElementById(src));
	//50--->only card informations with 50 characters are allowed  
	//str[0]--->only one   textarea tag is available in TD in table
	
	if(strMsg.length >= 60)
	{
	
	strMsg[strMsg.length ]=" ";
	document.getElementById(parent).getElementsByTagName("textarea").value=strMsg.substring(0,61);
	document.getElementById(parent).getElementsByTagName("textarea").focus;
	//alert(strMsg.substring(0,51));
	//document.getElementById(src).focus();
	if(keynum==8)//8 is ASCII  value for Backspace key
	{		
           return true;
	}
	
	return false;
	}
	
	//strLength=document.getElementById('txt_msg').value.length;
	//document.getElementById('txt_count').value=80-strLength+' characters left ';
}

function display_color(src)//This function triggered when mouseover event occured for category table
{
	
	document.getElementById(src).style.backgroundColor='#FEF4DE';
}
function hide_color(src)//This function triggered when mouseout event occured for category table 
{
	document.getElementById(src).style.backgroundColor='white';
}
