var sessionId = null;
var profileId = null;

function notARegularMember( link, redirect ) {
	alert( "You must be a registered Job Board individual member at AsianMBA.org to post your resume.  Please log in or register now if you are not already a Job Board member." );
	url = SYSTEM_URL + "/main/frame.php?page_mode=link&main=members&sub=signup";
	if ( link ) url+= "?link=" + encodeURI( link );
	if ( redirect ) window.location = url;
}

function notAnEmployerMember( link, redirect ) {
	alert( "You must be a registered Job Board employer member at AsianMBA.org to do this.  Please log in or register now if you don't already have an Job Board employer account." );
	url = SYSTEM_URL + "/main/frame.php?page_mode=link&main=members&sub=signup";
	if ( link ) url+= "?link=" + encodeURI( link );
	if ( redirect ) window.location = url;
}

function checkLoginId( loginId ) {
	if ( loginId.length == 0 ) {
		alert( "Invalid login IDddfd" );
		return;
	}
	var ajax = new Ajax();
	ajax.post("../profile/profile_individual/edit_profile.php", checkLoginCallback, [
		["type", "check_login"],
		["loginId", encodeURI( loginId )]
	] );
}

function clearLoginId() {
	var login = document.register.loginId;
	var pwd = document.register.password;
	var pwd2 = document.register.confirm_password;
	pwd.value = pwd2.value = login.value = "";
	pwd.className = pwd2.className = "simpleform_ro";
	pwd.disabled = pwd2.disabled = true;
	login.className = "simpleform";
	login.disabled = false;
	document.getElementById( "check_btn" ).style.display = "";
	document.getElementById( "clear_btn" ).style.display = "none";
	login.focus();
}

function checkLoginCallback( msg ) {
	var login = document.register.loginId;
	if ( msg == "ok" ) {
		alert( "This login ID is available" );
		var pwd = document.register.password;
		var pwd2 = document.register.confirm_password;
		pwd.className = pwd2.className = "simpleform";
		pwd.disabled = pwd2.disabled = false;
		login.className = "simpleform_ro";
		login.disabled = true;
		document.getElementById( "check_btn" ).style.display = "none";
		document.getElementById( "clear_btn" ).style.display = "";
		pwd.focus();
	} else {
		alert( msg );
		login.focus();
		login.select();
	}
}

function addFavouriteCompany( sessionId, profileId ) {
	if ( confirm( "Click OK to add this employer to your list of favorite companies." )) {
		var ajax = new Ajax();
        ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "basic", [
			["type", "add_fav_company"],
			["sessionId", encodeURI( sessionId )],
			["profileId", encodeURI( profileId )]
		] );
		alert( "Employer added to your list of favorite companies." );
	}
}

function removeFavouriteCompany( sessionId, profileId, favoriteCompanyId ) {
	if ( confirm( "Click OK to remove this employer from your list of favorite companies." )) {
		var ajax = new Ajax();
        ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "basic", [
			["type", "remove_fav_company"],
			["sessionId", encodeURI( sessionId )],
			["profileId", encodeURI( profileId )],
			["favoriteCompanyId", encodeURI( favoriteCompanyId )]
		] );
		alert( "Employer removed from your list of favorite companies." );
	}
}
/*
function saveJob( sessionId, profileId, jobId ) {
	if ( confirm( "Click OK to save this job to your profile." )) {
		var ajax = new Ajax();
		ajax.post2html( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", "jobboard", [
			["type", "save_job"], 
			["sessionId", encodeURI( sessionId )], 
			["profileId", encodeURI( profileId )], 
			["jobId", encodeURI( jobId )]
		] );
		alert( "Job successfully saved to your profile." );
	}
}
*/
/*
function sendContactEmail( profileId, fromName, toName, toAddress, footer ) {
	var text = document.getElementById( "invite_text_" + toAddress ).value;
	var subject = "Invitation from " + fromName + " on AsianLife.com";
	var ajax = new Ajax();
	ajax.post( "../functions/send_email.php", null, [
		["profileId", encodeURI( profileId )], 
		["toAddress", encodeURI( toAddress )], 
		["toName", encodeURI( toName )], 
		["subject", encodeURI ( subject )],
		["footer", encodeURI ( footer )],
		["text", encodeURI( text )] 
	] );
	alert( "Your email invitation has been sent." );
	return false;
}
*/
function sendContactEmail( form ) {
	form.action.value = "Invite";
	form.submit();
}
/*
function removeSection( profileId, sessionId, sectionId ) {
	if ( confirm( "Click OK to remove this item from your profile.  You can add it back by using the 'Account Options' function." )) {
		document.getElementById( sectionId ).style.display = "none";
		var ajax = new Ajax();
        ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", null, [
			["type", "display"],
			["profileId", encodeURI( profileId )],
			["sessionId", encodeURI( sessionId )], 
			["sectionId", encodeURI( sectionId )]
		] );
	}
}
*/

function showBulletins( profileId, memberId, bulletinStart ) {
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_bulletin/edit_bulletin.php", "bulletin", [
		["action", "show"],
		["profileId", encodeURI( profileId )], 
		["memberId", encodeURI( memberId )], 
		["start", encodeURI( bulletinStart )] 
	] );
}

