var latitude = -1, longitude = -1;
var geocoder, map;
var curAddress = "";
function setCoords(lat1, longde) { 
	if (lat1 != -1 && longde != -1) {
		latitude = lat1;
		longitude = longde;
	}
}

function clearCoords() { 
	latitude = -1;
	longitude = -1;
	if ($('map')) {
		map = new GMap2(document.getElementById("map"));
	}
}

function updateMap() {
	// get the venueue id, lat and long points and set the new values on the map.
	// for additional info, see the google maps api docs http://www.google.com/apis/maps/documentation/reference.html
	venueID = $F('VenueID');
	lat1 = $F('VenueLatitude'+venueID);
	longde = $F('VenueLongitude'+venueID);
	if (lat1 != -1 && longde != -1) {
		setCoords(lat1, longde);
		var point = new GLatLng(latitude, longitude);
		map.setCenter(point, 13);
		var marker = new GMarker(point);
		map.clearOverlays();
		map.addOverlay(marker);
	}
}


function createGMap() {
	if ($('map')) {
		map = new GMap2(document.getElementById("map"));
		if (latitude != -1 && longitude != -1) {
			point = new GLatLng(latitude, longitude);
			map.setCenter(point, 13);
			var marker = new GMarker(point);
			map.addOverlay(marker);
		}
	}
}

function activateColors() {
	if ($('PrimaryColor')) {
		new Control.ColorPicker("PrimaryColor", { "swatch" : "primaryColorBox" }, { IMAGE_BASE : "/imgs/" });
		new Control.ColorPicker("SecondaryColor", { "swatch" : "secondaryColorBox" }, { IMAGE_BASE : "/imgs/" });
	}
}

function findLocation() {
	if ($F('Address') != "" && $F('City') != "" && $F('State') != "") {
		geocoder = new GClientGeocoder();
		address = $F('Address');
		address += " "+$F('City')+", "+$F('State')+" "+$F('Zip');
		if (address != curAddress) {
			curAddress = address;
			geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					latitude = point.lat();
					longitude = point.lng();
					$('Latitude').value = latitude;
					$('Longitude').value = longitude;
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
				}
			});
		}
	}
}

function viewUser(userID, url) {
	params = "action=viewUser&UserID="+userID;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params,
			method: 'post'
		}
	);
}

function showAdd(type, url) {
	params = "action=ShowAdd"+type;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params,
			method: 'post',
			onComplete: function() {
				if ((type == "Seminar" || type == "Venue") && GBrowserIsCompatible()) {
					setCoords($F('Latitude'), $F('Longitude'));
					createGMap();
				}
				if (type == "Seminar") {
					startTinyMCE();
				}
			}
		}
	);
}
function showAdd2(type, url, seminarID) {
	params = "action=ShowAdd"+type+"&"+"SeminarID="+seminarID;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params,
			method: 'post',
			onComplete: function() {
				if ((type == "Seminar" || type == "Venue") && GBrowserIsCompatible()) {
					setCoords($F('Latitude'), $F('Longitude'));
					createGMap();
				}
			}
		}
	);
}

function showMainPage(url) {
	params = "action=ShowList";
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params
		}
	);
}
function showMainPageWithID(url, type, id) {
	params = "action=ShowList&"+type+"ID="+id;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params
		}
	);
}

function removeObj(type, url, id) {
	params = "action=Delete"+type+"&"+type+"ID="+id;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params
		}
	);
}
function removeObj2(type, url, id, seminarID) {
	params = "action=Delete"+type+"&"+type+"ID="+id+"&SeminarID="+seminarID;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params
		}
	);
}

function showChange(type, url, id) {
	params = "action=ShowChange"+type+"&"+type+"ID="+id;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params,
			method: 'post',
			onComplete: function(request) {
				if ((type == "Seminar" || type == "Venue") && GBrowserIsCompatible()) {
					setCoords($F('Latitude'), $F('Longitude'));
					createGMap();
				}
				if (type == "Seminar") {
					startTinyMCE();
				}
			}
		}
	);
}

function showChange2(type, url, id, seminarID) {
	params = "action=ShowChange"+type+"&"+type+"ID="+id+"&SeminarID="+seminarID;
	var ajax = new Ajax.Updater(
		"content",
		url,
		{
			parameters: params,
			method: 'post',
			onComplete: function() {
				if ((type == "Seminar" || type == "Venue") && GBrowserIsCompatible()) {
					setCoords($F('Latitude'), $F('Longitude'));
					createGMap();
				}
			}
		}
	);
}

