var connection = false;
var server = new Server();

var lang = {english:0, french:1};
var language = lang.english;

var prior = {regular:0, high:1};
var priority = prior.regular;

var sendingEmail = false;

function Server() {

  this.txToServer =
    function(txStr) { 
      connection = this.getXMLHttpRequestObject();
      if (connection) {
        if (connection.readyState == 4 || connection.readyState == 0) {
          var url = '/cgi-bin/req.cgi?' + txStr;
          
          connection.open('GET', url, true);
    
// set the function to be called on a change in ajaxObj state
          connection.onreadystatechange = this.handleResponse;
          connection.setRequestHeader('Content-Type', 'text/xml');
          connection.send(null);
        }
      }
    }
    
  this.getXMLHttpRequestObject = 
    function() {

// initially set the object to false
      var XMLHttpRequestObject = false;

// check for Safari, Mozilla, Opera…
      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      }

// check for Internet Explorer
      else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject('Microsoft.XMLHTTP');
      }

      return XMLHttpRequestObject;
    }
  
  this.handleResponse = 
    function() {

// check if the request is complete
      if (connection.readyState == 4) {
        if (!sendingEmail) {
          var rxStr = connection.responseText; 
          server.responseRx(rxStr);        }  
      }  
    }
    
//        01234
// format &SN=<serial>&QP=<Q position>&EH=<>&EM=<>
  this.responseRx = 
    function(txt) {
    
// parse the serial #
      var i = txt.search('&QP=');
      if (i < 0) {
        finisher.showBadDataMsg();
        return;
      }
      var serialStr = txt.substring(4, i);
      matchesRx(serialStr);
    }
    
  this.timeString =
    function() {
      var now = new Date();
      var hours = now.getHours();
      var mins = now.getMinutes();
      if (mins < 10) return 'TM=' + hours + ':0' + mins
      else return 'TM=' + hours + ':' + mins;
    }     
    
  this.validEmail = 
    function(email) {
//      var re = /^\w+([\.-]?\w+)*@\w+([\.-\?\w+)*(\.\w{2,3})+$/;
//      return re.test(email);
    }
        
  this.truncatedString = 
    function(txt, maxSize) {
      var result;
      if (txt.length > maxSize) return (txt.substring(0,maxSize))
      else return txt;
    }
  
  this.timeString =
    function() {
      var now = new Date();
      var hours = now.getHours();
      var mins = now.getMinutes();
      if (mins < 10) return 'TM=' + hours + ':0' + mins
      else return 'TM=' + hours + ':' + mins;
    }  
    
  this.fixedString =
    function(txt,maxChars) {
      var result = this.truncatedString(txt);
      return (escape(result)); 
    }
    
  this.validEmail = 
    function(email) {
      return (email != '');
    }
    
// Format :LN=<language>&NM=<name>&LC=loc&LD=<light data>&EM=<email>
//         &CM=<comments>&NR=<nr>&LR=<lr>&ER=<er>&PR=7
// limits: name:40, loc:40, mail:40, ded:40, com:255
  this.submitDesign = 
    function(nameStr,locStr, emailStr, messageStr,
             friendNameStr, friendLocationStr, friendEmailStr) {
             
// truncate and fix the strings             
      nameStr           = this.fixedString(nameStr,40);
      locStr            = this.fixedString(locStr,40);
      emailStr          = this.fixedString(emailStr,40);
      messageStr        = this.fixedString(messageStr,255);
      friendNameStr     = this.fixedString(friendNameStr);
      friendLocationStr = this.fixedString(friendLocationStr,40);
      friendEmailStr    = this.fixedString(friendEmailStr,40);
           
// language
      var langStr;
      switch (language) {
        case lang.english :
          langStr = 'E';
          break;
        case lang.french :
          langStr = 'F';
          break;
      }

// light data
      var lightStr = "";
      for (var i = 0; i < LIGHTS; i++) {
        lightStr = lightStr + navLight[i].dataString();
      }

      var priorityStr;
      var priorityId = document.getElementById('priorityId');
      
      if (priorityId) priorityStr = '&PR=7'
      else priorityStr = '';
      
/*      switch (priority) {
      
        case (prior.regular) :
          priorityStr = '';
          break;
          
        case (prior.high) :
          priorityStr = '&PR=7';
          break;
      }        
      */

// assemble the final string
      var txStr = 'LN=' + langStr + '&NM=' + nameStr + '&LC=' + locStr + 
                  '&LD=' + lightStr + '&EM=' + emailStr +
                  '&CM=' + messageStr + '&NR=' + friendNameStr + 
                  '&LR=' + friendLocationStr + '&ER=' + friendEmailStr +
                   priorityStr;

// return the string
      log(txStr);
      this.txToServer(txStr);
    }      
}