
/*
 * Pops up the div element that uses AJAX to load the requested information to edit for
 * the profile.
 *
 * @param	String;	An identifier for what type of information is going to be edited. See the
 *					function 'profileDataEditForm' in MemberProfile.class for valid options
 * @return	VOID;
 */
function editMemberProfileData( dataType )
{
	if( !isValidString(dataType) )
		return null;
	
	if( !isValidObject(lightBox) )
		return null;
	
	// Set the title bar for the contact info edit
	lightBox.enableTitleBar("Edit your contact information");
	
	// Set a different height based on browser
	if( isIE || isIE7 )
		nHeight = 600;
	else if ( isFirefox )
		nHeight = 600;
	else if ( isOpera )
		nHeight = 525;
	else
		nHeight = 525;
		
	lightBox.setPopupSize(400, nHeight);
	
	// Now display the light box
	lightBox.reveal();
	
	// Enable tooltips for the popup
	tt_DynamicInit(lightBox.popupElement);
}


/*
 * Checks to see if the ENTER key was pressed from within the form element, and submits the member profile
 * data for saving if so
 *
 */
function saveDataOnEnter( oEvent )
{
	if(window.event && window.event.keyCode == 13 ) 
		submitMemberProfileData();
	else if( !window.event && oEvent && oEvent.which == 13 ) 
		submitMemberProfileData();
}


/*
 * Saves changes made to the exchange user's member profile by sending the data through an AJAX request
 * to check for errors, and either displaying any errors or finalizing the save
 *
 * @return	VOID
 */
function submitMemberProfileData()
{
	// Check for errors.  If there are any, do not save the data
	if( !checkMemberProfileData() )
		return null;
	
	tt_Hide();
	
	// Set working animation
	getObj("memberProfileSaveButtons").style.display = "none";
	getObj("profile_saving_ani").style.display = "";

	theObj = getObj("profileDataSubmit");
	
	var status = AjaxRequest.submit( theObj,
	{
			'onSuccess':function(ret) 
				{		
					getObj("profileDataEdit").innerHTML = ret.responseText;
					
					// Now reload the page to update the changes
					setTimeout('reloadProfile()',3000); 
					
				},
			'onError':function(ret) 
				{
					getObj("profileDataEdit").innerHTML = ret.responseText;
				}
				
	}
			);

}


/*
 * Cancels the changes made to the member profile data and hides the light box
 *
 * @return	VOID
 */
function cancelMemberProfileData()
{
	lightBox.hide();
}


/*
 * Checks the input fields for the member profile data and generates errors for any
 * fields that contain invalid data. Errors are added to the display from this function
 *
 * @return Boolean;	Returns true if the data is valid, else false
 */
