function DocumentSchema() {
	this.Sections = new Object();
	this.DocumentInfo = new DocumentInfo(null, null);
	
	this.AddSection = function(oSection) { 
		this.Sections[ oSection.ID ] = oSection;
	}
		
	this.ToXml = function() { 
		var strXml = "<DocumentSchema>";
		
		strXml += this.DocumentInfo.ToXml();
		for( key in this.Sections ) {
			strXml += this.Sections[ key ].ToXml();
		}
					
		strXml += "</DocumentSchema>";	
		return strXml;
	}
}

function DocumentInfo(sTitle, sDescription) {
	this.Title = sTitle;
	this.Description = sDescription;
	
	this.ToText = function() { 
		return this.Title + "\n" + this.Description + "\n\n";
	}

	this.ToXml = function() { 
		return "<DocumentInfo><Title>" + XmlEncode( this.Title ) + "</Title><Description>" + XmlEncode( this.Description ) + "</Description></DocumentInfo>";
	}
}

function Section(sID) {
	this.Content = new Array();
	this.ID = sID;
	
	this.ToText = function( oSectionData ) { 
		var strText = "";
		for( var i = 0; i < this.Content.length; i ++ ) {
			strText += this.Content[ i ].ToText( oSectionData );
			if ( this.Content[ i ].ContentType == "Question" && i + 1 < this.Content.length ) { 
				strText += "\n";
			}
		
		}
		return strText + "\n";
	}
	
	this.ToHtml = function( oSectionData ) { 
		var strHtml = "";
		for( var i = 0; i < this.Content.length; i ++ ) { 
			strHtml += this.Content[ i ].ToHtml( oSectionData );
			if ( i + 1 < this.Content.length ) { 
				strHtml += "<br>";
			}
		}
		return strHtml;		
	}
	
	this.ToXml = function() { 	
		var strXml = "<Section id=\"" + XmlEncode( this.ID ) + "\">" 

		for( var i = 0; i < this.Content.length; i ++ ) {
			strXml += this.Content[ i ].ToXml();
		}

		strXml += "</Section>";
		return strXml;
	}
}

function Question() {
	this.Text = null;
	this.Help = null;
	this.ResponseGroups = new Array();
	this.ContentType = "Question";
	
	this.ToText = function( oSectionData ) { 
		var strText = this.Text + "\n";
		
		for( var i = 0; i < this.ResponseGroups.length; i ++ ) {
			strText += this.ResponseGroups[ i ].ToText( oSectionData.GetResponseGroup( this.ResponseGroups[ i ].ID ) );			
			if ( i + 1 < this.ResponseGroups.length ) {
				strText += "\n";
			}
		}

		return strText;
	}
	
	this.ToHtml = function( oSectionData ) { 
		var strHtml = "<table width='100%'><tr><td width='1'><img src='" + pathToImg + "/smallQ.jpg' style=\"width:16px;height:16px;\"></td><td valign='center' width='100%'>" + XmlEncode( this.Text ) + "</td></tr></table>";
		
		for( var i = 0; i < this.ResponseGroups.length; i ++ ) {
			strHtml += this.ResponseGroups[ i ].ToHtml( oSectionData.GetResponseGroup( this.ResponseGroups[ i ].ID ) );			
		}
		
		return strHtml;
	}
	
	this.ToXml = function() { 
		var strXml = "<Question><Text>" + XmlEncode( this.Text ) + "</Text>";
		
		if ( this.Help != null && this.Help.length > 0 ) {
			strXml += "<Help>" + XmlEncode( this.Help ) + "</Help>";
		}
		
		for( var i = 0; i < this.ResponseGroups.length; i ++ ) { 
			strXml += this.ResponseGroups[ i ].ToXml();
		}			
		
		strXml += "</Question>";
		return strXml;
	}
}

