﻿// JScript File
$.ajaxSetup({ cache: false });

    jQuery.query = function(str) {
      var r = {};
      var params = str.split('&');
      for( var i = params.length-1;  i >= 0;  i-- ) {
         var p = params[i].split('='), key = p[0];
         if( key ) r[key] = decodeURIComponent(p[1]);
      }
      return r;
   }; 

	var blocking_call=false;
    var http_request = false;
    var http_obj;
    var closeAjaxDialog = false;
    var permissions_open = false;    
    var sitemappath;
    var sitemapchild;
    var objscriptpath;
    var lastURL;
    function checkField(obj)
    {
		blocking_call=true;
        var path = obj.id;
        objscriptpath = path;
        if (path.indexOf("-confirm")>0)
        {
            //password confirmation box
            path = path.replace("-confirm","");
        }
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "fieldValue=" + encodeURIComponent(document.getElementById(path).value).replace('&','%26');
        poststr += "&fieldPath=" + encodeURIComponent(path);
        poststr += "&action=partial"
        http_obj = document.getElementById('fieldWrapper_'+ path);
        //check for additional fields contained in the wrapper
        jQuery(http_obj).find("input[id^='"+path+"'][id^='"+path+"']").each(function() {
            //skip out for password non confirmation
            if (this.id.indexOf("-confirm")>0 && obj.id.indexOf("-confirm") <=0) return false;
            poststr += "&" + this.id + "=" + encodeURIComponent(this.value);
        });
        jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(poststr), function(data, textStatus) {
            alertContents(data, textStatus);blocking_call=false;
        });
    }
        
        
	function postSummaryUpdate(row,associationid)
	{
    var loc = window.location.href;
    
    jQuery("body").addClass('waiting');
    
	//add standard variables to post str
	var poststr = "action=partial&subaction=summaryPost&id=" + associationid  + '&';
	//enumerate the update fields and create a post string
	jQuery(row).find ('.inlineEdit').each(function()
	{
		poststr += this.name + '=' + encodeURI(this.value) + '&';
	});
	  jQuery.post(loc, jQuery.query(poststr), function(data, textStatus) {
			jQuery("body").removeClass('waiting');
			if (data=='Success') 
			{
				//update was ok/
				//update the fields to reflect a good update
				jQuery(row).find('.inlineEdit').each(function()
				{
					var outer=this;
					var isset=false;
					jQuery(this.parentNode.parentNode).find("*[class='hideOnEdit']").find('a').each(function()
					{
						var valtoset=outer.value;
						if (valtoset=='') {valtoset = '&lt;blank&gt;'}
						this.innerHTML=valtoset;
					    isset=true;
						
					});	
					if (!isset)
					{
						jQuery(this.parentNode.parentNode).find("*[class='hideOnEdit']").each(function()
						{
							this.innerHTML=outer.value;
							
						});
					}
				});
	
				toggleRowForEdit(row,false);
			}
			else
			{
				alert(data);
			}
        });
	

	return false;
	}
        
        
    function setTabSwitch(enableTabView) {
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "action=partial&subaction=setTabSwitch&value=" + enableTabView;
        //jQuery.post(loc, jQuery.query(poststr));
    }
    
    function associateDocument(parent,path,schemaKey,id,cardinality,options)
    {
        jQuery("#ajaxContainer").addClass('waiting');
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        loc += '?partial=partial';
        var poststr = "parent=" + parent;
        poststr += "&path=" + encodeURI(path);
        poststr += "&schemaKey=" + encodeURI(schemaKey);
        poststr += "&fieldPath=" + encodeURI(path);
        poststr += "&child=" + id;
        poststr += "&cardinality=" + encodeURI(cardinality);      
        poststr += "&action=partial";
        poststr += "&subaction=associate";
        if(options!=null) poststr += "&options=" + encodeURI(options) ;
        poststr += "&url=" + encodeURI(loc);
        if (path.indexOf('single-') >= 0)
        {
            http_obj = document.getElementById('fieldWrapper_'+ path.substring(7));
        }
        else
        {
            http_obj = document.getElementById('fieldWrapper_'+ path);
        }
        if (!permissions_open) {
                jQuery("#ajaxDialogContents").html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
                closeAjaxDialog = true;
         }
        jQuery('#' + http_obj.id).load(loc, jQuery.query(poststr), function(data, textStatus) {
            alertContents(data, textStatus);
        });
        return false;
    }
    function cancelAjaxPost()
    {
        jQuery("#ajaxDialogContents").html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
        jQuery("#ajaxContainer").addClass("waiting"); 
        //cancels an update in progress in an ajax dialog
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "action=partial";
        poststr += "&subaction=removePageFromStack";
        closeAjaxDialog = true;
        jQuery.post(loc, jQuery.query(poststr), function(data, textStatus) {
            alertContents(data, textStatus);
        });
        return false;
    }    
    
    function changeItemOrder(itemId, direction,path)
    {
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "itemid=" + itemId;
        poststr += "&fieldPath=" + encodeURI(path);
        poststr += "&direction=" + encodeURI(direction);
        poststr += "&action=partial";
        poststr += "&subaction=ChangeOrder";
		jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(poststr), function(data, textStatus) {
			alertContents(data, textStatus);
		});
        return false;
    }
    
    
    function AjaxChangeLandingBlockOrder(currentOrder, NewOrder ,path)
    {
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        //var poststr = "itemid=" + itemId;
        var poststr = "&fieldPath=" + encodeURI(path);
        poststr += "&currentOrder=" + currentOrder;
        poststr += "&newOrder=" + NewOrder;
        poststr += "&action=partial";
        poststr += "&subaction=ChangeLandingBlockOrder";
		disableActionButtons();
		jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(poststr), function(data, textStatus) {
			alertContents(data, textStatus);
		});
        return false;
    }
    function AjaxChangeLandingBlockSize(Order ,path,size)
    {
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        //var poststr = "itemid=" + itemId;
        var poststr = "&fieldPath=" + encodeURI(path);
        poststr += "&currentOrder=" + Order;
        poststr += "&size=" + size;
        poststr += "&action=partial";
        poststr += "&subaction=ChangeLandingBlockSize";
		disableActionButtons();
		jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(poststr), function(data, textStatus) {
			alertContents(data, textStatus);
		});
        return false;
    }

    
      function changeRowOrder(itemId, direction,path,currRow)
    {
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "itemid=" + itemId;
        poststr += "&fieldPath=" + encodeURI(path);
        poststr += "&direction=" + encodeURI(direction);
        poststr += "&row=" + encodeURI(currRow);
        poststr += "&action=partial";
        poststr += "&subaction=ChangeRowOrder";
		jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(poststr), function(data, textStatus) {alertContents(data, textStatus);});
        return false;
    }

    function ajaxChangeSitemapItemOrder(id,direction,parent,depth)
    {
        //document.body.className = "waiting";
        var temp = 'ulsitemap';
        if (parent > 0) { temp = 'ulsitemap_' + parent;}
        url = '/cms/sitemap.aspx?action=move&child=' + id + '&direction=' + direction + '&depth=' + depth;
        jQuery("#ajaxDialogContents").html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
        jQuery('#'+temp).load(url, function(data, textStatus) {alertContents(data, textStatus);});
    }

    function ajaxChooseSitemapItem(id,name)
    {
		if (blocking_call) setTimeout(function(){ajaxChooseSitemapItem(id,name)},1000);
        if (sitemappath !='')
        {
            jQuery('#'+sitemappath).val("internal://" + name + '(' + id + ')');
            jQuery('#'+sitemappath + '_display').html(name);
            ajaxCloseDialog();
        }
        else
        {
            //this must be a request on the main sitemap page
            //clear the ajax box to avoid issues with duplicate ids
            jQuery("#ajaxDialogContents").html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
            closeAjaxDialog = true;        
            //grabs the sitemap root element
            var url = '/cms/sitemap.aspx?action=changeparent&child=0&depth=0&sitemapparent=' + id + '&sitemapchild=' + sitemapchild ;
            //we need to reload the whole sitemap
            jQuery('#ulsitemap').load(url, function(data, textStatus) {alertContents(data, textStatus);});
        }
    }
    
    function ajaxChooseSitemapParent(_sitemappath,documentid,chosen,showFilterChildren)
    {
        hideObjects(); //defined in general .js
        sitemappath = _sitemappath;
        jQuery('#ajaxCloseBox').attr('onclick','');
        jQuery('#ajaxCloseBox').attr('href','javascript:void(ajaxCloseDialog());');
        
        jQuery("#ajaxDialogExpandLink").hide();
        closeAjaxDialog = false;
        
		jQuery('#ajaxTitle').text("Choose sitemap location");
		jQuery('#ajaxContainer').show();
		jQuery('#ajaxDialog').addClass('ajaxDialog');
		jQuery('#ajaxDialogContents').html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
        if (sitemappath!='') {
			chosen=document.getElementById(sitemappath).value;
			chosen=chosen.replace('internal://','');
			uri = "/cms/sitemap.aspx?dialog=y&chosen=" + chosen + '&documentid=' + documentid + "&showFilterChildren=" + showFilterChildren ;
        }
        else {
            sitemapchild = documentid;
            uri = "/cms/sitemap.aspx?showFilterChildren=" + showFilterChildren + "&dialog=y&chosen=" + encodeURI(chosen) + '&documentid=' + documentid
        }
		jQuery('#ajaxDialogContents').load(uri, function(data, textStatus) {alertContents(data, textStatus);jQuery('#ajaxDialogContents').html(data);});
        return false;
    }
    
    
    function showLandingBlockMenu(node)
    {
        hideObjects(); //defined in general .js
        permissions_open = false;    
        jQuery("#ajaxDialogExpandLink").hide();
        jQuery('#ajaxCloseBox').attr('href','javascript:void(ajaxCloseDialog());');
        closeAjaxDialog = false;
        jQuery('#ajaxContainer').show();
        jQuery('#ajaxTitle').html('Please choose the type of module to add');
        jQuery('#ajaxDialogContents').html(document.getElementById(node).innerHTML);
    }
    function listFilterResults(baseURL,objId)
    {
			var url=baseURL + ((baseURL.indexOf('?')>0)?'&':'?') + 'fragment=y&searchfilter=' + encodeURI(document.getElementById('Filter-Search').value) ;
			jQuery('#' + objId).html("Searching...");
			jQuery('#' + objId).load(url, function(data, textStatus) {alertContents(data, textStatus);});
    }
    
    function ajaxLoadListingFragment(fragmentDiv,baseURL,path,filter,heading)
    {
		if (document.getElementById(fragmentDiv).innerHTML=='')
		{
			document.getElementById(fragmentDiv).style.display='block';
			jQuery('#' + fragmentDiv).html("Loading...");
			url=baseURL + '&fragment=y&filterpath=' + path + "&filterval=" + encodeURI(filter);
			jQuery('#' + fragmentDiv).load(url, function(data, textStatus) {alertContents(data, textStatus);});
		}
		else
		{
			document.getElementById(fragmentDiv).style.display=(document.getElementById(fragmentDiv).style.display == 'none')?'block':'none';
			
		}
			heading.className=(document.getElementById(fragmentDiv).style.display == 'none')?'filter_closed':'filter_open';
		
    }
    function ajaxOpenDialog(url,width,height,title,forceSize)
    {
        hideObjects(); //defined in general .js
        permissions_open = false;    
        //hide the full screen link as we may not need it
        jQuery("#ajaxDialogExpandLink").hide();
        lastURL = url;
        if (forceSize)
        {
            $('#ajaxDialog').attr("style", "margin-left:-" + Math.floor((width / 2)) + 'px !important;margin-top:-' + Math.floor((height / 2)) + 'px !important');
			document.getElementById('ajaxDialog').style.height = height + 'px';
			document.getElementById('ajaxDialog').style.width = width + 'px';
        }
        if (url.indexOf('listing.aspx')>0 | url.indexOf('/cms/associate/')>0 | url.indexOf('stat.aspx')>0)
        {
            jQuery('#ajaxCloseBox').attr('onclick','');
            jQuery('#ajaxCloseBox').attr('href','javascript:void(ajaxCloseDialog());');
            }
        else
        {
            jQuery('#ajaxCloseBox').attr('onclick','');
            jQuery('#ajaxCloseBox').attr('href','javascript:void(cancelAjaxPost());');
        }
        closeAjaxDialog = false;
        jQuery('#ajaxContainer').show();
        jQuery('#ajaxTitle').html(title);
        jQuery('#ajaxDialogContents').html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
        jQuery('#ajaxContainer').addClass('waiting');
        if (url.indexOf('?')>0)
            {url += '&dialog=y'}
        else
            {url += '?dialog=y'}
         
		jQuery('#ajaxDialogContents').load(url, function(data, textStatus) {alertContents(data, textStatus);});
        return false;
    }
    
    function ajaxCloseDialog()
    {
       jQuery('#ajaxContainer').hide();
       jQuery('#ajaxDialogContents').html('');
       showObjects(); //defined in general .js
       return false;
    }
    
    function deleteAssociation(path,parent,associationId,child,cardinality)
    {
    if (cardinality == "global" || cardinality == "child")
    {
        if (!confirm("This will permanently delete the item. Are you sure?")) return false;
    }
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "parent=" + encodeURI(parent);
        poststr += "&child=" + encodeURI(child);
        poststr += "&association=" + associationId;
        poststr += "&fieldPath=" + encodeURI(path);
        poststr += "&action=partial";
        poststr += "&cardinality=" + cardinality;
        poststr += "&subaction=DeleteAssociation";
        jQuery.ajax({
            type: "POST",
            url: loc,
            data: jQuery.query(poststr),
            success: function(data, textStatus) {
                if(data.substring(0,8)=='message:')
                    alert(data.substring(8));
                else {
                    jQuery('#fieldWrapper_'+ path).replaceWith(data);
                    alertContents(data, textStatus);
                }
            }
        });
        return false;
    }
    
    function deleteDocument(documentid) {
        if (!confirm("This will permanently delete the item. Are you sure?")) return false;
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "deletedocumentid=" + documentid;
        jQuery.ajax({
            type: "POST",
            url: loc,
            data: jQuery.query(poststr),
            success: function(data, textStatus) {
                if(data.substring(0,8)=='message:')
                    alert(data.substring(8));
                else {
                    jQuery('#row_'+ documentid).remove();
                    //alertContents(data, textStatus);
                }
            }
        });
        return false;
    }
    
  function copyDocument(path,parent,associationId,child,cardinality)
    {
    if (cardinality == "global" || cardinality == "child")
    {
        if (!confirm("Are you sure you want to copy this item?")) return false;
    }
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "parent=" + encodeURI(parent);
        poststr += "&child=" + encodeURI(child);
        poststr += "&association=" + associationId;
        poststr += "&fieldPath=" + encodeURI(path);
        poststr += "&action=partial";
        poststr += "&cardinality=" + cardinality;
        poststr += "&subaction=CopyDocument";
        jQuery.ajax({
            type: "POST",
            url: loc,
            data: jQuery.query(poststr),
            success: function(data, textStatus) {
                if(data.substring(0,8)=='message:')
                    alert(data.substring(8));
                else {
                    jQuery('#fieldWrapper_'+ path).replaceWith(data);
                    alertContents(data, textStatus);
                }
            }
        });
        return false;
    }
  
