//To Show MyCart page
function fnAddItemsToCart(hdnBoxID,TargetMenu,alertstmt,treeId)
{    
	if(treeId==null || treeId=='')
		var canProceed=fnCheckForSelectedItems(hdnBoxID,alertstmt);
	else
		var canProceed=fnCheckSelectedItemsOfaTree(treeId,hdnBoxID,alertstmt);
	if(canProceed==true)
	{
		var ObjectIDList=document.getElementById(hdnBoxID).value;
		document.getElementById(hdnBoxID).value="";
		AddToCart(0,ObjectIDList,TargetMenu);
		
	}
	return false;
}
function fnPurchaseItemsToCart(hdnBoxID,TargetMenuID,alertstmt,UserAccountType,treeId)
{
	if(treeId==null || treeId=='')
		var canProceed=fnCheckForSelectedItems(hdnBoxID,alertstmt);
	else
		var canProceed=fnCheckSelectedItemsOfaTree(treeId,hdnBoxID,alertstmt);
	if(canProceed==true)
	{
		var ObjectIDList=document.getElementById(hdnBoxID).value;
		document.getElementById(hdnBoxID).value="";
		PurchaseItem(ObjectIDList,TargetMenuID,UserAccountType);
	}
	return false;
}
function fnSubscribe(hdnBoxID,alertstmt,comInstID,treeId,hdnRadioID)
{

        var canProceed=true;
	    if(treeId==null || treeId=='')
	    {
	        if(hdnRadioID!=null && hdnRadioID!='')
	        {
	            if(document.getElementById(hdnRadioID).value == "")
	            {
	                canProceed=fnCheckForSelectedItems(hdnBoxID,alertstmt);
	            }
	        }
	        else
	        {
	          canProceed=fnCheckForSelectedItems(hdnBoxID,alertstmt);
	        }
	      
		}
		else
			 canProceed=fnCheckSelectedItemsOfaTree(treeId,hdnBoxID,alertstmt);
		if(canProceed==true)
		{
		try
		  {
			// suresh
			var RadAjaxObjx = this.GetAjaxPanelInstance();
			var ET = comInstID+"_subscribe";
			var EA   = document.getElementById(hdnBoxID).value;			
			//	document.forms(0).__EVENTTARGET.value=comInstID+"_subscribe";
			//	document.forms(0).__EVENTARGUMENT.value=document.getElementById(hdnBoxID).value;
			document.getElementById(hdnBoxID).value="";
				//document.forms(0).submit();
			fnAjaxPostBack(ET,EA,RadAjaxObjx);
		  }
		 catch(err)
		   {
		 
			document.forms(0).__EVENTTARGET.value=comInstID+"_subscribe";
			var EA ;
		    if(hdnRadioID!=null && hdnRadioID!='')
            {
               EA=document.getElementById(hdnBoxID).value + ":" + document.getElementById(hdnRadioID).value ;
               document.getElementById(hdnRadioID).value="";
            }
            else
            {
                EA=document.getElementById(hdnBoxID).value;
            }
			document.forms(0).__EVENTARGUMENT.value=EA;
			document.getElementById(hdnBoxID).value="";
		    document.forms(0).submit();
		   }		
		}
		return false;
}

function fnSubscribeClick(cid)
{
    //document.forms(0).__EVENTTARGET.value=cid + "_lnkSubscribe";
    //document.forms(0).__EVENTARGUMENT.value=cid;
    fnUpdateConditionList('action,cid','subscribe,' + cid,strClientID);	
	document.getElementById(strClientID+"dummylink").click();
	//document.location.href = document.getElementById(strClientID+"dummylink").href;
}

