// JavaScript Document

var d=document;
function sendData(ctr,action)
{
	var formInputs=$('#visible_form input, #visible_form select').not('[type=button]');
	var attr_array=[];
	s="";
	for(var i=0;i<formInputs.length;i++)
	{
		attr_array[i]=[];
		attr_array[i][0]=formInputs[i].name;
		attr_array[i][1]=formInputs[i].value;
		s+=attr_array[i][0]+":"+attr_array[i][1]+"\n";
	}
	//alert(s);
	postForm(ctr,action,attr_array);
}
function postForm (ctr, action, attr)
{
	var form=d.getElementById('postform');
	form.ctr.value=ctr;
	form.action.value=action;
	for (var i=0;i<attr.length;i++)
	{
		jQuery("#attr_array").append('<input name="attr_name[]" id="attr" type="hidden" value ="'+attr[i][0]+'" />');
		jQuery("#attr_array").append('<input name="attr_value[]" id="attr" type="hidden" value ="'+attr[i][1]+'" />');
	}
	form.submit();
}
var addedAttributesCount=0;
function addAttribute()
{
	var input=jQuery("input[name^=aa_]:last");
	var row;
	var label;
	if (input.length!=0){
		row=input.parent().parent();
		label=row.children('td:first').text();
		var num=parseInt(label.substr(label.lastIndexOf(" ")))+1;
		label=label.substr(0,label.lastIndexOf(" ")+1)+num;
	}
	else{
		input=jQuery("input[name=eventtype_name]");
		row=input.parent().parent();
		label='Attribute 1';
	}
	var tbl=row.parent();
	tbl.append("<tr><td>"+label+"</td><td><input type=\"text\" name=\"aa_new_"+addedAttributesCount+"\" /></td><td><a style=\"cursor:pointer\" onclick=\"delAttribute(this);\"><img border=\"0\" title=\"Delete\" alt=\"Delete\" src=\"media/style/MODxCarbon/images/icons/delete.gif\" /></a></td></tr>");
	addedAttributesCount++;
}
function delAttribute(obj){
	var row=jQuery(obj).parent().parent();
	row.children('td:first').css({"text-decoration":"line-through","color":"red"});
	var input=row.contents('td:eq(1)').children();
	input.attr('name','aa_del_'+input.attr('name').substr(3));
	row.children('td:last').children('a').attr('onClick','restoreAttribute(this);');
	row.children('td:last').children('a').children('img').attr({'src':'media/style/MODxCarbon/images/icons/save.png','title':'Restore','alt':'Restore'});
}
function restoreAttribute(obj){
	var row=jQuery(obj).parent().parent();
	row.children('td:first').css({"text-decoration":"none","color":"black"});
	var input=row.contents('td:eq(1)').children();
	input.attr('name','aa_'+input.attr('name').substr(7));
	row.children('td:last').children('a').attr('onClick','delAttribute(this);');
	row.children('td:last').children('a').children('img').attr({'src':'media/style/MODxCarbon/images/icons/delete.gif','title':'Delete','alt':'Delete'});
}
