function addFriendRequest() {

	var email = $('friend_new_email').value;
	//alert("email: " + email);
	var groupid = 0;
	new Ajax.Updater('friend_new', 'friend_request.php', {
  	parameters: { 'email':email, 'groupid': groupid},
		onSuccess: function() {
			setTimeout(removeAlert, 3000);
			//setTimeout($(update_location).remove(), 3000);
		}
	});
}

function addFriend() {
	// print out form for adding friend
	var str='friend_new';
	if($('friend_new_email')){
	}else{
		var boxcontent = "<p size='20'> Email <input id='friend_new_email' type='text' size='20'/><br><input class='buttons' type='button' onclick='addFriendRequest();' value='add' /> <input type='button' class='buttons' onclick='cancel_friend_new();' value='Cancel' /></p>"; 
		if($('friend_new')){
			$('friend_new').remove();
		}
		var elem = document.createElement('div');
		elem.id = 'friend_new';
		elem.innerHTML = boxcontent; 
		$('add_friends').parentNode.insertBefore(elem, $('add_friends').nextSibling);
		$('friend_new_email').focus();
	}
}

function requestFriend() {
	new Ajax.Updater('add_friend', 'addfriend.php', {
  	parameters: {'profileid': profileid},
		onSuccess: function() {
			setTimeout(removeAlert, 3000);
			//setTimeout($(update_location).remove(), 3000);
		}
	});
}

function invite() {
	var str='friend_new';
	if($('invite_new_email')){
	}else{
		var boxcontent = "<p size='20'>Email <input id='invite_new_email' type='text' size='20' /><br><input type='button' class='buttons' onclick='inviteRequest();' value='Invite' /> <input type='button' class='buttons' onclick='cancel_friend_new();' value='Cancel' /></p>"; 
		if($('friend_new')){
			$('friend_new').remove();
		}
		var elem = document.createElement('div');
		elem.id = 'friend_new';
		elem.innerHTML = boxcontent; 
		$('add_friends').parentNode.insertBefore(elem, $('add_friends').nextSibling);
		$('invite_new_email').focus();
	}
}


function inviteRequest() {
	var email = $('invite_new_email').value;
	//alert(email);
	var invite = 'true';
	new Ajax.Updater('friend_new', 'invite.php', {
  	parameters: { 'invite':invite, 'email': email},
		onSuccess: function() {
			setTimeout(removeAlert, 3000);
			//setTimeout($(update_location).remove(), 3000);
		}
	});
}

function cancel_friend_new(){
	$('friend_new').remove();
}
/********************************************************************************
 * Groups
 */
function addFriendsGroup (){
	if($('create_group')){
	}else{
		var boxcontent = "<p size='20'>Group Name <input id='create_group' type='text' size='20' /><br><input type='button' class='buttons' onclick='createGroupRequest();' value='Create' /> <input type='button' class='buttons' onclick='cancel_group_new();' value='Cancel' /></p>"; 
		if($('group_new')){
			$('group_new').remove();
		}
		var elem = document.createElement('div');
		elem.id = 'group_new';
		elem.innerHTML = boxcontent; 
		$('add_group').parentNode.insertBefore(elem, $('add_group').nextSibling);
		$('create_group').focus();
	}
}

function createGroupRequest() {
	var groupname = $('create_group').value;
	var privacy = 'private';

	// send AJAX request to add group to db
	// recreate sortable
	new Ajax.Updater('group_new', 'add_group.php', {
  	parameters: {'groupname': groupname, 'privacy': privacy},
		onSuccess: function() {
			//$('sc_profile_content').remove();
			new Ajax.Updater('sc_profile_content', 'ajax.php', {
				parameters: { 'func':'print_friends_browser', 'params':profileid+'&'},
				evalScripts: true
			});
			setTimeout(removeAlert, 3000);
		}
	});
}

function deleteGroup(groupid, groupname) {
	var del=confirm("are you sure you want to remove '"+groupname+"'? all members of this group will be listed under Individual.");
	if(!del) return;
	new Ajax.Updater('group_new', 'delete_group.php', {
  	parameters: {'groupname': groupname, 'groupid': groupid},
		onSuccess: function() {
			//$('sc_profile_content').remove();
			new Ajax.Updater('sc_profile_content', 'ajax.php', {
				parameters: { 'func':'print_friends_browser', 'params':profileid+'&'},
				evalScripts: true
			});
			setTimeout(removeAlert, 3000);
		}
	});
}


function cancel_group_new(){
	$('group_new').remove();
}

/*************import contacts********************/

function importMSN(){
	
	new Ajax.Updater('sc_profile_content', 'msn.php', {
  		parameters: {},
		evalScripts: true,
		onSuccess: function() {
			setTimeout(removeAlert, 3000);
			//setTimeout($(update_location).remove(), 3000);
		}
	});
	
	
}

function cancelImport(){
	
	new Ajax.Updater('sc_profile_content', 'ajax.php', {
	parameters: { 'func':'print_friends_browser', 'params':profileid+'&'},
	evalScripts: true
	});
}


