// This script requires xmlparser2.js and extrabasemethods script to be included

// Object heirarchy for creating reports
function Report()
{
	this.sType = "report";
	this.aSurveys = new Array();
	document.oReport = this;
}
Report.prototype.AddSurvey = Report_AddSurvey;
Report.prototype.GetSurvey = Report_GetSurvey;

function Report_AddSurvey(p_Id, p_sName)
{
	var oSurvey = new Survey(this, p_Id, p_sName);
	this.aSurveys[this.aSurveys.length] = oSurvey;
	return oSurvey;
}

function Report_GetSurvey(p_Id)
{
	var iLength = this.aSurveys.length;
	for (var i = 0; i < iLength; i++)
	{
		if (this.aSurveys[i].Id == p_Id)
			return this.aSurveys[i];
	}
	return null;
}

var BasicElement__aAllElementIds = null;
var BasicElement__iNextIndex = 0;
function BasicElement(p_oParent, p_Id)
{
	if (BasicElement__aAllElementIds == null)
		BasicElement__aAllElementIds = new Array();
	
	this.UniqueId = p_Id;
	
	if (BasicElement__aAllElementIds.contains(p_Id))
	{
		this.UniqueId = ("" + p_Id) + BasicElement__iNextIndex;
		BasicElement__iNextIndex++;
	}
	else
		BasicElement__aAllElementIds[BasicElement__aAllElementIds.length] = p_Id;

	//alert(this.UniqueId);
	this.oParent = p_oParent;
	this.Id = p_Id;
	this.sType = "basicelement";
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = BasicElement_AddXmlAttributes;
	this.BasicElement_AddXmlAttributes = BasicElement_AddXmlAttributes;
}

function BasicElement_AddXmlAttributes(p_oXmlElement)
{
	p_oXmlElement.addAttribute("id", this.Id);
	p_oXmlElement.addAttribute("type", this.sType);
}

function Survey(p_oParent, p_Id, p_sName)
{
	// Important to execute code in base class constructor
	this._Base = BasicElement;
	this._Base(p_oParent, p_Id);
	this.sName = p_sName;
	this.sType = "survey";
	this.aElements = new Array();
	document.oSurvey = this;
	
	this.AddXmlAttributes = Survey_AddXmlAttributes;
}
Survey.prototype.AddBooleanField = Survey_AddBooleanField;
Survey.prototype.AddCountryField = Survey_AddCountryField;
Survey.prototype.AddDateField = Survey_AddDateField;
Survey.prototype.AddDecimalField = Survey_AddDecimalField;
Survey.prototype.AddFieldGroup = Survey_AddFieldGroup;
Survey.prototype.AddIntegerField = Survey_AddIntegerField;
Survey.prototype.AddOptionListField = Survey_AddOptionListField;
Survey.prototype.AddTextField = Survey_AddTextField;
Survey.prototype.GetField = Survey_GetField;
Survey.prototype.GetFieldGroup = Survey_GetFieldGroup;

function Survey_AddBooleanField(p_Id, p_sShowNullAs)
{
	var oField = new BooleanField(this, p_Id, "survey", p_sShowNullAs);
	this.aElements[this.aElements.length] = oField;
	return oField;
}

function Survey_AddCountryField()
{
	var oField = new CountryField(this, "Country", "survey");
	this.aElements[this.aElements.length] = oField;
	return oField;
}

function Survey_AddDateField(p_Id, p_sShowNullAs)
{
	var oField = new DateField(this, p_Id, "survey", p_sShowNullAs);
	this.aElements[this.aElements.length] = oField;
	return oField;
}

function Survey_AddDecimalField(p_Id, p_sShowNullAs, p_iDecimalPlaces, p_sGroupMethod)
{
	var oField = new DecimalField(this, p_Id, "survey", p_sShowNullAs, p_iDecimalPlaces, p_sGroupMethod);
	this.aElements[this.aElements.length] = oField;
	return oField;
}

function Survey_AddFieldGroup(p_Id)
{
	var oFieldGroup = new FieldGroup(this, p_Id);
	this.aElements[this.aElements.length] = oFieldGroup;
	return oFieldGroup;
}

