/*  form.js
 Copyright 2008,2009 Erik Bogaerts
 Support site: http://www.zingiri.com

 This file is part of Zingiri Apps.

 Zingiri Apps is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Zingiri Apps is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Zingiri Apps; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

var zfFace = Class.create({
		
	initialize : function(name)
	{
	this.zf_i = 1;
	this.zf_a = new Array(); //type
	this.zf_c = new Array(); //form ID
	this.zf_o = new Array(); //element options
	
	this.zf_o['zfmandatory']=new Array();
	this.zf_o['zfunique']=new Array();
	this.zf_o['zfreadonly']=new Array();
	this.zf_o['zfhidden']=new Array();
	this.zf_e = new Object();
	this.zf_s = new Array();
	this.name=name;
	this.signature="face";
	var a;
	
	var c=$(name+'control');
	if (c) {
		a=c.down('#zfnewform');
		if (a) a.observe('click', this.zfAddForm.bindAsEventListener(this));
		a=c.down('#zf_saveface');
		if (a) a.observe('click', this.zfSaveFace.bindAsEventListener(this));
		a=c.down('#zf_deleteface');
		if (a) a.observe('click', this.zfDeleteFace.bindAsEventListener(this));
		a=c.down('#zf_type');
		if (a) a.observe('change', this.zfChangeType.bindAsEventListener(this));
		a=c.down('#zfform');
		if (a) a.observe('change', this.zfChangeForm.bindAsEventListener(this));
		a=c.down('#zfdeleteelement');
		if (a) a.observe('click', this.zfDeleteForm.bindAsEventListener(this));
		
	}
	
	}
,

zfAddForm : function() {

	var divFaces=$("zfaces");
	var divTag = document.createElement("div");
	var type = $("zf_type").value;
	if (type == "") {
		alert('Please select a type of field');
		return;
	}
	var form = $("zfform").value;
	if (form == "") {
		alert('Please select a form');
		return;
	}
	divTag.className = "faces "+type;
	divTag.setAttribute("onclick", this.name+".zfActiveField(" + this.zf_i + ");");
	divTag.id = "face_" + this.zf_i;
	divTag.style.background="red";
	divTag.style.minHeight = "30px";
	divTag.style.cursor = "move";
	divTag.style.position = "relative";
	divTag.innerHTML='<center>'+this.zf_i+'</center>';
	divFaces.appendChild(divTag);
	
	var ulTag = document.createElement("ul");
	ulTag.id = "zfaces"+this.zf_i;
	divTag.appendChild(ulTag);
	
	var n=new zfForm(this.zf_i,form,false);
	n.zfLoadForm();
	
	/*
	new Ajax.Request(
			zfurl+"../scripts/form.php",
			{
				method : "post",
				parameters : {
					'form' : form,
					'zf_id' : this.zf_i
				},
				onComplete : function(request) {
					divTag.innerHTML = request.responseText;
				}.bind(this)
			});
*/
	this.zf_a[this.zf_i] = type;
	this.zf_c[this.zf_i] = $("zfform").value;

	
	var params = {
	    tag: 'div', 
	    constraint: '', 
	    dropOnEmpty: true
	};
	Sortable.create($("zfaces"), params);
	
	this.zf_i++;
},

zfDeleteForm : function() {
	var divFaces=$("zfaces");
	var id = $("zffield").value;
	var divtag = $("face_" + id);
	divFaces.removeChild(divtag);
	this.zf_a[id] = "";
},

zfLoadElement : function(e) {
	
	var maindiv = this.zfFindPosition("zfaces");
	var divFaces=$("zfaces");
	//var divTag = document.createElement("div");
	var divTag = document.createElement("li");
	// $("zcontrol").value = maindiv;
	var type = e.type;
	var id = e.id;

	divTag.setAttribute("onclick", this.name+".zfActiveField(" + id + ");");
	divTag.id = "face_" + id;
	//divTag.class = "faces field";
	//divTag.setAttribute("align", "center");
	divTag.style.background = "#7baaed";
	divTag.style.margin = "3px auto";
	divTag.style.padding = "5px";

	new Ajax.Request(
			zfurl+"xgetFacesContent.php",
			{
				method : "post",
				parameters : {
					'zf_type' : type,
					'zf_id' : id,
					'zf_label' : e.label,
					'zfData' : Object.toJSON(e)
				},
				onComplete : function(request) {
					divTag.innerHTML = request.responseText;
				}.bind(this)
			});

	this.zf_a[id] = type;
	this.zf_c[id] = e.column;
	this.zf_o['zfmandatory'][id] = e.mandatory;
	this.zf_o['zfunique'][id] = e.unique;
	this.zf_o['zfreadonly'][id] = e.readonly;
	this.zf_o['zfhidden'][id] = e.hidden;
	this.zf_e[id]=new Object();
	this.zf_e[id]=e;
	
	if (id >= this.zf_i) this.zf_i=id+1;

	divFaces.appendChild(divTag);
	Sortable.create("zfaces");

},

zfActiveField : function(zf_field) {
	
	$("zffield").value = zf_field;
	$("zf_type").value = this.zf_a[zf_field];
	$("zfform").value = this.zf_c[zf_field];
	$("zfmandatory").checked = this.zf_o['zfmandatory'][zf_field];
	$("zfunique").checked = this.zf_o['zfunique'][zf_field];
	$("zfreadonly").checked = this.zf_o['zfreadonly'][zf_field];
	$("zfhidden").checked = this.zf_o['zfhidden'][zf_field];
},

