/**
 * Javascript Form Utils
 */

if ( HP == null ) var HP = {};
if ( HP.Forms == null ) HP.Forms = {};
if ( HP.Forms.Utils == null ) HP.Forms.Utils = {};


HP.Forms.Utils.ClickBox = new Class({
	implements : [Events],
	
	initialize : function(link, formid) {
		this.link = $(link);
		this.form = $(formid);
		this.input = $(formid+"_input");
		this.submit = $(formid+"_submit");
		this.cancel = $(formid+"_cancel");
		this.form.addEvent("submit", this.update.bindWithEvent(this));
		
		this.link.addEvent("mouseover", function(evt) {
			this.highlight();
		});
		
		this.submit.addEvent("click", this.update.bindWithEvent(this));
		this.cancel.addEvent("click", this.hide.bindWithEvent(this)),
		this.link.addEvent("click", this.show.bindWithEvent(this));
	},
	
	show : function() {
		this.link.tween("opacity",0);
		this.form.set("opacity",0);
		this.form.style.visibility = "visible";
		this.form.style.display = "block";
		this.form.tween("opacity",1);
		this.input.value = this.link.innerHTML;
		this.input.select();
		
		return false;
	},
	
	hide : function() {
		this.link.tween("opacity",1);
		this.form.set("opacity",0);
		this.form.style.visibility = "hidden";
		this.form.style.display = "none";
	},
	
	update : function() {
		this.form.send();
		this.link.innerHTML = this.input.value;
		this.hide();
		return false;
	}
	
});


HP.Forms.Utils.SyncPriceList = function(name, inputlist) {
	var i=0;
	
	while(true) {
		if ( $(name + '_' +i) == null) break;
		
		$each(inputlist, function(itm,idx,obj){
			$(itm+'_'+i).value = $(name + '_' +i).value; 
		},this);
		
		i++;
	}
	
}

HP.Forms.Utils.BuyAbo = function(Abo_id, Praemien_id) {
	$('products_id').value = Abo_id;
	$('praemien_products_id').value = Praemien_id;
	
	$('products_form').submit();
	
	return false;
}

HP.Forms.Utils.BuyProduct = function(Products_id) {
	$('products_id').value = Products_id;
	$('products_form').submit();

	return false;
}


HP.Forms.Utils.ShowThemenbereichItems = function(Themenbereich, namespace, target, source) {
	source = $(source);
	target = $(target);
	
	var myRequest = new Request.JSON({
		"url"		: "/AJAXRequest/" + Math.random(),
		"method"	: "post",
		"data"		: {
			"namespace" : namespace,
			"data"		: Themenbereich
		},
		"onSuccess" : function(responseJSON, responseText) {
			target.empty();
			//alert(responseText);
			var span;
			var input;
			var p;
			var br;
			var item;
			
			for(i=0; i < responseJSON.length; i++) {
				item = responseJSON[i];
				
				p = new Element("p",{ "styles": { "float" : "left", "margin" : "3px 0px 7px 0px" }})
				
				span = new Element("p",{
					"html" : item.Name,
					"styles": {
						"float": "left",
						"width": "315px"
					}
				});		
				
				input = new Element("input", {
					"type" : "checkbox",
					"name" : "id[]",
					"value": item.Id, 
					"styles": {
						"float" : "left"
					},
					"events" : {
						"click" : function() {
							var list = target.getElementsByTagName("input");
							
							var checked = false;
							for (i=0; i < list.length; i++) {
								if ( list[i].checked ) {
									checked = true;
									break;
								}
							}
							
							//source.readonly = !checked;
						}
						
					}
				});
				
				br = new Element("br",{
					"clear" : "all"
				});
				
				input.inject(p);
				span.inject(p);
				p.inject(target);
				br.inject(target);
			}
			
			
		}
	});
	
	myRequest.send();
}

/**
 * updateSelectBox
 * zum nachladen einer SelectBox wenn der Inhalt einer anderen sich geändert hat
 * 
 * @author Bruno Lorenz, <b.lorenz@happy-pixel.de>
 */
