//Variables for controlling opening and closing tags (function tag)
var b = 2;
var i = 2;
var u = 2;
var q = 2;
var c = 2;
var url = 2;
var img = 2;

//Function for creating non-font tags

function tag(v, tagadd, newbut, tagclose, oldbut, name) {
  if (eval(v)%2 == 0) {
      eval("document.getElementById('"+name+"').value = newbut;");
      var post = document.getElementById('bbForm').post.value;
      document.getElementById('bbForm').post.value = post + tagadd;
      document.getElementById('bbForm').post.focus();
  } else {
      eval("document.getElementById('"+name+"').value = oldbut;");
      var post = document.getElementById('bbForm').post.value;
      document.getElementById('bbForm').post.value = post + tagclose;
      document.getElementById('bbForm').post.focus();
  }
  eval(v+"++;");
}


//Function for adding font color and size tags

function font(bbopen, bbclose) {
  var post = document.getElementById('bbForm').post;
  post.value += bbopen + bbclose;
  post.focus();
  return;
}

//Function for adding smilies

function smilie (smilie) {
  var post = document.getElementById('bbForm').post;
  post.value += smilie;
  post.focus();
  return;
}


//Helpbox messages
bold_help = "Bold text: [b]text[/b]";
italic_help = "Italic text: [i]text[/i]";
underline_help = "Underline text: [u]text[/u]";
quote_help = "Quote text: [quote]text[/quote] or [quote=name]text[/quote]";
code_help = "Code display: [code]code[/code]";
img_help = "Insert image: [img]http://image_url[/img]";
url_help = "Insert URL: [url]http://url[/url] or [url=http://url]URL text[/url]";
fontcolor_help = "Font color: [color=red]text[/color]  Tip: you can also use color=#FF0000";
fontsize_help = "Font size: [size=50%]small text[/size]";

//Function for displaying help information

// Shows the help messages in the helpline window
function helpline(help) {
        var helpbox = document.getElementById('bbForm').helpbox;
        helpbox.value = eval(help + "_help");
}

//Function to confirm reset
function confirm_reset () {
        if(confirm("If you continue you will loose everything you have entered so far. \n \n" +
                "Click OK to proceed and start again.  \n \n Alternatively click cancel to continue " +
                "working on your post.")) {
                        return true;
                                }
                else {
                        return false;
                                }
}
//Check the form submission for errors

function checkForm() {
  var subject = document.getElementById('bbForm').subject;
  var post = document.getElementById('bbForm').post;
  //Check to make sure post lengths are sensible
  if (subject.value.length < 2 && post.value.length < 2) {
    alert("This is a short post!" + " \n \n " +
                    "We require that each post (and subject) \n" +
                    "be at least 2 characters long. \n \n" +
                    "Go back and try again.");
    return false;
  } else { 
  	if (subject.value.length < 2) {
    	alert("Subjects must be at least 2 characters long.");
      return false;
    } else { if (post.value.length < 2) {
      alert("Posts must be at least 2 characters long.");
      return false;
    } else {
      return true;
    }
   }
  }
}
				

				
function checkDomain(inID, str){
	var myRegexp = /[^0-9a-zA-Z]/;
	$(inID).empty();
	if(str.length == 0){
		$(inID).append('Please enter a name');
	 	return false;
	} else if(str.length < 2){
		$(inID).append('<span class="err">Your name is too short.</span>');
	 	return false;
	} else if(myRegexp.test(str)){
		$(inID).append('<span class="err">Your website name may only contain letters and numbers.</span>');
	} else {
		$(inID).load('signup.html?checkDomain='+encodeURI(str));
		return false;
	}
}



function checkUser(inID, str){
	var myRegexp = /[^0-9a-zA-Z]/;
	$(inID).empty();
	if(str.length <= 0){
		$(inID).append('Please enter a username');
	 	return false;
	} else if(str.length < 6){
		$(inID).append('<span class="err">Your username is too short.</span>');
	 	return false;
	} else if(myRegexp.test(str)){
		$(inID).append('<span class="err">Your User Name may only contain letters and numbers.</span>');
	} else {
		$(inID).load('signup.html?checkuser='+encodeURI(str));
		return false;
	}
}

function checkPass(inID, str1, str2){
	$(inID).empty();
	if(str1.length == 0 && str2.length == 0){
		$(inID).append('Please enter a password');
	} else if(str1.length < 6){
		$(inID).append('<span class="err">Your password is too short</span>');
	} else if(str1 == str2){
		$(inID).append('<span class="aok">Your password is ok.</span>');
	} else {
		$(inID).append('<span class="err">Your passwords do not match.</span>');
	}				
}
				
				
				
				
function echeck(inID, str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	$(inID).empty();
	var err = '<span class="err">Your email address is invalid</span>';
	
	if(lstr == 0){
		 $(inID).append('Please enter your email address.');
	} else if (str.indexOf(at)==-1){
	   $(inID).append(err);
	} else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   $(inID).append(err);
	} else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    $(inID).append(err);
	} else if (str.indexOf(at,(lat+1))!=-1){
	    $(inID).append(err);
	} else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    $(inID).append(err);
	} else if (str.indexOf(dot,(lat+2))==-1){
	    $(inID).append(err);
	} else  if (str.indexOf(" ")!=-1){
	    $(inID).append(err);
	} else {
		$(inID).append('<span class="aok">Your email address is OK</span>');		
	}
}