// JScript File

var imageDir = 'gif/';
var visibleLayerId = null;
var activeLinkId = 'photographs';
var defaultLinkId = 'photographs';
var iPhoto = 0;
var iniSet = 0;

if (document.images) {

  var logo = new Image(171,146);
  logo.src = imageDir + 'logo.gif';

  var logo_anime = new Image(171,146);
  logo_anime.src = imageDir + 'logo_anime.gif';

  var photographsBtn_off = new Image(156,32);
  photographsBtn_off.src = imageDir + 'photographs_off.gif';

  var photographsBtn_on = new Image(156,32);
  photographsBtn_on.src = imageDir + 'photographs_on.gif';

  var aboutBtn_off = new Image(88,32);
  aboutBtn_off.src = imageDir + 'about_off.gif';

  var aboutBtn_on = new Image(88,32);
  aboutBtn_on.src = imageDir + 'about_on.gif';

  var contactBtn_off = new Image(100,32);
  contactBtn_off.src = imageDir + 'contact_off.gif';

  var contactBtn_on = new Image(100,32);
  contactBtn_on.src = imageDir + 'contact_on.gif';

  var signinBtn_off = new Image(86,32);
  signinBtn_off.src = imageDir + 'sign_in_off.gif';

  var signinBtn_on = new Image(86,32);
  signinBtn_on.src = imageDir + 'sign_in_on.gif';

  var previousBtn_off = new Image(32,685);
  previousBtn_off.src = imageDir + 'previous_off.gif';

  var previousBtn_on = new Image(32,685);
  previousBtn_on.src = imageDir + 'previous_on.gif';

  var nextBtn_off = new Image(32,685);
  nextBtn_off.src = imageDir + 'next_off.gif';

  var nextBtn_on = new Image(32,685);
  nextBtn_on.src = imageDir + 'next_on.gif';

  var close_off = new Image(32,32);
  close_off.src = imageDir + 'close_off.gif';

  var close_on = new Image(32,32);
  close_on.src = imageDir + 'close_on.gif';
}

function init() {
  getContent('menu');
  getPhoto(0,0);
}

function mouseOver(obj) {
  if (obj.className == 'close') {
    obj.src = close_on.src;
    window.status = 'Close';
  }
  else {
    if (obj.id != activeLinkId + 'Btn') {
      obj.src = eval(obj.id + '_on').src;
    }
    else {
      obj.src = eval(obj.id + '_off').src;
    }
    window.status = obj.getAttribute('alt');
  }
  obj.style.cursor = 'pointer';
}

function mouseOut(obj) {
  if (obj.className == 'close') {
    obj.src = close_off.src;
  }
  else {
    if (obj.id != activeLinkId + 'Btn') {
      obj.src = eval(obj.id + '_off').src;
    }
    else {
      obj.src = eval(obj.id + '_on').src;
    }
  }
  obj.style.cursor = 'default';
  window.status = '';
}