function alertContents(data,textStatus) {
	jQuery("#ajaxContainer").removeClass('waiting');
	dragDisabled = false;
    enableActionButtons();
    if (data.substring(0,7) =='failure')
		jQuery("#ajaxDialogContents").html(data);
    else if (data.substring(0,7)=='message')
        alert (data.substring(7));
    else {
		//onLoad();
        if (document.getElementById(objscriptpath + '_scriptlet'))
			 eval(document.getElementById (objscriptpath + '_scriptlet').value);
			     }
    if(closeAjaxDialog) 
        ajaxCloseDialog();
     //set the url for full screen
     if(document.getElementById("dialogPostURL")) {
            document.getElementById("ajaxDialogExpandLink").href = document.getElementById("dialogPostURL").value.replace('?dialog=y','');
            document.getElementById("ajaxDialogExpandLink").style.display = "inline";
      }
      onLoad();
}


function quickAddAssociation(parent,path,schemaKey,cardinality)
{
    var qA = document.getElementById(path + '-qa');
    var qAS = document.getElementById(path + '-qaselect');
    var child = qAS.value;
    var options ='';
    if(child=="" | child==null) return false;
    var qAE = qA.getElementsByTagName("input");
    for(var i=0;i<qAE.length;i++)
    {
        if (qAE[i].id.indexOf(path + '-qaoption-')==0)
        {   
            if (qAE[i].checked)
            {
                var oName = qAE[i].value;
                options += '<' + oName + '/>'
            }
        }
    }
    
    
    associateDocument(parent, path , schemaKey,child,cardinality,options )
}

