var dialog;

window.onfocus = DialogFocus;


function GetHref()
{
    var url;
	var href;
	var pos;
	
	pos = window.location.href.indexOf('?');
	if (pos != -1)
	{
		href = window.location.href.substring(0,pos);
	}
	else
	{
		href = window.location.href;
	}
	
	pos = href.indexOf('#');
	if (pos != -1)
	{
		href = href.substring(0,pos);
	}
	else
	{
		href = href;
	}
	return href;
}

function DialogFocus()
{
	if(dialog != null)
	{
		try
		{
			dialog.focus();
		}
		catch(x)
		{}
	}
}

function Edit_Content(app)
{
	var url;
	var href;
	
	href = GetHref();
		
	url = href + "?SiteNote_Mode=Edition";
	window.navigate(url);
}

function Design(app)
{
	var url;
	var href;
	
	href = GetHref();
		
	url = href + "?SiteNote_Mode=Design";
	window.navigate(url);
}


function DesignEdit(app)
{
	var url;
	var href;
	
	
	href = GetHref();
	
	url = href + "?SiteNote_Mode=DesignEdit";
	window.navigate(url);
}


function Catalog(app)
{
	var url;
	var href;
	
	href = GetHref();
		
	url = href + "?SiteNote_Mode=Catalog";
	window.navigate(url);
}

function Create_Page(app)
{
	var url;
	var returnUrl;
	var href;
	
	href = GetHref();
	
	url = app + "/Admin/CreatePage.aspx";
	url += "?CurrentPage=" + encodeURIComponent(href);
	
	dialog = window.open(url, null, "height=300,width=350,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=no,status=no,scrollbars=no");
	dialog.opener = window;
	dialog.moveTo((window.screen.availWidth - 400) / 2, (window.screen.availHeight - 150) / 2);
	dialog.focus();
	
}

function Page_Properties(app)
{
	var url;
	var returnUrl;
	var href;
	var pos;
	
	href = GetHref();
	
	url = app + "/Admin/PageProperties.aspx";
	url += "?CurrentPage=" + encodeURIComponent(href);
	dialog = window.open(url, null, "height=375,width=425,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=no");
	dialog.opener = window;
	dialog.moveTo((window.screen.availWidth - 400) / 2, (window.screen.availHeight - 150) / 2);
	dialog.focus();
}

function Page_Security(app)
{
	var url;
	var returnUrl;
	var href;
	var pos;
	
	href = GetHref();
	
	url = app + "/Admin/PageSecurity.aspx";
	url += "?CurrentPage=" + encodeURIComponent(href);
	dialog = window.open(url, null, "height=300,width=300,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=yes");
	dialog.opener = window;
	dialog.focus();
}

function Website_Administration(app)
{
	var url;
	url = app + "/Admin/Websites.aspx";
	window.navigate(url);
}

function Delete_Page(app, cmApp, defaultPage,parentPath)
{
	var result;
	var xmlHttp;
	var doc;
	var response;
	var responseDocument;
	var status;
	var post;
	var href;
	var pos;
	
	href = GetHref();
	
	if(window.confirm("Delete this page? (The page will be deleted immediately in BOTH the offline AND the online sites; the site hierarchy will be automatically published)"))
	{
		try
		{		
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			xmlHttp.Open("POST", app + cmApp + "/Admin/Webservices/PageSvc.asmx/DeletePage", false);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			post = "pageUrl=" + href;
			xmlHttp.Send(post);

			doc = xmlHttp.responseXML;

			if (doc.parseError.errorCode != 0 || doc.xml == '')
			{
				alert('Delete_Page - unable to execute request');
			}
			else
			{
				response = doc.documentElement.firstChild.text;
				
				/* MsXml modif */
				responseDocument = new ActiveXObject("Msxml2.DOMDocument");
				
				responseDocument.loadXML(response);
				
				if (responseDocument.parseError.errorCode != 0 || responseDocument.xml == '')
				{
					alert('unable to retrieve read response');
				}
				else
				{
					status = responseDocument.documentElement.firstChild;

					if (status.text == 'succeed')
					{
						//alert('Delete page succeed');
						if(parentPath != "")
						{
						    window.navigate(parentPath);
						}
						else
						{
						    window.navigate(app + defaultPage + ".off.aspx");
						}
					}
					else
					{
						alert('Delete page failed');
					}
				}
			}	
		}
		catch(e)
		{
			alert('Delete page Error');
			alert(e.message);
		}	
	}
	
}