function Survey_AddIntegerField(p_Id, p_sShowNullAs)
{
	var oField = new IntegerField(this, p_Id, "survey", p_sShowNullAs);
	this.aElements[this.aElements.length] = oField;
	return oField;
}

function Survey_AddOptionListField(p_Id, p_sShowNullAs)
{
	var oField = new OptionListField(this, p_Id, "survey", p_sShowNullAs);
	this.aElements[this.aElements.length] = oField;
	return oField;
}

function Survey_AddTextField(p_Id, p_sShowNullAs)
{
	var oField = new TextField(this, p_Id, "survey", p_sShowNullAs);
	this.aElements[this.aElements.length] = oField;
	return oField;
}

function Survey_AddXmlAttributes(p_oXmlElement)
{
	p_oXmlElement.addAttribute("id", this.Id);
	p_oXmlElement.addAttribute("name", this.sName);
}

function Survey_GetField(p_Id)
{
	var iLength = this.aElements.length;
	for (var i = 0; i < iLength; i++)
	{
		if (this.aElements[i].Id == p_Id && this.aElements[i].sType != "fieldgroup")
			return this.aElements[i];
	}
	return null;
}

function Survey_GetFieldGroup(p_Id)
{
	var iLength = this.aElements.length;
	for (var i = 0; i < iLength; i++)
	{
		if (this.aElements[i].Id == p_Id && this.aElements[i].sType == "fieldgroup")
			return this.aElements[i];
	}
	return null;
}

// Only groups with fields should be included
function FieldGroup(p_oParent, p_Id)
{
	// Important to execute code in base class constructor
	this._Base = BasicElement;
	this._Base(p_oParent, p_Id);
	this.sType = "fieldgroup";
	this.aFields = new Array();
}
FieldGroup.prototype.AddBooleanField = FieldGroup_AddBooleanField;
FieldGroup.prototype.AddDateField = FieldGroup_AddDateField;
FieldGroup.prototype.AddDecimalField = FieldGroup_AddDecimalField;
FieldGroup.prototype.AddIntegerField = FieldGroup_AddIntegerField;
FieldGroup.prototype.AddOptionListField = FieldGroup_AddOptionListField;
FieldGroup.prototype.AddTextField = FieldGroup_AddTextField;
FieldGroup.prototype.GetField = FieldGroup_GetField;

function FieldGroup_AddBooleanField(p_Id, p_sShowNullAs)
{
	var oField = new BooleanField(this, p_Id, "fieldgroup", p_sShowNullAs);
	this.aFields[this.aFields.length] = oField;
	return oField;
}

function FieldGroup_AddDateField(p_Id, p_sShowNullAs)
{
	var oField = new DateField(this, p_Id, "fieldgroup", p_sShowNullAs);
	this.aFields[this.aFields.length] = oField;
	return oField;
}

function FieldGroup_AddDecimalField(p_Id, p_sShowNullAs, p_iDecimalPlaces, p_sGroupMethod)
{
	var oField = new DecimalField(this, p_Id, "fieldgroup", p_sShowNullAs, p_iDecimalPlaces, p_sGroupMethod);
	this.aFields[this.aFields.length] = oField;
	return oField;
}

function FieldGroup_AddIntegerField(p_Id, p_sShowNullAs)
{
	var oField = new IntegerField(this, p_Id, "fieldgroup", p_sShowNullAs);
	this.aFields[this.aFields.length] = oField;
	return oField;
}

function FieldGroup_AddOptionListField(p_Id, p_sShowNullAs)
{
	var oField = new OptionListField(this, p_Id, "fieldgroup", p_sShowNullAs);
	this.aFields[this.aFields.length] = oField;
	return oField;
}

function FieldGroup_AddTextField(p_Id, p_sShowNullAs)
{
	var oField = new TextField(this, p_Id, "fieldgroup", p_sShowNullAs);
	this.aFields[this.aFields.length] = oField;
	return oField;
}

function FieldGroup_GetField(p_Id)
{
	var iLength = this.aFields.length;
	for (var i = 0; i < iLength; i++)
	{
		if (this.aFields[i].Id == p_Id)
			return this.aFields[i];
	}
	return null;
}

