// JavaScript Document

$(document).ready(function(){

	// wake up the DreamWeaver MenuBar widget
	var MenuBar2 = new Spry.Widget.MenuBar("MenuBar2", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});

	// password reset link handler
	$('#pwdReset').click(function() {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test($('#email').val()))
			alert('Please provide a valid email address');
		else {
			$("#txtAjaxMessageBox").removeClass().addClass('messagebox').text('Sending password reminder...').fadeIn(900);
			//check the username exists or not from ajax 
			
			$.getJSON("service.php",{ ajaxaction:"PASSWORD",email:$('#email').val() }, function(jsondata) 
			{
				if("SUCCESS"==jsondata.message) //if correct login detail 
				{
					$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
					{
						//add message and change the class of the box and start fading
						$(this).html('Password reminder sent.').addClass('messageboxok').fadeTo(900,1);
					});
				}
				else
				{
					$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
					{
						var msg;
						
						switch(jsondata.message)
						{
							case "NOTEXIST":
								msg = "This email address has not been registered.<br />Click <a href=\"register.php\">HERE</a> to register for database access.";
								break;
							case "EXPIRED":
								msg = "Access for this email address has expired.<br />Please contact Engleman Associates to request an extension.";
								break;
							default:
								msg = "Unknown error sending reminder: " + jsondata.message + ".<br />Please contact Engleman Associates for assistance";
								break;
						}
						//add message and change the class of the box and start fading
						$(this).html(msg).addClass('messageboxerror').fadeTo(900,1);
					});

				}
				
			});
		}
		
	});


	// may be able to simplify by setting form action to jscript void(0) and dynamically changing, functionalizing cookie set, etc
 	//set up form processing
	$("#login_form").validate({
		submitHandler: function(form) {

//	$("#login_form").submit(function() {

			if ("none"!=$("#user_id").val()) 
			{
				return true;
			} 
			else 
			{
				if("yes"==$("#activate").val())
				{
					$("#activate").val("no");
					
					//remove all the class add the messagebox classes and start fading
					$("#txtAjaxMessageBox").removeClass().addClass('messagebox').text('Activating....').fadeIn(900);
					//check the username exists or not from ajax 
					
					$.getJSON("service.php",{ ajaxaction:"ACTIVATE",email:$('#email').val(),pass:$('#pass').val() }, function(jsondata) 
					{
						if("SUCCESS"==jsondata.message||"ALREADYACTIVATED"==jsondata.message) //if correct login detail 
						{
							$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
							{
								//add message and change the class of the box and start fading
								$(this).html('Loading...').addClass('messageboxok').fadeTo(900,1);
							});
							//alert(jsondata.userId);
							$("#user_id").val(jsondata.userId);
							
							var sessEnd = new Date();
							sessEnd.setMinutes(sessEnd.getMinutes() + 1440);  
		
							// get isInternal, clientIP, userId from web service. also put in hidden form fields for cookieless
							var sscookie = "";
							
							sscookie = "softselect=loggedin=y";
							sscookie += "&isInternal=" + jsondata.isInternal;
							sscookie += "&clientIP=" + jsondata.clientIP;
							sscookie += "&userId=" + jsondata.userId;
							sscookie += "&sessionExpires=" + sessEnd.toDateString() + " " + sessEnd.getHours() + ":" + sessEnd.getMinutes() + ";";
							
							document.cookie = sscookie;
							document.cookie = "sstimeout=tomin=1440;";	
							
							form.submit();
						}
						else
						{
							$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
							{
								var msg;
								
								switch(jsondata.message)
								{
									case "ALREADYACTIVATED":
										msg = "This email address has already been activated.<br />Enter your password to login.";
										break;
									case "NOTEXIST":
										msg = "This email address has not been registered.<br />Click <a href=\"register.php\">HERE</a> to register for database access.";
										break;
									case "EXPIRED":
										msg = "Access for this email address has expired.<br />Please contact Engleman Associates to request an extension.";
										break;
									default:
										msg = "Unknown error activating login: " + jsondata.message + ".<br />Please contact Engleman Associates for assistance";
										break;
									
								}
								//add message and change the class of the box and start fading
								$(this).html(msg).addClass('messageboxerror').fadeTo(900,1);
							});
		
							$("#user_id").val("none");
						}
					}); 

				}
				else
				{
					//remove all the class add the messagebox classes and start fading
					$("#txtAjaxMessageBox").removeClass().addClass('messagebox').text('Validating....').fadeIn(900);
					//check the username exists or not from ajax 
					
					$.getJSON("service.php",{ ajaxaction:"LOGIN",email:$('#email').val(),pass:$('#pass').val() }, function(jsondata) 
					{
						if("SUCCESS"==jsondata.message) //if correct login detail 
						{
							$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
							{
								//add message and change the class of the box and start fading
								$(this).html('Loading...').addClass('messageboxok').fadeTo(900,1);
							});
							
							$("#user_id").val(jsondata.userId);
							//alert($("#user_id").val());
							
							var sessEnd = new Date();
							sessEnd.setMinutes(sessEnd.getMinutes() + 1440);  
		
							// get isInternal, clientIP, userId from web service. also put in hidden form fields for cookieless
							var sscookie = "";
							
							sscookie = "softselect=loggedin=y";
							sscookie += "&isInternal=" + jsondata.isInternal;
							sscookie += "&clientIP=" + jsondata.clientIP;
							sscookie += "&userId=" + jsondata.userId;
							sscookie += "&sessionExpires=" + sessEnd.toDateString() + " " + sessEnd.getHours() + ":" + sessEnd.getMinutes() + ";";
							sscookie += " path=/;";
							
							document.cookie = sscookie;
							document.cookie = "sstimeout=tomin=1440; path=/;";	
							
							if ("y"==jsondata.isInternal||"Y"==jsondata.isInternal)
								form.action = "ss_insider.aspx";
								
							form.submit();
						}
						else
						{
							$("#txtAjaxMessageBox").fadeTo(200,0.1,function() //start fading the messagebox
							{
								var msg;
								
								switch(jsondata.message)
								{
									case "WRONGPW":
										msg = "Incorrect password - please try again.<br />Click the Forgot Password link below to send a password reminder to your registered email address.";
										break;
									case "UNACTIVATED":
										msg = "This email address has not yet been activated.<br />Check your email for activation instructions.";
										break;
									case "UNREGISTERED":
										msg = "This email address has not been registered.<br />Click <a href=\"register.php\">HERE</a> to register for database access.";
										break;
									case "EXPIRED":
										msg = "Access for this email address has expired.<br />Please contact Engleman Associates to request an extension.";
										break;
									default:
										msg = "Unknown error validating login: " + jsondata.message + ".<br />Please contact Engleman Associates for assistance";
										break;
									
								}
								//add message and change the class of the box and start fading
								$(this).html(msg).addClass('messageboxerror').fadeTo(900,1);
							});
		
							$("#user_id").val("none");
						}
					}); 
				}
				return false; // post form to report page if authed
			}
		} 
	});


	if("yes"==$("#activate").val())
	{
		$("#login_form").submit();	
	}
});

