var resizeptviewer_timeout=null;

function ptviewer_windowresized()
{
  if(resizeptviewer_timeout) clearTimeout(resizeptviewer_timeout);
  resizeptviewer_timeout=null;
  resizeptviewer_timeout=setTimeout("resizeptviewer()",300);
}

function resizeptviewer()
{
  resizeptviewer_timeout=null;
  var ptv=document.getElementById('ptviewer');
  if(ptv)
  {
    ptv.stop();
    ptv.start();
  }
}

function hasShockwave()
{
  var result=false;
  if(typeof hasVB != 'undefined')
  {
    if(hasActiveXControl("SWCtl.SWCtl")
    || hasActiveXControl("SWCtl.SWCtl.1") )
    {
      return true;
    }
  }
  else if(typeof navigator != 'undefined')
  {
    if(typeof  navigator.mimeTypes != 'undefined')
    {
      if(navigator.mimeTypes["application/x-director"])
      {
        if(navigator.mimeTypes["application/x-director"].enabledPlugin)
        {
          return true;
        }
      }
    }
  }
}

function useSpiV()
{
/*
  if(typeof navigator != 'undefined')
  {
    if(typeof navigator.product != 'undefined')
    {
      if(typeof navigator.productSub != 'undefined')
      {
        if(  (navigator.product == "Gecko")
          && (navigator.productSub == "20051111") )
        {
          // firefox 1.5! Has a bug, we don't use SPI-V:
          return false;
        }

      }
    }
  }
*/
  if(hasShockwave())
  {
    return true;
  }
  return false;
}

function openGallery(url,ignore_error)
{
  if(useSpiV())
  {
    if(url.indexOf("?") >= 0)
    {
      url += "&";
    }
    else
    {
      url += "?";
    }
    url += "sw=1";
  }
  var w=window.open(url,'gallery','directories=no,location=no,menubar=no,resizable=yes,scrollbars=1,status=yes,titlebar=yes,toolbar=yes,height=600,width=780');
  if(w)
  {
    w.focus();
    return false;
  }
  else
  {
    if(!ignore_error)
    {
      alert("The PTGui site was unable to open a popup window. Possibly a popup blocker is preventing this.\n\nThe gallery will now open in the same window.");
      window.location=url;
    }
  }
}


function checkFormFieldsMalingList()
{
  var emailbox=document.getElementById('email');
  var email=emailbox.value;
  if(email.length > 0)
  {
    if(! isValidEmailAddress(email) )
    {
      alert("This does not seem to be a valid e-mail address! Please correct it, or just leave it empty if you don't wish to receive information about updates.");
      emailbox.value="";
      emailbox.focus();
      return false;
    }
  }
  return true;
}

function isValidEmailAddress(v)
{
  var hadat=false;
  var haddot=false;
  var charcounter=0;
  for(i=0;i<v.length;i++)
  {
    var c=v.charAt(i);
    if(c==' ') return false;
    else if(c == '@')
    {
      if(hadat) return false;
      if(i==0) return false;
      hadat=true;
      charcounter=0;
    }
    else if(c == '.')
    {
      if(hadat)
      {
        if(charcounter == 0) return false;
        haddot=true;
        charcounter=0;
      }
    }
    else
    {
      charcounter++;
    }
  }
  if(! hadat) return false;
  if(! haddot) return false;
  if(charcounter==0) return false;
  return true;
}

function checkUploadForm(frm)
{
  var error="";
  if(frm.file.value == "")
  {
    error="You have not yet selected a file to upload. Press the Browse button and select the file to be uploaded on your computer's hard disk.";
  }
  else if(frm.email.value == "")
  {
    error="Please fill in your email address, otherwise we have no way to know who sent the file!";
    frm.email.focus();
  }
  if(error)
  {
    alert(error);
    return false;
  }
  else
  {
    if(document.getElementById)
    {
      var id1=document.getElementById("uploadformdiv");
      var id2=document.getElementById("uploadingdiv");
      var submitbutton=document.getElementById("uploadsubmit");
      if(id1 && id2)
      {
        var canhideform=true;
        if(navigator.userAgent.indexOf("Safari") >= 0)
        {
          canhideform=false;
        }
        if(submitbutton)
        {
          submitbutton.disabled=true;
        }
        if(canhideform)
        {
          id1.style.display="none";
        }
        id2.style.display="block";
        doUploadDots();
      }
    }
    return true;
  }
}


var dotsindex=3;
var dotstimeout=null;
function doUploadDots()
{
  if(dotstimeout)
  {
    clearTimeout(dotstimeout);
    dotstimeout=null;
  }
  if(document.getElementById)
  {
    var id=document.getElementById("uploaddots");
    if(id)
    {
      var str="";
      for(var i=0; i < dotsindex; i++)
      {
        str += ".";
      }
      id.innerHTML=str;
      dotsindex++;
      if(dotsindex > 5) dotsindex=0;
    }
    dotstimeout=setTimeout("doUploadDots()",300);
  }
}
