/**
 * @version	$Id: easydns.message.js 652 2007-11-15 14:11:50Z awyld $
 * @author Andrew Wyld <awyld@easydns.com>
 * @category javascript
 * @copyright Copyright (c) 2007, EasyDNS Technologies Inc.
 */

/**
 * GLOBAL VARIABLES
 */
var UERROR = false;
var TIMER_SHOW = null;
var E_COUNT = 1;

function UserErrorItem(msg) {
	this.idx = E_COUNT++;
	this.field_name = null;
	this.el = null;
	this.msg = msg;
	this.domain = false;
	this.before = '<div style="display: block; border: 1px solid;" class="in ltin tpin" ';
	this.after = '</div>'; 
	
	this.print = UserErrorItem__print;
};

/* Output error message HTML string */
function UserErrorItem__print() {
	var msg = '';
	var before = '';
	if(this.idx) {
		this.before += ' id="errnum_' + this.idx + '" ';
	}
	if(isEl(this.el)) {
		before = this.before+'  onclick="focusField(\''+this.el+'\');">';
	}
	msg = before+this.msg+this.after;
	return msg;
};

function UserErrorQueue() {
	this.pending = new Bucket();
	this.container = '#error';
	this.autoclean = true;
	this.mergeError = false;
	this.max_idx = 0;
	
	this.showServerErrors = UserErrorQueue__showServerErrors;
	this.missingRequired = UserErrorQueue__missingRequired;
	this.regError = UserErrorQueue__regError;
	this.flush = UserErrorQueue__flush;
	this.show = UserErrorQueue__show;
	this.clean = UserErrorQueue__clean;
	this.hasErrors = UserErrorQueue__hasErrors;
	
	try {
		if(isEl(this.container)) {
			jQuery(this.container).hide();
		}
	} catch(e) {
		return null;
	}
};

/**
 * UserErrorQueue::showServerErrors
 * 
 * @return bool Success
 */
function UserErrorQueue__showServerErrors() {
	var serrors, ecnt, i;
	var finalcnt = 0;

	serrors = jQuery('.error');
	
	if(serrors == null || serrors.length < 1) {
		return true;
	}
	
	ecnt = serrors.length
	finalcnt = ecnt;
	
	for(i = 0; i < ecnt; i++) {
			var msg = jQuery(serrors[i]).html();
			var item = new UserErrorItem(msg);
			var fid = jQuery(serrors[i]).attr('id');
			if(fid == null || !fid) {
				finalcnt--;
				continue;
			}
			fid = fid.replace('err_', '');
			item.el = fid;
			fid = '#'+fid;
			
			jQuery(fid).setInvalid();
			this.pending.add(item);
	}
	
	if(finalcnt > 0) {
		TIMER_SHOW = window.setTimeout(UERROR.show, 700);
	}

	return true;
};

/* Create error messages for form field problems. */
function UserErrorQueue__missingRequired(field_name, el) {
	var fn, fobj;
	if(field_name == null || field_name.length == 0) {
	}
	try {
		var ftype = typeof field_name;
		if((ftype == 'object')) {
			fobj = field_name;
			field_name = field_name.id;
		}
	} catch(e) {
		return false;
	}
	
	
	
	try {
		// var fid = jQuery('#'+field_name).attr('id');
		fn = jQuery.FIELD_NAMES[field_name];
		if(fn == null) {
			fn = field_name;
		}
		var item = new UserErrorItem('Please provide a valid '+fn+' in the field provided.');
		addError('Please provide a valid '+fn+' in the field provided.', {field: field_name});
		item.el = field_name;
		$J('#'+field_name).parent('li').addClass('formFieldError');
		// this.pending.add(item);
		
		if(el == null || !isEl(el)) {
			if(this.container == null || !isEl(this.container)) {
				return true;
			} else {
				el = this.container;
			}
		}

	} catch(e) {
		return false;
	}
	return true;
};

/* Create error messages for registration problems. */
function UserErrorQueue__regError(field_id, whois_status, dname) {
	
	addError(whois_status, {field: field_id});
	return true;
	
	/*
	var item = new UserErrorItem(whois_status);
	if(dname != null) {
		item.domain = dname;
	}
	item.el = field_id;
	$J('#'+field_id).parent('li').addClass('formFieldError');
	this.pending.add(item);
	return true;
	*/
};

/* Build the HTML to create the error boxes. */
function UserErrorQueue__flush() {
	var msg, item, buffer = false;
	if(this.pending.locked) {
		return false;
	}
	if(this.container == null || !isEl(this.container)) {
		return false;
	}
	if(this.pending == null || this.pending.length < 1) {
		return false;
	}
	var ecnt = this.pending.length;
	this.pending.locked = true;
	$J(this.container).addClass('out');
	while( (item = this.pending.next()) ) {
		if(item != null) {
			buffer = '';
			buffer = item.print();
			if(buffer != null && buffer.length > 0) {
				$J(this.container).append(buffer);
			}
		}
	}
	this.pending.locked = false;
	
	/* Reset counter */
	this.max_idx = E_COUNT;
	E_COUNT = 1;
	
	return true;
};

/* Display the full error box after it is populated. */
function UserErrorQueue__show() {
	
	return false;
	
	
	
	
	var frtn = false;
	try {
		if(TIMER_SHOW != null) {
			window.clearTimeout(TIMER_SHOW);
			TIMER_SHOW = null;
		}
		
		if(UERROR.container == null || !isEl(UERROR.container)) {
			UERROR.container = '#error';
		}
	} catch(e) {
		return false;
	}
	
	try {

		if(UERROR.autoclean) {
			UERROR.clean();
		}
		frtn = UERROR.flush();
		if(this.mergeError && E_COUNT > 1) {
			$('div.in:first').css('border-bottom', 'none');
			$('div.in:last').css('border-top', 'none');
		}
		if(frtn) {
			jQuery('#error').show();
		}
	} catch(e) {
		return false;
	}
	return true;
};

/* Hide and remove previous error messages. */
function UserErrorQueue__clean() {
	try {
		if(this.pending.locked) {
			return false;
		}
		
		if(this.container == null || !isEl(this.container)) {
			return false;
		}
		$(this.container).html('');
		$('#error2').hide();
	} catch(e) {
		return false;
	}

	return true;
};



function UserErrorQueue__hasErrors() {
	if(this.pending != null && this.pending.length == 0) {
		return false;
	}
	return true;
};

/**
 * Simple function to force the cursor to select whichever element
 * id you provide.
 *
 * NOTE:
 * This function does not use jQuery naming conventions.
 *
 * @param string Element ID in DOM format.
 * @return bool Success.
 */
function focusField(field_id) {
	try {
		var el = document.getElementById(field_id).focus();
	} catch(e) {
		return false;
	}
	return true;
};

jQuery(document).ready(function () {
		UERROR = new UserErrorQueue();
		jQuery.extend({
				uerror: UERROR		
		});
			
		UERROR.showServerErrors();
});

/* END */
jQuery('body').unload(function () {
	if(TIMER_SHOW != null) {
		window.clearInterval(TIMER_SHOW);
		TIMER_SHOW = null;
	}
});

