/*
 * Javascript TicketHandler AJAX Library 0.0.0.2
 *
 * Author: Tom Pennings (tom@pennings.be)
 * Legal: use of these methods is absolutely free when appropriate credit is provided
 *        use of webservice http://www.bestphoto4you.com/webservices/BestPhoto4YouTicket.asmx us strictly prohibited
 */

var TH_errorMessage;
var TH_isValidTicket;
var DEBUG_ENABLED = false;

function TH_handleRequest(ticket) {
  var httpRequest = null;
  if (typeof XMLHttpRequest != 'undefined') {
    httpRequest = new XMLHttpRequest();
  }
  else if (typeof ActiveXObject != 'undefined') {
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        httpRequest = null;
      }
    }
    @end @*/
  }

  // now that we have an XMLHttpRequest object let's try and use it
  if (httpRequest != null) {
    // we'll open with the GET method (the last parameter makes sure we handle
    // the transaction asynchronously, which prevents 'hanging' of the browser
    // when using a slow connection
    try {
      var requestAsynchronous = false;
      var SOAPMsg = TH_getSOAPMessage(ticket);

      httpRequest.open('POST', 'BestPhoto4YouTicket.asmx', requestAsynchronous);
      httpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
      httpRequest.setRequestHeader("SOAPAction", "http://tempuri.org/ValidateTicket");
      httpRequest.setRequestHeader("Content-Length", SOAPMsg.length);

      if (requestAsynchronous) {
        httpRequest.onreadystatechange = function () {
          // the ready state tells us about the state of the XMLHttpRequest:
          //   0 = uninitialized
          //   1 = loading
          //   2 = loaded
          //   3 = interactive
          //   4 = complete
          // while the status shows us the numeric code returned by the server
          // so when the object is complete and the server returns status OK we
          // use the loadHandler to deal with the returned stuff
          if (httpRequest.readyState == 4) {
            TH_handleResponse(httpRequest.responseText);
          }
        };
      }

      // send off the request
      httpRequest.send(SOAPMsg);

      // handle response is asynchronous
      if (!requestAsynchronous) {
        if (httpRequest.readyState == 4) {
          TH_handleResponse(httpRequest.responseText);
        }
      }
    } catch (e) {
      alert('Error occurred: ' + e.message + '.');
    }
  }
}

function TH_getSOAPMessage(ticket) {
  return "<?xml version='1.0' encoding='utf-8'?>" +
         "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
         "<soap:Body>"+
         "<ValidateTicket xmlns='http://tempuri.org/'>" +
         "<ticketString>" +
         ticket +
         "</ticketString>"+
         "</ValidateTicket>"+
         "</soap:Body>"+
         "</soap:Envelope>";
}

function TH_handleResponse(xml) {
  var int1 = xml.indexOf('<isValid>');
  var int2 = xml.indexOf('</isValid>');
  var int3 = xml.indexOf('<errorMessage>');
  var int4 = xml.indexOf('</errorMessage>');

  if (DEBUG_ENABLED)
    document.form_debug.returnxml_debug.value = xml;

  TH_isValidTicket = xml.substring((int1 + 9), int2);

  if (TH_isValidTicket == "false") {
    TH_errorMessage = xml.substring((int3 + 14), int4);
  } else {
    TH_errorMessage = "";
  }
}

function TH_getErrorMessage() {
  return TH_errorMessage;
}

function TH_getValidTicket() {
  return TH_isValidTicket;
}