function AddToCart(compInstID,ContentID,TargetMenuID,FilterID)
{
      // '--- Added by Chaitanya ---- 
    var qs=''
	if(FilterID != '' && FilterID != null)
	{
		qs='id='+ FilterID+'&'+'mode=list';
	}
	document.forms[0].__EVENTTARGET.value="AddToCart";
      
	document.forms[0].__EVENTARGUMENT.value=ContentID;
	document.forms[0].__VIEWSTATE.disabled=true;
	/*if(qs=='')
	    document.forms(0).action =AppURL+"/Modules/ManageCart.aspx";
	else
	     document.forms(0).action =AppURL+"/Modules/ManageCart.aspx?"+qs;*/
	     //document.forms(0).action =AppURL+"/ModulePages/ManageCart.aspx"
document.forms[0].submit();

}

function PurchaseItem(ContentID,TargetMenuID,UserAccountType)
{		
	document.forms(0).__EVENTTARGET.value="PurchaseItem";
	document.forms(0).__EVENTARGUMENT.value=ContentID;
	document.forms(0).__VIEWSTATE.disabled=true;
	var isUserLoggedIn=false;
	var PresentURL=document.location.href.toLowerCase();
	var secureURL=AppURL+'/';
	isUserLoggedIn=(PresentURL.indexOf('default.aspx')>0?true:false)
	var strPage='ModulePages/ManageCart.aspx';
	if(document.location.pathname.toLowerCase().indexOf('start.aspx')>0)
	strPage='ModulePages/ManageCart.aspx';
    secureURL=secureURL + strPage;
	if(UserAccountType.toLowerCase()!='multilogin')
	{
		if(isUserLoggedIn)
		{
			secureURL=secureURL.replace("http:","https:");
			secureURL=secureURL.replace("/" + strPage ,"/secure/" + strPage);	
		}
		//var questionLoc=secureURL.indexOf("?");
		//secureURL=secureURL.substring(0,questionLoc)	
		document.forms(0).action =secureURL + "?action=checkout"; 
		document.forms(0).submit();
		return false;	
	}
	else	
	{
		//var questionLoc=secureURL.indexOf("?");
		//secureURL=secureURL.substring(0,questionLoc)	
		document.forms(0).action =secureURL + "?action=checkout"; 
		document.forms(0).submit();
	}
}

function fnBookmarkItemsOfTree(hdnBoxID,alertstmt,treeId)
{
	if(treeId==null || treeId=='')
		var canProceed=fnCheckForSelectedItems(hdnBoxID,alertstmt);
	else
		var canProceed=fnCheckSelectedItemsOfaTree(treeId,hdnBoxID,alertstmt);
	if(canProceed==true)
	{
		AddBookMark(document.getElementById(hdnBoxID).value);
		document.getElementById(hdnBoxID).value="";
	}
	return false;
}


//To show add Ratings page		 
function AddRatings(ContentID,ContentTitle,CanRefresh)
{ 
	if(ContentTitle!=null)
		popupwindow=window.open(AppURL+"/Modules/AddRatings.aspx?ContentID="+ContentID+"&ContentTitle="+ContentTitle,"AddRatings","height=480,width=600,status=no,toolbar=no,resizable=yes,menubar=no,scrollbars=yes,left=30,top=50");
	else
		popupwindow=window.open(AppURL+"/Modules/AddRatings.aspx?ContentID="+ContentID,"AddRatings","height=480,width=600,status=no,toolbar=no,resizable=yes,menubar=no,scrollbars=yes,left=30,top=50");
	popupwindow.focus();
}



//To show Bookmark popup
function fnBookmarkItems(hdnBox)
{
	if(document.getElementById(hdnBox).value=="")
	{
		alert(alrtSelectContent);
		return false;
	}
	AddBookMark(document.getElementById(hdnBox).value);
	return false;
}

