// update this when you make edits!
var version_scripts_lang = 3

var oldSaveText = '';
var schemeChangeText = "Are you sure you want to change critique type? You will lose all work on this critique!";
var schemeChangeTextNo = "You have to delete your saved critique before you can change the critique type for this story!";
var autosavetime = 1000;
function alertAutoSave(e)
{
	//alert('Autosave has been turned on. The critique will be saved every '+(autosavetime)/1000/60+' minutes.');
	if (e) {
		if (oldSaveText.indexOf('disabled') == 0)
			document.getElementById('saved').innerHTML = oldSaveText;
	} else {
		oldSaveText = document.getElementById('saved').innerHTML;
		document.getElementById('saved').innerHTML = '<b><font color=green>Autosaving is enabled&nbsp;&nbsp;</font></b>';
		setTimeout("alertAutoSave(true)", 2000);
	}		
}

function AutoSaveCritique()
{
	if (isCritTextAdded) {
		SaveCritique('Autosaving critique...', true);
	}
	isCritTextAdded = false;
	autosaveCounterId = setTimeout("AutoSaveCritique()", autosavetime);
}

/*
function getCaretTextQT(element) 
{
	if (cp && cp.text != "")
		return cp.text;
	else if (element && element.createTextRange && element.caretPos && element.caretPos.text != '') {
		return element.caretPos.text;
	} else {
		var txt = prompt("Write text that should be placed in this QuickText slot (if you write nothing the slot will empty)", "");
		return txt;
	}
	return '';
}

function encloseSelection(formName, text1, text2) {
	encloseSelection2(FormName, text1, text2);
	return;
	var v = null;
	if (currForm != null)
		v = currForm;
	else
		v = document.getElementById(formName);
	if (v == 0 || v.style.display == "none") return;
	
	if (cp && cp.text != '') {
		cp.text = text1 + cp.text + text2;
	} else if (v.createTextRange && v.caretPos && v.caretPos.text != '') {
		v.caretPos.text = text1 + v.caretPos.text + text2;
	} else {
		var txt = prompt("Insert text to be formatted with "+text1+"  "+text2, "");
		if (txt) {
			l = text1 + txt + text2;
			if (cp)
				cp.text += l;
			else
				v.value += l;
		}
	}
	currForm.focus();
}
*/
function InsertLink(formName) {
	if (cp) {
		if (cp.text != '') {
			var link = prompt("Insert URL.", "http://");
			if (link) {
				var agree=confirm('Should the link open in a new window?');
				var l;
				if (agree) 
					l = '<a href="'+link+'" target="_new">';
				else
					l = '<a href="'+link+'">';
				encloseSelection(formName, l, '</a>');
			}
		} else
			alert("You must select text first.");
	}
}


function SetBookmark(number) {
	var dud = prompt('What do you want to call the bookmark to this page?', 'Untitled');
	if (dud != null) {
//		alert(window.frames["worker"].location.href);
		window.frames["worker"].location.href="worker_addbookmark.asp?set="+number+"&title="+dud+"&url="+escape(window.location.href);
	}
}


function confirmDeleteTemplate()
{
	return confirmSubmit('Are you sure you want to delete this template?');
}

function confirmDelFI()
{
	return confirmSubmit('Are you sure you want to delete this entry?');
}

function confirmCancelQueueSub()
{
	return confirmSubmit('Are you sure you want to cancel your subscription?');
}

function confirmCancelPrivQueueSub()
{
	return confirmSubmit('Are you sure you want to cancel your subscription?\nYou will not be able to access this Queue again without an invitation from its owner!');
}

