﻿function fnWindowHandlesCallback(ArrayString,SesisonName)
{
   SetWindowDataUsingPostBack('windowhandlers',ArrayString,SesisonName);   
}       
function DisplayResultsCallback( result, context )
{ 
    if(result=='1')
        alert("successful");
}        
function DisplayErrorCallback( error, context )
{
    alert("Failed. " + error);
}    
function leftTrim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}
function rightTrim(sString) 
{
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
}
function trimAll(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
}
function fnApplyTheme(ThemeID,AppUrl)
{
    var ThemeName = document.getElementById(MasterClientID + "img_" + ThemeID).getAttribute("imagename");
    var LinkHref=ThemePath + "/" + ThemeName + "/Style.css";
  
    AppUrl=AppUrl + "/PublicModules/AjaxForm.aspx";
    fnUpdateUserTheme(ThemeID,AppUrl);
      document.getElementById(MasterClientID + "styleobj").href=LinkHref;
       document.getElementById(MasterClientID + "imgSiteLogo").src=ThemePath + "/" + ThemeName +"/logo.gif";
}
function fnMenuNavigation(navigationurl)
{
    window.location.href=navigationurl;
}
function ShowFirstLevelIMSMenusLink(objspan,objectid)
{       
    var curleft = 0;
    var curtop = 0;
    if (document.getElementById || document.all) 
    {
    while (objspan.offsetParent) 
    {
        curleft += objspan.offsetLeft
        curtop += objspan.offsetTop
        objspan = objspan.offsetParent;
    }
    }
    else if (document.layers)
    {
    curleft += objspan.x;
    curtop  += objspan.y;
    }
    var leftspace = ( document.body.offsetWidth - 950 ) /2;
    document.getElementById(objectid).style.left = document.getElementById('ctl00_hypBLnk_Folders').offsetLeft + leftspace;
    //alert(document.getElementById(objectid).style.left);
    document.getElementById(objectid).style.top = document.getElementById('ctl00_tblHeaderPane').clientHeight + document.getElementById('ctl00_hypBLnk_Folders').clientHeight + document.getElementById('ctl00_hypBLnk_Folders').offsetTop + 30
    document.getElementById(objectid).style.display="";
}
function ShowFirstLevelManageMenusLink(objspan,objectid)
{       
    var curleft = 0;
    var curtop = 0;
    if (document.getElementById || document.all) 
    {
        while (objspan.offsetParent) 
        {
            curleft += objspan.offsetLeft
            curtop += objspan.offsetTop
            objspan = objspan.offsetParent;
        }
    }
    else if (document.layers)
    {
        curleft += objspan.x;
        curtop  += objspan.y;
    }
    var leftspace = ( document.body.offsetWidth - 950 ) /2;
     document.getElementById(objectid).style.left = document.getElementById('ctl00_hypBLnk_Manage').offsetLeft + leftspace;
     document.getElementById(objectid).style.top = document.getElementById('ctl00_tblHeaderPane').clientHeight + document.getElementById('ctl00_hypBLnk_Manage').clientHeight + document.getElementById('ctl00_hypBLnk_Manage').offsetTop + 30
    document.getElementById(objectid).style.display="";
}
function ShowFirstLevelMenusLink(objspan,objectid)
{       
var curleft = 0;
var curtop = 0;
if (document.getElementById || document.all) 
{
    while (objspan.offsetParent) 
    {
        curleft += objspan.offsetLeft
        curtop += objspan.offsetTop
        objspan = objspan.offsetParent;
    }
}
else if (document.layers)
{
    curleft += objspan.x;
    curtop  += objspan.y;
}
var leftspace = ( document.body.offsetWidth - 890 ) /2;
 document.getElementById(objectid).style.left = document.getElementById('ctl00_hypBLnk_Settings').offsetLeft - leftspace;
 document.getElementById(objectid).style.top = document.getElementById('ctl00_hypBLnk_Settings').clientHeight + document.getElementById('ctl00_hypBLnk_Settings').offsetTop + 16;
document.getElementById(objectid).style.display="";
}
function HideFirstLevelMenusLink(objspan,objectid)
{
    document.getElementById(objectid).style.display="none";
}
function showHelp()
{
   window.open(RelativeURL + "Help.aspx","CKSSupport","location=0,status=0,scrollbars=1,resizable=0,width=10,height=10");
}
function jsTrim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function jsLtrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function jsRtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}



