function gotoPage(pNum)
{
  var frm = getElem("searchForm");
  var pg  = getElem("curPage");

  pg.value = pNum;
  frm.submit();
}

function clipsPerPage(pNum)
{
  var frm = getElem("searchForm");
  var pg  = getElem("clipsPerPage");

  pg.value = pNum;
  frm.submit();
}

function sortPage(sort)
{
  var frm = getElem("searchForm");
  var s   = getElem("sort");

  s.value = sort;
  frm.submit();
}


function loadMovie(href, height, width) 
{
  var warning = '';
  var scale = 'scale';
  var scaleValue = 'tofit';

  if (navigator.appVersion.indexOf("Mac") > 0) {
    if (navigator.appName.substring(0,8) == "Netscape") {
      if (navigator.plugins) {
        for (i=0; i < navigator.plugins.length; i++ ) {
          if (navigator.plugins[i].name.indexOf("QuickTime Plug-in") >= 0) {
            if (navigator.plugins[i].name.indexOf("7.6.4") >= 0) {
              warning = "<p style='text-align:center; font-size:smaller'>If you are using FireFox a Mac, you may not see clips with the correct aspect ratio.  Try using Safari instead.</p>" ;
              scale = '';
              scaleValue = '';
            }
          }
        }
      }
    }
  }

  window.setTimeout(
    function() 
    { 
      var movDiv = getElem('us_mov_div'); 
      movDiv.innerHTML = QT_GenerateOBJECTText(href, height, width, '','autoplay', 'true', scale, scaleValue) + warning;
    }, 
    525);
}

function bin(link, id) 
{
  var http = createRequestObject();
  var op;
  var newText;

  if (link.innerHTML.indexOf('Add') > 0) {
    op = 'a';
    newText = '- Remove from bin';
  }
  else {
   op = 'd';
   newText = '+ Add To Bin';
  } 

  http.open('get', 'binOp.php?' + op + '=' + id, true);
    
  http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
      link.innerHTML = newText;
      if (link.id.indexOf('popup') > 0) {
        // we are in the popup so set the underlying text as well.
        link = getElem('c' + id + '_bin');
        link.innerHTML = newText;
      }
    }
  }

  http.send(null);
}

