/**
 * JavaScript file
 * coffeetableboox.com
 * 
 * @author: Gonzalo Massa
 */

function init() {
	fixheight();
	setSearchActions();
	initBooxNewsForm();
}

function fixheight()
{
	var divleft;
	var divmain;
	var divright;
	var hleft;
	var hmain;
	
	divleft = document.getElementById("leftcolumn");
	divmain = document.getElementById("maincontent");
	divright =document.getElementById("rightcolumn");
	
	hleft = divleft.offsetHeight;
	hmain = divmain.offsetHeight;
	
	if( hmain < hleft )
	{
		divmain.style.height = hleft + "px";
		divright.style.height = hleft + 128 + "px";
	}
	else
	{
		divleft.style.height = hmain - 20 + "px";
		divright.style.height = hmain - 20 + 105 + "px";
	}
	
	//var busqueda = document.getElementById("searchstring");
	
	//busqueda.focus();
}

function clearMailBox()
{
	var mailbox;
	var mailbox_text;
	
	mailbox = document.getElementById("newsmail");
	
	mailbox_text = mailbox.value;
	
	if( mailbox_text == 'Subscribe: Your mail here ' || mailbox_text == 'Suscribase: Su e-mail aqui ' ) {
		mailbox.value = '';
	}
	
}

function clearTextField(input,message)
{
	var textfield;
	var textfield_text;
	var default_text;
	
	textfield = input;
	textfield_text = textfield.value;
	default_text = message;
	
	if( textfield_text == default_text ) {
		textfield.value = '';
	}
	
}

function setSearchActions() {
	
	var sbpe = document.getElementById('searchby');
	
	sbpe.onchange = function() {
		if(this.value == 'publ') {
			window.location = 'publishers.php';
		}
	}	
}

function initBooxNewsForm() {
	
	// Get DOM elements
	var a_switch = document.getElementById('booxnews_switch');
	var div_content = document.getElementById('booxnews_content');
	var form = document.getElementById('booxnews_form');
	var input_name = document.getElementById('booxnews_name');
	var input_mail = document.getElementById('booxnews_mail');
	
	// Hide booxnews content
	div_content.style.display = 'none';
	
	// Set switcher action
	a_switch.onclick = booxnews_switch_form;
	
	// Set alert cleanup for input fields
	input_name.onchange = function() {booxnews_remove_alert(this);};
	input_mail.onchange = function() {booxnews_remove_alert(this);};
	
	// Set form submit action
	form.onsubmit = function() {return booxnews_validate_form();};
	
}

function booxnews_switch_form() {
	
	var a_switch = document.getElementById('booxnews_switch');
	var div_content = document.getElementById('booxnews_content');
	var input_name = document.getElementById('booxnews_name');
	
	if(div_content.style.display != 'block') {
		div_content.style.display = 'block';
		a_switch.className = 'switch_on';
		input_name.focus();
	} else {
		div_content.style.display = 'none';
		a_switch.className = '';
	}
	
}

function booxnews_remove_alert(element) {
	element.className = 'input_text';
}

function booxnews_set_alert(element) {
	element.className = 'input_text input_error';
}

function booxnews_validate_form() {
	
	var return_value = true;
	
	var input_name = document.getElementById('booxnews_name');
	var input_mail = document.getElementById('booxnews_mail');
	
	if(input_name.value == '') {
		return_value = false;
		booxnews_set_alert(input_name);
	}
	
	if(input_mail.value == '') {
		return_value = false;
		booxnews_set_alert(input_mail);
	}
	
	if(return_value == false) {
		alert(booxnews_error_message);
	}
	
	return return_value;
	
}

window.onload = init;