function showEmail() {
	// parse arguments, the first is the type (Seminar or All), the second is the url, the third is the Seminar ID value (if neccesarry)
	if (arguments[0] == "Seminar")
		params = "action=ShowEmailSeminar&SeminarID="+arguments[2];
	else
		params = "action=ShowEmailAll";
	var ajax = new Ajax.Updater(
		"content",
		arguments[1],
		{
			parameters: params,
			method: 'post'
		}
	);
}

function sendEmail() {
	// parse arguments, the first is the type (Seminar or All), the second is the url, the third is the Seminar ID value (if neccesarry)
	var i;
	if (arguments[0] == "Seminar") {
		params = "action=SendEmailSeminar&SeminarID="+arguments[2];
		i = 3;
	} else {
		params = "action=SendEmailAll";
		i = 2;
	}
	for( i; i<arguments.length; i++) {
		params += "&"+arguments[i]+"="+escape($F(arguments[i]));
	}
	var ajax = new Ajax.Updater(
		"content",
		arguments[1],
		{
			parameters: params,
			method: 'post'
		}
	);
}

function add() {
	// parse arguments, the first is the type, the second is the url, the third is the ID value, and the rest are the parameters
	type = arguments[0];
	params = "action=Add"+type;
	if (type == "Seminar"){
		unsetTextareaToTinyMCE('description');
	}
	if (type == "Quote") {
		unsetTextareaToTinyMCE('SundayText');
		unsetTextareaToTinyMCE('MondayText');
		unsetTextareaToTinyMCE('TuesdayText');
		unsetTextareaToTinyMCE('WednesdayText');
		unsetTextareaToTinyMCE('ThursdayText');
		unsetTextareaToTinyMCE('FridayText');
		unsetTextareaToTinyMCE('SaturdayText');
	}
	for (var i=2; i<arguments.length; i++) {
		if (arguments[i].charAt(0) == "_") {
			for (var j=0; $(arguments[i].substr(1)+j) != null; j++) {
				var field = arguments[i].substr(1)+j;
				if (   $(field) != null && ( ($(field).type ==  "checkbox" && $(field).checked)  || $(field).type !=  "checkbox" )   ) 
						params += "&"+field+"="+escape($F(field));
			}
		} else if ($(arguments[i] != null)) {
			//alert("params += &"+arguments[i]+"="+escape($F(arguments[i]))+";");
			params += "&"+arguments[i]+"="+escape($F(arguments[i]));
		}
	}
	var ajax = new Ajax.Updater(
		"content",
		arguments[1],
		{
			parameters: params,
			method: 'post',
			onComplete: function() {
				if ((type == "Seminar" || type == "Venue") && GBrowserIsCompatible()) {
					setCoords($F('Latitude'), $F('Longitude'));
					createGMap();
				}
				if(type == "Quote") {
					initialLoad();
					//setTextareaToTinyMCE("SundayText");
				}
				if (type == "Seminar") {
					startTinyMCE();
				}
			}
		}
	);
}

function change() {
	// parse arguments, the first is the type, the second is the url, the third is the ID value, and the rest are the parameters
	type = arguments[0];
	params = "action=Change"+type+"&"+type+"ID="+arguments[2];
	if (type == "Seminar"){
		//unsetTextareaToTinyMCE('description');
	}
	
	for (var i=3; i<arguments.length; i++) {
		if (arguments[i].charAt(0) == "_") {
			for (var j=0; $(arguments[i].substr(1)+j) != null; j++) {
				var field = arguments[i].substr(1)+j;
				if ( $(field) != null && ( ($(field).type ==  "checkbox" && $(field).checked)  || $(field).type !=  "checkbox" )   ) 
					params += "&"+field+"="+escape($F(field));
			}
		} else if ($(arguments[i] != null)) {
			params += "&"+arguments[i]+"="+escape($F(arguments[i]));
		}
	}
	var ajax = new Ajax.Updater(
		"content",
		arguments[1],
		{
			parameters: params,
			method: 'post',
			onComplete: function() {
				if ((type == "Seminar" || type == "Venue") && GBrowserIsCompatible()) {
					setCoords($F('Latitude'), $F('Longitude'));
					createGMap();
				if(type == "Seminar")
					startTinyMCE();
				}
			}
		}
	);
}

