//---------------------------------------------------------------------------------------------------
//
// Functions for forms
//
//---------------------------------------------------------------------------------------------------

function SubmitForm(oForm, oErrors) {
	
	//return false;
	
	oErrors.Clear();
	
	//return false;
	
	ValidateForm(oErrors, oForm);
	
	if (oErrors.Length() > 0) {
		oErrors.Show();
		return false;
	}
	else {
		oErrors.Hide();
		return true;
	}
}

function SubmitFormRegister(oForm, oErrors) {
	
	//return false;
	
	//oErrors.Clear();
	
	//return false;
	
	ValidateForm(oErrors, oForm);
	
	if (oErrors.Length() > 0) {
		oErrors.Show();
		return false;
	}
	else {
		oErrors.Hide();
		return true;
	}
}

function CheckAddress(oForm, oErrors) {

    //alert("hey");

	oErrors.Clear();
			
	ValidateAddress(oErrors, oForm);
				
	if (oErrors.Length() > 0) {
		oErrors.Show();
		//return false;
	}
	else {
	    oErrors.Hide();
		//return true;
	}
			
}


function ResetForm(oForm) {
	var	oFields = oForm.elements;
	var oField;
		
	for (i=0; i<oFields.length; i++) {
		oField = oFields[i];
		
		switch (oField.tagName) {
			case "INPUT":
				oField.value = "";
				break;
			case "SELECT":
				oField.selectedIndex = 0;
				break;
			case "TEXTAREA":
				oField.value = "";
				break;
		}
	}	
	
	return false;
}
	
function CancelForm(oForm) {
	oForm.action = "default.asp";
	oForm.submit();
}

//---------------------------------------------------------------------------------------------------
//
// Functions for displaying errors
//
//---------------------------------------------------------------------------------------------------

function FormErrors(sErrorsName) {
	//----------------------------------------------------------------------------------------------
	// Private declarations
	//----------------------------------------------------------------------------------------------
	var m_aErrors = new Array(0); 
	var m_sErrorsName = sErrorsName;
		
	//----------------------------------------------------------------------------------------------
	// Public declarations
	//----------------------------------------------------------------------------------------------
	this.Show = Show;
	this.Hide = Hide;
	this.Add = Add;
	this.Clear = Clear;
	this.Length = Length;

	//----------------------------------------------------------------------------------------------
	// Public methods
	//----------------------------------------------------------------------------------------------
	function Show() {
		var oRow, oCell;
		var i;
		
		var oTable = GetTable();
		
		if (m_aErrors.length > 0) {
			DeleteRows(oTable);
			
			InsertSpacerRow(oTable, 1);
			
			for (i=0; i<m_aErrors.length; i++) {
				oRow = oTable.insertRow(-1);
				
				oCell = oRow.insertCell(-1);
				oCell.vAlign = "top";
				oCell.width = "2%";
				oCell.className = "Error";
				
				oCell = oRow.insertCell(-1);
				oCell.vAlign = "top";
				oCell.width = "1%";
				oCell.className = "Error";
				oCell.innerHTML = "-";
				
				oCell = oRow.insertCell(-1);
				oCell.vAlign = "top";
				oCell.width = "98%";
				oCell.colSpan = 2;
				oCell.className = "Error";
				oCell.innerHTML = m_aErrors[i];
			}
			
			//InsertSpacerRow(oTable, 10);
		
			var oHeading = document.getElementById(m_sErrorsName + "_heading");
				
			oHeading.innerHTML = "Your request could not be processed, please correct the following and try again:";
		
			oTable.style.display = "inline";
		}
	}

	function Hide() {
		var oTable = GetTable();
		
		if (m_aErrors.length > 0) {
			DeleteRows(oTable);
		
			var oHeading = document.getElementById(m_sErrorsName + "_heading");
			
			oHeading.innerHTML = "Errors occurred while processing your request.";
		}
		else {		
			oTable.style.display = "none";
		}
	}

	function Add(sError) {
		m_aErrors.push(sError);
	}

	function Length() {
		return m_aErrors.length;
	}
	
	function Clear() {
		m_aErrors = new Array(0); 
	}
	
	//----------------------------------------------------------------------------------------------
	// Private methods
	//----------------------------------------------------------------------------------------------
	function GetTable() {
		var oTable = document.getElementById(m_sErrorsName + "_table");
	
		if (!oTable) {
			oTable = CreateTable();
		}
		
		return oTable;
	}
	
	function CreateTable() {
		var oError, oTable, oRow, oCell, oElement;
		
		oError = document.getElementById(m_sErrorsName + "_div");
		
		if (oError) {
			oTable = document.createElement("TABLE");
			oRow = oTable.insertRow(-1);
			oCell = oRow.insertCell(-1);
			oElement = document.createElement("SPAN");
			
			oTable.name = m_sErrorsName + "_table";
			oTable.id = m_sErrorsName + "_table";
			oTable.border = 0;
			oTable.cellPadding = 1;
			oTable.cellSpacing = 0;
			oTable.width = "100%";
			
			oCell.colSpan = 3;
			
			oElement.name = m_sErrorsName + "_heading";
			oElement.id = m_sErrorsName + "_heading";
			oElement.className = "ErrorHeading";
			oElement.innerHTML = "Your request could not be processed, please correct the following and try again:";

			oError.appendChild(oTable);
			oCell.appendChild(oElement);
		}
		
		return oTable;
	}
	
	function DeleteRows(oTable) {
		if (oTable.rows) {
			for (i=oTable.rows.length - 1; i>0; i--) {
				oTable.deleteRow(i);
			}
		}
	}
	
	function InsertSpacerRow(oTable, iHeight) {
		oRow = oTable.insertRow(-1);
		oCell = oRow.insertCell(-1);
		oCell.colSpan = 3;
		
		oElement = document.createElement("IMG");
		oElement.src = "/_common/_images/misc/spacer.gif";
		oElement.width = 1;
		oElement.height = iHeight;
		oElement.alt = "";
		
		oCell.appendChild(oElement);
	}
}


