// Global Virtual Classroom (functions) //look at var process

var field_type;
var id;

function gvc_ca(field_type, id){
	var opt = {
		method:'post', 
		postBody:'process=check_availability&field=' + field_type + '&value=' + $F(id),
		onSuccess: function (t) { update_check_div(t, field_type, $F(id)); }
	}
	new Ajax.Request('process.php', opt);
		
	function update_check_div(t, field_type, email){
		if(t.responseText == "0"){
			//Email already registered
			$('EMAIL_CHECK').style.display = "block"; 
			$(field_type  + '_CHECK').innerHTML = email + ' is already registered';
			$('EMAIL').value = "";
			$('EMAIL').focus();
		} else {
			$('EMAIL_CHECK').style.display = "none"; 
			$('EMAIL').value = email;
		}
	}
}

//Checking every field for a value
var fields;
function check_if_empty(fields){
	var gen_err_msg = "Required field cannot be empty";
	for (i=0; i<fields.length; i++){
		if ($F(fields[i]) == ""){ 
			$(fields[i] + '_CHECK').innerHTML = gen_err_msg; 	$(fields[i] + '_CHECK').style.display = "block";
			$response = true;
		} else {
			$(fields[i] + '_CHECK').innerHTML = ""; 	$(fields[i] + '_CHECK').style.display = "none";
			$response = false;
		}
	}
	return $response;
}

function create_new_account(){
	//Basic error checking
	var fields = new Array ("TITLE", "FIRST_NAME", "LAST_NAME", "EMAIL", "PASSWORD", "PASSWORD_CONFIRM", "PHONE_WORK", "COUNTRY", "UNIVERSITY_AFFILIATION", "EXPERTISE", "BRIEF_BIO", "MEMBER_TYPE");
	var proceed = "Yes";
	
	if (!check_if_empty(fields)){
		if(($F('PASSWORD').length > 0) && ($F('PASSWORD').length < 6)){
			$('PASSWORD_CHECK').style.display = "block"; 
			$('PASSWORD_CHECK').innerHTML = "The password should be minimum 6 characters long. Please re-enter your password.";		
			proceed = "No";
		}
	
		if($F('PASSWORD') !== $F('PASSWORD_CONFIRM')){
			$('PASSWORD_CONFIRM_CHECK').style.display = "block"; 
			$('PASSWORD_CONFIRM_CHECK').innerHTML = "Password does not match. Please re-enter your password.";		
			proceed = "No";
		} else {
			proceed = "Yes";
		}
		
		if (proceed == "Yes"){
			var opt = {
				method:'post', 
				postBody:'process=create_new_account&' + Form.serialize('new_account_form'),
				onSuccess: function(t) { $('CREATE_ACC_BTN').value = "Create my account";  $('CREATE_ACC_BTN').disabled = false; handle_create_account(t); },
				onLoading: function() { $('CREATE_ACC_BTN').value = "Processing..."; $('CREATE_ACC_BTN').disabled = true; }
			}
			new Ajax.Request('process.php', opt);
				
			function handle_create_account(t){
				var data = t.responseText;
				$('page_header').innerHTML = "REGISTRATION SUCCESSFUL";
				$('page_description').innerHTML = "Congratulations! You have successfully registered to the GVC system.";			
				$('new_account_form_div').innerHTML = data;
			}
		}
	}
}

function create_student_profile(){
	//Basic error checking
	var fields = new Array ("TITLE", "FIRST_NAME", "LAST_NAME", "PASSWORD", "PASSWORD_CONFIRM", "UNIVERSITY_AFFILIATION", "EXPERTISE", "BRIEF_BIO");
	var proceed = "Yes";
	
	if (!check_if_empty(fields)){
		if(($F('PASSWORD').length > 0) && ($F('PASSWORD').length < 6)){
			$('PASSWORD_CHECK').style.display = "block"; 
			$('PASSWORD_CHECK').innerHTML = "The password should be minimum 6 characters long. Please re-enter your password.";		
			proceed = "No";
		}
	
		if($F('PASSWORD') !== $F('PASSWORD_CONFIRM')){
			$('PASSWORD_CONFIRM_CHECK').style.display = "block"; 
			$('PASSWORD_CONFIRM_CHECK').innerHTML = "Password does not match. Please re-enter your password.";		
			proceed = "No";
		} else {
			proceed = "Yes";
		}
		
		if (proceed == "Yes"){
			var opt = {
				method:'post', 
				postBody:'process=create_student_profile&' + Form.serialize('student_account_form'),
				onSuccess: function(t) { $('CREATE_STUDENT_ACC_BTN').value = "Create my account";  $('CREATE_STUDENT_ACC_BTN').disabled = false; handle_create_student_profile(t); },
				onLoading: function() { $('CREATE_STUDENT_ACC_BTN').value = "Processing..."; $('CREATE_STUDENT_ACC_BTN').disabled = true; }
			}
			new Ajax.Request('process.php', opt);
				
			function handle_create_student_profile(t){
				var data = t.responseText;
				$('page_header').innerHTML = "PROFILE CREATED SUCCESSFULLY";
				$('student_account_form_div').innerHTML = data;
			}
		}
	}
}