function checkMemberProfileData()
{
	sErrorStr = '';

	// Clear existing errors before we begin
	getObj("profileSaveErrors").innerHTML = "";

	// Resize the shadow div to fit the size of the content after errors
	//getObj("profileDataEditShadow").style.height = getObj("profileDataEdit").style.height;

	if( getObj("first_name") && !getObj("first_name").value.trim() )
		sErrorStr += "Your first name is required.<br>";
	else if ( getObj("first_name") && getObj("first_name").value.trim().length > 30 )
		sErrorStr += "Your first name is limited to 30 characters.<br>";

	if( getObj("last_name") && !getObj("last_name").value.trim() )
		sErrorStr += "Your last name is required.<br>";
	else if ( getObj("last_name") && getObj("last_name").value.trim().length > 30 )
		sErrorStr += "Your last name is limited to 30 characters.<br>";

	if( getObj("client_name") && getObj("client_name").value.trim().length > 100 )
		sErrorStr += "Your company name is limited to 100 characters.<br>";
	else if( getObj("client_name") && !getObj("client_name").value.trim() )
		sErrorStr += "You must provide your company name.<br>";

	if( getObj("client_website") && getObj("client_website").value.trim().length > 256 )
		sErrorStr += "Your compnay website is limited to 256 characters.<br>";
	
	if( getObj("address1") && getObj("address1").value.trim().length > 100 )
		sErrorStr += "Your address (line 1) is limited to 100 characters.<br>";
	
	if( getObj("address2") && getObj("address2").value.trim().length > 100 )
		sErrorStr += "Your address (line 2) is limited to 100 characters.<br>";
	
	if( getObj("address_city") && getObj("address_city").value.trim().length > 20 )
		sErrorStr += "Your address city is limited to 20 characters.<br>";

	if( getObj("address_state_province") && getObj("address_state_province").value.trim().length > 20 )
		sErrorStr += "Your address state/province is limited to 20 characters.<br>";

	if( getObj("address_postal_code") && getObj("address_postal_code").value.trim().length > 15 )
		sErrorStr += "Your address postal code is limited to 15 characters.<br>";

	// If an email address was provided, check to see if it is in a valid format
	if( getObj("email_address") && getObj("email_address").value.trim().length > 0 )
	{
		if( !emailIsValid(getObj("email_address").value.trim()) )
			sErrorStr += "Your email address is not in a valid format.<br>";
	}

	// Keep track of any phone information entered, because if any information is provided but the phone
	// number is not, we have to throw an error, because that is a required field to save
	if( ( getObj("phone_country_code") && getObj("phone_country_code").value.trim() ) ||
	    ( getObj("phone_city_code") && getObj("phone_city_code").value.trim() ) ||
	    ( getObj("phone_extension") && getObj("phone_extension").value.trim() ) )
	{
		phoneDataEntered = true;
	}
	else
		phoneDataEntered = false;

	if( getObj("phone_phone") && !getObj("phone_phone").value.trim() && phoneDataEntered )
		sErrorStr += "You must provide a complete phone number to save it.<br>";
	else if ( getObj("phone_phone") && getObj("phone_phone").value.trim().length > 30 )
		sErrorStr += "Your phone number (excluding city/country codes) is limited to 30 digits.<br>";
	//else if ( getObj("phone_phone") && isNaN(getObj("phone_phone").value) )
	//	sErrorStr += "Your phone number may only contain digits.<br>";

	if( getObj("phone_country_code") && getObj("phone_country_code").value.trim().length > 3 )
		sErrorStr += "Your country code (phone) is limited to 3 digits.<br>";

	if( getObj("phone_country_code") && isNaN(getObj("phone_country_code").value.trim()) )
		sErrorStr += "Your country code (phone) must be a number.<br>";
	
	if( getObj("phone_city_code") && getObj("phone_city_code").value.trim().length > 10 )
		sErrorStr += "Your city code (phone) is limited to 10 digits<br>";

	if( getObj("phone_city_code") && isNaN(getObj("phone_city_code").value.trim()) )
		sErrorStr += "Your city code (phone) must be a number.<br>";

	if( getObj("phone_extension") && getObj("phone_extension").value.trim().length > 6 )
		sErrorStr += "Your phone extension is limited to 6 digits<br>";

	if( getObj("phone_extension") && isNaN(getObj("phone_extension").value.trim()) )
		sErrorStr += "Your phone extension must be a number.<br>";
	
	// Keep track of any fax information entered, because if any information is provided but the fax
	// number is not, we have to throw an error, because that is a required field to save
	if( ( getObj("fax_country_code") && getObj("fax_country_code").value.trim() ) ||
	    ( getObj("fax_city_code") && getObj("fax_city_code").value.trim() )
	     )
	{
		faxDataEntered = true;
	}
	else
		faxDataEntered = false;

	if( getObj("fax_phone") && !getObj("fax_phone").value.trim() && faxDataEntered )
		sErrorStr += "You must provide a complete fax number to save it.<br>";
	else if ( getObj("fax_phone") && getObj("fax_phone").value.trim().length > 30 )
		sErrorStr += "Your fax number (excluding city/country codes) is limited to 30 digits.<br>";
	//else if ( getObj("fax_phone") && isNaN(getObj("fax_phone").value) )
	//	sErrorStr += "Your fax number may only contain digits.<br>";


	if( getObj("fax_country_code") && getObj("fax_country_code").value.trim().length > 3 )
		sErrorStr += "Your country code (fax) is limited to 3 digits.<br>";

	if( getObj("fax_country_code") && isNaN(getObj("fax_country_code").value.trim()) )
		sErrorStr += "Your country code (fax) must be a number.<br>";
	
	if( getObj("fax_city_code") && getObj("fax_city_code").value.trim().length > 10 )
		sErrorStr += "Your city code (fax) is limited to 10 digits<br>";

	if( getObj("fax_city_code") && isNaN(getObj("fax_city_code").value.trim()) )
		sErrorStr += "Your city code (fax) must be a number.<br>";

	if( sErrorStr.length )
	{
		getObj("profileSaveErrors").innerHTML = sErrorStr;
		
		// Because the errors were added to the popup, it probably scrolls vertically
		// now, so resize the title bar
		lightBox.resizeTitleBar();
		
		return false;
	}

	return true;
}