/***************************************************************************************************************/
/********************************* Multiple Windows ************************************************************/
function fnOnLoad()
{
	if(SessionWinInt !=  '') intWinNames = parseInt(SessionWinInt);
	else intWinNames = 0;
	if(SessionWinArray !=  '') 
	{	
		var winStr=SessionWinArray;
		arrWinNames = new Array(winStr.split("$").length);
		for (a=0;a<=winStr.split("$").length-1;a++)
		{
			var item=winStr.split("$")[a];
			arrWinNames[a]=new Array(item.split("~").length);
			for(b=0;b<=item.split("~").length-1;b++)
			{
				arrWinNames[a][b]=item.split("~")[b];
			}
		}
	}
	else 
	{
		arrWinNames = new Array();
	}
	
}


function fnStoreArrayData()
{
	fnNullifySessions();
	fnSaveWinArray();
	fnSaveWinInt();
}

function fnNullifySessions()
{
	fnClearSessions('WinInt');
	fnClearSessions('WinArray');
	//MakeCallback(rcbClientID,'fnClearSessions','WinInt');
	//MakeCallback(rcbClientID,'fnClearSessions','WinArray');
	
}

function fnSaveWinArray()
{
	if (arrWinNames != null)
	{	
		if(arrWinNames.length>0)
		{
			var index =0;
			for(index=0;index<=arrWinNames.length-1;index++)
			{
				var strArrayVals="";
				strArrayVals = arrWinNames[index][0] + "~" + arrWinNames[index][1] + "~" + arrWinNames[index][2] + "~" + arrWinNames[index][3];
				fnWindowHandles(strArrayVals,'WinArray');
				//MakeCallback(rcbClientID,'fnWindowHandles',strArrayVals + '|' + 'WinArray');
			}
		}
	}					
}

function fnSaveWinInt()
{
	if (intWinNames != null)
	{
		fnIndexValues(intWinNames,'WinInt');
		//MakeCallback(rcbClientID,'fnIndexValues',intWinNames + '|' + 'WinArray');
	}
}

function AddToArrayOnCreateImport(ContentID,action,wintitle)
{
	var wName=replaceAll(ContentID,"-","_");
	wName=wName.toLowerCase();
	
	var retVal=fnSearchArray(ContentID,arrWinNames,action,'edit');
	if ((retVal.split("~")[0]<1) || (retVal.split("~")[1].toLowerCase()=="true"))
	{
		arrWinNames[intWinNames] = new Array(4);
		arrWinNames[intWinNames][0] = wintitle;//wName + "_" + 'create';
		arrWinNames[intWinNames][1] = ContentID;
		arrWinNames[intWinNames][2] = 'edit';
		arrWinNames[intWinNames][3] = action;
		intWinNames = intWinNames ;
	}
}

function newGuid(){
	var g = "";
	for(var i = 0; i < 32; i++)
	g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "")
	return g;
}