function showHideLayer(id) {
  var isButton = false;
  if (id.indexOf('Btn') != -1) {
    isButton = true;
    id = id.replace('Btn','');
  }
  if (visibleLayerId) {
    document.getElementById('content').style.visibility = 'hidden';
    if (document.getElementById('signinError')) {
      document.getElementById('signinError').style.visibility = 'hidden';
    }
    if (activeLinkId) {
       document.getElementById(activeLinkId + 'Btn').src = eval(activeLinkId + 'Btn_off').src;
    }
    if (isButton && (document.getElementById(id + 'Btn').className == 'close') || (id == defaultLinkId)) {
      document.getElementById(defaultLinkId + 'Btn').src = eval(defaultLinkId + 'Btn_on.src');
      visibleLayerId = null;
      activeLinkId = defaultLinkId;
    }
    else {
      if (visibleLayerId != id) {
        getContent(id);
        document.getElementById('content').style.visibility = 'visible';
        visibleLayerId = id;
        if (isButton) {
          document.getElementById(id + 'Btn').src = eval(id + 'Btn_on.src');
          activeLinkId = id;
        }
        else {
          document.getElementById(defaultLinkId + 'Btn').src = eval(defaultLinkId + 'Btn_off.src');
          activeLinkId = null;
        }
      }
      else {
        if (isButton) {
          document.getElementById(id + 'Btn').src = eval(id + 'Btn_off').src;
        }
        document.getElementById(defaultLinkId + 'Btn').src = eval(defaultLinkId + 'Btn_on.src');
        visibleLayerId = null;
        activeLinkId = defaultLinkId;
      }
    }
  }
  else {
    if (id != defaultLinkId) {
      document.getElementById(defaultLinkId + 'Btn').src = eval(defaultLinkId + 'Btn_off.src');
      getContent(id);
      document.getElementById('content').style.visibility = 'visible';
      visibleLayerId = id;
      if (isButton) {
        document.getElementById(id + 'Btn').src = eval(id + 'Btn_on').src;
        activeLinkId = id;
      }
      else {
        activeLinkId = null;
      }
  }
    else {
      iniSet = 0;
      getPhoto(0,0);
    }
  }
}

function getPhoto(set,_iniSet) {
  if (set != 0 && iniSet == 0) {
    iniSet = _iniSet;
  }
  showHideWorking(1);
  iPhoto = 0;
  var httpObj = getNewHttpObject();
  var url = 'photo.php';
  var params = 'set=' + set + '&ini=' + iniSet;
  httpObj.open('POST', url, true);
  httpObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  httpObj.setRequestHeader('Content-length', params.length);
  httpObj.setRequestHeader('Connection', 'close');
  httpObj.onreadystatechange = function() {
    if(httpObj.readyState == 4) {
      eval(httpObj.responseText);
    }
  }
  httpObj.send(params);
}

function tallyPhoto(count) {
  iPhoto++;
  if (iPhoto == count) {
    document.getElementById('photographs').scrollLeft = 0;
    showHideWorking(0);
  }
}

function getContent(div) {
  var httpObj = getNewHttpObject();
  var url = 'content.php';
  var params = 'div=' +  div;
  httpObj.open('POST', url, true);
  httpObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  httpObj.setRequestHeader('Content-length', params.length);
  httpObj.setRequestHeader('Connection', 'close');
  httpObj.onreadystatechange = function() {
    if(httpObj.readyState == 4) {
      if (div == 'menu') {
        document.getElementById('menu').innerHTML = httpObj.responseText;
      }
      else {
        document.getElementById('content').innerHTML = httpObj.responseText;
        showHideWorking(0);
      }
    }
  }
  httpObj.send(params);
}

function showHideWorking(action) {
  if (action == 1) {
    document.getElementById('logoImage').src = logo_anime.src;
  }
  else {
    document.getElementById('logoImage').src = logo.src;
  }
}

function validateContactForm(thisForm) {
  var firstName = thisForm.firstName.value;
  var lastName = thisForm.lastName.value;
  var email = thisForm.email.value;
  var message = thisForm.message.value;
  var whiteSpaceRegEx = /^[\s]+$/;
  var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (firstName == '' || whiteSpaceRegEx.test(firstName)) {
     alert('Please include your first name.');
     thisForm.firstName.focus();
  }
  else if (lastName == '' || whiteSpaceRegEx.test(lastName)) {
     alert('Please include your last name.');
     thisForm.lastName.focus();
  }
  else if (email == '' || whiteSpaceRegEx.test(email)) {
     alert('Please include your email address.');
     thisForm.email.focus();
  }
  else if (!emailRegEx.test(email)) {
     alert('You have entered an invalid email address.');
   thisForm.email.focus();
   thisForm.email.select();
  }
  else if (message == '' || whiteSpaceRegEx.test(message)) {
     alert('Please include a message.');
  }
  else {
    sendContactForm(thisForm);
  }
}

