// 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();
	}
}

// Validate comments posting
function validateCommentFields() {
	var theForm=document.forms.post_comment_form;
	var emailPattern=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var msg=new Array;
	msg.push("Antes de continuar, verifique lo siguiente:\n");
	// Evaluate provided email address
	if(theForm.user_name && theForm.user_name.value==""){
		msg.push("* Escriba su nombre");
	}
	if(theForm.user_email && theForm.user_email.value==""){
		msg.push("* Escriba su correo electrónico");
	} else if (theForm.user_email && !emailPattern.test(theForm.user_email.value)){
		msg.push("* Verifique la dirección de correo");
	}
	if(theForm.subject.value==""){
		msg.push("* Escriba el asunto de su comentario");
	}
	if(theForm.comment.value==""){
		msg.push("* Escriba su comentario");
	}
	if(msg.length>1){
		alert(msg.join("\n"));
		return false;
	}else{
		return true;
	}
}

// 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;
	reply_field.value = postID;
	subject_field.value = "RE: " + subject_text;
	document.post_comment_form.comment.focus();
	return true;
}

// 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 = 'show_replies_collapse';
	} else {
		el.style.display = 'none';
		ln.className = 'show_replies_expand';
	}
}