function reloadProfile()
{
	document.location = document.location;
}




/*
 * Allows the user to delete their connections from their list by displaying a border around
 * the image for the connections with an X to delete them. If the user was already editing
 * connections, hide the editing options instead
 *
 */
function editConnections()
{
	// Check to see if the directions are displayed. If they are, then we are already showing
	// the editing, so toggle it off instead
	if( getObj("editConnectionInstruct") && getObj("editConnectionInstruct").style.display == "" )
	{
		getObj("editConnectionInstruct").style.display = "none";
		
		for( i = 1; getObj("myConnection["+i+"]"); i++ )
		{
			getObj("myConnection["+i+"]").style.border = "none";
			getObj("deleteMyConnection["+i+"]").style.display = "none";
		}
		
		// Make sure that we come back to the connections view. If the URI does not contain
		// the variable to do that then we'll add it
		
		if( document.location.href.search("show=connections") == -1 )
		{
			
			if( document.location.href.indexOf("?") > 1 )
				document.location = document.location + "&show=connections";
			else
				document.location = document.location + "?show=connections";
		}
		else
		{
			document.location = document.location;
		}
		
			
	}
	else
	{
		getObj("editConnectionInstruct").style.display = "";
		
		for( i = 1; getObj("myConnection["+i+"]"); i++ )
		{
			getObj("myConnection["+i+"]").style.border = "1px solid black";
			getObj("deleteMyConnection["+i+"]").style.display = "";
		}

	}
	
}


function showEditProfileURL()
{
	getObj('my_url_edit_input').style.display = '';
	getObj('my_url_edit').style.display = 'none';
	getObj('my_url_href').style.display = 'none';
	
		
}
 
function hideEditProfileURL()
{
	getObj('my_url_edit_input').style.display = 'none';
	getObj('my_url_edit').style.display = '';
	getObj('my_url_href').style.display = '';	
}
 
 
function updateUserProfileURL()
{
	profileUrlObj = getObj('user_profile_url');
	var profile_url = "";
	if(profileUrlObj == null)
	{
		alert("Unable to update your profile URL");
		return false;
	}
	else if(profileUrlObj.value.length < 3)
	{
		alert("Please enter a value that is at least 3 characters long.");
		return false;	
	}
	else
		profile_url = profileUrlObj.value.trim();
		
	
	AjaxRequest.post(	
		{
			'url':'/exchange/background.html',
			'parameters':{ 'update_member_profile_url':true, 'profile_url':profile_url },
			'onSuccess': function(oResponse)
					 {
					 	if(oResponse.responseText == "in_use")
					 	{
					 		alert("That profile url is already in use by another member. Please select another name to use.");
					 		return false;
					 	}
					 	else if(oResponse.responseText == "updated")
					 	{
					 		getObj('my_url_edit_input').style.display = 'none';
					 		getObj('my_url_edit').style.display = '';
							getObj('my_url_href').style.display = '';
							
							getObj('profile_url_href').innerHTML = getObj('exchange_host_name').value+"/"+profile_url;
							getObj('profile_url_href').href = "http://"+getObj('exchange_host_name').value+"/"+profile_url;
							return false;
					 	}
					 },
			'onError': function(oResponse)
					 {
					 	alert("Unable to modify profile URL");
					 	return false;
					 }
		} 
	);

}
 