//function for added the puzzle

function fnc_validate(uid)
{
	
	var oEditor = FCKeditorAPI.GetInstance('puzzlename') ;
   // Get the Editor Area DOM (Document object).
	var oDOM = oEditor.EditorDocument ;
	var iLength ;
	// The are two diffent ways to get the text (without HTML markups).
	// It is browser specific.
	if ( document.all )		// If Internet Explorer.
	{
		iLength = oDOM.body.innerText.length ;
		//alert(iLength);
	}
	else					// If Gecko.
	{
		var r = oDOM.createRange() ;
		r.selectNodeContents( oDOM.body ) ;
		iLength = r.toString().length ;
		//alert(iLength);
	}
	var a = FCKeditorAPI.GetInstance('puzzlename').EditorWindow.parent.FCK.GetHTML() ; 
	//var EditorInstance = FCKeditorAPI.GetInstance(‘puzzlename’) ; 
	var id1=document.getElementById('puzzle_title').value;
	//var id2=document.getElementById('puzzlename').value;
	var id3=document.getElementById('puzzleans').value;
	var id4=document.getElementById('puzzlelevel').value;
if(id1=='')
{
alert("Please Fill The Title First");
document.getElementById('puzzle_title').focus(); 
return false;
}
//message is name of field to be validate
else if(iLength==0)
{

alert("Please fill the Question First");
oEditor.EditorDocument.body.focus();
return false;
}
/*else if(id2=='')
{
	alert(document.getElementById('puzzlename').value);
alert("Please Fill The Question First");
document.getElementById('puzzlename').focus();
return false;
}*/
else if(id3=='')
{
alert("Please Fill The Answer First");
document.getElementById('puzzleans').focus(); 
return false;
}
else if(id4=='')
{
alert("Please Fill The Level First");
document.getElementById('puzzlelevel').focus(); 
return false;
}
else
{
	xmlHttp	=	GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  		alert ("Your browser does not support AJAX!");
  		return;
  		} 
		
		//var url="../back.php";
		var url="back.php";
		url=url+"?pt="+id1;
		url=url+"&pn="+a;
		url=url+"&pa="+id3;
		url=url+"&pl="+id4;
		url=url+"&uid="+uid;
		//alert(url);
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
return true;
}
/**************To check the login*******************/
function fnc_check()
{
alert("You need to be login for use this functionality");	
}
/***********Delete the puzzle********************************/
function fnc_delete(pid)
{
	if (confirm("Are you sure you want to delete")) {
	//alert(pid);
	xmlHttp	=	GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  		alert ("Your browser does not support AJAX!");
  		return;
  		} 
		var url="backdel.php";
		url=url+"?pid="+pid;
		xmlHttp.onreadystatechange=stateChanged2;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
/**********load the page ********/
function refresh1()
{
    window.location.reload( false );
}

function stateChanged2() 
	{
  		if (xmlHttp.readyState==4)
		{
			alert(xmlHttp.responseText);
			refresh1();
			//document.getElementById("showdiv").innerHTML=xmlHttp.responseText;
 		}
	}

function fnc_approve(pid,uid,status)
{
	
	xmlHttp	=	GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  		alert ("Your browser does not support AJAX!");
  		return;
  		} 
		var url="approveback.php";
		url=url+"?pid="+pid;
		url=url+"&uid="+uid;
		url=url+"&status="+status;
		//alert(url);
		xmlHttp.onreadystatechange=stateChanged1;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}

function stateChanged1() 
	{
  		if (xmlHttp.readyState==4)
		{
			alert(xmlHttp.responseText);
			//refresh1();
			//document.getElementById("showdiv").innerHTML=xmlHttp.responseText;
 		}
	}
function stateChanged() 
	{
  		if (xmlHttp.readyState==4)
		{
			//alert(xmlHttp.responseText);
			document.getElementById('puzzle_title').value='';
			//FCKeditorAPI.GetInstance('puzzlename').EditorWindow.parent.FCK.GetHTML()=''; 
			document.getElementById('puzzlename').value='';
			document.getElementById('puzzleans').value='';
			document.getElementById('puzzlelevel').value='';
			alert("Puzzle Added Successfully");
			
			closebox();
			refresh1();
			//document.getElementById("showdiv").innerHTML=xmlHttp.responseText;
 		}
	}
