﻿function profiling(applicationPath)
{
	this.applicationPath = applicationPath;
	this.pagePath = '/Comment.aspx';
	this.connector = null;
};

profiling.prototype.LoginToComment = function()
{
	var f = document.forms[0];
	if(f == null)
		return;

	// get data
	var email = f.login_email.value;
	var pwd = f.login_pwd.value;
	// validate
	var errors = new Array();
	if(email.split(' ').join('') == '')
		errors.push('insert an email address');
	if(pwd.split(' ').join('') == '')
		errors.push('insert a password');
	if(this.AlertErrors(errors))
		return;
	// connect
	this.connector = new net.XMLHTTPRequestWrapper(this.onLoad, this.onError);
	this.connector.owner = this;
	this.connector.loadData(
		this.applicationPath + this.pagePath + '?action=loginToComment',
		'POST',
		'login_email=' + email + '&login_pwd=' + pwd
	);
};

profiling.prototype.GetPassword = function()
{
	var f = document.forms[0];
	if(f == null)
		return;

	// get data
	var email = f.get_password_email.value;
	// validate
	var errors = new Array();
	if(email.split(' ').join('') == '')
		errors.push('insert an email address');
	if(this.AlertErrors(errors))
		return;
	// connect
	this.connector = new net.XMLHTTPRequestWrapper(this.onLoad, this.onError);
	this.connector.owner = this;
	this.connector.loadData(
		this.applicationPath + this.pagePath + '?action=getPassword',
		'POST',
		'get_password_email=' + email
	);
};

profiling.prototype.SubmitComment = function(IDitem)
{
	var f = document.forms[0];
	if(f == null)
		return;

	// get data
	var comment_text = f.comment_text.value;

	// validate
	var errors = new Array();
	if(IDitem == null || isNaN(IDitem))
		errors.push('IDitem not valid');
	if(comment_text == '')
		errors.push('insert a comment');
	if(this.AlertErrors(errors))
		return;
	// connect
	this.connector = new net.XMLHTTPRequestWrapper(this.onLoad, this.onError);
	this.connector.owner = this;
	comment_text = EpureFromTags(comment_text);
	this.connector.loadData(
		this.applicationPath + this.pagePath + '?action=insert_comment',
		'POST',
		'IDitem=' + IDitem + '&comment_text=' + comment_text
	);
};

profiling.prototype.Login = function()
{
	var f = document.forms[0];
	if(f == null)
		return;

	// get data
	var email = f.login_email.value;
	var pwd = f.login_pwd.value;
	// validate
	var errors = new Array();
	if(email.split(' ').join('') == '')
		errors.push('insert an email address');
	if(pwd.split(' ').join('') == '')
		errors.push('insert a password');
	if(this.AlertErrors(errors))
		return;
	// connect
	this.connector = new net.XMLHTTPRequestWrapper(this.onLoad, this.onError);
	this.connector.owner = this;
	this.connector.loadData(
		this.applicationPath + this.pagePath + '?action=login',
		'POST',
		'login_email=' + email + '&login_pwd=' + pwd
	);
};

profiling.prototype.AlertErrors = function(errors)
{
	if(errors.length == 0)
		return false;
	else
	{
		var errorString = '';
		for(var i = 0; i < errors.length; i++)
			errorString += errors[i].toString() + '\n';
		alert(errorString);
		return true;
	}
};

profiling.prototype.GetConnectionForm = function(IDitem)
{
	// connect
	this.connector = new net.XMLHTTPRequestWrapper(this.onLoad, this.onError);
	this.connector.owner = this;
	this.connector.IDitem = IDitem;
	this.connector.loadData(
		this.applicationPath + this.pagePath + '?action=islogged',
		'GET'
	);
};

profiling.prototype.Register = function()
{
	var f = document.forms[0];
	if(f == null)
		return;

	// get data
	var email1 = f.register_email1.value;
	var email2 = f.register_email2.value;
	var pwd1 = f.register_pwd1.value;
	var pwd2 = f.register_pwd2.value;
	var name = f.register_name.value;
	// validate
	var errors = new Array();
	if(email1.split(' ').join('') == '')
		errors.push('insert an email address');
	if(pwd1.split(' ').join('') == '')
		errors.push('insert a password');
	if(email1 != email2)
		errors.push('the two email addresses are not the same');
	if(pwd1 != pwd2)
		errors.push('the two email passwords are not the same');
	if(name.split(' ').join('') == '')
		errors.push('insert a display name');
	if(name.length > 20)
		errors.push('chosen name is too long, max 20 chars');
	if(name.indexOf('  ') != -1)
		errors.push('chosen name contains multiple spaces');
	if(this.AlertErrors(errors))
		return;
	// connect
	this.connector = new net.XMLHTTPRequestWrapper(this.onLoad, this.onError);
	this.connector.owner = this;
	this.connector.loadData(
		this.applicationPath + this.pagePath + '?action=register',
		'POST',
		'register_email=' + email1 + '&register_pwd=' + pwd1 + '&register_name=' + name
	);
};

profiling.prototype.onLoad = function()
{
	//alert('onLoad');
	var rootNode = this.owner.connector.req.responseXML.firstChild;
	var action = rootNode.attributes[0].value;
	switch(action)
	{
		case 'login':
			//alert(rootNode.firstChild.nodeValue);
			document.location = this.owner.applicationPath;
			break;
		case 'loginToComment':
			//alert(rootNode.firstChild.nodeValue);
			document.location.reload(true);
			break;
		case 'register':
			alert(rootNode.firstChild.nodeValue);
			document.location = this.owner.applicationPath;
			break;
		case 'islogged':
			var retValue = rootNode.attributes[1].value;
			this.owner.FillWithForm(this.IDitem);
			break;
		case 'insert_comment':
			document.location.reload(true);
			break;
		case 'getPassword':
			alert(rootNode.firstChild.nodeValue);
			break;
		default: alert('Unknown response.');
	}
};

profiling.prototype.onError = function()
{
	//alert('onError');
	alert(this.owner.connector.req.responseXML.firstChild.firstChild.nodeValue);
};

profiling.prototype.FillWithForm = function(IDitem)
{
	//alert('Comment form: ' + IDitem);
	var obj = Get('comment_form_' + IDitem.toString());
	var content = this.connector.req.responseXML.firstChild.firstChild.nodeValue;
	content = content.split('&lt;!--!IDitem!--&gt;').join(IDitem.toString());
	obj.innerHTML = content;
	//
	var f = document.forms[0];
	f.comment_text.value = '';
};

var Profiling = null;
function InitializeProfiling(applicationPath)
{
	Profiling = new profiling(applicationPath);
};

// epures strings form html tags and their content	
function EpureFromTags(str)
{
	var counting = true;
	var str2 = new String();
	for(var p = 0; p < str.length; p++)
	{
		if(str.charAt(p) == '<') counting = false;
		else if(str.charAt(p) == '>') counting = true;
			else if(counting == true) str2 += str.charAt(p)
	}
	return str2;
}

