// JavaScript Document
var ajaxAddItin = function(imgroot,recid,itype){
	initItin();
	ajaxAddItin(imgroot,recid,itype);
}

//Initialize the additin function to choose between jquery or prototye version
function initItin(){
	if(typeof(window['Ajax']) != 'undefined' && Ajax.Request){
		ajaxAddItin = ajaxAddItin_prototype;
	}
	else{
		ajaxAddItin = ajaxAddItin_jquery;
	}
}

//Handle Failures
function handlerFailure(response){
	alert('We were unable to comply with your requrest.  Please reload the page and try again');
}


/*Begin JQuery Functions*/

//JQuery version - do not call this version directly all calls should be made through ajaxAddITin
function ajaxAddItin_jquery(imgroot,recid,itype)
{
	var thisUrl  = imgroot+'/itinerary/ajaxAddItin.cfm';
	var thisData = 'type=' + itype + '&recid='+recid;
	//alert("here1");
	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
			if(itype == 1){
				$('#itin_' + recid).html('<a href="'+imgroot+'/TripBuilder/index.cfm"  class="vwItin" ><span title="Added to Itinerary"></span></a>');
				$('#itin_' + recid).removeClass('itineraryAdd');
				$('#itin_' + recid).addClass('itineraryAdded');
			}
		//Calendar
			else{
				$('#itin_' + recid).html('<a href="'+imgroot+'/TripBuilder/index.cfm"  class="vwItin"  ><span title="Added to Itinerary"></span></a>');
				$('#itin_' + recid).removeClass('itineraryAdd');
				$('#itin_' + recid).addClass('itineraryAdded');
			}
			return true;
		},
	   	onFailure: function (response){
			document.getElementById('itin_' + recid).innerHTML = response.responseText;	
		}
	 });
}
/*End Jquery Functions*/


/*Begin Prototype Functions*/

//Prototype version - *****do not call this version directly all calls should be made through ajaxAddItin***
function ajaxAddItin_prototype(imgroot,recid,itype)
{
	var thisUrl  = imgroot+'/itinerary/ajaxAddItin.cfm';
	var thisData = 'type=' + itype + '&recid='+recid; 
	new Ajax.Request(thisUrl,{
		method: 'post',
		parameters: thisData,
		onSuccess: function (response){
			if(itype == 1){

				$P('itin_' + recid).innerHTML = ('<a href="'+imgroot+'/TripBuilder/index.cfm" class="vwItin" ><span title="Added to Itinerary"></span></a>');
				$P('itin_' + recid).removeClassName('itineraryAdd');
				$P('itin_' + recid).addClassName('itineraryAdded');
				document.getElementById('itinerary').innerHTML = response.responseText;	
			}
		//Calendar
			else{
				$P('itin_' + recid).innerHTML = ('<a href="'+imgroot+'/TripBuilder/index.cfm"  class="vwItin" ><span title="Added to Itinerary"></span></a>');
				$P('itin_' + recid).removeClassName('itineraryAdd');
				$P('itin_' + recid).addClassName('itineraryAdded');
				document.getElementById('itinerary').innerHTML = response.responseText;	
			}
			return true;
		},
		onFailure: function (response){
			document.getElementById('itin_' + recid).innerHTML = response.responseText;	
		}
 });
}
/*End Prototype Functions*/

function removeEventFromItinAjax(imgroot,eventid)
{
	var thisUrl  = imgroot+'/includes/itinerary/index.cfm';
	var thisData = 'action=ajax_removeEventItin&eventid='+eventid;
	

	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
		   	try{
				try{
				$P('itin_' + eventid).removeClassName('itineraryAdded');
				$P('itin_' + eventid).addClassName('itineraryAdd');
				//$P('itinEvent_' + eventid).html('add to itinerary');
				document.getElementById('itin_' + eventid).innerHTML = '<a href="javascript:ajaxAddItin(\''+imgroot+'\', '+eventid+', 2)"></a>';
				}catch(e){}
				document.getElementById('itinerary').innerHTML = response;
					
			}catch(e){}
				return true;
			},
	   error: function(response){	
			  document.getElementById('itinerary').innerHTML = response.responseText;
	   }
	 });
}

function removeListingFromItinAjax(imgroot,listingid)
{
	var thisUrl  = imgroot+'/includes/itinerary/index.cfm';
	var thisData = 'action=ajax_removeItin&listingid='+listingid;

	$.ajax({
	   type: "POST",
	   url: thisUrl,
	   data: thisData,
	   success: function (response){
		   		try{
					try{
					$P('itin_' + listingid).removeClassName('itineraryAdded');
					$P('itin_' + listingid).addClassName('itineraryAdd');
					document.getElementById('itin_' + listingid).innerHTML = '<a href="javascript:ajaxAddItin(\''+imgroot+'\', '+listingid+', 1);"></a>';
				//	$P('itin_' + listingid).html('add to itinerary');
					}catch(e){}
					document.getElementById('itinerary').innerHTML = response;
				}catch(e){}
				return true;
			},
	   error: function(response){	
			  document.getElementById('itinerary').innerHTML = response.responseText;
	   }
	 });
}