function validateSignInForm(thisForm) {
  var userId = thisForm.userId.value;
  var pwd = thisForm.pwd.value;
  var whiteSpaceRegEx = /^[\s]+$/;
  var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (userId == '' || whiteSpaceRegEx.test(userId)) {
    alert('Please enter a user ID.');
    thisForm.userId.focus();
  }
  else if (pwd == '' || whiteSpaceRegEx.test(pwd)) {
    alert('Please enter a password.');
    thisForm.pwd.focus();
  }
  else {
    sendSignInForm(thisForm);
  }
}

function sendContactForm(thisForm) {
  showHideWorking(1);
  var firstName = thisForm.firstName.value;
  var lastName = thisForm.lastName.value;
  var email = thisForm.email.value;
  var message = thisForm.message.value;
  var str1 = firstName;
  str1 = replaceToxicChar(str1);
  var str2 = lastName;
  str2 = replaceToxicChar(str2);
  var str3 = email;
  str3 = replaceToxicChar(str3);
  var str4 = message;
  str4 = replaceToxicChar(str4);
  var httpObj = getNewHttpObject();
  var url = 'http://www.yamamotofoto.com/contact.php';
  var params  = 'firstName=' + str1 + '&lastName=' + str2 + '&email=' + str3 + '&message=' + str4;
  httpObj.open('POST', url, true);
  httpObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  httpObj.setRequestHeader('Content-length', params.length);
  httpObj.setRequestHeader('Connection', 'close');
  httpObj.onreadystatechange = function() {
    if (httpObj.readyState == 4) {
      if (httpObj.status == 200) {
        if (getTagValueByName(httpObj,'status') == 'BAD' ) {
          showHideWorking(0);
          alert('Error sending message. Please try again later.');
        }
        else {
          getContent('contactConfirm');
          var t = setTimeout("showHideLayer('contact')",2000)
        }
      }
    }
  }
  httpObj.send(params);
}

function sendSignInForm(thisForm) {
  showHideWorking(1);
  var userId = thisForm.userId.value;
  var pwd = thisForm.pwd.value;
  var str1 = userId;
  str1 = replaceToxicChar(str1);
  var str2 = pwd;
  str2 = replaceToxicChar(str2);
  var httpObj = getNewHttpObject();
  var url = 'http://www.yamamotofoto.com/signin.php';
  var params  = 'userId=' + str1 + 'pwd=' + str2;
  httpObj.open('POST', url, true);
  httpObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  httpObj.setRequestHeader('Content-length', params.length);
  httpObj.setRequestHeader('Connection', 'close');
  httpObj.onreadystatechange = function() {
    if (httpObj.readyState == 4) {
      if (httpObj.status == 200) {
        showHideWorking(0);
        if (getTagValueByName(httpObj,'status') == 'BAD' ) {
          document.getElementById('signinError').style.visibility = 'visible';
          wipeForm(thisForm);
        }
        else {
          alert('Redirecting.');
        }
      }
    }
  }
  httpObj.send(params);
}

function getTagValueByName(httpObj,tagName) {
  return httpObj.responseXML.documentElement.getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
}

function wipeForm(thisForm) {
  for(i=0; i < thisForm.elements.length; i++) {
    if (thisForm.elements[i].type != 'button') {
    thisForm.elements[i].value = '';
    }
  }
}

function replaceToxicChar(str) {
  str = str.replace(/&/g,'**am**');
  str = str.replace(/=/g,'**eq**');
  str = str.replace(/\+/g,'**pl**');
  return str;
}

function getNewHttpObject() {
  var httpObj;
  try {  // Firefox, Opera 8.0+, Safari
    httpObj = new XMLHttpRequest();
  }
  catch (e) {  // Internet Explorer
    try {
      httpObj = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
      try {
        httpObj = new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch (e) {
        alert('Your browser does not support AJAX!');
        return false;
      }
    }
  }
  return httpObj;
}