//argObj contains  method("GET","POST"), queryString(string), postData(string), onSuccess (response), onFailure (status),
//responseType ("responseXML","responseText"(default))
// formName(string)
//quesryString can be used with either GET or POST
//when POST is sepcified, either postData or formName is mandatory
//when vars are to be passed via GET method, queryString must be specified as seperate parameter rather then appending vars to the url
//async true or false
function pAjaxCall(url,argObj)
{
    var xmlhttp=null;
	var me=this;
    this.url=url;
	for(prop in argObj){
		//alert(prop+" = "+argObj[prop]);
		this[prop]=argObj[prop];
	}

	function createRequest()
	{
		try{
            xmlhttp=new XMLHttpRequest();
		}
		catch(err){
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		if (!xmlhttp)
  		alert("Error initializing XMLHttpRequest!");
	}
	createRequest();
	function callback(){
		if(typeof me.responseType=="string"){
			var response=xmlhttp[me.responseType];
		}
		else{
			var response=xmlhttp.responseText;
		}
		//if onSuccess is not defined, typeof simply returns "undefined"
		if(typeof(me.onSuccess)=="function"){
			me.onSuccess(response);
		}		
	}
	function callbackf()
	{
        //alert("called back");
        if (xmlhttp.readyState == 4){
			if (xmlhttp.status == 200){
    			//alert("Server is done!");
				callback();
			}
			else{
       			alert("Error: status code is "+ xmlhttp.status+" "+ xmlhttp.statusText);
				if(typeof(me.onFailure)=="function"){
					me.onFailure(xmlhttp.status);
				}
			}
        	xmlhttp=null;
		}
	};

	function open(method){
        var _rand=Math.random();
		var glue;
		if(me.url.indexOf("?")==-1){
			glue="?";
		}
		else{
			glue="&";
		}
		if(me.async==undefined || me.async==true){
			me.async=true;
		}
		else{
			me.async=false;
		}
		if(typeof(me.queryString)=="string"){
   			xmlhttp.open(method,me.url+ glue + me.queryString+"&cache="+_rand,me.async);
		}
		else{
        	xmlhttp.open(method,me.url+ glue + "cache="+_rand,me.async);
		}
	}
	if(this.method=="GET"||this.method==undefined){
		open("GET");
		xmlhttp.send(null);
	}
	else{//for POST
		open("POST");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if(typeof(me.postData)=="string"){
    		xmlhttp.send(me.postData);
		}
		else if(typeof(me.formName)=="string"){
            var frm=document.forms[me.formName];
			if(typeof(frm)=="undefined"){
				alert("Error: wrong formName");
			}
			var str=pSerialiseForm(frm);
            xmlhttp.send(str);
		}
		else{
			alert("post type but no postData or formName specified!!");
		}
	}
	if(me.async==false){
		callback();
	}
	else{
		xmlhttp.onreadystatechange=callbackf;
	}
}

function pSerialiseForm(node){
	var frm=node;
	var str="";
	function addStr(name,value){
		if(name.length>0){
			if(str==""){
				str=name+"="+escape(value);
			}
			else{
				str+="&"+name+"="+escape(value);
			}
		}
	}
	for(var i=0;i<frm.elements.length;i++){
		var ele=frm.elements[i];
		var tag=ele.type.toLowerCase();	
		//alert(tag);
		if(tag=="radio"||tag=="checkbox"){
			if(ele.checked){
            	addStr(ele.name,ele.value);
			}
			//alert(ele.type);
		}
		else if(tag=="text"||tag=="textarea"||tag=="hidden"){
		    addStr(ele.name,ele.value);
		}
		else if(tag.indexOf("select")!=-1){//select-multiple or select-one
			for(var j=0;j<ele.options.length;j++){
				if(ele.options[j].selected){
					addStr(ele.name, ele.options[j].value);
				}
			}
			//addStr(ele.name, ele.options[ele.selectedIndex].value);
		}
	}
	return str;
}
