//
// FORMATTING PANEL FOR DISCUS 'ADD A MESSAGE' box
// Copyright (c) 2001, DiscusWare, LLC, all rights reserved
// This code is distributed under the same terms as Discus itself
//

function panelclose () {
	if (document.userpass) {
		if (document.userpass.format_b.value == 1) {
			insertText(document.userpass.message, '}');
		}
		if (document.userpass.format_i.value == 1) {
			insertText(document.userpass.message, '}');
		}
		if (document.userpass.format_u.value == 1) {
			insertText(document.userpass.message, '}');
		}
		if (document.userpass.format_c.value == 1) {
      insertText(document.userpass.message, '}');
    }
    if (document.userpass.format_indent.value == 1) {
      insertText(document.userpass.message, '}');
    }
    if (document.userpass.format_table.value == 1) {
    insertText(document.userpass.message, '}');
    }
    if (document.userpass.format_list.value == 1) {
    insertText(document.userpass.message, '}');
    }
//		if (document.userpass.format_link.value == 1) {
//			insertText(document.userpass.message, '}');
//		}
		if (document.userpass.format_clr.value == 1) {
			insertText(document.userpass.message, '}');
		}
		if (document.userpass.format_font.value == 1) {
			insertText(document.userpass.message, '}');
		}
		if (document.userpass.format_size.value == 1) {
			insertText(document.userpass.message, '}');
		}	
		if (document.userpass.format_alignleft.value == 1) {
      insertText(document.userpass.message, '}');
    }
    if (document.userpass.format_alignright.value == 1) {
       insertText(document.userpass.message, '}');
		}
	}
	return true;
}

function storeCursorPosition (elem) {
 if(document.selection) { 
	if (elem.createTextRange) {
		elem.caretPos = document.selection.createRange().duplicate();
	}
  } else if(elem.selectionStart) { 
        // Go the Gecko way  ?? what to do here? 
 }
}