function BasicField(p_oParent, p_Id, p_sParentType)
{
	// Important to execute code in base class constructor
	this._Base = BasicElement;
	this._Base(p_oParent, p_Id);

	this.sParentType = p_sParentType;
	this.sType = "basicfield";

	this.oAlignField = eval("document.frmReport.ddAlign" + this.UniqueId);
	this.oIsIncludedField = eval("document.frmReport.cbInclude" + this.UniqueId);
	this.oNameField = eval("document.frmReport.tbsName" + this.UniqueId);
	this.oOrderField = eval("document.frmReport.tbiDisplayOrder" + this.UniqueId);
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = BasicField_AddXmlAttributes;
	this.BasicField_AddXmlAttributes = BasicField_AddXmlAttributes;
	this.GetAlignField = BasicField_GetAlignField;
	this.GetIsIncludedField = BasicField_GetIsIncludedField;
	this.GetNameField = BasicField_GetNameField;
	this.GetOrderField = BasicField_GetOrderField;
}

function BasicField_AddXmlAttributes(p_oXmlElement)
{
	this.BasicElement_AddXmlAttributes(p_oXmlElement);
	
	p_oXmlElement.addAttribute("isincluded", this.GetIsIncludedField().checked);
	p_oXmlElement.addAttribute("name", PrepareForXml(this.GetNameField().value));
	p_oXmlElement.addAttribute("order", this.GetOrderField().value);
	p_oXmlElement.addAttribute("align", this.GetAlignField().value);
}

function BasicField_GetAlignField()
{
	return this.oAlignField;
}

function BasicField_GetIsIncludedField()
{
	return this.oIsIncludedField;
}

function BasicField_GetNameField()
{
	return this.oNameField;
}

function BasicField_GetOrderField()
{
	return this.oOrderField;
}

function Field(p_oParent, p_Id, p_sParentType, p_sShowNullAs)
{
	// Important to execute code in base class constructor
	this._Base = BasicField;
	this._Base(p_oParent, p_Id, p_sParentType);
	
	this.sShowNullAs = p_sShowNullAs;
	this.sType = "field";
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = Field_AddXmlAttributes;
	this.Field_AddXmlAttributes = Field_AddXmlAttributes;
}

function Field_AddXmlAttributes(p_oXmlElement)
{
	this.BasicField_AddXmlAttributes(p_oXmlElement);
	
	p_oXmlElement.addAttribute("shownullas", PrepareForXml(this.sShowNullAs));
}

function BooleanField(p_oParent, p_Id, p_sParentType, p_sShowNullAs)
{
	// Important to execute code in base class constructor
	this._Base = Field;
	this._Base(p_oParent, p_Id, p_sParentType, p_sShowNullAs);
	this.sType = "boolean";
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = BooleanField_AddXmlAttributes;
}

function BooleanField_AddXmlAttributes(p_oXmlElement)
{
	this.Field_AddXmlAttributes(p_oXmlElement);
}

function CountryField(p_oParent, p_sParentType)
{
	// Important to execute code in base class constructor
	this._Base = BasicField;
	this._Base(p_oParent, "Country", p_sParentType);
	
	this.sType = "country";
	this.aoOrderedValues = new Array();
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = CountryField_AddXmlAttributes;
}
CountryField.prototype.AddValue = CountryField_AddValue;
CountryField.prototype.GetValueById = CountryField_GetValueById;

function CountryField_AddValue(p_Id, p_sName, p_oValue)
{
	var oOptionListValue = new OptionListValue(p_Id, p_sName, p_oValue);
	this.aoOrderedValues[this.aoOrderedValues.length] = oOptionListValue;
	return oOptionListValue;
}

function CountryField_AddXmlAttributes(p_oXmlElement)
{
	this.BasicField_AddXmlAttributes(p_oXmlElement);
	
	var iLength = aAllCountryCheckboxes.length;
	var sList = "";
	for (var i = 0; i < iLength; i++)
	{
		with (document.frmReport)
		{
			if (eval(aAllCountryCheckboxes[i] + ".checked") == true)
				sList += eval(aAllCountryCheckboxes[i] + ".value") + ", ";
		}
	}
	if (sList.length > 0)
		sList = sList.substring(0, sList.length - 2);
	if (sList.length > 0)
	{
		sList = "(" + sList + ")";
		p_oXmlElement.addAttribute("whereis", " IN ");
		p_oXmlElement.addAttribute("wherevalue", sList);
	}
}

