/**
 * Validate Form Fields
 */
function ValidField(id, filter)
{
	if (filter == 'empty')
	{
		if ($('#' + id).val() != '')
		{
			return HighlightForm(id, true);
		}
		else
		{
			return HighlightForm(id, false);
		}
	}
	else if (filter == 'email')
	{
		regexEmail = new RegExp('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}$', 'gi');

		if (regexEmail.test($('#' + id).val()))
		{
			return HighlightForm(id, true);
		}
		else
		{
			return HighlightForm(id, false);
		}
	}
	else if (filter == 'postal_code')
	{
		regexpPc = new RegExp('^(2[ab]|0[1-9]|[1-9][0-9])[0-9]{3}$', 'gi');

		if(regexpPc.test($('#' + id).val()))
		{
			return HighlightForm(id, true);
		}
		else
		{
			return HighlightForm(id, false);
		}
	}
	else if (filter == 'postal_code_international')
	{
		regexpPc = new RegExp('[0-9]', 'gi');

		if(regexpPc.test($('#' + id).val()))
		{
			return HighlightForm(id, true);
		}
		else
		{
			return HighlightForm(id, false);
		}
	}
	else if (filter == 'telephone')
	{
		regexpTelephone = new RegExp('^(0[1-9])[0-9]{8}$', 'gi');

		telephone = $('#' + id).val();

		for (i = 0; i < 5; i++)
		{
			telephone = telephone.replace(' ', '');
			telephone = telephone.replace('-', '');
			telephone = telephone.replace('.', '');
		}

		if(regexpTelephone.test(telephone) && telephone.length == 10)
		{
			$('#' + id).val(telephone);
			return HighlightForm(id, true);
		}
		else
		{
			return HighlightForm(id, false);
		}

	}
	else if (filter == 'cellphone')
	{
		regexpCellphone = new RegExp('^(0[6-7])[0-9]{8}$', 'gi');

		cellphone = $('#' + id).val();

		for (i = 0; i < 5; i++)
		{
			cellphone = cellphone.replace(' ', '');
			cellphone = cellphone.replace('-', '');
			cellphone = cellphone.replace('.', '');
		}

		if(regexpCellphone.test(cellphone) && cellphone.length == 10)
		{
			$('#' + id).val(cellphone);
			return HighlightForm(id, true);
		}
		else
		{
			return HighlightForm(id, false);
		}
	}
	else if (filter == 'password')
	{
		if ($('#' + id).val().length > 7)
		{
			return HighlightForm(id, true);
		}
		else
		{
			HighlightForm(id, false);
		}
	}
	else
	{
		return false;
	}
}


/**
 * Hightlight Form Fields
 */
function HighlightForm(id, valid)
{
	if (valid)
	{
		$('#' + id).removeClass('error');
		$('label[for="' + id + '"]').removeClass('error');

		$('#' + id).addClass('valid');
		$('label[for="' + id + '"]').addClass('valid');

		return true;
	}
	else
	{
		$('#' + id).removeClass('valid');
		$('label[for="' + id + '"]').removeClass('valid');

		$('#' + id).addClass('error');
		$('label[for="' + id + '"]').addClass('error');

		return false;
	}
}


/**
 * Specific Validation
 */
function Validate(id, filter)
{
	if (filter == 'telephone')
	{
		regexpTelephone = new RegExp('^(0[1-5])[0-9]{8}$', 'gi');

		telephone = $('#' + id).val();

		for (i = 0; i < 5; i++)
		{
			telephone = telephone.replace(' ', '');
			telephone = telephone.replace('-', '');
			telephone = telephone.replace('.', '');
		}

		if(regexpTelephone.test(telephone) && telephone.length == 10)
		{
			$('#' + id).val(telephone);
			return true;
		}
		else
		{
			return false;
		}

	}
	else if (filter == 'cellphone')
	{
		regexpCellphone = new RegExp('^(0[6-7])[0-9]{8}$', 'gi');

		cellphone = $('#' + id).val();

		for (i = 0; i < 5; i++)
		{
			cellphone = cellphone.replace(' ', '');
			cellphone = cellphone.replace('-', '');
			cellphone = cellphone.replace('.', '');
		}

		if(regexpCellphone.test(cellphone) && cellphone.length == 10)
		{
			$('#' + id).val(cellphone);
			return true;
		}
		else
		{
			return false;
		}
	}
}

