
function templateFix()
{
	// alert('templateFix()');

	// Custom fixes
	
	
	
	
	// Apply table fix
	tableFix();
	
	// Apply form fix
	formFix();
}

	
	

/*
	This function 
	- Highlights table rows on hover for "table.list"-formatted tables.
*/
function tableFix()
{
	var aElements = document.getElementsByTagName('tr');

	for(var i = 0; i < aElements.length; i++)
	{
		if((aElements[i].className == 'odd') || (aElements[i].className == 'even'))
		{
			aElements[i].onmouseover = function() { tableFix_hover(this); };
			aElements[i].onmouseout = function() { tableFix_restore(this); };
		}
	}
}

function tableFix_hover(f_object)
{
	var sClassName = f_object.className;

	if(sClassName.length > 0)
	{
		sClassName += ' hover';
	}
	else
	{
		sClassName = 'hover';
	}

	f_object.className = sClassName;
}

function tableFix_restore(f_object)
{
	var sClassName = f_object.className;

	if(sClassName.length > 5)
	{
		if(sClassName.substr(sClassName.length - 6, 6) == ' hover')
		{
			sClassName = sClassName.substr(0, sClassName.length - 6);
		}
	}
	else if(sClassName == 'hover')
	{
		sClassName = '';
	}

	f_object.className = sClassName;
}
	


/*
	This function 
	- Adds basic javascript form validation
	- Adds a background hint for form fields
*/
	
var bformFixInit = false;
var sformFixBackground = '#FFFFFF';

function formFix()
{
	if(bformFixInit == false)
	{
		bformFixInit = true;

		var aFields = document.getElementsByTagName('input');

		for(var i = 0; i < aFields.length; i++)
		{
			if(aFields[i].id && aFields[i].className)
			{
				var sClassName = aFields[i].className;

				if(sClassName.match(/textfield_color/ig))
				{
					setTextfieldBackground(aFields[i].id, 'color');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'color', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'color'); setTextfieldBackground(this.id, 'color', false); };
				}
				else if(sClassName.match(/textfield_csv/ig))
				{
					setTextfieldBackground(aFields[i].id, 'csv');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'csv', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'csv'); setTextfieldBackground(this.id, 'csv', false); };
				}
				else if(sClassName.match(/textfield_date/ig))
				{
					setTextfieldBackground(aFields[i].id, 'date');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'date', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'date'); setTextfieldBackground(this.id, 'date', false); };
				}
				else if(sClassName.match(/textfield_file/ig))
				{
					setTextfieldBackground(aFields[i].id, 'file');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'file', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'file'); setTextfieldBackground(this.id, 'file', false); };
				}
				else if(sClassName.match(/textfield_email/ig))
				{
					setTextfieldBackground(aFields[i].id, 'email');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'email', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'email'); setTextfieldBackground(this.id, 'email', false); };
				}
				else if(sClassName.match(/textfield_float/ig))
				{
					setTextfieldBackground(aFields[i].id, 'float');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'float', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'float'); setTextfieldBackground(this.id, 'float', false); };
				}
				else if(sClassName.match(/textfield_hexcode/ig))
				{
					setTextfieldBackground(aFields[i].id, 'hexcode');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'hexcode', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'hexcode'); setTextfieldBackground(this.id, 'hexcode', false); };
				}
				else if(sClassName.match(/textfield_int/ig))
				{
					setTextfieldBackground(aFields[i].id, 'int');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'int', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'int'); setTextfieldBackground(this.id, 'int', false); };
				}
				else if(sClassName.match(/textfield_ip/ig))
				{
					setTextfieldBackground(aFields[i].id, 'ip');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'ip', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'ip'); setTextfieldBackground(this.id, 'ip', false); };
				}
				else if(sClassName.match(/textfield_password/ig))
				{
					setTextfieldBackground(aFields[i].id, 'password');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'password', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'password'); setTextfieldBackground(this.id, 'password', false); };
				}
				else if(sClassName.match(/textfield_phone/ig))
				{
					setTextfieldBackground(aFields[i].id, 'phone');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'phone', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'phone'); setTextfieldBackground(this.id, 'phone', false); };
				}
				else if(sClassName.match(/textfield_postalcode/ig))
				{
					setTextfieldBackground(aFields[i].id, 'postalcode');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'postalcode', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'postalcode'); setTextfieldBackground(this.id, 'postalcode', false); };
				}
				else if(sClassName.match(/textfield_search/ig))
				{
					setTextfieldBackground(aFields[i].id, 'search');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'search', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'search'); setTextfieldBackground(this.id, 'search', false); };
				}
				else if(sClassName.match(/textfield_time/ig))
				{
					setTextfieldBackground(aFields[i].id, 'time');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'time', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'time'); setTextfieldBackground(this.id, 'time', false); };
				}
				else if(sClassName.match(/textfield_url/ig))
				{
					setTextfieldBackground(aFields[i].id, 'url');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'url', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'url'); setTextfieldBackground(this.id, 'url', false); };
				}
			}
		}
	}
}


