function dtext(element, values, constraint, action)
{
	this.element = element;
	element.dtext = this;
	this.values = values;
	this.constraint = constraint;
	this.action = action;
	this.div = null;
	if(constraint)
	{
		this.saved = element.value;
		element.onkeydown = dtext.keydown;
	}
	if(values)
	{
		element.onmousedown = dtext.popvalues;
	}
	element.onkeyup = dtext.keyup;
}

dtext.keydown = function()
{
	this.dtext.keydown();
}

dtext.keyup = function()
{
	this.dtext.keyup();
}

dtext.popvalues = function()
{
	this.dtext.popvalues();
}

dtext.prototype.keyup = function()
{
	if(!this.constraint(this.element.value))
	{
		this.element.value = this.saved;
	}
	this.pophide();
	if(this.action)
	{
		this.action(this.element);
	}
}

dtext.prototype.keydown = function()
{
	if(this.constraint(this.element.value))
	{
		this.saved = this.element.value;
	}
}

dtext.prototype.popvalues = function()
{
	var x = 0;
	var y = 0;
	var p = this.element;
	y += p.offsetHeight;
	while(p)
	{
		x += p.offsetLeft;
		y += p.offsetTop;
		p = p.offsetParent;
	}
	var div = document.createElement("div");
	div.style.backgroundColor = 'white';
	div.style.position = "absolute";
	div.style.borderTopWidth = "1px";
	div.style.borderRightWidth = "2px";
	div.style.borderBottomWidth = "2px";
	div.style.border.LeftWidth = "1px";
	div.style.borderStyle = "solid";
	div.style.borderTopColor = "#eeeeee";
	div.style.borderRightColor = "#dddddd";
	div.style.borderBottomColor = "#dddddd";
	div.style.borderLeftColor = "#eeeeee";
	div.style.top = y + "px";
	div.style.left = x + "px";
	div.style.fontFamily = "Arial,Helvetica,Sans-Serif";
	div.style.fontSize = "14px";
	var scroll = 0;
	if(this.values.length > 15)
	{
		div.style.height = "340px";
		div.style.overflow = "scroll";
		scroll = 1;
	}
	var div2 = document.createElement("div");
	div.appendChild(div2);
	document.body.appendChild(div);
	for(var i = 0; i < this.values.length; i++)
	{
		var item = document.createElement("div");
		item.style.paddingTop = "1px";
		item.style.paddingRight = "4px";
		item.style.paddingBottom = "1px";
		item.style.paddingLeft = "4px";
		item.innerHTML = this.values[i];
		item.onmouseover = dtext.itemover;
		item.onmouseout = dtext.itemout;
		item.onmouseup = dtext.itemup;
		item.dtext = this;
		div2.appendChild(item);
	}
	this.div = div;
	if(scroll)
	{
		div.style.width = (div2.offsetWidth + 18) + "px";
	}
}

dtext.prototype.pophide = function()
{
	if(this.div)
	{
		this.div.parentNode.removeChild(this.div);
		this.div = null;
	}
}

dtext.itemover = function()
{
	this.style.backgroundColor = "#222266";
	this.style.color = "#ffffff";
}

dtext.itemout = function()
{
	this.style.backgroundColor = "#ffffff";
	this.style.color = "#000000";
}

dtext.itemup = function()
{
	this.dtext.itemup(this);
}

dtext.prototype.itemup = function(item)
{
	this.element.value = item.innerHTML;
	this.pophide();
	if(this.action)
	{
		this.action(this.element);
	}
}

dtext.numcheck = function(value)
{
	if(value)
	{
		if(value.match(/^\d*$/))
		{
			var n = value - 0;
			return n < 100000;
			// return true;
		}
		else
		{
			return false;
		}
	}
	return true;
}

dtext.floatcheck = function(value)
{
	if(value)
	{
		if(value.match(/^\d*[\.,]?\d{0,2}$/))
		{
			value = value.replace(/,/, ".");
			var n = value - 0;
			return n < 100000;
			// return true;
		}
		else
		{
			return false;
		}
	}
	return true;
}

function cvalact()
{
	var cval = document.getElementById("cval");
	var pval = document.getElementById("pval");
	var value = cval.value;
	var amount = pval.value - 0;
	value = value.replace(/,/, ".") - 0;
	var card = null;
	for(var i = 0; i < gcards.length; i++)
	{
		var e = document.getElementById("crd" + i);
		if(e.checked)
		{
			card = e.value;
			break;
		}
	}
	var shownxt = card !== null && value >= 5 && amount > 0;
	var nxt = document.getElementById("cnxt1");
	nxt.style.visibility = shownxt ? "visible" : "hidden";
	cnxt3show(!shownxt);
}

function pvalact()
{
	var card = null;
	for(var i = 0; i < gpackings.length; i++)
	{
		var e = document.getElementById("pck" + i);
		var nxt = document.getElementById("pnxt" + i);
		if(e.checked)
		{
			nxt.style.visibility = "visible";
		}
		else
		{
			nxt.style.visibility = "hidden";
		}
	}
}

function cnxt3show(on)
{
	var elem = document.getElementById("cnxt3");
	if(elem)
	{
		elem.style.visibility = on ? "visible" : "hidden";
	}
}

