
// Table names
var table_names = new Array();

// Path of nav images
var navPath = 'images/profile/';
var navSelected = 'images/profile/selected_arrow.gif';
var navNormal =   'images/blank.gif';

// Booleans
var skill_hid = false;
var experience_hid = false;
var reference_hid = false;
var award_hid = false;
var publication_hid = false;
var education_hid = false;
var benefit_hid = false;
var location_hid = false;
var desired_hid = false;


/*
 * Sets this as the default callback function for the inline edit functionality to
 * use when the user clicks save. Displays the save warning at the top of the profile
 *
 * @param	Object;	The original element that the inline edit object was created for
 * @return	VOID
 */
var inlineEditCallback =
		function ( oElement )
		{
				//document.getElementById("saveWarning").style.display = 'inline';
				//document.getElementById("saveResult").style.display = '';
				saveChanges();

		}


// Sets table names so that we know what tables to show/hide when needed
function setTables( tables )
{
	table_names = tables;
}

// Hides all the tables and data
// if "tables_only" is true, only switch tables, do not manipulate data rows
function hideAll( tables_only )
{
	if( !tables_only )
	{
		manip( "skill" );	
		manip( "experience" );
		manip( "reference" );
		manip( "award" );
		manip( "publication" );
		manip( "education" );
		manip( "desired" );
	}

	if( !table_names[0] )
		return;

	for( i = 0; i < table_names.length; i++ )
	{
		document.getElementById( 'show_'+table_names[i] ).style.display = 'none';
		document.getElementById( 'img_'+table_names[i] ).src = navNormal;
		navOff( table_names[i] );
	}
}



// Hides all tables, and then shows only the one selected
function showTable( table )
{
	var to_show = document.getElementById( 'show_'+table );
	var nav = document.getElementById( 'img_'+table );

	hideAll( true );

	to_show.style.display = '';
	nav.src = navSelected;

	navOn( table );
}



// Changes tab image when moused over
function navOn( tab )
{
	document.getElementById( 'nav_'+tab ).src = navPath+'tab_'+tab+'_on.gif';
}

// Changes tab image when mouse is moved off
function navOff( tab )
{
	// If this table is showing, don't turn off the 'on' image when they mouse over and out
	if( document.getElementById( 'show_'+tab ).style.display == '' )
		return;

	document.getElementById( 'nav_'+tab ).src = navPath+'tab_'+tab+'.gif';
}

function toggleProfileImage()
{
	if( document.getElementById("showLogo").style.display == 'none' )
	{
		document.getElementById("showLogo").style.display = '';
		document.getElementById("showPhoto").style.display = 'none';

		document.getElementById("swapImage").innerHTML = 'Show Member Photo';
	}
	else if( document.getElementById("showPhoto").style.display == 'none' )
	{
		document.getElementById("showPhoto").style.display = '';
		document.getElementById("showLogo").style.display = 'none';

		document.getElementById("swapImage").innerHTML = 'Show Company Logo';
	}

}

function addNewIndustry( oElement, industryList )
{
	// Find the first index for which there is no edit element object
	for( i = 0; i < 100; i++ )
	{
		if( isValidObject(editElements["industry["+i+"]"]) )
			continue;
		else
			break;
	}

	editElement = document.createElement( 'span' );
	editElement.style.marginTop = '0';
	editElement.style.width = 100;
	editElement.style.marginBottom = '0';

	nextIDX = i;

	editElement.setAttribute( "onMouseOver", "createInlineSelect( this, industryList, 'industry[" + nextIDX + "]', '', this.innerHTML );  ttWidth=50; return escape('Click to edit');");

	editElement.innerHTML = '[[<b>New Industry</b>]]';
	oElement.parentNode.insertBefore( editElement, oElement );

	buffer = document.createElement( 'span' );
	buffer.innerHTML = ' -- ';
	oElement.parentNode.insertBefore( buffer, oElement );

}


function createPhoneNumberEdit( oElement )
{
	
}


var regVals = Array();


function regValID( valID )
{
	regVals[valID] = true;
}