function saveBin(mode) 
{
  var params;

  if (mode == 'quote') {
    params = 'q=1';
  }
  else if (mode == 'valet') {
    params = 'v=1';
  } 
  else {
    params = 's=1';
  }

  // add the project tidbits to the request -- bail if anything is missing.
  var email = getElem('bin_email').value;
  if (email == '') {
    alert('You must specify an email address.');
    return(false);
  }
  else if (email != getElem('bin_email2').value) {
    alert('The email addresses you supplied do not match.  Please check the values.');
    return(false);
  }
  else if ((email.indexOf('@') < 0)  || (email.lastIndexOf('.') < email.indexOf('@'))) {
    alert('The email address you supplied does not appear to be a valid email address.  Please check the value.');
    return(false);
  }
  else {
    params = params + '&bin_email=' + email;
    params = params + '&bin_email2=' + email;
  }

  if (mode == 'valet') {
    params = params + '&bin_name=' + escape('Valet Request');
  }
  else if (getElem('bin_name').value == '') {
    alert('You must specify a name for this bin.');
    return(false);
  }
  else {
    params = params + '&bin_name=' + escape(getElem('bin_name').value);
  }

  // optional unless we are quoting.
  if ((mode != 'save') && getElem('bin_contactname').value == '') {
    alert('You must specify a contact name for this bin.');
    return(false);
  }
  else {
    params = params + '&bin_contactname=' + escape(getElem('bin_contactname').value);
  }

  // phone number is optional -- unless it is the best contact and we are quoting
  if ((mode != 'save') && (getElem('bin_phone').value == '') && (getElem('bin_bestcontact').value == 'phone')){
    alert('You must specify a phone number or change the contact method to "email".');
    return(false);
  }
  else {
    params = params + '&bin_phone=' + escape(getElem('bin_phone').value);
  }

  // timezone is optional -- unless phone is the best contact and we are quoting
  if ((mode != 'save') && (getElem('bin_timezone').value == '') && (getElem('bin_bestcontact').value == 'phone')){
    alert('You must specify your timezone or change the contact method to "email".');
    return(false);
  }
  else {
    params = params + '&bin_timezone=' + escape(getElem('bin_timezone').value);
  }

  // contact time is optional -- unless phone is the best contact and we are quoting
  if ((mode != 'save') && (getElem('bin_besttime').value == '') && (getElem('bin_bestcontact').value == 'phone')){
    alert('You must specify the best time to contact you or change the contact method to "email".');
    return(false);
  }
  else {
    params = params + '&bin_besttime=' + escape(getElem('bin_besttime').value);
  }

  // optional unless we are quoting.
  if ((mode != 'save') && getElem('bin_bestcontact').value == '') {
    alert('You must indicate the best way to contact you about this bin.');
    return(false);
  }
  else {
    params = params + '&bin_bestcontact=' + escape(getElem('bin_bestcontact').value);
  }

  // notes are optional -- unless we are in valet mode
  if ((mode == 'valet') && getElem('bin_notes').value == '') {
    alert('You must provide a description of your project.');
    return(false);
  }
  else {
    params = params + '&bin_notes=' + escape(getElem('bin_notes').value);
  }
  // optional unless we are quoting.
  if ((mode != 'save') && (getElem('bin_project_type').value == '')) {
    alert('You must specify a project type.');
    return(false);
  }
  else {
    params = params + '&bin_project_type=' + escape(getElem('bin_project_type').value);
  }

  // optional unless we are quoting
  if ((mode != 'save') && (getElem('bin_audience').value == '')) {
    alert('You must specify an audience.');
    return(false);
  }
  else {
    params = params + '&bin_audience=' + escape(getElem('bin_audience').value);
  }

  // optional unless we are quoting
  if ((mode != 'save') && (getElem('bin_distribution').value == '')) {
    alert('You must specify a distribution value.');
    return(false);
  }
  else {
    params = params + '&bin_distribution=' + escape(getElem('bin_distribution').value);
  }

  // optional unless we are quoting
  if ((mode != 'save') && (getElem('bin_delivery').value == '')) {
    alert('You must specify a delivery method.');
    return(false);
  }
  else {
    params = params + '&bin_delivery=' + escape(getElem('bin_delivery').value);
  }

  // optional unless we are quoting
  if ((mode != 'save') && (getElem('bin_format').value == '')) {
    alert('You must specify a footage format.');
    return(false);
  }
  else {
    params = params + '&bin_format=' + escape(getElem('bin_format').value);
  }

  // Short circuit for valet requests (just use the above code to check the data)
  if (mode == 'valet') {
    return(true);
  }

  // Disable the 'save' button and show a busy cursor
  getElem('saveButton').disabled = true;
  document.getElementsByTagName('body')[0].style.cursor = 'wait';
  
  // Send the proper header information along with the request
  http.open('post', 'binOp.php', true);

  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");
    
  http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
      setIBoxContent(http.responseText);
      document.getElementsByTagName('body')[0].style.cursor = 'default';
    }
  }
  
  http.send(params);
  return(true);
}

function loadBin(id) 
{
  hideIbox();
  if (showIbox('binOp.php?l&bin_id=' + escape(id), '', parseQuery('height=500;width=800'))) {
    showBG();
    window.onscroll = maintPos;
    window.onresize = maintPos;
  }
}

function loadBinList() 
{
  var now = new Date();
  var params = 'l=1' + '&bin_email=' + escape(getElem('bin_email').value)  + '&tzOffset=' + now.getTimezoneOffset();

  // Send the proper header information along with the request
  http.open('post', 'binOp.php', true);

  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");
    
  http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
      setIBoxContent(http.responseText);
    }
  }
  
  http.send(params);
}

function clearBin(confirm) 
{
  var params = 'c=1';

  if (confirm) {
    params = params + '&confirm=1';
  }

  // Send the proper header information along with the request
  http.open('post', 'binOp.php', true);

  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");
    
  http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
      setIBoxContent(http.responseText);
    }
  }
  
  http.send(params);
}