zfChange : function(e) {
	o=Event.element(e).id;
	var zf_field = $("zffield").value;
	var f=$(o);
	var checked=f.checked;
	this.zf_o[o][zf_field]=checked;
},

zfChangeType : function(e) {
	var id = $("zffield").value;
	if (id.blank()) return;
	var elt=Event.element(e);
	this.zf_a[id]=elt.value;
	var divTag=$('face_'+id);
	divTag.className = "faces "+elt.value;

},

zfChangeForm : function() {
	var zf_field = $("zffield").value;
	//$("zfform").value=$("zfform").value.toUpperCase();
	if ($("zfform").value == "") { 
		alert("Element column can't be empty!");
		$("zfform").value=this.zf_c[zf_field];
		return; 
		}
	this.zf_c[zf_field] = $("zfform").value;
		
	var divTag=$("face_" + zf_field);
	$(divTag).innerHTML="";
	var ulTag = document.createElement("ul");
	ulTag.id = "zfaces"+zf_field;
	divTag.appendChild(ulTag);
	$(ulTag).focus;

	var n=new zfForm(zf_field,$("zfform").value,false);
	n.zfLoadForm();

},

zfSaveFace : function() {
	var j, p, label, c, ctot, ch;
	var e = new Object();
	var ec = 0;
	var cc = 0;
	var a = new Object();
	var refframe = this.zfFindPosition("zfaces");

	if ($("zffacename").value == "") { alert("Face name is mandatory!"); return; }
	if ($("zffaceentity").value == "") { alert("Entity name is mandatory!"); return; }
	if (this.zf_i <= 1) { alert("Add at least 1 element to your face!"); return; } 
	for (j = 1; j < this.zf_i; j++) {

		if (this.zf_a[j] != null && this.zf_a[j] != "") {
			if (this.zf_e[j] != null) e=this.zf_e[j]; else e=new Object();
			//ec++;
			cc++;
			ec=j;
			label = 'zf_' + j + '_name'
			//e = $("zf_" + j);
			frame = this.zfFindPosition("zf_" + j);
			ctot = $("zf_" + j + "_sf").childNodes.length - 1; //remove 1 because of clear div
			a[ec] = new Object();
			a[ec].subelements = new Object();
			if (this.zf_o['zfmandatory'][ec]) {
				a[ec].mandatory=1;
			} 
			if (this.zf_o['zfunique'][ec]) {
				a[ec].unique=1;
			} 
			if (this.zf_o['zfreadonly'][ec]) {
				a[ec].readonly=1;
			} 
			if (this.zf_o['zfhidden'][ec]) {
				a[ec].hidden=1;
			} 

			for (c = 1; c <= ctot; c++) {
				a[ec].subelements[c] = new Object();
				a[ec].subelements[c].id = c;
				if ($("element_" + j + "_" + c) != null)
					a[ec].subelements[c].populate = $("element_" + j + "_" + c).value;
				if ($("label_" + j + "_" + c) != null) {
					a[ec].subelements[c].label = $("label_" + j + "_" + c).childNodes[0].nodeValue;
				}
				if (e.subelements != null && e.subelements[c].sortorder != null)
					a[ec].subelements[c].sortorder=e.subelements[c].sortorder;
				if (e.subelements != null && e.subelements[c].hide != null)
					a[ec].subelements[c].hide=e.subelements[c].hide;
			}
			a[ec].id = ec;
			a[ec].label = $(label).childNodes[0].nodeValue;
			a[ec].x = frame[0] - refframe[0];
			a[ec].y = frame[1] - refframe[1];
			a[ec].type = this.zf_a[j];
			a[ec].column = this.zf_c[j];
			a[ec].children = $("zf_" + j + "_sf").childNodes.length;
		}

	}

	new Ajax.Request(
			zfurl+"saveFace.php",
			{
				method : "post",
				parameters : {
					zfFaceName : $("zffacename").value,
					zfFaceType : $("zffacetype").value,
					zfFaceEntity : $("zffaceentity").value,
					zfElementCount : cc,
					zfData : Object.toJSON(a),
					zfSortOrder : Sortable.serialize("zfaces")
				},
				onComplete : function(request) {
					alert(request.responseText);
				}.bind(this)
			});

},

zfDeleteFace : function() {
	new Ajax.Request(
			zfurl+"deleteFace.php",
			{
				method : "post",
				parameters : {
					zfFaceName : $("zffacename").value
				},
				onComplete : function(request) {
					alert(request.responseText);
					window.location="?zfaces=summary";
				}.bind(this)
			});

	//history.go(1);
	
},

zfLoadFace : function() {
	this.zf_i = 1;
	this.zf_a = new Array();
	this.zf_c = new Array();
	
	var this2=this;
	new Ajax.Request(
			zfurl+"loadFace.php",
			{
				method : "post",
				parameters : {
					zfFaceName : $("zffacename").value
				},
				onComplete : function(request) {
					this.zfLoadElements(request);
				}.bind(this)
			});
	
},

zfLoadElements : function(request) {
	// alert(request.responseText);
	var js = eval("(" + request.responseText + ")");
	var face = js.facename;
	var elementcount = js.elementcount;
	var e = new Object();
	$("zffacetype").value = js.facetype;
	$("zffaceentity").value = js.faceentity;
	// alert(face+"-"+elementcount);

	for (j = 1; j <= elementcount; j++) {
		e = js.data[j];
		// alert(e.label+"-"+e.x+"-"+e.y);
		this.zfLoadElement(e);
	}
	//zf_i = j;

},

zfCleanUp : function() {
	$("zffacename").value = "";
	$("zffield").value = "";
	$("zfform").value = "";
	$("zf_type").value = "";
}

})