function saveChanges()
{
	// editElements is the global array for inline edit objects

	//if( !isValidObject(editElements) )
	//	return null;

	sPostParameters = String('');

	// Loop through all elements
	for( index in editElements )
	{
		// If it has not been changed, move to the next index
		if( !editElements[index].modified )
			continue;

		// Add this item to the list of parameters to save
		sPostParameters += String( '&' + index + '=' + escape(editElements[index].inputElement.value) );
	}

	//if( !sPostParameters || sPostParameters.length <= 0 )
	//	return null;


	sParamTemp = '?pageAction=save&profile_id=' + document.getElementById("profile_id").value;

	for( valID in regVals )
	{
		sTempName = document.getElementById( valID ).getAttribute("name");
		mTempValue = document.getElementById( valID ).value;

		sParamTemp += '&' + escape(sTempName) + '=' + escape(mTempValue);
	}
	
	sParamTemp += '&'+escape('industry[0]')+'='+escape(document.getElementById('industrySelect0').value);
	sParamTemp += '&'+escape('industry[1]')+'='+escape(document.getElementById('industrySelect1').value);
	sParamTemp += '&'+escape('industry[2]')+'='+escape(document.getElementById('industrySelect2').value);
	sParamTemp += '&'+escape('industry[3]')+'='+escape(document.getElementById('industrySelect3').value);
	sParamTemp += '&'+escape('industry[4]')+'='+escape(document.getElementById('industrySelect4').value);
	sParamTemp += '&'+escape('industry[5]')+'='+escape(document.getElementById('industrySelect5').value);

	sPostParameters = sParamTemp + sPostParameters;

	// Create the http object
	
	var http_obj = new HTTPobject();
	http_obj.forceSyc = true;

	http_obj.httpResultsFunc =
		function ( result_obj )
		{
			document.getElementById("saveResult").innerHTML = result_obj.responseText;
			setTimeout("clearResults",30);

		}

	
	document.getElementById("saveWarning").style.display = '';
	document.getElementById("saveResult").style.display = 'inline';
	document.getElementById("saveResult").innerHTML = '<br>Saving changes...';

	http_obj.sendHTTPrequest( '/exchange/profile.html', sPostParameters, 'POST', '', '' );

}


function clearResults()
{
	document.getElementById("saveResult").innerHTML = '!';

}

function checkSetImageSize()
{
	if(!getObj('memberPicture'))
		return false;
	max_height = 130;
	if(getObj('memberPicture').height > 160)
	{
		// Find correct proportions
		
		// This will get up the percentage that we need to shrink the width
		// and height by
		shrink = Math.round(100 - (getObj('memberPicture').height/(getObj('memberPicture').height - max_height)))/100;
		
		// set new height
		getObj('memberPicture').height = max_height;
		
		// set new width
		getObj('memberPicture').width = Math.round(getObj('memberPicture').width*shrink);
	}
	
	if(getObj('companyLogo').height > 160)
	{
		// Find correct proportions
		
		// This will get up the percentage that we need to shrink the width
		// and height by
		shrink = Math.round(100 - (getObj('companyLogo').height/(getObj('companyLogo').height - max_height)))/100;
		
		// set new height
		getObj('companyLogo').height = max_height;
		
		// set new width
		getObj('companyLogo').width = Math.round(getObj('companyLogo').width*shrink);
	}	
}

// For selecting between profile images when uploading/removing them
function selectImage( selWhich )
{
	if( selWhich == 'logo' )
	{
		getObj('companyLogo').style.border = '2px solid red';
		getObj('memberPicture').style.border = '1px solid black';
	}
	else
	{
		getObj('companyLogo').style.border = '1px solid black';
		getObj('memberPicture').style.border = '2px solid red';
	}

	getObj('updateImage').value = selWhich;
}


// Checks to see if an image is selected and a new one selected for upload before
// submitting the image management form
function checkImageUpload()
{
	if( document.getElementById("updateImage").value == "" )
	{
		alert("You must select which image to upload");
		return false;
	}
	else if( document.getElementById("newImage").value == "" )
	{
		alert("You must select a new image to upload to your profile");
	}
	else
		document.getElementById("manageProfileImage").submit();
}


// Checks to see if an image is selected to be removed before submitting
// the image management form
function checkImageRemove()
{
	if( document.getElementById("updateImage").value == "" )
	{
		alert("You must select which image to remove");
		return false;
	}
	else
	{
		document.getElementById("delete").value = 1;
		document.getElementById("manageProfileImage").submit();
	}

}


// Number of records to skip, and max record number to hide
var counts = new Array();

// Sets numbers for how many of each record to use
// MUST BE USED BEFORE SHOW/HIDE FUNCTIONS
function setCount( type, skip, maximum )
{
	eval( 'var '+type+'_hid = false' );
	
	if( maximum > skip )
	{
		counts[type] = new Array();
		counts[type]['skip'] = skip;
		counts[type]['max'] = maximum;
	}
		
}


// Expands or collapses information for 'type'
function manip( type )
{
	if( !counts[type] || !isValidObject(getObj('show'+type)) )
		return;

	if( eval( type+'_hid' ))
	{
		var style = '';
		eval( type+'_hid = false' );
		
		document.getElementById( 'show'+type ).style.display = 'none';
		document.getElementById( 'hide'+type ).style.display = '';
		
		// Check to see if this is a type of data that has a spacer beneath the rows
		var spacer = document.getElementById( type+'_spacer_0' );
	}
	else
	{
		var style = 'none';
		eval( type+'_hid = true' );
		
		document.getElementById( 'show'+type ).style.display = '';
		document.getElementById('hide'+type ).style.display = 'none';
		
		// Check to see if this is a type of data that has a spacer beneath the rows
		var spacer = document.getElementById( type+'_spacer_0' );
	}
	
	for( i = counts[type]['skip']; i < counts[type]['max']; i++ )
	{
		document.getElementById( type+'_row_'+i ).style.display = style;
		
		// If there are spacers for this data type
		if( spacer )
			document.getElementById( type+'_spacer_'+i ).style.display = style;
	}
}