function clearform(form_name_partial){
	Form.reset(form_name_partial + '_form');
	Element.hide(form_name_partial + '_block');
}

//Request a collaborative project listing
function req_prj(){
	var fields = new Array ("TITLE", "PRJ_DESCRIPTION", "COLLABORATING_FACULTY", "START_DATE_TIME", "END_DATE_TIME");

	if (!check_if_empty(fields)){
		var opt = {
			method:'post', 
			postBody:'process=req_prj&' + Form.serialize('req_prj_form'),
			onSuccess: function(t) { $('req_prj_btn').disabled = false; handle_req_prj(t); },
			onLoading: function() { $('req_prj_btn').value = "Processing..."; $('req_prj_btn').disabled = true; }
		}
		new Ajax.Request('process.php', opt);
			
		function handle_req_prj(t){
			clearform('req_prj');
			$('req_prj_btn').value = "Submit";
			var data = t.responseText;
			$('response').innerHTML = t.responseText;
			$('no_prjs').innerHTML = "";
		}
	}
}


//Handles Key actions in a form
function handlekeydown(field, evt, fn){
	var keyCode = evt.which ? evt.which : evt.keyCode;
	if (keyCode == 13) {//ENTER KEY
		if (fn == "login"){
			login();
		} else if (fn == "req_prj"){
			req_prj();	
		}
		return false;
	} else if(keyCode == 27) {//ESC KEY
		if (fn == "req_prj"){
			clearform('req_prj');		
		} else if (fn == "welcome_note"){
			clearform('welcome_note');		
		}
	} else
	return true;
}

var activation_key;
var member_id;

function account_activation(activation_key,member_id){
	var opt = {
		method:'post', 
		postBody:'process=activate_account&ACTIVATION_KEY=' + activation_key + '&MEMBER_ID=' + member_id,
		onSuccess: function (t) { handle_activate_account(t); }
	}
	new Ajax.Request('process.php', opt);
		
	function handle_activate_account(t){
		var data = t.responseText;
		$('page_header').innerHTML = "Account Activation";
		$('page_description').innerHTML = "";			
		$('new_account_form_div').innerHTML = data;
	}
}

function student_account_activation(activation_key,email,pid){
	var opt = {
		method:'post', 
		postBody:'process=activate_student_account&ACTIVATION_KEY=' + activation_key + '&EMAIL=' + email+ '&PROJECT=' + pid,
		onSuccess: function (t) { handle_activate_student_account(t); }
	}
	new Ajax.Request('process.php', opt);
		
	function handle_activate_student_account(t){
		var data = t.responseText;
		$('page_header').innerHTML = "Account Activation";
		$('page_description').innerHTML = "";			
		$('new_account_form_div').innerHTML = data;
	}
}

function resend_activation(){
	var opt = {
		method:'post', 
		postBody:'process=resend_activation&EMAIL=' + $F('EMAIL'),
		onSuccess: function (t) { handle_resend_activation(t); }
	}
	new Ajax.Request('process.php', opt);
		
	function handle_resend_activation(t){
		var data = t.responseText;
		$('new_account_form_div').innerHTML = data;
	}
}

function pwd_reset_send(){
	var opt = {
		method:'post', 
		postBody:'process=pwd_reset_send&EMAIL=' + $F('EMAIL'),
		onSuccess: function (t) { handle_pwd_reset(t); }
	}
	new Ajax.Request('process.php', opt);
		
	function handle_pwd_reset(t){
		var data = t.responseText;
		$('new_account_form_div').innerHTML = data;
	}
}


