// JavaScript Document
$(function(){
	var aInputs = document.getElementsByTagName('input');
	
	for (var i=0;i<aInputs.length;i++) if (aInputs[i].className.indexOf('focusSwap') !== -1) {
		var eInput = aInputs[i];
		eInput.setAttribute('orgValue', eInput.value);
		eInput.onfocus = function() {focusSwap(this, true);}
		eInput.onblur = function() {focusSwap(this, false);}
	}
});

function goBack() {history.back()}


function numbersonly(myfield, e, dec,check)
{
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
	key = e.which;
	else
	return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	(key==9) || (key==13) || (key==27) )
	return true;

	// numbers
	else if (((check).indexOf(keychar) > -1))
	return true;

	// decimal point jump
	else if (dec && (keychar == "."))
	{
		myfield.form.elements[dec].focus();
		return false;
	}
	else
	return false;
}



function focusSwap(eElement, bState) {
	if (bState) {
		// Focus
		if (eElement.className.indexOf('password') !== -1) {
			var eInput = document.createElement('input');
			eInput.type = 'password';
			eInput.name = eElement.name;
			eInput.className = eElement.className;
			eInput.setAttribute('orgValue',eElement.value);
			
			eElement.parentNode.insertBefore(eInput, eElement);
			
			eElement.parentNode.removeChild(eElement);
			eInput.focus();
			eInput.onblur = function() {focusSwap(this, false);}
			
		} else if (eElement.value == eElement.getAttribute('orgValue')) eElement.value = '';
	} else {
		// Blur
		if (eElement.value == '') {
			if (eElement.className.indexOf('password') !== -1) {
				var eInput = document.createElement('input');
				eInput.type = 'input';
				eInput.name = eElement.name;
				eInput.className = eElement.className;
				eInput.value = eElement.getAttribute('orgValue');
				
				eElement.parentNode.insertBefore(eInput, eElement);
				
				eElement.parentNode.removeChild(eElement);
				eInput.onfocus = function() {focusSwap(this, true);}
			
			} else eElement.value = eElement.getAttribute('orgValue');
		}
	}
}


function doProduct(eElement) {
	document.location.href = './'+eElement.value;
}

function goParameter(sPage, eElement) {
	if (eElement.value == '') return;
	document.location.href = '/'+sPage+eElement.value;
}

function addUploadField() {
	var eForm = document.getElementById('uploadFields');
	
	var iFieldCount = eForm.getElementsByTagName('textarea').length+1;

	
	var eField = document.createElement('div');
	eField.className = 'field';
	var eLabel = document.createElement('label');
	eLabel.appendChild(document.createTextNode('Bestand '+(iFieldCount)));
	eField.appendChild(eLabel);
	
	eInput = document.createElement('input');
	eInput.type = 'file';
	eInput.name = 'file[]';
	eField.appendChild(eInput);
	
	eText = document.createElement('textarea');
	eText.name = 'desc[]';
	eText.className = 'empty';
	eText.appendChild(document.createTextNode('Beschrijving bestand '+iFieldCount));
	eText.onfocus = function(){emptyCheck(this);}
	eField.appendChild(eText);
	
	eForm.appendChild(eField);
}

function emptyCheck(eElement) {
	if (eElement.className == 'empty') {
		eElement.className = '';
		eElement.value = '';
	}
}
