var data;
var data1;
var tempdata1Div;
var tempdataDiv;
var tempUrl;

dojo.require("dijit.Dialog");
dojo.require("dijit.layout.ContentPane");
var addTestSeriesDialog = null;
var editTestSeriesDialog = null;
var editTestDialog = null;
var shareTestDialog = null;
var addTestDialog = null;
var testStatusDialog = null;
var testOmrTemplateDialog = null;
var videoStringDialog = null;
dojo.addOnLoad(function(){
	addTestSeriesDialog = new dijit.Dialog({title:'New Test Series'});
	editTestSeriesDialog = new dijit.Dialog({title:'Edit Test Series Information'});
	editTestDialog = new dijit.Dialog({title:'Edit Test Schedules'});
	shareTestDialog = new dijit.Dialog({title:'Share Test'});
	listTestDialog = new dijit.Dialog({title:'Test List'});
	addTestDialog = new dijit.Dialog({title:'Add New Test'});
	profileDialog = new dijit.Dialog({title:'Profiledd'});
	editProfileDialog = new dijit.Dialog({title:'Edit Profile'});
	moveTestSeriesDialog = new dijit.Dialog({title:'Move test series to another course'});
	openTestProfileDialog = new dijit.Dialog({title:'Test Profile'});
	editLocationDialog=new dijit.Dialog({title:'Edit Location'});
	editTabDialog=new dijit.Dialog({title:'Edit Tab'});
	editTabPortletDialog=new dijit.Dialog({title:'Edit Tab Portlet'});
	editFeeCollectionDialog=new dijit.Dialog({title:'Edit Fee Collection'});
	addNewGenericLibraryDialog=new dijit.Dialog({title:'Add New'});
	testStatusDialog=new dijit.Dialog({title:''});
	testOmrTemplateDialog=new dijit.Dialog({title:'Edit Omr Template'});
	videoStringDialog=new dijit.Dialog({title:''});
});

