$(document).ready(function(){
	
	//member login
	$("#loginform-dialog").dialog({
		autoOpen: false,
		height: 200,
		width: 450,
		modal: true,
		show: 'clip',
		hide: 'clip',
		buttons: {
			'Ok': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#loginform').contents().find("input:text").val('');
			$('#loginform').contents().find("input:password").val('');
			$('#email').focus();
		}
	});

	if($('#loginform').length > 0)
	{ 
		$('#loginform').submit(function(e){
			e.preventDefault();

				$('#loginform-dialog').dialog('open');
				$('#loginform-dialog').html('<div id="login-status"><p>Please wait while we log you in...</p><div class="progressbar"></div></div>');
				$('.ui-dialog-buttonpane').css('display','none');

					$.ajax({
						type: 'POST',
						url: $(this).attr('action'),
						data: $('#loginform').serialize(),
						async: true,
						success: function(msg){
							
							var resp = msg.split('||');
							switch(resp[0])
							{
								case "success":

									if(resp[1]=="services")
									{
										window.open('google.com', '_self');
									} else {
										//console.log[resp[2]];
										window.open(resp[2], '_self');
									}

								break;

								case "invalid":

									$('.ui-dialog-buttonpane').css('display','block');
									$('#loginform-dialog').attr('title','Invalid Credentials');
									$('#loginform-dialog').html(resp[1]);
								break;

								case "membership":
									$('.ui-dialog-buttonpane').css('display','block');
									$('#loginform-dialog').attr('title','Membership Problem');
									$('#loginform-dialog').html(resp[1]);
								break;
								case "retains_invalid":
									
									$('.ui-dialog-buttonpane').css('display','block');
									$('#loginform-dialog').attr('title','RETAINS Subscription Problem');
									$('#loginform-dialog').html(resp[1]);
								break;
								case "needinfo":
									//alert('You are being redirected to our Registration page because we need to gather a few more details');
									window.open('/users/register/commcenter/', '_self');
								break;
							}
						}
					});
			
		});
	}
	
	//student login
	$("#loginform_student-dialog").dialog({
		autoOpen: false,
		height: 200,
		width: 450,
		modal: true,
		show: 'clip',
		hide: 'clip',
		buttons: {
			'Ok': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#loginform_student').contents().find("input:text").val('');
			$('#loginform_student').contents().find("input:password").val('');
			$('#email_student').focus();
		}
	});

	if($('#loginform_student').length > 0)
	{ 
		$('#loginform_student').submit(function(e){

			e.preventDefault();

				$('#loginform_student-dialog').dialog('open');
				$('#loginform_student-dialog').html('<div id="login-status"><p>Please wait while we log you in...</p><div class="progressbar"></div></div>');
				$('.ui-dialog-buttonpane').css('display','none');
					$.ajax({
						type: 'POST',
						url: $(this).attr('action'),
						data: $('#loginform_student').serialize(),
						async: true,
						success: function(msg){
							
							var resp = msg.split('||');
							switch(resp[0])
							{
								case "success":

									if(resp[1]=="services")
									{
										window.open('google.com', '_self');
									} else {
										//console.log[resp[2]];
										window.open(resp[2], '_self');
									}

								break;

								case "invalid":

									$('.ui-dialog-buttonpane').css('display','block');
									$('#loginform_student-dialog').attr('title','Invalid Credentials');
									$('#loginform_student-dialog').html(resp[1]);
								break;

								case "membership":
									$('.ui-dialog-buttonpane').css('display','block');
									$('#loginform_student-dialog').attr('title','Membership Problem');
									$('#loginform_student-dialog').html(resp[1]);
								break;
								case "retains_invalid":
									
									$('.ui-dialog-buttonpane').css('display','block');
									$('#loginform_student-dialog').attr('title','RETAINS Subscription Problem');
									$('#loginform_student-dialog').html(resp[1]);
								break;
								case "needinfo":
									//alert('You are being redirected to our Registration page because we need to gather a few more details');
									window.open('/users/register/commcenter/', '_self');
								break;
							}
						}
					});
			
		});
	}
	

});

