var _SELECTED_PLANT;
var _SELECTED_PLANT_DISPLAY_NAME;
var _SELECTED_PLANT_URL;

function generateSendPlantBody() {	

		if(_VIEWER == null) {
				return 'waiting to load viewer object';
		}

        var band = (typeof(arguments[0]) == 'undefined') ? 0 : arguments[0];
		//alert("at A " + _BASE_URL);
		// Perform actual send plant back on the server using PHP
		var sendPlantUrl = _BASE_URL + "os/greentrees/send-plant.php";
		sendPlantUrl += "?userId=" + _VIEWER.getId();
		sendPlantUrl += "&userType=" + _USER_TYPE;
		sendPlantUrl += "&op=listPlants";
	    sendPlantUrl += "&band=" + band;
		sendPlantUrl += "&uq=" + (new Date().getUTCMilliseconds());

		//alert("at B -> " +sendPlantUrl );

		var params = {};
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
		gadgets.io.makeRequest(sendPlantUrl, onListPlantsRequestComplete, params);
		
		return 'Loading...';
}

function onListPlantsRequestComplete(obj) {
    
    if(checkMaintenanceMode(obj) == true) {
        return;
    }
    
		var html = generateHeaderText();
//		html += '<BR>';
//		html += '<BR>';
//		html += generateSendButton();
		html += '<BR>';
		html += '<BR>';
		html += obj.text;
		html += '<BR>';
		html += '<BR>';
		html += generateSendButton();
		html += '<BR>';

		//alert (obj.text);
		document.getElementById('gt-body').innerHTML = html;
		//alert ('adjusting height');
		gadgets.window.adjustHeight();
}

function generateHeaderText() {
	var html = '<div class="plantsToSendBoxMessage" align="center"> ';
	html += '<LABEL>';
	html += 'Select a plant for your friends to grow in their (Lil) Green Patch. ';
	if(_USER_TYPE == 6) {
		html += 'Revenue from the use of this application will help you save squarefeet of rainforest!';
	}
	else {
		html += 'For every 10 plants received your friends will save 1 square foot of rain forest from deforestation!';
	}
	html += '</LABEL>';
	html += '</div>';
	return html;
}

function generateSendButton() {
	var html = '';
    if(_USER_TYPE == 6) {
        html += '<table width="100%">';
        html += '  <tr width="100%">';
        html += '    <td width="50%" align="left">';
	    html += '      <div style="text-align: left; padding-left: 6px;">';
		html += '        <input id="sendWithoutBulletin" class="fbbutton fbbuttonpatchaction" type="button" value="Send Plant >>" ';
	    html += '        name="sendWithoutBulletin" onclick="onProceedToSend(0 /* sendBulletin */);"/> ';
        html += '      </div>';
        html += '    </td>';
        html += '    <td width="50%" align="right">';
        html += '      <div style="text-align: right; padding-right: 10px;">';
		html += '       <input id="send" class="fbbutton fbbuttonpatchaction" type="button" value="Send Plant and Post Bulletin >>" ';
	    html += '       name="buttonProceedToSend" onclick="onProceedToSend(1 /* sendBulletin */);"/> ';
	    html += '      </div>';
        html += '    </td>';
        html += '  </tr>';
        html += '</table>';
    }
    else {
        html += '<div style="text-align: right; padding-right: 10px;">';
		html += ' <input id="send" class="fbbutton fbbuttonpatchaction" type="button" value="Proceed to Send >>" ';
	    html += ' name="buttonProceedToSend" onclick="onProceedToSend(0 /* sendBulletin */);"/> ';
	    html += '</div>';
    }
	return html;
}

function onProceedToSend(sendBulletin) {
    
	var plants = document.getElementsByName('PlantToSend');
    
	_SELECTED_PLANT = null;
	_SELECTED_PLANT_DISPLAY_NAME = null;
	_SELECTED_PLANT_URL = null;
    
	for (var i = 0; i < plants.length; i++) {
		var plant = plants[i];
		//alert (plant.checked);
		if (plant.checked == true) {
			//alert (plant.value + " selected");
			_SELECTED_PLANT = plant.value;
			break;
		}
	}
    
	if(_SELECTED_PLANT == null) {
		alert('Please select a plant to send');
	}
    else {
        if(_USER_TYPE == 6 && sendBulletin == 1) {
            sendMyspaceBulletin();
        }
		generateSelectFriendsPage(_SELECTED_PLANT);
    }
}

function buyPlantUsingGreenBucks(formName) {
	//alert ('buy button of ' + formName + ' clicked');
	//var buyForm = document.getElementById(formName);
	var purchaseDetails = serializeForm(formName); 
	var url = _BASE_URL + "os/greentrees/send-plant.php";
	url += "?userId=" + _VIEWER.getId();
	url += "&userType=" + _USER_TYPE;
	url += "&op=buyPlantUsingGreenBucks";
	url += "&purchaseDetails=" + purchaseDetails;
	url += "&uq=" + (new Date().getUTCMilliseconds());
	
	//alert(url);
	//debug(url);

	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
	gadgets.io.makeRequest(url, onBuyPlantUsingGreenBucksRequestComplete, params);
}

function onBuyPlantUsingGreenBucksRequestComplete(obj) {
    if(checkMaintenanceMode(obj) == true) {
        return;
    }
	//alert ('called onBuyPlantUsingGreenBucksRequestComplete');
	updateGreenBucks(obj.data.coinsAfterSale);
	generateSelectFriendsPage(obj.data.purchasedPlant);
}

function getMoreGreenBucks(formName) {
	var html = generateOffersPage();
	document.getElementById('gt-body').innerHTML = html;
	gadgets.window.adjustHeight();
}