/***** YUI drag & drop *******/
function YUIdragdrop() {

var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var DDM = YAHOO.util.DragDropMgr;

//////////////////////////////////////////////////////////////////////////////
// example app
//////////////////////////////////////////////////////////////////////////////
YAHOO.example.DDApp = {
    init: function() {
        var i;
		widget=document.getElementById('sc_profile_content');
		//alert(widget.id);
		groups=widget.getElementsByTagName('ul');
        for (i=0;i<groups.length;i=i+1) {
            new YAHOO.util.DDTarget(groups[i].id);
			//alert(groups[i].id);
        }
		lists=widget.getElementsByTagName('li');
        for (i=0;i<lists.length;i=i+1) {
            new YAHOO.example.DDList(lists[i]);
        }
		//alert ("here");
    }
};

//////////////////////////////////////////////////////////////////////////////
// custom drag and drop implementation
//////////////////////////////////////////////////////////////////////////////

YAHOO.example.DDList = function(id, sGroup, config) {

    YAHOO.example.DDList.superclass.constructor.call(this, id, sGroup, config);

    this.logger = this.logger || YAHOO;
    var el = this.getDragEl();
    Dom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent

    this.goingUp = false;
    this.lastY = 0;
};

YAHOO.extend(YAHOO.example.DDList, YAHOO.util.DDProxy, {

    startDrag: function(x, y) {
        this.logger.log(this.id + " startDrag");

        // make the proxy look like the source element
        var dragEl = this.getDragEl();
        var clickEl = this.getEl();
        Dom.setStyle(clickEl, "visibility", "hidden");

        dragEl.innerHTML = clickEl.innerHTML;

        Dom.setStyle(dragEl, "color", Dom.getStyle(clickEl, "color"));
        Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor"));
        Dom.setStyle(dragEl, "border", "2px solid gray");
    },

    endDrag: function(e) {

        var srcEl = this.getEl();
        var proxy = this.getDragEl();

        // Show the proxy element and animate it to the src element's location
        Dom.setStyle(proxy, "visibility", "");
        var a = new YAHOO.util.Motion( 
            proxy, { 
                points: { 
                    to: Dom.getXY(srcEl)
                }
            }, 
            0.2, 
            YAHOO.util.Easing.easeOut 
        )
        var proxyid = proxy.id;
        var thisid = this.id;

        // Hide the proxy and show the source element when finished with the animation
        a.onComplete.subscribe(function() {
                Dom.setStyle(proxyid, "visibility", "hidden");
                Dom.setStyle(thisid, "visibility", "");
            });
        a.animate();
		//alert("end drag");
		
		//Update Groupmember
		
		widget=document.getElementById('sc_profile_content');
		groups=widget.getElementsByTagName('ul');
		for(var i=0;i<groups.length;i++){
			var friends = '';
			var sc_profile_content = groups[i].getElementsByTagName('li');
			for (var j=0;j< sc_profile_content.length;j++) {
				friends += sc_profile_content[j].getAttribute('id') + '&';
			}
			new Ajax.Request('add_groupmember.php', {
				method: 'post',
				parameters: { data: friends, groupname: groups[i].id}}
			);
		}
		
		
		
    },

    onDragDrop: function(e, id) {

        // If there is one drop interaction, the li was dropped either on the list,
        // or it was dropped on the current location of the source element.
        if (DDM.interactionInfo.drop.length === 1) {

            // The position of the cursor at the time of the drop (YAHOO.util.Point)
            var pt = DDM.interactionInfo.point; 

            // The region occupied by the source element at the time of the drop
            var region = DDM.interactionInfo.sourceRegion; 

            // Check to see if we are over the source element's location.  We will
            // append to the bottom of the list once we are sure it was a drop in
            // the negative space (the area of the list without any list items)
            if (!region.intersect(pt)) {
                var destEl = Dom.get(id);
                var destDD = DDM.getDDById(id);
                destEl.appendChild(this.getEl());
                destDD.isEmpty = false;
                DDM.refreshCache();
            }

        }
    },

    onDrag: function(e) {

        // Keep track of the direction of the drag for use during onDragOver
        var y = Event.getPageY(e);

        if (y < this.lastY) {
            this.goingUp = true;
        } else if (y > this.lastY) {
            this.goingUp = false;
        }

        this.lastY = y;
    },

    onDragOver: function(e, id) {
    
        var srcEl = this.getEl();
        var destEl = Dom.get(id);

        // We are only concerned with list items, we ignore the dragover
        // notifications for the list.
        if (destEl.nodeName.toLowerCase() == "li") {
            var orig_p = srcEl.parentNode;
            var p = destEl.parentNode;

            if (this.goingUp) {
                p.insertBefore(srcEl, destEl); // insert above
            } else {
                p.insertBefore(srcEl, destEl.nextSibling); // insert below
            }

            DDM.refreshCache();
        }
    }
});

//Event.onDOMReady(YAHOO.example.DDApp.init, YAHOO.example.DDApp, true);
}