function ajaxPostDialog(form,loc)
{
    jQuery("#ajaxContainer").addClass("waiting");
	objscriptpath = "ajaxdialog";    
    //select boxes
    var eS = form.getElementsByTagName("select");
    var postStr ="";
    for (var i=0;i<eS.length;i++)
    {
        if(eS[i].name != '' & eS[i].name !=null)
        postStr += eS[i].name + '=' + encodeURI(eS[i].value) + '&';
    }
    
    var eS = form.getElementsByTagName("textarea");
    for (var i=0;i<eS.length;i++)
    {
        if(eS[i].name != '' & eS[i].name !=null)
        
        postStr += eS[i].name + '=' + encodeURI(eS[i].value) + '&';
    }
    var eS = form.getElementsByTagName("input");
    for (var i=0;i<eS.length;i++)
    {
        if(eS[i].name != '' & eS[i].name !=null)
		{
			if ((eS[i].type=='checkbox' & eS[i].checked) || eS[i].type != 'checkbox')
			postStr += eS[i].name + '=' + encodeURI(eS[i].value) + '&';
		}
    }
    jQuery("#ajaxDialogContents").html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
	jQuery('#ajaxDialogContents').load(loc, jQuery.query(postStr), function(data, textStatus) {alertContents(data, textStatus);});
    return false;
}   


