/**
 * @author Carlos Peņa - carlos@7dos.com
 * @copyright 2007 Secretos2.com
 * @version 1.0
 */

function tabs_switch(to) {			
	var tabs = document.getElementById('tabs').getElementsByTagName('li');
	for(i=0; i<3; i++) {
		tabs[i].className = tabs[i].className.replace('_active', '_inactive');
	}
	document.getElementById('tabs_id_'+to).className = document.getElementById('tabs_id_'+to).className.replace('_inactive', '_active');
	
	var cont = document.getElementById('tops_content').getElementsByTagName('div');
	for(i=0; i<3; i++) {
		cont[i].className = cont[i].className.replace('_active', '_inactive');
	}
	document.getElementById('tops_content_'+to).className = document.getElementById('tops_content_'+to).className.replace('_inactive', '_active');
}

function check_form() {
	
	if(document.getElementById('titulo').value == '') {
		alert('Por favor escriba un titulo para su secreto');
		document.getElementById('titulo').focus();
		return false;
	} else if(document.getElementById('captcha').value == '') {
		alert('Tienes que hacer la suma para comprobar que no eres un bot');
		document.getElementById('captcha').focus();
		return false;
	} else if(isNaN(document.getElementById('captcha').value) == true) {
		alert('La suma de dos numeros no puede dar una letra');
		document.getElementById('captcha').focus();
		return false;
	} else if(document.getElementById('comment').value == '') {
		alert('Quieres enviar un secreto sin escribirlo? Buen intento..');
		document.getElementById('comment').focus();
		return false;
	} else {
		return true;
	}
	
}

// fieldId, remainingId, maxchars
function checkFieldLength(fi, ri, mc) {
	fi = document.getElementById(fi);
	var len = fi.value.length;
	if (len > mc) {
		fi.value = fi.value.substring(0,mc);
		len = mc;
	}
	document.getElementById(ri).innerHTML = mc - len;
}

$(function() {
	$("#form_secret #comment").keyup(function() {
		checkFieldLength("comment", "remaining", 1500)
	}).keydown(function() {
		checkFieldLength("comment", "remaining", 1500)
	}).mouseup(	function() {
		checkFieldLength("comment", "remaining", 1500)
	});
	
	$("#commentform #comment").keyup(function() {
		checkFieldLength("comment", "remaining", 500)
	}).keydown(function() {
		checkFieldLength("comment", "remaining", 500)
	}).mouseup(	function() {
		checkFieldLength("comment", "remaining", 500)
	});
	
	$(".report_link").click(function() {
		This = $(this);
		if (!This.hasClass("active")) {
			Id = This.attr("id");
			$.post("/report.php", {id: Id}, function() {
				This.addClass("active");
				This.text("Reportado!");
			});
		}
		
		return false;
	});
	
	$("#form_selector a[class!='active']").click(function() {
		Id = $(this).attr("id");
		
		$("div.form:visible").hide(300); // Hide visible form
		$("#form_"+Id).show(300);
		
		$("#form_selector a").removeClass("active");
		$(this).addClass("active");
		
		$("#form_type").val(Id);
		
		return false;
	});
	
	// Create a Poll custom script			
	$("#add_option").click(function() {
		var pollCount = $("#form_poll_answers p").length+1;
		var pollInput = '<p class="answer_'+pollCount+'"><label><small>'+pollCount+'.</small></label> <input type="text" size="50" maxlength="200" name="poll_answers[]" /><input type="button" value="Eliminar" id="remove_'+pollCount+'" class="button_grey poll_remove" /></p>';
		
		if (pollCount > 10) {
			$("#add_option").hide();
			return false;
		}
		
		$("#form_poll_answers").append(pollInput);
						
		return false;
	});
	
	$("input.poll_remove").live("click", function() {
		$(this).parent().remove();
		$("#add_option").show();
		
		// Re-order answer numbers
		$("#form_poll_answers p label small").each(function(i) {					
			$(this).text((i+1)+".");
		});
		
		return false;
	});
	
	// Validate Form when Submitted
	$("#submit_form").submit(function() {
		Form = $(this);
		formType = $("#form_type").val();
		Valid = true;
		msg = [];
		
		if (formType == "secret") {
			// Validate Secret form
			if ($("#titulo").val() == '') {
				msg.push("Escribe un titulo");
				Valid = false;
			}
			if ($("#age").val() == 'default') {
				msg.push("Selecciona tu edad");
				Valid = false;
			}
			if ($("#sex").val() == 'default') {
				msg.push("Selecciona tu sexo");
				Valid = false;
			}
			if ($("#comment").val() == '') {
				msg.push("Escribe un secreto...");
				Valid = false;
			}
		} else if (formType == "poll") {
			// Validate Poll form
			if ($("#poll_titulo").val() == '') {
				msg.push("Escribe un titulo");
				Valid = false;
			}
			if ($("#poll_age").val() == 'default') {
				msg.push("Selecciona tu edad");
				Valid = false;
			}
			if ($("#poll_sex").val() == 'default') {
				msg.push("Selecciona tu sexo");
				Valid = false;
			}
			
			totalAnswers = $("#form_poll_answers p input[type=text]").length;
			if (totalAnswers < 2) {
				msg.push("Necesitas minimo 2 opciones/respuestas");
				Valid = false;
			} else {
				// Count the number of empty answers to see if
				// less than 2 "real" answers are being submitted
				var emptyAnswers = 0;
				$("#form_poll_answers p input[type=text]").each(function() {
					if ($(this).val() == "") {
						emptyAnswers = emptyAnswers + 1;
					}
				});
				
				if ((totalAnswers - emptyAnswers) < 2) {
					msg.push("Necesitas minimo 2 opciones/respuestas");
					Valid = false;
				}
			}
		}
		
		if (Valid) {
			return true;
		} else {
			// Form not valid
			// Show error messages
			$("ul.error_list").remove(); // Delete old error list, if any
			error = $("<ul>");
			error.addClass("error_list").hide();
			error.append("<li class='title'>Verifica los siguientes errores:</li>");
			
			$.each(msg, function(i, v) {
				if (v != "") {
					error.append("<li>"+v+"</li>");
				}
			});
			
			$("#content").prepend(error);
			error.show(300);
			
			return false;
		}
	});
});