//---------------------------------------------------------------------------------------------------
//
// Functions for displaying comments
//
//---------------------------------------------------------------------------------------------------

function FormComments(sCommentsName) {
	//----------------------------------------------------------------------------------------------
	// Private declarations
	//----------------------------------------------------------------------------------------------
	var m_aComments = new Array(0); 
	var m_sCommentsName = sCommentsName;
	
	//----------------------------------------------------------------------------------------------
	// Public declarations
	//----------------------------------------------------------------------------------------------
	this.Show = Show;
	this.Hide = Hide;
	this.Add = Add;
	this.Clear = Clear;
	this.Length = Length;

	//----------------------------------------------------------------------------------------------
	// Public methods
	//----------------------------------------------------------------------------------------------
	function Show() {
		var oRow, oCell;
		var i;
		
		var oTable = GetTable();
		
		if (m_aComments.length > 0) {
			DeleteRows(oTable);
			
			InsertSpacerRow(oTable, 1);
		
			for (i=0; i<m_aComments.length; i++) {
				oRow = oTable.insertRow(-1);
				
				oCell = oRow.insertCell(-1);
				oCell.vAlign = "top";
				oCell.width = "2%";
				oCell.className = "List";
				
				oCell = oRow.insertCell(-1);
				oCell.vAlign = "top";
				oCell.width = "1%";
				oCell.className = "List";
				oCell.innerHTML = "-";
				
				oCell = oRow.insertCell(-1);
				oCell.vAlign = "top";
				oCell.width = "98%";
				oCell.colSpan = 2;
				oCell.className = "List";
				oCell.innerHTML = m_aComments[i];
			}
			
			//InsertSpacerRow(oTable, 10);
		
			var oHeading = document.getElementById(m_sCommentsName + "_heading");
				
			oHeading.innerHTML = "Please note the following before continuing:";
		
			oTable.style.display = "inline";
		}
	}

	function Hide() {
		var oTable = GetTable();
		
		if (m_aComments.length > 0) {
			DeleteRows(oTable);
		
			var oHeading = document.getElementById(m_sCommentsName + "_heading");
			
			oHeading.innerHTML = "Comments were made while processing your request.";
		}
		else {		
			oTable.style.display = "none";
		}
	}

	function Add(sComment) {
		m_aComments.push(sComment);
	}

	function Length() {
		return m_aComments.length;
	}
	
	function Clear() {
		m_aComments = new Array(0); 
	}
	
	//----------------------------------------------------------------------------------------------
	// Private methods
	//----------------------------------------------------------------------------------------------
	function GetTable() {
		var oTable = document.getElementById(m_sCommentsName + "_table");
	
		if (!oTable) {
			oTable = CreateTable();
		}
		
		return oTable;
	}
	
	function CreateTable() {
		var oComment, oTable, oRow, oCell, oElement;
		
		oComment = document.getElementById(m_sCommentsName + "_div");
		
		if (oComment) {
			oTable = document.createElement("TABLE");
			oRow = oTable.insertRow(-1);
			oCell = oRow.insertCell(-1);
			oElement = document.createElement("SPAN");
			
			oTable.name = m_sCommentsName + "_table";
			oTable.id = m_sCommentsName + "_table";
			oTable.border = 0;
			oTable.cellPadding = 1;
			oTable.cellSpacing = 0;
			oTable.width = "100%";
			
			oCell.colSpan = 3;
			
			oElement.name = m_sCommentsName + "_heading";
			oElement.id = m_sCommentsName + "_heading";
			oElement.className = "ListHeading";
			oElement.innerHTML = "Please note the following before continuing:";

			oComment.appendChild(oTable);
			oCell.appendChild(oElement);
		}
		
		return oTable;
	}
	
	function DeleteRows(oTable) {
		for (i=oTable.rows.length - 1; i>0; i--) {
			oTable.deleteRow(i);
		}
	}
	
	function InsertSpacerRow(oTable, iHeight) {
		oRow = oTable.insertRow(-1);
		oCell = oRow.insertCell(-1);
		oCell.colSpan = 3;
		
		oElement = document.createElement("IMG");
		oElement.src = "/_common/_images/misc/spacer.gif";
		oElement.width = 1;
		oElement.height = iHeight;
		oElement.alt = "";
		
		oCell.appendChild(oElement);
	}
}