// add thing from FashionJobs Stored Procs to identify files

/*////////////////////////////////////////
------------Public Functions-------------
////////////////////////////////////////*/

// function to clear a field, just pass a pointer to any
// text, password, textarea, radio button or group, checkbox or group, or multiple select menu
// this function will clear all values
function clearField(field) {
	var typeValue;
	(field.type)?typeValue = field.type.split('-')[0]:typeValue = field[0].type.split('-')[0];
	
	if (typeValue == 'checkbox' || typeValue == 'radio') {
		_clearGroup(field);
	}
	else if (typeValue == 'select') {
		_clearSelect(field);
	}	
	else {
		field.value = '';
	}
}

// function to clear a menu
function clearMenu(field) {
	_clearMenu(field);
}

// function to format a postal code
// omit the country field or use 'Canada' for Canadian Postal Code verification
// add others as required
function format_postalCode(field, country) {
	if (!country || country == 'Canada') {
		field.value = _formatCanadianPostalCode(field.value);
	}
}

// function to format a number to look like a currency value
function format_number(field, currencyFlag, numDigitsAfterDecimal, includeLeadingDigit, useParensForNegativeNumbers, groupDigits, groupDigitsSeparator) {
	/* INPUTS
			input:            				the number to be formatted
			currencyFlag:					(true/false) return the dollar sign?
			numDigitsAfterDecimal:     		(integer) the number of decimals after the digit
			includeLeadingDigit:			(true/false) start with a 0 for fractions?
			useParensForNegativeNumbers:	(true/false) brackets if negative?
			groupDigits:					(true/false) group thousands with a separator
			groupDigitsSeparator:			(char(1)) separate the thousands with this 
	
	  RETVAL - formatted number
	*/
	field.value = _formatNumber(field.value, currencyFlag, numDigitsAfterDecimal, includeLeadingDigit, useParensForNegativeNumbers, groupDigits, groupDigitsSeparator);
}

// function to disable a particular field in a form
// if its a radio or checkbox group, disable the entire thing
function field_disable(field) {
	var typeValue;
	(field.type)?typeValue = field.type.split('-')[0]:typeValue = field[0].type.split('-')[0];
	
	if (typeValue == 'radio' || typeValue == 'checkbox') {
		_disableFieldGroup(field);	
	}
	else
		field.disabled = true;
}

// function to disable a particular field in a form
// if its a radio or checkbox group, disable the entire thing
function field_enable(field) {
	var typeValue;
	(field.type)?typeValue = field.type.split('-')[0]:typeValue = field[0].type.split('-')[0];
	
	if (typeValue == 'radio' || typeValue == 'checkbox') {
		_enableFieldGroup(field);
	}
	else
		field.disabled = false;
}



/*////////////////////////////////////////
------------Private Functions-------------
////////////////////////////////////////*/

// function to clear a menu of options
function _clearMenu(field) {
	for (var i = field.options.length - 1; i >= 0; i--) {
		field.options[i] = null;
	}
	return true;
}

// function to clear a radio button group, or a checkbox group
function _clearGroup(field) {
	if (!field.length) { // its a single field
		field.checked = false;
	}
	else {
		for (var i = 0; i < field.length; i++) {
			field[i].checked = false;
		}
	}
}

// function to clear a select menu
function _clearSelect(field) {
	for (var i = 0; i < field.options.length; i++) {
		field.options[i].selected = false;
	}
}

// internal function to format a Canadian postal code
// this function doesn't validate!
function _formatCanadianPostalCode(input) {
	// capitalize any letters, ensure there's a space in between
	input = stripChars(input, ' ');
	if (input.length == 6) { // if the input isn't 6 now, do nothing since it isn't a valid postal code
		input = input.toUpperCase();
		input = input.substring(0, 3) + ' ' + input.substring(3, 6);
	}
	return input;
}

// internal function to disable a group of radio or checkboxes
function _disableFieldGroup(field) {
	if (field.length) { // there is more than one, must loop
		for (var i = 0; i < field.length; i++) {
			field[i].disabled = true;
		}
	}
	else {
		field.disabled = true;
	}
}

// internal function to disable a group of radio or checkboxes
function _enableFieldGroup(field) {
	if (field.length) { // there is more than one, must loop
		for (var i = 0; i < field.length; i++) {
			field[i].disabled = false;
		}
	}
	else {
		field.disabled = false;
	}
}

// internal function to format a number to a currency value
function _formatNumber(input, currencyFlag, numDigitsAfterDecimal, includeLeadingDigit, useParensForNegativeNumbers, groupDigits, groupDigitsSeparator) {
	input = input.toString().replace(/\$|\,/g,'');
	if (isNaN(input))
		input = "0";
		
	sign = (input == (input = Math.abs(input)));
	input = Math.floor(input * 100 + 0.50000000001);
	cents = input % 100;
	input = Math.floor(input / 100).toString();
	
	if (cents < 10)
		cents = "0" + cents;
	
	// group the digits
	if (groupDigits) {
		if (!groupDigitsSeparator) groupDigitsSeparator = ',';
		for (var i = 0; i < Math.floor((input.length - (1 + i)) / 3); i++)
			input = input.substring(0, input.length - (4 * i + 3)) + groupDigitsSeparator + input.substring(input.length - (4 * i + 3));
	}

	return (((sign)?'':((useParensForNegativeNumbers)?'(':'-')) + ((currencyFlag)?'$':'') + input + '.' + cents + ((sign)?'':((useParensForNegativeNumbers)?')':'')));
}

/*////////////////////////////////////////
------------Useful Prototypes-------------
////////////////////////////////////////*/

// prototypes month array names in as many languages as necessary
Date.prototype.monthNamesEN = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
Date.prototype.monthNamesFR = new Array("janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre");
  
Date.prototype.getMonthName = _getMonthName;

  
// function for the getMonthName() method which is prototyped to the Date object
function _getMonthName(language) {
	return eval('this.monthNames' + language + '[this.getMonth()]');
}