var getCheckedParenVersions = function() {
	var a = new Array();
	var eList = dojo.query(".parent_version_id");	
	for(var i=0;i<eList.length;i++) {
		node= eList[i];
		if(node.checked)
			a.push(node.value);
	}
	return a;
}
//example onclick="openDialog(addTestSeriesDialog,'/abc.htm');"
var openDialog = function(d,h) {
	if(h)
	    d.attr('href',h);
	d.show();
} 
function showDialog(title,content) {
		dialog=new dijit.Dialog({
	 	content: content,
		title: title,
		style: "width:500px;",
		onCancel: function() {
            closeVRModelBox();
            }
    });
	dialog.show();
}
   function editTestSeries(courseId)
	{
		
		tempdataDiv="editCourseDiv_"+courseId;
		data =  document.getElementById("editCourseDiv_"+courseId).innerHTML;
		document.getElementById("editCourseDiv_"+courseId).innerHTML="";
		if(data==null || data=='')
			data = "<div>No details available</div>";
		editTestSeriesDialog.attr('content',data); editTestSeriesDialog.show();
		
	}
	function moveTestSeries(courseId)
	{
		if(courseId>0)
		{
			var url = "/adminCollege/attachTestSeries/"+courseId+".htm?type=1";
				AjaxRequest.get(
				{
					'url':url
					,'onSuccess':function(req)
					{
						data = req.responseText;
						if(data==null || data=='')
						data = "<div>No Data available</div>";
						moveTestSeriesDialog.attr('content',data);
						moveTestSeriesDialog.show();
					}
				  }
				);
		}
		
	}

	function addNewGenericLibrary(entityType, personName)
	{
		var url = "/library/addNew/"+entityType+".htm?personName=" + escape(personName);
		AjaxRequest.get(
		{
			'url':url
			,'onSuccess':function(req)
			{
				data = req.responseText;
				if(data==null || data=='')
					data = "<div>No Data available</div>";
					addNewGenericLibraryDialog.attr('content',data);
					addNewGenericLibraryDialog.show();
			}
		}
		);
	}
	function openVideoString(openUrl)
	{
		var url = openUrl;
		AjaxRequest.get(
		{
			'url':url
			,'onSuccess':function(req)
			{
				data = req.responseText;
				if(data==null || data=='')
					data = "<div>No Data available</div>";
					videoStringDialog.attr('content',data);
					videoStringDialog.show();
			}
		}
		);
	}
	function editTestOmrTemplate(versionId,testId)
	{
		var url = "/tests/editTestOmrTemplate/"+testId+"/"+versionId+".htm";
		AjaxRequest.get(
		{
			'url':url
			,'onSuccess':function(req)
			{
				data = req.responseText;
				if(data==null || data=='')
					data = "<div>No Data available</div>";
					testOmrTemplateDialog.attr('content',data);
					testOmrTemplateDialog.show();
			}
		}
		);
	}
	
	function editLocation(locId,appId)
	{
		if(locId>0)
		{
			var url = "/customTest/editLocation/"+locId+"/"+appId+".htm";
				AjaxRequest.get(
				{
					'url':url
					,'onSuccess':function(req)
					{
						data = req.responseText;
						if(data==null || data=='')
						data = "<div>No Data available</div>";
						editLocationDialog.attr('content',data);
						editLocationDialog.show();
					}
				  }
				);
		}
		
	}
	function editTab(tabId)
	{
		if(tabId>0)
		{
			var url = "/adminCollege/editTab/"+tabId+".htm";
				AjaxRequest.get(
				{
					'url':url
					,'onSuccess':function(req)
					{
						data = req.responseText;
						if(data==null || data=='')
						data = "<div>No Data available</div>";
						editTabDialog.attr('content',data);
						editTabDialog.show();
					}
				  }
				);
		}
		
	}
	function editTabPortlet(tabPortletId)
	{
		if(tabPortletId>0)
		{
			var url = "/adminCollege/editTabPortlet/"+tabPortletId+".htm";
				AjaxRequest.get(
				{
					'url':url
					,'onSuccess':function(req)
					{
						data = req.responseText;
						if(data==null || data=='')
						data = "<div>No Data available</div>";
						editTabPortletDialog.attr('content',data);
						editTabPortletDialog.show();
					}
				  }
				);
		}
		
	}
	function editFeeCollection(collectionId)
	{
		if(collectionId>0)
		{
			var url = "/feeManage/editFeeCollection/"+collectionId+".htm";
				AjaxRequest.get(
				{
					'url':url
					,'onSuccess':function(req)
					{
						data = req.responseText;
						if(data==null || data=='')
						data = "<div>No Data available</div>";
						editFeeCollectionDialog.attr('content',data);
						editFeeCollectionDialog.show();
					}
				  }
				);
		}
		
	}
	function attachTestSeries(courseId)
	{
		if(courseId>0)
		{
			var url = "/adminCollege/attachTestSeries/"+courseId+".htm?type=2";
				AjaxRequest.get(
				{
					'url':url
					,'onSuccess':function(req)
					{
						data = req.responseText;
						if(data==null || data=='')
						data = "<div>No Data available</div>";
						moveTestSeriesDialog.attr('content',data);
						moveTestSeriesDialog.show();
					}
				  }
				);
		}
		
	}
	
	function getProfile(userId)
       {
		   if(userId>0)
		   {
		    var url = "/base/userProfile/"+userId+".htm";
		    AjaxRequest.get(
		    {
		        'url':url
		        ,'onSuccess':function(req)
		        {
		            data = req.responseText;
					if(data==null || data=='')
					data = "<div>No Data available</div>";
				    profileDialog.attr('content',data);
					profileDialog.show();
		        }
		      }
		    );
		  }
           }
         function getProfilePage(userId)
            {
            	profileDialog.hide();
		   if(userId>0)
		   {
		    var url = "/user/editUser/"+userId+".htm";
		    AjaxRequest.get(
		    {
		        'url':url
		        ,'onSuccess':function(req)
		        {
		             data = req.responseText;
					if(data==null || data=='')
					data = "<div>No Data available</div>";
				    editProfileDialog.attr('content',data);
					editProfileDialog.show();
		        }
		      }
		    );
		  }
                }  
	function addTestSeries(divId)
	{
		 tempdata1Div=divId;
		 data1 =  document.getElementById(divId).innerHTML;
		document.getElementById(divId).innerHTML="";
		if(data1==null || data1=='')
			data1 = "<div>No details available</div>";
	
		addTestSeriesDialog.attr('content',data1); addTestSeriesDialog.show();

	}
	function openTest(linkUrl)
	{
		tempUrl=linkUrl;
	        editTestDialog.attr('href',linkUrl); editTestDialog.show();
	}
	function showTest(linkUrl)
	{
		tempUrl=linkUrl;
	        listTestDialog.attr('href',linkUrl); listTestDialog.show();
	}
	
	function shareTest(linkUrl)
	{
		tempUrl=linkUrl;
	        shareTestDialog.attr('href',linkUrl); shareTestDialog.show();
	}
  function openTestForm(linkUrl)
	{
		addTestDialog.attr('href',linkUrl); addTestDialog.show();
		editTestDialog.hide();	
	} 
  function closeAddTest(divId)
         {
         	document.getElementById(divId).innerHTML=data1;
         	addTestSeriesDialog.hide();
         }
   function closeEditTest(divId)
         {
         	document.getElementById('editCourseDiv_'+divId).innerHTML=data;
         	editTestSeriesDialog.hide();
         }      		
