function ApplyCSS(container, cssclass)
{  
    container.className = cssclass;     
}
function RemoveCSS(container, cssclass)
{
    container.className = cssclass;     
}

//GLobal variable
var GridContextData;
var Data;
var SourcePopUpPageLibrary;

function setContainerOperation(contextData, operation)
{
    contextData.Container.ContentOperation = operation;
    contextData.HiddenContentOperation.value = operation;
    
    //If any content is added/modified/replaced, Ismodified property would be always true 
    contextData.Container.IsModified = "True";
    contextData.HiddenIsModified.value = "True";
    if (contextData.Container.getAttribute("FWContainerType") != "FWProductPropertyControl")
    {
        iapps_container_contentChanged_ShowAlert = true;
    }
} 

function setFwObjectId(contextData, fwObjectId)
{
  if(fwObjectId == "")
  {
    contextData.Container.FwObjectId = "";
    contextData.HiddenFwObjectId.value = "";
  }
  else
  {
    contextData.Container.FwObjectId = fwObjectId;
    contextData.HiddenFwObjectId.value = fwObjectId;
  }
}


     function ConvertToHtml(str)
     {
        var replacedString = str;
        replacedString = replacedString.replace(new RegExp("&amp;","g"), "&");
        replacedString = replacedString.replace(new RegExp("&lt;","g"), "<");
        replacedString = replacedString.replace(new RegExp("&gt;","g"), ">");
        replacedString = replacedString.replace(new RegExp("&quot;","g"), "\"");
        replacedString = replacedString.replace(new RegExp("&apos;","g"), "'");
        replacedString = replacedString.replace(new RegExp("&slash;","g"), "\\");
//                
        replacedString = replacedString.replace(new RegExp("&spamp;","g"), "&");
//        replacedString = replacedString.replace(new RegExp("&splt;","g"), "<");
//        replacedString = replacedString.replace(new RegExp("&spgt;","g"), ">");
//        replacedString = replacedString.replace(new RegExp("&spquot;","g"), "\"");
//        replacedString = replacedString.replace(new RegExp("&spapos;","g"), "'");
//        replacedString = replacedString.replace(new RegExp("&spslash;","g"), "\\");
        
        replacedString = replacedString.replace(new RegExp("&doublequot;","g"), "\"");
        replacedString = replacedString.replace(new RegExp("&splt;","g"), "<");       
         
        return replacedString;          
    }
    
    
    function ShowHideContainer(Data)
    {   
        
        if (Data.Container.getAttribute("IsVisible") == "False")
        {
            Data.Container.setAttribute("IsVisible","True");            
            Data.HiddenIsVisible.value = "True";
            Data.Container.className = "EditOptionalCSSClass";           
        }
        else  
        {          
            Data.Container.setAttribute("IsVisible","False"); 
            Data.HiddenIsVisible.value = "False";
            Data.Container.className ="EditOptionalCSSClass HiddenContainerCssClass" ;            
        }
        
        Data.Container.IsModified = "True";
        Data.HiddenIsModified.value = "True";
        //setContainerOperation(Data, "Modified");
        Data.ContextMenu.hide(); 
    }
    
    















//Token passing code
var navigateURL ;
var destinationUrl= "";
var functionName = "";
var req;
function sendRequest(url,callback,postData) {
    req = null;
	req = createXMLHTTPObject();
	if (!req) { alert ('return ');  return; }
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');

	req.onreadystatechange = function () {
	    try{
		    if (req.readyState != 4) return;
                //alert(req.responseText);
		    if (req.status != 200 && req.status != 304) {
			    //alert('HTTP error ' + req.status);
			    return;
		    }
		    callback(req);
		}
		catch(e)
		{
		    alert("Could not execute your request at this time.");
		}    
	}

    req.send(postData);
	return;
}

function pausecomp(sec) 
{
    var millis = sec * 1000;
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); } 
    while(curDate-date < millis);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function handleResponse(req) 
{
   var callResponse =  req.responseText;
   var emptyGuid = '00000000-0000-0000-0000-000000000000' ;
   if (callResponse.length == emptyGuid.length )
   {
       if(callResponse!='Invalid')
       {
       //check '?' we have to add here
          if (navigateURL.indexOf('?') > -1)
          {
             destinationUrl = navigateURL + '&Token=' + callResponse ;
          }       
          else
          {
             destinationUrl = navigateURL + '?Token=' + callResponse ;
          }
          setTimeout(functionName,1000);  
       }
   }
}

function SendTokenRequestByURL(redirectURL)
{
    destinationUrl= "";
    var tokenPageUrl = "LoginTokenProvider.aspx?userId=" + jUserId + "&appId=" + jAppId + "&productId=" + jProductId + "&userName="+juserName  ;
    var urlToCall = jPublicSiteUrl + '/' + tokenPageUrl;
    navigateURL = redirectURL;
    sendRequest(urlToCall,handleResponse,"GET");  
}

