function Comm() {
	this.xmlrpc = null;
}

var comm = new Comm();


jrapid.SERVER_PATH = '/comm/xml/';
            
com_htmli_ui_Source.cache = false; 

function doForm(url, entity, id, defaultset, foo, websiteForm, centered, dontopenwindow) {
    var source = application.wrapNode(document.getElementById('source'));
    source.setUrl(url);
    source.open(function() {
        jrapid.loadForm(source.getChildContainer(), entity, id, defaultset, !dontopenwindow);
    });
}

function registerOk() {  window.location = '/management/main/index.htmli'; }

function loginOk(result) {
    if (result > 0) {
			window.location.reload();
        } else {
            alert('Invalid username or password');
        }
    }
            
function postUserCommentOk() {
	document.location.reload();
}

function doOnload() {
	var source = document.getElementById('loginSource');
	if (source) {
	var c2 = application.wrapNode(source); 
        c2.open(function() {
        jrapid.loadForm(c2.getChildContainer(), 'Login', 0);
    });
    }
}
            
            
function commentItem(id, comment, f) {
	comm.xmlrpc.executeAsync('UserComment.commentItem', function(ret, e) {
		if (e) {
			alert('Cannot send comments');
		} else {
			if (f)  {
				f();
			}
		}
    },id,comment);
    return false;
}
          
function commentView(id, comment, f) {
	comm.xmlrpc.executeAsync('UserComment.commentItem', function(ret, e) {
		if (e) {
			alert('Cannot send comments');
		} else {
			if (f)  {
				f();
			}
		}
    },id,comment);
    return false;
}

function commentWidget(id, comment, f) {
	comm.xmlrpc.executeAsync('UserComment.commentItem', function(ret, e) {
		if (e) {
			alert('Cannot send comments');
		} else {
			if (f)  {
				f();
			}
		}
    },id,comment);
    return false;    
}


function commentTransformer(id, comment, f) {
	comm.xmlrpc.executeAsync('UserComment.commentItem', function(ret, e) {
		if (e) {
			alert('Cannot send comments');
		} else {
			if (f)  {
				f();
			}
		}
    },id,comment);
    return false;    
}

function commentTemplate(id, comment, f) {
	comm.xmlrpc.executeAsync('UserComment.commentItem', function(ret, e) {
		if (e) {
			alert('Cannot send comments');
		} else {
			if (f)  {
				f();
			}
		}
    },id,comment);
    return false;    
}



function addComment() {
	$('commentComments').innerHTML = $('commentBox').getValue();
	$('commentBox').setValue('');
	var c = $('commentTemplate').cloneNode(true);
	$('commentCanvas').insertBefore(c,$('commentCanvas').children[0]);
	c.slideDown();
}



            
function rateView(id, f) {
	var rating = comm.stars;
	comm.xmlrpc.executeAsync('UserRating.rateItem', function(ret, e) {
		if (e) {
			alert('Cannot send rating');
		} else {
			if (ret) {
				if (f)  {
					f('View',id);
				}
			} else {
				alert('User already rated this view');
			}
		}
    },id,rating);
    $('ratingAverage'+id).pulsate();    
    return false;
}

function rateWidget(id, f) {
	var rating = comm.stars;
	comm.xmlrpc.executeAsync('UserRating.rateItem', function(ret, e) {
		if (e) {
			alert('Cannot send rating');
		} else {
			if (ret) {
				if (f)  {
					f('Widget',id);
				}
			} else {
				alert('User already rated this widget');
			}
		}
    },id,rating);
    $('ratingAverage'+id).pulsate();
    return false;
}

function rateTransformer(id, f) {
	var rating = comm.stars;
	comm.xmlrpc.executeAsync('UserRating.rateItem', function(ret, e) {
		if (e) {
			alert('Cannot send rating');
		} else {
			if (ret) {
				if (f)  {
					f();
				}
			} else {
				alert('User already rated this widget');
			}
		}
    },id,rating);
    $('ratingAverage'+id).pulsate();    
    return false;
}

function rateTemplate(id, f) {
	var rating = comm.stars;
	comm.xmlrpc.executeAsync('UserRating.rateItem', function(ret, e) {
		if (e) {
			alert('Cannot send rating');
		} else {
			if (ret) {
				if (f)  {
					f('Template',id);
				}
			} else {
				alert('User already rated this template');
			}
		}
    },id,rating);
    $('ratingAverage'+id).pulsate();    
    return false;
}

function rateItem(id, f) {
	var rating = comm.stars;
	comm.xmlrpc.executeAsync('UserRating.rateItem', function(ret, e) {
		if (e) {
			alert('Cannot send rating');
		} else {
			if (ret) {
				if (f)  {
					f(id);
				}
			} else {
				alert('You have already rated this');
			}
		}
    },id,rating);
    $('ratingAverage'+id).pulsate();    
    return false;
}