function ajaxPostDocument(form,loc)
{
    jQuery("#ajaxContainer").addClass("waiting");
    
    //select boxes
    var eS = form.getElementsByTagName("select");
    var postStr ="";
    for (var i=0;i<eS.length;i++)
    {
        if(eS[i].name != '' & eS[i].name !=null)
        postStr += eS[i].name + '=' + encodeURI(eS[i].value) + '&';
    }
    var eS = form.getElementsByTagName("textarea");
    for (var i=0;i<eS.length;i++)
    {
        if(eS[i].name != '' & eS[i].name !=null)
        
        postStr += eS[i].name + '=' + encodeURI(eS[i].value) + '&';
    }
    var eS = form.getElementsByTagName("input");
    for (var i=0;i<eS.length;i++)
    {
        if(eS[i].name != '' & eS[i].name !=null)

        postStr += eS[i].name + '=' + encodeURI(eS[i].value) + '&';
    }
    postStr += "&fieldPath=" + document.getElementById("ajaxFieldPath").value;
    postStr += "&action=partial";
    postStr += "&subaction=updateDocument";
    var loc = document.getElementById("dialogPostURL").value;
    closeAjaxDialog = true;
    jQuery("#ajaxDialogContents").html("<img src = '/cms/img/wait2.gif' style = 'left:50%;margin-left:-80px;top:45%;position:absolute;'/>");
	jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(postStr), function(data, textStatus) {alertContents(data, textStatus);});
    return false;
}   
    