/* fixed FF/Gecko : 080908 Frank Damgaard */
function insertText (elem, text) {
  elem.focus();
  if(document.selection) { 
        // Go the IE way 
	//elem.focus();
	if (elem.createTextRange && elem.caretPos) {
		var caretPos = elem.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else {
   	  	/* else just add last */
		elem.value = elem.value  + text ;
	}
  } else if(elem.selectionStart) { 
        // Go the Gecko way 
	var start = elem.selectionStart; 
	var eend   = elem.selectionEnd; 

	/* Remember obj is a textarea or input field */ 
	elem.value = elem.value.substr(0, start) 
	    + text  
	    + elem.value.substr(eend, elem.value.length); 
        //elem.focus(); 
        elem.setSelectionRange(start + text.length, start + text.length); 

  } else { 
	// Fallback for any other browser 
     	/* else just add last */
	elem.value = elem.value  + text ;
  } 
}

function bolder () {
	if (document.userpass) {
		if (document.userpass.format_b.value == 1) {
			document.button_b.src = img_bold_up.src;
			insertText(document.userpass.message, '}');
			document.userpass.format_b.value = "0";
		} else {
			document.button_b.src = img_bold_dn.src;
			insertText(document.userpass.message, '\\b{');
			document.userpass.format_b.value = "1";
		}
		document.userpass.message.focus();
	}
	return false;
}
// fix discus links with triax 090128
function fix_link_triax(txt) {
 var l=txt;
 if (l != null) { 
    var tstr= l.replace(/{/g,"\\{") ;
    l= tstr.replace(/}/g,"\\}") ;
 }
 return l;
}

// common part to add links - 080311
function link_common(text, defurl,cmd) {
  if (document.userpass) {
    var l = prompt(text, defurl); 
    if (l != null) { 
      var p = prompt("Giv nu linket et beskrivende navn", ""); 
      if (p != null) { 
	var tstr= l.replace(/{/g,"\\{") ;
	var lc= tstr.replace(/}/g,"\\}") ;
	tstr= p.replace(/{/g,"\\{") ;
	var pc= tstr.replace(/}/g,"\\}") ;
        insertText(document.userpass.message, '\\'+cmd+'{' + lc + ',\\b{' + pc + '}}'); 
      }
    } 
    document.userpass.message.focus(); 
  }
  return false; 
}

// two pass link added 08.03.11
function linky_click (text) { 
  link_common(text,"","link");
  return false; 
} 

// two pass link added 08.03.11
function wwwlinky_click (text) {
  link_common(text,"http://","newurl");
  return false; 
}

function italic () {
	if (document.userpass) {
		if (document.userpass.format_i.value == 1) {
			document.button_i.src = img_ital_up.src;
			insertText(document.userpass.message, '}');
			document.userpass.format_i.value = "0";
		} else {
			document.button_i.src = img_ital_dn.src;
			insertText(document.userpass.message, '\\i{');
			document.userpass.format_i.value = "1";
		}
		document.userpass.message.focus();
	}
	return false;
}

function underl () {
	if (document.userpass) {
		if (document.userpass.format_u.value == 1) {
			document.button_u.src = img_undl_up.src;
			insertText(document.userpass.message, '}');
			document.userpass.format_u.value = "0";
		} else {
			document.button_u.src = img_undl_dn.src;
			insertText(document.userpass.message, '\\u{');
			document.userpass.format_u.value = "1";
		}
		document.userpass.message.focus();
	}
	return false;
}

function center () {
        if (document.userpass) {
                if (document.userpass.format_c.value == 1) {
                        document.button_c.src = img_cent_up.src;
                        insertText(document.userpass.message, '}');
                        document.userpass.format_c.value = "0";
                } else {
                        document.button_c.src = img_cent_dn.src;
                        insertText(document.userpass.message, '\\c{');
                        document.userpass.format_c.value = "1";
                }
                document.userpass.message.focus();
        }
        return false;
}

function indent () {
        if (document.userpass) {
                if (document.userpass.format_indent.value == 1) {
                        document.button_indent.src = img_indent_up.src;
                        insertText(document.userpass.message, '}');
                        document.userpass.format_indent.value = "0";
                } else {
                        document.button_indent.src = img_indent_dn.src;
                        insertText(document.userpass.message, '\\indent{');
                        document.userpass.format_indent.value = "1";
                }
                document.userpass.message.focus();
        }
        return false;
}

function table () {
        if (document.userpass) {
                if (document.userpass.format_table.value == 1) {
                        document.button_table.src = img_table_up.src;
                        insertText(document.userpass.message, '}');
                        document.userpass.format_table.value = "0";
                } else {
                        document.button_table.src = img_table_dn.src;
                        insertText(document.userpass.message, '\\table{');
                        document.userpass.format_table.value = "1";
                }
                document.userpass.message.focus();
        }
        return false;
}

function list () {
        if (document.userpass) {
                if (document.userpass.format_list.value == 1) {
                        document.button_list.src = img_list_up.src;
                        insertText(document.userpass.message, '}');
                        document.userpass.format_list.value = "0";
                } else {
                        document.button_list.src = img_list_dn.src;
                        insertText(document.userpass.message, '\\list{');
                        document.userpass.format_list.value = "1";
                }
                document.userpass.message.focus();
        }
        return false;
}

function smiley () {
	//alert('smiley');
	if (document.userpass.inserter.value != "") {
		if (typeof document.userpass.tagname != 'undefined') {
			insertText(document.userpass.message, "\\" + document.userpass.tagname.value + "{" + document.userpass.inserter.value + "}");
		} else {
			insertText(document.userpass.message, "\\clipart{" + document.userpass.inserter.value + "}");
		}
		document.userpass.inserter.value = "";
	}
	document.userpass.message.focus();
}

function colorsel () {
	if (document.userpass) {
		if (document.userpass.color) {
			var z = document.userpass.color[document.userpass.color.selectedIndex].value;
			if (z == "") {
				insertText(document.userpass.message, '}');
				document.userpass.format_clr.value = 0;
			} else {
				if (document.userpass.format_clr.value == 1) {
					insertText(document.userpass.message, "}\\" + z + "{");
				} else {
					document.userpass.format_clr.value = 1;
					insertText(document.userpass.message, "\\" + z + "{");
				}
			}
		}
		document.userpass.message.focus();
	}
}

function fontsel () {
	if (document.userpass) {
		if (document.userpass.font) {
			var z = document.userpass.font[document.userpass.font.selectedIndex].value;
			if (z == "") {
				insertText(document.userpass.message, '}');
				document.userpass.format_font.value = 0;
			} else {
				if (document.userpass.format_font.value == 1) {
					insertText(document.userpass.message, "}\\font{" + z + ",");
				} else {
					insertText(document.userpass.message, "\\font{" + z + ",");
					document.userpass.format_font.value = 1;
				}
			}
		}
		document.userpass.message.focus();
	}
}

function sizesel () {
	if (document.userpass) {
		if (document.userpass.size) {
			var z = document.userpass.size[document.userpass.size.selectedIndex].value;
			if (z == "") {
				insertText(document.userpass.message, '}');
				document.userpass.format_size.value = "0";
			} else {
				if (document.userpass.format_size.value == 1) {
					insertText(document.userpass.message, "}\\" + z + "{");
				} else {
					insertText(document.userpass.message, "\\" + z + "{");
					document.userpass.format_size.value = 1;
				}
			}
		}
		document.userpass.message.focus();
	}
}
function alignleft () {
        if (document.userpass) {
                if (document.userpass.format_alignleft.value == 1) {
                        document.button_alignleft.src = img_alignleft_up.src;
                        insertText(document.userpass.message, '}');
                        document.userpass.format_alignleft.value = "0";
                } else {
                        document.button_alignleft.src = img_alignleft_dn.src;
                        insertText(document.userpass.message, '\\alignleft{');
                        document.userpass.format_alignleft.value = "1";
                }
                document.userpass.message.focus();
        }
        return false;
}

function alignright () {
        if (document.userpass) {
                if (document.userpass.format_alignright.value == 1) {
                        document.button_alignright.src = img_alignright_up.src;
                        insertText(document.userpass.message, '}');
                        document.userpass.format_alignright.value = "0";
                } else {
                        document.button_alignright.src = img_alignright_dn.src;
                        insertText(document.userpass.message, '\\alignright{');
                        document.userpass.format_alignright.value = "1";
                }
                document.userpass.message.focus();
        }
        return false;
}

function attach (text) {
  if (document.userpass) {
// if (document.userpass.format_attach.value == 1) {
//   document.button_attach.src = img_attach.src;
// insertText(document.userpass.message, '}');
// document.userpass.format_attach.value = "";
// } else {
    var l = prompt(text, "http://");
    if (l != null) {
// document.button_attach.src = img_attach.src;
    insertText(document.userpass.message, '\attach{' + l + '}');
// document.userpass.format_attach.value = "1";
// }
    }
    document.userpass.message.focus();
  }
  return false;
} 
