var Ajax = {};

Ajax.callAttempt = new Array();

Ajax.call = function(_url, _type, _data, _successFunc)
{
    if(Ajax.callAttempt[_url] == null)
        Ajax.callAttempt[_url] = 1;
    
    // must attach random number to prevent IE from caching AJAX responses
    if(_data == '')
        _data = 'rand=' + (Math.random()*999999999);
    else
        _data = _data + '&rand=' + (Math.random()*999999999);
        
    
    $.ajax({
        url: _url,
        type: _type,
        data: _data,
        error: function(reqobj, errType, exceptionObj)
        {
            //Ajax.callAttempt[_url]++;
            
            //if(Ajax.callAttempt[_url] > 3)
            //{
                alert('Failed to make AJAX call.\r\nStatus = ' + reqobj.statusText);
            //}
            //else
            //{
                // retry
            //    Ajax.call(_url, _type, _data, _successFunc);
            //}
        },
        
        success: function(xml)
        {        
            var rc = $('rc', xml).text();
            var msg = $('message', xml).text();
            
            if(rc == "-1")
            {
                window.location = "default.html";
                return;
            }
        
            Ajax.callAttempt[_url] = 1;
            _successFunc(xml);
        }
    });
}

Ajax.submitForm = function(servlet, theForm, successFunc)
{    
   var boundaryString = 'AaB03x';
   var boundary = '--' + boundaryString;

   var requestBody = new Array();
   
   requestBody.push(boundary);
   
   for (var i=0; i<theForm.length; i++)
   {
     requestBody.push('Content-Disposition: form-data; name="' + theForm.elements[i].name + '"');  
     requestBody.push('');
     requestBody.push(theForm.elements[i].value);
     requestBody.push(boundary);
   }

   var reqBodyText = requestBody.join('\r\n');
   
    $.ajax({
        url: servlet,
        type: 'POST',
        contentType: 'multipart/form-data; charset=UTF-8; boundary=' + boundaryString,
        data: reqBodyText,
        error: function(reqobj, errType, exceptionObj){
            alert('Failed to submit form data.\r\nStatus = ' + reqobj.statusText);
        },
        
        success: function(xml)
        { 
            if(successFunc != null)
                successFunc(xml);
        }
        
        });
}


// deprecated
function ajaxSubmitForm(servlet, theForm, extraFunc)
{    
   var boundaryString = 'AaB03x';
   var boundary = '--' + boundaryString;

   var requestBody = new Array();
   
   requestBody.push(boundary);
   
   for (var i=0; i<theForm.length; i++)
   {
     requestBody.push('Content-Disposition: form-data; name="' + theForm.elements[i].name + '"');  
     requestBody.push('');
     requestBody.push(theForm.elements[i].value);
     requestBody.push(boundary);
   }

   var reqBodyText = requestBody.join('\r\n');   
   
    $.ajax({
        url: servlet,
        type: 'POST',
        contentType: 'multipart/form-data; charset=UTF-8; boundary=' + boundaryString + '',
        data: reqBodyText,
        error: function(reqobj, errType, exceptionObj){
            alert('Failed to submit form data.\r\nStatus = ' + reqobj.statusText);
            // displayHTMLContent(reqobj.responseText);
        },
        
        success: function(xml)
        {            
            if(extraFunc != null)
                extraFunc();
        }
        
        });
}

