/* edit_regular_<section> functions to handle events when the user clicks the "Edit" button. 
*  Return "true" if you want the edit process to continue, return "false" if you want it to stop (i.e. for link redirects)
*/
function edit_regular_basic( elementId, profileId ) {
	puc.removeAllItems();
	return true;
}
function edit_regular_video( elementId, profileId ) {
	vuc.removeAllItems();
	return true;
}
function edit_regular_reference( elementId, profileId ) {
	window.location = SYSTEM_URL + "/main/frame.php?page_mode=link&main=profile&sub=profile&id=" + profileId + "&cate=recommend_list&tab=references";
	return false;
}
function edit_regular_education( elementId, profileId ) {
	add_education_layer.ShowLayer();
	return false;
}
function edit_regular_experience( elementId, profileId ) {
	add_experience_layer.ShowLayer();
	return false;
}

/* update_regular_<section> functions to handle events when the user clicks the "Done" button when finished editing. */
function update_regular_basic( elementId, sessionId, profileId ) {
	var form = document.forms["basic_form"];

	// Validate input.
	if ( form.givenName.value.length == 0 ) {
		alert( "Missing field: First Name" );
		form.givenName.focus();
		return false;
	}
	if ( form.familyName.value.length == 0 ) {
		alert( "Missing field: Last Name" );
		form.lastName.focus();
		return false;
	}

	var showContact = 0;

	var region = form.region.value;
	if ( form.countryCode.value == "US" ) region = form.state.value;
	if ( form.countryCode.value == "CA" ) region = form.province.value;
	if ( region.length == 0 ) {
		alert( "Missing field: Region" );
		form.region.focus();
		return false;
	}

	if ( form.showContact.checked ) showContact = 1;
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "basic", [
		["type", "basic"],
		["sessionId", encodeURI( sessionId )],
		["profileId", encodeURI( profileId )],
		["givenName", encodeURI( form.givenName.value )], 
		["familyName", encodeURI( form.familyName.value )], 
		["role", encodeURI( form.role.value )], 
		["website", encodeURI( form.website.value )], 
		["telephone", encodeURI( form.telephone.value )], 
		["cellphone", encodeURI( form.cellphone.value )],
		["fax", encodeURI( form.fax.value )], 
		["address", encodeURI( form.address.value )], 
		["city", encodeURI( form.city.value )], 
		["region", encodeURI( region )],
		["countryCode", encodeURI( form.countryCode.value )],
		["postalCode", encodeURI( form.postalCode.value )],
		["showContact", encodeURI( showContact )],
		["tempImageFile", encodeURI( tempImageFile )]
	]);
	tempImageFile = "";
	return true;
}

function update_regular_video( elementId, sessionId, profileId ) {
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "video", [
		["type", "video"],
		["sessionId", encodeURI( sessionId )],
		["profileId", encodeURI( profileId )],
		["tempVideoFile", encodeURI( tempVideoFile )]
	]);
	tempVideoFile = "";
	return true;
}

