using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Web.Services2.Security.X509;
using System.Xml;
using System.Text;
using System.IO;

namespace WS2Client {
	/// <summary>
	/// WS2Client pro testovani webovek.
	/// Modifikovano pro konfigurovatelne pouziti s SSL a volitelnym pouzitim smerovaci adresy
	/// (target URI - adresa aplikace kde se bude WS provadet, via URI - adresa webserveru kudy to jde)
	/// Verze 2.0 by Ondrej Koller (vigh78)
    /// vig2w4-2017/10: Verze modifikace ROM + Britned
	/// </summary>
	public class FormWSTest2 : System.Windows.Forms.Form {
		//proxy konfiguracni formular
		private FormProxy proxyForm;
		
		private XmlDocument docConfig;
		private string outXml;
		private string soapXml;
        private ViewCert mSignCert;
        private IContainer components;
		private System.Windows.Forms.Panel panelTop;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.TextBox textBoxUserName;
		private System.Windows.Forms.CheckBox checkBoxFID;
		private System.Windows.Forms.ComboBox comboBoxCertLogin;
		private System.Windows.Forms.ComboBox comboBoxDataFlow;
		private System.Windows.Forms.Label labelDataFlow;
		private System.Windows.Forms.ComboBox comboBoxCertSign;
		private System.Windows.Forms.Label labelCertSign;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox comboBoxServers;
		private System.Windows.Forms.Label labelCertLogin;
		private System.Windows.Forms.Panel panelSetting;
		private System.Windows.Forms.TextBox textBoxPassword;
		private System.Windows.Forms.Label labelUserName;
		private System.Windows.Forms.Label labelPassword;
		private System.Windows.Forms.TabPage tabPageParamImput;
		private System.Windows.Forms.TabPage tabPageParamXML;
		private System.Windows.Forms.Panel panelButtons;
		private System.Windows.Forms.Button buttonRunSynchronous;
		private System.Windows.Forms.TabPage tabPageRetXml;
		private System.Windows.Forms.Button buttonRunAsynchronous;
		private System.Windows.Forms.Label labelReqId;
		private System.Windows.Forms.TextBox textBoxReqId;
		private System.Windows.Forms.Button buttonCheckRQResult;
		private System.Windows.Forms.Button buttonSaveTab;
		private System.Windows.Forms.TabPage tabPageResSoap;
		private System.Windows.Forms.TabPage tabPageSign;
		private System.Windows.Forms.TextBox textBoxSignName;
		private System.Windows.Forms.Label labelSignName;
		private System.Windows.Forms.TextBox textBoxSignSN;
		private System.Windows.Forms.Label labelSignSN;
		private System.Windows.Forms.TextBox textBoxSignSubject;
		private System.Windows.Forms.Label labelSignSubject;
		private System.Windows.Forms.TextBox textBoxSignIssuer;
		private System.Windows.Forms.Label labelSignIssuer;
		private System.Windows.Forms.ErrorProvider errorProvider;
		private System.Windows.Forms.Label labelTimeOut;
		private System.Windows.Forms.Panel panelDown;
		private System.Windows.Forms.TextBox textBoxTimeOut;
		private MyXmlEdit.XmlEdit textBoxXML;
		private MyXmlEdit.XmlEdit textBoxRetXml;
		private MyXmlEdit.XmlEdit textBoxSoapIn;
		private System.Windows.Forms.ContextMenu contextMenuStatus;
		private System.Windows.Forms.MenuItem menuItemCopyStatus;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox comboBoxPaths;
		private System.Windows.Forms.CheckBox chbUseSSL;
		private System.Windows.Forms.Button btnProxy;
        private System.Windows.Forms.Button buttonGetActualDateTime;
        private Button buttonUploadSynchronous;
		private System.Windows.Forms.StatusBar statusBar1;

		public FormWSTest2() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//prepare the proxy form for usage
			proxyForm = new FormProxy();
			proxyForm.Hide();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			docConfig=new XmlDocument();
			docConfig.Load("Config.xml");
			XmlNodeList nodeList;
			//nacteni seznamu pocitacu
			nodeList=docConfig.SelectNodes("/Configuration/Servers/Server/ServerName");
			foreach(XmlNode node in nodeList) {
				comboBoxServers.Items.Add(node.InnerText);
			}
			//nodeList=docConfig.SelectNodes("/Configuration/Application");
			//textBoxApplication.Text = nodeList[0].InnerText;
			nodeList=null;