function openWindow(contentID,action,url,winDim,wNameSet)
{
	var wName=replaceAll(contentID,"-","_");
	wName=wName.toLowerCase();
	
	var retVal=fnSearchArray(contentID,arrWinNames,action.toLowerCase(),wNameSet);
	//if count=0 or blnNew=true, then open new window.
	//the return value has count~blnNew~windowtitle
	if ((retVal.split("~")[0]<1) || (retVal.split("~")[1].toLowerCase()=="true"))
	{
		if(retVal.split("~")[2]!=null && retVal.split("~")[2]!="")
		{
			var chldwin = window.open("",retVal.split("~")[2]);
			chldwin.close();
			fnRemoveActionSetFromArray(arrWinNames, contentID, wNameSet)
			intWinNames=arrWinNames.length;
			return chldwin;
		}
		
		if(url!="")
		{    
		    intWinNames=arrWinNames.length;
			arrWinNames[intWinNames] = new Array(4);
			arrWinNames[intWinNames][0] = wName + "_" + wNameSet;
			arrWinNames[intWinNames][1] = contentID;
			arrWinNames[intWinNames][2] = wNameSet;
			arrWinNames[intWinNames][3] = action;								
			
		
			var chldwin1 = window.open(url,wName + "_" + wNameSet,winDim);
			chldwin1.focus();
			intWinNames = intWinNames ;
			return chldwin1;
		}
	}
}

function fnSearchArray( searchS, arraySA , action, winNameSet) {
	var Count = 0;
	var newCount = 0;
	var s = "";
	var blnNew=false;
	var winTitle="";
	/**************************************************************************************/
	//USECASE 1:
	/**************************************************************************************/
	//In My Catalog, at any point of time, there will be only one eLearning object open
	//Non eLearning Objects can be opened in multiple windows.
	/**************************************************************************************/
	//begin
	if(winNameSet.toLowerCase()=='view')
	{
		for(I=0;I<arraySA.length;I++)
		{
			s1 = arraySA[ I ][1];
			if((searchS.toLowerCase() != s1.toLowerCase()) && arraySA[ I ][2] == 'view' && arraySA[ I ][3] == 'view')
			{
				Count = parseInt(Count) ;
				winTitle=arraySA[ I ][0];
				break;
			}						
		}
		if (Count>=1)
		{
			alert("An eLearning item is already open. Please close it to access another eLearning item.");
			return (Count + "~" + blnNew + "~" + winTitle);
		}
	}
	//end
	
	/**************************************************************************************/
	//USECASE 2:
	/**************************************************************************************/
	//if the current nameset is not one of the following,
	//check if an window with nameset as edit exists or not.
	//if exists, cannot open new window; else open new window.				
	/**************************************************************************************/
	//begin
	if(winNameSet.toLowerCase()!='edit' && winNameSet.toLowerCase()!='review' && winNameSet.toLowerCase()!='preview' && winNameSet.toLowerCase()!='viewcomments' && 

winNameSet.toLowerCase()!='viewhistory' && winNameSet.toLowerCase()!='whereused')
	{
		for(I=0;I<arraySA.length;I++)
		{
			s1 = arraySA[ I ][1];
			if((searchS.toLowerCase() == s1.toLowerCase()) && (arraySA[ I ][2] == 'edit' ||  arraySA[ I ][2] == 'delete' || arraySA[ I ][2] == 'replace'))
			{
				Count = parseInt(Count) ;
				winTitle=arraySA[ I ][0];
				break;
			}						
		}
		if (Count>=1)
		{
			alert("An editor for this item is already open. Please close it to continue.");
			return (Count + "~" + blnNew + "~" + winTitle);
		}
	}
	//end
	
	/**************************************************************************************/
	//USECASE 3:
	/**************************************************************************************/
	//if the current nameset is edit,delete or replace,
	//check if an window with nameset as workspaceactions exists or not.
	//if exists, cannot open new window; else open new window.
	/**************************************************************************************/
	//begin
	if(winNameSet.toLowerCase()=='edit' ||  winNameSet == 'delete' || winNameSet == 'replace')
	{
		for(I=0;I<arraySA.length;I++)
		{
			s1 = arraySA[ I ][1];
			if((searchS.toLowerCase() == s1.toLowerCase()) && (arraySA[ I ][2] == 'workspaceactions'))
			{
				Count = parseInt(Count) ;
				winTitle=arraySA[ I ][0];
				break;
			}						
		}
		if (Count>=1)
		{
			alert("A workflow step is in-process, please complete the process and retry.");
			return (Count + "~" + blnNew + "~" + winTitle);
		}
	}
	//end
	
	/**************************************************************************************/
	//USECASE 4:
	/**************************************************************************************/
	//if one addcomments window is already open, 
	//the following code is to not allow another addcomments window to be opened (even if it is a different item).
	/**************************************************************************************/
	//begin
	if(winNameSet.toLowerCase()=='workspaceactions')
	{
		for(I=0;I<arraySA.length;I++)
		{
			if(arraySA[ I ][2] == winNameSet)
			{
				Count = parseInt(Count) ;
				winTitle=arraySA[ I ][0];
				break;
			}
		}
		if(Count>=1)
			alert("A workflow step is in-process, please complete the process and retry.");
		return (Count + "~" + blnNew + "~" + winTitle);
	}
	//end
	Count=0;
	
	/**************************************************************************************/
	//USECASE 5:
	/**************************************************************************************/
	//if an item with same cid,nameset but different action exits (e.g., edit, editmetadata),
	//alert the user about it, and if user clicks ok, close the other window and open the 
	//current window.
	//if such an item does not exist, check if an item with same cid,nameset and action exits
	//if exists, no action required. if not, open new window.
	/**************************************************************************************/
	//begin
	for(I=0;I<arraySA.length;I++)
	{
		s = arraySA[ I ][1];								
		if((searchS.toLowerCase() == s.toLowerCase()) && arraySA[ I ][2].toLowerCase() == winNameSet.toLowerCase() && arraySA[ I ][3].toLowerCase() != 

action.toLowerCase())
		{
			blnNew=true;
			winTitle=arraySA[ I ][0];
			break;
		}
		if (searchS.toLowerCase() == s.toLowerCase() && arraySA[ I ][3].toLowerCase() == action.toLowerCase())
		{
			newCount = parseInt(newCount) ;
			winTitle=arraySA[ I ][0];
		}
	}
	if(blnNew==false)
	{
		Count=parseInt(newCount);					
	}
	
	if (blnNew==true)
	{
		Count = Count ;
		if(confirm("A window for this item is already open, clicking OK button will loose the changes in that window. \nDo you wish to continue?")==true)
		{
			fnRemoveActionSetFromArray(arraySA, searchS, winNameSet)
			intWinNames=arrWinNames.length;
		}
		else
		{
			blnNew=false;
		}
	}
	return (Count + "~" + blnNew + "~" + winTitle);
	//end
}