//To launch content item from catalog  page without tracking(authoring tools items)
function fnGeneratePreview(ApplicationPath,SCOID,Path,ObjectTypeID,ContentID,WinProperties,mode,track,eventkey,urltype)
{
		if(eventkey==null)	eventkey='';
		if(urltype==null) 	urltype='';
		if(track == null || track == '') fnTrackLinkClicks(SCOID,ApplicationPath);
		var targetURL;
		Path = escape(Path);
		if (WinProperties==null) WinProperties='';
		targetURL=ApplicationPath + '/PublicModules/KSPreview.aspx?Path=' + Path + '&ObjectTypeID=' + ObjectTypeID+ '&ContentID=' + ContentID+ '&CanTrack=No&eventkey='+eventkey+'&urltype='+ urltype;
		var winid=replaceAll(ContentID,"-","_") + "view" + urltype;
		var chWin=window.open(targetURL,winid,WinProperties); 	
		chWin.focus();		
	
}
		
//To show user progress report
function ShowProgressReport(url,ContentID,ObjectTypeID,Name)
{
	document.forms(0).action = url;
	document.forms(0).submit();
}
//To show notes
function AddNotes(url)
{//alert(url);
	
	//document.location.href = url;
	document.getElementById(strClientID+"hdnConditionList").setAttribute("value",url.substring(url.indexOf("?")+1,url.length));
	document.getElementById(strClientID+"dummyLink").click();
	/*document.forms(0).action = url;
	document.forms(0).submit();*/
}	
function AddDetails(keys,values)
{
    fnUpdateConditionList(keys,values,strClientID);	
	//document.location.href = document.getElementById(strClientID+"dummylink").href;
	document.getElementById(strClientID+"dummylink").click();
}

function HomeDetailsClick(Url)
{
    //document.forms(0).action = Url;
   // document.forms(0).submit();
   document.location.href=Url;
}

function DetailsBreadCrumbClick(keys,values,ClientID)
{
 if (ClientID==null) ClientID=strClientID;
 /* var url = fnGetNewURL('',keys,values);
   url=fnRemoveFromURL('currentaction',url);
       document.forms(0).action = url;
	document.forms(0).submit();*/
	 fnUpdateConditionList(keys,values,ClientID);
	document.getElementById(ClientID+"dummylink").click();
}
//To Unassign the content from user mycatalog
function fnUnassignUserContent(ContentID,confirmstmt)
{
	var agree=confirm(confirmstmt);
	if(agree==true)
	{
		document.forms(0).__EVENTTARGET.value='UnassignUserContent';
		document.forms(0).__EVENTARGUMENT.value=ContentID;
		document.forms(0).action=fnRemoveFromURL('page',document.location.href);
		document.forms(0).submit();
	}
}
		

//To launch content item(third party objects) from my catalog page by enabling tracking	
function fnGeneratePreviewForMedia(Path,CourseName,ContentID,ObjectTypeID,SCOID,WinProperties,track,IsMyCatalog,mode,eventkey,eventtype,OrgsID)
{
	if(eventkey==null) eventkey='';
	if(eventtype==null) eventtype='';
	if (WinProperties==null) WinProperties='';
	if (track==null)
		 CanTrack='Yes';
	else 
		CanTrack=track;
	var targetURL;
	Path = escape(Path);
	if(document.getElementById(clientID + "_hdnAllowTracking") !=null)
	{
		if(IsMyCatalog==1)
		{
			document.getElementById(clientID + "_hdnAllowTracking").value="yes";
		}
		else if(IsMyCatalog==0)
		{
			document.getElementById(clientID + "_hdnAllowTracking").value="no";
		}
	}
	if(ObjectTypeID==97)
	{
	    targetURL=pathToAppend + 'Remote/CourseFrame.aspx?contentid=' + ContentID + '&SCOID=' + SCOID + '&orgsid=' + OrgsID + '&Path=' + Path;
	}
	else
	{
	    targetURL=pathToAppend + 'Remote/LaunchPage.aspx?Path=' + Path + "&CourseName=" + CourseName + '&ContentID=' + ContentID + '&ObjectTypeID=' + ObjectTypeID+ '&CanTrack='+ CanTrack + '&SCOID=' + SCOID+'&eventkey='+eventkey+'&eventtype='+eventtype;
    }	
	//alert(targetURL);	
    WinProperties = WinProperties + ',width=' + (screen.availWidth - 10).toString();
    WinProperties = WinProperties + ',height=' + (screen.availHeight - 122).toString();
    WinProperties = WinProperties + ',screenX=0,screenY=0,left=0,top=0';
	var winid=replaceAll(ContentID,"-","_") + "view";
	var chwin=window.open(targetURL,winid,WinProperties); 	
	chwin.focus();	
	chwin.moveTo( 0, 0 );
    chwin.resizeTo( screen.availWidth, screen.availHeight );


}