function ResponseGroup(sID) {
	this.Responses = new Array();
	this.ID = sID;
	this.Field = null;
	this.SelectMinimum = 0;
	this.SelectMaximum = -1;
	
	this.ToText = function( oInputResponseGroup ) { 
		if ( oInputResponseGroup != null ) { 
			var strText = "";
			for( var i = 0; i < this.Responses.length; i ++ ) { 
				var answer = this.Responses[ i ].ToText( oInputResponseGroup.GetResponse( this.Responses[ i ].ID ) );
				if ( strText.length > 0 && answer.length > 0 ) { 
					strText += ", ";
				}
				strText += answer;
			}
			if ( strText.length == 0 ) { 
				strText = "(No input provided)";
			}
			
			return strText + "\n";
		}
		else {
			return "";
		}
	}
	
	this.ToHtml = function( oInputResponseGroup ) { 
		if ( oInputResponseGroup != null ) { 
			var strHtml = "";
			for( var i = 0; i < this.Responses.length; i ++ ) { 
				var answer = this.Responses[ i ].ToText( oInputResponseGroup.GetResponse( this.Responses[ i ].ID ) );
				if ( this.Responses[ i ].Type == "ResponseOption" && strHtml.length > 0 && answer.length > 0 ) { 
					strHtml += ", ";
				}
				else if ( this.Responses[ i ].Type == "ResponseText" && strHtml.length > 0 && answer.length > 0 ) { 
					strHtml += "<BR>";
				}
				strHtml += XmlEncode( answer );
			}
			if ( strHtml.length == 0 ) { 
				strHtml = "(No input provided)"
			}
			return "<table width='100%'><tr><td width='1'><img src=\"" + pathToImg + "/smallA.jpg\" style=\"width:16px;height:16px\"></td><td valign='center' width='100%'>" + strHtml + "</td></tr></table>";
			
		}
		else {
			return "";
		}		
	}
		
	this.ToXml = function() { 
		var strXml = "";
		if ( this.Responses.length > 0 ) { 
			strXml = "<ResponseGroup id=\"" + XmlEncode( this.ID ) + "\"";
			if ( this.Field != null && this.Field.length > 0 ) { 
				strXml += " field=\"" + XmlEncode( this.Field ) + "\"";
			}
			if ( this.SelectMaximum != -1 ) { 
				strXml += " selectMin=\"" + this.SelectMinimum + "\" selectMax=\"" + this.SelectMaximum + "\"";
			}
			
			strXml += ">";
			
			for( var i = 0; i < this.Responses.length; i ++ ) { 
				strXml += this.Responses[ i ].ToXml();
			}
			
			strXml += "</ResponseGroup>";
		}
		return strXml;
	}
}

function ResponseOption(sID, sText) {
	this.Type = "ResponseOption";
	this.ID = sID;
	this.Text = sText;
	
	this.ToText = function( oResponseOptionData ) { 
		if ( oResponseOptionData.Selected ) { 
			return this.Text;
		}
		else { 
			return "";
		}
	}
	
	this.ToXml = function() { 
		return "<ResponseOption id=\"" + XmlEncode( this.ID ) + "\"><Text>" + XmlEncode( this.Text ) + "</Text></ResponseOption>";
	}
}

function ResponseText(sID, sText) {
	this.Type = "ResponseText";
	this.ID = sID;
	this.Text = sText;
	
	this.ToText = function( oResponseTextData ) { 
		if ( oResponseTextData.Value != null && oResponseTextData.Value.length > 0 ) {
			return this.Text + ": " + oResponseTextData.Value;
		}		
		else { 
			return "";
		}
	}
		
	this.ToXml = function() { 
		return "<ResponseText id=\"" + XmlEncode( this.ID ) + "\"><Text>" + XmlEncode( this.Text ) + "</Text></ResponseText>";
	}
}

