// Rivisat_ajax.js
//  Created on Mar 30, 2007
// @author     Habes Alkhraisat <hma@jensentechnologies.com>

node_array=  new Array();
label_array= new Array();
errorflag=0;


var RivistaAjax = {
	result:false,
	url:"/core/ajaxserver.php?sid=" + new Date().getTime(),
	ER:"We're so sorry, the server is busy right now please try again",	
	iconon_array :new Array(),
	iconoff_array: new Array(),
	asynchronous:false,
	doAjaxCall:function(params,method,callback){
		var myAjax = new Ajax.Request(
      	this.url,
      	{
        method: method,
        parameters: params,
        asynchronous:this.asynchronous,
        setrequestheaders:['Cache-Control: no-store, no-cache, must-revalidate','Accept-Encoding: compress, gzip'],
        onfailure:function(){alert(this.ER);},
        onComplete: callback
      });
	},
	doToggleField: function(node,params,label,iconon,iconoff) {
	label_array.push(label);
	node_array.push(node);
	params = "req=toggle&"+params;
	RivistaAjax.iconon_array.push(iconon);
	RivistaAjax.iconoff_array.push(iconoff);
	node.childNodes[0].src = "/admin/media/images/ajax-loader.gif";
	RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.doToggleField_handler);
	},
	doToggleField_handler:function(transport){
		if (transport.readyState == 4 && transport.status == 200){
			if(transport.responseText){
   		 	node_t = node_array.shift();
 			iconon=RivistaAjax.iconon_array.shift();
			iconoff=RivistaAjax.iconoff_array.shift();
			label_t = label_array.shift().split('|');
	        	if(transport.responseText.trim()=='f'){
				node_t.childNodes[0].src = "/admin/media/images/red-button.gif";
				node_t.childNodes[0].alt= label_t[0];
				node_t.childNodes[0].title= label_t[0];
				RivistaAjax.changeAllRelated(node_t.childNodes[0].src,node_t.childNodes[0].id,label_t[0]);
			} else{
				node_t.childNodes[0].src = "/admin/media/images/green-button.gif";
				node_t.childNodes[0].alt= label_t[1];
				node_t.childNodes[0].title= label_t[1];
				RivistaAjax.changeAllRelated(node_t.childNodes[0].src,node_t.childNodes[0].id,label_t[1]);
				}
			}
		}
},
	changeAllRelated:function(imgsrc,id,n_label){
  			var imgs;
		  	imgs =document.getElementsByTagName('img');
		  	for (i=0;i<imgs.length;i++)
				if(imgs[i].id==id && !(id=='')){
					imgs[i].src = imgsrc;
					imgs[i].alt=n_label;
					imgs[i].title=n_label;
				}
		},
	increaseViews: function(params,id) {
			params = "req=increaseviews&field="+params+"&id="+id;
			RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.increaseViews_handler);
  		},
  	increaseViews_handler:function(transport){},


  		checkEmail:function(fld,id) {
	  	    errorflag=0;
			if($F(fld)=='' || $F(fld).trimAll=='') {

				alert('The Email field cannot be left blank.');
		     	        errorflag=1;
				return false;
			}
	   		params = "req=checkEmail&email="+escape($F(fld))+"&id="+id;
	   		RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.checkEmail_handler);
  		},


  		checkEmail_handler:function(transport){
        	if(transport.responseText.trim()=='f'){
        		alert('The email address you specified already exists.');
				fld.focus();
				errorflag=1;
				} else{
					alert('The email address seems to be ok!');
					return;
				}
  		},
  	checkScreenname:function(fld,id) {
  	    errorflag=0;
		if($F(fld)=='' || $F(fld).trimAll==''){
			return true;
		}
		params = "req=checkScreenname&screenname="+escape($F(fld))+"&id="+id;
		RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.checkScreenname_handler);
  		},
  	checkScreenname_handler:function(transport){
  		if(transport.responseText){
       	if(transport.responseText=='f'){
       			alert('That screen name is in use.  Please choose another.');
		  	    errorflag=1;
			fld.focus();
			} else{
				alert('That screen name seems ok!');
				return;
			}
  		}
  	},	
  	getGeobaseListings:function(geobaseid,search,initOptions) {
   		params = "req=getGeobaseListings&geobaseid="+escape(geobaseid)+'&search='+escape(search);
	   	var options = initOptions || {};
    	if(options.zipcodes) {
    	    var zipcodes = options.zipcodes.replace(/[^0-9,]/g,""); //strip out text
            if(zipcodes) params = params + "&zipcodes=" + escape(zipcodes);
        }
		RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.getGeobaseListings_handler);
  	},
  	getGeobaseListings_handler:function(transport){
		if(transport.responseText){
			// incoming data is json encoded
			//geobasedata=transport.responseText;
			if(window.listingscallback)
				listingscallback(transport.responseText);
        }
	},
	getGeobaseListing:function(listingid) {
   		params = "req=getGeobaseListing&listingid="+escape(listingid);
   		RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.getGeobaseListing_handler);
	},
	getGeobaseListing_handler:function(transport){
		if(transport.responseText){
			// incoming data is json encoded
			//geobasedata=transport.responseText;
			if(window.getGeobaseListingCallback)
				getGeobaseListingCallback(transport.responseText);
		}
	},
	opsCalendarLoadEvent:function(eventId,calendarId){
		if(RivistaAjax.doShowHideEvent(eventId,calendarId)) return;
		params = "req=getCalendarEvent&eventid="+escape(eventId)+"&calendarid="+escape(calendarId);
		$('eventContainer').hide();
		$('ajaxloader').show();
   		RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.opsCalendarLoadEvent_handler);
	},
	opsCalendarLoadEvent_handler:function(transport){
		if(transport.responseText){
			$('ajaxloader').hide();
			new Insertion.After("eventContainer",transport.responseText);
		}
	},
	doShowHideEvent:function(id){
		Effect.ScrollTo('calendar',{offset: -15});
		if($('event_'+id)!=null) 
			if($('event_'+id).visible()){
				$('eventContainer').show();
				$('event_'+id).hide();
				return true;
			}else {
				$('eventContainer').hide();
				$('event_'+id).show();
				return true;
			}
		return false;		
	},
	loadCalendar:function(m,y,d,calId,view,filter){
		$('calendar').select('.event').each(function (element){element.hide();});
		params ='req=getCalendar&view='+view+'&m='+m+'&y='+y+"&d="+d+"&calid="+escape(calId)+"&categoryfilter="+escape(filter);
		if(view.include('mini_view')) $('mini_view').replace("<div id='mini_view'></div>");
		$('eventContainer').hide();
		$('ajaxloader').show();
   		RivistaAjax.doAjaxCall(params,"GET",RivistaAjax.loadCalendar_handler);
	},
	loadCalendar_handler:function(transport){
		if(transport.responseText){
			if($('mini_view')!=null && $('mini_view').innerHTML !='')
				$('eventContainer').update(transport.responseText);
			else
			$('calendar').replace(transport.responseText);
			
			$('ajaxloader').hide();
			$('eventContainer').show();
			
		}
	},
	doSwitch:function(obj,container,newClass,classStyle){
		$(container).select('.'+classStyle).each(function (element){element.removeClassName(classStyle);});
		obj.className=classStyle;
	}
}