//To launch content item(CSW authored contents) from my catalog page by enabling tracking	
function ShowCourse(URL,CourseName,ContentID,ObjectTypeID,SCOID,WinProperties,IsMyCatalog,mode)
{
	
	if(WinProperties==null) WinProperties='';
	if(document.getElementById(clientID + "_hdnAllowTracking") !=null)
	{
		if(IsMyCatalog==1)
		{
			document.getElementById(clientID + "_hdnAllowTracking").value="yes";
		}
		else if(IsMyCatalog==0)
		{
			document.getElementById(clientID + "_hdnAllowTracking").value="no";
		}
	}
	var targetUrl = pathToAppend + "remote/LaunchPage.aspx?URL="+URL+ "&CourseName=" + CourseName + '&ContentID=' + ContentID + '&CanTrack=Yes&ObjectTypeID='+ ObjectTypeID + '&SCOID=' + SCOID;				

//alert(targetUrl);	
	
	 WinProperties = WinProperties + ',width=' + (screen.availWidth - 10).toString();
    WinProperties = WinProperties + ',height=' + (screen.availHeight - 122).toString();
    WinProperties = WinProperties + ',screenX=0,screenY=0,left=0,top=0';
	CourseWindowInstance=openWindow(ContentID,"view",targetUrl,WinProperties,"view");
	try
	{
	    CourseWindowInstance.moveTo( 0, 0 );
        CourseWindowInstance.resizeTo(screen.availWidth, screen.availHeight );
    }
    catch(e){}    
}

//To launch content item(CSW authored contents) from my catalog page by enabling tracking	
function ShowAssignment(CourseName,ContentID,ObjectTypeID,WinProperties)
{
	if(WinProperties==null) WinProperties='';
	var targetUrl = pathToAppend + "ModulePages/Assignment.aspx?CourseName=" + CourseName + '&ContentID=' + ContentID + '&ObjectTypeID='+ ObjectTypeID;				

//alert(targetUrl);	
	
	 WinProperties = WinProperties + ',width=' + (screen.availWidth - 10).toString();
    WinProperties = WinProperties + ',height=' + (screen.availHeight - 122).toString();
    WinProperties = WinProperties + ',screenX=0,screenY=0,left=0,top=0';    
    var newContentID = newGuid();
	CourseWindowInstance=openWindow(newContentID,"view",targetUrl,WinProperties,"view");
	try
	{
	    CourseWindowInstance.moveTo( 0, 0 );
        CourseWindowInstance.resizeTo(screen.availWidth, screen.availHeight );
    }
    catch(e){}    
}
	

/********* TO update the status of the contentitem in the parent page ,when the course is closed(my catalog) *********/

function ChkNetwork()
{ 
   document.body.style.cursor="default"; 
   CheckNetworkConnection();
   startTimerForNetworkConnection();
}

var timerID;
function startTimerForNetworkConnection()
{
	 timerID = setInterval("checkIfNetworkConnected()",1000)
}

function checkIfNetworkConnected()
{
    try{
        var hdnObj;
        hdnObj = document.getElementById("hdnCheckConnection");
        if(hdnObj != null){
		    if(hdnObj.value != ''){
		       clearInterval(timerID);
		       document.body.style.cursor="default";
		       if(hdnObj.value == 'false')
		       {//not connected 
				  alert("Your network connection seems to be lost!");
			   }
			   else if(hdnObj.value == 'true')
			   {//connected
			       try{				       
				    updateDatalistAfterLaunch();			    
				    }catch(ex){}
		       }	   
		    }
		 }		
	}catch(ex){}
}

