/**
* errorcodes:
* 101: ajaxFailed (server can't be reached.) 
* 201: checkFailed (input check failed.) 
* 
*/

var ajaxSendForm = function(opts){
	var cur = this;
	var succesCallback = null;
	var attachedTo = null;
	var method = '';
	var sendToUrl = "";
	var baseUrl = "";
	var checks = [];
	var checkErrorCallbacks = [];
	var errorCallback = null;
	
	this.init = function(){
		this.setEvents();
	}
	this.addCheck = function(name, callback, errorCallback){
		checks[name] = callback;
		checkErrorCallbacks[name] = errorCallback;
	}
	this.setEvents = function(){
		attachedTo.submit(function(){
			$(attachedTo).find('.error').remove();
			if(cur.check()){
				cur.submit();
			}
			else{
				cur.error('checkFailed', 201, 'input check failed.');
			}
			return false;
		});
	}
	
	this.check = function(){
		var goon = true;
		for(i in checks){
			if(!checks[i]($(attachedTo).find('[name='+i+']').val(), $(attachedTo).find('[name='+i+']'), attachedTo, checkErrorCallbacks[i])){
				goon = false;
			}
		}
		return goon;
	}
	
	this.error = function(a, b, c){
		// errorcallback(type, errorcode, errormessage, errorfor, errorobject);
		if(typeof a == 'object' && a instanceof XMLHttpRequest){
			if(errorCallback != null) errorCallback('ajaxFailed', 101, 'server can\'t be reached.', attachedTo, c);
		}
		else{
			if(errorCallback != null) errorCallback(a, b, c, attachedTo, null);
		}
	}
	this.sent = function(data){
		succesCallback(data, attachedTo, cur.getUrl(), method);
	}
	this.getUrl = function(){
		if(!sendToUrl.match(/^http:\/\//)) 
			return baseUrl+sendToUrl;
		return sendToUrl;
	}
	this.submit = function(){
		url = cur.getUrl();
		$.ajax({
		  url: url,
		  success: cur.sent,
		  error: cur.error,
		  type: method,
		  data: attachedTo.serialize()
		});
		return false;
	}
	
	for(x in opts){
		switch(x){
			case 'base':
			case 'baseUrl':
				baseUrl = opts[x];
			break;
			case 'succes':
			case 'succesCallback':
				succesCallback = opts[x];
			break;
			case 'error':
			case 'errorCallback':
				errorCallback = opts[x];
			break;
			case 'method':
				method = opts[x].toUpperCase();
			break;
			case 'sendToUrl':
				sendToUrl = opts[x];
			break;

			case 'attachedTo':
			case 'attachTo':
				attachedTo = $(opts[x]);
			break;
		}
	}

	if(method.length == 0){
		method = attachedTo.attr('method').toUpperCase();
	}
	if(sendToUrl.length == 0){
		sendToUrl = attachedTo.attr('action');
	}
	this.init();
}

// check callbacks. these are not part of the ajaxsendform script, its merely an extra. you can define these functions yourself.
// a check function can also hold a callback, you can define them using the addCheck method. 
var checkEmail = function(str, element, form, callback){
	
	if(Boolean(str.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$/))){
		if(callback != undefined) callback(true, str, element, form);
		return true;
	}
	else{
		if(callback != undefined) callback(false, str, element, form);
		return false;
	}
}

var checkNonEmpty = function(str, element, form, callback){

	if(str.length > 0 && str != $(element).attr('title')){
		if(callback != undefined) callback(true, str, element);
		return true;
	}
	if(callback != undefined) callback(false, str, element, form);
	return false;
}

