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

AUTHOR: Pawan Tripathi (pawantri@gmail.com)

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,
	obj1:null,
	obl2: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
				switch(ajax.func){
				case 'zn':
					populateList(ajax.obj, response);
				break;
				//case 'cat':
						//populateList(ajax.obj, response);
				//break;
				case 'logIn':
				 	if(response == 1)
						location.href = 'welcome.php';
					else if(response == 0)
						document.getElementById('error_msg').innerHTML = "Invalid user name/password or your acoount is not active!";
					else
					 document.getElementById('error_msg').innerHTML = "Please Enter ("+response+") Email Id";
					break;

				case "lia":
					if(parseInt(response) == 0){
						with(ajax.obj.style){
							textDecoration = "none";
							color = "green";
						}
						ajax.obj.innerHTML = "Email ID Available";
					}
					else
					{
						with(ajax.obj.style){
							textDecoration = "none";
							color = "red";
						}
						ajax.obj.innerHTML = "Email ID Not-available";
					}
				break;
				
				case "liap":
					if(parseInt(response) == 0){
							with(ajax.obj.style){
								textDecoration = "none";
								color = "green";
							}
							ajax.obj.innerHTML = "Available";
							ajax.obj1.style.display = 'none';
							ajax.obj2.style.display = '';
						}
						else
						{
							with(ajax.obj.style){
								textDecoration = "none";
								color = "red";
							}
							ajax.obj.innerHTML = "Not-available";
						}
				break;

				case "us":
					populateList(ajax.obj, response);
				break;
				
				case "shwPage": 
				  
				  ajax.obj.innerHTML = response;
				
				break;
				
			}
       	} 
    }
}