<html>
	<head>
	<title>Dialogfenster zur Bezahlsimulation (zum Testen)</title>
		<style type='text/css'>
		<!--
			body
			{
				margin:0px;
				font-family:Arial;
				font-size:9pt;
			}
			td
			{
				font-family:Arial;
				font-size:9pt;
			}
		//-->
		</style>
		<script type='text/jscript'>
		
			// We still need some global objects in order to emulate some kind of "session"
			// First, our currency formatter (used with all the numbers)
		var CurFormatter;
			// Then, the Post Object. Needed in order to communicate with our webserver
		var PostObj = new Object();
			// The User Object holds all user-relevant information
		var UserObj = new Object();
			// And the number of open posts started by the "OnSiteCashDebit"-function
		var openposts = 0;
			// Here all our posts will land
		var ServerPage = "http://vision.sitekiosk.net/nocoinsys/userpost.php";
		
		
			// ---- Function ----
			// InitDialog
			// ---- Parameters --
			// none
			// ------------------
			// this function is called while initializing the page
		function InitDialog()
		{
				// we need to set up our SiteKiosk-interface
			window.external.InitScriptInterface();
				
				// Init the ScriptDevice
				// The account needs to be enabled in order to debit/credit it
			ScriptDevice.AccountEnabled = true;
			
				// Volatile means the accountmoney can change spontaneously
			ScriptDevice.Volatile = true;
				
				// Retransfer enables the Device to handle the money itself, when user wants to log out
			ScriptDevice.RetransferEnabled = true;
			
				// Eventhandler for the log out
			ScriptDevice.OnCheckRetransfer = OnCheckRetransfer;
				// Eventhandler for the log out
			ScriptDevice.OnRetransfer = OnRetransfer;
			
				// Eventhandler for "Volatile" mode
			ScriptDevice.OnSiteCashDebit = OnSiteCashDebit;
			
				// Create our currency formatter
			CurFormatter = SiteKiosk.Plugins("SiteCash").CreateCurrencyFormatter();
			
				// Create the post object
			PostObj = SiteKiosk.Network.CreateHTTPPost();
				// bind the eventhandler used when the post returns
			PostObj.OnPostComplete = OnPostComplete;
			
			
				// Debug-notifications
			SiteKiosk.Logfile.Notification("ScriptDevice Initialized!");
			SiteKiosk.Logfile.Notification("ScriptDevice's remaining money (last session): " + ScriptDevice.Balance);
		}


			// ---- Function ----
			// FormSubmit
			// ---- Parameters --
			// none
			// ------------------
			// User gave her login-infos and clicked on "OK" - create HTTP-Post Object and 
			// send to the server
		function FormSubmit()
		{
				// Debug notification
			SiteKiosk.Logfile.Notification("ScriptDevice Logging in!");
			
				// Prearrange the Post Object parameters
			PostObj.OnPostComplete = OnPostComplete;
			PostObj.AddParameter("login", "SiteKiosk");
			PostObj.AddParameter("action", "login");
			PostObj.AddParameter("login_name", edtLoginName.value);
			PostObj.AddParameter("login_password", edtLoginPassword.value);
			
			UserObj.name = edtLoginName.value;
			UserObj.password = edtLoginPassword.value;
				
				// And posting...
			PostObj.Submit(ServerPage);
		}
		
		
			// ---- Event --------
			// OnPostComplete
			// ---- Parameters --
			// success - >= 0 means "OK", post was successful
			// returnvalue - the page that was actually thrown by the server
			// ------------------
			// User wanted to log in, server sent an answer where extended info to 
			// the user is given
		function OnPostComplete(success, returnvalue)
		{
				// Debug notification
			SiteKiosk.Logfile.Notification("Got answer from Server: " + returnvalue);
			
				// Parse our return value and put it into local variables
			ParseReturnValue(returnvalue, false);
				// To be sure, create a new post object
			PostObj = SiteKiosk.Network.CreateHTTPPost();
			PostObj.OnPostComplete = OnPostComplete;
		}
		
		
			// ---- Event -------
			// OnCheckRetransfer
			// ---- Parameters --
			// amount - the amount that's retransferred (after logout)
			// ------------------
			// SiteKiosk calls this function before it really retransfers the money
			// returnvalue "true" means that everything can be transferred
			// "false" means "stop event chain here"
		function OnCheckRetransfer(amount)
		{
			return true;
		}


			// ---- Event -------
			// OnRetransfer
			// ---- Parameters --
			// amount - the amount that's retransferred (after logout)
			// ------------------
			// SiteKiosk got the OK to transfer the money back to the user 
			// (or, in this case - the Database)
		function OnRetransfer(amount)
		{
				// We want a new event handler for our post object (can't be handled the same way as the other ones)
			PostObj.OnPostComplete = OnREPostComplete;
				// Debug notification
			SiteKiosk.Logfile.Notification("ScriptDevice Logout  Amount: " + amount);
				
				// Parameters for the server
				// for details, have a look at the other part of the tutorial (Server-part)
			PostObj.AddParameter("login", "SiteKiosk");
			PostObj.AddParameter("action", "setmoney");
			PostObj.AddParameter("MoneyBack", ScriptDevice.Balance + amount);
			PostObj.AddParameter("login_name", UserObj.name);
			PostObj.AddParameter("login_password", UserObj.password);
				// Now, send the request to the server!
			PostObj.Submit(ServerPage);
			
				// We need to debit the account manually in order to reset our SiteCash account
			ScriptDevice.Debit(ScriptDevice.Balance, false);

			return true;
		}
		

			// ---- Event -------
			// OnREPostComplete
			// ---- Parameters --
			// success - >= 0 means "OK", post was successful
			// returnvalue - the page that was actually thrown by the server
			// ------------------
			// User logged out, server needed to be notified and the result of this 
			// post lands here
		function OnREPostComplete(success, returnvalue)
		{
				// Debug notification
			SiteKiosk.Logfile.Notification("Got answer from Server (RE): " + returnvalue);
			
				// Parse our returned value and don't put it into local variables
			ParseReturnValue(returnvalue, true);
			
				// To be sure that there are no corpses, create a new post object
			PostObj = SiteKiosk.Network.CreateHTTPPost();
			PostObj.OnPostComplete = OnPostComplete;
			
				// User logged out, so destroy the old user infos
			edtLoginName.value = "";
			edtLoginPassword.value = "";
			LogInfo.innerHTML = "";
			UserObj = new Object();

				// Display the login box again
			trName.style.display = "block";
			trPW.style.display = "block";
			trOK.style.display = "block";
		}
		
		
			// ---- Function ----
			// ParseReturnValue
			// ---- Parameters --
			// value - the actual return value from the server
			// is_retransfer - when this is true, the user already logged out, so don't do anything
			// ------------------
			// Here the extended info, given by the server, is parsed and put into local 
			// (JS-Global) variables
		function ParseReturnValue(value, is_retransfer)
		{
				// The delimiter for each value is "|" (for details, look at part 2 of the tutorial (Server part))
			var RetArr = value.split("|");
				
				// Is this a retransfer (log out)?
			if (is_retransfer == false)
			{
					// No, so just put everything into local variables
					
					// Error prophylaxis (answer could be some kind of screwed)
				if (RetArr[2] != null)
				{
					UserObj.RetVal = RetArr[2];
					UserObj.RetName = RetArr[3];
					UserObj.RetLastName = RetArr[4];
					UserObj.RetStartPage = RetArr[5];
				}
				else
					UserObj.RetVal = 0;
			}
			else
				UserObj.RetVal = 0;

				// Put some atributes to our user object
			UserObj.ReturnValue = value;
			UserObj.RetCode = parseInt(RetArr[0]);
			UserObj.RetMsg = RetArr[1];
				
				// Debug notification
			SiteKiosk.Logfile.Notification("ScriptDevice's UserName (ParseReturnValue): " + UserObj.name);
			
				// RetCode < 0 means we got an error (for details have a look at the Server part of the tutorial)
			if (UserObj.RetCode < 0)
				alert(UserObj.RetMsg);
			else if (UserObj.RetVal > 0 && is_retransfer == false)
			{
					// Everything went fine, so we're logged in and ready to go
				LogInfo.innerHTML = "Successfully logged in as:<br><b>" + UserObj.RetName + " " + UserObj.RetLastName + "</b>";
					// Hide the input boxes
				trName.style.display = "none";
				trPW.style.display = "none";
				trOK.style.display = "none";
					// ... and Credit our account with the money stored on the server
				CreditDebit(UserObj.RetVal, true);
					// User got a start page? So why don't navigate there...
				if (UserObj.RetStartPage != "")
					SiteKiosk.WindowList.MainWindow.SiteKioskWindow.SiteKioskWebBrowser.Navigate(UserObj.RetStartPage, true);
			}
		}
		

			// ---- Function ----
			// CreditDebit
			// ---- Parameters --
			// fValue - the string that holds the money to credit/debit
			// bCredit - Credit account?
			// ------------------
			// Just credit is used... 
		function CreditDebit(fValue, bCredit)
		{
			fValue = parseFloat(fValue);
			if (bCredit)
				ScriptDevice.Credit(fValue, false);
			else
				ScriptDevice.Debit(fValue, false);
		}
		

			// ---- Function ----
			// OnSCDPostComplete (On SiteCashDebit PostComplete)
			// ---- Parameters --
			// success - >= 0 means "OK", post was successful
			// returnvalue - the page that was actually thrown by the server
			// ------------------
			// One of our minutely posted queries got back, so decrement counter
		function OnSCDPostComplete(success, returnvalue)
		{
				// Debug notification
			SiteKiosk.Logfile.Notification("Got answer from Server (SCD): " + returnvalue);
				// Decrement openposts, since this is the PostComplete Event
			openposts--;
		}
		

			// ---- Function ----
			// OnSiteCashDebit
			// ---- Parameters --
			// payedamount - the amount the user payed for this span of time
			// newbalance - the new balance of the account
			// ------------------
			// When the "Volatile"-Flag is used with the Script Device, SiteCash wants to 
			// debit every minute (so the server needs to be updated)
		function OnSiteCashDebit(payedamount, newbalance)
		{
				// Debug notification
			SiteKiosk.Logfile.Notification("ScriptDevice Debiting (" + payedamount + ", " + newbalance + ")!");
			
				// Three retries (about three minutes)
			if (openposts < 3)
			{
					// ok...
					// Do we have a user?
				if (UserObj.name != null)
				{
						// ok...
						// Prepare post object to receive the updates from the server
					PostObj.OnPostComplete = OnSCDPostComplete;
					
					PostObj.AddParameter("login", "SiteKiosk");
					PostObj.AddParameter("action", "setmoney");
					PostObj.AddParameter("MoneyBack", newbalance);
					PostObj.AddParameter("login_name", UserObj.name);
					PostObj.AddParameter("login_password", UserObj.password);
					
						// Debug notification
					SiteKiosk.Logfile.Notification("ScriptDevice Debiting for User: " + UserObj.name);
						
						// And away...
					PostObj.Submit(ServerPage);
						// increment openposts - we just posted a new one
					++openposts;
				}
				else
				{
						// no, we don't have a user, don't do anything
					return false;
				}
			}
			else
			{
					// openposts >= 3
					// Three retries, log out - maybe server dead?
				OnREPostComplete(true, "");
				ScriptDevice.Debit(ScriptDevice.Balance, false);
					// Notify the user and the logfiles
				SiteKiosk.Logfile.Notification("ScriptDevice got a server timeout! User was logged out!");
				alert("The server timed out, please call support personell!");
				openposts = 0;
			}
		}
		
		</script>
	</head>
	
	<body onload="InitDialog()" style='background:url(../Images/hintergrund.jpg);'>
		<table style='width:100%;'>
			<tr>
				<td align=''>
					<table>
						<tr id='trName'>
							<td style='color:white;'>Name:
							<td><input type='text' name='edtLoginName'>
						<tr id='trPW'>
							<td style='color:white;'>Passwort:
							<td><input type='password' name='edtLoginPassword'>
						<tr id='trOK'>
							<td colspan='2'><input type='Button' value='OK' onClick='FormSubmit();'>
						<tr>
							<td style='color:white;' id='LogInfo' colspan='2'>
					</table>
				</td>
			</tr>
		</table>
	</body>
</html>