//------------------------------------------------------------------------------
// Class:  ManageFriend
//------------------------------------------------------------------------------
// Author:  CL
// Date:  2008/01/30
// Description:  This class defines the functions used by the Manage Group section.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
function ManageFriend()
{
	// Variables:
	
	//--------------------------------------------------------------------------

	// Methods:
	this.generateRandomPassword = generateRandomPassword;
	this.addNewGroup = addNewGroup;
	this.addGroupProcess = addGroupProcess;
	this.addFriend = addFriend;
	this.updateFriend = updateFriend;
	this.sendLoginToFriend = sendLoginToFriend;
	
	//--------------------------------------------------------------------------
	
	function generateRandomPassword(passwordID1, passwordID2,length)
	{
		var theLength = oRandPassword.generateRandRange(6,15,length);		
		$.ajax({
			type: "POST",
			url: "/gallery/_var/inc_generateString.cfm",
			data: "isGenerate=1",
			dataType: "html",
			success: function(html){
				if(html.length > 0){
					document.getElementById(passwordID1).value = jQuery.trim(html);
					document.getElementById(passwordID2).value = jQuery.trim(html);
				}
				else{
					alert("Unable to generate password.");			
				}
			},
			error: function(){
				alert("Unable to generate password.");
			}
		});
	}
	
	//--------------------------------------------------------------------------
	
	function addNewGroup(idString)
	{	
		theElement = document.getElementById(idString);
		if (theElement.style.display == "none")
		{
			theElement.style.display = "block";
		}
		YAHOO.addNewGroup.container.addNewGroup = new YAHOO.widget.ResizePanel('addNewGroup', { width:'350px', close:false, draggable:false, fixedcenter:true, visible:true, constraintoviewport:false } );
		YAHOO.addNewGroup.container.addNewGroup.render();
		YAHOO.util.Event.addListener("closeAddNewGroup", "click", YAHOO.addNewGroup.container.addNewGroup.hide, YAHOO.addNewGroup.container.addNewGroup, true);
		//YAHOO.util.Event.addListener("closeAddNewGroupProcess", "click", YAHOO.addNewGroup.container.addNewGroup.hide, YAHOO.addNewGroup.container.addNewGroup, true);
	}
	
	//--------------------------------------------------------------------------
	
	function addGroupProcess(theForm)
	{		
		/*theForm.isAddGroup.value = 1;
		theForm.submit();
		*/
		var theValue = theForm.newGroupName.value;
		
		if((theValue.length == 0) || (theValue.match(/^\s+$/)))
		{
			alert('* Please enter the group name.');
		}
		else
		{
			document.getElementById('addressBookAddGroupSuccess').style.display = "none";	
			document.getElementById('addressBookAddGroupSuccess').innerHTML = "";
			document.getElementById('addressBookAddGroupError').style.display = "none";	
			document.getElementById('addressBookAddGroupError').innerHTML = "";
			var theGroupListingObj = document.getElementById('groupList');
			var theGroupListing = theGroupListingObj.innerHTML;
		
			YAHOO.addNewGroup.container.addNewGroup.hide();
			$.ajax({
				type: "POST",
				url: "/gallery/_var/inc_addNewGroup.cfm",
				data: "newGroupName=" + encodeURIComponent(theValue),
				dataType: "html",
				success: function(html){
						if(html.length > 0)
						{
							if(theForm.intCurrentTotalGroup.value == 0)
							{
								document.getElementById('noGroupError').style.display = "none";
							}
							theForm.newGroupName.value = "";
							document.getElementById('groupList').innerHTML = theGroupListing + html ;
							//theGroupListingObj.scrollTop = theGroupListingObj.scrollHeight;
							document.getElementById('addressBookAddGroupSuccess').innerHTML = 
								"* The group " + theValue + " has been successfully added.";
							document.getElementById('addressBookAddGroupSuccess').style.display = "block";	
						}
						else
						{
							document.getElementById('addressBookAddGroupError').innerHTML = 
								"* The group " + theValue + " cannot be added. Please try again.";
							document.getElementById('addressBookAddGroupError').style.display = "block";	
						}
					},
					error: function(){
						document.getElementById('addressBookAddGroupError').innerHTML = 
								"* The group " + theValue + " cannot be added. Please try again.";
						document.getElementById('addressBookAddGroupError').style.display = "block";	
					}
			});
		}
		
	}
	
	//--------------------------------------------------------------------------
	
	function addFriend()
	{
		document.frmAddFriend.isAddFriend.value = 1;
		document.frmAddFriend.submit();
	}
	
	//--------------------------------------------------------------------------
	
	function updateFriend()
	{
		document.frmUpdateFriend.isUpdateFriend.value = 1;
		document.frmUpdateFriend.submit();
	}
	
	//--------------------------------------------------------------------------
	
	function sendLoginToFriend(iFriendID,sErrorID)
	{
		$.ajax({
			type: "POST",
			url: "/gallery/_var/inc_retrieveLogin.cfm",
			data: "fID=" + iFriendID,
			dataType: "text",
			success: function(text){
					if(text == "true")
					{
						document.getElementById(sErrorID).className = "successMesgBox";
						document.getElementById(sErrorID).innerHTML =
							"* The login has been send to your friend.<br/>";
					}
					else
					{
						document.getElementById(sErrorID).className = "errMesgBox";
						document.getElementById(sErrorID).innerHTML =
							"* The login cannot be send to your friend. Please try again. <br/>";
					}
				},
				error: function(){
					document.getElementById(sErrorID).className = "errMesgBox";
					document.getElementById(sErrorID).innerHTML =
						"* The login cannot be send to your friend. Please try again. <br/>";
				}
		});
	}
}

// Create Object
oManageFriend = new ManageFriend();

//--------------------------------------------------------------------------
