
/* 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.ckcm.nl/images/textfield_transparent.gif)';
		}
		else
		{
			oElement.style.backgroundImage = 'url(http://www.ckcm.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;
	}
}

// Multiple select fields
function multipleselect_selectAll(f_id)
{
	var oElement = document.getElementById(f_id);

	if(oElement)
	{
		for(var i = 0; i < oElement.options.length; i++)
		{
			oElement.options[i].selected = true;
		}
	}
}

function multipleselect_selectNone(f_id)
{
	var oElement = document.getElementById(f_id);

	if(oElement)
	{
		for(var i = 0; i < oElement.options.length; i++)
		{
			oElement.options[i].selected = false;
		}
	}
}

function multipleselect_selectReverse(f_id)
{
	var oElement = document.getElementById(f_id);

	if(oElement)
	{
		for(var i = 0; i < oElement.options.length; i++)
		{
			oElement.options[i].selected = (oElement.options[i].selected == false);
		}
	}
}





/* 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;
	}
}





/* Inline Popup */
function InlinePopup(f_html, f_width, f_height, f_top, f_overflow)
{
	this.MSIE = (navigator.appVersion.indexOf('MSIE') > -1);

	this._html = '';
	this._top = 50;
	this._width = 400;
	this._height = 0;
	this._zindex = 850;
	this._overflow = 'auto';

	this._body_width = 0;
	this._body_height = 0;

	this._overlay = null;
	this._content = null;


	this.load = function(f_html, f_width, f_height, f_top, f_overflow)
	{
		this._html = f_html;
		this._width = (f_width ? f_width : 400);
		this._height = (f_height ? f_height : 0);
		this._overflow = (f_overflow ? f_overflow : 'auto');

		if(f_top != true)
		{
			if(this._height > 0)
			{
				this._top = Math.floor((this._body_height - this._height) / 2);
			}
			else
			{
				this._top = 100;
			}
		}

		// Setup
		this._content.style.top = this._top + 'px';
		this._content.style.left = Math.floor((this._body_width - this._width) / 2) + 'px';
		this._content.style.width = this._width + 'px';
		this._content.style.height = ((this._height > 0) ? this._height + 'px' : 'auto');
		this._content.style.overflow = this._overflow;
		this._content.innerHTML = this._html;

		// Display
		this._overlay.style.display = 'block';
		this._content.style.display = 'block';
	}

	this._close = function()
	{
		if(this._overlay && this._content)
		{
			this._overlay.style.display = 'none';
			this._content.style.display = 'none';
		}
	}

	this._init = function()
	{
		this._body_width = this._getBodyWidth();
		this._body_height = this._getBodyHeight();


		// Create overlay
		if(document.getElementById('inline-popup-overlay'))
		{
			this._overlay = document.getElementById('inline-popup-overlay');
		}
		else
		{
			this._overlay = document.createElement('div');
			this._overlay.id = 'inline-popup-overlay';
			this._overlay.style.position = 'fixed';
			this._overlay.style.top = '0px';
			this._overlay.style.left = '0px';
			this._overlay.style.width = this._body_width + 'px';
			this._overlay.style.height = this._body_height + 'px';
			this._overlay.style.zIndex = (this._zindex);
			this._overlay.style.overflow = 'hidden';
			this._overlay.style.background = '#000000';
			this._overlay.style.display = 'none';

			this._overlay.onclick = function() { document.getElementById('inline-popup-overlay').style.display = 'none'; document.getElementById('inline-popup-content').style.display = 'none'; }

			if(this.MSIE)
			{
				this._overlay.style.filter = 'alpha(opacity=80)';
			}
			else
			{
				this._overlay.style.opacity = 0.8;
			}

			document.body.appendChild(this._overlay);
		}


		// Create content
		if(document.getElementById('inline-popup-content'))
		{
			this._content = document.getElementById('inline-popup-content');
		}
		else
		{
			this._content = document.createElement('div');
			this._content.id = 'inline-popup-content';
			this._content.style.position = 'fixed';
			this._content.style.top = this._top + 'px';
			this._content.style.left = Math.floor((this._body_width - this._width) / 2) + 'px';
			this._content.style.width = this._width + 'px';
			this._content.style.height = ((this._height > 0) ? this._height + 'px' : 'auto');
			this._content.style.zIndex = (this._zindex + 1);
			this._content.style.overflow = this._overflow;
			this._content.style.background = '#FFFFFF';
			this._overlay.style.display = 'none';
			this._content.innerHTML = '';

			document.body.appendChild(this._content);
		}
	}

	// Get browser body width; return integer
	this._getBodyWidth = function()
	{
		var iWidth = 0;

		if(window.innerWidth)
		{
			iWidth = window.innerWidth;
		}
		else if(document.documentElement && document.documentElement.clientWidth)
		{
			iWidth = document.documentElement.clientWidth;
		}
		else if(document.body && document.body.clientWidth) 
		{
			iWidth = document.body.clientWidth;
		}

		return iWidth;
	}

	// Get browser body height; return integer
	this._getBodyHeight = function()
	{
		var iHeight = 0;

		if(window.innerHeight)
		{
			iHeight = window.innerHeight;
		}
		else if(document.documentElement && document.documentElement.clientHeight)
		{
			iHeight = document.documentElement.clientHeight;
		}
		else if(document.body && document.body.clientHeight)
		{
			iHeight = document.body.clientHeight;
		}

		return iHeight;
	}

	this._init();

	if(f_html != false)
	{
		this.load(f_html, f_width, f_height, f_top, f_overflow);
	}
}




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

	// browser
	



	// Apply form fix
	formFix();

	// Apply table fix
	tableFix();
}


var CART_DIALOG_POPUP = null;

function showCartDialog(f_cart_url, f_catalog_url, f_count)
{
	var sHtml = '<table border="0" cellpadding="0" cellspacing="20" width="100%"><tr><td align="center" colspan="2">Product toevoegen aan de winkelwagen en..</td></tr><tr><td align="left"><input class="button" type="button" value="Verder winkelen" onclick="javascript: document.location.href = \'' + f_catalog_url + '\';"></td><td align="right"><input class="button" type="button" value="Bestellen" onclick="javascript: document.location.href = \'' + f_cart_url + '\';"></td></tr></table>';
	CART_DIALOG_POPUP = new InlinePopup(sHtml, 350, 100, false, 'hidden');
}