function HyperLink(sText, sUrl, sHelp) {
	this.Text = sText;
	this.Url = sUrl;
	this.Help = sHelp;
	this.ContentType = "Hyperlink";
	
	this.ToText = function() { 
		return this.Text + " (" + this.Url + ")\n\n";
	}
	
	this.ToHtml = function() { 
		return "<table width='100%'><tr><td width='1'><img src=\"" + pathToImg + "/smallhyperlink.jpg\" style=\"width:16px;height:16px\"></td><td valign='center' width='100%'>" + XmlEncode( this.Text ) + "</td></tr></table>";
	}
	
	this.ToXml = function() { 	
		var strXml = "<HyperLink><Text>" + XmlEncode( this.Text ) + "</Text><Url>" + XmlEncode( this.Url ) + "</Url>"
		if ( this.Help != null && this.Help.length > 0 ) { 
			strXml += "<Help>" + XmlEncode( this.Help ) + "</Help>";
		}
		strXml += "</HyperLink>";
		return strXml;
	}
}

function TextBlock(sText) {
	this.Text = sText;
	this.ContentType = "TextBlock";
	
	this.ToText = function() { 
		return this.Text + "\n\n";
	}
	
	this.ToHtml = function() { 
		return "<table width='100%'><tr><td width='1'><img src=\"" + pathToImg + "/smallI.jpg\" style=\"width:16px;height:16px\"></td><td valign='center' width='100%'>" + XmlEncode( this.Text ) + "</td></tr></table>"; 
	}
	
	this.ToXml = function() { 
		return "<TextBlock><Text>" + XmlEncode( this.Text ) + "</Text></TextBlock>";
	}
}


function SectionData(sSectionID, iSequence) {
	this.SectionID = sSectionID;
	this.ResponseGroups = new Array();
	this.Sequence = iSequence;
	
	this.GetResponseGroup = function( strID ) { 
		for ( var i = 0; i < this.ResponseGroups.length; i ++ ) {
			if ( this.ResponseGroups[ i ].ResponseGroupID == strID ) { 
				return this.ResponseGroups[ i ];
			}
		}
		return null;
	}
	
	this.ToXml = function() {
		if ( this.ResponseGroups.length > 0 ) {
			var strXml = "<Section refID=\"" + XmlEncode( this.SectionID ) + "\" sequence=\"" + this.Sequence + "\">";
			
			for( var i = 0; i < this.ResponseGroups.length; i ++ ) { 
				strXml += this.ResponseGroups[ i ].ToXml();
			}
			
			strXml += "</Section>";
			return strXml;		
		}	
		else {
			return "<Section refID=\"" + XmlEncode( this.SectionID ) + "\" sequence=\"" + this.Sequence + "\" />";
		}
	}
}

function ResponseGroupData(sResponseGroupID) {
	this.ResponseGroupID = sResponseGroupID;
	this.Responses = new Array();
	
	this.GetResponse = function( strID ) { 
		for( var i = 0; i < this.Responses.length; i ++ ) {
			if( ( this.Responses[ i ].Type == "ResponseOptionData" && this.Responses[ i ].ResponseOptionID == strID ) || ( this.Responses[ i ].Type == "ResponseTextData" && this.Responses[ i ].ResponseTextID == strID ) ) {
				return this.Responses[ i ];
			}
		}
		return null;
	}
	
	this.ToXml = function() {
		var strXml = "<ResponseGroup refID=\"" + XmlEncode( this.ResponseGroupID ) + "\">";
		
		for( var i = 0; i < this.Responses.length; i ++ ) { 
			strXml += this.Responses[ i ].ToXml();
		}
		
		strXml += "</ResponseGroup>";
		return strXml;
	}
}

function ResponseOptionData(sResponseOptionID, bSelected) {
	this.Type = "ResponseOptionData";
	this.ResponseOptionID = sResponseOptionID;
	this.Selected = bSelected;
	
	this.ToXml = function() {
		return "<ResponseOption refID=\"" + XmlEncode( this.ResponseOptionID ) + "\" selected=\"" + this.Selected + "\"/>";
	}
}