function update_regular_information( elementId, sessionId, profileId ) {
	var form = document.forms["personal_form"];
	var status = document.getElementsByName( "status[]" );
	var params = new Array();
	for ( i = 0; i < status.length; i++ ) {
		if ( status[i].checked ) {
			params.push( ["status[]", encodeURI( status[i].value )] );
		}
	}
	var showBirthday = "0";
	if ( form.showBirthday.checked ) showBirthday = "1";
	params.push( ["type", "personal"] );
	params.push( ["profileId", encodeURI( profileId )] );
	params.push( ["sessionId", encodeURI( sessionId )] );
	params.push( ["description", encodeURI( form.description.value )] );
	params.push( ["interests", encodeURI( form.interests.value )] );
	params.push( ["ethnicity", encodeURI( form.ethnicity.value )] );
	params.push( ["birth_date", encodeURI( form.birth_date.value )] ); 
	params.push( ["birth_month", encodeURI( form.birth_month.value )] ); 
	params.push( ["birth_year", encodeURI( form.birth_year.value )] );
	params.push( ["showBirthday", encodeURI( showBirthday )] );
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "information", params );
	return true;
}
/*
function updateContact( profileId, sessionId ) {
	var form = document.forms["contact_form"];
	var region = form.region.value;
	if ( form.country.value == "US" ) {
		region = form.state.value;
	}
	this.sessionId = sessionId;
	this.profileId = profileId;
	var ajax = new Ajax();
	ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", contactStateChanged, [
		["type", "contact"], 
		["profileId", encodeURI( profileId )], 
		["sessionId", encodeURI( sessionId )],
		["website", encodeURI( form.website.value )], 
		["telephone", encodeURI( form.telephone.value )], 
		["cellphone", encodeURI( form.cellphone.value )],
		["fax", encodeURI( form.fax.value )], 
		["address", encodeURI( form.address.value )], 
		["city", encodeURI( form.city.value )], 
		["region", encodeURI( region )],
		["country", encodeURI( form.country.value )],
		["postal_code", encodeURI( form.postal_code.value )]
	] );
	form.submit.className = "small_btn_process";
	form.submit.disabled = true;
	return false;
}
*/
function updateExperience( profileId, sessionId, experienceId, form ) 
{
	if ( form.company_name.value == "" ) 
    {
		alert( "Invalid company name2." );
		form.company_name.focus();
		return false;
	}
	if ( form.industry.value == "" ) 
    {
		alert( "Invalid industry." );
		form.industry.focus();
		return false;
	}
	if ( form.position.value == "" ) {
		alert( "Invalid position." );
		form.position.focus();
		return false;
	}
	var e_month = 0;
	var e_year = 0;
	var is_current = 0;
	if ( form.current_employer.checked ) 
    {
		is_current = 1;
	} 
    else 
    {        
		e_month = form.com_e_month.value;
		e_year = form.com_e_year.value;
        
	}
	var region = form.company_region.value;
	if ( form.company_country.value == "US" ) region = form.company_state.value;
	if ( form.company_country.value == "CA" ) region = form.company_province.value;
	this.sessionId = sessionId;
	this.profileId = profileId;
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "experience", [
		["type", "update_experience"], 
		["profileId", encodeURI( profileId )], 
		["sessionId", encodeURI( sessionId )], 
		["experienceId", encodeURI( experienceId )],
		["current_employer", encodeURI( is_current )], 
		["company_name", encodeURI( form.company_name.value )], 
		["industry", encodeURI( form.industry.value )],
		["position", encodeURI( form.position.value )], 
		["start_month", encodeURI( form.com_s_month.value )], 
		["start_year", encodeURI( form.com_s_year.value )], 
		["end_month", encodeURI( e_month )], 
		["end_year", encodeURI( e_year )],
		["country", encodeURI( form.company_country.value )], 
		["region", encodeURI( region )], 
		["city", encodeURI( form.company_city.value )], 
		["description", encodeURI( form.company_desc.value )]
	] );
	return false;
}

function updateEducation( profileId, sessionId, educationId, form ) {
	if ( form.school_name.value == "" ) {
		alert( "Invalid school name." );
		form.school_name.focus();
		return false;
	}
	if ( form.major.value == "" ) {
		alert( "Invalid major." );
		form.major.focus();
		return false;
	}
	if ( form.degree.value == "" ) {
		alert( "Invalid degree." );
		form.degree.focus();
		return false;
	}
	var e_month = 0;
	var e_year = 0;
	var is_current = 0;
	if ( form.current_school.checked ) {
		is_current = 1;
	} else {
		e_month = form.sch_e_month.value;
		e_year = form.sch_e_year.value;
	}
	var region = form.school_region.value;
	if ( form.school_country.value == "US" ) region = form.school_state.value;
	if ( form.school_country.value == "CA" ) region = form.school_province.value;
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "education", [
		["type", "update_education"], 
		["profileId", encodeURI( profileId )], 
		["sessionId", encodeURI( sessionId )], 
		["educationId", encodeURI( educationId )],
		["current_school", encodeURI( is_current )], 
		["school_name", encodeURI( form.school_name.value )], 
		["major", encodeURI( form.major.value )],
		["degree", encodeURI( form.degree.value )], 
		["end_month", encodeURI( e_month )], 
		["end_year", encodeURI( e_year )],
		["country", encodeURI( form.school_country.value )], 
		["region", encodeURI( region )], 
		["city", encodeURI( form.school_city.value )], 
		["description", encodeURI( form.school_desc.value )]
	] );
	return false;
}