var ContentID;

function updateDatalistAfterLaunch()
{ 
    document.body.style.cursor="default";
    if(document.getElementById(ContentID + "_reportbutton") != null)
        document.getElementById(ContentID + "_reportbutton").style.display="";
     if(document.getElementById("btnReport") != null)
         if(document.getElementById("btnReport").parentNode != null)
            document.getElementById("btnReport").parentNode.style.display="";
	var strVal='';
	try
	{
		strVal=API.GetStatus("");
	}
	catch(err)
	{
	}	
	if(strVal != null && strVal != '')
	{
	                var status=strVal.split(";")[0];
	                var datecompleted=strVal.split(";")[1];
	                var actualstatus=strVal.split(";")[2];
	                var startdate=strVal.split(";")[3];
	                var lastaccesseddate=strVal.split(";")[4];
	                if(status == 'Not Started')
	                    document.getElementById(ContentID + "_reportbutton").style.display="none";
	                if(datecompleted != '')
	                {
		                if(document.getElementById(ContentID + "_datecompleted") !=null)
		                {
			                document.getElementById(ContentID + "_datecompleted").innerHTML='&nbsp;'+ datecompleted;
		                }
		                else if(document.getElementById(ContentID.toLowerCase() + "_datecompleted") !=null)
		                {
			                document.getElementById(ContentID.toLowerCase() + "_datecompleted").innerHTML='&nbsp;'+ datecompleted;
		                }
	                }
	                if(document.getElementById(ContentID + "_corelessonstatus") !=null)
	                {
		                document.getElementById(ContentID+ "_corelessonstatus").innerHTML='&nbsp;'+ status;
	                }
	                else if(document.getElementById(ContentID.toLowerCase() + "_corelessonstatus") !=null)
	                {
		                document.getElementById(ContentID.toLowerCase() + "_corelessonstatus").innerHTML='&nbsp;'+ status;
	                }
	               
	                if( actualstatus=='completed' || actualstatus=='passed')//|| actualstatus=='failed' )
	                {
		                if(document.getElementById("remove_" + ContentID.toLowerCase()) !=null)
		                {
			                var parentstr=document.getElementById("remove_" + ContentID.toLowerCase()).parentElement.innerHTML;
			                var lengthOfStr=document.getElementById("remove_" + ContentID.toLowerCase()).outerHTML.length
			                var index=parentstr.indexOf(document.getElementById("remove_" + ContentID.toLowerCase()).outerHTML);
			                var firstpart=parentstr.substring(0,index-2);
			                var secondpart='';
			                if(parentstr.charAt(index + 1 + lengthOfStr)=='|')
				                secondpart=parentstr.substring(index + 1 + lengthOfStr);
			                document.getElementById("remove_" + ContentID.toLowerCase()).parentElement.innerHTML=firstpart+secondpart;
		                }	
		                if(document.getElementById("certificate_" + ContentID.toLowerCase()) !=null)
		                {
			                var certificateSpan=document.getElementById("certificate_" + ContentID.toLowerCase());
			                certificateSpan.style.display="";
		                }		
	                }
	                
	                if(document.getElementById(ContentID + "_startdate") !=null)
                    {
	                    document.getElementById(ContentID+ "_startdate").innerHTML='&nbsp;'+ startdate;
                    }
                    else if(document.getElementById(ContentID.toLowerCase() + "_startdate") !=null)
                    {
	                    document.getElementById(ContentID.toLowerCase() + "_startdate").innerHTML='&nbsp;'+ startdate;
                    }
                    
                    if(document.getElementById(ContentID + "_lastaccesseddate") !=null)
	                {
		                document.getElementById(ContentID+ "_lastaccesseddate").innerHTML='&nbsp;'+ lastaccesseddate;
	                }
	                else if(document.getElementById(ContentID.toLowerCase() + "_lastaccesseddate") !=null)
	                {
		                document.getElementById(ContentID.toLowerCase() + "_lastaccesseddate").innerHTML='&nbsp;'+ lastaccesseddate;
	                }
	                
	}
}



