// *******************************************************

tags = new Array();

// *******************************************************

function getarraysize(thearray) {
// replacement for arrayname.length property
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "texto") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

function arraypush(thearray,value) {
// replacement for arrayname.push(value)
	thearraysize = getarraysize(thearray);
	thearray[thearraysize] = value;
}

function arraypop(thearray) {
// replacement for arrayname.pop()
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

// *******************************************************

function setmode(modevalue) {
// sets cookie for normal (0) and enhanced (1) modes
	document.cookie = "vbcodemode="+modevalue+"; path=/; expires=Wed, 1 Jan 2020 00:00:00 GMT;";
}

function normalmode(theform) {
// checks value of mode radio buttons. returns true if normal mode
	return true;
}

// *******************************************************

function closetag(theform) {
// closes last opened tag
	if (normalmode(theform))
		stat('enhanced_only');
	else
		if (tags[0]) {
			theform.mensaje.value += "[/"+ arraypop(tags) +"]";
			}
		else {
			stat('no_tags');
			}
	//theform.closecurrent.checked=false;
	theform.mensaje.focus();
}

function closeall(theform) {
// closes all open tags
	if (normalmode(theform))
		stat('enhanced_only');
	else {
		if (tags[0]) {
			while (tags[0]) {
				theform.mensaje.value += "[/"+ arraypop(tags) +"]";
				}
			theform.mensaje.value += " ";
			}
		else {
			stat('no_tags');
			}
		}
	//theform.closealltags.checked=false;
	theform.mensaje.focus();
}

// *******************************************************

function vbcode(theform,vbcode,prompttext) {
// insert [x]yyy[/x] style markup
	if ((normalmode(theform)) || (vbcode=="IMG")) {
		inserttext = prompt(tag_prompt+"\n["+vbcode+"]xxx[/"+vbcode+"]",prompttext);
		if ((inserttext != null) && (inserttext != "")) theform.mensaje.value += "["+vbcode+"]"+inserttext+"[/"+vbcode+"] ";
	}else{
		donotinsert = false;
		for (i = 0; i < tags.length; i++) {
			if (tags[i] == vbcode) donotinsert = true;
		}
		if (donotinsert) stat("already_open");
		else{
			theform.mensaje.value += "["+vbcode+"]";
			arraypush(tags,vbcode);
		}
	}
	theform.mensaje.focus();
}

// *******************************************************

function fontformat(theform,thevalue,thetype) {
// insert two-parameter markup - [x=y]zzz[/x]
	if (normalmode(theform)) {
		if (thevalue != 0) {
			inserttext = prompt(font_formatter_prompt+" "+thetype,"");
			if ((inserttext != null) && (inserttext != ""))
				theform.mensaje.value += "["+thetype+"="+thevalue+"]"+inserttext+"[/"+thetype+"] ";
			}
		}
	else {
		theform.mensaje.value += "["+thetype+"="+thevalue+"]";
		arraypush(tags,thetype);
		}
	theform.sizeselect.selectedIndex = 0;
	theform.fontselect.selectedIndex = 0;
	theform.colorselect.selectedIndex = 0;
	theform.mensaje.focus();
}

// *******************************************************

function namedlink(theform,thetype) {
// inserts named url or email link - [url=mylink]text[/url]
	linktext = prompt(link_text_prompt,"");
		var prompttext;
		if (thetype == "URL") {
			prompt_text = link_url_prompt;
			prompt_contents = "http://";
			}
		else {
			prompt_text = link_email_prompt;
			prompt_contents = "";
			}
	linkurl = prompt(prompt_text,prompt_contents);
	if ((linkurl != null) && (linkurl != "")) {
		if ((linktext != null) && (linktext != ""))
			theform.mensaje.value += "["+thetype+"="+linkurl+"]"+linktext+"[/"+thetype+"] ";
		else
			theform.mensaje.value += "["+thetype+"="+linkurl+"]"+linkurl+"[/"+thetype+"] ";
		}
	theform.mensaje.focus();
}

// *******************************************************

function sonrizas(smile){
	document.forms.noti.mensaje.value = document.forms.noti.mensaje.value +" "+smile+" ";
	document.forms.noti.mensaje.focus();
}


















function formato(form, que){
  var $tb = document.getElementById("noti");
  if (document.selection){
    var str=document.selection.createRange().text;
	
	if(str){
    	var sel=document.selection.createRange();
    	sel.text="["+que+"]"+str+"[/"+que+"]";
  	}		

  }else if (typeof $tb.selectionStart != 'undefined'){
    var $before, $after, $selection;
    $before= $tb.value.substring(0, $tb.selectionStart)
    $selection = $tb.value.substring($tb.selectionStart, $tb.selectionEnd)
    
	$after = $tb.value.substring($tb.selectionEnd, $tb.value.length)   
    $tb.value= String.concat($before, "["+que+"]", $selection, "[/"+que+"]", $after)
  }
   $tb.focus();

}



function youtube(theform,vbcode,prompttext) {
// insert [x]yyy[/x] style markup
	if ((normalmode(theform)) || (vbcode=="IMG")) {
		inserttext = prompt("Pegá el link de youtube\n"+prompttext,"http://");
		if ((inserttext != null) && (inserttext != "")){
			var principio = inserttext.lastIndexOf("v=")+2;
			var fin = inserttext.lastIndexOf("&");
			if(fin == -1){
				var fin = inserttext.lastIndexOf("");
			}
			var inserttext = inserttext.substring(principio,fin);
			theform.mensaje.value += "["+vbcode+"="+inserttext+"] ";
		}
	}else{
		donotinsert = false;
		for (i = 0; i < tags.length; i++) {
			if (tags[i] == vbcode) donotinsert = true;
		}
		if (donotinsert) stat("already_open");
		else{
			theform.mensaje.value += "["+vbcode+"]";
			arraypush(tags,vbcode);
		}
	}
	theform.mensaje.focus();
}
