//******************************************** VARIABLES ***************************************************
var ajaxUrl = '/assets/ajax/';

//******************************************************************************************************
// 	             						   CALENDAR FUNCTIONS
//****************************************************************************************************** 
//***************************************************
// 	             	Clear Event
//***************************************************
function clearEvent() {
	var titleBox		= document.getElementById("eventTitle").value = '';
	var startdateBox	= document.getElementById("eventStartDate").value = '';
	var enddateBox		= document.getElementById("eventEndDate").value = '';
	var descriptionBox	= document.getElementById("eventDescription").value = '';
	var keywordsBox		= document.getElementById("eventKeywords").value = '';
	var eIdBox			= document.getElementById("eId").value = '';
	
	var eventStatus	= document.getElementById("eventStatus").value = 'New';
	$('#deleteEvent').hide();
}

//***************************************************
//                Toggle New Event
//***************************************************
function toggleNewEvent() {
	var newEventDiv = document.getElementById('newEvent');
	if (newEventDiv.style.display == 'none') {
		$('#newEvent').show();
	}
	else {
		$('#newEvent').hide();
	}
}

function loadPubPage(page) {
	window.location = page;
}
function toggleemail() {
	var emailDiv = document.getElementById('emailad');
	if (emailDiv.style.display == 'none') {
		$('#emailad').show();
	}
	else {
		$('#emailad').hide();	
	}
}
function emailad(adtype,content) {
	var toaddress		= document.getElementById('toaddress').value;
	var fromaddress		= document.getElementById('fromaddress').value;
	var returnurl		= document.getElementById('returnurl').value;
	var dataString		= "adtype=" + adtype + "&content=" + escape(content) + "&toaddress=" + escape(toaddress) + "&fromaddress=" + fromaddress + "&returnurl=" + returnurl;

	$.ajax({
		method: "get",url: ajaxUrl + "ajax_emailAd.php", data: dataString,
		beforeSend: function(){$("#emailad").html(smLoadingDisplay);},
		success: function(html){
			$("#emailad").html(html);
		}
	});

}
function printad(adtype,imagesource,width,height) {
	var originalImage = '<img id="imageViewer" src="' + imagesource + '" />';
	
	popup = window.open('','popup','toolbar=no,menubar=no,width=200,height=150');
	popup.document.open();
	popup.document.write("<html><head></head><body onload='print()'>");
	popup.document.write(originalImage);
	popup.document.write("</body></html>");
	popup.document.close();

}
//***************************************************
// 	               Submit Event
//***************************************************
function submitEvent() {
	var title		= document.getElementById("eventTitle").value;
	var startdate	= document.getElementById("eventStartDate").value;
	var enddate		= document.getElementById("eventEndDate").value;
	var description	= document.getElementById("eventDescription").value;
	var eventStatus	= document.getElementById("eventStatus").value;
	var keywords	= document.getElementById("eventKeywords").value;
	var dataString = 'title=' + escape(title) + '&startdate=' + startdate + '&enddate=' + enddate + '&description=' + escape(description) + '&mode=' + eventStatus + '&keywords=' + keywords + '&eId=New';

	$.ajax({
		method: "get",url: ajaxUrl + "ajax_processEvent.php", data: dataString,
		beforeSend: function(){$("#hoverpopup").html(loadingDisplay);},
		success: function(html){
			$("#hoverpopup").html(html);
			$.ajax({
				method: "get",url: ajaxUrl + "ajax_calendar.php", data: "",
				beforeSend: function(){$("#calendar").html(loadingDisplay);},
				success: function(html){
					$("#calendar").html(html);
					clearEvent();
				}
			});
		}
	});
}

//***************************************************
// 	             	Next Month
//***************************************************
function nextMonth(month,year) {
	if (month == 12) {
		month = 1;
		++year;
	}
	else {
		++month;
	}
	$.ajax({
		method: "get",url: ajaxUrl + "ajax_events.php", data: "month="+month+"&amp;year="+year,
		beforeSend: function(){$("#calendar").html(loadingDisplay);},
		success: function(html){
			$("#calendar").html(html);
		}
	});
}

//***************************************************
// 	              Previous Month
//***************************************************
function previousMonth(month,year) {
	if (month == 1) {
		month = 12;
		--year;
	}
	else {
		--month;
	}
	$.ajax({
		method: "get",url: ajaxUrl + "ajax_events.php", data: "month="+month+"&amp;year="+year,
		beforeSend: function(){$("#calendar").html(loadingDisplay);},
		success: function(html){
			$("#calendar").html(html);
		}
	});
}

