/*global $, document */function set_captcha() {
	$.post('/join_ajax/set_captcha', {}, 
		function(data) {
			
			if (data.sError) {
				$.prompt(data.sError);
				return;
			}
			
			show_captcha(data.sString);
			
			$('#captcha_data').data('captcha_string', data.sString);
		}, 
		'json'
	);
}

function show_captcha(sId) {
	var img = '/join_ajax/get_captcha/'+sId;
	
	$("#captch_sum").attr('src', img);
}



function join_tbn(oObj, sPath) {
	var sCaptchaString = $('#captcha_data').data('captcha_string');
	
	var sJoinEmail = $('#join_email').val();
	
	var sJoinEmailRetype = $('#join_email_retype').val();
	
	var sJoinPassword = $('#join_password').val();
	
	var sJoinTitle = $('#join_title').val();
	
	var sJoinFirstName = $('#join_fname').val();
	
	var sJoinLastName = $('#join_lname').val();
	
	var sJoinCompany = $('#join_company').val();
	
	var sJoinIndustry = $('#join_industry').val();
	
	var sJoinPosition = $('#join_position').val();
	
	var sJoinCountry = $('#join_country').val();
	
	var sJoinCaptcha = $('#join_captcha').val();
	
	if (sJoinEmail === '') {
		$.prompt(oSiteMessages.tbn_alert_missing_email);
		return;
	}
	
	if (validate_email(sJoinEmail) === false) {
		$.prompt(oSiteMessages.tbn_alert_invalid_email);
		return;
	}
	
	if (sJoinEmail !== sJoinEmailRetype) {
		$.prompt(oSiteMessages.tbn_alert_emails_differ);
		return;
	}
	
	if (sJoinPassword === '') {
		$.prompt(oSiteMessages.tbn_alert_missing_password);
		return;
	}
	
	if (sJoinPassword.length < oSiteVariables.min_password_length) {
		var sMessage = oSiteMessages.tbn_alert_password_length_short;
		sMessage = sMessage.replace(/__length__/g, oSiteVariables.min_password_length);
		$.prompt(sMessage);
		return;
	}
	
	if (sJoinTitle === '') {
		$.prompt(oSiteMessages.tbn_alert_profile_enter_title);
		return;
	}
	
	if (sJoinFirstName === '') {
		$.prompt(oSiteMessages.tbn_alert_profile_enter_fname);
		return;
	}
	
	if (sJoinLastName === '') {
		$.prompt(oSiteMessages.tbn_alert_profile_enter_lname);
		return;
	}
	
	if (sJoinCompany === '') {
		$.prompt(oSiteMessages.tbn_alert_profile_enter_company);
		return;
	}
	
	if (sJoinIndustry === '') {
		$.prompt(oSiteMessages.tbn_alert_profile_enter_industry);
		return;
	}
	
	if (sJoinPosition === '') {
		$.prompt(oSiteMessages.tbn_alert_profile_enter_position);
		return;
	}
	
	if (sJoinCountry === '') {
		$.prompt(oSiteMessages.tbn_alert_profile_enter_country);
		return;
	}
	
	
	var oPassthru = {
		sCaptchaString: sCaptchaString,
		sJoinEmail: sJoinEmail,
		sJoinEmailRetype: sJoinEmailRetype,
		sJoinPassword: sJoinPassword,
		sJoinTitle: sJoinTitle,
		sJoinFirstName: sJoinFirstName,
		sJoinLastName: sJoinLastName,
		sJoinCompany: sJoinCompany,
		sJoinIndustry: sJoinIndustry,
		sJoinPosition: sJoinPosition,
		sJoinCountry: sJoinCountry,
		sJoinCaptcha: sJoinCaptcha
	};
	
	
	$.post('/join_ajax/join_tbn', oPassthru, function(data) {
		if (data.sError) {
			$.prompt(data.sError);
			return;
		}
		
		
		if (data.sUserValid === 'Y') {
			$.prompt(oSiteMessages.tbn_alert_reg_complete, {
				prompt_callback : function() {
					location.href = sPath;	
				}
			});
			
		}
	}, 'json');
	
	
}