function replaceAll( str, from, to ) {
	var idx = str.indexOf( from );


	while ( idx > -1 ) {
		str = str.replace( from, to ); 
		idx = str.indexOf( from );
	}

	return str;
}


function fnRemoveActionSetFromArray(arraySA1, searchS, NameSet)
{
	var Count = 0;
	var s = "";
	for(I=0;I<arraySA1.length;I++)
	{
		//s = arraySA1[ I ][1];
		if(arraySA1[ I ][2].toLowerCase()== NameSet.toLowerCase())
		{
				arraySA1.splice(I,1);
				//break;
		}
	}
}

function fnRemoveFromArray( arraySA2, searchS, Action) {
	var Count = 0;
	var s = "";
	for(I=0;I<arraySA2.length;I++)
	{
		s = arraySA2[ I ][1];
		if((searchS.toLowerCase() == s.toLowerCase()) &&  arraySA2[ I ][3].toLowerCase()==Action.toLowerCase())
		{
				arraySA2.splice(I,1);
				break;
		}
	}
	
}

function fnUpdateArray(prevContentID, curContentID) {
	var Count1 = 0;
	var s = "";
	var index=0;
	for(I=0;I<arrWinNames.length;I++)
	{
		s = arraySA2[ I ][1];
		if((curContentID.toLowerCase() == s.toLowerCase()) &&  arrWinNames[ I ][2].toLowerCase()=='edit')
			Count1 = parseInt(Count1) ;
		if((prevContentID.toLowerCase() == s.toLowerCase()) &&  arrWinNames[ I ][2].toLowerCase()=='edit')
			index=I;
	}	
	if (Count1>=1)
	{
		alert("The editor is already open for this content item");
		return false;
	}
	else			
	{
		if(arrWinNames[index]!=null)
		{
			arrWinNames[index][1] = contentID;
			arrWinNames[index][2] = 'edit';
			arrWinNames[index][3] = 'edit';	
			return true;
		}
	}
}
					