function showBulletins_open( profileId, memberId, bulletinStart, b_no, bulletinId ) {
    var ajax = new Ajax();
    ajax.post2html( SYSTEM_URL + "/profile/profile_bulletin/edit_bulletin.php", "bulletin", [
        ["action", "show_open"],
        ["profileId", encodeURI( profileId )], 
        ["memberId", encodeURI( memberId )], 
        ["start", encodeURI( bulletinStart )],
        ["b_no", encodeURI( b_no )],
        ["bulletinId", encodeURI( bulletinId )]
         
    ] );
}

function deleteBulletin( profileId, memberId, bulletinId ) {
	if ( confirm( "Click OK to permanently delete this bulletin message." )) {
		var ajax = new Ajax();
		ajax.post2html( SYSTEM_URL + "/profile/profile_bulletin/edit_bulletin.php", "bulletin", [
			["action", "delete"],
			["profileId", encodeURI( profileId )], 
			["memberId", encodeURI( memberId )], 
			["bulletinId", encodeURI( bulletinId )] 
		] );
	}
}

function writeBulletin( profileId, memberId ) { 
	var form = document.getElementById( "bulletin_form" );
	if ( form.bulletin_text.value.length == 0 ) {
		alert( "No text was entered in the bulletin." );
		form.bulletin_text.focus();
		return false;
	}
	var ajax = new Ajax();
	ajax.post2html( SYSTEM_URL + "/profile/profile_bulletin/edit_bulletin.php", "bulletin", [
		["action", "add"],
		["profileId", encodeURI( profileId )], 
		["memberId", encodeURI( memberId )], 
		["text", encodeURI( form.bulletin_text.value )] 
	] );
	form.submit.style.color = "#dddddd";
	form.submit.disabled = true;
	return false;
}

function removeJob( sessionId, profileId, jobId ) {
	if ( confirm( "Click OK to remove this job posting from your save area." )) {
		var ajax = new Ajax();
		ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", callbackRefresh, [
			["type", "remove_job"], 
			["profileId", encodeURI( profileId )], 
			["jobId", encodeURI( jobId )] 
		] );
	}
}

function removeResume( sessionId, profileId, resumeId ) {
	if ( confirm( "Click OK to delete this resume from your profile." )) {
		var ajax = new Ajax();
		ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", callbackRefresh, [
			["type", "remove_resume"], 
			["profileId", encodeURI( profileId )], 
			["resumeId", encodeURI( resumeId )]
		] );
	}
}

/*
function updateResume( profileId, sessionId, form ) {
	document.getElementById( "edit_resume_button" ).src = "../images/br_button.gif";
	xmlHttp = GetXmlHttpObject()
	if ( xmlHttp == null ) {
		alert ( "Browser does not support HTTP Request" );
		return false;
	}
	var categories = form.elements( "category[]" );
	var subcategories = form.elements( "subcategory[]" );
	var subcategoryList = new Array();
	for ( i = 0; i < subcategories.length; i++ ) {
		if ( subcategories[i] instanceof HTMLSelectElement ) {
			subcategoryList[i] = subcategories[i].options[subcategories[i].selectedIndex].value;
		} else {
			subcategoryList[i] = 0;
		}

	}
	ajax.post( "edit_resume.php", jobsStateChanged, "profileId", encodeURI( profileId )], ["sessionId", encodeURI( sessionId ) +
		["year_experience", encodeURI( form.yearexperience.options[form.yearexperience.selectedIndex].value )], ["salary", encodeURI( form.salary.options[form.salary.selectedIndex].value ) + 
		["job_type", encodeURI( form.jobtype.options[form.jobtype.selectedIndex].value )], ["job_status", encodeURI( form.jobstatus.options[form.jobstatus.selectedIndex].value ) +
		["category[0]", encodeURI( categories[0].options[categories[0].selectedIndex].value )], ["category[1]", encodeURI( categories[1].options[categories[1].selectedIndex].value )], ["category[2]", encodeURI( categories[2].options[categories[2].selectedIndex].value ) + 
		["subcategory[0]", encodeURI( subcategoryList[0] )], ["subcategory[1]", encodeURI( subcategoryList[1] )], ["subcategory[2]", encodeURI( subcategoryList[2] ) + 
		["comments", encodeURI( form.comments.value )
	);
	return false;
}
*/


function selectProfileTab( id ) {
	var network = document.getElementById( "network" );
	var groups = document.getElementById( "groups" );
	if ( id == "network" ) {
		network.style.display = "";
		groups.style.display = "none";
	} else if ( id == "groups" ) {
		network.style.display = "none";
		groups.style.display = "";
	}
}

function selectContactCountry( countryCode ) {
	if ( countryCode == "US" ) {
		document.getElementById( "edit_postal_code" ).innerHTML = "ZIP Code";
	} else {
		document.getElementById( "edit_postal_code" ).innerHTML = "Postal Code";
	}
	selectCountry( countryCode, "edit" );
}

