document.setDatabaseContainerWidth = function(container, controlbar, setOnResize) {
	if(container = $(container)) {
		if(controlbar = $(controlbar)) {
			container.setStyle('width', Math.max(window.getSize().x - (Browser.Engine.webkit ? 367 : 352), controlbar.getSize().x+15));
		} else {
			container.setStyle('width', window.getSize().x - (Browser.Engine.webkit ? 367 : 352));
		}
		if(Browser.Engine.trident5 && container.scrollHeight > container.clientHeight) {
			container.setStyle('height', container.scrollHeight+17);
		}
		if(setOnResize) {
			document.databaseResizeFunction = function() {document.setDatabaseContainerWidth(container.id, controlbar);}
			window.addEvent('resize', document.databaseResizeFunction);
		}
	} else if(!setOnResize) {
		window.removeEvent('resize', document.databaseResizeFunction);
	}
}

function createExpirationDate(dateFieldID, monthFieldID, yearFieldID) {
	var dateField = document.getElementById(dateFieldID);
	var monthField = document.getElementById(monthFieldID);
	var yearField = document.getElementById(yearFieldID);

	if (dateField && monthField && yearField) {
		if (monthField.value != "" || yearField.value != "") {
			dateField.value = monthField.value + "/" + yearField.value;
		}
		else {
			dateField.value = "";
		}
		
	}

}

document.togglePrettyCheckbox = function(el) {
	if(el = $(el)) {
		var input = el.getPrevious();
		if(input) {
			input.checked = !input.checked;
			el.select('prettyCheckbox', input.checked);
		}
	}
}

document.toggleAllCheckboxes = function(container, on) {
	if(container = $(container)) container.getElements('input[type=checkbox]').each(function(el) {el.checked = on;if(el.getNext() && el.getNext()) {el.getNext().select('prettyCheckbox', on);}});
}

document.loginError = function(markUsername, markPassword, errorMessage) {
	var usernameLabelEl = $('loginFormUsernameLabel');
	var passwordLabelEl = $('loginFormPasswordLabel');
	var errorMessageEl = $('loginFormErrorMessage');
	
	if(usernameLabelEl && passwordLabelEl && errorMessageEl) {
		if(markUsername) {
			usernameLabelEl.className = 'error';
		} else {
			usernameLabelEl.className = '';
		}
		if(markPassword) {
			passwordLabelEl.className = 'error';
		} else {
			passwordLabelEl.className = '';
		}
		errorMessageEl.innerHTML = errorMessage;
	}
}

document.loginFinish = function(r) {
	if(r) {
		eval(r);
	} else {
		jt.ajax.processCurrentState();
	}
}

document.downloadSelectedItems = function() {
	var queryString = '';
	$$('div.Selcheckbox').each(function(item) {
		queryString += '&id[]='+item.get('which');
	});
	if(queryString != '') {
		location.href= 'call.getDownload?'+queryString;
	}
}

document.showTooltip = function(e, title, data) {
	e = new Event(e);
	var tooltipContainer = $('tooltipSlipContainer'), tooltipContent = $('tooltipSlipContent');
	if(tooltipContainer && tooltipContent && e) {
		var newHtml = '<div class="slipTitle" style="cursor: default;">'+title+'</div>';
		
		newHtml += '<div class="tooltipContent"><table border="0" cellpadding="0" cellspacing="0">';
		if(data == 'digital_sound') {
			newHtml += '<tr><td valign="top" align="right" class="tooltipLabel">T&nbsp;=&nbsp;</td><td valign="top"> DTS</td></tr>';
			newHtml += '<tr><td valign="top" align="right" class="tooltipLabel">D&nbsp;=&nbsp;</td><td valign="top"> SR&bull;D</td></tr>';
			newHtml += '<tr><td valign="top" align="right" class="tooltipLabel">DX&nbsp;=&nbsp;</td><td valign="top"> SR&bull;D EX</td></tr>';
			newHtml += '<tr><td valign="top" align="right" class="tooltipLabel">S&nbsp;=&nbsp;</td><td valign="top"> SDDS</td></tr>';
			newHtml += '<tr><td valign="top" align="right" class="tooltipLabel">S8&nbsp;=&nbsp;</td><td valign="top"> SDDS 8 Channel</td></tr>';
		} else if(data == 'gross_to_date') {
			newHtml += '<tr><td valign="top" align="right" class="tooltipLabel"></td><td valign="top">$0.0 indicates film has grossed less than $50,000.</td></tr>';
		} else if(data) {
			data.each(function(item) {
				newHtml += '<tr><td valign="top" align="right" class="tooltipLabel">'+item.label+'</td><td valign="top">'+item.content+'</td></tr>';
			});
		}
		newHtml += '</table></div>';
		
		tooltipContent.innerHTML = newHtml;
		tooltipContainer.setStyle('visibility', 'hidden').show();
		if(tooltipContent.getSize().x > 400) {
			tooltipContent.setStyle('width', 400);
		}
		if(e.page.x+55+tooltipContainer.getSize().x < window.getSize().x) {
			tooltipContainer.setStyles({left: e.page.x+40, top: e.page.y-40, visibility: 'visible'});
		} else {
			tooltipContainer.setStyles({left: e.page.x-40-tooltipContainer.getSize().x, top: e.page.y-40, visibility: 'visible'});
		}
	}
}
document.hideTooltip = function() {
	var tooltipContainer = $('tooltipSlipContainer'), tooltipContent = $('tooltipSlipContent');
	if(tooltipContainer && tooltipContent) {
		tooltipContainer.hide();
		tooltipContent.setStyle('width', 'auto');
	}
}

document.orderSwitchType = function(which) {
	var type = which.value;
	var theForm = document.getElementById('orderForm1');

	if(type == 'one_individual') {
		theForm.num_one_feb.disabled = false;
		theForm.num_one_may.disabled = false;
		theForm.num_one_aug.disabled = false;
		theForm.num_one_nov.disabled = false;
		
		theForm.num_all_4.disabled = true; theForm.num_all_4.value = '';
	} else if(type == 'all_individual') {
		theForm.num_one_feb.disabled = true; theForm.num_one_feb.value = '';
		theForm.num_one_may.disabled = true; theForm.num_one_may.value = '';
		theForm.num_one_aug.disabled = true; theForm.num_one_aug.value = '';
		theForm.num_one_nov.disabled = true; theForm.num_one_nov.value = '';
		
		theForm.num_all_4.disabled = false;
	}
}

document.toggleHomeTab = function(cur) {
	var curContent;
	if((cur = $(cur)) && (curContent = $(cur.id+'Content'))) {
		['newsHomeTab','changesHomeTab','ratingsHomeTab'].each(function(el) {
			var elContent;
			if((el = $(el)) && el != cur && (elContent = $(el.id+'Content'))) {
				el.removeClass(el.id+'On');
				elContent.hide();
			}
		});
		
		cur.addClass(cur.id+'On');
		curContent.show();
	}
}