function changeTemplate(obj,newURL)
{
    var orig = obj.value;
    if (confirm("Are you sure you want to change the template for this document? You will lose all unsaved data in doing so. In addition, you will lose saved data that is associated with this document that there is no place for in the new template."))
    {
        window.location.href = newURL;
        return true;
    }
    obj.value =origTemplateVal ; //set in field.xsl when the select box is created
    return false;
}

function ajaxSort(path,order,position)
{
    var loc = window.location.href;
    if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
    loc += '?partial=partial';
    var poststr = '';
    
    setPersistentCookie('sort_' + path,order + position);
    
    if (path == 'documentopenlist') {
		jQuery('#ajaxDialogContents'+ path).load(loc, function(data, textStatus) {alertContents(data, textStatus);});
    }
    else {
        poststr += "fieldPath=" + encodeURI(path);
        poststr += "&action=partial";
		jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(poststr), function(data, textStatus) {alertContents(data, textStatus);});
    }
    return false;
}

function ajaxHandleDrop(droppedOn,dropped) {
	if (dropped.className=='landingmoduleparent')
	{
		//re-order of block parent for modules
		var parentUL = dropped.parentNode;
	    var parentSiblings = jQuery("#" + parentUL.id + ">li")
	    var newOrder =0;
	    var previous=null;
	    for(i=0;i<parentSiblings.length;i++)
	    {
			newOrder++
			if(parentSiblings[i].id==dropped.id) 
			{
				break;
				}
				if (parentSiblings[i].id != '')
				{
				previous = parentSiblings[i];
				}
	    }
	    
	    if (previous == null)
	    {
			newOrder = 1 * document.getElementById(parentSiblings[1].id + "_origOrder").value;
		}
		else
		{
			newOrder = 1 * document.getElementById(previous.id + "_origOrder").value + 1;
		}
	    var oldOrder=document.getElementById(dropped.id + "_origOrder").value;
		var Path = dropped.id.split('_')[1];
	    AjaxChangeLandingBlockOrder (oldOrder,newOrder,Path);
	}
	else
	{
		var beforeId;	
		var Id;
		var Path;
		var aSplit = dropped.id.split('-XPATHX-')
		Id = aSplit[0].replace('association_','');
		Path = aSplit[1];

		if (droppedOn!=null)
		{
			aSplit = droppedOn.id.split('-XPATHX-')
			beforeId = aSplit[0].replace('association_','');
		}   
		else
		{
			beforeId =0;
		}
		disableActionButtons();
		ajaxPlaceItemBefore (beforeId,Id,Path);
	}
}