function selectCountry( countryCode, prefix ) {
	if ( countryCode == "US" ) {
		if(document.getElementById( prefix + "_state" )) document.getElementById( prefix + "_state" ).style.display = "";
		if(document.getElementById( prefix + "_region" )) document.getElementById( prefix + "_region" ).style.display = "none";
		if(document.getElementById( prefix + "_province" )) document.getElementById( prefix + "_province" ).style.display = "none";
	} else if ( countryCode == "CA" ) {
		if(document.getElementById( prefix + "_state" )) document.getElementById( prefix + "_state" ).style.display = "none";
		if(document.getElementById( prefix + "_region" )) document.getElementById( prefix + "_region" ).style.display = "none";
		if(document.getElementById( prefix + "_province" )) document.getElementById( prefix + "_province" ).style.display = "";
	} else {
		if(document.getElementById( prefix + "_state" )) document.getElementById( prefix + "_state" ).style.display = "none";
		if(document.getElementById( prefix + "_region" )) document.getElementById( prefix + "_region" ).style.display = "";
		if(document.getElementById( prefix + "_province" )) document.getElementById( prefix + "_province" ).style.display = "none";
	}
}

function setPublic() {
	var open = document.getElementById( "openaccess" );
	if ( document.getElementById( "pubaccess" ).checked ) {
		open.checked = false;
		open.disabled = false;
	} else {
		open.checked = true;
		open.disabled = true;
	}
}
function showOption( option, checked ) {
	var option = document.getElementById( "show" + option );
	if ( checked ) {
		option.disabled = false;
		option.checked = true;
	} else {
		option.checked = false;
		option.disabled = true;
	}
}
function toggleProfile( id ) {
	var item = document.getElementById( id );
	if ( item.style.display == "none" ) {
		item.style.display = "";
	} else {
		item.style.display = "none";
	}
}

function toggleSection( id, link ) {
	var item = document.getElementById( id );
	if ( item.style.display == "none" ) {
		item.style.display = "";
		link.innerHTML = "-";
	} else {
		item.style.display = "none";
		link.innerHTML = "+";
	}
}

function sendInvitations( form ) {
	form.action = "Invite";
	if ( contacts_check( form )) {
		form.submit();
	}
}

function select_contacts( checked ) {
	var checks = document.getElementsByName( "no[]" );
	for ( i = 0; i < checks.length; i++ ) {
		checks[i].checked = checked;
	}
}
function contacts_check( form ) {
	var checks = document.getElementsByName( "no[]" );
	for ( i = 0; i < checks.length; i++ ) {
		if ( checks[i].checked ) {
			return true;
		}
	}
	alert( "No items selected." );
	return false;
}

function recommendPerson( fromMemberId, toMemberId, sessionId ) {
    var description = document.getElementById( "reference_description" ).value;
	if ( description ) {
		var ajax = new Ajax();
		ajax.post2html( SYSTEM_URL + "/profile/profile_individual/recommend_member_process.php", "reference", [
			["action", "send"],
			["fromMemberId", encodeURI( fromMemberId )], 
			["toMemberId", encodeURI( toMemberId )], 
			["sessionId", encodeURI( sessionId )], 
			["description", encodeURI( description )] 
		] );
		alert( "Your recommendation has been sent." );
	} else {
		alert( "Reference cannot be empty." );
	}
}


function viewRecommend( toMemberId, currentPos, type, sessionId ) {
    var ajax = new Ajax();
    ajax.post2html( SYSTEM_URL + "/profile/profile_individual/view_recommend.php", "reference", [
        ["action", "send"],
        ["toMemberId", encodeURI( toMemberId )], 
        ["sessionId", encodeURI( sessionId )], 
        ["type", encodeURI( type )], 
        ["currentPos", encodeURI( currentPos )] 
    ] );
}

function sendApplication( sessionId, profileId, jobId, subject, message, resumeId ) {
    var ajax = new Ajax();
    ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", callbackRefresh, [
		["type", "send_application"], 
		["sessionId", encodeURI( sessionId )], 
		["profileId", encodeURI( profileId )], 
		["resumeId", encodeURI( resumeId )], 
		["jobId", encodeURI( jobId )],
		["subject", encodeURI( subject )],
		["message", encodeURI( message )],
    ] );
}

function saveJob( sessionId, profileId, jobId ) {
	if ( confirm( "Click OK to save this job posting." )) {
		var ajax = new Ajax();
		ajax.post( SYSTEM_URL + "/profile/profile_individual/edit_profile.php", callbackRefresh, [
			["type", "save_job"], 
			["sessionId", encodeURI( sessionId )], 
			["profileId", encodeURI( profileId )], 
			["jobId", encodeURI( jobId )] 
		] ); 
	}
}


function callbackRefresh( msg ) {
	alert( msg );
	window.location.reload( true );
}

function toggle( id ) 
{
    var item = document.getElementById( id );
	if ( item.style.display == "none" ) 
    {
		item.style.display = "";
	} 
    else 
    {
		item.style.display = "none";
	}
}