// Add hint image to background of textfield
function setTextfieldBackground(f_id, f_type, f_focus)
{
	if(f_focus == undefined)
	{
		f_focus = false;
	}

	if(oElement = document.getElementById(f_id))
	{
		if(f_focus || oElement.value.length)
		{
			oElement.style.backgroundImage = 'url(http://www.cafephilip.nl/images/textfield_transparent.gif)';
		}
		else
		{
			oElement.style.backgroundImage = 'url(http://www.cafephilip.nl/images/textfield_' + f_type + '.gif)';
			oElement.style.backgroundRepeat = 'no-repeat';
			oElement.style.backgroundPosition = 'center left';
		}
	}
}


// Basic syntax validation
function setTextfieldValid(f_id, f_type)
{
	var oExpression = false;
	var bValid = true;
	var sValue = '';

	if(oElement = document.getElementById(f_id))
	{
		if(oElement.value)
		{
			sValue = oElement.value;

			if(f_type == 'color')
			{
				oExpression = new RegExp('^[#]{1,1}([0-9a-fA-F]){6,6}$');
			}
			else if(f_type == 'csv')
			{
				// oExpression = new RegExp('^[#]{1,1}([0-9a-fA-F]){6,6}$');
			}
			else if(f_type == 'date')
			{
				oExpression = new RegExp('^[0-9]{1,2}[-]{1,1}[0-9]{1,2}[-]{1,1}[0-9]{4,4}$');
			}
			else if(f_type == 'float')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]+)?$');
			}
			else if(f_type == 'file')
			{
				oExpression = new RegExp('^(http://.+)|(https://.+)|(ftp://.+)', 'i');
			}
			else if(f_type == 'email')
			{
				oExpression = new RegExp('^([a-z0-9\\-_\\.]+)@([a-z0-9\\-_\\.]+)[\\.][a-z]{2,6}$', 'i');
			}
			else if(f_type == 'hexcode')
			{
				oExpression = new RegExp('^([0-9a-fA-F]+)$');
			}
			else if(f_type == 'int')
			{
				oExpression = new RegExp('^([-]?)[0-9]+$');
			}
			else if(f_type == 'ip')
			{
				oExpression = new RegExp('^([0-9]){1,3}([\\.]){1,1}([0-9]){1,3}([\\.]){1,1}([0-9]){1,3}([\\.]){1,1}([0-9]){1,3}$');
			}
			else if(f_type == 'phone')
			{
				oExpression = new RegExp('^[\\s0-9()+\\-]{8,}$');
			}
			else if(f_type == 'postalcode')
			{
				// Check for postalcode in NL or BE
				oExpression = new RegExp('^([0-9]{4,4}\\s{0,1}[a-zA-Z]{2,2})|(([Bb][-])?[0-9]{4,4})$');
			}
			else if(f_type == 'search')
			{
				oExpression = new RegExp('^([\\sa-zA-Z0-9\\.\\-]+)$');
			}
			else if(f_type == 'time')
			{
				oExpression = new RegExp('^([0-9]){1,2}([:]){1,1}([0-9]){2,2}$');
			}
			else if(f_type == 'url')
			{
				oExpression = new RegExp('^(http://.+)|(https://.+)|(ftp://.+)', 'i');
			}

			if(oExpression)
			{
				bValid = oExpression.test(sValue.toString());
			}
		}
	}

	if(bValid == false)
	{
		// oElement.setAttribute('__backgroundcolor', oElement.style.background);
		oElement.style.background = '#FFE0E0';
	}
	else 
	{
		// var sBackgroundColor = oElement.getAttribute('__backgroundcolor');
		oElement.style.background = '#FFFFFF';
	}
}

