// declare a global  XMLHTTP Request object
var AjaxObj;
var dates = new Date();



// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateAjaxObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		AjaxObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) 
	{
		try
		{
			AjaxObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			AjaxObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!AjaxObj && typeof XMLHttpRequest != "undefined") 
	{
		AjaxObj = new XMLHttpRequest();
	}
	
}

var gcontentarea = '';
var gflag = '';
var gid = '';
var gcallback = undefined;

function ajaxgetxmlcontent(url, choice, id, contentarea, callback)
{
	gflag = choice;
	gid = id;
	gcontentarea = contentarea;
	gcallback = callback;
	//alert('URL : ' +url);
	//alert('choice ' +choice);
	//alert('id ' +id);
	//alert('contentarea obj ' +document.getElementById(gcontentarea).innerHTML);
	//alert('content area : '+gcontentarea);
	document.getElementById(gcontentarea).innerHTML = '&nbsp;<img src="/ifora/layouts/images/loading.gif"><font size=1 face=verdana>Loading...'; 

	var strTS = new Date().toString().replace(/(\s|\:|\+)/gi,"");
	var requestUrl = url+"?choice="+choice+"&filter="+id + "&strTS=" + strTS;
	//alert('URL is ' +requestUrl);
	
	CreateAjaxObj();
		
	if(AjaxObj) {
		// assign the StateChangeHandler function ( defined below in this file)
		// to be called when the state of the AjaxObj changes
		// receiving data back from the server is one such change
		AjaxObj.onreadystatechange = StateChangeHandler;

		// define the iteraction with the server -- true for as asynchronous.
		AjaxObj.open("GET", requestUrl, true);
		
		// send request to server, null arg  when using "GET"
		AjaxObj.send(null);		
		

		return false
	}		

	
	return false
}


function StateChangeHandler()
{
	var res;
	// state ==4 indicates receiving response data from server is completed
	if(AjaxObj.readyState == 4){
		// To make sure valid response is received from the server, 200 means response received is OK
		if(AjaxObj.status == 200){			
			//res=PopulateContent(AjaxObj.responseXML.documentElement, AjaxObj);
			document.getElementById(gcontentarea).innerHTML = AjaxObj.responseText; 
			/*if (gcallback != undefined){
				alert('before calledback ' + gcallback)
				eval(gcallback);
				alert('after calledback ' + gcallback)
			}*/
			//alert(AjaxObj.responseText)
		}
		else{
			alert("problem retrieving data from the server, status code: "  + AjaxObj.status);
		}
	}
	return false
}


function ShowLayer(id, area){
	//alert(id+' '+area);
	//alert(document.getElementById(id).innerHTML);
	document.getElementById(area).innerHTML = document.getElementById(id).innerHTML;
}

function HideLayer(id){
	document.getElementById(id).innerHTML = '';	
}

function getFormValues(obj,url,choice,filter,contentarea){
	/*alert(url);
	alert(choice);
	alert(filter);
	alert(id);*/
	  var getstr = "&";
      for (i=0; i<obj.elements.length; i++) {
		//alert(obj.elements[i].tagName);
         if (obj.elements[i].tagName == "INPUT") {
            if (obj.elements[i].type == "text") {
               getstr += obj.elements[i].name + "=" +encodeURIComponent(obj.elements[i].value)+ "&";
            }
			/*if (obj.elements[i].type == "file") {
				 var fileName = obj.elements[i].value;
    			  if (fileName.indexOf('/') > -1) { 
			    	  fileName = fileName.substring(fileName.lastIndexOf('/')+1, fileName.length);
			      }	else {
			    	  fileName = fileName.substring(fileName.lastIndexOf('\\')+1, fileName.length);
				   }			     
               getstr += obj.elements[i].name+"[name]"+"="+fileName+"&";
			   var type  = fileName.substring(fileName.lastIndexOf('.')+1, fileName.length);
			   getstr += obj.elements[i].name+ "[type]"+ "="+type+ "&";
			 //  getstr += obj.elements[i].name+ "[tmp_name]"+ "="+ajaxgetxmlcontent('/member/member.php','setFiles',fileName,'innerpagecontent');
			  // getstr +='&';
            }*/
			if (obj.elements[i].type == "hidden") {
               getstr += obj.elements[i].name + "=" +encodeURIComponent(obj.elements[i].value) + "&";
            }
            if (obj.elements[i].type == "checkbox") {
               if (obj.elements[i].checked) {
                  getstr += obj.elements[i].name + "=" + encodeURIComponent(obj.elements[i].value) + "&";
               } else {
                  getstr += obj.elements[i].name + "=&";
               }
            }
            if (obj.elements[i].type == "radio") {
               if (obj.elements[i].checked) {
                  getstr += obj.elements[i].name + "=" + encodeURIComponent(obj.elements[i].value) + "&";
               }
            }
         }   
         if (obj.elements[i].tagName == "SELECT") {
            var sel = obj.elements[i];
			 for (j=0; j<sel.length; j++) {
				 if(sel.options[j].selected == true) {						
		            getstr += sel.name + "=" + encodeURIComponent(sel.options[j].value) + "&";
				 }
			 }
         }
		 if (obj.elements[i].tagName == "TEXTAREA") {
                getstr += obj.elements[i].name + "=" + encodeURIComponent(obj.elements[i].value) + "&";
           }
         
      }

	 // alert(getstr);
	  ajaxgetxmlcontent(url,choice+getstr,filter,contentarea);
  }

/*if (navigator.userAgent.indexOf("Opera")!=-1
    && document.getElementById) type="OP";
if (document.all) type="IE";
if (document.layers) type="NN";
if (!document.all && document.getElementById) type="MO";

gstateid = '';
gcityid = '';
gcategoryid = '';
gsubcategoryid = '';

function ShowLayer(id, area){
	document.getElementById(area).innerHTML = document.getElementById(id).innerHTML;
}

function setCityId(cityid){
	gcityid = cityid;
}

function setStateId(stateid){
	gstateid = stateid;
}

function setCategoryId(categoryid){
	//alert(categoryid)
	gcategoryid = categoryid;
}

function setSubcategoryId(subcategoryid){
	//alert(subcategoryid)
	gsubcategoryid = subcategoryid;
}

function getAjaxLayer(){
	var callback="ShowLayer('CategoryLayer" + gcategoryid + "', 'DataLayer');" 
	ajaxgetxmlcontent('postAdvertisement', '&stateid=' + gstateid + '&cityid=' + gcityid + 
					'&categoryid='+gcategoryid + '&subcategoryid=' + gsubcategoryid, 'advertisement', callback)
}*/
// {/literal}