function RemovePopupElementsFromArray(ContentID, action)
{
	fnRemoveFromArray(arrWinNames, ContentID, action);
	intWinNames=arrWinNames.length;
}
		
/*function ClosePopupsOnSelfClose()
{
	if (window.event.clientX < 0 && window.event.clientY < 0)
	{
		for(J=0;J<arrWinNames.length;J++)
		{
			var s = arrWinNames[ J ][0];
			alert(s);
			popwin=window.open("",s);
			popwin.close();						
		}
	}				
}*/
		
function ClosePopupsOnSignOut()
{
	for(J=0;J<arrWinNames.length;J++)
	{
		var s = arrWinNames[ J ][0];
		var popwin=window.open("",s);
		popwin.close();						
	}				
}
//this function returns 1 if date in value1 is grater than date in value2 else 0
// this function returns -1 if the dates are equal if the date arte in mm/dd/yyyy format
function compareDates (value1, value2) 
{ var date1, date2;
  var month1,month2;
  var year1, year2;
  month1 = value1.substring (0,value1.indexOf ("/"));
  date1 = value1.substring(value1.indexOf ("/")+1,value1.lastIndexOf ("/"));
  year1 = value1.substring(value1.lastIndexOf("/")+1, value1.length);
  month2 = value2.substring(0, value2.indexOf("/"));
  date2 = value2.substring(value2.indexOf ("/")+1,value2.lastIndexOf ("/"));
  year2 = value2.substring(value2.lastIndexOf("/")+1, value2.length);
  if(year1>year2) return 1;
  else if(year1<year2) return 0;
  else if(month1>month2) return 1;
  else if(month1<month2) return 0;
  else if(date1>date2) return 1;
  else if(date1<date2) return 0;
  else return -1;
 }
/***************************************************************************************************************/

/*function $(id) {
	return document.getElementById(id);
}*/

function fnGoToHome(url)
{
    window.location.href=url;
}

/*****************Changing bg to dim ****************************/
        
        function fnChangeStyle()
        {  
           document.body.style.cursor = "wait";
            document.getElementById("modalDiv").style.display = "block";
            document.getElementById("modalDiv").style.top = document.body.scrollTop;
            document.getElementById("modalDiv").className="modalBackgroundwhite";
            document.getElementById("modalDiv").style.height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) + "px";
            document.getElementById("modalDiv").style.width = (Math.max(document.documentElement.scrollWidth, document.body.scrollWidth)-50) + "px";
              window.resize = function () 
            { 
                document.getElementById("modalDiv").style.height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) + "px";
            document.getElementById("modalDiv").style.width = (Math.max(document.documentElement.scrollWidth, document.body.scrollWidth)-50) + "px";
            };
        }


        function fnClearStyle()
        {
            document.body.style.cursor = "default";
            document.getElementById("modalDiv").className="";
            document.getElementById("modalDiv").style.display = "none";
        }
        
     function load(){
     Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequest); 
     Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    }

    function EndRequestHandler(){
        fnClearStyle();
    }

    function InitializeRequest(){
       fnChangeStyle();
    }
    
    function fnLaunchContentFromFC(ContentID){
       RSExecute("PublicModules/AjaxForm.aspx","RedirectToDetailsPage",ContentID);
       if (UserID == '-1')
       {
            fnChangeCloseBodyStyle();
            fnOpenLoginForm('modalDiv','QFrame','show','visible','visible','inline');
            return;
       }
       document.location.href="ModulePages/ListingPage.aspx"; 
       return;
    }

if( typeof(Sys) != "undefined" && typeof(Sys.Application) != "undefined" )
{ 
Sys.Application.notifyScriptLoaded(); 
} 