function pwd_reset(){
	//Basic error checking
	var fields = new Array ("PASSWORD", "PASSWORD_CONFIRM");
	var proceed = "Yes";
	
	if (!check_if_empty(fields)){
		if(($F('PASSWORD').length > 0) && ($F('PASSWORD').length < 6)){
			$('PASSWORD_CHECK').style.display = "block"; 
			$('PASSWORD_CHECK').innerHTML = "The password should be minimum 6 characters long. Please re-enter your password.";		
			proceed = "No";
		}
	
		if($F('PASSWORD') !== $F('PASSWORD_CONFIRM')){
			$('PASSWORD_CONFIRM_CHECK').style.display = "block"; 
			$('PASSWORD_CONFIRM_CHECK').innerHTML = "Password does not match. Please re-enter your password.";		
			proceed = "No";
		} else {
			proceed = "Yes";
		}
		
		if (proceed == "Yes"){
			var opt = {
				method:'post', 
				postBody:'process=pwd_reset&' + Form.serialize('pwd_reset_form'),
				onSuccess: function(t) { handle_pwd_reset(t); }
				//onLoading: function() { $('pwd_reset_btn').value = "Processing..."; $('pwd_reset_btn').disabled = true; }
			}
			new Ajax.Request('process.php', opt);
				
			function handle_pwd_reset(t){
				var data = t.responseText;
				$('new_account_form_div').innerHTML = data;
				$('authenticated_div').innerHTML = "";
			}
		}
	}
}

function authenticate(activation_key,member_id){
	var opt = {
		method:'post', 
		postBody:'process=authenticate&ACTIVATION_KEY=' + activation_key + '&MEMBER_ID=' + member_id,
		onSuccess: function (t) { handle_authentication(t, member_id); }
	}
	new Ajax.Request('process.php', opt);
		
	function handle_authentication(t, member_id){
		var data = t.responseText.split("|");
		if(data[0] == 1){
			$('page_description').innerHTML = "";			
			$('new_account_form_div').innerHTML = "";
			Element.show('authenticated_div');
			$('MEMBER_ID').value = member_id;
		} else {
			$('page_description').innerHTML = "";			
			$('new_account_form_div').innerHTML = data[1];
			Element.hide('authenticated_div');
		}
	}
}

function login(){
	var fields = new Array ("EMAIL", "PASSWORD");
	
	if (!check_if_empty(fields)){
		$('AUTHENTICATION').innerHTML = '';
		Element.hide('AUTHENTICATION');		
		var opt = {
			method:'post', 
			postBody:'process=login&EMAIL=' + $F('EMAIL') + '&PASSWORD=' + $F('PASSWORD'),
			onSuccess: function(t) { handleLogin_response(t); },
			onLoading: function() { $('AUTHENTICATION').innerHML = 'Processing....'; }
		}
		new Ajax.Request('accounts/process.php', opt);
	}
}

function handleLogin_response(t){
	if(t.responseText == 1){
		window.location = 'accounts/my_account.php';
	} else if(t.responseText == -1){
		Element.show('AUTHENTICATION');
		$('AUTHENTICATION').innerHTML = 'The Email and password combination is incorrect. Please try again!';
	} else if(t.responseText == 0){
		Element.show('AUTHENTICATION');
		$('AUTHENTICATION').innerHTML = 'Account has not yet been activated. Please check your email.';
	} else if(t.responseText == -2){
		Element.show('AUTHENTICATION');
		$('AUTHENTICATION').innerHTML = '<br />Seems like you are not registered yet? <br /><a href="accounts/index.php">Please click here register now</a>.';
	}
}

//Check if any field value exists
function checkif_fieldvalue_exists(field_type, div_id){
	var opt = {
		method:'post', 
		postBody:'process=checkif_fieldvalue_exists&field=' + field_type + '&value=' + $F(div_id),
		onSuccess: function (t) { update_check_div(t, field_type); }
	}
	new Ajax.Request('process.php', opt);
		
	function update_check_div(t, field_type){
		if(t.responseText == "0"){
			$(field_type + '_CHECK').style.display = "block"; 
			$(field_type  + '_CHECK').innerHTML = 'Please verify and re-enter. The value you provided does not exist in our database';
		} else {
			$(field_type + '_CHECK').style.display = "none"; 
		}
	}
}

