﻿         
/** A AJAXConnection class  */
function AJAXConnection(name) {    
    this.className = 'AJAXConnection';
    //alert(this.className + ' ' + name);
    
    /** Default construtor
     *
     * name - div name
     */
    {    
        this.name = name;
    }

    this.xmlhttpPost = function (pageName,parameter, functionObj) {
        var xmlHttpReq = false;
        var self = this;
        var strURL = pageName;
        
        if(parameter !="")
         {
            strURL = strURL +"?"+parameter;
        }
        
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
            if (self.xmlHttpReq.overrideMimeType) {
                self.xmlHttpReq.overrideMimeType('text/xml');
                // See note below about this line
            }
        // IE
        } else if (window.ActiveXObject) { // IE
            try {
                self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
            }
        }
        if (!self.xmlHttpReq) {
           var error = "<div id=\"dialog\" title=\"Unsupported Browser\" style=\"border:1px solid #aaaaaa\"><p>Browser do not support Javascript</p></div>";
                 $('form').append(error);
                 $.ui.dialog.defaults.bgiframe = true;
	 	         $("#dialog").dialog({ resizable: false,buttons: {
				OK: function() {
					$(this).dialog('close');}},
close: function(){$('#dialog').remove()}});  
            return false;
        }    
        self.xmlHttpReq.open('GET', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type',
            'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() { 
            _callBackFunction(self.xmlHttpReq, functionObj); 
        };
        self.xmlHttpReq.send("");
    }
    
    _callBackFunction = function (http_request, functionObj) {
        var error;
           
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                $('#dialog').remove();
                functionObj.callBackFunction(http_request.responseText);
                
            } else {
            
               if(document.getElementById('dialog')==null){
                 error = "<div id=\"dialog\" title=\"Connection Error\" ><p>AJAX failed to establish connection with the server. Please check your internet connection and refresh the page</p></div>";
                 $('form').append(error);
                 $.ui.dialog.defaults.bgiframe = true;
	 	         $("#dialog").dialog({ resizable: false, buttons: {
				Ok: function() {
					$(this).dialog('close');}},close: function(){$('#dialog').remove()}});   
		             
                }
            }
        }
        else
        {   
            functionObj.callBackFunction("<img class=\"loading-gif\" src = \"./JQuery/images/lightbox-ico-loading.gif\"/>");
	    }
	
	}
}

function Testimonial() {	
    this.className = 'Testimonial';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(strResult) 
    {
       if(strResult!=null){

         document.getElementById('testimonialContent').innerHTML = strResult;
       }
     
    }    
}

function News() {	
    this.className = 'News';
    
    /** Call Back Function - called by AJAXAdaptor
     *
     * str - string from XMLHttpRequest
     */    
    this.callBackFunction = function(strResult) {
     $('#NewsContent > img.loading-gif').remove();
     if(strResult!= ""){
       
        $('#NewsContent').append(strResult);  
        updateStatus();
        }
    else{
            
        }
    }    
}

function Poll()
{
    this.className = 'Poll';

    this.callBackFunction = function(strResult)
    {
        $('#LeftBox_Content > img.loading-gif').remove();
        if(strResult!= "")
        {
               $('#LeftBox_Content').append(strResult);  
        }
     }
}

function CastVote()
{
    this.className = 'CastVote';

    this.callBackFunction = function(strResult)
    {
        $('#LeftBox_Content > img.loading-gif').remove();
        if(strResult!= "")
        {           
               $('#LeftBox_Content').text("");  
               $('#LeftBox_Content').append(strResult);
        }
     }
}

function updateStatus(){  
    //Show number of loaded items  
    $('#NewsStatus').text('Loaded '+$('div.newsText').length + ' items');  
    }
      
        
        
        