
/* Form fixes */

var FORM_FIX_INIT = false;

function formFix()
{
	if(FORM_FIX_INIT == false)
	{
		FORM_FIX_INIT = 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;
				var bDisabled = aFields[i].disabled;

				if(bDisabled || (sClassName == undefined))
				{
					// Ignore it
				}
				else if(sClassName.match(/textfield_address/ig))
				{
					setTextfieldBackground(aFields[i].id, 'address');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'address', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'address'); setTextfieldBackground(this.id, 'address', false); };
				}
				else 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_datetime/ig))
				{
					setTextfieldBackground(aFields[i].id, 'datetime');

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'datetime', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'datetime'); setTextfieldBackground(this.id, 'datetime', 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_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_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_float1/ig))
				{
					setTextfieldBackground(aFields[i].id, 'float1');

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

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

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

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

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

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'float6', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'float6'); setTextfieldBackground(this.id, 'float6', 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_html/ig))
				{
					setTextfieldBackground(aFields[i].id, 'html');

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

					aFields[i].onfocus = function() { setTextfieldBackground(this.id, 'integer', true); };
					aFields[i].onblur = function() { setTextfieldValid(this.id, 'integer'); setTextfieldBackground(this.id, 'integer', 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); };
				}
				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); };
				}
			}
		}
	}
}


// 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.kmb-cf.nl/images/textfield_transparent.gif)';
		}
		else
		{
			oElement.style.backgroundImage = 'url(http://www.kmb-cf.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 == 'address')
			{
			}
			else if(f_type == 'color')
			{
				oExpression = new RegExp('^[#]{1,1}([0-9a-fA-F]){6,6}$');
			}
			else if(f_type == 'csv')
			{
			}
			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 == 'datetime')
			{
				oExpression = new RegExp('^[0-9]{1,2}[-]{1,1}[0-9]{1,2}[-]{1,1}[0-9]{4,4}[,]{1,1}[\\s]{0,1}[0-9]{1,2}[:]{1,1}[0-9]{1,2}[:]{1,1}[0-9]{1,2}$');
			}
			else if(f_type == 'float')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]{1,6})?$');
			}
			else if(f_type == 'float1')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]{1,1})?$');
			}
			else if(f_type == 'float2')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]{1,2})?$');
			}
			else if(f_type == 'float3')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]{1,3})?$');
			}
			else if(f_type == 'float4')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]{1,4})?$');
			}
			else if(f_type == 'float5')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]{1,5})?$');
			}
			else if(f_type == 'float6')
			{
				oExpression = new RegExp('^([-]?)[0-9]+([\\.,][0-9]{1,6})?$');
			}
			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 == 'integer')
			{
				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}(([:]){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'; // Error background color
	}
	else 
	{
		// var sBackgroundColor = oElement.getAttribute('__backgroundcolor');
		oElement.style.background = '#FFFFFF'; // Valid background color
	}
}


// Toggle between type=password and type=text
function togglePasswordField(f_name)
{
	var oElement = document.getElementById(f_name);

	// Find current type
	var sOldType = oElement.getAttribute('type').toLowerCase();

	// Find type to change to
	var sNewType = ((sOldType == 'text') ? 'password' : 'text');
	
	try
	{
		oElement.setAttribute('type', sNewType);
	}
	catch(e1) // IE
	{
		// Save value
		var sValue = oElement.value;

		var sHtml =	oElement.parentNode.innerHTML;
		sHtml =	sHtml.replace('type=' + sOldType + ' ', 'type=' + sNewType + ' ');
		oElement.parentNode.innerHTML = sHtml;

		// Add value (IE removes it sometimes)
		document.getElementById(f_name).value = sValue;
	}
}







/* Table fixes */

function tableFix()
{
	var aElements = document.getElementsByTagName('tr');

	for(var i = 0; i < aElements.length; i++)
	{
		var sClassName = aElements[i].className;

		if(sClassName == undefined)
		{
			// Ignore it
		}
		else if(sClassName.match(/odd/ig) || sClassName.match(/even/ig))
		{
			aElements[i].onmouseover = function() { tableFix_onmouseover(this); };
			aElements[i].onmouseout = function() { tableFix_onmouseout(this); };
		}
	}
}

function tableFix_onmouseover(f_object)
{
	if(f_object)
	{
		var sClassName = f_object.className;

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

		f_object.className = sClassName;
	}
}

function tableFix_onmouseout(f_object)
{
	if(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;
	}
}







// Apply custom fixes to template
function doSetup()
{
	// alert('doSetup()');	
	



	// Apply form fix
	formFix();

	// Apply table fix
	tableFix();
}

