/**
*author-Tithira Hiranjith Perera
*Copyright - Enziz Software Solutions 
*/
Ajax = {
    http: "",
    container: "",
	rtnval:"",
	html_path:"",
	val:"",
	array:null,
	i:0,
	url:"",
    makehttpObj: function(){
        var http = false;
        if (window.XMLHttpRequest) {
            http = new XMLHttpRequest();
        }
        else 
            if (window.ActiveXObject) {
                http = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else {
                alert("Your browser doesn't support Ajax");
            }
        Ajax.http = http;
    },    
    handleResponse: function(){ 
	var browser=navigator.appName;
	if(browser=="Microsoft Internet Explorer"){
		if(Ajax.http.readyState == 4) {
            var response = Ajax.http.responseText;			
            document.getElementById(Ajax.container).innerHTML = response;
        }
        else {
            document.getElementById(Ajax.container).innerHTML = 'Loading ...<img src="'+Ajax.html_path+'/images/load.gif" width="22" height="22" />';
        }
	}else{
		if(Ajax.http.status == 200 && Ajax.http.readyState == 4) {
            var response = Ajax.http.responseText;			
            document.getElementById(Ajax.container).innerHTML = response;
        }
        else {
            document.getElementById(Ajax.container).innerHTML = 'Loading ...<img src="'+Ajax.html_path+'/images/load.gif" width="22" height="22" />';
        }
	}
        
    },    
    makeConnection: function(url){
        var sid = "&sid=" + Math.random();
        Ajax.http.open('POST',  Ajax.html_path+'Ajax.php');
        Ajax.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        Ajax.http.onreadystatechange = Ajax.handleResponse;
        Ajax.http.send(url + sid);
    },	
	setGo : function(url,container,path){
		Ajax.container = container;
		Ajax.html_path=path;
		Ajax.makehttpObj();
		Ajax.makeConnection(url);
		
	},	
	handleResponseLogin: function(){
		if (Ajax.http.status == 200 && Ajax.http.readyState==4) {
			document.getElementById("ErrorMessage").innerHTML='<img src="'+Ajax.html_path+'/images/load.gif" width="22" height="22" />';
			var tmp =Ajax.http.responseText;
			Ajax.val= Ajax.http.responseText;
			if(Ajax.val=="ok"){				
				//document.getElementById("ErrorMessage").innerHTML='*'+Ajax.http.responseText+'*';
				//document.getElementById("val").value="ok";
				Ajax.val="ok";
			}else{
				document.getElementById("ErrorMessage").innerHTML=Ajax.http.responseText;
				Ajax.val="";
			}
		}else{
			document.getElementById("ErrorMessage").innerHTML= '<img src="'+Ajax.html_path+'/images/load.gif" width="22" height="22" />';
		}        
    },    
    makeConnectionLogin: function(url){
        var sid = "&sid=" + Math.random();
        Ajax.http.open('POST', Ajax.html_path+'Ajax.php');
        Ajax.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        Ajax.http.onreadystatechange = Ajax.handleResponseLogin;
        Ajax.http.send(url + sid);
    },
	handleResponseSignUp: function(){
		if (Ajax.http.status == 200 && Ajax.http.readyState==4) {
			document.getElementById("ErrorMessage").innerHTML='<img src="images/load.gif" width="22" height="22" />';
			var tmp =Ajax.http.responseText;
			Ajax.val= Ajax.http.responseText;
			if(Ajax.val=="ok"){				
				//document.getElementById("ErrorMessage").innerHTML='*'+Ajax.http.responseText+'*';
				//document.getElementById("val").value="ok";
				document.forms["signup"].submit();
			}else{
				document.getElementById("ErrorMessage").innerHTML=Ajax.http.responseText;
				Ajax.val="";
			}
		}else{
			document.getElementById("ErrorMessage").innerHTML= '<img src="images/load.gif" width="22" height="22" />';
		}        
    },	
	makeConnectionSignUp: function(url){
        var sid = "&sid=" + Math.random();
        Ajax.http.open('POST','Ajax.php');
        Ajax.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        Ajax.http.onreadystatechange = Ajax.handleResponseSignUp;
        Ajax.http.send(url + sid);
    },	
	setGoRtn: function(url,path){
    	Ajax.html_path=path;
		Ajax.makehttpObj();
		Ajax.makeConnectionLogin(url);
	},	
	signup: function(url){
		Ajax.makehttpObj();
		Ajax.makeConnectionSignUp(url);
	},	
	handleComment: function(){
		if (Ajax.http.status == 200 && Ajax.http.readyState==4) {
			document.getElementById("Comments").innerHTML='<img src="images/load.gif" width="22" height="22" />';
			var tmp =Ajax.http.responseText;
			document.getElementById('Comments').innerHTML = tmp;
		}else{
			document.getElementById("Comments").innerHTML= '<img src="images/load.gif" width="22" height="22" />';
		}        
    },
	makeComment: function(url){
        var sid = "&sid=" + Math.random();
        Ajax.http.open('POST','Ajax.php');
        Ajax.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        Ajax.http.onreadystatechange = Ajax.handleComment;
        Ajax.http.send(url + sid);
    },	
	addCom: function(url){
		Ajax.makehttpObj();
		Ajax.makeComment(url);
	},
	handleResponseTwo: function(){ 
	var browser=navigator.appName;
	if(browser=="Microsoft Internet Explorer"){
		if(Ajax.http.readyState == 4) {
            var response = Ajax.http.responseText;
			var JSONobject = JSON.parse(response);
			for(var i=0;i<JSONobject.length;i++){
				document.getElementById(JSONobject[i].id).innerHTML = JSONobject[i].pager;
			}
        }
        else {
            //document.getElementById(Ajax.array[Ajax.i].id).innerHTML = 'Loading ...<img src="'+Ajax.html_path+'/images/load.gif" width="22" height="22" />';
        }
	}else{
		if(Ajax.http.status == 200 && Ajax.http.readyState == 4) {
            var response = Ajax.http.responseText;
			var JSONobject = JSON.parse(response);
			for(var i=0;i<JSONobject.length;i++){
				document.getElementById(JSONobject[i].id).innerHTML = JSONobject[i].pager;
			}
            //document.getElementById('test').innerHTML = response;			
        }
        else {
           // document.getElementById(Ajax.array[Ajax.i].id).innerHTML = 'Loading ...<img src="'+Ajax.html_path+'/images/load.gif" width="22" height="22" />';
        }
	}
        
    },    
    makeConnectionTwo: function(url){
		
        var sid = "&sid=" + Math.random();
        Ajax.http.open('POST',  Ajax.html_path+'Ajax.php');
        Ajax.http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        Ajax.http.onreadystatechange = Ajax.handleResponseTwo;
		Ajax.http.send(url+sid);
       
    },	
	setGoTwo : function(url,path){		
		Ajax.html_path=path;
		Ajax.url = url;
		Ajax.makehttpObj();
		Ajax.makeConnectionTwo(url);
		
	}
};

