//
//	cslibrary.js
//
//	Client side script support for Website CMS
//
//					Copyright (c) 2003 -- 2005 Sky High Interactive
//		All rights reserved. Unauthorized use or disclosure strictly prohibited.
//
	// True for debugging, biggest effect is that dialogs open in scrollable windows so trace output is visible.
var DEBUG = false;	

	//
	//	Handles Postback failures
	//
function WCMSPostBackFailed(s)
{
	alert("WebsiteCMS error:\n\nPostback failed with message '" + s + "'\n\nDid you include a:\n\n    <wcms:WebsiteCMSPostBack Id='WCMSpb' runat='server' />\n\nserver control in the page template?");
}
	//
	//	Not yet implemented
	//
function NotYetImplemented(s)
{
	alert("WebsiteCMS client-side error:\n\n" + s + " has not yet been implemented");
}

function FileUploadPicture()
{
	FileUpload('picture');
}

function FileUploadDocument()
{
	FileUpload('document');
}

function FileUploadOther()
{
	FileUpload('other');
}

function FileImportPicture()
{
	FileImport('picture');
}

function FileImportDocument()
{
	FileImport('document');
}

function FileImportOther()
{
	FileImport('other');
}

function wcmsProcessRegionMenuClick(sender, eventArgs) 
{
	var sAction = eventArgs.Item.NavigateUrl;
	var i = sAction.indexOf("?");
	sAction = sAction.substring(i + 1);
	// Next line causes the function named in the NavigateUrl to be called with the CB id as a parameter
	try
	{
	    eval(sAction + "('" + sender.ID + "')");
	}
	catch (X)
	{
	    alert("WebsiteCMS internal error: could not call the function '" + sAction + "()'");
	}
	// Cancels further processing
	return false;
}

	// Displays the About box on the client side
AboutBox = [
	'<DIV STYLE="position:absolute; top:0; left:0; width:520px; height:260px; border:1px solid black;">', 
	'<table cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="#e8e8e8" align="left" valign="top"><br/><br/><img src="images/SHIlogowithgraybg.jpg"/></td><td bgcolor="white">',
	'<DIV STYLE="padding:20px; font-size:8pt; line-height:1.5em; font-family:verdana; color:black;"><center><img src="Images/SHC_260x94.jpg"><br/><br/>',
		'<strong>Version 4.0</strong><br/><br/>&copy;2003-2006 Sky High Interactive. All rights reserved.<br/><br/></center><span style="text-align:justify">',
		'Your use of this software is governed by the terms of<br/> your license agreement. Unauthorized use or disclosure<br/>is strictly prohibited.',
		'</span><center><br/><button style="width:40px;" onclick="parent.AboutHide();">OK</button></center></DIV></td></tr></table></DIV>'
];
	// Create and display the splash screen, centered in the edit window
var oPopup = null;
var sAbout = "";
function About()
{
	var a = AboutBox;
	if (oPopup == null){
		oPopup = window.createPopup();
		for (var i = 0; i < a.length; i++)
			sAbout += a[i];
	}
	 var oPopupBody = oPopup.document.body;
	
	 oPopupBody.innerHTML = sAbout;
	    // Center the popup if possible
	 var Y = document.body.clientHeight;
	 var X = document.body.clientWidth;
	 if (X < 520) 
	    X = 0;
	 else
	    X = (X - 520)/2;
	 if (Y < 339) 
	    Y = 0;
	 else
	    Y = (Y - 339)/2;
	 oPopup.show(X, Y, 520, 339, document.body);
	 document.body.onmousedown = oPopup.hide;
}

function AboutHide()
{
	oPopup.hide();
}

	// Worker functions for opening a modal dialog page and a non-modal window
function WCMSOpenPage(sPage, P, sWidth, sHeight)
{
	if (DEBUG){
		var sSize = " width=" + sWidth + ",height=" + sHeight + ",left=150,top=100"; 
		var result = window.open(sPage, "WCMSDialog", "status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=yes," + sSize, true); 
		return result;
	} else {
		var sW = " dialogWidth:" + sWidth + "px;"; 
		var sH = " dialogHeight:" + sHeight + "px;";
		var result = window.showModalDialog(sPage, P, "status:no; center:yes; help:no; minimize:no; maximize:no; scroll:no; border:thin; statusbar:no; " + sH + sW); 
		return result;
	}
}