function ResponseTextData(sResponseTextID, sValue) {
	this.Type = "ResponseTextData";
	this.ResponseTextID = sResponseTextID;
	this.Value = sValue;
	
	this.ToXml = function() {
		if ( this.Value.length > 0 ) { 
			return "<ResponseText refID=\"" + XmlEncode( this.ResponseTextID ) + "\"><Value>" + XmlEncode( this.Value ) + "</Value></ResponseText>";
		}
		else {
			return "<ResponseText refID=\"" + XmlEncode( this.ResponseTextID ) + "\"><Value/></ResponseText>";
		}
	}
}


function SessionManager() {
	var sectionData = new Array();
	var documentSchema = new DocumentSchema();

	this.SetDocumentSchema = function(oDocumentSchema) { 
		documentSchema = oDocumentSchema;
	}

	this.AddSectionData = function(oSectionData) {
		sectionData.push(oSectionData);	
	}

	this.GetSectionData = function( iIndex ) { 
		return sectionData[ iIndex ];
	}
	
	this.RemoveSectionData = function() {
		sectionData.pop();
	}

	this.GetSessionSummaryHtml = function() {
		var strHtml = "";
		
		for( var i = 0; i < sectionData.length; i ++ ) { 
			var data = sectionData[ i ];
			strHtml += documentSchema.Sections[ data.SectionID ].ToHtml( data );
			if ( i + 1 < sectionData.length ) {
				strHtml += "<img src=\"" + pathToImg + "/line.jpg\" style=\"width:508px;height:2px;margin-top:10px;margin-bottom:10px;\">";
			}
		}
		
		if ( objCurrentSection != null ) { 
			strHtml += "<img src=\"" + pathToImg + "/line.jpg\" style=\"width:508px;height:2px;margin-top:10px;margin-bottom:10px;\">";	
			
			var strToRun = "var data = " + objCurrentSection.id + ".GetSectionData()";
			eval( strToRun );
			strHtml += documentSchema.Sections[ data.SectionID ].ToHtml( data );
		}
		
		return strHtml;
	}

	this.GetSessionSummaryText = function() {
		var strText = documentSchema.DocumentInfo.ToText();
		
		for( var i = 0; i < sectionData.length; i ++ ) { 
			var data = sectionData[ i ];
			strText += documentSchema.Sections[ data.SectionID ].ToText( data );
		}
		
		if ( objCurrentSection != null ) { 
			var strToRun = "var data = " + objCurrentSection.id + ".GetSectionData()";
			eval( strToRun );
			strText += documentSchema.Sections[ data.SectionID ].ToText( data );
		}
		
		return strText;
	}
	
	this.GetSessionSummaryXml = function() {
		var strXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ScriptedFaqSession xmlns=\"http://www.knowledgebase.co.nz/XmlSchemas/2007/ScriptedFaqSession10\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.knowledgebase.co.nz/XmlSchemas/2007/ScriptedFaqSession10 http://www.knowledgebase.co.nz/XmlSchemas/2007/ScriptedFaqSession10.xsd\">";
		strXml += documentSchema.ToXml();
		strXml += "<Data>";
		for( var i = 0; i < sectionData.length; i ++ ) { 
			strXml += sectionData[ i ].ToXml();
		}
		
		if ( objCurrentSection != null ) { 
			var strToRun = "strXml +=" + objCurrentSection.id + ".GetSectionData().ToXml()";
			eval( strToRun );
		}
		
		strXml += "</Data></ScriptedFaqSession>";
		return strXml;		
	}

	this.SendSessionXmlHttpPost = function(sUrl, sTarget) {
		var form = document.createElement("form");
		document.body.appendChild(form);
		form.name = "form1";
		form.id = "form1";
		form.innerHTML = "<input name='xml' id='xml' type='hidden'/>";
		var input = document.getElementById('xml');
		form.method = "POST";
		form.action = sUrl;	
		form.target = sTarget;	
		input.value = this.GetSessionSummaryXml();			
		form.submit();
		document.body.removeChild(form);
	}
}

function XmlEncode(str) {
	return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;')
}

var sessionManager = new SessionManager();
var documentSchema = new DocumentSchema();
sessionManager.SetDocumentSchema( documentSchema );