HP.Forms.Utils.updateSelectBox = function(target,source,namespace) {
	source = $(source);
	target = $(target);
	
	while ( target.options.length > 0 ) {
		target.options[0] = null;
	}

	var myRequest = new Request.JSON({
		"url"		: "/AJAXRequest",
		"method"	: "post",
		"data"		: {
			"namespace" : namespace,
			"data"		: source.options[ source.options.selectedIndex ].value
		},
		"onSuccess" : function(responseJSON, responseText) {
			var data;
			for(var i=0; i < responseJSON.length; i++) {
				data = responseJSON[i];
				target.options[ target.options.length ] = new Option(data.Display, data.Value);
			}
		}
	});
	
	myRequest.send();
}

/*
 * StartSeitenkonfigurator
 * 
 */
HP.Forms.Utils.StartSeitenkonfigurator = function() {
	var data = [];
	$each($$(".DragContainer div.img, .DragContainer-clear div.img"), function(itm,idx,obj) {
		data.push({
			"id"	: itm.id,
			"sort"	: idx
		});
	},this);

	//alert(JSON.encode(data));
	var myRequest = new Request.JSON({
		"url"		: "/AJAXRequest",
		"method"	: "post",
		"data"		: {
			"namespace" : "Admin.Startseitenverwaltung.Sortieren",
			"data"		: JSON.encode(data)
		}
	});
	
	myRequest.send();	
}

/**
 * SortItems
 * dient dem Sorter als Sortierfunktion
 * 
 */
HP.Forms.Utils.SortItems = function(mUL, redirectURL, namespace) {
	mUL = $(mUL);
	
	var ary = [];
	var list = mUL.getElementsByTagName("li");
	for(var i=0; i < list.length; i++ ) {
		//list[i].setAttribute("sort", i);
		ary.push({
			"id" 	: list[i].getAttribute("value"),
			"sort"	: i
		});
	}
	
	var myRequest = new Request.JSON({
		"url"		: "/AJAXRequest",
		"method"	: "post",
		"data"		: {
			"namespace" : namespace,
			"data"		: JSON.encode(ary)
		},
		"onSuccess" : function(responseJSON, responseText) {
			var returnValue = responseJSON;
			if ( returnValue.Return == 0 ) {
				window.location.href= redirectURL;
			} else {
				alert( returnValue.Error );
			}
		}		
	});
	
	myRequest.send();
}

HP.Forms.Utils.CharCounter = new Class({
	initialize: function(input, maxchars) {
		this.input = $(input);			
		if (maxchars == null) {
			if (this.input != null)		 {
				this.maxchars = this.input.getAttribute("maxlength");
			}
		} else {
			this.maxchars = maxchars;
		}

		this.counter = new Element("div", {
			"html" : parseInt((this.input!=null ? this.maxchars - this.input.value.length : 0)),
			"class" : "CharCounter"
		});
		
		if (this.input != null && this.input.nodeName == "TEXTAREA") {
			this.counter.addClass("cc-nextline");
		}
		
		window.addEvent("domready", this.setup.bind(this));
	},
	
	setup : function() {
		if (this.input == null) { alert("blubb"); return; }
		
		this.input.addEvent("keydown", this.onKeydown.bind(this));
		this.input.addEvent("keyup", this.onKeydown.bind(this));
		this.counter.inject(this.input.getParent());
	},
	
	recountChars : function() {
		var val = this.maxchars - this.input.value.length;
		if ( val < 0) val = 0;
		this.counter.innerHTML = val;
	},
	
	onKeydown : function(sender) {
		var evt = new Event(sender);
		
		this.recountChars();
		
		if (this.input.value.length > this.maxchars) {
			this.input.value = this.input.value.substr(0,this.maxchars);
			if (evt.key!="backspace" && evt.key !="delete"&& evt.key !="left"&& evt.key !="right" && evt.key!="$" && evt.key!="#") return false;
		}
		return true;
	},
	
	onKeyup : function(sender) {
		this.recountChars();
	}
});