function updateRating(id) {
	var avg;
	var count;
	comm.xmlrpc.executeAsync('UserRating.getForItem', function(ret, e) {
		if (e) {
			alert('Cannot send rating');
		} else {
			avg = ret.split('|')[0];
			count = ret.split('|')[1];			
			var width = ((avg/5)*90) + 'px';
			$('ratingAverage'+id).children[0].children[0].style.width = width;			
			comm.previousWidth = width;
			$('ratingCount'+id).innerHTML = count;						
		}
    },id);
}

function moveStarsFront(target, event) {
	var front = target.children[0];

	if (!comm.previousWidth) {
		comm.previousWidth = front.style.width;
	}
	var x = application.wrapEvent(event).getClientX();

	var newWidth = 	x-application.wrapNode(front).getX()-4;	
	comm.stars = Math.round(newWidth / 18)+1;
	front.style.width = (comm.stars*18)  + 'px';
}


function restoreStars(target) {
	var front = target.children[0];
	front.style.width = comm.previousWidth;
	comm.previousWidth = null;

}

function addToProject(id) {
	var div = $('projects'+id);
	if (div.style.display == 'none') {
		div.slideDown();
	} else {
		div.slideUp();
	}
}
            
function addViewToProject(obj, id, projectUrl, projectId, viewName) {
	var url = projectUrl + '/jrapid/comm/add-view.jsp?view=' + id + '&viewName=' + viewName;
   	addElementToProject(obj, 'View', id, projectId, url);
}

function addWidgetToProject(obj, id, projectUrl, projectId, widgetName) {
	var url = projectUrl + '/jrapid/comm/add-widget.jsp?widget=' + id + '&widgetName=' + widgetName;
   	addElementToProject(obj, 'Widget', id, projectId, url);			
}

function addTransformerToProject(obj, id, projectUrl, projectId, transformerName, outputfile, regenerate, perentity, xpath) {
	var url = projectUrl + '/jrapid/comm/add-transformer.jsp?transformer=' + id 
			+ '&transformerName=' + transformerName + '&outputfile=' + outputfile 
			+ '&regenerate=' + regenerate + '&perentity=' + perentity + '&xpath=' + xpath;
	addElementToProject(obj, 'Transformer', id, projectId, url);			
}

function addTemplateToProject(obj, id, projectUrl, projectId, templateName) {
	var url = projectUrl + '/jrapid/comm/add-template.jsp?template=' + id + '&templateName=' + templateName;
	addElementToProject(obj, 'Template', id, projectId, url);
}

function addElementToProject(obj, element, id, projectId, url) {
	obj.disabled = true;
	window.frames['loginIframe'].location = url;
	$('addToProject'+id).pulsate();	
	//comm.xmlrpc.executeAsync('Project.add'+element, function(ret, e) {
	comm.xmlrpc.executeAsync('Project.addItem', function(ret, e) {
		if (e) {
			alert('Cannot add to project');
		} else {
			obj.disabled = false;
		}
    },projectId, id);
}

function removeViewFromProject(obj, id, projectUrl, projectId, viewName) {
	var url = projectUrl + '/jrapid/comm/remove-view.jsp?viewName=' + viewName;
	removeElementFromProject(obj, 'View', id, projectId, url);    
}

function removeWidgetFromProject(obj, id, projectUrl, projectId, widgetName) {
	var url = projectUrl + '/jrapid/comm/remove-widget.jsp?widgetName=' + widgetName;
	removeElementFromProject(obj, 'Widget', id, projectId, url);    
}

function removeTransformerFromProject(obj, id, projectUrl, projectId, transformerName, outputfile, regenerate, perentity, xpath) {
	var url = projectUrl + '/jrapid/comm/remove-transformer.jsp?transformerName=' + transformerName;
	removeElementFromProject(obj, 'Transformer', id, projectId, url);
}

function removeTemplateFromProject(obj, id, projectUrl, projectId, templateName) {
	var url = projectUrl + '/jrapid/comm/remove-template.jsp?templateName=' + templateName;
    removeElementFromProject(obj, 'Template', id, projectId, url);
}

function removeElementFromProject(obj, element, id, projectId, url) {
	obj.disabled = true;
	window.frames['loginIframe'].location = url;
	
	//comm.xmlrpc.executeAsync('Project.remove'+element, function(ret, e) {
	comm.xmlrpc.executeAsync('Project.addItem', function(ret, e) {
		if (e) {
			alert('Cannot add to project');
		} else {
			obj.disabled = false;
		}
    },projectId, id);

}
function viewRedirect(id) {
    window.location.href = 'view.jsp?id='+id;
 }
            
function widgetRedirect(id) {
    window.location.href = 'widget.jsp?id='+id;
 }
            
function transformerRedirect(id) {
    window.location.href = 'transformer.jsp?id='+id;
 }

function templateRedirect(id) {
    window.location.href = 'template.jsp?id='+id;
 }
 
function filterItem(id, page) {
	var select = document.getElementById(id); 
	var cat = select.value;
	if(cat=="-1") {
		window.location.href = page;
	} else {
		window.location.href = page+'?category='+escape(cat);
	}
}
            