function join_suggest_industry(oObj, iIndustryParentId) {
	
	if (iIndustryParentId === -1) {
		iIndustryParentId = $('#profile_industry').val();
	}
	
	var sInputName = 'profile_suggest_industry';
	
	$.prompt(oSiteMessages.tbn_alert_profile_suggest_industry, {
		prompt_buttons : {
			ok : 'ok',
			cancel : 'cancel'
		},
		prompt_loaded : function() {
			if($('#'+sInputName).length == 0) {
				var oInput = document.createElement('input');
				
				$(oInput).attr({ type : 'text', name : sInputName, id : sInputName, size : '25', maxlength : '40' });
				
				var oDiv = document.createElement('div');
				
				$(oDiv).append(oInput).appendTo($('#tbn_prompt_cont'));
			}
			
			$('#'+sInputName).focus();
		},
		prompt_callback : function(sSubmit) {
			if (sSubmit === 'ok') {
				var sIndustryName = $('#' + sInputName).val();
				
				if (sIndustryName === '') {
					$.prompt(oSiteMessages.tbn_alert_industry_suggestion_missing);
					return;	
				}
				
				$.post('/profile_ajax/profile_suggest_industry', { iIndustryParentId : iIndustryParentId, sIndustryName : sIndustryName }, function(data) {
					if (data.sError) {
						$.prompt(data.sError);
					} else {
						$.post('/profile_ajax/get_profile_industries', { iIndustryId : data.iIndustryId, iIndustryParentId : data.iIndustryParentId }, function(data) {
							
							if (data.sError) {
								$.prompt(data.sError);
								return;
							}							
							
							$('#join_industry').html(data.sContent);
						}, 'json');
						$.prompt(data.sMessage);
					}
				}, 'json');
				
			}
		}
	});
};




function join_suggest_position(oObj) {
	
	var sInputName = 'profile_suggest_position';
	
	$.prompt(oSiteMessages.tbn_alert_profile_suggest_position, {
		prompt_buttons : {
			ok : 'ok',
			cancel : 'cancel'
		},
		prompt_loaded : function() {
			if($('#'+sInputName).length == 0) {
				var oInput = document.createElement('input');
				
				$(oInput).attr({ type : 'text', name : sInputName, id : sInputName, size : '25', maxlength : '40' });
				
				var oDiv = document.createElement('div');
				
				$(oDiv).append(oInput).appendTo($('#tbn_prompt_cont'));
			}
			
			$('#'+sInputName).focus();
		},
		prompt_callback : function(sSubmit) {
			if (sSubmit === 'ok') {
				var sPositionName = $('#' + sInputName).val();
				
				if (sPositionName === '') {
					$.prompt(oSiteMessages.tbn_alert_position_suggestion_missing);
					return;	
				}
				
				$.post('/profile_ajax/profile_suggest_position', { sPositionName : sPositionName }, function(data) {
					if (data.sError) {
						$.prompt(data.sError);
					} else {
						$.post('/profile_ajax/get_profile_positions', { iPositionId : data.iPositionId }, function(data) {
							
							if (data.sError) {
								$.prompt(data.sError);
								return;
							}							
							
							$('#join_position').html(data.sContent);
						}, 'json');
						$.prompt(data.sMessage);
					}
				}, 'json');
				
			}
		}
	});
};


function captcha_help() {
	create_popup(450, 450, oSiteMessages.link_popup_help, function() {
		$.post('/join_ajax/captcha_popup', {  }, 
			function(data) {
				
				if (data.sError) {
					$.prompt(data.sError);
					return;
				}				
				
				$('.tbn_popup_container_content').html(data.sContent);
			}, 
			'json'
		);
	});
}