function ajaxPlaceItemBefore(beforeId,actualId,path)
    {
        var loc = window.location.href;
        if(loc.indexOf('#')>0) loc =loc.substring(0,window.location.href.indexOf('#'));
        var poststr = "beforeid=" + beforeId;
        poststr += "&itemid=" + actualId;
        poststr += "&fieldPath=" +  encodeURI(path);
        poststr += "&action=partial";
        poststr += "&subaction=PlaceItemBefore";
        dragDisabled = true;
		jQuery('#fieldWrapper_'+ path).load(loc, jQuery.query(poststr), function(data, textStatus) {alertContents(data, textStatus);});
        return false;
    }
    
function showWorkflowDialog(obj)
{
        hideObjects() // defined in general.js
        jQuery("#ajaxDialogExpandLink").hide();
        jQuery('#ajaxCloseBox').attr('onclick','').click(function() {alert("Please select an option first"); return false;});
        jQuery('#ajaxContainer').show();
        jQuery('#ajaxTitle').text("Workflow");
        jQuery('#ajaxDialogContents').html(jQuery(obj).html());
        jQuery('#ajaxForm :input:button').click(function() {
            jQuery('#mainForm').append('<input type="hidden" name="actionBarButton" value="' + jQuery(this).val() + '" />');
            jQuery('#mainForm').submit();
        });
        closeAjaxDialog = false; 
        return false;
}

function workflowReject(obj)
{
        hideObjects() // defined in general.js
        document.getElementById("ajaxDialogExpandLink").style.display = "none";
        document.getElementById('ajaxCloseBox').onclick = function(){ajaxCloseDialog ()};
        var aD = document.getElementById('ajaxContainer');
        var aDT = document.getElementById('ajaxTitle');
        document.getElementById("ajaxForm").action = obj.href;
        document.getElementById("ajaxForm").onsubmit = "";
        closeAjaxDialog = false; 
        var aDD = document.getElementById('ajaxDialog');
        aDT.innerHTML = "Workflow";
        aD.style.display = "block";
        aDD.className = 'ajaxDialog';
        document.getElementById("ajaxDialogContents").innerHTML = document.getElementById('workflowInformation').innerHTML;
        return false;
}

function reloadSchemas() {
    jQuery("body").addClass("waiting");
	jQuery.ajax({
		type : 'get', 
		url : '/CMS/reloadschemas.aspx', 
		success : function(data, textStatus) {
			ajaxCall = eval(data);
			alert(ajaxCall.schemas.length + ' schemas were loaded successfully.');
			window.location.href='/cms/sitemap.aspx';
            jQuery("body").removeClass("waiting");
		},
		error : function() {
			alert('There was an error reloading the schemas.');
            jQuery("body").removeClass("waiting");
		}
	});
	return false;
}

function ApplyCMSFilter(form,pageobjid,nodeName,postURL)
{
    var postStr ="";
    var eS = form.getElementsByTagName("input");
    for (var i=0;i<eS.length;i++)
    {
        if(eS[i].name != '' & eS[i].name !=null)
        if (eS[i].checked)
        {
			postStr += eS[i].name + '=' + encodeURI(eS[i].value) + '&';
        }
        else
        {
			postStr += eS[i].name + '=' + '&';
        }
    }
    postStr += "&fieldPath=" + pageobjid;
    postStr += "&nodeName=" + nodeName;
    postStr += "&action=partial";
    postStr += "&subaction=applyfilter";
    var loc=postURL;
    if (postURL = '') loc = window.location.href;// document.getElementById("dialogPostURL").value;
    var boxId
    if (pageobjid != '')
    {
		boxId = 'fieldWrapper_'+ pageobjid;
    }
    else
    {
		boxId = 'ajaxDialogContents';	
    }
    if (document.getElementById(boxId).getElementsByTagName("dl").length > 0)
    {
		document.getElementById(boxId).getElementsByTagName("dl")[0].style.opacity=0.25;
	}
	else
	{
		document.getElementById(boxId).getElementsByTagName("table")[0].style.opacity=0.25;
	}
	jQuery('#' + boxId).load(loc, jQuery.query(postStr), function(data, textStatus) {alertContents(data, textStatus);});
    return false;
}