function LogOut(app,absPath)
{
	//if(window.confirm("Do you want to quit SiteNote Editor?(if you have not published the pages, your modifications will not appear online)"))
	//{
		var url;
		url = app + "/Admin/Logout.aspx?absPath=" + absPath;
		window.navigate(url);
	//}
}

function View_Basket(app)
{
	var url;
	var returnUrl;
	
	url = app + "/Admin/Basket.aspx";
	dialog = window.open(url, null, "height=500,width=400,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=yes");
	dialog.opener = window;
	dialog.focus();
}

function Back_Page(app)
{
	var tmp;
	history.back();
}

function Move_Page(position, app)
{
	var result;
	var xmlHttp;
	var doc;
	var response;
	var responseDocument;
	var status;
	var post;
	var href;
	var pos;
	
	href = GetHref();
		
	try
	{		
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		xmlHttp.Open("POST", app + "/Admin/Webservices/PageSvc.asmx/MovePage", false);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		post = "pageUrl=" + encodeURIComponent(href);
		post += "&position=" + position;
		xmlHttp.Send(post);

		doc = xmlHttp.responseXML;

		if (doc.parseError.errorCode != 0 || doc.xml == '')
		{
			alert('Move_Page - unable to execute request');
		}
		else
		{
			response = doc.documentElement.firstChild.text;
			//alert(response);
			
			/* MsXml modif */
			responseDocument = new ActiveXObject("Msxml2.DOMDocument");
			
			responseDocument.loadXML(response);
			if (responseDocument.parseError.errorCode != 0 || responseDocument.xml == '')
			{
				alert('unable to retrieve read response');
			}
			else
			{
				status = responseDocument.documentElement.firstChild;
				
				if (status.text == 'succeed')
				{
					//alert('Move page succeed');
					location.href = location.href
				}
				else
				{
					//alert('Move page failed');
				}
			}
		}	
	}
	catch(e)
	{
		alert('Move page Error');
		alert(e.message);
	}	
}

function Save_Page()
{
	__doPostBack('', null);
}

function ViewHide(chkView,app,cmApp)
{
	var pos;
	var post;
	var href;
	var url;
	try
	{
		href = GetHref();
		
		post = "?pageUrl=" + encodeURIComponent(href);
					
		switch(chkView.name)
		{
			case "viewHidden":
				post += "&pagetype=hidden";
				break;
			case "viewOffline":
				post += "&pagetype=offline";
				break;
			default:break;
		}
		post += "&ischecked="+chkView.checked;
		url = app + cmApp + "/Admin/ChangeParam.aspx"+post;
		window.navigate(url);
	}
	catch(e)
	{
		alert('Error');
		alert(e.message);
	}
}

function ViewHiddenCatalogZone()
{
    if(document.all["CatalogZoneDiv"] != null)
    {
        if (document.all["CatalogZoneDiv"].style.display == '')
        {
            document.all["CatalogZoneDiv"].style.display = 'none';
        }
        else
        {
            document.all["CatalogZoneDiv"].style.display = '';
        }
   }
}

if (location.href.indexOf('AutoSave=True') != -1)
{
    setTimeout('AutoSave()',5000);
}

function AutoSave()
{
    var buttonSave;
    buttonSave = document.all['ctl00_SAVE_PAGE'];
    if (buttonSave != null)
    {
       buttonSave.click();
    }
}




            
        
    
    

