function showColleague(elm) {
    var x = findPosX(elm);
    var y = findPosY(elm);

    $('ShareColleague').setStyle('left', x);
    $('ShareColleague').setStyle('top', y);
    $('ShareColleague').setStyle('display', "block");
}

function hideColleague() {
    $('ShareColleague').setStyle('display', "none");
}

function sendColleague(email) {
    if (email == "") {
	alert("You need to enter both an e-mail address and a name");
	return false;
    }

    new Request.JSON( {
	url :'/rpc.php',
	method :'get',
	onComplete : function(r) {
	    if (r != true) {
		alert("There was a problem sending. Please try again later.");
	    } else {
		alert("Sent successfully!");
		$('ShareColleague').setStyle('display', "none");
	    }
	}
    }).get( {
	'method' :"sendColleagueEmail",
	'email' :email
    });
}

function showSignup() {
    var d = $('SignupFormDiv');

    new Fx.Tween(d, {
	duration :1
    }).start('opacity', 0, 0).chain( function() {
	d.setStyle('display', "block");

	new Fx.Tween(d, {
	    duration :1000
	}).start('opacity', 0, 1);
    });
}

function showProjectCreate() {
    var d = $('ProjectCreateDiv');

    new Fx.Tween(d, {
	duration :1
    }).start('opacity', 0, 0).chain( function() {
	d.setStyle('display', "block");

	new Fx.Tween(d, {
	    duration :1000
	}).start('opacity', 0, 1);
    });
}

function loadAvailableMapProducts(tbl, ratecard_id) {
	var oidx = 0;
    if ($type(tbl) == 'element') {
	new Request.JSON(
		{
		    url :'/admin/rpc.php',
		    method :'get',
		    onComplete : function(r) {
			if ($type(r) == 'array') {
			    // parse and make fields ...
		    var tbody = tbl.getElement('tbody');
		    tbody.empty();
		    $each(
			    r,
			    function(e, idx) {
			    oidx++;
				var rw = new Element('tr', {'id': "ratecard_mapping_" + oidx});
				var rc_ratecard_format = new Element(
					'td',
					{
					    html :'<h4>'
					    	+ e.product_manufacturer
					    	+ ': '
						    + e.product_name
						    + '</h4><input class="FIELD" size="40" type="text" name="ratecard_format" value="'
						    + (($type(e.ratecard_map) == 'object') ? e.ratecard_map.ratecard_format
							    : "") + '" />',
					    valign :'bottom',
					    vAlign :'bottom'
					});
				var rc_daily_amount = new Element(
					'td',
					{
					    html :'<input class="FIELD" size="20" type="text" name="daily_amount" value="' + (($type(e.ratecard_map) == 'object') ? e.ratecard_map.daily_amount
						    : "") + '" />',
					    valign :'bottom',
					    vAlign :'bottom'
					});
				var rc_daily_rate = new Element(
					'td',
					{
						html :'<input class="FIELD" size="20" type="text" name="daily_rate" value="' + (($type(e.ratecard_map) == 'object') ? e.ratecard_map.daily_rate
						    : "") + '" />',
						valign: 'bottom',
						vAlign: 'bottom'
					});
				var rc_body_amount = new Element(
					'td',
					{
					    html :'<input class="FIELD" size="20" type="text" name="body_amount" value="' +
					    	(($type(e.ratecard_map) == 'object' && $type(e.ratecard_map.body_amount) == 'string') ? e.ratecard_map.body_amount : "") + '" />',
					    valign :'bottom',
					    vAlign :'bottom'
					});
				var rc_sort = new Element(
					'td',
					{
					    html :'<input class="FIELD" size="20" type="text" name="sort" value="' +
					    	(($type(e.ratecard_map) == 'object' && $type(e.ratecard_map.sort) == 'string') ? e.ratecard_map.sort : "") + '" />',
					    valign :'bottom',
					    vAlign :'bottom'
					});
				var rc_description = new Element(
					'td',
					{
					    html :'<input class="FIELD" size="25" type="text" name="description" value="' +
					    	(($type(e.ratecard_map) == 'object' && $type(e.ratecard_map.description) == 'string') ? e.ratecard_map.description : "") + '" />',
					    valign :'bottom',
					    vAlign :'bottom'
					});
				var rc_ctrls = new Element('td', {
				    html :"&nbsp;",
				    valign :'bottom',
				    vAlign :'bottom'
				});

				var input_save = new Element(
					'input',
					{
					    type :'button',
					    value :'Save',
					    events : {
						'click' : function() {
						    this.disabled = true;
						    var ratecard_format = rw.getElement('input[name=ratecard_format]').value;
						    var sort = rw.getElement('input[name=sort]').value;
						    var description = rw.getElement('input[name=description]').value;
						    var daily_amount = rw.getElement('input[name=daily_amount]').value.toFloat();
						    var body_amount = rw.getElement('input[name=body_amount]').value.toFloat();
							var daily_rate = rw.getElement('input[name=daily_rate]').value.toFloat();

						    if (ratecard_format != "" && $type(daily_amount) == 'number') {
							new Request.JSON( {
							    url :'/admin/rpc.php',
							    method :'get',
							    onComplete : function(save_result) {
									if (save_result === true) {
										loadAvailableMapProducts(tbl, ratecard_id);
									}
							    }
							}).get( {
							    'method' :"setRatecardProductMapping",
							    'ratecard_id' :ratecard_id,
							    'product_id' :e.product_id,
							    'ratecard_format' :ratecard_format,
							    'sort' :sort,
							    'description' :description,
							    'daily_amount' :daily_amount,
							    'body_amount' :body_amount,
							    'daily_rate' :daily_rate
							});
						    } else {
							alert("You must enter a valid:\n-Ratecard Format\n-Daily Amount (without dollar sign)");
						    }
						}
					    }
					});

				rc_ctrls.grab(input_save);

				if ($type(e.ratecard_map) == 'object') {
				    var input_delete = new Element('input', {
					type :'button',
					value :'Delete',
					events : {
					    'click' : function() {
						if (confirm("Are you sure you wish to delete this ratecard mapping?")) {
						    new Request.JSON( {
								url :'/admin/rpc.php',
								method :'get',
								onComplete : function(delete_result) {
									    if (delete_result === true) {
								    		//var orig_id = this.options.oidx;
											loadAvailableMapProducts(tbl, ratecard_id);
											//$('ratecard_mapping_' + orig_id).destroy();
											
									    }
								}
							    }).get( {
								'method' :"removeRatecardProductMapping",
								'ratecard_id' :e.ratecard_map.ratecard_id,
								'product_id' :e.product_id
						    });
						} else {

						}
					    }
					}
				    });
				    rc_ctrls.grab(input_delete);
				}
				
				rw.adopt([rc_ratecard_format, rc_daily_amount, rc_body_amount, rc_daily_rate, rc_sort, rc_description, rc_ctrls]);
				rw.inject(tbody);
			    });

		    tbl.setStyle('display', "block");
		} else {
		    // no products available
		    tbl.setStyle('display', "none");
		}
	    }
		}).get( {
	    'method' :"getAvailableRatecardProducts",
	    'ratecard_id' :ratecard_id
	});
    }
}