function formatNumber(amount) {
	amount = amount.toString().replace(/[^0-9\.\-]/g,'');
	
	var a = parseNumber(amount).split('.',2), d = a[1], i = parseInt(a[0]), minus = '';
	var n = new String(i), a = [], delimiter = ",";
	
	if(isNaN(i)) return '';
	if(i < 0) minus = '-';
	
	i = Math.abs(i);
	
	while(n.length > 3) {
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	
	if(n.length > 0) a.unshift(n);
	n = a.join(delimiter);
	if(d.length < 1) amount = n;
	else amount = n + '.' + d;
	amount = minus + amount;
	
	return amount;
}

	
function parseNumber(amount) {
	var i = parseFloat(amount), minus = '';
	
	if(isNaN(i)) i = 0.00;
	if(i < 0) minus = '-';
	
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	
	if(s.indexOf('.') < 0) s += '.00';
	if(s.indexOf('.') == (s.length - 2)) s += '0';
	s = minus + s;
	
	return s;
}

function validateNumeric(e, acceptPeriods) {
	var key, keychar;
	
	if (acceptPeriods === undefined) var acceptPeriods = true;
	
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
		
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) 
			|| (key==13) || ((acceptPeriods) && (key==46))) //|| (key==27) || (key==46))
		return true;
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;

	return false;
}

var outputContentInfo = '';

function centerThumb(obj) {
	var imgWidth = $(obj).width();
	//outputContentInfo = outputContentInfo + imgWidth + '<br>';
	//$('#outputContent').html(outputContentInfo);
	
	if(imgWidth != 0) {
		$(obj).css({marginLeft: (((imgWidth/2) - 50)*-1) + 'px'});
	}
}

function isValidEmail(sEmail) {
	return new RegExp(/^[\w\.\-]+\@[\w\.\-]+\.[A-Za-z]{2,6}$/).test(sEmail);
}

/* var messageBoxA = {
	defaultMainContainerId: "dialogMissingInfo",
	defaultMessageContainerId: "dialogMissingInfoText",
	defaultTittle: "Missing Information",
	
	defaultWidth: 460,
	defaultClose: function() { return false; };
	
	setupMessageBox : function() {
		#("#" + this.defaultMainContainerId).dialog({
			autoOpen: true,
			bgiframe: true,
			resizable: false,
			title: title,
			height: 'auto',
			width: this.defaultWidth,
			modal: true,
			close: function(event, ui) {
				this.defaultClose();
			},
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				Ok: function() {
					jQuery(this).dialog('close');
				}
			}
		});
	},
	
	writeMessageBox : function() {
		$("body").append(
			$(document.createElement("div")).attr({
				"id": this.defaultMainContainerId,
				"title": this.defaultTittle,
				"style": "display: none;"
			}).append(
				$(document.createElement("p")).append(
					$(document.createElement("span")).attr({
						"class": "ui-icon ui-icon-alert",
						"style": "float:left; margin:0 7px 20px 0;"
					})
				).append(
					$(document.createElement("span")).attr({
						"id": this.defaultMessageContainerId
					})
				)
			)
		);
	}
}; */

function messageBox(message,func,title,buttons) {
	jQuery("#dialogMissingInfoText").html(message);
	
	if(func === undefined || typeof func != 'function')
		var func = function() { return false; };
	if(title === undefined)
		var title = "Missing Information";
	if(buttons === undefined || typeof buttons != 'object')
		var buttons = 	{Ok: function() { jQuery(this).dialog('close'); }};
		
	if(!!jQuery("#dialogMissingInfo").data('dialog')) {
		jQuery("#dialogMissingInfo").dialog('option', 'title', title)
		jQuery("#dialogMissingInfo").dialog('option', 'close', func);
		jQuery("#dialogMissingInfo").dialog('option', 'buttons', buttons);
		jQuery("#dialogMissingInfo").dialog('open');
	} else
		jQuery("#dialogMissingInfo").dialog({
			autoOpen: true,
			bgiframe: true,
			resizable: false,
			title: title,
			height: 'auto',
			width: 460,
			modal: true,
			close: function(event, ui) {
				func();
			},
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			buttons: {
				Ok: function() {
					jQuery(this).dialog('close');
				}
			}
		});
}

function checkFormLogin(oForm) {
	if(jQuery.trim(oForm.username.value) == '') { //if(!isValidEmail(oForm.username.value)) {
		messageBox("Please enter your email", function() { oForm.username.focus(); });
		return false;
	}
	if(oForm.password.value == '') {
		messageBox("Please enter your password", function() { oForm.password.focus(); });
		return false;
	}
	return true;
}

function checkFormListingMain(oForm) {
	if(jQuery.trim(oForm.title.value) == '') {
		messageBox("Please enter the title", function() { oForm.title.focus(); });
		return false;
	}
	if((jQuery.trim(oForm.categoryID.value) == '') ||  isNaN(oForm.categoryID.value)) {
		messageBox("Please enter the category", function() { oForm.categoryID.focus(); });
		return false;
	}
	if(jQuery.trim(oForm.brandname.value) == '') {
		messageBox("Please select the brand", function() { oForm.brandname.focus(); });
		return false;
	}
	if((jQuery("input[name='attributes']:checkbox").length > 0)
			&& (jQuery("input[name='attributes']:checkbox:checked").length == 0)) {
		messageBox("Please select one or more attributes", function() { oForm.categoryID.focus(); });
		return false;
	}
	if(jQuery("input[name='conditionid']:radio:checked").length == 0) {
		messageBox("Please select the condition of the product", function() { oForm.conditionid[0].focus(); });
		return false;
	}
	if(parseNumber(oForm.price.value) == 0) {
		messageBox("Please enter the amount you want for the product", function() { oForm.price.focus(); });
		return false;
	}
	if(jQuery.trim(oForm.firstname.value) == '') {
		messageBox("Please enter your first name", function() { oForm.firstname.focus(); });
		return false;
	}
	if(jQuery.trim(oForm.lastname.value) == '') {
		messageBox("Please enter your last name", function() { oForm.lastname.focus(); });
		return false;
	}
	if(jQuery.trim(oForm.address.value) == '') {
		messageBox("Please enter your address", function() { oForm.address.focus(); });
		return false;
	}
	if(jQuery.trim(oForm.phone1.value) == '') {
		messageBox("Please enter your phone", function() { oForm.phone1.focus(); });
		return false;
	}
	if(!isValidEmail(oForm.email.value)) {
		messageBox("Please enter your email", function() { oForm.email.focus(); });
		return false;
	}
	if(oForm.username && oForm.password) {
		if((jQuery.trim(oForm.username.value) == '') || (oForm.validUsername.value != 1)) { //if(!isValidEmail(oForm.username.value)) {
			messageBox("Please enter your username", function() { oForm.username.focus(); });
			return false;
		}
		if(jQuery.trim(oForm.password.value) == '') {
			messageBox("Please enter your password", function() { oForm.password.focus(); });
			return false;
		}
		if(jQuery.trim(oForm.password2.value) == '') {
			messageBox("Please enter your password confirmation", function() { oForm.password2.focus(); });
			return false;
		}
		if(oForm.password.value != oForm.password2.value) {
			oForm.password.value = "";
			oForm.password2.value = "";
			messageBox("Your passwords must match", function() { oForm.password.focus(); });
			return false;
		}		
	}

	return true;
}