function RequestTokenForEditBlogPost(redirectURL)
{
   functionName = "DisplayBlogPostPopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForManageBlog(redirectURL)
{
   functionName = "RedirectToManageBlog();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForContent(redirectURL)
{
   functionName = "DisplayContentPopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForInsertXML(redirectURL)
{
   functionName = "DisplayInsertXML();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForEditXML(redirectURL)
{
   functionName = "DisplayEditXML();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForTypeOfContent(redirectURL)
{
   functionName = "DisplayContentDefinitionPopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForInsertImage(redirectURL)
{
   functionName = "DisplayInsertImagePopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForEditImage(redirectURL)
{
   functionName = "DisplayEditImagePopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForNewList(redirectURL)
{
   functionName = "DisplayNewListPopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForInsertContentListItem(redirectURL)
{
   functionName = "DisplayInsertContentListItemPopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForInsertPageListItem(redirectURL)
{
   functionName = "DisplayInsertPageListItemPopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForManualListPopup(redirectURL)
{
   functionName = "DisplayManualListPopup();";
   SendTokenRequestByURL(redirectURL);
}

function RequestTokenForManageList(redirectURL)
{
   functionName = "DisplayManageList();";
   SendTokenRequestByURL(redirectURL);
}
function RequestTokenForInsertFilefromLibraryPopup(redirectURL)
{
   functionName = "DisplayInsertFilefromLibraryPopup();";
   SendTokenRequestByURL(redirectURL);
}

function DisplayInsertFilefromLibraryPopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('InsertFlashFile', 'iframe',destinationUrl, 'Insert Flash File', 'width=780px,height=514px,center=1,resize=0,scrolling=1');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('InsertFlashFile');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayManageList()
{
    if (destinationUrl!="")
    {
        //debugger;
        window.location.href = destinationUrl;    
    }
}

function DisplayManualListPopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('InsertFileToList', 'iframe',destinationUrl, 'Insert File to List', 'width=1000px,height=550px,center=1,resize=0,scrolling=1');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('InsertFileToList');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayInsertPageListItemPopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('InsertPageToList', 'iframe',destinationUrl, 'Insert Page to List', 'width=1000px,height=550px,center=1,resize=0,scrolling=1');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('InsertPageToList');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayInsertContentListItemPopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('InsertContentToList', 'iframe',destinationUrl, 'Insert Content to List', 'width=1000px,height=550px,center=1,resize=0,scrolling=1');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('InsertContentToList');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayNewListPopup()
{
    if (destinationUrl!="")
    {
        ListGridWin = dhtmlmodal.open('InsertList', 'iframe', destinationUrl, 'Insert List', 'width=388px,height=387px,center=1,resize=0,scrolling=1');
        ListGridWin.onclose=function()
        { 
            var a=document.getElementById('InsertList');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayInsertImagePopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('InsertImage', 'iframe', destinationUrl, 'Insert Image', 'width=780px,height=484px,center=1,resize=0,scrolling=0');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('InsertImage');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayEditImagePopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('EditImage', 'iframe', destinationUrl, 'Edit Image', 'width=1000px,height=560px,center=1,resize=0,scrolling=0');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('EditImage');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayContentPopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('InsertContent', 'iframe', destinationUrl, 'Insert Content', 'width=780px,height=482px,center=1,resize=0,scrolling=1');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('InsertContent');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }
    }
}

function DisplayBlogPostPopup()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('EditBlogPost', 'iframe', destinationUrl, 'Edit BlogPost', 'width=1000px,height=570px,center=1,resize=0,scrolling=1');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('EditBlogPost');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }
    }
}

function RedirectToManageBlog()
{
    if (destinationUrl != "")
    {
        window.location.href = destinationUrl;
    }
}

function DisplayInsertXML()
{
    if (destinationUrl!="")
    {
        viewPopupWindow = dhtmlmodal.open('InsertXMLContent', 'iframe', destinationUrl, 'Insert Content', 'width=780px,height=482px,center=1,resize=0,scrolling=1');
        viewPopupWindow.onclose=function()
        { 
            var a=document.getElementById('InsertXMLContent');
            var ifr=a.getElementsByTagName("iframe");
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }        
    }
}

function DisplayEditXML()
{
    if (destinationUrl!="")
    {
        contentDefinitionPopup = dhtmlmodal.open('EditXMLContent', 'iframe', destinationUrl, 'Manage Content Definition', 'width=1000px,height=570px,center=1,resize=0,scrolling=1');
        contentDefinitionPopup.onclose=function()
        { 
            var a=document.getElementById('EditXMLContent');
            var ifr=a.getElementsByTagName("iframe");
            Data.ContextMenu.hide(); 
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            return true;
        }
    }
    else
    {
        alert("This container is already empty.");
    }
}

function DisplayContentDefinitionPopup()
{
    if (destinationUrl!="")
    {
        contentDefinitionPopup = dhtmlmodal.open('ContentDefinition', 'iframe', destinationUrl, 'Manage Content Definition', 'width=1000px,height=570px,center=1,resize=0,scrolling=1');
        contentDefinitionPopup.onclose=function()
        { 
            var a=document.getElementById('ContentDefinition');
            var ifr=a.getElementsByTagName("iframe");
            Data.ContextMenu.hide(); 
            window.frames[ifr[0].name].location.replace("/blank.html") ; 
            
            return true;
        }
    }
}





    
