// Used for limiting the amount of input in the flash forum
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

// Used for collapsing menus and lists
// uses the name="" property
function toggleVisible(id){
    var e = document.getElementById(id);
    if (e){
        if (e.style.display == "none"){
            e.style.display = 'block'; 
        } else {
            e.style.display = 'none';
        }
    }
}

// Used for toggling icons when collapsing menus and lists
// uses the name="" property

function toggleIcon(name){
    var e = document.getElementById(name);
    if (/closed.png/.test(e.src)){
        e.setAttribute('src', '/files/img/open.png');
        e.setAttribute('alt', '-');
    } else {
        e.setAttribute('src', '/files/img/closed.png');
        e.setAttribute('alt', '+')
    }
        
}

// Used for asking if you want to leave the page while editing an article

function leave(){
        event.returnValue = "Dude!";
}

function setSubmit(){
    var submit = True;
}


//Add more choice fields dynamically.
function addChoiceFields(){
    var field_area = document.getElementById("extra_choice_fields");
    field_counter = document.getElementById("poll_form").elements.length - 6;
    var label = document.createElement('label');
        label.setAttribute("for", 'id_choice' + field_counter);
        field_area.appendChild(label);
    var input = document.createElement('input');
        input.id = 'id_choice' + field_counter;
        input.name = 'choice' + field_counter;
        input.type = 'text';
        field_area.appendChild(input);
    var div = document.createElement('div');
        div.setAttribute("class", "form_spacer");
        field_area.appendChild(div);
}

// Used for controling tabs in member_edit
// uses the name="" property
function showTab(name){
    var e = document.getElementsByName(name);
    for (var i=0;i<e.length;i++){
        e[i].style.display = 'block';
    }
    var e = document.getElementsByName(name + '_top');
    for (var i=0;i<e.length;i++){
        e[i].setAttribute('class', 'tab_active'); 
    }

}

function hideTab(name){
    var e = document.getElementsByName(name);
    for (var i=0;i<e.length;i++){
        e[i].style.display = 'none'; 
    }
    var e = document.getElementsByName(name + '_top');
    for (var i=0;i<e.length;i++){
        e[i].setAttribute('class', 'tab_inactive'); 
    }
}



function fancyPantsHttpRequest(url, method, id)
// stolen from http://www.w3schools.com/dom/tryit.asp?filename=try_dom_xmlhttprequest_first
// send request to url, use method, insert result into element with id
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(id).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open(method,url,true);
xmlhttp.send();
}


function fancyPantsFlashForum()
// modified version of code stolen from 
// http://www.w3schools.com/dom/tryit.asp?filename=try_dom_xmlhttprequest_first
// hardcoded to post to the flashforum, could probably be more elegant
{
    var form = document.forms.flashpost_form;
    var flashtext = encodeURIComponent(form.elements.flashtext.value);
    var name = encodeURIComponent(form.elements.name.value);
    var answer = encodeURIComponent(form.elements.answer.value);
    var data = "flashtext=" + flashtext + "&name=" + name + "&answer=" + answer;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("flashposts").innerHTML=xmlhttp.responseText;
        form.elements.flashtext.value = "";
        form.elements.countdown.value = 180;
        toggleVisible("flashpost_form");
        }
      }
    xmlhttp.open("POST","/flashforum/add/",true);
    xmlhttp.send(data);
}

function bunt(){
    var b=document.createElement('div');
    b.style.position='absolute';
    b.style.top='0';
    b.style.left='0';
    b.style.width='100%';
    b.style.height='98px';
    b.style.background='transparent url(http://www.readandfindout.com/resources/images/bunting.png) 0 0 repeat-x';document.body.appendChild(b);
}


