var formatErrors = null;
var templateLines = new Array(
	"this contract is covered by the government procurement agreement (gpa): no.",
	"section i: contracting authority",
	"i.1) official name and address of the contracting authority:",
	"i.2)  address from which further information can be obtained:",
	"i.3)  address from which documentation may be obtained:",
	"i.4)  address to which tenders/requests to participate must be sent:",
	"i.5)  type of contracting authority:",
	"section ii: object of the contract",
	"ii.1)  description",
	"ii.1.1)  type of works contract:",
	"ii.1.2)  type of supplies contract:",
	"ii.1.3)  type of service contract",
	"ii.1.4)  framework agreement:",
	"ii.1.5) title attributed to the contract by the contracting authority:",
	"ii.1.6) description/object of the contract:",
	"ii.1.7)  site or location of works, place of delivery or performance:",
	"ii.1.8)  nomenclature",
	"ii.1.8.1) common procurement vocabulary (cpv):",
	"ii.1.8.2)  other relevant nomenclature (cpa/nace/cpc):",
	"ii.1.9)  division into lots:",
	"ii.1.10)  will variants be accepted:",
	"ii.2)  quantity or scope of the contract",
	"ii.2.1)  total quantity or scope:",
	"ii.2.2)  options. description and time when they may be exercised:",
	"ii.3)  duration of the contract or time limit for completion:",
	"section iii: legal, economic, financial and technical information",
	"iii.1)  conditions relating to the contract",
	"iii.1.1)  deposits and guarantees required:",
	"iii.1.2)  main terms of financing and payment and/or reference to the relevant provisions:",
	"iii.1.3)  legal form to be taken by the grouping of suppliers, contractors or service providers to whom the contract is awarded:",
	"iii.2)  conditions for participation",
	"iii.2.1)  information concerning the personal situation of the contractor, supplier or service provider and information and formalities necessary for the evaluation of the minimum economic, financial and technical capacity required:",
	"iii.2.1.1)  legal position – means of proof required:",
	"iii.2.1.2)  economic and financial capacity – means of proof required:",
	"iii.2.1.3)  technical capacity – means of proof required:",
	"iii.3)  conditions specific to services contracts",
	"iii.3.1)  provision of the service is reserved to a specific profession:",
	"iii.3.2)  legal entities required to state the names and professional qualifications of the personnel responsible for execution of the contract:",
	"section iv: procedure",
	"iv.1)  type of procedure:",
	"iv.1.1)  candidates already selected:",
	"iv.1.2)  justification for the choice of accelerated procedure:",
	"iv.1.3)  previous publication concerning the same contract",
	"iv.1.3.1)  prior information notice concerning the same contract:",
	"iv.1.3.2)  other previous publications:",
	"iv.1.4)  envisaged number of suppliers which will be invited to tender:",
	"iv.2)  award criteria:",
	"iv.3)  administrative information",
	"iv.3.1)  reference number attributed to the file by the contracting authority:",
	"iv.3.2)  conditions for obtaining contract document and additional documents:",
	"iv.3.3)  time-limit for receipt of tenders or requests to participate:",
	"iv.3.4)  dispatch of invitations to tender to selected candidates:",
	"iv.3.5)  language or languages in which tenders or requests to participate can be drawn up:",
	"iv.3.6)  minimum time frame during which the tenderer must maintain its tender:",
	"iv.3.7)  conditions for opening tenders",
	"iv.3.7.1)  persons authorised to be present at the opening of tenders:",
	"iv.3.7.2)  date, time and place:",
	"section vi: other information",
	"vi.1)  non-mandatory notice:",
	"vi.2)  indication whether this procurement is a recurrent one and the estimated timing for further notices to be published:",
	"vi.3)  the contract relates to a project/programme financed by eu funds:",
	"vi.4)  additional information:",
	"vi.5)  date of dispatch of this notice:"
);
function checkField(name, legend, missingFields)
{
	if (document.all[name].value == "")
	{
		if (missingFields.length > 0)
			missingFields += ",";
		missingFields += legend;
	}
	return missingFields;
}
function initErrors()
{
	formatErrors = new Array();
}
function addFormatError(key, messageId)
{
	formatErrors[key] = messageId;
}
function showFormatErrors()
{
	var errors = "";
	for (var elementName in formatErrors)
	{
		if (errors.length > 0)
			errors += "\r\n";
		errors += getErrorMessage(formatErrors[elementName], elementName);
	}
	if (errors.length > 0)
		alert("Validation Errors:\r\n" + errors);
}
function getErrorMessage(key, friendlyName)
{
	var rule = key;
	var param = null;
	var colonIndex = key.indexOf(":");
	if (colonIndex != -1)
	{
		rule = key.substring(0, colonIndex);
		param = key.substring(colonIndex + 1);
	}
	
	var message = "";
	if (rule == "NotEmpty")
		message = "{0} must be supplied.";
	else if (rule == "MaxLength")
		message = "{0} must not exceeded maximum allowed length of {1} characters.";
	else if (rule == "NoScript")
		message = "{0} field contains invalid content. '<' symbols must not be part of a word.";
	else if (rule == "AlphaNum")
		message = "{0} must contain only alphanumeric characters.";
	else if (rule == "NoTemplateWords")
		message = "{0} value is not valid since it appears in the contract notice template.";
	else if (rule == "Email")
		message = "{0} value is not a valid email address format.";
	else if (rule == "Date")
		message = "{0} must be a valid date in the format dd/mm/yyyy (eg. 21/09/2004).";
	else if (rule == "DateAfterNow")
		message = "{0} must be later than today.";
	else if (rule == "UKPostcode")
		message = "{0} value is not a valid UK postcode format.";
	else if (rule == "Password")
		message = "{0} entries must match.";
	else if (rule == "DocumentTypes")
		message = "One or more {0} must be supplied.";
	else if (rule == "Regions")
		message = "One or more {0} must be supplied.";
	else if (rule == "CPV")
		message = "One or more {0} must be supplied.";
		
	message = message.replace(/\{0\}/, friendlyName);
	if (param != null)
		message = message.replace(/\{1\}/, param);
		
	return message;
}
function validateField(element, friendlyName, mandatory, ruleList)
{
	var valid = true;
	if (ruleList.length > 0)
	{
		var rules = ruleList.split(",");
		for (var i = 0; i < rules.length; i++)
		{
			var ruleName = rules[i];
			var ruleCmd = "";
			var colonIndex = ruleName.indexOf(":");
			if (colonIndex == -1)
				ruleCmd = "format" + ruleName + "(element)";
			else
				ruleCmd = "format" + ruleName.substring(0, colonIndex)
				+ "(element," + ruleName.substring(colonIndex + 1) + ")";
			
			if (!eval(ruleCmd))
			{
				if ((ruleName != "NotEmpty") || mandatory)
				{
					valid = false;
					addFormatError(friendlyName, ruleName);
				}
				break;
			}
		}
	}
	return valid;
}
function formatNotEmpty(element)
{
	return ((element.value != undefined) && (element.value.length > 0));
}
function formatTrim(element)
{
	element.value = element.value.replace(/^(\s*)(.*)(\b\s*$)/, '$2');
	return true;
}
function formatNoTemplateWords(element)
{
	var result = true;
	var word = element.value.toLowerCase();
	for (var i = 0; i < templateLines.length; i++)
	{
		var line = templateLines[i];
		var r = new RegExp("[^a-z]" + word + "s?[^a-z]");
		if (r.test(line))
		{
			result = false;
			break;
		}
	}
	return result;
}
function formatAlphaNum(element)
{
	var pattern = /^([\w ]+)$/;
	return (pattern.test(element.value));
}
function formatTruncate(element, max)
{
	element.value = element.value.substring(0, max);
	return true;
}
function formatMaxLength(element, max)
{
	return (element.value.length <= max);
}
function formatUpperCase(element)
{
	element.value = element.value.toUpperCase();
	return true;
}
function formatLowerCase(element)
{
	element.value = element.value.toLowerCase();
	return true;
}
function formatNoScript(element)
{
	var pattern = /<[a-zA-Z]+/;
	return (!pattern.test(element.value));
}
function formatDate(element)
{
	var result = false;
	var pattern = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/;
	if (pattern.test(element.value))
	{
		var dateParts = element.value.split("/");
		var d = dateParts[0];
		var m = dateParts[1];
		var y = dateParts[2];

		if (((m >= 1) && (m <= 12)) &&
			((d >= 1) && (d <= 31)) &&
			((d < 31) || ((m != 2) && (m != 4) && (m != 6) && (m != 11))) &&
			((m != 2) || (d < 29) || ((d == 29) && ((y % 4 == 0) && ((y % 100 != 0) ||(y % 400 == 0))))))
			return true;
	}
	return result;
}
function formatDateAfterNow(element)
{
	var dateParts = element.value.split("/");
	var d = dateParts[0];
	var m = dateParts[1];
	var y = dateParts[2];
	var valueDate = new Date(y, m - 1, d, 0, 0, 0);
	var now = new Date();
	return (valueDate > now)
}
function formatEmail(element)
{
	var pattern = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
	return (pattern.test(element.value));
}
function formatUKPostcode(element)
{
	element.value = element.value.replace(/^([A-Z])([A-Z0-9]{1,3})( *)([0-9])([A-Z]{2})$/, "$1$2 $4$5");
	var pattern = /^[A-Z][A-Z0-9]{1,3} [0-9][A-Z]{2}$/;
	return (pattern.test(element.value));
}
function formatCPV(element)
{
	element.value = "";
	for (var code in cpvArray)
	{
		if (cpvArray[code] == "selected")
		{
			if (element.value.length > 0)
				element.value += ",";
			element.value += code;
		}
	}
/*
	if (element.value.length == 0)
	{
		cpvArray = new Array();
		return false;
	}
	else
		return true;
*/
	if (element.value.length == 0)
		cpvArray = new Array();
	return true;
}	
function formatDocumentTypes(element)
{
	element.value = "";
	addCheckboxValue("DocumentTypes", "t1");
	addCheckboxValue("DocumentTypes", "t2");
	addCheckboxValue("DocumentTypes", "t3");
	addCheckboxValue("DocumentTypes", "t4");
	addCheckboxValue("DocumentTypes", "t5");
	addCheckboxValue("DocumentTypes", "t6");
	addCheckboxValue("DocumentTypes", "t7");
	addCheckboxValue("DocumentTypes", "t8");
	addCheckboxValue("DocumentTypes", "t9");
	addCheckboxValue("DocumentTypes", "t10");
	return (element.value.length > 0);
}
function formatRegions(element)
{
	element.value = "";
	addRegions();
	return (element.value.length > 0);
}