/******************** Hide Complete items function ****************/
function fnHideCompletItems(url,chkobj)
{
	
		try{
		if (chkobj.checked)
			url=url.replace('hidecompletevalue','true');
		else
			url=url.replace('hidecompletevalue','false');
		fnAjaxSubmit(url.substring(url.indexOf("?")+1),GetAjaxPanelInstance());
	}
	catch(ex){
		if (chkobj.checked)
			document.forms[0].action =url.replace('hidecompletevalue','true');
		else
			document.forms[0].action =url.replace('hidecompletevalue','false');
		document.forms[0].submit();
	}	     

}

function fnShowCertificate(CertID,CertPage,CID,SCOID)
{
	var chwin = window.open(AppURL + "/Modules/LogoPage.aspx?CertID=" + CertID + "&CertPage=" + CertPage + "&CID=" + CID + "&SCOID=" + SCOID,"Certificate","height=650,width=850,toolbar=no,menubar=no,resizable=no,scrollbars=no,left=30,top=50"); 					
}

function LaunchWebpages(applicationurl,ContentID,AddToCartMenuID,LaunchWindowSettings,CanTrack)
{
	//popWin=window.open(applicationurl + '/LaunchContent.aspx?CID=' + ContentID + '&addtocartmenu=' + AddToCartMenuID ,'', LaunchWindowSettings );
	var winid=replaceAll(ContentID,"-","_") + "view";
	if (CanTrack!=null)
	{
		var chwin=window.open(applicationurl + '/LaunchContent.aspx?CID=' + ContentID + '&addtocartmenu=' + AddToCartMenuID + '&cantrack=' + CanTrack,winid,LaunchWindowSettings); 	
		chwin.focus();
	}
	else
	{
		var chwin=window.open(applicationurl + '/LaunchContent.aspx?CID=' + ContentID + '&addtocartmenu=' + AddToCartMenuID,winid,LaunchWindowSettings); 	
		chwin.focus();
	}
}

/********* TO update the stars of the contentitem in the parent page ,when the rating has been changed *********/
function UpdateRatings(ContentID,ImageStr)
{ 
	if(document.getElementById(ContentID + "_rating") !=null)
	{
		document.getElementById(ContentID + "_rating").innerHTML=ImageStr;
	}
	else if(document.getElementById(ContentID.toLowerCase() + "_rating") !=null)
	{
		document.getElementById(ContentID.toLowerCase() + "_rating").innerHTML=ImageStr;
	}
}


function fnDownloadContent(ContentID)
{

	popWin = window.open(AppURL + "/Modules/Download_Latest.aspx?ContentID=" + ContentID ,'DownloadPackage','statusbar=no,resizable=yes,scrollbars=yes,width=370,height=170,left=500,top=350');
	//popWin = window.open("Modules/Download.aspx?FileName=" + FileName + "&DownloadPath=" + Path,'DownloadPackage','statusbar=no,resizable=yes,scrollbars=yes,width=570,height=270');
	popWin.focus();
}

function fnUpdateListing(newvalues)
{
    document.getElementById(strClientID +"hdnConditionList").setAttribute("value",newvalues);     
    document.getElementById(strClientID +"dummylink").click();      
}

function fnCategoryLinkClick(apppath,keyvalues)
{
   	document.location.href=apppath + keyvalues;

}

function GoToECourseView(url,cid)
{
       RSExecute("../PublicModules/AjaxForm.aspx","ClearSessions","TrackAttempt");
       document.location.href=url + "?TrackID=" + cid; 
}

if( typeof(Sys) != "undefined" && typeof(Sys.Application) != "undefined" )
{ Sys.Application.notifyScriptLoaded(); 
}