function callbackRefresh( msg ) {
	alert( msg );
	window.location.reload( true );
}

// AJAX callback methods for updating the html.
/*
function networkStateChanged( html ) { 
	document.getElementById( "basic_info" ).innerHTML = html;
}
*/
/*
function basicStateChanged( html ) { 
	document.getElementById( "basic_info" ).innerHTML = html;
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/update_profile.php", "contact", [ ["type", "contact"], ["sessionId", encodeURI( sessionId )], ["profileId", encodeURI( profileId )]] );
	ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/update_profile.php", "professional_list", [ ["type", "experience"], ["sessionId", encodeURI( sessionId )], ["profileId", encodeURI( profileId )]] );
	if ( document.getElementById( "employer_info" )) {
		ajax = new Ajax();
		ajax.post2html( SYSTEM_URL + "/profile/profile_individual/update_profile.php", "employer_info", [ ["type", "company"], ["sessionId", encodeURI( sessionId )], ["profileId", encodeURI( profileId )]] );
	}
}

function experienceStateChanged( html ) { 
	document.getElementById( "professional_list" ).innerHTML = html;
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/update_profile.php", "basic_info", [ ["type", "basic"], ["sessionId", encodeURI( sessionId )], ["profileId", encodeURI( profileId )]] );
	ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/update_profile.php", "employer_info", [ ["type", "company"], ["sessionId", encodeURI( sessionId )], ["profileId", encodeURI( profileId )]] );
	document.getElementById( "blur" ).style.zIndex = -2;
}

function educationStateChanged( html ) { 
	document.getElementById( "education_list" ).innerHTML = html;
	document.getElementById( "blur" ).style.zIndex = -2;
}

function contactStateChanged( html ) { 
	document.getElementById( "contact" ).innerHTML = html;
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_individual/update_profile.php", "basic_info", [ ["type", "basic"], ["sessionId", encodeURI( sessionId )], ["profileId", encodeURI( profileId )]] );
}

function activateResumeStateChanged( html ) {
	alert( "You have successfully activated resume." );
	document.getElementById( "job_board" ).innerHTML = html;
}

*/
function removeExperience( profileId, sessionId, experienceId ) {
	if ( confirm( "Click OK to permanently remove this experience item from your profile." )) {
		document.getElementById( "show_experience" + experienceId ).style.display = "none";
		var ajax = new Ajax();
		ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", null, [
			["type", "remove_experience"],
			["profileId", encodeURI( profileId )], 
			["sessionId", encodeURI( sessionId )], 
			["experienceId", encodeURI( experienceId )] 
		] );
	}
}

function removeEducation( profileId, sessionId, educationId ) {
	if ( confirm( "Click OK to permanently remove this education item from your profile." )) {
		document.getElementById( "show_education" + educationId ).style.display = "none";
		var ajax = new Ajax();
		ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", null, [
			["type", "remove_education"],
			["profileId", encodeURI( profileId )], 
			["sessionId", encodeURI( sessionId )], 
			["educationId", encodeURI( educationId )] 
		] );
	}
}

function isCurrentEmployer( checked, form ) {
	if ( checked ) {
		form.com_e_month.style.visibility = "hidden";
		form.com_e_year.style.visibility = "hidden";
	} else {
		form.com_e_month.style.visibility = "visible";
		form.com_e_year.style.visibility = "visible";
		form.com_e_month.selectedIndex = 0;
		form.com_e_year.selectedIndex = 0;
	}
}

function isCurrentSchool( checked, form ) {
	if ( checked ) {
		form.sch_e_month.style.visibility = "hidden";
		form.sch_e_year.style.visibility = "hidden";
	} else {
		form.sch_e_month.style.visibility = "visible";
		form.sch_e_year.style.visibility = "visible";
		form.sch_e_month.selectedIndex = 0;
		form.sch_e_year.selectedIndex = 0;
	}
}

