
function remove_spaces(field_id) {
	jQuery('#' + field_id).val(jQuery('#' + field_id).val().split(' ').join(''));
}

function focus_next_field(field_from, field_to, num_chars) {
	var chars = jQuery('#' + field_from).val().length;

	if (chars == num_chars) {
		jQuery('#' + field_from).next('input').focus();
	}
}

function explode(item, delimiter) {
	tmp_array = new Array(1);
	var count = 0;
	var tempstring = new String(item);

	while (tempstring.indexOf(delimiter) > 0) {
		tmp_array[count] = tempstring.substr(0, tempstring.indexOf(delimiter));
		tempstring = tempstring.substr(tempstring.indexOf(delimiter) + 1, tempstring.length - tempstring.indexOf(delimiter) + 1);
		count = count + 1;
	}

	tmp_array[count] = tempstring;
	return tmp_array;
}

function in_array(what, where) {
	var out = false;

	for (i=0; i < where.length; i++) {
		if (what == where[i]) {
			out = true;
			break;
		}
	}

	return out;
}

function print_1d_array(array) {
	document.write("<table border=1>");
	document.write("<tr>");
	for (row=0; row < array.length; row++) {
		document.write("<td>" + array[row] + "</td>");
	}
	document.write("</tr>");
	document.write("</table>");
}

function print_2d_array(array) {
	document.write("<table border=1>");
	for (row=0; row < array.length; row++) {
		document.write("<tr>");
		for (col=0; col < array[row].length; col++) {
			document.write("<td>" + array[row][col] + "</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
}

function is_array(obj) {
	return obj && !(obj.propertyIsEnumerable('length')) && typeof obj === 'object' && typeof obj.length === 'number';
}

function round_decimals(original_number, decimals) {
	var result1 = original_number * Math.pow(10, decimals);
	var result2 = Math.round(result1);
	var result3 = result2 / Math.pow(10, decimals);

	return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places) {
	// Convert the number to a string
	var value_string = rounded_value.toString();

	// Locate the decimal point
	var decimal_location = value_string.indexOf('.');

	// Is there a decimal point?
	if (decimal_location == -1) {
		// If no, then all decimal places will be padded with 0s
		decimal_part_length = 0;

		// If decimal_places is greater than zero, tack on a decimal point
		value_string += decimal_places > 0 ? '.' : '';
	}
	else {
		// If yes, then only the extra decimal places will be padded with 0s
		decimal_part_length = value_string.length - decimal_location - 1;
	}

	// Calculate the number of decimal places that need to be padded with 0s
	var pad_total = decimal_places - decimal_part_length;

	if (pad_total > 0) {
		// Pad the string with 0s
		for (var counter = 1; counter <= pad_total; counter++) {
			value_string += '0';
		}
	}

	return value_string;
}

function format_float(obj) {
	var o = document.getElementById(obj);
	var oo = o.value.replace(',', '.');

	if (isFloat(oo)) {
		o.value = round_decimals(oo, 2);
	}
	else {
		o.value = '';
	}
}

function toInt(n) {
	return n * 1;
}

function isFloat(n) {
	if (n == 0 || n == 0.00) {
		return false;
	}

	// Test for integer
	if ((n.length > 0) && !(/[^0-9]/).test(n)) {
		return true;
	}
	else {
		// Test for float
		if ((n.length > 0) && !(/[^0-9.]/).test(n) && (/\.\d/).test(n)) {
			return true;
		}
		else {
			return false;
		}
	}
}

function left(str, n) {
	if (n <= 0) {
		return '';
	} else if (n > String(str).length) {
		return str;
	} else {
		return String(str).substring(0,n);
	}
}

function right(str, n) {
	if (n <= 0) {
		return '';
	} else if (n > String(str).length) {
		return str;
	} else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

function mid(str, start, len) {
	// Make sure start and len are within proper bounds
	if (start < 0 || len < 0) {
		return '';
	}

	var iEnd, iLen = String(str).length;

	if (start + len > iLen) {
		iEnd = iLen;
	} else {
		iEnd = start + len;
	}

	return String(str).substring(start,iEnd);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function check_email(email) {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

	if (filter.test(email)) {
		return true;
	}

	return false;
}

function gtab_click(id) {
	// Hide all tabs
	for (i=1; i <= 4; i++) {
		$('#gtab'+i+'_content').hide();
	}
	
	// Fide in the tab
	$('#gtab'+id+'_content').fadeIn();
	
	// Unhighlight all tabs
	for (i=1; i <= 4; i++) {
		$('#gtab'+i+'_content').css('display', 'none');
		$('#gtab'+i).attr('class', 'gtab_unselected');
	}
	
	// Highlight tab
	$('#gtab'+id+'_content').css('display', 'block');
	$('#gtab'+id).attr('class', 'gtab_selected');
}

