/***************************************

AUTHOR: Supriya

CLASS NAME: ajax

PROPERTIES: obj (you can set it to any html object at the time of sending request for using after getting response from server)

FUNCTIONS: callurl(url, func)
This function requests a page asynchronously. 'url' specifies the url to call and 'func' is used to handle response from the page.

HOW TO USE:
1. ajax.callurl('example.php?param=value', 'example');
2. Edit `handleResponse` function of this class and add this case label in switch block. See it in the function

***************************************/
var ajax = {
	func:null, xobj:null, obj:null,
	getXMLObject : function(){
		try{ this.xobj = new XMLHttpRequest(); }
		catch(e){ 
			try{ this.xobj = new ActiveXObject("Msxml2.XMLHTTP"); }
			catch(e){
				try{ this.xobj = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch(e){ alert("Your browser doesn't support AJAX"); }
			}
		}
	},

    callurl : function(url, func, postdata){
        this.getXMLObject();
        this.func = func;
        this.xobj.onreadystatechange = this.handleResponse;
        url += "&r=" + Math.random();
        if(postdata){
	        this.xobj.open("POST", url, true);
          this.xobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	        this.xobj.send(postdata);
        }
        else{
        	this.xobj.open("GET", url, true);
	        this.xobj.send(null);
        }
    },

    handleResponse : function(){
      if(ajax.xobj.readyState == 4 || ajax.xobj.readyState == "complete"){
	      var response = ajax.xobj.responseText; //content returned by the server
		  //alert(response);
	      switch(ajax.func){
				case "lia":
					if(parseInt(response) == 0){
								with(ajax.obj.style){
									textDecoration = "none";
										color = "green";
								}
						ajax.obj.innerHTML = "Login ID Available";
							}
							else{
								with(ajax.obj.style){
										textDecoration = "none";
										color = "red";
								}
								ajax.obj.innerHTML = "<br> Login ID Not-available";
							}
					break;
					
					case "chk":					
						with(ajax.obj.style){
								textDecoration = "none";
								color = "red";
								fontSize = "12px";
						}
						ajax.obj.innerHTML = response;
						ajax.obj1.value = response;
						
					break;
				 }
       } 
    }
}