//***************************************************
// 	             	Next Month
//***************************************************
function nextMonthWidget(month,year) {
	if (month == 12) {
		month = 1;
		++year;
	}
	else {
		++month;
	}
	$.ajax({
		method: "get",url: ajaxUrl + "ajax_events_widget.php", data: "month="+month+"&amp;year="+year,
		beforeSend: function(){$("#calendar").html('');},
		success: function(html){
			$("#calendar").html(html);
		}
	});
}

//***************************************************
// 	              Previous Month
//***************************************************
function previousMonthWidget(month,year) {
	if (month == 1) {
		month = 12;
		--year;
	}
	else {
		--month;
	}
	$.ajax({
		method: "get",url: ajaxUrl + "ajax_events_widget.php", data: "month="+month+"&amp;year="+year,
		beforeSend: function(){$("#calendar").html('');},
		success: function(html){
			$("#calendar").html(html);
		}
	});
}

//******************************************************************************************************
// 	             						CLASSIFIED AD FUNCTIONS
//****************************************************************************************************** 
//***************************************************
// 	             Toggle Edit Tab
//*************************************************** 
function toggleClassified() {
	//Categories
	var classifiedMainContent = document.getElementById('classifiedMainContent');

	$('#classifiedMainContent').show();

	$.ajax({
		method: "get",url: ajaxUrl + "ajax_tab_edit.php",data: "uriCat=" + uriCat,
		beforeSend: function(){$("#classifiedMainContent").html(loadingDisplay);},
		success: function(html){
			$("#classifiedMainContent").html(html);
		}
	});	 
}

//***************************************************
//            Expand Selected Category
//*************************************************** 
function expandCat(strId) {
	//Categories
	var catDiv = document.getElementById('catDiv' + strId);
	var adDiv = document.getElementById('adDiv' + strId);
	var siteId = document.getElementById('siteId').value;

	if (catDiv.style.display == 'none') {
		$('#catDiv' + strId).show();
		$('#tgClassifiedLnk' + strId).html('Collapse');
		
		$.ajax({
			method: "get",url: ajaxUrl + "ajax_get_subcategories.php",data: "subcatid="+strId+"&siteId="+siteId,
			beforeSend: function(){$("#catContent" + strId).html(loadingDisplay);},
			success: function(html){
				$("#catContent" + strId).html(html);
			}
		});	 
	}
	else {
		$('#catDiv' + strId).hide();
		$('#tgClassifiedLnk' + strId).html('Expand');
	}

	//Ads
	if (adDiv.style.display == 'none') {
		
		$('#adDiv' + strId).show();
		$('#tgClassifiedLnk' + strId).html('Collapse');
		
		$.ajax({
			method: "get",url: ajaxUrl + "ajax_get_ads.php",data: "subcatid="+strId+"&siteId="+siteId,
			beforeSend: function(){$("#adContent" + strId).html(loadingDisplay);},
			success: function(html){
				$("#adContent" + strId).html(html);
				}
		 });
	}
	else {
		$('#adDiv' + strId).hide();
		$('#tgClassifiedLnk' + strId).html('Expand');
	}
}

//***************************************************
//            Expand URI Selected Category
//*************************************************** 
function expandUriCat(strId) {
	//Categories
	var catDiv = document.getElementById('catDiv' + strId);
	var adDiv = document.getElementById('adDiv' + strId);
	var siteId = document.getElementById('siteId').value;

	if (catDiv.style.display == 'none') {
		$('#catDiv' + strId).show();
		$('#tgClassifiedLnk' + strId).html('Collapse');
		
		$.ajax({
			method: "get",url: ajaxUrl + "ajax_get_subcategories.php",data: "siteId="+siteId+"&subcatid="+strId+"&amp;uriCat=<?= $uriCat ?>",
			beforeSend: function(){$("#catContent" + strId).html(loadingDisplay);},
			success: function(html){
				$("#catContent" + strId).html(html);
			}
		});
	}

	//Ads
	if (adDiv.style.display == 'none') {
		
		$('#adDiv' + strId).show();
		$('#tgClassifiedLnk' + strId).html('Collapse');
		
		$.ajax({
			method: "get",url: ajaxUrl + "ajax_get_ads.php",data: "subcatid="+strId+"&siteId="+siteId,
			beforeSend: function(){$("#adContent" + strId).html(loadingDisplay);},
			success: function(html){
				$("#adContent" + strId).html(html);
				}
		 });
		 
	}
	else {
		$('#adDiv' + strId).hide();
		$('#tgClassifiedLnk' + strId).html('Expand');
	}	
}

