// Client side session management

// Core input tags
var cFormInputSubmitFlag    = "submitflag";
var cFormInputSubmitName    = "submitformname";
var cFormInputSubmitPage    = "submitformpage";
var cFormInputClientState   = "clientstate";
var cFormInputTodo   		= "todo";

// Core form names
var cFormNameLink       	= "linkform";
var cFormNameUpload       	= "uploadform";

// Core HTML requests
var cRequestAspPage         	= "asppage"
var cRequestUploadImage     	= "uploadimage"
var cRequestUploadPID   		= "uploadpid";
var cRequestAutologin   		= "autologin";
var cRequestGetDataFromParent	= "getdatafromparent";
var cRequestHelpHeight   		= "helpheight";
var cRequestHelpSubject   		= "helpsubject";

var gPageLoaded = false;
var gPageFormReady = false;
var gFormSubmited = false;

var gImageCache = new Array();

var gDateValue = new Date();

var gClientState;

var gPopupHelpWindow;

var gWebsitePage = "";
var gPopupHelpPage = "";

var cSID_Reserved = 0;

var gSID_Count = "1"; // equal to the number of properties and is a string literal!

var gWindowStatus = "Done";

var gSpecialRequest = "";

var gToDo = 0;

function AspSmithInit(WebsitePage, SID_Count, PopupHelpPage, Todo)
{
	gWebsitePage = WebsitePage;
	gPopupHelpPage = PopupHelpPage;
	
	gSID_Count = SID_Count;
	
	gToDo = Todo;
}

function AddSpecialRequest(SpecialRequest)
{
	gSpecialRequest = gSpecialRequest + (gSpecialRequest.length == 0 ? "" : "&") + SpecialRequest;
}

function AddImageToCache(ImageKey, ImageSource)
{
	gImageCache[ImageKey] = new Image();
	gImageCache[ImageKey].src = ImageSource;
}

function GetSessionProperty(PropID)
{
	return (gClientState[PropID]);
}

function SetSessionProperty(PropID, PropValue)
{
	var SessionString;
	
	gClientState[PropID] = PropValue;
	
	SessionString = gClientState.toString()
	
	document.getElementById(cFormNameLink).clientstate.value = SessionString;
}

function UpdateSessionData()
{
	var i;
	var forms;
	
	if (gClientState.length < 2) {
		gWindowStatus += " (New Client-State) ";
		SetSessionProperty(cSID_Reserved, gSID_Count);
	}
	
	window.status = gWindowStatus;
}

function AlternateDisplay(ElementId)
{
	var ElementRef = document.getElementById(ElementId);
	
	ElementRef.style.display = (ElementRef.style.display == "none") ? "block" : "none";
}

function GotoPage(PageLink)
{
	if (!gPageFormReady) {return}

	document.getElementById(cFormInputSubmitFlag).value="link";
	document.getElementById(cFormNameLink).action=gWebsitePage + "?" + cRequestAspPage + "=" + PageLink + (gSpecialRequest.length == 0 ? "" : "&") + gSpecialRequest;
	document.getElementById(cFormNameLink).submit();
}

function GotoPageEx(PageName /*, [PageRequest]*/ ) 
{
	var RequestBuffer = "";
	var PageRequest = (arguments.length > 1) ? arguments[1] : "";
	
	if (!gPageFormReady) {return}
	
	RequestBuffer = PageRequest
	RequestBuffer = RequestBuffer + ((RequestBuffer.length > 0 && gSpecialRequest.length > 0) ? "&" : "") + gSpecialRequest

	document.getElementById(cFormInputSubmitFlag).value="link";
	document.getElementById(cFormNameLink).action=PageName + (RequestBuffer.length == 0 ? "" : "?") + RequestBuffer;
	document.getElementById(cFormNameLink).submit();
}

function SubmitForm(TargetPage)
{
	if (!gPageFormReady) {return}
	//if (gFormSubmited) {return}
	
	//gFormSubmited = true;

	document.getElementById(cFormInputSubmitFlag).value="submit";
	document.getElementById(cFormNameLink).action=gWebsitePage + "?" + cRequestAspPage + "=" + TargetPage + (gSpecialRequest.length == 0 ? "" : "&") + gSpecialRequest;
	document.getElementById(cFormNameLink).submit();
}

function SubmitToForm(FormName, TargetPage)
{
	if (!gPageFormReady) {return}

	document.getElementById("submitformname").value=FormName;
	
	SubmitForm(TargetPage);
}


/////////////////////////////////////////////////////////
// FormAction object