function applyJob( sessionId, profileId, jobId, companyName, jobTitle ) {
	document.getElementById( "showName" ).innerHTML = companyName;
	document.getElementById( "showJob" ).innerHTML = jobTitle;
	document.make_application_form.memberId.value = profileId;
	document.make_application_form.title.value = "";
	document.make_application_form.text.value = "";
	document.make_application_form.jobId.value = jobId;
	make_application_layer.ShowLayer();
}

function sendInterview( fromMemberId, toMemberId, sessionId ) {
    var title = document.getElementById( "title" ).value;
    var text = document.getElementById( "text" ).value;
	var jobElement = document.getElementById( "jobId" );
	var jobId = "";
	if ( jobElement ) jobId = jobElement.value;
    var ajax = new Ajax();
    ajax.post( SYSTEM_URL + "/profile/profile_individual/make_interview_process.php", callbackRefresh, [
        ["action", "send"],
        ["fromMemberId", encodeURI( fromMemberId )], 
        ["toMemberId", encodeURI( toMemberId )], 
        ["sessionId", encodeURI( sessionId )], 
        ["title", encodeURI( title)], 
		["jobId", encodeURI( jobId)],
        ["text", encodeURI( text)] 
    ] );
}



function sendInvitation( profileId, sessionId ) {
	var text = document.getElementById( "invite_text" ).value;
	var ajax = new Ajax();
	ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_network.php", callbackRefresh, [
		["action", "send"],
		["profileId", encodeURI( profileId )], 
		["sessionId", encodeURI( sessionId )], 
		["text", encodeURI( text )] 
	] );
}

function acceptConnection( profileId, sessionId ) {
	if ( confirm( "Click OK to accept this person into your network." )) {
		connection( "add", profileId, sessionId );
	}
}

function declineConnection( profileId, sessionId ) {
	if ( confirm( "Click OK to decline this person's network invitation." )) {
		connection( "decline", profileId, sessionId );
	}
}

function connection( action, profileId, sessionId ) {
	var ajax = new Ajax();
	ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_network.php", callbackRefresh, [
		["action", action],
		["profileId", encodeURI( profileId )], 
		["sessionId", encodeURI( sessionId )] 
	] );
}

function removeConnection( profileId, sessionId ) {
	if ( confirm( "Click OK to remove this person from your network." )) {
		connection( "remove", profileId, sessionId );
	}
}

function saveJobAgent( form ) {
	if ( form.inactive.checked == true ) {
		if ( confirm( "Click OK to deactivate your job search agent." )) {
			form.submit();
		} 
		return;
	}
	if ( form.active.checked == true ) {

		// Check that at least one parameter is set or don't activate search agent.
		if ( form.countryCode.value == "" && form.region.value == "" && form.state.value == "" && form.province.value == "" && 
			form.city.value == "" && form.careerType.value == "0" && form.jobType.value == "0" && form.jobStatus.value == "0" &&
			form.minBasePayAmount.value == "" && form.keywords.value == "" ) {

			var selected = false;
			var categories = form.elements["category[]"];
			for ( var i = 0; i < categories.length; i++ ) {
				if ( categories[i].value != "" ) selected = true; 
			}
			var languages = form.elements["language[]"];
			for ( var i = 0; i < categories.length; i++ ) {
				if ( languages[i].value != "" ) selected = true; 
			}
			if ( !selected ) {
				alert( "You must set at least one parameter to activate your job search agent." );
				return false;
			}
		}
	}
	form.submit();
}

function removePhoto( sessionId, profileId ) {
	if ( confirm( "Click OK to remove this photo from your profile." )) {
		var ajax = new Ajax();
        ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", null, [
			["type", "remove_photo"],
			["sessionId", encodeURI( sessionId )],
			["profileId", encodeURI( profileId )]
		] );
		tempImageFile = "";
		document.getElementById( "edit_regular_image" ).src = SYSTEM_URL + "/images/people_normal_logo_profile.gif";
		document.getElementById( "remove_regular_image" ).style.display = "none";
		puc.removeAllItems();
	}
}