function WCMSOpenWindow(sPage, sWidth, sHeight, sTitle)
{
	var sSize = " width=" + sWidth + ",height=" + sHeight + ",left=150,top=100"; 
	var result = window.open(sPage, sTitle, "status=yes,toolbar=no,scrollbars=auto,menubar=no,location=no,resizable=yes," + sSize, true); 
}

	// Dialog box handler for the 'New Content Block' menu operation
function CBNew()
{
	var P = new Object();
		// Pass in the region list for this page as the parameters
	P.WCMSRegions = WCMSRegions;
		// These are set up by the dialog
	P.NewRegion = "";
	P.NewSequence = -1;
	var result = WCMSOpenPage("Scripts/CBNew.htm", P, "540", "350"); 
	if (result){
		try {
			__doPostBack("WebsiteCMS", "NewCB:" + P.NewRegion + ":" + P.NewSequence);
		} catch (X) {
			WCMSPostBackFailed(X.message);
		}
	}
}

	// Properties dialog box handler for the region menu
function CBProperties(sCB)
{
	var P = new Object();
		// Pass in the descriptor for this CB and the region list for this page as the parameters
		// sCB looks like this: tlrkRootWcmsregion1_Region_Menu_Header_CB_0_i9
	P.ID = sCB;
	P.WCMSRegions = WCMSRegions;
	P.OrigRegion = "";		// These are set up by the dialog
	P.NewRegion = "";
	P.OrigSequence = -1;
	P.NewSequence = -1;
	P.AllPages = false;
	
	var result = WCMSOpenPage("CBProperties.aspx?PAGE=" + WCMSActivePage + "&CB=" + sCB, P, "720", "675"); 
	if (result){
		try {
			__doPostBack("WebsiteCMS", "PropertiesCB:" + P.OrigRegion + ":" + P.OrigSequence + ":" + P.NewRegion + ":" + P.NewSequence + ":" + P.AllPages);
		} catch (X) {
			WCMSPostBackFailed(X.message);
		}
	}
}

	// Status dialog box handler for the region menu
function CBStatus(sCB)
{
	var P = new Object();
		// Pass in the descriptor for this CB and the region list for this page as the parameters
		// sCB looks like this: Region_Menu_Header_CB_0_i9
	P.ID = sCB;
	var result = WCMSOpenPage("CBStatus.aspx?CB=" + sCB, P, "740", "400"); 
}

	// Start review of a CB