// formAction.toString >> BuildActionString() 
function BuildActionString()
{
	var i;
	var Request;
	
	Request = "";
	
	for (i = 0; i < this.request.length; i++) {
		if (this.request[i].length > 0) {
			if (Request.length > 0) {
				Request += "&";
			}
			Request += this.request[i];
		}
	}
	
	if (Request.length > 0) {
		return(this.url + "?" + Request);
	} else {
		return(this.url);
	}
}

// formAction.getRequest(Param) 
function getRequest(Param)
{
	var i;
	var Request;
	
	for (i = 0; i < this.request.length; i++) {
		if (this.request[i].length > 0) {
			Request = this.request[i].split("=");
			if (Request[0] == Param) {
				return (Request[1]);
			}
		}
	}
	
	return ("");
}

// formAction.addRequest(Param, Value) 
function addRequest(Param, Value)   
{
	var i;
	var Request;
	
	for (i = 0; i < this.request.length; i++) {
		if (this.request[i].length > 0) {
			Request = this.request[i].split("=");
			if (Request[0] == Param) {
				this.request[i] = Param + "=" + Value;
				return;
			}
		}
	}
	this.request[this.request.length] = Param + "=" + Value;
}

// formAction.deleteRequest(Param) 
function deleteRequest(Param)  
{
	var i;
	var Request;
	
	for (i = 0; i < this.request.length; i++) {
		if (this.request[i].length > 0) {
			Request = this.request[i].split("=");
			if (Request[0] == Param) {
				this.request[i] = "";
				return;
			}
		}
	}
}

function formAction(action)
{
	var actionstring;
	var urlarray;
	
 	actionstring = new String(action);

	urlarray = actionstring.split("?");
	
	// the URL part of the action string
    this.url = new String(urlarray[0]);

    // the array of requests
	if (urlarray.length == 1) {
    	this.request = new Array();     
	} else {
    	this.request = urlarray[1].split("&");     
	}
	
	// Get current action string
	this.toString = BuildActionString;

    // Get request function
    this.getRequest = getRequest;   

    // Add request function
    this.addRequest = addRequest;   

    // Delete request function
    this.deleteRequest = deleteRequest;  
}

/////////////////////////////////////////////////////////
// ASPUpload support

function SubmitUploadForm(barRef)
{
	ShowUploadProgress(barRef);
	
  	document.getElementById(cFormNameUpload).submit();
}

function ShowUploadProgress(barRef)
{
  	var strAppVersion = navigator.appVersion;
	var winstyle;
  
    if (strAppVersion.indexOf("MSIE") != -1 && strAppVersion.substr(strAppVersion.indexOf("MSIE") + 5, 1) > 4) {
		if (strAppVersion.indexOf("Macintosh") != -1 && strAppVersion.charAt(0) >= 3 ) {
			window.open("&b=NN","","width=370, height=115", true);
		} else {
      		winstyle = "dialogWidth=375px; dialogHeight:130px; center:yes";
      		window.showModelessDialog(barRef + "&b=IE", null, winstyle);
		}
    } else {
      	window.open(barRef + "&b=NN", "", "width=370,height=115", true);
    }
}

function InitUploadForm(UploadPID, ClientState, SubmitName, SubmitPage, Todo)
{
	var formaction;
	
	formaction = new formAction(document.getElementById(cFormNameUpload).action);

	formaction.addRequest(cRequestUploadPID, escape(UploadPID));
	
	formaction.addRequest(cFormInputClientState, ClientState.toString());
	
	formaction.addRequest(cFormInputSubmitName, SubmitName);
	formaction.addRequest(cFormInputSubmitPage, SubmitPage);

	formaction.addRequest(cFormInputTodo, Todo);

	formaction.addRequest(cRequestUploadImage, "true");

	document.getElementById(cFormNameUpload).action=formaction.toString();
}

