/************ Functions to work with expand/collapse ******************/
function togglegroups(groupname) {
  if (document.getElementById) {
    if (document.getElementById(groupname).className == "invisible") {
      document.getElementById(groupname).className = "visible";
      document.getElementById(groupname+"_sign").src = "/images/minus.gif";
    } else if (document.getElementById(groupname).className == "visible") {
      document.getElementById(groupname).className = "invisible";
      document.getElementById(groupname+"_sign").src = "/images/plus.gif";
    }
  }
}

function getElementByClass(classname){
  ccollect = new Array();
  var inc = 0;
  var alltags = document.all ? document.all : document.getElementsByTagName("*");
  for (i = 0; i < alltags.length; i++) {
    if (alltags[i].className == classname) {
      ccollect[inc] = alltags[i];
      inc++;
    }
  }
  return ccollect;
}

function toggleall(actiontodo){
  var tagarray = new Array();
  //pattern matching to find the image tags with /images/plus.gif and /images/minus.gif
  var patternplus = /.*\/images\/plus\.gif/;
  var patternminus = /.*\/images\/minus\.gif/;
  //if statement to either expand or collapse
  if (actiontodo == "expand") {
    //create array of all elements with class = invisible
    tagarray = getElementByClass("invisible");
    //loop through array to change class of all elements form invisible to visible
    for (i = 0; i < tagarray.length; i++) {
      tagarray[i].className = "visible";
    }
    //create array of all image tags
    tagarray = document.getElementsByTagName("img");
    //loop through array to find image tags with source = /images/plus.gif and change to /images/minus.gif
    for (i = 0; i < tagarray.length; i++) {
      if (patternplus.test(tagarray[i].src)) {tagarray[i].src = "/images/minus.gif";}
    }
  //same as above, but for collapse
  } else if (actiontodo == "collapse") {
    tagarray = getElementByClass("visible");
    for (i = 0; i < tagarray.length; i++) {
      tagarray[i].className = "invisible";
    }
    tagarray = document.getElementsByTagName("img");
    for (i = 0; i < tagarray.length; i++) {
      if (patternminus.test(tagarray[i].src)) {tagarray[i].src = "/images/plus.gif";}
    }
  }
}

/*********** Functions to work with expand/collapse cookies ***********/
function saveState(cookieName) {
  var tagarray = new Array();
  var invisarray = getElementByClass("invisible");
  var visarray = getElementByClass("visible");
  tagarray = invisarray.concat(visarray);

  var state = "";
  state = cookieName + '=';
  for (var i=0; i < tagarray.length; i++) {
    state += tagarray[i].id + ':' + tagarray[i].className;
    if (i != tagarray.length - 1) {state += '&'}
  }
  document.cookie = state;
}
function restoreState(cookieName) {
  var state = document.cookie;
  cName = cookieName + '=';
  var pos = state.indexOf(cName);

  if (pos != -1) {
    var start = pos + cName.length;
    var end = state.indexOf(';',start);
    if (end == -1) end = state.length;
    var cValue = state.substring(start, end);

    var stateArr = cValue.split('&');
    for (var i=0; i < stateArr.length; i++) {stateArr[i] = stateArr[i].split(':');}

    for (var i=0; i < stateArr.length; i++) {
      document.getElementById(stateArr[i][0]).className = stateArr[i][1];
      if (stateArr[i][1] == "invisible") {
        document.getElementById(stateArr[i][0]+"_sign").src = "/images/plus.gif";
      } else {
        document.getElementById(stateArr[i][0]+"_sign").src = "/images/minus.gif";
      }
    }
  }
}

/******************* Functions to work with pictures ******************/
function pictureWindow(picture,w,h) {
   w = w + 20;
   h = h + 20;
   winName = picture.slice(9,-4);
   prefix = "http://www.sintons.net/";
   picLocation = prefix + picture;
   picWindow = window.open(picLocation, winName, 'width=' + w + ',height=' + h + ',toolbar=no,status=no,location=no,menubar=no');
   picWindow.focus();
}

