// sascipt.js
// Javascripts for the PHP-driven Stacy Adams Jewelry Website
// Matt Schloss 2005

// FUNCTIONS IN THIS SCRIPT:

// showpic
// addtotal
// entsub
// roundit
// checkform

//---------------------------------------------------------------------------

var ultimateshow=new Array()
// gifs must be 150 x 150 pixels, as many as you like, border should be 60% grey
ultimateshow=['46686.gif', '46676.gif', '46666.gif']

var nothome=(document.URL.indexOf("?")) // only on home page!!!
if (nothome == -1) nothome = 0

var slidecycles="continuous" //number of cycles before slideshow stops (ie: "2" or "continous")
var randomorder="yes" //randomize the order in which images are displayed? "yes" or "no"
var preloadimages="yes" //preload images? "yes" or "no"
var slidedelay=5000 // 5 seconds for each pic

var ie=document.all
var dom=document.getElementById
var curcycle=0

if (preloadimages=="yes") {
  for (i=0;i<ultimateshow.length;i++) {
    var cacheimage=new Image()
    cacheimage.src= "imgs/" + ultimateshow[i]
  }
}

var currentslide=0

function randomize(targetarray) {
  ultimateshowCopy=new Array()
  var the_one
  var z=0
  while (z<targetarray.length) {
    the_one=Math.floor(Math.random()*targetarray.length)
    if (targetarray[the_one]!="_selected!") {
      ultimateshowCopy[z]=targetarray[the_one]
      targetarray[the_one]="_selected!"
      z++
    }
  }
}

if (randomorder=="yes")
  randomize(ultimateshow)
else
  ultimateshowCopy=ultimateshow


function rotateimages() { // replaces the img src for slidepic with 1 sec dissolve transition
  curcycle=(currentslide==0)? curcycle+1 : curcycle
  document.images.slidepic.style.filter="blendTrans(duration=1)";
  document.images.slidepic.filters.blendTrans(duration=1).Apply();
  document.images.slidepic.filters.blendTrans.Play();
  document.images.slidepic.src="imgs/" + ultimateshowCopy[currentslide];
  lastlink = ultimateshowCopy[currentslide]; // for follow pic link
  lastlink = lastlink.split('.'); // get rid of .gif
  if (currentslide==ultimateshow.length-1) currentslide=0
  else currentslide++
  if (curcycle==parseInt(slidecycles) && currentslide==0)
    return
  setTimeout("rotateimages()",slidedelay) // keep on going!
}

function followpic() { // if user clicks on picture, they are taken to its page
  window.location = "index.php?family:" + lastlink[0]
}

function start_slider(){ // either DOM or IE's document.all
  crossrotateobj=dom? document.getElementById("slidedom") : document.all.slidedom
  rotateimages()
}

if ((ie||dom) && (!(nothome)))
window.onload=start_slider

//---------------------------------------------------------------------------


function showimage(thispic) {
  document.images.largepic.src = thispic;
  return true;
}

//---------------------------------------------------------------------------

function addtotal() {
var totalprice = 0.0
var i = 0
while (document.forms["prodfam"].elements["quan["+i+"]"]) {
  if (document.forms["prodfam"].elements["quan["+i+"]"].value != 0)
    totalprice += parseInt(document.forms["prodfam"].elements["quan["+i+"]"].value) * parseFloat(document.forms["prodfam"].elements["price["+i+"]"].value)
  else
	document.forms["prodfam"].elements["quan["+i+"]"].value = 0
  i++
}
document.prodfam.subtotal.value = roundit(totalprice)
}

//-------------------------------------------------------------------------

// press enter after entering password
function entsub() {
   if (window.event && window.event.keyCode == 13)
      password(pass.word.value);
   else
      return true;
}

//----------------------------------------------------------------------------

// formats number to dollars and cents
function roundit(num) {
      var dec = num.toString().length - num.toString().indexOf('.');
      if (num.toString().indexOf('.') == -1) {     // no decimal
         var ans = num.toString() + ".00";
         return ans; }
      else {
         if (dec == 3) {                           // one place
            var ans = num.toString();
            return ans; } 
         if (dec == 2) {                           // two places
            var ans = num.toString() + "0";
            return ans; }
         if (dec > 3) {                            // many places
            var Rounder = Math.pow(10, 2);
            return Math.round(num * Rounder) / Rounder; }
      }
}

//--------------------------------------------------------------------------

// validates the add new account form
function checkform(form) {
   if ((form.webpass.value != "usa") && (form.webpass.value != "USA")) {
      alert( "The website access password is incorrect.  Please call 1-800-837-8686 for access." );
	  document.accountform.webpass.value = "";
      form.webpass.focus();
      return false ;
   }
   if (form.company.value == "") {
      alert( "Please enter your company name." );
      form.company.focus();
      return false ;
   }
   if (form.fullname.value == "") {
      alert( "Please enter your name." );
      form.fullname.focus();
      return false ;
   }
   if (form.phone.value == "") {
      alert( "Please enter your phone number." );
      form.phone.focus();
      return false ;
   }
   var emailaddress1 = form.email.value;
   var emailaddress2 = form.email2.value;
   apos = emailaddress1.indexOf("@");
   dotpos = emailaddress1.lastIndexOf(".");
   badchar = emailaddress1.match(" ");
   notvalid = (apos < 1 || dotpos - apos < 2 || badchar);
   if ((notvalid) || (form.email.value != form.email2.value)) {
      alert( "That is not a valid email address, please reenter your email address." );
	  document.accountform.email.value = "";
	  document.accountform.email2.value = "";
      form.email.focus();
      return false ;
   }
   if ((form.pass.value == "") || (form.pass.value != form.pass2.value)) {
      alert( "Please reenter your password." );
	  document.accountform.pass.value = "";
	  document.accountform.pass2.value = "";
      form.pass.focus();
      return false ;
   }
   return true ;
}