function AddFormattingToolbar(textarea, type) {
  if ((typeof(document["selection"]) == "undefined")
   && (typeof(textarea["setSelectionRange"]) == "undefined")) {
    return;
  }

  w = 180
  
  if (type == 3)
  	w = 212
  else if (type == 1)
  	w = 195
  else if (type == 5)
  	w = 124
  else if (type == 2)
  	w = 144

  var t = document.createElement("table");
  t.width = "100%";
  r = t.insertRow(-1);
  c_tb = r.insertCell(-1);
  c_tb.id = 'tb';
  c_tbr = r.insertCell(-1);
  c_tbr.id = 'tbr';
  c_tbr.width = w;
  textarea.parentNode.insertBefore(t, textarea.nextSibling);

  function addButton(id, title, fn, width) {
    var a = document.createElement("a");
    a.href = "javascript:void(0);";
    a.id = id;
    a.title = title;
    a.onclick = function() { try { fn() } catch (e) { } return false };
    a.tabIndex = 400;
    if (width) {
	    a.style.width = width + 'px';
	}
    toolbar.appendChild(a);
  }


  function encloseSelection(prefix, suffix) {
    DoEncloseSelection2(textarea, prefix, suffix);
    return;
    textarea.focus();
    var start, end, sel, scrollPos, subst;
    if (typeof(document["selection"]) != "undefined") {
      sel = document.selection.createRange().text;
    } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
      start = textarea.selectionStart;
      end = textarea.selectionEnd;
      scrollPos = textarea.scrollTop;
      sel = textarea.value.substring(start, end);
    }

    if (sel.match(/ $/)) { // exclude ending space char, if any
      sel = sel.substring(0, sel.length - 1);
      suffix = suffix + " ";
    }

    subst = prefix + sel + suffix;

    if (typeof(document["selection"]) != "undefined") {
      var range = document.selection.createRange().text = subst;
      textarea.caretPos -= suffix.length;
    } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
      textarea.value = textarea.value.substring(0, start) + subst +
                       textarea.value.substring(end);
      if (sel) {
        textarea.setSelectionRange(start + subst.length, start + subst.length);
      } else {
        textarea.setSelectionRange(start + prefix.length, start + prefix.length);
      }
      textarea.scrollTop = scrollPos;
    }
  }
  
  function InsertLink()
  {

    var txt = prompt("Insert the address of the site you want to link to", "");
    if (txt == null)
    	return;
    prefix = "[url="+txt+"]"
    suffix = "[/url]";
    textarea.focus();
    var start, end, sel, scrollPos, subst;
    if (typeof(document["selection"]) != "undefined") {
      sel = document.selection.createRange().text;
    } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
      start = textarea.selectionStart;
      end = textarea.selectionEnd;
      scrollPos = textarea.scrollTop;
      sel = textarea.value.substring(start, end);
    }

    if (sel.match(/ $/)) { // exclude ending space char, if any
      sel = sel.substring(0, sel.length - 1);
      suffix = suffix + " ";
    }

    subst = prefix + sel + suffix;

    if (typeof(document["selection"]) != "undefined") {
      var range = document.selection.createRange().text = subst;
      textarea.caretPos -= suffix.length;
    } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
      textarea.value = textarea.value.substring(0, start) + subst +
                       textarea.value.substring(end);
      if (sel) {
        textarea.setSelectionRange(start + subst.length, start + subst.length);
      } else {
        textarea.setSelectionRange(start + prefix.length, start + prefix.length);
      }
      textarea.scrollTop = scrollPos;
    }
  }


  var toolbar = document.createElement("span");
  toolbar.className = "toolbar";

  addButton("smile", ":)", function() {
    encloseSelection(":)", "");
  });

  addButton("sad", ":(", function() {
    encloseSelection(":(", "");
  });

  addButton("wide", ":D", function() {
    encloseSelection(":D", "");
  });
  
  addButton("grin", ";)", function() {
    encloseSelection(";)", "");
  });
  if (type < 5) {
	  addButton("shocked", ":|", function() {
	    encloseSelection(":|", "");
	  });

	  addButton("surprised", ":o", function() {
	    encloseSelection(":o", "");
	  });

	  addButton("cry", ":'(", function() {
	    encloseSelection(":'(", "");
	  });

	  addButton("angel", "O:)", function() {
	     encloseSelection("O:)", "");
	  });

	  addButton("ashamed", ":$", function() {
	    encloseSelection(":$", "");
	  });

	  addButton("confused", ":S", function() {
	    encloseSelection(":S", "");
	  });
	  addButton("tongue", ":p", function() {
	    encloseSelection(":p", "");
	  });
  } 
  if (type < 4) {
	  addButton("mad", ":@", function() {
	    encloseSelection(":@", "");
	  });
	  addButton("star", "(*)", function() {
	    encloseSelection("(*)", "");
	  });
	  addButton("shades", "B-)", function() {
	    encloseSelection("B-)", "");
	  });
	  addButton("dizzy", "S-|", function() {
	    encloseSelection("S-|", "");
	  });
	  addButton("ill", ":-%", function() {
	     encloseSelection(":-%", "");
	  });
	  addButton("scrooge", ";-(", function() {
	    encloseSelection(";-(", "");
	  });
  }
  
  a = c_tb;
  a.appendChild(toolbar);

  var toolbar = document.createElement("span");
  toolbar.className = "toolbar";

  if (type == 3) {
	  addButton("text", "Story snippet: [text]Example[/text]", function() {
	    encloseSelection("[text]", "[/text]");
	  });

  }
  
  addButton("bold", "Bold text: [b]Example[/b] (ctrl-b)", function() {
    encloseSelection("[b]", "[/b]");
  });

  addButton("italics", "Italics text: [i]Example[/i] (ctrl-i)", function() {
    encloseSelection("[i]", "[/i]");
  });
  addButton("underline", "Underlined text: [u]Example[/u] (ctrl-u)", function() {
    encloseSelection("[u]", "[/u]");
  });

  if (type >= 2 && type < 5) {
	  addButton("strikethrough", "Strike-through text: [y]Example[/y]", function() {
	    encloseSelection("[y]", "[/y]");
	  });

  }
  
  addButton("center", "Centered text: [c]Example[/c]", function() {
    encloseSelection("[c]", "[/c]");
  });

 if (type == 1) {
	  addButton("quote", "Quoted text: [quote]Example[/quote]", function() {
	    encloseSelection("[quote]", "[/quote]");
	  });
 }
 if (type == 1 || type == 3) {

	  addButton("link", "Insert a Link: [url=http://www.critiquecircle.com]Example[/url]", function() {
	    InsertLink();
	  });
  }
  
  addButton("red", "Coloured text: [colR]Example[/col]", function() {
     encloseSelection("[colR]", "[/col]");
  }, 8);
  addButton("green", "Coloured text: [colG]Example[/col]", function() {
     encloseSelection("[colG]", "[/col]");
  }, 8);
  addButton("blue", "Coloured text: [colB]Example[/col]", function() {
     encloseSelection("[colB]", "[/col]");
  }, 8);
  if (type < 5) {
	  addButton("grey", "Coloured text: [colGr]Example[/col]", function() {
	     encloseSelection("[colGr]", "[/col]");
	  }, 8);
	   addButton("yellow", "Coloured text: [colY]Example[/col]", function() {
	      encloseSelection("[colY]", "[/col]");
	  }, 8);
  }
  if (type != 2 && type != 5) {
	  addButton("spellcheck", "Toggle Spell-checking", function() {
	    SpellCheck(textarea.name);
	  });

  }

  if (type != 2) {
	  addButton("quicktext", "Insert a QuickText", function() {
	    InsertQuickText(toolbar, textarea.id);
	  });

  }
  
  a = c_tbr
  a.appendChild(toolbar);

}


