
function focusInput() {
	var i = $(this).attr('ref');
	$('#'+i).focus();
	/* $('#'+i).parent('li').removeClass('formFieldError'); */
};

function missingReqField(field_name) {
	if(typeof field_name == 'object') {
		field_name = field_name.id;
	}
	var fn = jQuery.FIELD_NAMES[field_name];
	if(fn == null) {
		fn = field_name;
	}
	
	addError('Please provide a valid '+fn+' in the field provided.', {field: field_name});
};

function addError(msg, opts) {
	// show error box
	jQuery('#errorSection').show();
	
	var settings = jQuery.extend({
		field: null
	}, opts);
	
	if(settings.field != null) {
		$('#'+settings.field).setInvalid();
	}
	
	var e = document.createElement('li');
	e.setAttribute('class', "error_msg");
	e.setAttribute('ref', settings.field);
	e.onclick = focusInput;
	
	var txtmsg = document.createTextNode(msg);
	e.appendChild(txtmsg);
	
	var elist = document.getElementById('elist');
	if(elist == null || !elist) {
		return;
	}
	elist.appendChild(e);
};

function resetError() {
	jQuery('#elist').empty();
	jQuery('#errorSection').hide();
};

function hideError() {
	var section = document.getElementById('errorSection');
	
	if(section == null || !section) {
		return;
	}
	jQuery('#errorSection').hide();
	jQuery('#elist').hide().empty().show();
	
};

function cleanError() {
	var section = document.getElementById('errorSection');
	
	if(section == null || !section) {
		return;
	}
	jQuery('#errorSection').hide();
	jQuery('#elist').hide().empty().show();
};

function showPHPErrors() {
	jQuery('SPAN.error').each(function () {
		var msg = $(this).text();
		var f = $(this).attr('id');
		f = f.replace('err_', '');
		addError(msg, {field: f});
	});
};

function initErrorBox() {
	var section = document.getElementById('errorSection');
	
	if(section == null || !section) {
		return;
	}
	
	$(section).attr('ref', 0).hide();
	
	var box = document.createElement('div');
	box.setAttribute('id', "errorbox");

	var elist = document.createElement('ul');
	elist.setAttribute('class', "error_list");
	elist.setAttribute('id', "elist");

	box.appendChild(elist);
	section.appendChild(box);
	
	showPHPErrors();
	
	return true;
};

jQuery(document).ready(function () {
	initErrorBox();
	// addError('Please provide a valid domain name with 3+ letters.');
});