function CountryField_GetValueById(p_Id)
{
	for (var i = 0; i < this.aoOrderedValues.length; i++)
		if (this.aoOrderedValues[i].Id == p_Id)
			return this.aoOrderedValues[i];
	
	return null;
}

function DateField(p_oParent, p_Id, p_sParentType, p_sShowNullAs)
{
	// Important to execute code in base class constructor
	this._Base = Field;
	this._Base(p_oParent, p_Id, p_sParentType, p_sShowNullAs);
	this.sType = "date";
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = DateField_AddXmlAttributes;
}

function DateField_AddXmlAttributes(p_oXmlElement)
{
	this.Field_AddXmlAttributes(p_oXmlElement);
}

function DecimalField(p_oParent, p_Id, p_sParentType, p_sShowNullAs, p_iDecimalPlaces, p_sGroupMethod)
{
	// Important to execute code in base class constructor
	this._Base = Field;
	this._Base(p_oParent, p_Id, p_sParentType, p_sShowNullAs);
	this.sType = "decimal";
	this.iDecimalPlaces = p_iDecimalPlaces;
	this.sGroupMethod = p_sGroupMethod;
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = DecimalField_AddXmlAttributes;
}

function DecimalField_AddXmlAttributes(p_oXmlElement)
{
	this.Field_AddXmlAttributes(p_oXmlElement);
	p_oXmlElement.addAttribute("size", this.iDecimalPlaces);
	p_oXmlElement.addAttribute("groupmethod", this.sGroupMethod);
}

function IntegerField(p_oParent, p_Id, p_sParentType, p_sShowNullAs)
{
	// Important to execute code in base class constructor
	this._Base = Field;
	this._Base(p_oParent, p_Id, p_sParentType, p_sShowNullAs);
	this.sType = "integer";

	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = IntegerField_AddXmlAttributes;
}

function IntegerField_AddXmlAttributes(p_oXmlElement)
{
	this.Field_AddXmlAttributes(p_oXmlElement);
}

function OptionListField(p_oParent, p_Id, p_sParentType, p_sShowNullAs)
{
	// Important to execute code in base class constructor
	this._Base = Field;
	this._Base(p_oParent, p_Id, p_sParentType, p_sShowNullAs);
	this.sType = "optionlist";
	this.aoOrderedValues = new Array();

	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddValue = OptionListField_AddValue;
	this.AddXmlAttributes = OptionListField_AddXmlAttributes;
	this.GetValueById = OptionListField_GetValueById;
}

function OptionListField_AddValue(p_Id, p_sName, p_oValue)
{
	var oOptionListValue = new OptionListValue(p_Id, p_sName, p_oValue);
	this.aoOrderedValues[this.aoOrderedValues.length] = oOptionListValue;
	return oOptionListValue;
}

function OptionListField_AddXmlAttributes(p_oXmlElement)
{
	this.Field_AddXmlAttributes(p_oXmlElement);
}

function OptionListField_GetValueById(p_Id)
{
	for (var i = 0; i < this.aoOrderedValues.length; i++)
		if (this.aoOrderedValues[i].Id == p_Id)
			return this.aoOrderedValues[i];
	
	return null;
}

function OptionListValue(p_Id, p_sName, p_oValue)
{
	this.Id = p_Id;
	this.sName = p_sName;
	this.oValue = p_oValue;
}

function TextField(p_oParent, p_Id, p_sParentType, p_sShowNullAs)
{
	// Important to execute code in base class constructor
	this._Base = Field;
	this._Base(p_oParent, p_Id, p_sParentType, p_sShowNullAs);
	this.sType = "text";
	
	// Using this to create methods because they should not need to be overridden
	// and it leaves the prototype for a child
	this.AddXmlAttributes = TextField_AddXmlAttributes;
}

function TextField_AddXmlAttributes(p_oXmlElement)
{
	this.Field_AddXmlAttributes(p_oXmlElement);
}