function closeVRModelBox() {
if(document.getElementById(tempdata1Div)!=null)
   		   document.getElementById(tempdata1Div).innerHTML=data1;
if(document.getElementById(tempdataDiv)!=null)
   		   document.getElementById(tempdataDiv).innerHTML=data;		   
    if(dialog!=null) {
        dialog.destroyRecursive();
        dialog=null;
    }	
}
var postTestSeriesData=function(content)
	 {
	   var xhrArgs={
		url : '/goiit.htm',
		method : 'post',
		content: content,
		handleAs: 'text',
		load : function(data) {
			testSeriesList.refresh();
			addTestSeriesDialog.hide();
		},
		error : function(error) { alert(error); }
		};
		var defered = dojo.xhrPost(xhrArgs);	
	 }
var postData=function(content,divId)
	 {
         	 
	   var xhrArgs={
		url : '/goiit.htm',
		method : 'post',
		content: content,
		handleAs: 'text',
		load : function(data) {
			
			if(data.status==1)
		     alert('Test is already exists');
		  else	
			alert('Test Successfully added');
			openTest(tempUrl);
			addTestDialog.hide();
		},
		error : function(error) { alert(error); }
		};
		var defered = dojo.xhrPost(xhrArgs);	
	 }
var shareTestData=function(content)
	 {
         	 
	   var xhrArgs={
		url : '/goiit.htm',
		method : 'post',
		content: content,
		handleAs: 'json',
		load : function(data) {
		  if(data.status==1)
		     alert('Test is already exists');
		  else	
			alert('Test Successfully Shared');
		  	
			shareTestDialog.hide();
			editTestDialog.hide();
		},
		error : function(error) { alert(error); }
		};
		var defered = dojo.xhrPost(xhrArgs);	
	 }	 
	 
	 var openTestSeriesEditor = function(courseId,vendorId,testSeriesId) {
	 			dojo.require("vui.widgets.TestSeriesEditor");
	 			var dialog = new dijit.Dialog({title:"Edit Test Series Information",onCancel: function() {testSeriesList.refresh(); this.destroyRecursive();}});
	 			var editor = new vui.widgets.TestSeriesEditor({courseId:courseId,vendorId:vendorId,testSeriesId:testSeriesId});
	 			dialog.containerNode.appendChild(editor.domNode);
	 			dialog.show();
	 	}
		
function checkStatus(url)
{	
 var data3 = null;
	AjaxRequest.get(
    {
        'url':url
        ,'onSuccess':function(req)
        {
	           data3=req.responseText;
			   testStatusDialog = new dijit.Dialog({title:'Test Status'});
			   testStatusDialog.attr('content',data3);
		       testStatusDialog.show();
        }
      }
    );
	    		
}		 
function checkStatus(url,pageTitle)
{	
	testStatusDialog.set('title',pageTitle);
	testStatusDialog.set('href',url);	    
	testStatusDialog.show();
}	
 	function filterByVersion(versionId,selectedVersionId)
     {
 		
 		  var url='/adminCollege/insertVendorCourseTest/'+selectedVersionId+'.htm?testVersionId='+versionId;
 		  openTestForm(url);
 	
     }

