/*
 * Transdealware
 * JavaScript File
 *
 * (c) Cameron McKay, 2007.
 * Rights transferred to Van Toen Innovations Incorporated
 * upon payment of a prearranged fee.
 *
 */
 
function copyName(form)
{
	for (var i = 0; i < form.elements.length; i++)
	{
		var element = form.elements[i];
		element.id = element.name;
	}
}

function readOnlyAll(form)
{
	for (var i = 0; i < form.elements.length; i++)
	{
		var element = form.elements[i];
		element.readOnly = true;
	}
}
 
function initializeFields(form, fields)
{		
	for (var i = 0; i < form.elements.length; i++)
	{
		var element = form.elements[i];
		
		switch (element.type)
		{
			// Textbox.
			case 'text':
			case 'textarea':			
				element.value = fields[element.name];
				break;		
				
			// Checkbox.
			case 'checkbox':
				if (fields[element.name] == 'Yes') 
					element.checked = true;
				else
					element.checked = false;
				break;
				
			// Select.
			case 'select':
			case 'select-one':
				// Select the proper element.
				for (var j = 0; j < element.options.length; j++)
				{
					//alert(element.options[j].value + ", " + fields[element.name]);
					if (element.options[j].value == fields[element.name])					
						element.options[j].selected = true;
				} 														
				
			// Everything else.
			default:
				//alert(element.name + "(" + element.type + ")");
		}
	}	
}

function populateLists(elementIds, list)
{		
	// Add a blank element to the beginning of the list.
	list.unshift('');				

	for (var i = 0; i < list.length; i++)
	{			
		for (var j = 0; j < elementIds.length; j++)
		{	
			var newOption = document.createElement('option');
			
			// Set the text.
			if (list[i] == '')
				newOption.text = "Select one";
			else
				newOption.text = list[i];			
			
			// Set the value.
			newOption.value = list[i];
				
			var element = document.getElementById(elementIds[j]);
			try { element.add(newOption, null); }
			catch (ex) { element.add(newOption); }
		}
	}
}

function validateField(element, name, popAlert)
{	
	if (name == null) name = '';
	if (popAlert == null) popAlert = false;
	
	if (element.value.length == 0)
	{
		if (popAlert) alert(name + " is a required field.  Please fill it.");
		return false;
	}
	else
		return true;
}

function validateSelect(element, name, popAlert)
{	
	if (name == null) name = '';
	if (popAlert == null) popAlert = false;

	if (element.options[element.selectedIndex].value == '')
	{
		if (popAlert) alert(name + " is a required field.  Please select one.");
		return false;
	}
	else
		return true;
}

function validateEmail(element, required, popAlert)
{	
	if (required == null) required = true;
	if (popAlert == null) popAlert = false;

	// If it's not required and empty, then we leave it.
	if (element.value.length == 0 && !required)
		return true;

	//alert(element.value.indexOf('@'));
	//alert(element.value.indexOf('.'));

	if (element.value.indexOf('@') == -1 || element.value.indexOf('.') == -1)
	{
		if (popAlert) alert("E-mail is invalid. Format is name@host.com.");	
		return false;
	}
	else
		return true;
}

function validatePhoneNumber(element, required, popAlert)
{
	if (required == null) required = true;
	if (popAlert == null) popAlert = false;

	// If it's not required and empty, then we leave it.
	if (element.value.length == 0 && !required)
		return true;

	// Phone number pattern.
	var pattern = /^\d{10}$/;
	
	// Remove all non-numeric data.
	element.value = element.value.replace(/[^0-9]/g, '');
	
	// See if pattern matches.
	if (!element.value.match(pattern))
	{
		if (popAlert) alert("Phone number is invalid. Format is 3335550000.");
		return false;
	}
	else
		return true;
}

function validatePostalCode(element, required, popAlert)
{
	if (required == null) required = true;
	if (popAlert == null) popAlert = false;

	// If it's not required and empty, then we leave it.
	if (element.value.length == 0 && !required)
		return true;

	// Uppercase it.
	element.value = element.value.toUpperCase();

	// Postal code pattern.
	var patternPostal = /^\w\d\w \d\w\d$/;	
	var patternZip1 = /^\d{5}$/;
	var patternZip2 = /^\d{9}$/;
		
	// See if pattern matches.
	if (!element.value.match(patternPostal) 
		&& !element.value.match(patternZip1)
		&& !element.value.match(patternZip2))
	{
		if (popAlert) 
		{
			alert("Postal or ZIP code is invalid.\n"
				+ "Allowed formats are A0A 0A0,\n"
				+ "##### (5 digits), ######### (9 digits).");
		}
		return false;
	}
	else
		return true;
}

function validateDate(element, required, popAlert)
{
	if (required == null) required = true;
	if (popAlert == null) popAlert = false;
	
	// If it's not required and empty, then we leave it.
	if (element.value.length == 0 && !required)
		return true;

	// Postal code pattern.
	var pattern = /^\d{4}-\d{2}-\d{2}$/;		
		
	// See if pattern matches.
	if (!element.value.match(pattern))
	{
		if (popAlert) alert("Date is invalid.  Format is YYYY-MM-DD.");
		return false;
	}
	else
		return true;
}

function validateYear(element, required, popAlert)
{
	if (required == null) required = true;
	if (popAlert == null) popAlert = false;
	
	// If it's not required and empty, then we leave it.
	if (element.value.length == 0 && !required)
		return true;

	// Postal code pattern.
	var pattern = /^\d{4}$/;		
		
	// See if pattern matches.
	if (!element.value.match(pattern))
	{
		if (popAlert) alert("Year is invalid.  Format is YYYY.");
		return false;
	}
	else
		return true;
}