var db_min_word_len = 3;

// Validates search input
function validateSearch(field)
{
	var searchField = document.getElementById(field);
	var searchString = trim(searchField.value);
	var msgEmpty = "Por favor ingrese una palabra en el campo de búsqueda";
	var msgTooShort = "La palabra de búsqueda debe contener al menos " + db_min_word_len + " caracteres";
	var msgSpaces = "Debe haber alguna palabra de al menos " + db_min_word_len + " caracteres en su búsqueda";

	if ((searchString.length == 0) || (searchString == "Búsqueda"))
	{
		alert(msgEmpty);
		return false;
	}
	else
	{
		string_array = searchString.split(" ");

		if (string_array.length > 1)
		{
			for (var i in string_array)
			{
				if (string_array[i].length >= db_min_word_len)
					return true;
			}

			alert(msgSpaces);
			return false;
		}
		else
		{
			if (searchString.length >= db_min_word_len)
				return true;
			else
			{
				alert(msgTooShort);
				return false;
			}
		}
	}
}

// Trims a string
function trim(inputString) {
   inputString = inputString.toString();
   return inputString.replace(/^\s*|\s*$/g,"");
}

// Recover ID of replying comment
function replyTo(postID)
{
	var reply_field = document.getElementById("reply_to");
	
	if (reply_field.value == 'NULL')
	{
		var agree = confirm('Para poder responder a este comentario, es necesario estar registrado como usuario.\n¿Desea registrase ahora?');
		
		if (agree)
		{
			return window.open('/?module=users&format=html&op=login','_self');
		}
		return false;
	}

	var subject_field = document.getElementById("subject");
	var subject_text = document.getElementById("subject_" + postID).innerHTML;

	// Updates 'real' reply field value
	reply_field.value = postID;
	
	// Updates subject text field
	subject_field.value = "RE: " + subject_text;
	
	// Set cursor focus on comment field
	document.post_comment_form.comment.focus();
	
	return true;
}

// Inserts an emoticon into the given field
function insertEmoticon(code,field_id) {
	code += " ";
	msgField = document.getElementById(field_id);
	msgField.focus();  
	if(document.selection){
		//IE support
		sel=document.selection.createRange();
		sel.text=code;
		sel.select();
	}else if(msgField.selectionStart>=0){
		//Mozilla/Firefox/Netscape 7+ support
		var startPos=msgField.selectionStart;
		var endPos=msgField.selectionEnd;
		msgField.setSelectionRange(endPos,endPos);
		msgField.value = msgField.value.substring(0,startPos) + code + msgField.value.substring(endPos,msgField.value.length);
		endPos+=code.length;
    msgField.setSelectionRange(endPos,endPos);
	}else{
		msgField.value += code;
	}
}

// Counts the number of chars in "el" and updates "id_txt" field
function countchars(el,MAX,id_txt){
	var str = new String(el.value);
	count = MAX - str.length;
	if(count>0){
		document.getElementById(id_txt).innerHTML=count;
	}else{
		alert("Has alcanzado el límite de caracteres para este campo");
		el.value=el.value.substr(0,MAX);
		document.getElementById(id_txt).innerHTML=0;
		el.blur();
	}
}
	
// Expand/collapse comment replies
function showChilds(index) {
	var el = document.getElementById('ch_' + index);
	var ln = document.getElementById('ex_' + index);
	if (el.style.display == 'none')
	{
		el.style.display = 'block';
		ln.className = 'showRepliesCollapse_link';
	}
	else
	{
		el.style.display = 'none';
		ln.className = 'showReplies_link';
	}
}

function confirmAction(msg) {
var agree = confirm(msg);
if (agree)
	return true ;
else
	return false ;
}