function showChecked() {
	// parse arguments, the first is the url, the second is the SeminarID, and the rest are the parameters;
	params = "action=ShowList&SeminarID="+arguments[1];
	for (var i=2; i<arguments.length; i++) {
		if (arguments[i].charAt(0) == "_") {
			for (var j=0; $(arguments[i].substr(1)+j) != null; j++) {
				var field = arguments[i].substr(1)+j;
				if (   $(field) != null && ( ($(field).type ==  "checkbox" && $(field).checked)  || $(field).type !=  "checkbox" )   ) 
						params += "&"+field+"="+escape($F(field));
			}
		} else if ($(arguments[i] != null)) {
			params += "&"+arguments[i]+"="+escape($F(arguments[i]));
		}
	}
	var ajax = new Ajax.Updater(
		"content",
		arguments[0],
		{
			parameters: params,
			method: 'post',
			onComplete: function() {
				if ((type == "Seminar" || type == "Venue") && GBrowserIsCompatible()) {
					setCoords($F('Latitude'), $F('Longitude'));
					createGMap();
				}
			}
		}
	);
}

function showPaid() {
	// parse arguments, the first is the url, and the rest are the parameters;
	params = "action=ShowPaid";
	for (var i=1; i<arguments.length; i++) {
		if (arguments[i].charAt(0) == "_") {
			for (var j=0; $(arguments[i].substr(1)+j) != null; j++) {
				var field = arguments[i].substr(1)+j;
				if (   $(field) != null && ( ($(field).type ==  "checkbox" && $(field).checked)  || $(field).type !=  "checkbox" )   ) 
						params += "&"+field+"="+escape($F(field));
			}
		} else if ($(arguments[i] != null)) {
			params += "&"+arguments[i]+"="+escape($F(arguments[i]));
		}
	}
	var ajax = new Ajax.Updater(
		"content",
		arguments[0],
		{
			parameters: params,
			method: 'post'
		}
	);
}

function startTinyMCE() {
	//unsetTextareaToTinyMCE("description");
	setTextareaToTinyMCE("description");
}

bTextareaWasTinyfied = false; //this should be global, could be stored in a cookie...
function setTextareaToTinyMCE(sEditorID) {
	var oEditor = document.getElementById(sEditorID);
	//if(oEditor && !bTextareaWasTinyfied) {
		tinyMCE.execCommand('mceAddControl', true, sEditorID);
		//bTextareaWasTinyfied = true;
	//}
	return;
}

function unsetTextareaToTinyMCE(sEditorID) {
	var oEditor = document.getElementById(sEditorID);
	//if(oEditor && bTextareaWasTinyfied) {
		tinyMCE.execCommand('mceRemoveControl', true, sEditorID);
		//bTextareaWasTinyfied = false;
	//}
	return;
}
// the code to add to your ajax request.
/*onComplete: function(request) { 
	startTinyMCE();
}*/

function hideAll() {
	$('Sunday').style.display = 'none';
	$('SundayBtn').style.border = '#000000 1px solid';
	//unsetTextareaToTinyMCE('SundayText');
	$('Monday').style.display = 'none';
	$('MondayBtn').style.border = '#000000 1px solid';
	//unsetTextareaToTinyMCE('MondayText');
	$('Tuesday').style.display = 'none';
	$('TuesdayBtn').style.border = '#000000 1px solid';
	//unsetTextareaToTinyMCE('TuesdayText');
	$('Wednesday').style.display = 'none';
	$('WednesdayBtn').style.border = '#000000 1px solid';
	//unsetTextareaToTinyMCE('WednesdayText');
	$('Thursday').style.display = 'none';
	$('ThursdayBtn').style.border = '#000000 1px solid';
	//unsetTextareaToTinyMCE('ThursdayText');
	$('Friday').style.display = 'none';
	$('FridayBtn').style.border = '#000000 1px solid';
	//unsetTextareaToTinyMCE('FridayText');
	$('Saturday').style.display = 'none';
	$('SaturdayBtn').style.border = '#000000 1px solid';
	//unsetTextareaToTinyMCE('SaturdayText');
}

function initialLoad() {
	//alert("re-init");
	setTextareaToTinyMCE("SundayText");
	setTextareaToTinyMCE("MondayText");
	setTextareaToTinyMCE("TuesdayText");
	setTextareaToTinyMCE("WednesdayText");
	setTextareaToTinyMCE("ThursdayText");
	setTextareaToTinyMCE("FridayText");
	setTextareaToTinyMCE("SaturdayText");
	$('Sunday').style.visibility = 'visible';
	$('Monday').style.visibility = 'visible';
	$('Tuesday').style.visibility = 'visible';
	$('Wednesday').style.visibility = 'visible';
	$('Thursday').style.visibility = 'visible';
	$('Friday').style.visibility = 'visible';
	$('Saturday').style.visibility = 'visible';
	showInfo('Sunday');
}

function showInfo(IDtag){
	hideAll();
	$(IDtag).style.display = 'block';
	$(IDtag+"Btn").style.borderBottom = '0px';
}