			comboBoxServers.SelectedIndex=0;
			nodeList=docConfig.SelectNodes("/Configuration/Services/Service");
			foreach(XmlNode node in nodeList) {
				int i = comboBoxPaths.Items.Add(new ServiceObject(node.SelectSingleNode("Path").InnerText, node.SelectSingleNode("ClientClass").InnerText));
			}
			nodeList=null;
			comboBoxPaths.SelectedIndex=0;
			//nacteni seznamu datovych toku
			loadFIDs();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.contextMenuStatus = new System.Windows.Forms.ContextMenu();
            this.menuItemCopyStatus = new System.Windows.Forms.MenuItem();
            this.panelButtons = new System.Windows.Forms.Panel();
            this.buttonUploadSynchronous = new System.Windows.Forms.Button();
            this.buttonGetActualDateTime = new System.Windows.Forms.Button();
            this.btnProxy = new System.Windows.Forms.Button();
            this.textBoxTimeOut = new System.Windows.Forms.TextBox();
            this.labelTimeOut = new System.Windows.Forms.Label();
            this.buttonSaveTab = new System.Windows.Forms.Button();
            this.buttonCheckRQResult = new System.Windows.Forms.Button();
            this.buttonRunAsynchronous = new System.Windows.Forms.Button();
            this.buttonRunSynchronous = new System.Windows.Forms.Button();
            this.panelTop = new System.Windows.Forms.Panel();
            this.panelSetting = new System.Windows.Forms.Panel();
            this.chbUseSSL = new System.Windows.Forms.CheckBox();
            this.comboBoxPaths = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.textBoxReqId = new System.Windows.Forms.TextBox();
            this.labelReqId = new System.Windows.Forms.Label();
            this.labelPassword = new System.Windows.Forms.Label();
            this.labelUserName = new System.Windows.Forms.Label();
            this.textBoxPassword = new System.Windows.Forms.TextBox();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.textBoxUserName = new System.Windows.Forms.TextBox();
            this.checkBoxFID = new System.Windows.Forms.CheckBox();
            this.comboBoxCertLogin = new System.Windows.Forms.ComboBox();
            this.comboBoxDataFlow = new System.Windows.Forms.ComboBox();
            this.labelDataFlow = new System.Windows.Forms.Label();
            this.comboBoxCertSign = new System.Windows.Forms.ComboBox();
            this.labelCertSign = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBoxServers = new System.Windows.Forms.ComboBox();
            this.labelCertLogin = new System.Windows.Forms.Label();
            this.panelDown = new System.Windows.Forms.Panel();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPageParamImput = new System.Windows.Forms.TabPage();
            this.tabPageParamXML = new System.Windows.Forms.TabPage();
            this.tabPageRetXml = new System.Windows.Forms.TabPage();
            this.textBoxRetXml = new MyXmlEdit.XmlEdit();
            this.tabPageResSoap = new System.Windows.Forms.TabPage();
            this.textBoxSoapIn = new MyXmlEdit.XmlEdit();
            this.tabPageSign = new System.Windows.Forms.TabPage();
            this.label2 = new System.Windows.Forms.Label();
            this.textBoxSignIssuer = new System.Windows.Forms.TextBox();
            this.labelSignIssuer = new System.Windows.Forms.Label();
            this.textBoxSignSubject = new System.Windows.Forms.TextBox();
            this.textBoxSignSN = new System.Windows.Forms.TextBox();
            this.textBoxSignName = new System.Windows.Forms.TextBox();
            this.labelSignSubject = new System.Windows.Forms.Label();
            this.labelSignSN = new System.Windows.Forms.Label();
            this.labelSignName = new System.Windows.Forms.Label();
            this.errorProvider = new System.Windows.Forms.ErrorProvider(this.components);
            this.textBoxXML = new MyXmlEdit.XmlEdit();
            this.panelButtons.SuspendLayout();
            this.panelTop.SuspendLayout();
            this.panelSetting.SuspendLayout();
            this.panelDown.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabPageRetXml.SuspendLayout();
            this.tabPageResSoap.SuspendLayout();
            this.tabPageSign.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
            this.SuspendLayout();
            // 
            // statusBar1
            // 
            this.statusBar1.ContextMenu = this.contextMenuStatus;
            this.statusBar1.Location = new System.Drawing.Point(0, 543);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Size = new System.Drawing.Size(672, 22);
            this.statusBar1.TabIndex = 3;
            this.statusBar1.Text = "Testovaci aplikace pro WS2";
            // 
            // contextMenuStatus
            // 
            this.contextMenuStatus.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItemCopyStatus});
            this.contextMenuStatus.Popup += new System.EventHandler(this.contextMenuStatus_Popup);
            // 
            // menuItemCopyStatus
            // 
            this.menuItemCopyStatus.Index = 0;
            this.menuItemCopyStatus.Text = "&Kopírovat";
            this.menuItemCopyStatus.Click += new System.EventHandler(this.menuItemCopyStatus_Click);
            // 
            // panelButtons
            // 
            this.panelButtons.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panelButtons.Controls.Add(this.buttonUploadSynchronous);
            this.panelButtons.Controls.Add(this.buttonGetActualDateTime);
            this.panelButtons.Controls.Add(this.btnProxy);
            this.panelButtons.Controls.Add(this.textBoxTimeOut);
            this.panelButtons.Controls.Add(this.labelTimeOut);
            this.panelButtons.Controls.Add(this.buttonSaveTab);
            this.panelButtons.Controls.Add(this.buttonCheckRQResult);
            this.panelButtons.Controls.Add(this.buttonRunAsynchronous);
            this.panelButtons.Controls.Add(this.buttonRunSynchronous);
            this.panelButtons.Location = new System.Drawing.Point(480, 0);
            this.panelButtons.Name = "panelButtons";
            this.panelButtons.Size = new System.Drawing.Size(184, 289);
            this.panelButtons.TabIndex = 12;
            // 
            // buttonUploadSynchronous
            // 
            this.buttonUploadSynchronous.Enabled = false;
            this.buttonUploadSynchronous.Location = new System.Drawing.Point(16, 9);
            this.buttonUploadSynchronous.Name = "buttonUploadSynchronous";
            this.buttonUploadSynchronous.Size = new System.Drawing.Size(128, 23);
            this.buttonUploadSynchronous.TabIndex = 18;
            this.buttonUploadSynchronous.Text = "Upload";
            this.buttonUploadSynchronous.Click += new System.EventHandler(this.buttonRunSynchronous_Click);
            // 
            // buttonGetActualDateTime
            // 
            this.buttonGetActualDateTime.Location = new System.Drawing.Point(16, 137);
            this.buttonGetActualDateTime.Name = "buttonGetActualDateTime";
            this.buttonGetActualDateTime.Size = new System.Drawing.Size(128, 24);
            this.buttonGetActualDateTime.TabIndex = 14;
            this.buttonGetActualDateTime.Text = "GetActualDateTime";
            this.buttonGetActualDateTime.Click += new System.EventHandler(this.buttonGetActualDateTime_Click);
            // 
            // btnProxy
            // 
            this.btnProxy.Location = new System.Drawing.Point(16, 251);
            this.btnProxy.Name = "btnProxy";
            this.btnProxy.Size = new System.Drawing.Size(128, 24);
            this.btnProxy.TabIndex = 17;
            this.btnProxy.Text = "Proxy Config";
            this.btnProxy.Click += new System.EventHandler(this.btnProxy_Click);
            // 
            // textBoxTimeOut
            // 
            this.textBoxTimeOut.Location = new System.Drawing.Point(88, 179);
            this.textBoxTimeOut.Name = "textBoxTimeOut";
            this.textBoxTimeOut.Size = new System.Drawing.Size(56, 20);
            this.textBoxTimeOut.TabIndex = 15;
            this.textBoxTimeOut.Text = "5";
            // 
            // labelTimeOut
            // 
            this.labelTimeOut.Location = new System.Drawing.Point(16, 179);
            this.labelTimeOut.Name = "labelTimeOut";
            this.labelTimeOut.Size = new System.Drawing.Size(64, 23);
            this.labelTimeOut.TabIndex = 0;
            this.labelTimeOut.Text = "Timeout [s]:";
            // 
            // buttonSaveTab
            // 
            this.buttonSaveTab.Location = new System.Drawing.Point(16, 211);
            this.buttonSaveTab.Name = "buttonSaveTab";
            this.buttonSaveTab.Size = new System.Drawing.Size(128, 23);
            this.buttonSaveTab.TabIndex = 16;
            this.buttonSaveTab.Text = "SaveTab";
            this.buttonSaveTab.Click += new System.EventHandler(this.buttonSaveTab_Click);
            // 
            // buttonCheckRQResult
            // 
            this.buttonCheckRQResult.Location = new System.Drawing.Point(16, 105);
            this.buttonCheckRQResult.Name = "buttonCheckRQResult";
            this.buttonCheckRQResult.Size = new System.Drawing.Size(128, 23);
            this.buttonCheckRQResult.TabIndex = 13;
            this.buttonCheckRQResult.Text = "CheckRQResult";
            this.buttonCheckRQResult.Click += new System.EventHandler(this.buttonCheckRQResult_Click);
            // 
            // buttonRunAsynchronous
            // 
            this.buttonRunAsynchronous.Location = new System.Drawing.Point(16, 73);
            this.buttonRunAsynchronous.Name = "buttonRunAsynchronous";
            this.buttonRunAsynchronous.Size = new System.Drawing.Size(128, 23);
            this.buttonRunAsynchronous.TabIndex = 12;
            this.buttonRunAsynchronous.Text = "RunAsynchronous";
            this.buttonRunAsynchronous.Click += new System.EventHandler(this.buttonRunAsynchronous_Click);
            // 
            // buttonRunSynchronous
            // 
            this.buttonRunSynchronous.Location = new System.Drawing.Point(16, 41);
            this.buttonRunSynchronous.Name = "buttonRunSynchronous";
            this.buttonRunSynchronous.Size = new System.Drawing.Size(128, 23);
            this.buttonRunSynchronous.TabIndex = 11;
            this.buttonRunSynchronous.Text = "RunSynchronous";
            this.buttonRunSynchronous.Click += new System.EventHandler(this.buttonRunSynchronous_Click);
            // 
            // panelTop
            // 
            this.panelTop.Controls.Add(this.panelSetting);
            this.panelTop.Controls.Add(this.panelButtons);
            this.panelTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelTop.Location = new System.Drawing.Point(0, 0);
            this.panelTop.Name = "panelTop";
            this.panelTop.Size = new System.Drawing.Size(672, 297);
            this.panelTop.TabIndex = 15;
            // 
            // panelSetting
            // 
            this.panelSetting.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panelSetting.Controls.Add(this.chbUseSSL);
            this.panelSetting.Controls.Add(this.comboBoxPaths);
            this.panelSetting.Controls.Add(this.label3);
            this.panelSetting.Controls.Add(this.textBoxReqId);
            this.panelSetting.Controls.Add(this.labelReqId);
            this.panelSetting.Controls.Add(this.labelPassword);
            this.panelSetting.Controls.Add(this.labelUserName);
            this.panelSetting.Controls.Add(this.textBoxPassword);
            this.panelSetting.Controls.Add(this.splitter1);
            this.panelSetting.Controls.Add(this.textBoxUserName);
            this.panelSetting.Controls.Add(this.checkBoxFID);
            this.panelSetting.Controls.Add(this.comboBoxCertLogin);
            this.panelSetting.Controls.Add(this.comboBoxDataFlow);
            this.panelSetting.Controls.Add(this.labelDataFlow);
            this.panelSetting.Controls.Add(this.comboBoxCertSign);
            this.panelSetting.Controls.Add(this.labelCertSign);
            this.panelSetting.Controls.Add(this.label1);
            this.panelSetting.Controls.Add(this.comboBoxServers);
            this.panelSetting.Controls.Add(this.labelCertLogin);
            this.panelSetting.Location = new System.Drawing.Point(0, 0);
            this.panelSetting.Name = "panelSetting";
            this.panelSetting.Size = new System.Drawing.Size(472, 289);
            this.panelSetting.TabIndex = 11;
            // 
            // chbUseSSL
            // 
            this.chbUseSSL.Location = new System.Drawing.Point(384, 8);
            this.chbUseSSL.Name = "chbUseSSL";
            this.chbUseSSL.Size = new System.Drawing.Size(104, 24);
            this.chbUseSSL.TabIndex = 2;
            this.chbUseSSL.Text = "Use SSL";
            // 
            // comboBoxPaths
            // 
            this.comboBoxPaths.Location = new System.Drawing.Point(144, 32);
            this.comboBoxPaths.Name = "comboBoxPaths";
            this.comboBoxPaths.Size = new System.Drawing.Size(232, 21);
            this.comboBoxPaths.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 32);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(100, 16);
            this.label3.TabIndex = 0;
            this.label3.Text = "System:";
            // 
            // textBoxReqId
            // 
            this.textBoxReqId.Location = new System.Drawing.Point(144, 216);
            this.textBoxReqId.Name = "textBoxReqId";
            this.textBoxReqId.Size = new System.Drawing.Size(232, 20);
            this.textBoxReqId.TabIndex = 10;
            // 
            // labelReqId
            // 
            this.labelReqId.Location = new System.Drawing.Point(8, 216);
            this.labelReqId.Name = "labelReqId";
            this.labelReqId.Size = new System.Drawing.Size(100, 23);
            this.labelReqId.TabIndex = 0;
            this.labelReqId.Text = "Request Id:";
            // 
            // labelPassword
            // 
            this.labelPassword.Location = new System.Drawing.Point(8, 88);
            this.labelPassword.Name = "labelPassword";
            this.labelPassword.Size = new System.Drawing.Size(100, 23);
            this.labelPassword.TabIndex = 0;
            this.labelPassword.Text = "Password:";
            // 
            // labelUserName
            // 
            this.labelUserName.Location = new System.Drawing.Point(8, 64);
            this.labelUserName.Name = "labelUserName";
            this.labelUserName.Size = new System.Drawing.Size(100, 23);
            this.labelUserName.TabIndex = 0;
            this.labelUserName.Text = "User:";
            // 
            // textBoxPassword
            // 
            this.textBoxPassword.Location = new System.Drawing.Point(144, 88);
            this.textBoxPassword.Name = "textBoxPassword";
            this.textBoxPassword.PasswordChar = '*';
            this.textBoxPassword.Size = new System.Drawing.Size(232, 20);
            this.textBoxPassword.TabIndex = 5;
            this.textBoxPassword.Text = "unicorn";
            // 
            // splitter1
            // 
            this.splitter1.Location = new System.Drawing.Point(0, 0);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(3, 285);
            this.splitter1.TabIndex = 13;
            this.splitter1.TabStop = false;
            // 
            // textBoxUserName
            // 
            this.textBoxUserName.Location = new System.Drawing.Point(144, 64);
            this.textBoxUserName.Name = "textBoxUserName";
            this.textBoxUserName.Size = new System.Drawing.Size(232, 20);
            this.textBoxUserName.TabIndex = 4;
            this.textBoxUserName.Text = "unicorn";
            // 
            // checkBoxFID
            // 
            this.checkBoxFID.Location = new System.Drawing.Point(376, 184);
            this.checkBoxFID.Name = "checkBoxFID";
            this.checkBoxFID.Size = new System.Drawing.Size(48, 24);
            this.checkBoxFID.TabIndex = 9;
            this.checkBoxFID.Text = "FID";
            this.checkBoxFID.CheckedChanged += new System.EventHandler(this.checkBoxFID_CheckedChanged);
            // 
            // comboBoxCertLogin
            // 
            this.comboBoxCertLogin.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxCertLogin.ItemHeight = 13;
            this.comboBoxCertLogin.Location = new System.Drawing.Point(144, 120);
            this.comboBoxCertLogin.Name = "comboBoxCertLogin";
            this.comboBoxCertLogin.Size = new System.Drawing.Size(232, 21);
            this.comboBoxCertLogin.TabIndex = 6;
            this.comboBoxCertLogin.SelectedIndexChanged += new System.EventHandler(this.comboBoxCerts_SelectedIndexChanged);
            // 
            // comboBoxDataFlow
            // 
            this.comboBoxDataFlow.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxDataFlow.ItemHeight = 13;
            this.comboBoxDataFlow.Location = new System.Drawing.Point(144, 184);
            this.comboBoxDataFlow.Name = "comboBoxDataFlow";
            this.comboBoxDataFlow.Size = new System.Drawing.Size(232, 21);
            this.comboBoxDataFlow.TabIndex = 8;
            this.comboBoxDataFlow.SelectedIndexChanged += new System.EventHandler(this.comboBoxDataFlow_SelectedIndexChanged);
            // 
            // labelDataFlow
            // 
            this.labelDataFlow.Location = new System.Drawing.Point(8, 184);
            this.labelDataFlow.Name = "labelDataFlow";
            this.labelDataFlow.Size = new System.Drawing.Size(128, 23);
            this.labelDataFlow.TabIndex = 0;
            this.labelDataFlow.Text = "Dataflow:";
            // 
            // comboBoxCertSign
            // 
            this.comboBoxCertSign.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxCertSign.ItemHeight = 13;
            this.comboBoxCertSign.Location = new System.Drawing.Point(144, 152);
            this.comboBoxCertSign.Name = "comboBoxCertSign";
            this.comboBoxCertSign.Size = new System.Drawing.Size(232, 21);
            this.comboBoxCertSign.TabIndex = 7;
            this.comboBoxCertSign.SelectedIndexChanged += new System.EventHandler(this.comboBoxCerts_SelectedIndexChanged);
            // 
            // labelCertSign
            // 
            this.labelCertSign.Location = new System.Drawing.Point(8, 152);
            this.labelCertSign.Name = "labelCertSign";
            this.labelCertSign.Size = new System.Drawing.Size(128, 23);
            this.labelCertSign.TabIndex = 0;
            this.labelCertSign.Text = "Certificate for signing";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(128, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Server:";
            // 
            // comboBoxServers
            // 
            this.comboBoxServers.ItemHeight = 13;
            this.comboBoxServers.Location = new System.Drawing.Point(144, 8);
            this.comboBoxServers.Name = "comboBoxServers";
            this.comboBoxServers.Size = new System.Drawing.Size(232, 21);
            this.comboBoxServers.TabIndex = 1;
            // 
            // labelCertLogin
            // 
            this.labelCertLogin.Location = new System.Drawing.Point(8, 120);
            this.labelCertLogin.Name = "labelCertLogin";
            this.labelCertLogin.Size = new System.Drawing.Size(128, 23);
            this.labelCertLogin.TabIndex = 0;
            this.labelCertLogin.Text = "Certificate for log on:";
            // 
            // panelDown
            // 
            this.panelDown.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panelDown.Controls.Add(this.tabControl1);
            this.panelDown.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelDown.Location = new System.Drawing.Point(0, 297);
            this.panelDown.Name = "panelDown";
            this.panelDown.Size = new System.Drawing.Size(672, 246);
            this.panelDown.TabIndex = 17;
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPageParamImput);
            this.tabControl1.Controls.Add(this.tabPageParamXML);
            this.tabControl1.Controls.Add(this.tabPageRetXml);
            this.tabControl1.Controls.Add(this.tabPageResSoap);
            this.tabControl1.Controls.Add(this.tabPageSign);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(670, 244);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPageParamImput
            // 
            this.tabPageParamImput.AutoScroll = true;
            this.tabPageParamImput.Location = new System.Drawing.Point(4, 22);
            this.tabPageParamImput.Name = "tabPageParamImput";
            this.tabPageParamImput.Size = new System.Drawing.Size(662, 218);
            this.tabPageParamImput.TabIndex = 0;
            this.tabPageParamImput.Text = "Params";
            this.tabPageParamImput.UseVisualStyleBackColor = true;
            this.tabPageParamImput.Leave += new System.EventHandler(this.tabPageParamImput_Leave);
            // 
            // tabPageParamXML
            // 
            this.tabPageParamXML.Location = new System.Drawing.Point(4, 22);
            this.tabPageParamXML.Name = "tabPageParamXML";
            this.tabPageParamXML.Size = new System.Drawing.Size(662, 218);
            this.tabPageParamXML.TabIndex = 1;
            this.tabPageParamXML.Text = "XML";
            this.tabPageParamXML.UseVisualStyleBackColor = true;
            this.tabPageParamXML.Leave += new System.EventHandler(this.tabPageParamXML_Leave);
            // 
            // tabPageRetXml
            // 
            this.tabPageRetXml.Controls.Add(this.textBoxRetXml);
            this.tabPageRetXml.Location = new System.Drawing.Point(4, 22);
            this.tabPageRetXml.Name = "tabPageRetXml";
            this.tabPageRetXml.Size = new System.Drawing.Size(662, 218);
            this.tabPageRetXml.TabIndex = 2;
            this.tabPageRetXml.Text = "Ret XML";
            this.tabPageRetXml.UseVisualStyleBackColor = true;
            // 
            // textBoxRetXml
            // 
            this.textBoxRetXml.AutoFormat = false;
            this.textBoxRetXml.CommentColor = System.Drawing.Color.Gray;
            this.textBoxRetXml.DeclarationColor = System.Drawing.Color.Blue;
            this.textBoxRetXml.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBoxRetXml.ElementColor = System.Drawing.Color.Brown;
            this.textBoxRetXml.IndentLength = 4;
            this.textBoxRetXml.Location = new System.Drawing.Point(0, 0);
            this.textBoxRetXml.Name = "textBoxRetXml";
            this.textBoxRetXml.PunctuationColor = System.Drawing.Color.Blue;
            this.textBoxRetXml.ReadOnly = true;
            this.textBoxRetXml.Size = new System.Drawing.Size(662, 218);
            this.textBoxRetXml.TabIndex = 0;
            this.textBoxRetXml.Text = "";
            this.textBoxRetXml.TextNodeColor = System.Drawing.Color.Black;
            // 
            // tabPageResSoap
            // 
            this.tabPageResSoap.Controls.Add(this.textBoxSoapIn);
            this.tabPageResSoap.Location = new System.Drawing.Point(4, 22);
            this.tabPageResSoap.Name = "tabPageResSoap";
            this.tabPageResSoap.Size = new System.Drawing.Size(662, 218);
            this.tabPageResSoap.TabIndex = 4;
            this.tabPageResSoap.Text = "SOAP";
            this.tabPageResSoap.UseVisualStyleBackColor = true;
            // 
            // textBoxSoapIn
            // 
            this.textBoxSoapIn.AutoFormat = false;
            this.textBoxSoapIn.CommentColor = System.Drawing.Color.Gray;
            this.textBoxSoapIn.DeclarationColor = System.Drawing.Color.Blue;
            this.textBoxSoapIn.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBoxSoapIn.ElementColor = System.Drawing.Color.Brown;
            this.textBoxSoapIn.IndentLength = 4;
            this.textBoxSoapIn.Location = new System.Drawing.Point(0, 0);
            this.textBoxSoapIn.Name = "textBoxSoapIn";
            this.textBoxSoapIn.PunctuationColor = System.Drawing.Color.Blue;
            this.textBoxSoapIn.ReadOnly = true;
            this.textBoxSoapIn.Size = new System.Drawing.Size(662, 218);
            this.textBoxSoapIn.TabIndex = 0;
            this.textBoxSoapIn.Text = "";
            this.textBoxSoapIn.TextNodeColor = System.Drawing.Color.Black;
            // 
            // tabPageSign
            // 
            this.tabPageSign.Controls.Add(this.label2);
            this.tabPageSign.Controls.Add(this.textBoxSignIssuer);
            this.tabPageSign.Controls.Add(this.labelSignIssuer);
            this.tabPageSign.Controls.Add(this.textBoxSignSubject);
            this.tabPageSign.Controls.Add(this.textBoxSignSN);
            this.tabPageSign.Controls.Add(this.textBoxSignName);
            this.tabPageSign.Controls.Add(this.labelSignSubject);
            this.tabPageSign.Controls.Add(this.labelSignSN);
            this.tabPageSign.Controls.Add(this.labelSignName);
            this.tabPageSign.Location = new System.Drawing.Point(4, 22);
            this.tabPageSign.Name = "tabPageSign";
            this.tabPageSign.Size = new System.Drawing.Size(662, 218);
            this.tabPageSign.TabIndex = 5;
            this.tabPageSign.Text = "Sign";
            this.tabPageSign.UseVisualStyleBackColor = true;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 8);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(360, 23);
            this.label2.TabIndex = 8;
            this.label2.Text = "Certificate from signed server response";
            // 
            // textBoxSignIssuer
            // 
            this.textBoxSignIssuer.Location = new System.Drawing.Point(112, 104);
            this.textBoxSignIssuer.Name = "textBoxSignIssuer";
            this.textBoxSignIssuer.ReadOnly = true;
            this.textBoxSignIssuer.Size = new System.Drawing.Size(520, 20);
            this.textBoxSignIssuer.TabIndex = 7;
            // 
            // labelSignIssuer
            // 
            this.labelSignIssuer.Location = new System.Drawing.Point(8, 104);
            this.labelSignIssuer.Name = "labelSignIssuer";
            this.labelSignIssuer.Size = new System.Drawing.Size(100, 23);
            this.labelSignIssuer.TabIndex = 6;
            this.labelSignIssuer.Text = "Issuer:";
            // 
            // textBoxSignSubject
            // 
            this.textBoxSignSubject.Location = new System.Drawing.Point(112, 80);
            this.textBoxSignSubject.Name = "textBoxSignSubject";
            this.textBoxSignSubject.ReadOnly = true;
            this.textBoxSignSubject.Size = new System.Drawing.Size(520, 20);
            this.textBoxSignSubject.TabIndex = 5;
            // 
            // textBoxSignSN
            // 
            this.textBoxSignSN.Location = new System.Drawing.Point(112, 56);
            this.textBoxSignSN.Name = "textBoxSignSN";
            this.textBoxSignSN.ReadOnly = true;
            this.textBoxSignSN.Size = new System.Drawing.Size(520, 20);
            this.textBoxSignSN.TabIndex = 4;
            // 
            // textBoxSignName
            // 
            this.textBoxSignName.Location = new System.Drawing.Point(112, 32);
            this.textBoxSignName.Name = "textBoxSignName";
            this.textBoxSignName.ReadOnly = true;
            this.textBoxSignName.Size = new System.Drawing.Size(520, 20);
            this.textBoxSignName.TabIndex = 3;
            // 
            // labelSignSubject
            // 
            this.labelSignSubject.Location = new System.Drawing.Point(8, 80);
            this.labelSignSubject.Name = "labelSignSubject";
            this.labelSignSubject.Size = new System.Drawing.Size(100, 23);
            this.labelSignSubject.TabIndex = 2;
            this.labelSignSubject.Text = "Subject:";
            // 
            // labelSignSN
            // 
            this.labelSignSN.Location = new System.Drawing.Point(8, 56);
            this.labelSignSN.Name = "labelSignSN";
            this.labelSignSN.Size = new System.Drawing.Size(100, 23);
            this.labelSignSN.TabIndex = 1;
            this.labelSignSN.Text = "SN:";
            // 
            // labelSignName
            // 
            this.labelSignName.Location = new System.Drawing.Point(8, 32);
            this.labelSignName.Name = "labelSignName";
            this.labelSignName.Size = new System.Drawing.Size(100, 23);
            this.labelSignName.TabIndex = 0;
            this.labelSignName.Text = "Name:";
            // 
            // errorProvider
            // 
            this.errorProvider.ContainerControl = this;
            // 
            // textBoxXML
            // 
            this.textBoxXML.AutoFormat = false;
            this.textBoxXML.CommentColor = System.Drawing.Color.Gray;
            this.textBoxXML.DeclarationColor = System.Drawing.Color.Blue;
            this.textBoxXML.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBoxXML.ElementColor = System.Drawing.Color.Brown;
            this.textBoxXML.IndentLength = 4;
            this.textBoxXML.Location = new System.Drawing.Point(0, 0);
            this.textBoxXML.Name = "textBoxXML";
            this.textBoxXML.PunctuationColor = System.Drawing.Color.Blue;
            this.textBoxXML.Size = new System.Drawing.Size(654, 187);
            this.textBoxXML.TabIndex = 0;
            this.textBoxXML.Text = "";
            this.textBoxXML.TextNodeColor = System.Drawing.Color.Black;
            // 
            // FormWSTest2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(672, 565);
            this.Controls.Add(this.panelDown);
            this.Controls.Add(this.panelTop);
            this.Controls.Add(this.statusBar1);
            this.Name = "FormWSTest2";
            this.Text = "WS2 Test";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panelButtons.ResumeLayout(false);
            this.panelButtons.PerformLayout();
            this.panelTop.ResumeLayout(false);
            this.panelSetting.ResumeLayout(false);
            this.panelSetting.PerformLayout();
            this.panelDown.ResumeLayout(false);
            this.tabControl1.ResumeLayout(false);
            this.tabPageRetXml.ResumeLayout(false);
            this.tabPageResSoap.ResumeLayout(false);
            this.tabPageSign.ResumeLayout(false);
            this.tabPageSign.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() {
			Application.Run(new FormWSTest2());
		}


		private void Form1_Load(object sender, System.EventArgs e) {
			this.comboBoxCertSign.Items.Add("No certificate");
            this.comboBoxCertLogin.Items.Add("No certificate");
			//nacteni certifikatu k podepisovani pro vyber
			X509CertificateStore store;
			store = X509CertificateStore.CurrentUserStore(
				X509CertificateStore.MyStore);
			bool open = store.OpenRead();
			foreach(X509Certificate cert in store.Certificates) {
				try {
					if (cert.SupportsDigitalSignature) {
						this.comboBoxCertSign.Items.Add(new ViewCert(cert));
					}
					if (cert.SupportsDigitalSignature) {
						this.comboBoxCertLogin.Items.Add(new ViewCert(cert));
					}
				}
				catch(Exception err) {
					setStatusBarError(err);
				}
			}
			if(this.comboBoxCertSign.Items.Count>0){this.comboBoxCertSign.SelectedIndex=0;};
			if(this.comboBoxCertLogin.Items.Count>0){this.comboBoxCertLogin.SelectedIndex=0;};
		}

		private void comboBoxCerts_SelectedIndexChanged(object sender, System.EventArgs e) {
			//certifikat je vybranej, tak se popisek napise dole v panelu
			if(((ComboBox)sender).SelectedItem is ViewCert) {
				ViewCert vc=(ViewCert)(((ComboBox)sender).SelectedItem);
				setStatusBar(vc.GetCertDescription());
			}
		}

		private void loadFIDs() {
			comboBoxDataFlow.Items.Clear();
			//nacteni datovejch toku
			XmlNodeList	nodeList=docConfig.SelectNodes("/Configuration/Dataflows/Dataflow");
			foreach(XmlNode node in nodeList) {
				comboBoxDataFlow.Items.Add(new ViewFID(node,checkBoxFID.Checked));
			}
			//comboBoxDataFlow.SelectedIndex=0;

		}

		#region metody pro volani ws/zpracovani 

		private void buttonRunSynchronous_Click(object sender, System.EventArgs e) {
			setStatusBar("Running...");
			//RunSynchronous
			errorProvider.SetError(buttonRunSynchronous,null);
			try {				
				IRun erun = ((ServiceObject)comboBoxPaths.Items[comboBoxPaths.SelectedIndex]).CreateClientObject();
				prepareRun(erun);
				prepareParams(erun);
				try {
					OutParamsShared result=erun.RunSynchronous(((ViewFID)comboBoxDataFlow.SelectedItem).GetNode().SelectSingleNode("Input/FID").InnerText);	
					viewSoap(erun);
					viewResult(result);	
					viewSign(erun);
				}
				catch(System.Web.Services.Protocols.SoapException err) {
					errorProvider.SetError(buttonRunSynchronous,err.Message);
					setStatusBarError(err);
					viewSoap(erun);
					viewSign(erun);
				}
				erun.Dispose();
			}

			catch(Exception err) {
				errorProvider.SetError(buttonRunSynchronous,err.Message);
				setStatusBarError(err);
			}
		
		}

        private void buttonRunAsynchronous_Click(object sender, System.EventArgs e)
        {
            //RunSsynchronous
            errorProvider.SetError(buttonRunAsynchronous, null);
            try
            {
                IRun erun = ((ServiceObject)comboBoxPaths.Items[comboBoxPaths.SelectedIndex]).CreateClientObject();
                prepareRun(erun);
                prepareParams(erun);
                try
                {
                    OutParamsShared result = erun.RunAsynchronous(((ViewFID)comboBoxDataFlow.SelectedItem).GetNode().SelectSingleNode("Input/FID").InnerText);
                    viewSoap(erun);
                    viewResult(result);
                    viewSign(erun);
                }
                catch (System.Web.Services.Protocols.SoapException err)
                {
                    errorProvider.SetError(buttonRunAsynchronous, err.Message);
                    setStatusBarError(err);
                    viewSoap(erun);
                    viewSign(erun);
                }
                erun.Dispose();
            }
            catch (Exception err)
            {
                errorProvider.SetError(buttonRunAsynchronous, err.Message);
                setStatusBarError(err);
            }


        }

        private void buttonCheckRQResult_Click(object sender, System.EventArgs e)
        {
            //CheckRQResult
            errorProvider.SetError(buttonCheckRQResult, null);
            try
            {
                IRun erun = ((ServiceObject)comboBoxPaths.Items[comboBoxPaths.SelectedIndex]).CreateClientObject();
                prepareRun(erun);
                prepareParams(erun);
                try
                {
                    OutParamsShared result = erun.CheckRQResult(int.Parse(textBoxReqId.Text));
                    viewSoap(erun);
                    viewResult(result);
                    viewSign(erun);
                }
                catch (System.Web.Services.Protocols.SoapException err)
                {
                    errorProvider.SetError(buttonCheckRQResult, err.Message);
                    setStatusBarError(err);
                    viewSoap(erun);
                    viewSign(erun);
                }
                erun.Dispose();
            }
            catch (Exception err)
            {
                errorProvider.SetError(buttonCheckRQResult, err.Message);
                setStatusBarError(err);
            }
        }

		private void buttonGetActualDateTime_Click(object sender, System.EventArgs e) {
			errorProvider.SetError(buttonGetActualDateTime,null);
			try{
				IRun erun = ((ServiceObject)comboBoxPaths.Items[comboBoxPaths.SelectedIndex]).CreateClientObject();
                string ss = "PR: buttonGetActualDateTime_Click.init";///!!!
                log(ss);///!!!
                log(String.Concat("PR: SelectedText=", comboBoxPaths.SelectedText));///!!!
                log(String.Concat("PR: SelectedIndex=", Convert.ToString(comboBoxPaths.SelectedIndex)));///!!!
                log("prepareRun");///!!!
                prepareRun(erun);
				//prepareParams(erun);
				try {
                    log("erun.GetActualDateTime()");///!!!
                    OutParamsShared result = erun.GetActualDateTime();

                    ///!!!
                    log("PR: buttonGetActualDateTime_Click.result");///!!!
                    log(String.Concat("PR: result.RequestID=", result.RequestID.ToString()));///!!!
                    log(String.Concat("PR: result.ToString()=", result.ToString()));///!!!_path
                     viewSoap(erun);
					viewResult(result);	
					viewSign(erun);
				}
				catch(System.Web.Services.Protocols.SoapException err) {
                     log(String.Concat("PR: buttonGetActualDateTime_Click.EXC=", err.Message));///!!!
                    errorProvider.SetError(buttonGetActualDateTime, err.Message);
					setStatusBarError(err);
					viewSoap(erun);
					viewSign(erun);
				}
				erun.Dispose();
			}			
			catch(Exception err) {
				errorProvider.SetError(buttonGetActualDateTime,err.Message);
				setStatusBarError(err);
			}
		}

		private void viewSign(IRun eRun) {
			X509Certificate cert= eRun.signCertResponse();
			mSignCert=new ViewCert(cert); //xxxx
			if (cert==null) {
				textBoxSignName.Text="Response does not contain certificate or the certificate is not valid";
			}
			else{
				textBoxSignName.Text=mSignCert.ToString();
			}
			
			textBoxSignSN.Text=mSignCert.GetSerialNummber();
			textBoxSignSubject.Text=mSignCert.GetSubject();
			textBoxSignIssuer.Text= mSignCert.GetIssuer();
		}

		private void viewSoap(IRun eRun) {
			textBoxRetXml.Text="";
			textBoxSoapIn.Text="";
			outXml=null;
			soapXml=null;
			//textBoxSoapOut.Text=eRun.getSoapRequest().OuterXml;
			XmlDocument doc=eRun.getSoapResponse();
			soapXml=doc.OuterXml;
			textBoxSoapIn.Text=soapXml;
			textBoxSoapIn.FormatXML();
			tabControl1.SelectedIndex=3;//tabPageResSoap
			try {
				//XmlNode faultNode=doc.SelectSingleNode("soap:Fault");
				XmlNodeList faults = doc.GetElementsByTagName("Fault", "http://schemas.xmlsoap.org/soap/envelope/");
				XmlNode faultNode = faults[0];

				outXml=faultNode.OuterXml;
				textBoxRetXml.Text=outXml;
				textBoxRetXml.FormatXML();
				tabControl1.SelectedIndex=2;//tabPageRetXml
			}
			catch{}
		}
		private void viewResult(OutParamsShared result) {
			if(result!=null) {
				//zobrazeni reqid
				textBoxReqId.Text=result.RequestID.ToString();	
				//zobrazeni vystupniho xml
				if(result.Result!=null) {
					//result.Result.Normalize();
					outXml=result.Result.OuterXml;
					textBoxRetXml.Text=outXml;
					textBoxRetXml.FormatXML();
					tabControl1.SelectedIndex=2;//tabPageRetXml

				}
				//zobrazeni stavu procesu
				if(result.RQStateCode!=null) {
					setStatusBar("Process state: "+result.RQStateCode+" ("+result.RQStateDesc+")");
					tabControl1.SelectedIndex=2;//tabPageRetXml
				}
				else {
					setStatusBar("Request finished");
				}
			}
		}

		private void prepareRun(IRun a_run) {
			//blbbustky pro zobrazeni
			setStatusBar("Running");
			textBoxRetXml.Text="";
			textBoxSoapIn.Text="";
            
            log("PR: prepareRun.start");///!!!

			//priprava dat
			X509Certificate loginCert=null;
			X509Certificate signCert=null;
			if(comboBoxCertLogin.SelectedItem is ViewCert) {
				loginCert=((ViewCert)(comboBoxCertLogin.SelectedItem)).GetCert();
			}
			if(comboBoxCertSign.SelectedItem is ViewCert) {
				signCert=((ViewCert)(comboBoxCertSign.SelectedItem)).GetCert();
			}
			
			string URL = (chbUseSSL.Checked  ? "https://" : "http://")+ comboBoxServers.Text+"/"+comboBoxPaths.Text;
			String userName=textBoxUserName.Text;
			String password=textBoxPassword.Text;
			
			//kouknem jestli nemame nahodou target url
			string serverXpath = "/Configuration/Servers/Server[ServerName='"+comboBoxServers.Text+"']";
			XmlNode serverNode = docConfig.SelectSingleNode(serverXpath);
			string targetURL = serverNode.SelectSingleNode("TargetURL").InnerText;

            log(String.Concat("PR: targetURL=", targetURL));///!!!
            log(String.Concat("PR: serverXpath=", serverXpath));///!!!
            log(String.Concat("PR: docConfig..Value=", docConfig.SelectSingleNode(serverXpath).Value));///!!!
            log("prepareRun.Init.start");///!!!

			a_run.Init(URL,targetURL,userName,password,loginCert,signCert);
            log("PR: prepareRun.Init.end");///!!!
		}

		private void prepareParams(IRun erun) {
			//vycucnuti aktivniho nodu s parametrama
			XmlNode parentNode=((ViewFID)comboBoxDataFlow.SelectedItem).GetNode();

			XmlNodeList nodeList;
			//nacteni seznamu parametru
			nodeList=parentNode.SelectNodes("Input/Parameters/*");
			foreach(XmlNode node in nodeList) {
				//rozhazeni hodnot parametru
				ParamType mParamType=(ParamType)Enum.Parse(typeof(ParamType),node.LocalName,true);
				switch(mParamType) {
					case ParamType.BooleanParam:
						erun.AddBoolParam(node.Attributes["Name"].InnerText,bool.Parse(node.InnerText));
						break;
					case ParamType.DateParam:
						erun.AddDateParam(node.Attributes["Name"].InnerText,DateTime.ParseExact(node.InnerText,"yyyy-MM-dd",null));
						break;
					case ParamType.DateTimeParam:
						erun.AddDateTimeParam(node.Attributes["Name"].InnerText,DateTime.ParseExact(node.InnerText,@"yyyy-MM-dd\THH:mm:ss\C",null));
						break;
					case ParamType.DecimalParam:
						erun.AddDecimalParam(node.Attributes["Name"].InnerText,Decimal.Parse(node.InnerText));
						break;
					case ParamType.IntParam:
						erun.AddIntParam(node.Attributes["Name"].InnerText,int.Parse(node.InnerText));
						break;
					case ParamType.StringParam:
						erun.AddStringParam(node.Attributes["Name"].InnerText,node.InnerText);
						break;
					case ParamType.XmlParam:
						erun.AddXmlParam(node.Attributes["Name"].InnerText,node.InnerXml);
						break;
				}
			}
		}
		#endregion


		#region metody pro praci s parametry


		private void LoadParams(XmlNode nodeFID) {
			int row=0;
			tabPageParamImput.Controls.Clear();
			XmlNodeList	nodeList=nodeFID.SelectNodes("Input/Parameters/*");
			foreach(XmlNode node in nodeList) {
				tabPageParamImput.Controls.Add(new ParamPanel(row++,node));
			}
		}

		private void checkBoxFID_CheckedChanged(object sender, System.EventArgs e) {
			int poz=comboBoxDataFlow.SelectedIndex;
			loadFIDs();
			comboBoxDataFlow.SelectedIndex=poz;
		}

		private void comboBoxDataFlow_SelectedIndexChanged(object sender, System.EventArgs e) {
			XmlNode node=((ViewFID)((ComboBox)sender).SelectedItem).GetNode();
			LoadParams(node);
			//textBoxXML.Text=node.SelectSingleNode("Input").InnerXml;
			XmlNode test = node.SelectSingleNode("Input");
			textBoxXML.Text=node.SelectSingleNode("Input").OuterXml;
			//textBoxXML.FormatXML();
			buttonRunSynchronous.Enabled=true;
			buttonRunAsynchronous.Enabled=true;
			tabControl1.SelectedIndex=0;
		}

		private void tabPageParamImput_Leave(object sender, System.EventArgs e) {
			if(comboBoxDataFlow.SelectedItem is ViewFID) {
				XmlNode node=((ViewFID)comboBoxDataFlow.SelectedItem).GetNode();
				//textBoxXML.Text=node.InnerXml;
				//textBoxXML.Text=node.SelectSingleNode("Input").InnerXml;//xxxx
				textBoxXML.Text=node.SelectSingleNode("Input").OuterXml;//xxxx
				//textBoxXML.FormatXML();
			}	
		}

		private void tabPageParamXML_Leave(object sender, System.EventArgs e) {
			if(comboBoxDataFlow.SelectedItem is ViewFID) {
				//TODO validovat vkladaby xml aby aplikacka neumrela
				XmlNode node=((ViewFID)comboBoxDataFlow.SelectedItem).GetNode();
				//pokus o validaci
				try {
					XmlDocument doc=new XmlDocument();
					doc.LoadXml(textBoxXML.Text);
					//TODO validace prez xsd
					//pokud to dojde sem tak je to v poho
					node.SelectSingleNode("Input").InnerXml=doc.SelectSingleNode("Input").InnerXml;
					LoadParams(node);
				}
				catch(Exception err) {
					setStatusBarError(err);
				}
			}

		}

		#endregion

		
		private void buttonSaveTab_Click(object sender, System.EventArgs e) {
			//ukladani dat co jsou na tabu
			//OuterXml
			Stream myStream ;
			SaveFileDialog sd=new SaveFileDialog();
			switch (tabControl1.SelectedIndex) {
				case 0://tabPageParamImput
				case 1://tabPageParamXML
					if(!(comboBoxDataFlow.SelectedItem is ViewFID)){return;};
					sd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
					break;
				case 2://tabPageRetXml
					if(outXml==null){return;}
					sd.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"  ;
					break;
				case 3://tabPageResSoap
					if(soapXml==null){return;}
					sd.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"  ;
					break;
				case 4://tabPageSign
					if(mSignCert==null || mSignCert.GetCert()==null){return;}
					sd.Filter = "cer files (*.cer)|*.cer|All files (*.*)|*.*"  ;
					break;
			}
			
			sd.FilterIndex = 0 ;
			sd.RestoreDirectory = true ;
 
			if(sd.ShowDialog() == DialogResult.OK) {
				if((myStream = sd.OpenFile()) != null) {
					// Code to write the stream goes here.
					StreamWriter sw=new StreamWriter(myStream,Encoding.UTF8);
					//co se uklada
					switch (tabControl1.SelectedIndex) {
						case 0://tabPageParamImput
						case 1://tabPageParamXML
							//xml s parametrama
							XmlNode node=((ViewFID)comboBoxDataFlow.SelectedItem).GetNode().SelectSingleNode("Input");
							sw.Write(node.InnerXml);
							break;
						case 2://tabPageRetXml
							//vystupni xml
							if(outXml!=null)sw.Write(outXml);
							break;
						case 3://tabPageResSoap
							if(soapXml!=null)sw.Write(soapXml);
							break;
						case 4://tabPageSign
							sw.Write("-----BEGIN CERTIFICATE-----"+mSignCert.GetCert().ToBase64String()+"-----END CERTIFICATE-----");
							break;
					}
					sw.Flush();
					sw.Close();
					myStream.Close();
				}
			}
		}


		private void setStatusBarError(Exception err) {
			
			statusBar1.Text="["+DateTime.Now.ToLongTimeString()+ "] Error: "+err.Message+" ToString: "+err.ToString()+" Source:"+err.Source+" StackTrace:"+err.StackTrace;
			log(err.ToString());
			log(err.StackTrace);
		}
		private void setStatusBar(string text) {
			statusBar1.Text="["+DateTime.Now.ToLongTimeString()+"] "+text;
		}

		private void contextMenuStatus_Popup(object sender, System.EventArgs e) {
		
		}

		private void menuItemCopyStatus_Click(object sender, System.EventArgs e) {
			Clipboard.SetDataObject(statusBar1.Text,true);
		}

		private void log(string text) {
			TextWriter tw=new StreamWriter("log.txt",true);
			tw.WriteLine("["+DateTime.Now.ToLongDateString()+" "+DateTime.Now.ToLongTimeString() +"] "+text);
			tw.Flush();
			tw.Close();
		}

		private void btnProxy_Click(object sender, System.EventArgs e) {
			proxyForm.Show();
		}

	}

	#region obaleni nodu s konfiguraci datoveho toku+ke zobrazeni v kombu
	//obaleni nodu s konfiguraci datoveho toku+ke zobrazeni v kombu
	class ViewFID {
		private XmlNode mNode;
		private bool mViewFID=false;
		private string desc;
		
		public ViewFID(XmlNode node,bool viewFID) {
			this.mViewFID=viewFID;
			this.mNode=node;
			if(mViewFID){desc=mNode.SelectSingleNode("Input/FID").InnerText;}
			else{desc=mNode.SelectSingleNode("Name").InnerText;}
		}
		public override String ToString () {
			return desc;
		}
		public XmlNode GetNode () {
			return mNode;
		}
	}
	#endregion


	#region obaleni objektu certifikatu jen pro zobrazeni v kombu
	//obaleni objektu certifikatu jen pro zobrazeni v kombu
	class ViewCert {
		X509Certificate mCert;
		public ViewCert(X509Certificate cert) {
			mCert=cert;
		}
		public override String ToString () {
			if(mCert!=null){return mCert.SimpleDisplayName;}
			else{return "Certificate does not exist";};
		}
		public X509Certificate GetCert () {
			return mCert;
		}
		public string GetSerialNummber() {
			string serial="";
			if(mCert==null){return "";};
			// Serial number in X509Certificates has reverse order of bytes than it is
			// expected in database. Following code reverses order and formats serial
			// number to XX-XX-XX-XX etc.
			StringBuilder sn = new StringBuilder();
			char[] x509_sn = mCert.GetSerialNumberString().ToCharArray();
			if (x509_sn.Length>0) {
				int x509_pos = x509_sn.Length-1;
			
				while (x509_pos > 0) {
					sn.Append(x509_sn[x509_pos-1]).Append(x509_sn[x509_pos]).Append('-');
					x509_pos = x509_pos - 2;
				}

				sn.Remove(sn.Length-1, 1);
				
				// TODO: upper/lower case?
				serial = sn.ToString();
			}
			return serial;
		}
		public string GetIssuer() {
			if(mCert==null){return "";};
            // gets O field of certificate issuer
            //3.10.2013 - vigy21 - aktualizace na .NET 3.5
            //return getObjectField(mCert.GetIssuerName());
			return getObjectField(mCert.Issuer);
		}
		public string GetSubject() {
			if(mCert==null){return "";};
			// gets O field of certificate subject
            //3.10.2013 - vigy21 - aktualizace na .NET 3.5
            //return getObjectField(mCert.GetName());
			return getObjectField(mCert.Subject);
		}
		public String GetCertDescription () {
			string serial="";
			string issuer="";
			string subject="";
			if(mCert==null){return "";};
			serial=GetSerialNummber();
			issuer = GetIssuer();
			subject = GetSubject();
			return "Certificate SN: "+serial+" subject: \""+subject+"\" issuer: \""+issuer+"\"";
			//return mCert.FriendlyDisplayName;
		}

		/// <summary>
		/// TODO: neni spravne, docasna verze
		/// </summary>
		/// <param name="a_field"></param>
		/// <returns></returns>
		private string getObjectField(string a_field) {
			string o_field = "";
			// first we find out where the O field begins (it's marked with "O=")
			int o_pos = a_field.IndexOf("O=");
			if (o_pos > -1) {
				// O field is present, let's cut out everything before it
				o_field = a_field.Substring(o_pos+2);
				// checks out if there's another field after the O field
				int end_o_pos = o_field.IndexOf("=");
				if (end_o_pos > -1) {
					// cuts out another fields
					o_field = o_field.Substring(0, end_o_pos);
					o_field = o_field.Substring(0, o_field.LastIndexOf(","));
				}
			}
			return o_field;
		}
	}
	#endregion

	
	#region Objekt panelu pro zobrazeni parametru

	enum ParamType {BooleanParam,DateParam,DateTimeParam,DecimalParam,IntParam,StringParam,XmlParam};
	class ParamPanel:Panel {
		
		private ParamType mParamType;
		private XmlNode node;
		Label lSize;
		public ParamPanel(int row, XmlNode node) {
			this.node=node;
			//typ mParamType
			mParamType=(ParamType)Enum.Parse(typeof(ParamType),node.LocalName,true);
			//jmeno elementu
			Label lName=new Label();
			lName.Location = new System.Drawing.Point(8, 1);
			lName.Size = new System.Drawing.Size(150, 20);
			lName.TabIndex = 0;
			lName.Text = node.Attributes["Name"].InnerText+ " ["+mParamType.ToString()+"]";
			this.Controls.Add(lName);

			switch(mParamType) {
				case ParamType.BooleanParam:
					CheckBox cb=new CheckBox();
					cb.Text="";
					try {
						cb.Checked=bool.Parse(node.InnerText);
					}
					catch{}
					cb.Location = new System.Drawing.Point(8+200, 1);
					cb.Size=new System.Drawing.Size(50, 20);
					cb.TabIndex = 1;
					cb.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged);
					this.Controls.Add(cb);
					break;
				case ParamType.XmlParam:
					Button b;
					//tlacitko nahrej ze souboru
					b=new Button();
					b.Location = new System.Drawing.Point(8+200, 1);
					b.Size=new System.Drawing.Size(60, 20);
					b.Text="File";
					b.TabIndex = 1;
					b.Click += new System.EventHandler(this.button_Click);
					this.Controls.Add(b);
					//tlacitko kopiruj
					b=new Button();
					b.Location = new System.Drawing.Point(8+200+64, 1);
					b.Size=new System.Drawing.Size(60, 20);
					b.Text="Copy";
					b.TabIndex = 1;
					b.Click += new System.EventHandler(this.buttonCopy_Click);
					this.Controls.Add(b);
					//tlacitko vloz
					b=new Button();
					b.Location = new System.Drawing.Point(8+200+2*64, 1);
					b.Size=new System.Drawing.Size(60, 20);
					b.Text="Paste";
					b.TabIndex = 1;
					b.Click += new System.EventHandler(this.buttonPaste_Click);
					this.Controls.Add(b);
					//jmeno elementu
					lSize=new Label();
					lSize.Location = new System.Drawing.Point(8+200+3*64, 1);
					lSize.Size = new System.Drawing.Size(150, 20);
					lSize.TabIndex = 0;
					lSize.Text = "size: "+node.InnerXml.Length;
					this.Controls.Add(lSize);
					break;
				default:
					TextBox tb=new TextBox();
					tb.Location = new System.Drawing.Point(8+200, 1);
					tb.Size=new System.Drawing.Size(200, 20);
					tb.Text=node.InnerText;
					tb.TabIndex = 1;
					tb.TextChanged += new System.EventHandler(this.textBox_TextChanged);
					//tb.Click += new System.EventHandler(this.button_Click);
					this.Controls.Add(tb);
					break;
			}	
			this.Location = new System.Drawing.Point(8, 8+row*30);
			this.Size = new System.Drawing.Size(500, 30);
		}
		private void button_Click(object sender, System.EventArgs e) {
			/*MessageBox.Show("ahoj");
			node.Attributes["Name"].InnerText="no fuj";
			Clipboard.SetDataObject("Ahoj");*/
			Stream myStream;
			OpenFileDialog od = new OpenFileDialog();
			od.InitialDirectory = @"c:\" ;
			od.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*" ;
			od.FilterIndex = 2 ;
			od.RestoreDirectory = true ;

			if(od.ShowDialog() == DialogResult.OK) {
				if((myStream = od.OpenFile())!= null) {
					// Insert code to read the stream here.
					TextReader tr=new StreamReader(myStream);
					//TODO validovat vkladaby xml aby aplikacka neumrela
					//pokus o validaci
					XmlDocument doc=new XmlDocument();
					doc.Load(tr);
					//TODO validace prez xsd
					//pokud to dojde sem tak je to v poho
					node.InnerXml=doc.OuterXml;
					tr.Close();
					myStream.Close();
				}
			}
			
		}
		private void buttonCopy_Click(object sender, System.EventArgs e) {
			Clipboard.SetDataObject(node.InnerXml);
		}
		private void buttonPaste_Click(object sender, System.EventArgs e) {
			// Declares an IDataObject to hold the data returned from the clipboard.
			// Retrieves the data from the clipboard.
			IDataObject iData = Clipboard.GetDataObject();
			// Determines whether the data is in a format you can use.
			if(iData.GetDataPresent(DataFormats.Text)) {
				node.InnerXml = (String)iData.GetData(DataFormats.Text); 
			}
			lSize.Text = "size: "+node.InnerXml.Length;
		}
		private void checkBox_CheckedChanged(object sender, System.EventArgs e) {
			node.InnerText=((CheckBox)sender).Checked.ToString();
		}
		private void textBox_TextChanged(object sender, System.EventArgs e) {
			node.InnerText=((TextBox)sender).Text;
		}
	}
	#endregion
}