function CBBeginReview(sCB)
{
	if (!confirm("Begin the review of this content block?")) return;
	var a = sCB.split("_");
	try {
		__doPostBack("WebsiteCMS", "BeginReviewCB:" + a[3] + ":" + a[5]);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Stop review of a CB
function CBStopReview(sCB)
{
	if (!confirm("Withdraw this content block from review?")) return;
	var a = sCB.split("_");
	try {
		__doPostBack("WebsiteCMS", "StopReviewCB:" + a[3] + ":" + a[5]);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Delete a CB
function CBDelete(sCB)
{
	if (!confirm("Are you sure you want to delete this content block?")) return;
	var a = sCB.split("_");
	try {
		__doPostBack("WebsiteCMS", "DeleteCB:" + a[3] + ":" + a[5]);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Duplicate a CB
function CBDuplicate(sCB)
{
	var a = sCB.split("_");
	try {
		__doPostBack("WebsiteCMS", "DuplicateCB:" + a[3] + ":" + a[5]);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Publish a CB
function CBPublish(sCB)
{
	var a = sCB.split("_");
	try {
		__doPostBack("WebsiteCMS", "PublishCB:" + a[3] + ":" + a[5]);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Approve a CB
function CBApprove(sCB)
{
	var a = sCB.split("_");
	try {
		__doPostBack("WebsiteCMS", "ApproveCB:" + a[3] + ":" + a[5]);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Withdraw a CB
function CBWithdraw(sCB)
{
	if (!confirm("Are you sure you want to withdraw this content block?")) return;
	var a = sCB.split("_");
	try {
		__doPostBack("WebsiteCMS", "WithdrawCB:" + a[3] + ":" + a[5]);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Edit a CB in a separate window
function CBEditInPopup(sCB)
{
	var a = sCB.split("_");
	    // Sized so it fits on an 800 x 600 screen
	var result = WCMSOpenPage("CBEditor.aspx?Page=" + WCMSActivePage + "&Region=" + a[3] + "&Sequence=" + a[5], null, "792", "610"); 
	try {
		__doPostBack("WebsiteCMS", "Refresh:");
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}
}

	// Reviewer comment on a CB
function CBComment(sCB)
{
	var a = sCB.split("_");
	var a = sCB.split("_");
	var result = WCMSOpenPage("CBComment.aspx?Page=" + WCMSActivePage + "&Region=" + a[3] + "&Sequence=" + a[5], null, "502", "540"); 
}

	// Do the admin login
function AdminLogin()
{
		// This reference only exists when we are in Admin Mode, so we are already logged in
	if (typeof(WCMSActivePage) != "undefined") return;
		// Left button click opens up an "advertising" dialog, right click goes to the login dialog
	if (event.button != 2)
	{
	    About();
	    //window.event.cancelBubble = true;
	    return;
	}
	var result = WCMSOpenPage("WCMSlogin.aspx", null, "400", "340"); 
	window.event.cancelBubble = true;
	try 
	{
		__doPostBack("WebsiteCMS", "Refresh:");
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}	
}

	// File New Page menu operation
function FilePageNew()
{
	var result = WCMSOpenPage("WCMSFileNewPage.aspx", null, "650", "320"); 
}

function FilePageDelete()
{
	if (!confirm("Are you sure you want to delete this page from the website?")) return;
	try {
		__doPostBack("WebsiteCMS", "PageDelete:" + WCMSActivePage);
	} catch (X) {
		WCMSPostBackFailed(X.message);
	}	
}

function FilePageProperties()
{
	var result = WCMSOpenPage("WCMSFilePageProperties.aspx?PAGE=" + WCMSActivePage, null, "650", "280"); 
}

function FileUpload(s)
{
	var result = WCMSOpenPage("WCMSFileUpload.aspx?TYPE=" + s, null, "650", "320"); 
}

function FileImport(s)
{
	var result = WCMSOpenPage("WCMSFileImport.aspx?TYPE=" + s, null, "650", "320"); 
}

function NewUserAccount()
{
	var result = WCMSOpenPage("WCMSAdministrationNewUserAccount.aspx", null, "650", "630"); 
}

function DisplayUserAccount()
{
	var result = WCMSOpenPage("WCMSAdministrationDisplayUserAccount.aspx", null, "650", "320"); 
}

function ReportSiteVisits()
{
	var result = WCMSOpenPage("WCMSAdministrationReportSiteVisits.aspx", null, "650", "320"); 
}

function Backup()
{
	var result = WCMSOpenPage("WCMSAdministrationBackup.aspx", null, "650", "320"); 
}

function Advertising()
{
	var result = WCMSOpenPage("WCMSApplicationAdvertising.aspx", null, "650", "320"); 
}

function OnlineStore()
{
	var result = WCMSOpenPage("WCMSApplicationOnlineStore.aspx", null, "650", "320"); 
}

	// CB editor help
function CBEditorHelp()
{
	WCMSOpenWindow("CBEditorHelp.aspx", "490", "510", "CBEDitorHelp"); 
}
	// WebsiteCMS help
function WebsiteCMSHelp()
{
	WCMSOpenWindow("WebsiteCMSHelp.htm", "490", "510", "CBEDitorHelp"); 
}

	//
	//	General client side support functions
	//
	// Add an <OPTION value="sValue">sDisplay<OPTION> tag to the referenced SELECT control
function AddSelectOption(oSelect, sValue, sDisplay)
{
	if (oSelect == null) return;
	var o = document.createElement("OPTION");
	o.value = sValue.toString();
	o.innerText = sDisplay.toString();
	oSelect.appendChild(o);
}
	//	ClearSelections()
	//
	//	Unselects all the selected items in the named SELECT control
function ClearSelections(sSelect)
{
	var o = document.getElementById(sSelect);
	if (o == null) return;
	for (var i = 0; i < o.length; i++)
		o.options[i].selected = false;
}
