var ie   = document.all;
var ns6  = ( document.getElementById && !document.all );
var httpReq = false;
var emailAddr = "ergoff@mail.bigpond.com";
var theDocObj = false;
var theFormObj = false;
var theDoneFunc = false;
var req = false;


function setFormObj( doc, frm, pfn )
{
   theDocObj = doc;
   theFormObj = frm;
   theDoneFunc = pfn
   return false;
}


function postEmailData()
{
   if ( !theDocObj || !theFormObj || !theDoneFunc )
   {
      return false;
   }

   if ( !validateAll() )
   {
      return false;
   }

   var params = getEntries();
   makePOSTRequest( "postEmailData.php", params, mailSent )
   return true;
}


function makePOSTRequest( url, params, cbResponse )
{
   req = new getXmlHttpRequestObject();
   req.onreadystatechange = cbResponse;
   req.open( 'POST', url, true );
   req.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
   req.setRequestHeader( "Content-length", params.length );
   req.setRequestHeader( "Connection", "close" );
   req.send( params );
}


function mailSent()
{
   if ( theDocObj && req && req.readyState == 4 )
   {
      if ( req.status == 200 )
      {
         var rslt = req.responseText;
         theDoneFunc( rslt );
      }
      else
      {
         alert( 'There was a problem with the request.' );
      }
   }
}


function getXmlHttpRequestObject()
{
   var xmlHttpReq = false;
   /*@cc_on
   @if ( @_jscript_version >= 5 )
      try
      {
          xmlHttpReq = new ActiveXObject( "Msxml2.XMLHTTP" );
      }
      catch( e )
      {
         try
         {
            xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch( E )
         {
            xmlHttpReq = false;
         }
      }
   @else
      xmlHttpReq = false;
   @end @*/

   if ( !xmlHttpReq && typeof XMLHttpRequest != 'undefined' )
   {
      try
      {
         xmlHttpReq = new XMLHttpRequest();
      }
      catch( e )
      {
         xmlHttpReq = false;
      }
   }
   return xmlHttpReq;
}


function validateAll()
{
   if ( !theFormObj )
   {
      return false;
   }

   i = 0;
   var nameStr = new String( theFormObj.name.value );
   if ( nameStr.length <= 0 )
   {
      ++i;
      alert( "You must enter a valid name" );
   }
   var phoneStr = new String( theFormObj.phone.value );
   if ( phoneStr.length <= 0 )
   {
      ++i;
      alert( "You must enter a valid phone" );
   }
   var emailChk = checkEmail( theFormObj.email.value );
   if ( emailChk != 0 )
   {
      ++i;
      alert( "You must enter a valid email address" );
   }
   var cRegoStr = new String( theFormObj.carRego.value );
   if ( cRegoStr.length <= 0 )
   {
      ++i;
      alert( "You must enter a valid Car Rego" );
   }
   if ( i > 0 )
   {  // Missing some of the required data
      return false;
   }

   // Everything checks out
   return true;
}


function getEntries()
{
   var str = new String();
   if ( theFormObj )
   {
      str += "subject="       + encodeURI( theFormObj.subject.value );
      str += "&name="         + encodeURI( theFormObj.name.value );
      str += "&phone="        + encodeURI( theFormObj.phone.value );
      str += "&email="        + encodeURI( theFormObj.email.value );
      str += "&carRego="      + encodeURI( theFormObj.carRego.value );
      str += "&carMake="      + encodeURI( theFormObj.carMake.value );
      str += "&carModel="     + encodeURI( theFormObj.carModel.value );
      str += "&carYear="      + encodeURI( theFormObj.carYear.value );
      str += "&cylinders="    + parseInt( theFormObj.cylinders.value );
      str += "&speedometer="  + encodeURI( theFormObj.speedometer.value );
      var val = new String( getTransmissionSel() );
      if ( val.length > 0 )
      {
         str += "&transmission=" + encodeURI( val );
      }
      str += "&comments=" + encodeURI( theFormObj.comments.value );
   }
   return str;
}


function getTransmissionSel()
{
   var str = "";
   var obj = ( !theFormObj )  ?  null : theFormObj.transmission;
   if ( obj == null )
   {
      return str;
   }

   var len = obj.length;
   if ( len == undefined )
   {
      str = ( obj.checked )  ?  obj.value : "";
   }
   else
   {
      for( var i = 0; i < len; ++i )
      {
         if ( obj[i].checked )
         {
            str = obj[i].value;
            break;
         }
      }
   }
   return str;
}


function getEmailAddress()
{
   return emailAddr;
}


function checkEmail( str )
{
   var emailStr = new String( str );
   if ( emailStr.length <= 0 )
   {
      return 1;
   }

   var emailArr = emailStr.split( "@" );
   if ( emailArr.length != 2 )
   {
      return 2;
   }

   var person = new String( emailArr[0] );
   var domain = new String( emailArr[1] );
   if ( person.length <= 0 || domain.length <= 0 )
   {
      return 3;
   }

   var domainArr = domain.split( "." );
   var len = domainArr.length;
   if ( len < 2 )
   {
      return 4;
   }

   var i;
   for( i = 0; i < len; ++i )
   {
      var str = new String( domainArr[i] );
      if ( str.length <= 0 )
      {
         return 5;
      }
   }

   return 0;
}