function PopupHelp(HelpTopic) // PopupHelp(HelpTopic, [FixedHeight as integer], [FixedWidth as integer])
{
	var URL;
	var FixedHeight;
	var FixedWidth;
	
	if (gPopupHelpWindow) {
		gPopupHelpWindow.close;
	}

	if (arguments.length > 1) {
		FixedHeight = arguments[1];
		
		if (arguments.length > 2) {
			FixedWidth = arguments[2];
		} else {
			FixedWidth = 500;
		}
	} else {
		FixedHeight = -1;
	}

	//URL = new String(gWebsitePage + "?" + cRequestAspPage + "=" + gPopupHelpPage + "&autologin=0&getdatafromparent=1&helpheight=" + FixedHeight.toString() + "&helpsubject=" + HelpTopic);
	URL = new String(gPopupHelpPage + "?todo=" + gToDo.toString() + "&autologin=0&getdatafromparent=1&helpheight=" + FixedHeight.toString() + "&helpsubject=" + HelpTopic);
		
	//if (FixedHeight == -1) {
		//gPopupHelpWindow = window.open(URL, "subWindow",
		 	//"menubar=0,status=0,toolbar=0,location=0,resizable=0,scrollbars=0" + 
			//",left=100,top=100,height=100,width=" + FixedWidth);
	//} else {
		//gPopupHelpWindow = window.open(URL, "subWindow",
			//"menubar=0,status=0,toolbar=0,location=0,resizable=1,scrollbars=1" + 
			//",left=100,top=100,height=" + FixedHeight + ",width=" + FixedWidth);
	//}

	gPopupHelpWindow = window.open(
		URL, 
		"subWindow",
		"menubar=0,status=0,toolbar=0,location=0,resizable=1,scrollbars=1,left=100,top=100" + 
		(FixedHeight == -1 ? ",width=" + FixedWidth : ",height=" + FixedHeight + ",width=" + FixedWidth));

	gPopupHelpWindow.focus();

	return(false);
}

function PopupAspPage(URL) // PopupAspPage(URL, [newWindow as boolean], [FixedHeight as integer], [FixedWidth as integer])
{
	var newWindow;
	var FixedHeight;
	var FixedWidth;

	if (arguments.length > 1) {
		newWindow = arguments[1];
			
		if (arguments.length > 2) {
			FixedHeight = arguments[2];
			
			if (arguments.length > 3) {
				FixedWidth = arguments[3];
			} else {
				FixedWidth = 500;
			}
		} else {
			FixedWidth = -1;
		}
	} else {
		newWindow = false;
	}

	//return(PopupWindow(gWebsitePage + "?" + cRequestAspPage + "=" + URL + "&autologin=0", newWindow));
	if (URL.indexOf("?") == -1) {
		return(PopupWindow(URL + "?autologin=0&todo=" + gToDo.toString(), newWindow, FixedHeight , FixedWidth));
	} else {
		return(PopupWindow(URL + "&autologin=0&todo=" + gToDo.toString(), newWindow, FixedHeight , FixedWidth));
	}
}

function PopupWindow(URL, newWindow, FixedHeight , FixedWidth) // PopupWindow(URL, [newWindow as boolean])
{
	return(window.open(URL, newWindow ? "newWind" : "subWindow", "menubar=0,status=0,toolbar=0,resizable=1,scrollbars=1,left=100,top=100,height=" + FixedHeight + ",width=" + FixedWidth));
}

function ShowYesNoDialog(DlgArguments)
{
	var DialogValue;
 
	DialogValue = showModalDialog("SSI/AspSmithYesNoDialog.htm", DlgArguments, "status:no; resizable:yes");
	 
	return(DialogValue);
}

function getIEVersionNumber()
{
	var ua = navigator.userAgent;
	if (ua.indexOf("Opera") != -1) return 0;
	var IE = ua.indexOf("MSIE ");
	if (IE == -1) return 0;
	return parseFloat(ua.substring(IE+5, ua.indexOf(";", IE)));
}

/////////////////////////////////////////////////////////
// Image-Swap support

function OxPreloadImages()
{ //v3.0
	var d=document;
	
	if (d.images) { 
		if(!d.PreloadStore) d.PreloadStore=new Array();
		
    	var i, j=d.PreloadStore.length, a=OxPreloadImages.arguments; 
		
		for(i = 0; i < a.length; i++) {
    		if (a[i].indexOf("#") !=0 ) {
				d.PreloadStore[j] = new Image; 
				d.PreloadStore[j++].src = a[i];
			}
		}
	}
}

function OxSwapImgRestore()
{ //v3.0
	var i,x,a=document.SwapStore;
	
	for(i=0; a && i < a.length && (x=a[i]) && x.oSrc; i++) x.src=x.oSrc;
}

function OxFindObj(n, d)
{ //v4.01
	var p,i,x;
	
	if(!d) d=document; 
	
	if((p = n.indexOf("?")) > 0 && parent.frames.length) {
    	d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
  	
	if(!(x=d[n])&&d.all) x=d.all[n]; 
	
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	
  	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=OxFindObj(n,d.layers[i].document);
	
  	if(!x && d.getElementById) x=d.getElementById(n); 
	
	return x;
}

function OxSwapImage()
{ //v3.0
	var i,j=0,x,a=OxSwapImage.arguments; 
	
	document.SwapStore=new Array; 
	
	for(i=0; i < (a.length-2); i+=3) {
        if ((x=OxFindObj(a[i]))!=null) {
			document.SwapStore[j++]=x;
			if(!x.oSrc) x.oSrc=x.src; 
			x.src=a[i+2];
		}
	}
}