/******************* Functions to work with forms *********************/
function placeCursor() {
  if (document.forms[0]) {
     if (document.forms[0].name == "gb_form") {
      document.gb_form.visitor_name.focus();
      document.gb_form.how_know_us.selectedIndex = -1;
   } else if (document.forms[0].name == "pr_form") {
      document.pr_form.visitor_name.focus();
    document.pr_form.visitor_anon.checked = false;
   } else if (document.forms[0].name == "notify_form") {
      document.notify_form.notify_name.focus();
   } else if (document.forms[0].name == "notify_remove_form") {
      document.notify_remove_form.notify_email.focus();
   } else if (document.forms[0].name == "supdate_form") {
      document.supdate_form.updated_page.focus();
      document.supdate_form.updated_section.selectedIndex = -1;
   } else if (document.forms[0].name == "subpic_form") {
      document.subpic_form.visitor_name.focus();
      if (document.subpic_form.picture_desc.value == "nodesc") {
         document.subpic_form.picture_desc.value = "";
    }
   }
  }
}

function validateForms() {
  errormessage = "";
  if (document.forms[0].name == "pr_form") {
    if (document.pr_form.visitor_anon.checked == false) {
      if (document.pr_form.visitor_name.value == "") {errormessage += "Name is required.\n";}
      if (document.pr_form.visitor_email.value == "") {errormessage += "Email is required.\n";}
      if (document.pr_form.visitor_prayer_request.value == "") {errormessage += "Prayer Request is required.\n";}
    } else {
      if (document.pr_form.visitor_prayer_request.value == "") {errormessage += "Prayer Request is required.\n";}
    }
    if (errormessage != "") {
      alert(errormessage);
      event.returnValue = false;
    }
  } else if (document.forms[0].name == "gb_form") {
    if (document.gb_form.visitor_name.value == "") {errormessage += "Name is required.\n";}
    if (document.gb_form.visitor_email.value == "") {errormessage += "Email is required.\n";}
    if (document.gb_form.how_know_us.selectedIndex == -1) {errormessage += "Please select how you know us.\n";}
    if (document.gb_form.visitor_message.value == "") {errormessage += "Message is required.\n";}
    if (errormessage != "") {
      alert(errormessage);
      event.returnValue = false;
    }
  } else if (document.forms[0].name == "notify_form") {
    if (document.notify_form.notify_name.value == "") {errormessage += "Name is required.\n";}
    if (document.notify_form.notify_email.value == "") {errormessage += "Email is required.\n";}
    if (errormessage != "") {
      alert(errormessage);
      event.returnValue = false;
    }
  } else if (document.forms[0].name == "notify_remove_form") {
    if (document.notify_remove_form.notify_email.value == "") {errormessage += "Email is required.\n";}
    if (errormessage != "") {
      alert(errormessage);
      event.returnValue = false;
    }
  } else if (document.forms[0].name == "supdate_form") {
    if (document.supdate_form.updated_page.value == "") {errormessage += "Updated Page is required.\n";}
    if (document.supdate_form.updated_section.selectedIndex == -1) {errormessage += "Please select an Updated Section.\n";}
    if (document.supdate_form.update_description.value == "") {errormessage += "Description is required.\n";}
    if (errormessage != "") {
      alert(errormessage);
      event.returnValue = false;
    }
  } else if (document.forms[0].name == "subpic_form") {
    if (document.subpic_form.visitor_name.value == "") {errormessage += "Name is required.\n";}
    if (document.subpic_form.visitor_email.value == "") {errormessage += "Email is required.\n";}
    if (document.subpic_form.pic_file.value == "") {errormessage += "A picture must be selected.\n";}
    if (errormessage != "") {
      alert(errormessage);
      event.returnValue = false;
    } else {
      if (document.subpic_form.pic_desc.value == "") {document.subpic_form.pic_desc.value = "nodesc";}
    }
  }
}

function prAnon() {
  if (document.pr_form.visitor_anon.checked == true) {
    document.pr_form.visitor_name.disabled = true;
    document.pr_form.visitor_email.disabled = true;
    document.getElementById("asterick1").setAttribute("color", "white");
    document.getElementById("asterick2").setAttribute("color", "white");
  } else {
    document.pr_form.visitor_name.disabled = false;
    document.pr_form.visitor_email.disabled = false;
    document.getElementById("asterick1").setAttribute("color", "red");
    document.getElementById("asterick2").setAttribute("color", "red");
  }
}