function removeVideo() {
	if ( confirm( "Click OK to remove this video from your profile." )) {
		tempVideoFile = "";
		document.getElementById( "play_employer_video" ).innerHTML = "Upload a promotional video to appear in your profile.";
		document.getElementById( "remove_video" ).style.display = "none";
		vuc.removeAllItems();
	}
}


var tempImageFile = tempVideoFile = "";
var puc = vuc = null;

function createPhotoUploadComponent( element ) {
	preLoadImages();
	puc = new dhtmlXVaultObject();
	puc.setServerHandlers( FILE_SERVER + "/UploadHandler.php", 
		SYSTEM_URL + "/lib/lib.proxy.php?NBTUS_Path="+encodeURIComponent(FILE_SERVER + '/GetInfoHandler.php'), 
		SYSTEM_URL + "/lib/lib.proxy.php?NBTUS_Path="+encodeURIComponent(FILE_SERVER + '/GetIdHandler.php')
	);
	puc.setSize(160,100);													// content size of file uploading
	puc.setMaxSize(1024*1024);											// maximum file size
	puc.setMaxNumber(1);													// Maximum number of files
	var FileType = new Array("gif","jpg","png");						// allowed extentions
	var container = document.getElementById( "edit_regular_image" );
	puc.setAfterUpload(													// after uploading handler for profile image
		function() { 
			container.src = puc.getLastUploadedTempURL(); 
			tempImageFile = puc.getLastUploadedTempFileName();
			document.getElementById( "remove_regular_image" ).style.display = "";
			document.getElementById( element ).style.display = "none";
		}
	);
	puc.setAfterClean(				 									// after cleaning handler
		function() { 
//			if ( oldFile ) container.src = oldFile; 
			tempImageFile = "";
		} 
	);
	puc.setImagePath( SYSTEM_URL + "/extension/dhtmlxVault/imgs/" );		// image path
	puc.setFileType(FileType);
	puc.create( element, "UploadComponent", false );								// set Div object to put uploading form, set Variable name
}

function createVideoUploadComponent( element ) {
	if ( !document.getElementById( element )) return false;
	preLoadImages();
	vuc = new dhtmlXVaultObject();
	vuc.setServerHandlers( FILE_SERVER + "/UploadHandler.php", 
		SYSTEM_URL + "/lib/lib.proxy.php?NBTUS_Path="+encodeURIComponent(FILE_SERVER + '/GetInfoHandler.php'), 
		SYSTEM_URL + "/lib/lib.proxy.php?NBTUS_Path="+encodeURIComponent(FILE_SERVER + '/GetIdHandler.php')
	);
	vuc.setSize(200,100);													// content size of file uploading
	vuc.setMaxSize(1024*1024*5);												// maximum file size
	vuc.setMaxNumber(1);													// Maximum number of files
	var FileType = new Array("avi","mpg","wmv");							// allowed extentions
	vuc.setAfterUpload(													// after uploading handler for profile image
		function() { 
			tempVideoFile = vuc.getLastUploadedTempFileName();
//			vuc.removeAllItems();
		}
	);
	vuc.setAfterClean(				 									// after cleaning handler
		function() { 
			tempVideoFile = "";
		} 
	);
	vuc.setImagePath( SYSTEM_URL + "/extension/dhtmlxVault/imgs/" );		// image path
	vuc.setFileType(FileType);
	vuc.create( element, "UploadComponent",false );								// set Div object to put uploading form, set Variable name
}

function preLoadImages(){
	var imSrcAr = new Array("btn_add.gif","btn_clean.gif","btn_upload.gif","ico_file.png","ico_image.png","ico_sound.png","ico_video.png","ico_zip.png","pb_back.gif","pb_demoUload.gif","pb_empty.gif");
	var imAr = new Array(0);
	for(var i=0;i<imSrcAr.length;i++){
		imAr[imAr.length] = new Image();
		imAr[imAr.length-1].src = SYSTEM_URL + "/extension/dhtmlxVault/imgs/"+imSrcAr[i];
	}
} 



