
var autoPlayOn = false;
var autoPlayTimerId;
var MaxImg = 0;
var CurImg = 0;
var LastImg = 0;
var browserType = -2;
var shareShowing = false;
var shareResponseShowing = false;
var show_text = false;
var curPicLandscape = true;

function facilityImg(source,full,descrip)
{
  var pattern = /(\d+)x(\d+)/;
  var res = full.match(pattern);
  if (res == null) return;
  this.full = new Image(res[1],res[2]);
  this.full.src = source + full;
  this.landscape = this.full.width > this.full.height;
  this.descrip = descrip;
}


function startMemorialBoard()
{
  setMaxImg(facilities.length);
  show_text = true;
}





function showResponse()
{
  idNew = "MemPic" + CurImg;
  changeVisibility(idNew,"","",0);
  if (show_text){
    idNew = "MemText" + CurImg;
    changeVisibility(idNew,"","",0);
  }
  changeVisibility("MemBoardShareResponse","","",1);
  shareResponseShowing = true;
  return(false);  
}


function showResponseStop()
{
  changeVisibility("MemBoardShareResponse","","",0);
  shareResponseShowing = false;
  return(false);  
}

function setMaxImg(val)
{
  MaxImg = val;
  LastImg = val - 1;
}


function setNewImgNum(dir)
{
  LastImg = CurImg;
  CurImg += dir;
  if (CurImg < 0) CurImg = MaxImg - 1;
  if (CurImg == MaxImg) CurImg = 0;
}




function transImg(dir)
{
  stopAutoplay();
  setNewImgNum(dir);
  showPic(CurImg);
  return(false);
}    


function showPic(i)
{
  var picBackgroundId = document.getElementById("MemPictureArea");
  var picId = document.getElementById("facilityPic");
  var facilityTextId = document.getElementById("MemTextArea");


if (picId == null) alert("picId is null");

  if ((curPicLandscape && facilities[i].landscape) || (!curPicLandscape && !facilities[i].landscape)) {
    picId.src = facilities[i].full.src;

  } else {
    if (facilities[i].landscape) { // switch from portrait to landscape
      picBackgroundId.style.backgroundPosition = "28px 0px";
      picId.style.margin = "0px 0px 0px 0px";
      picBackgroundId.style.backgroundImage = "url('" + horizBack.full.src + "')";
      picId.src = facilities[i].full.src;
      curPicLandscape = true;
    } else { // switch from landscape to portrait
      picBackgroundId.style.backgroundImage = "url('" + vertBack.full.src + "')";
      picBackgroundId.style.backgroundPosition = "98px -1px";
      picId.style.margin = "0px 0px 0px 0px";
      picId.src = facilities[i].full.src;
      curPicLandscape = false;
    }
  }
  facilityTextId.innerHTML = '<p class="memPicParagraph">' + facilities[i].descrip + '</p>';

}


function startAutoplay()
{
  stopAutoplay();
  shareMemBoardOff();
  transImg(1);
  autoPlayTimerId = setTimeout("startAutoplay()",5000);
  autoPlayOn = true;
  return(false);
}

function stopAutoplay()
{
  if(!autoPlayOn) return(false);
  clearTimeout(autoPlayTimerId);
  autoPlayOn = false;
  return(false);
}


function changeVisibility(id1,id2,id3,show)
{
  switch(getBrowserType()){
    case -1:
      t = "";
      break;
    case 0:
      t = (show == 1) ? "'visible'" : "'hidden'";
      t = "document.all." + id1 + ".style.visibility=" + t;
      eval(t);
      return;
    case 1:
      t = (show == 1) ? "visible" : "hidden";
      u = document.getElementById(id1);
      u.style.visibility = t;
      return;
    case 2:
      t = (show == 1) ? "show" : "hide";
      if (id2.length == 0 && id3.length == 0) document.layers[id1].visibility = t;
      else if (id3.length == 0) document.layers[id2].document.layers[id1].visibility = t;
      else document.layers[id3].document.layers[id2].document.layers[id1].visibility = t;
      return;
  }
}



function getBrowserType()
{
  if (browserType > -2) return(browserType);
  browserType = -1;
  if (navigator.appName == "Microsoft Internet Explorer"){
    if (parseInt(navigator.appVersion) >= 4) browserType = 0;
    return(browserType);
  }
  if (navigator.appName == "Netscape"){
    switch(parseInt(navigator.appVersion)){
      case 5:
        browserType = 1;
        break;
      case 4:
        browserType = 2;
        break;
    }
  }
  return(browserType);
}



function shareMemBoard()
{
  stopAutoplay();
  showResponseStop();
  changeVisibility("MemBoardTitle","","",0);
  idOld = "MemPic" + CurImg;
  changeVisibility(idOld,"","",0);
  if (show_text){
    idOld = "MemText" + CurImg;
    changeVisibility(idOld,"","",0);
  }
  changeVisibility("MemBoardShare","","",1);
  shareShowing = true;
  return(false);  
}



function shareMemBoardOff()
{
  if (!shareShowing) return(false);
  changeVisibility("MemBoardShare","","",0);
  shareShowing = false;
  return(false);
}


function email_valid(email)
{
  re = /[\/:,#'`\$~!%\^&\*()\+";<>\?\\|\s]/;
  if (re.test(email)) return(false);
  re = /^[^@]+@.+\.../;
  return(re.test(email));
}



function trimWhite(s) {
  s = s.replace(/^\s+/,"");
  s = s.replace(/\s+$/,"");
  return(s);
}



function checkShare(tf)
{
  if (!email_valid(tf.senderEmail.value)){
    alert("Your email address does not appear to be valid.");
    tf.senderEmail.select();
    tf.senderEmail.focus();
    return(false);
  }
  if (!email_valid(tf.recipEmail.value)){
    alert("The recipient's email address does not appear to be valid.");
    tf.recipEmail.select();
    tf.recipEmail.focus();
    return(false);
  }
  if (tf.senderName.value.length < 2){
    alert("Please enter your name.");
    tf.senderName.select();
    tf.senderName.focus();
    return(false);
  }
  if (tf.recipName.value.length < 2){
    alert("Please enter your name.");
    tf.recipName.select();
    tf.recipName.focus();
    return(false);
  }
  tf.senderName.value = trimWhite(tf.senderName.value);
  tf.recipName.value = trimWhite(tf.recipName.value);
  tf.senderEmail.value = trimWhite(tf.senderEmail.value);
  tf.recipEmail.value = trimWhite(tf.recipEmail.value);
//  tf.recipName.value = tf.recipName.value.replace(/\s/g,'%20');
//  tf.senderName.value = tf.senderName.value.replace(/\s/g,'%20');
  t = tf.Title.value;
  if (t.length > 0) t = t + ' ';
  if (tf.fname.value.length > 0) t = t + tf.fname.value + ' ';
  if (tf.mname.value.length > 0) t = t + tf.mname.value + ' ';
  if (tf.lname.value.length > 0) t = t + tf.lname.value;
  tf.deceasedName.value = t;
  t = t.replace(/\s/g,'%20');
//  tf.CoName.value = tf.CoName.value.replace(/\s/g,'%20');
  if (tf.fhEmail.value == "FHEMAIL") tf.fhEmail.value = "info@" + tf.AgencyName.value;
  return(true);
}
