/**
* The date picker settings object, jQuery UI is used
*/
var datePickerSettings = {
	changeMonth: true,
	changeYear: true,
	showButtonPanel: true,
	showOn: 'button',
	buttonText: 'Choose',
	buttonImage: 'templates/!shared/images/calendar/calendar.gif',
	buttonImageOnly: true
};

/**
* The date and time picker settings object, AnyTime is used
*/
var dateTimePickerSettings = {
  format: "%m/%d/%Y %H:%i",
  firstDOW: 1
};

/**
* Converts the first character of the string to upper case
* and the remaining characters to lower case
*/
function FirstCharUppercase(value)
{
	var result = '';
	if (value)
	{
		value = value.toString();
		result = value.substr(0, 1).toUpperCase() + value.substr(1).toLowerCase();
	}

	return result;
}

/**
* Limits the string length to the specified value and
* adds ellipsis at the end of the string
*/
function LimitStringLength(str, length)
{
  if (!str) { str = ''; }
  length = parseInt(length);
  
  var result = str.substr(0, length);
  if (str.length > length) { result += '...'; }
  
  return result;
}

/**
* Reloads an image
*/
function ReloadImage(imageID) {
  var source = $(imageID).attr('src');
  var position = source.indexOf('?');
  if (position >= 0) { source = source.substr(0, position); }
  
  var date = new Date();
  source += '?d=' + date.getTime();
  
  $(imageID).attr({ src: source });
}

/**
* Limits the text maximum length for the object
* In general, it should be used for textarea objects
*/
function CheckMaxLength(object, maxLength)
{
	if ($(object) && $(object).val().length > maxLength)
	{
		$(object).val($(object).val().substring(0, maxLength));
	}
}

/**
* Converts seconds to 'd h' time format or to 'd H:m:s' time format for the detailed info
*/
function GetTimeInterval(totalTimeInSeconds, showDetailedInfo)
{
	var secondsInDay = 86400;
	var secondsInHour = 3600;
	var secondsInMinute = 60;
	var timeInterval = '';
	
	var totalTimeInSeconds = parseInt(totalTimeInSeconds);
	var timeOfDayInSeconds = totalTimeInSeconds % secondsInDay;
	
	var days = parseInt(totalTimeInSeconds / secondsInDay);
	var hours = parseInt(timeOfDayInSeconds / secondsInHour);
	
	if (days > 0) timeInterval = days + 'd';
	
	if (!showDetailedInfo)
	{
		if (hours > 0)
		{
			if (days > 0) timeInterval += ' ';
			timeInterval += hours + 'h';
		}
		
		if (days > 0 || hours > 0) return timeInterval;
	}
	
	var timeOfHourInSeconds = timeOfDayInSeconds % secondsInHour;
	var minutes = parseInt(timeOfHourInSeconds / secondsInMinute);
	var seconds = parseInt(timeOfHourInSeconds % secondsInMinute);
	
	if (hours < 10) hours = '0' + hours;
	if (minutes < 10) minutes = '0' + minutes;
	if (seconds < 10) seconds = '0' + seconds;
	
	return timeInterval + ' ' + hours + ':' + minutes + ':' + seconds;
}

/**
* Initializes the default form button
*/
function InitializeDefaultButton(formID, buttonID)
{
	$(formID + ' :text,' + formID + ' select,' + formID + ' textarea,' + formID + ' :password,' +
		formID + ' :radio,' + formID + ' :checkbox,' + formID + ' :file').keypress(function (e) {
		// Processing the pressed Enter key
		if (e.which == 13)
		{
			$(buttonID).click();
			return false;
		}
		
		return true;
	});
}

/**
* Initializes the list having the array of values or pairs of identifier and value
*/
function InitializeList(data, listID)
{
	if (data && data['rows'])
	{
		$(data['rows']).each(function(index, item) {
		  var optionValue = '', optionText = '';
		  
		  if ($.isArray(item) && $(item).length == 2)
		  {
		    optionValue = $(item)[0];
        optionText = $(item)[1];
		  }
		  else if (!$.isArray(this))
		  {
		    optionValue = item;
        optionText = item;
		  }
		  
			if (optionValue && optionText)
			{
				$(listID).append(CreateElement('option', null, { value: optionValue }, optionText));
			}
		});
	}
}

