var txTime;
var rxStr;

function showSubmitControls() {
  showOrHideControls(false);
  
  var navCombo = document.getElementById('navCombo');
  navCombo.selectedIndex = 0;
  navCamera.orbit();
  
//  setTimeout('navCamera.rotate()', 3000); 
 
  var topView = document.getElementById('topView');  
  topView.innerHTML = 
 '<table width="100%" style="padding:20px;" border="0">' +	  
    '  <tr>' +
    '    <td width="100%" valign="top">' +
    '      <p style="color:black">' +
    '      Vectorial Elevation is no longer live in Vancouver, so instead you can search for designs in the archive that are closest to your own.</p><br><br> ' +
	  '<input type="submit" name="showMatchesBtn" onclick="showClosestMatchesBtnClicked(this)" id="showMatchesBtn" value="Show closest matches"/><br><br><br>' +
	  '<p>Please add your email below if you would like to be notified next time the project goes live</p><br>'+
	  ' <input type="text" name="textfield" id="emailInput"> <input name="Add" type="button" value="Add" onclick="addBtnClicked(this)">'+
    '    </td>' +
    '  </tr>' +  
    '  ' +
    '</table>';
}

function addBtnClicked(sender) {
  var emailInput = document.getElementById('emailInput');
  var emailStr = emailInput.value;
  
  sendingEmail = true;
  server.submitDesign('','',emailStr,'','','','');
  
  alert('Thanks, we will send you an email when the project is live again.');
}

function makeAnotherDesignBtnClicked() {
  showOrHideControls(true);
  showTopView2();
  
  for (var i = 0; i < LIGHTS; i++) {
    lightSelected[i] = true;
  }
  syncCheckBoxes();  
  
  marker.move(originLatitude, originLongitude);
  slider.setZ(StartSliderZ);
  
// select pyramid  
  var stylesCombo = document.getElementById('stylesCombo');
  stylesCombo.selectedIndex = 0;
  designMode = DesignMode.pyramid;
  
  syncBeams();

  var camModeCombo = document.getElementById('camModeCombo');
  camModeCombo.selectedIndex = 0;

  var navCombo = document.getElementById('navCombo');
  navCombo.selectedIndex = 1;
  
  navCamera.stop();
  navCamera.selectView(1);
}  

function showClosestMatchesBtnClicked(sender) {
  var emailInput = document.getElementById('emailInput');
  var emailStr = emailInput.value;
  
  var now = new Date();
  txTime = now.getTime();
  
  showWaitingForResponseMsg();
  
  sendingEmail = false;
  server.submitDesign('','',emailStr,'','','','');
  //setTimeout(fakeResponse, 2000);
}

function showOrHideControls(visible) {
  var setting;
  if (visible) setting = ''
  else setting = 'none';

  var instructLbl = document.getElementById('instruct1');
  var submitDiv = document.getElementById('submitDiv');
  
  var selectlightsDiv = document.getElementById('selectlights');
  var sliderDiv = document.getElementById('sliderDiv');
  
  var stylesDiv = document.getElementById('presets');
  
  if (visible) {
    instructLbl.style.color = '#666666';
  }
  else instructLbl.style.color = '#FFFFFF';
  
  selectlightsDiv.style.display = setting;
  sliderDiv.style.display = setting;
  submitDiv.style.display = setting;
  stylesDiv.style.display = setting;
}  

function newDesignBtnClicked(sender) {
  showTopView();
  syncBeams();
  showOrHideControls(true);
  
  var stopRB = document.getElementById('radio1');
  stopRB.checked = true;
  
  var navCombo = document.getElementById('navCombo');
  navCombo.selectedIndex = 0;

  navCamera.stop();
  navCamera.selectView(0);             
}

function showWaitingForResponseMsg() {
  var topView = document.getElementById('topView');  
  topView.innerHTML = 
    '<div style="padding:25px;"><br /><br />' +
      'Your data has been sent to the server.  Please wait a few seconds while we find the closest matches in our archive.' + 
    '</div>';
}

function fakeResponse() {
//  finisher.showResponse('1002');
  window.open('http://www.vectorialvancouver.net/cgi-bin/matchsearch.cgi?SR=1002','_self');
}

function finalSubmitBtnClicked(sender) {
  var nameInput = document.getElementById('nameInput');
  var locationInput = document.getElementById('locationInput');
  var emailInput = document.getElementById('emailInput');
  var messageTextArea = document.getElementById('messageTextArea');
  var friendCB = document.getElementById('friendCB');
  var friendNameInput = document.getElementById('friendNameInput');
  var friendLocationInput = document.getElementById('friendLocationInput');
  var friendEmailInput = document.getElementById('friendEmailInput');
   
  var nameStr = nameInput.value;
  if (nameStr == '') {
    alert('Please fill in your name first');
    return;
  }
  
  var locStr = locationInput.value;
  if (locStr == '') {
    alert('Please fill in your location');
    return;
  }
  
  var emailStr = emailInput.value;
  var messageStr = messageTextArea.value;
  
  var friendNameStr = '';
  var friendEmailStr = '';
  var friendLocationStr = '';
  
  var friendNameStr = friendNameInput.value;
  var friendLocationStr = friendLocationInput.value;
  var friendEmailStr = friendEmailInput.value;

  log(nameStr);
  log(locStr);
  log(emailStr);
  log(messageStr);
  log(friendNameStr);
  log(friendLocationStr);
  log(friendEmailStr);

  server.submitDesign(nameStr,locStr,emailStr,messageStr,friendNameStr,friendLocationStr,friendEmailStr);
//setTimeout(fakeResponse, 2000);

  showWaitingForResponseMsg();
}  

function matchesRx(serialStr) {
  rxStr = serialStr;
  
  var now = new Date();
  var time = now.getTime();

  var waitTime = (txTime + 3000) - time;  
  
log('Rx : ' + rxStr);
log('tx time = ' + txTime +' wait time = ' + waitTime);
  
  if (waitTime > 0) setTimeout(showMatches, waitTime);
  else showMatches();
}  

function showMatches() {
  window.open('http://www.vectorialvancouver.net/cgi-bin/matchsearch.cgi?SR=' + rxStr,'_self');
}