function GetColorSettingsContents()
{
	txt = '<table border=0 cellspacing=0 cellpadding=2 class="or colbdr" style="color:#330066 ">'
	txt += '<tr><td class=smalltext>Background color</td>'
	txt += '<td class=link OnClick="SwitchLayout(\'bgcol\', -1);"><img src="images/butt_prev.gif" width=16></td>'
	txt += '<td class=link OnClick="SwitchLayout(\'bgcol\', 1);"><img src="images/butt_next.gif" width=16></td>'
	txt += '</tr>'
	
	txt += '<tr><td class=smalltext>Foreground color</td>'
	txt += '<td class=link OnClick="SwitchLayout(\'fgcol\', -1);"><img src="images/butt_prev.gif" width=16></td>'
	txt += '<td class=link OnClick="SwitchLayout(\'fgcol\', 1);"><img src="images/butt_next.gif" width=16></td>'
	txt += '</tr>'

	txt += '<tr><td class=smalltext>Font Size</td>'
	txt += '<td onClick="DispFontSize(-1);" class="curs"><img src="images/butt_prev.gif" width=16></td>'
	txt += '<td onClick="DispFontSize(1);" class="curs"><img src="images/butt_next.gif" width=16></td>'
	txt += '</tr>'
	txt += '<tr><td class=smalltext>Sidebar theme</td><td class=link OnClick="SwitchLayout(\'col\', -1);"><img src="images/butt_prev.gif" width=16></td><td class=link OnClick="SwitchLayout(\'col\', 1);"><img src="images/butt_next.gif" width=16></td></tr>'
	txt += '<tr><td class=smalltext style="padding-right:8px; ">Frontpage picture</td><td class=link OnClick="SwitchLayout(\'fp\', -1);"><img src="images/butt_prev.gif" width=16></td><td class=link OnClick="SwitchLayout(\'fp\', 1);"><img src="images/butt_next.gif" width=16></td></tr>'
	txt += '<tr><td class=smalltext style="padding-right:8px; ">Icons</td><td class=link OnClick="SwitchLayout(\'ic\', -1);"><img src="images/butt_prev.gif" width=16></td><td class=link OnClick="SwitchLayout(\'ic\', -1);"><img src="images/butt_next.gif" width=16></td></tr>'
	txt += '<tr><td class=smalltext style="padding-right:8px; " colspan=2><a href="mysettings.asp?action=togglethin">Toggle Thin Menu</a></td></td></tr>'

	txt += '</table>'
	return txt
}
