var ajax = new AJAX_HANDLER();
function file_test (myid) {
	var theResult = true;
	var myfile = document.getElementById(myid).value;
	if (myfile) {
		matriz = myfile.split('.');
		last = matriz.length-1;
		type = matriz[last].toLowerCase();
	
		if (type != "jpg" && type != "gif") {
			theResult = false;
			alert('JPG or GIG only');
		}
	}
	return theResult;
}
function showHide(oID)
{
	if (get(oID).style.display == 'none') get(oID).style.display = 'block';
	else get(oID).style.display = 'none';
}
function showHideDate(o, oID)
{
	if (o.checked && o.id == 'ReleaseType_2') get(oID).style.display = '';
	else get(oID).style.display = 'none';
}
function countWords(textarea)
{
	var cant = trim(textarea.value).replace(/<[^\s]+?(.*?)>/ig, '').split(/\s+/ig);
	if (cant[0] === '') cant = 0;
	else cant = cant.length;
	if (get('word_count')) get('word_count').innerHTML = 'Word Count: ' + cant;
}
function newPublish(do_preview)
{
	// validate new publish form
	if (!valueTest('ContactName', 'CompanyName', 'Phone', 'Address1', 'Zip', 'City', 'Title', 'CName', 'CTelephone', 'FullStory')) return;
	if (!file_test('Photo')) return;
	if (!emailFilter.test(get('Email').value))
	{
		alert('Please enter a valid Email Address!');
		return;
	}
	var checks1 = document.getElementsByName('PublishCat[]');
	var checks2 = document.getElementsByName('States[]');
	var i, checkCount = 0;
	for (i=0;i<checks1.length;i++)
	{
		if (checks1[i].checked) checkCount++;
	}
	for (i=0;i<checks2.length;i++)
	{
		if (checks2[i].checked) checkCount++;
	}
	if (checkCount == 0)
	{
		alert('Please select at least one category!');
		return;
	}
	if (get('ReleaseType_2').checked && (!/\d{2}\/\d{2}\/\d{4}/.test(get('Date').value)/* || !/\d{2}:\d{2}(:\d{2})?/.test(get('Date_Time').value)*/))
	{
		alert('Please enter a valid date!');
		return;
	}
	if (do_preview)
	{
		//alert('All images must be less than 2 MB to submit properly')
		
		get('newPublishForm').action = 'index.php?module=releases&task=preview';
		get('newPublishForm').target = '_blank';
		
	}
	else {
		get('newPublishForm').action = window.location.href;
		get('sub').disabled = 1;
	}
	get('newPublishForm').submit();
}

function adjustContent()
{
	// ajustar capa principal al alto de la ventana
	var cont = get('content');
	var maxXY = getMaxXY();
	var dif = maxXY[1] - get('container').offsetHeight;
	if (dif > 0)
	{
		cont.style.height = cont.offsetHeight + dif + 'px';
	}
}

function signUp()
{
	// signup en el menu izq
	if (!emailFilter.test(get('Signup_Email').value))
	{
		alert('Please enter a valid email address');
		get('Signup_Email').focus();
		return;
	}
	get('signUpForm').submit();
}

function AJAXSearch()
{
	ajax.handler.onreadystatechange = function() {
		ajax.showStatus('Searching...');
		if (ajax.ok()) get('search_results').innerHTML = ajax.text;
	}
	ajax.request('POST', 'do=search&SearchKeywords=' + encode(get('SearchKeywords').value) + '&RSSCat=' + get('RSSCat').value, 'includes/releases_search.php');
}

function bannerRotationInit()
{
	if (top_rotation_cont > top_links.length-1) top_rotation_cont = 0;
	get('top_banner').href = top_links[top_rotation_cont];
	get('top_banner').firstChild.src = top_srcs[top_rotation_cont];
	get('top_banner').firstChild.title = top_titles[top_rotation_cont];

	if (bottom_rotation_cont > bottom_links.length-1) bottom_rotation_cont = 0;
	get('bottom_banner').href = bottom_links[bottom_rotation_cont];
	get('bottom_banner').firstChild.src = bottom_srcs[bottom_rotation_cont];
	get('bottom_banner').firstChild.title = bottom_titles[bottom_rotation_cont];
	top_rotation_cont++;
	bottom_rotation_cont++;
	setTimeout(bannerRotationInit, 5000);
}

function disableEnter()
{
	if (window.event && event.keyCode == 13) return false;
	return true;
}