function GetXmlHttpObject()
	{
	var xmlHttp=null;
	try
  	{
  	// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  	// Internet Explorer
 	 try
    {
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  	catch (e)
    {
    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  	}
	return xmlHttp;
	}	
function gradient(id, level)
{
	var box = document.getElementById(id);
	box.style.opacity = level;
	box.style.MozOpacity = level;
	box.style.KhtmlOpacity = level;
	box.style.filter = "alpha(opacity=" + level * 100 + ")";
	box.style.display="block";
	return;
}


function fadein(id) 
{
	var level = 0;
	while(level <= 1)
	{
		setTimeout( "gradient('" + id + "'," + level + ")", (level* 1000) + 10);
		level += 0.01;
	}
}


// Open the lightbox


function openbox(formtitle, fadin)
{
	
 var box = document.getElementById('box'); 
  document.getElementById('filter').style.display='block';

  var btitle = document.getElementById('boxtitle');
  btitle.innerHTML = formtitle;
  
  if(fadin)
  {
	 gradient("box", 0);
	 fadein("box");
  }
  else
  { 	
    box.style.display='block';
	//box.innerHTML='Test of the form';
  }  	
}
//set the edit puzzle form inner html
function openboxfinal(editformvalue,fadin)
{
	
 var box = document.getElementById('box'); 
  //document.getElementById('filter').style.display='block';

  //var btitle = document.getElementById('boxtitle');
  //btitle.innerHTML = formtitle;
  
  if(fadin)
  {
	 gradient("box", 0);
	 fadein("box");
  }
  else
  { 
  	//alert("I am in the openboxfinal");
    box.style.display='block';
	//box.innerHTML.= ;
	<!-- <form name="editpuzzle" id="puzzle" action="#" method="post">-->
	box.innerHTML=editformvalue;
	//box.innerHTML.='</form>';
	}  	
}
//edit the puzzle
function openboxedit(formtitle, fadin,pid)
{
	
		//alert(pid);
		//alert(formtitle);
		//alert(fadin);
		xmlHttp	=	GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  		alert ("Your browser does not support AJAX!");
  		return;
  		} 
		var url="editback.php";
		url=url+"?pid="+pid;
		//alert(url);
		//url=url+"&status="+status;
		xmlHttp.onreadystatechange=stateChanged3;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
function stateChanged3() 
	{
  		if (xmlHttp.readyState==4)
		{
			  
			//alert(xmlHttp.responseText);
			openboxfinal(xmlHttp.responseText);
			//alert(document.getElementById("box").value);
			//alert("Responce for Div");
			//alert(xmlHttp.responseText);
			//document.getElementById("box").innerHTML=xmlHttp.responseText;
			
 		}
	}

// Close the lightbox

function closebox()
{
   document.getElementById('box').style.display='none';
   document.getElementById('filter').style.display='none';
}
function closeboxedit()
{	
	/*var oEditor = FCKeditorAPI.GetInstance('puzzlename') ;
	alert(oEditor);
	// Get the Editor Area DOM (Document object).
	var oDOM = oEditor.EditorDocument ;
	var iLength ;
	// The are two diffent ways to get the text (without HTML markups).
	// It is browser specific.
	if ( document.all )		// If Internet Explorer.
	{
		iLength = oDOM.body.innerText.length ;
		//alert(iLength);
	}
	else					// If Gecko.
	{
		var r = oDOM.createRange() ;
		r.selectNodeContents( oDOM.body ) ;
		iLength = r.toString().length ;
		//alert(iLength);
	}*/
	
	document.getElementById('puzzle_title').value='';
	//oEditor.EditorDocument.body.value=='';
	document.getElementById('puzzleans').value='';
	document.getElementById('puzzlelevel').value='';
   document.getElementById('box').style.display='none';
   document.getElementById('filter').style.display='none';
}




