function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=" + opacity + ",style=0)";
}

function formdatapreview(formid)
{
	form = document.forms[formid];
	for(var id=0; id<form.elements.length;id++)
	{
		var e = document.getElementById( form.elements[id].name + '_txt' );
		if(e)
		{
			var t = form.elements[id].value;
			e.innerHTML = t.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/\n/g, "<br>");
		}
	}

}

function switchelements(element_off_id,element_on_id)
{
    var element_off = document.getElementById(element_off_id);
    var element_on = document.getElementById(element_on_id);
    element_off.style.display = 'none';
    element_on.style.display = 'block';
    return false;
}

function dofadetopreview(formid, formdivid, previewid, validationfn)
{
	hideerror();
	
	if(!validationfn())
		return false;

	formdatapreview(formid);

	opacity(formdivid, 100, 0, 300);
	opacity(previewid, 0, 100, 1);
	setTimeout("switchpreview('" + formdivid + "','" + previewid + "')",300);
	return false;
}

function dofadetoedit(formdivid,previewid)
{
	opacity(previewid, 100, 0, 300);
	opacity(formdivid, 0, 100, 1);
	setTimeout("switchelements('" + previewid + "','" + formdivid + "')",300);
	return false;
}

function seterror(error)
{
	var errors = document.getElementById('errors');
	errors.style.display = "block";
	errors.innerHTML = errors.innerHTML + "<div>" + error + "</div>";
}

function hideerror()
{
	var errors = document.getElementById('errors');
	errors.innerHTML = "";
	document.getElementById('errors').style.display = "none";
}


function submitformtoproto(loadfn,module,page,formid)
{
	var serial = $(formid).serialize();
var url = "/ajax/" + module + '/' + page + '/';
// notice the use of a proxy to circumvent the Same Origin Policy.

new Ajax.Request(url, {
  method: 'post',
  onSuccess: loadfn,
  postBody: serial
});

//	var args = {url: , load: loadfn, formNode: document.getElementById(formid)};
//	dojo.io.bind(args);
	return false;
}

function requestproto(loadfn,module,page,action,payload)
{
var url = "/ajax/" + module + '/' + page + '/' + action + '/';
// notice the use of a proxy to circumvent the Same Origin Policy.
new Ajax.Request(url, {
  method: 'post',
  postBody: payload,
  onSuccess: function(obj) {
	var data = obj.responseText;
	loadfn(obj,data);
	}
});
return false;
}

function requestprotojson(loadfn,module,page,action,payload)
{
	var url = "/json/" + module + '/' + page + '/' + action + '/';
// notice the use of a proxy to circumvent the Same Origin Policy.
new Ajax.Request(url, {
  method: 'post',
  postBody: payload,
  onSuccess: function(obj) {
	var data = eval(obj.responseText);
	loadfn(obj,data);
	}
});
return false;
}


function submitformtoprotojson(loadfn,module,page,action,formid)
{
	var serial = $(formid).serialize();
	var url = "/json/" + module + '/' + page + '/' + action + '/';
// notice the use of a proxy to circumvent the Same Origin Policy.
new Ajax.Request(url, {
  method: 'post',
  onSuccess: function(obj,json) {
	var data = eval(obj.responseText);
	loadfn(obj,data);
	},
  postBody: serial
});
//	var args = {url: , load: loadfn, formNode: document.getElementById(formid)};
//	dojo.io.bind(args);
	return false;
}



function dofadetojsonrequest(formname,module,page,action,validationfn,loadfn)
{
	if(validationfn)
	{
		hideerror();
		if(!validationfn())
			return false;
	}
	submitformtoprotojson(loadfn, module, page, action, formname);
	return false;
}

