/*************************** shared functions   *****************************************************/
function showBusy(id)
{
	var id_new = id;
	$(id_new).toggle();	
}



/*************************** main applicant checks *****************************************************/
function checkEmailMainApp(id,busy) {
	
	//use this var to empty the email field if already enrolled
	id_val = id;
	
	var email = document.getElementById(id);
	if(email.value==''){
		alert('Please enter your email address!');
		return false;
	}
	var str = 'email='+email.value+'&class_id='+id_val;
	id_email = busy;
	showBusy(id_email);
	var myAjax = new Ajax.Request(newsletter_url, { method: 'post', parameters: str, onComplete:showResponseMainApp });
}

function checkGrecMainApp(id,busy) {
	var grec = document.getElementById(id);
	var str = 'grec='+grec.value+'&';
	id_grec = busy;
	showBusy(id_grec);
	var myAjaxGrec = new Ajax.Request(newsletter_url, { method: 'post', parameters: str, onComplete:showResponseMainGrec });
}


function showResponseMainApp(originalRequest)
{
	if (originalRequest.responseText != false)
	{
		if (originalRequest.responseText == '1')
		{
			
			alert('This person is already enrolled on this course');
			document.getElementById(id_val).value = '';
			
		}
		else
		{
			//split main string
			var msg_array = originalRequest.responseText.split("&");
			var first_name = msg_array[0].split("=");
			var last_name = msg_array[1].split("=");
			var student_id = msg_array[2].split("=");
			var logged = msg_array[3].split("=");
	
			var msg = "We found a match for the email address you provided.\n If these are your details hit OK.\n If they are incorrect please change the email address your provided.\n";
			msg += 'First Name: ' + first_name[1] + '\n';
			msg += 'Last Name: ' + last_name[1] + '\n';
			
			var agree=confirm(msg);
			
			if(agree)
			{	if(logged[1]=='1'){
					window.location = '/student/student_edit.php';
				}else{
					alert('You will need to log in then use "My Account" to sign up for e-mail updates');
					window.location = '/student/index.php';
				}
			}
		}
	}
	else
	{
		document.getElementById('details').style.display = 'block';
		document.getElementById('grec').style.display = 'block';
	}
	
	showBusy(id_email);

}

function showResponseMainGrec(originalRequest)
{
	//if there is a match
	if (originalRequest.responseText != false)
	{
		var msg = "We found a match for the grec number you provided.\n GREC numbers must be unique.\n If you are sure this is your number then you it must be associated with another email address.\n Please go back and change the email address.";
		alert(msg);
	}
	else
	{
		document.getElementById('details').style.display = 'block';
		document.getElementById('extras').style.display = 'block';
	}
	
	showBusy(id_grec);

}
//checkEmail('email1','busy1');
/*************************** sub applicant checks *****************************************************/
function checkEmail(id,busy) {
	
	//use this var to empty the email field if already enrolled
	id_val = id;
	
	//get email address and set string
	var email = document.getElementById(id).value;
	var str = 'email='+email+'&class_id='+id_val;
	
	//set busy icon to global and show it
	id_email = busy;
	showBusy(id_email);
	
	//send request
	var myAjax = new Ajax.Request(email_url, { method: 'post', parameters: str, onComplete:showResponse });
}

// Access Methods /////////////////////////
function checkGrec(id,busy) {
	var grec = document.getElementById(id).value;
	var str = 'grec='+grec+'&';
	id_grec = busy;
	showBusy(id_grec);
	var myAjaxGrec = new Ajax.Request(grec_url, { method: 'post', parameters: str, onComplete:showResponseGrec });
}

function showResponse(originalRequest)
{
	//if there is a match
	if (originalRequest.responseText != false)
	{
		if (originalRequest.responseText == '1')
		{
			
			alert('This person is already enrolled on this course');
			document.getElementById(id_val).value = '';
			
		}
		else
		{
		
			//split main string
			var msg_array = originalRequest.responseText.split("&");
	
			//split sub-strings
			var first_name = msg_array[0].split("=");
			var last_name = msg_array[1].split("=");
	
			var msg = "We found a match for the email address you provided.\n If these are your details hit OK.\n If they are incorrect please change the email address your provided.\n";
			msg += 'First Name: ' + first_name[1] + '\n';
			msg += 'Last Name: ' + last_name[1] + '\n';
			
			var agree=confirm(msg);
		}
		
		
	}
	else
	{
		//call function to display the grec field
		adjustRows('nameSet','grec');
	}
	
	//hide busy icon
	showBusy(id_email);

}

function showResponseGrec(originalRequest)
{	
	//if there is a match
	if (originalRequest.responseText != false)
	{
		var msg = "We found a match for the grec number you provided.\n GREC numbers must be unique.\n If you are sure this is your number then you it must be associated with another email address.\n Please go back and change the email address.";
		alert(msg);
	}
	//show first name, last name fields
	else
	{
		//call function to display the grec field
		adjustRows('nameSet','names');
	}
	showBusy(id_grec);

}