/*
 * Finds the skill with index 'nSkillNum' and copies the appropriate select list
 * options over from the lists used when adding/editing a new skill
 *
 * @param	Number;	The skill id index
 * @param	String;	The value to select for Skill Level
 * @param	String; The value to select for Skill Experience
 * @param	String;	The value to select for Skill Interest
 * @param	String;	The value to select for Last Used
 * @return 	null;
 */		
function createCandProfSkillOptions( nSkillNum, sSelectLevel, sSelectExperience, sSelectInterest, sSelectUsed )
{
	return null;
	oSource = getObj("skillLevelSelect");
	oTarget = getObj("skill_level[" + nSkillNum + "]");
	copySelectOptions( oSource, oTarget, '', sSelectLevel, '', true );
	
	oSource = getObj("skillExperienceSelect");
	oTarget = getObj("skill_experience[" + nSkillNum + "]");
	copySelectOptions( oSource, oTarget, '', sSelectExperience, '', true );
	
	oSource = getObj("skillInterestSelect");
	oTarget = getObj("skill_interest[" + nSkillNum + "]");
	copySelectOptions( oSource, oTarget, '', sSelectInterest, '', true );
	
	oSource = getObj("skillLastUsedSelect");
	oTarget = getObj("skill_last_used[" + nSkillNum + "]");
	copySelectOptions( oSource, oTarget, '', sSelectUsed, '', true );	
}

function addToRecruitingPartners( ex_vendor_id, jobPosNum )
{
	if(!ex_vendor_id)
	{
		alert("Unable to add as connection - No Vendor ID");
		return false;
	}
		
	AjaxRequest.post(	
		{
			'url':'/exchange/background.html',
			'parameters':{ 'ex_vendor_id':ex_vendor_id, 'add_recruiting_partner':true },
			'onSuccess': function(oResponse)
					 {
					 	if(getObj("recruiting_partner"+jobPosNum))
					 	{
					 		getObj("recruiting_partner"+jobPosNum).innerHTML = "<IMG SRC='/exchange/images/buttons/request_sent.gif' BORDER=0  VSPACE=5 HSPACE=2>";
					 	}
					 	
					 	alert(oResponse.responseText);
					 	
					 },
			'onError': function(oResponse)
					 {
					 	alert("Unable to make connection - " + oResponse.responseText);
					 }
		} 
	);
}


function sendPrivateMessage2(to_uaid,from_uaid,message,subject)
{
	if(!to_uaid)
	{
		alert("Unable to add send - No to uaid");
		return false;
	}

	if(!from_uaid)
	{
		alert("Unable to add send - No from uaid");
		return false;
	}
	
	if(!message || (message &&  message.length == 0))
	{
		alert("Please provide a message");
		return false;
	}
	//alert(subject);
	//alert(message);
	
	if(!subject)
		subject = "Message from user";

	AjaxRequest.post(	
		{
			'url':'/exchange/messages.html',
			'parameters':{ 
				'send_msg':true, 
				'crypt_to_id':to_uaid,
				'crypt_from_id':from_uaid,
				'type':'U',
				'subject':subject,
				'message':message
				},
			'onSuccess': function(oResponse)
					 {
					 	getObj("msg_form").innerHTML = "<B>Message Sent.</B>";
					 },
			'onError': function(oResponse)
					 {
					 	alert("Unable send message to user - "+oResponse.error);
					 }
		} 
	);
}

function clearPrivateMessage2()
{
	getObj("pm_message").value = "";
	getObj("pm_subject").value = "";
	
}

function editIndustries()
{
	if( getObj("industryList").style.display == '' )
	{
		selDisp = '';
		listDisp = 'none';
		editDisp = '';
		action = 'Back to List';
	}
	else
	{
		selDisp = 'none';
		listDisp = '';
		editDisp = 'none';
		action = 'Edit Industries';
	}
	
	inner = '';
	
	for( i = 0; i < 6; i++ )
	{
		if( getObj("industrySelect"+i).value )
		{
			inner += getObj("industrySelect"+i).options[getObj("industrySelect"+i).selectedIndex].text;
			inner += "<br>";
		}
		
		getObj("industrySelect"+i).style.display = selDisp;
	}
	
	//alert(inner);
	
	getObj("industryEditAction").innerHTML = action;
	getObj("industryList").style.display = listDisp;
	getObj("industryList").innerHTML = inner;
	getObj("editIndustries").style.display = editDisp;
}

function saveIndustries( indIndex, indValue )
{
	inlineEditCallback();	
	//saveChanges();
}


function modifyIndustryList( newText )
{
	//alert('try to set industryList'+listPos+' to '+newText);
	getObj("industryList").innerHTML += "<br>"+newText;
}
