/* function to check  */

function submitLogin(objForm, hidRequest)
{	
	Loader();

	/* To assign a memory to a variable */
	var mail,email;
	/* To save value within variable */
	mail=objForm.txtEmailAdd.value;
	/* To create a regular expression for email validation */
	emailRegEx = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	/* To string conversion */

	if(objForm.txtEmailAdd.value == '' )
	{
		hiddenFloatingDiv('alertDiv');
		/* Error message to display blank email address */
		var strMessage = EMPTYADDRESS;

		var strButtonAction = '<tr>' +
				'<td valign="top" align="center">' +
				'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\');setFocus('+ objForm.name +', \'txtEmailAdd\')">&nbsp;' +
				'</td>' +
			'</tr>';
		
		alertBox(strMessage, strButtonAction);
	}
	else if(!emailRegEx.test(objForm.txtEmailAdd.value))
	{
		hiddenFloatingDiv('alertDiv');
		/* Error message to display invalid email address */
		var strMessage = INVALIDADDRESS;

		var strButtonAction = '<tr>' +
				'<td valign="top" align="center">' +
				'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\');setFocus('+ objForm.name +', \'txtEmailAdd\')">&nbsp;' +
				'</td>' +
			'</tr>';
		
		alertBox(strMessage, strButtonAction);

		/* Set focus to email address field */
		objForm.txtEmailAdd.value = "";
		objForm.txtPwd.value = "";
		objForm.chkRememberPass.checked = false;
	}
	else if(objForm.txtPwd.value == '' )
	{
		hiddenFloatingDiv('alertDiv');
		/* Error message to display blank password */
		var strMessage = EMPTYPASSWORD;

		var strButtonAction = '<tr>' +
				'<td valign="top" align="center">' +
				'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\');setFocus('+ objForm.name +', \'txtPwd\')">&nbsp;' +
				'</td>' +
			'</tr>';
		
		alertBox(strMessage, strButtonAction);
	}
	else
	{		
		checkUser(objForm.txtEmailAdd.value,objForm.txtPwd.value,objForm,hidRequest);
	}
}

function hideElements(objForm,strMode)
{
	if (strMode=="show")
	{
		objForm.txtPwd.style.display = "";
		objForm.cmdLogIn.style.display = "";
		objForm.cmdReg.style.display = "none";
		document.getElementById('tdRem').style.display = "";
		if(document.getElementById('lnkforgotPass').style.display == "none" )
		{
			document.getElementById('lnkforgotPass').style.display = "";
		}
	}
	else if(strMode == "hide")
	{		
		objForm.txtPwd.style.display = "none";		
		objForm.cmdLogIn.style.display = "none";
		objForm.cmdReg.style.display = "";
		//document.getElementById('tdRem').style.display = "none";
		//$('tdRem').fadeOu("slow1");
		//document.getElementById('lnkforgotPass').style.display = "none";
		//objForm.lnkforgotPass.style.display = "none";		
	}
}
function showMessage()
{
	document.getElementById("PasswordExpire").style.display = "";
}
function hideMessage()
{
	document.getElementById("PasswordExpire").style.display = "none";
}


//this function checks user authentication & depending on its rights user is directed to appropriate pages.
function checkUser(strEmailAdd,strPwd,objForm,hidRequest)
{	
	var rememberPwd;
	if (objForm.chkRememberPass.checked == true)
	{
		rememberPwd = "1";
	}
	else 
	{
		rememberPwd = "0";
	}
		
	strParams = "&UserEmail=" + strEmailAdd +"&UserPwd="+ strPwd + "&strRemeber=" + rememberPwd +"&hidRequest="+hidRequest;	
	
	/* Initial Ajax class */
	$.ajax({
		/* Assign ajax processing file to class variable */
		url: DIRSERVERNAME+'loginProcess.php',
		/* Assign ajax type to class variable */
		type: 'post',
		/* Assign ajax parameters to class variable */
		data:	 strParams,
		/* Assign ajax time out value to class variable */
		timeout: 6000,
		/* Assign on error function to class variable */
		error: function(strReponseResult){ showAjaxError(); },
		/* Assign on success function to class variable */
		success: function(strReponseResult){			
			//document.write(strReponseResult);			
			//alert(strReponseResult);
			var arrResult = strReponseResult.split("$#$");
			//alert(arrResult[0]);
			
			if (strReponseResult == "InvalidUser")
			{
				hiddenFloatingDiv('alertDiv');
				/* Error message to display blank password */
				var strMessage = IDPASSWORDNOTMATCH;

				var strButtonAction = '<tr>' +
					'<td valign="top" align="center">' +
					'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\'); setFocus('+ objForm.name +', \'txtEmailAdd\')">&nbsp;' +
					'</td>' +
				'</tr>';
				
				alertBox(strMessage, strButtonAction);
				objForm.txtEmailAdd.value = "";
				objForm.txtPwd.value = "";
				objForm.chkRememberPass.checked = false;
				return false;
			}
			else if (strReponseResult == "alreadyLogIn")
			{
				hiddenFloatingDiv('alertDiv');
				//alert("fgf");
				var strEmailID = objForm.txtEmailAdd.value;
				loggedOut(objForm.name ,strEmailID);
				//
				/* Error message to display blank password */
				//var strMessage = "You have currently logged in from another computer. This will cause your prior session to expire.";


				/*var strButtonAction = '<tr>' +
					'<td valign="top" align="center">' +
					'<input type="button" name="cmdOk" id="cmdOk" value="Yes" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\'); setFocus('+ objForm.name +',\'txtEmailAdd\'); loggedOut('+ objForm.name +',\''+strEmailID+'\');">&nbsp;<input type="button" name="cmdCancel" id="cmdCancel" value="No" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\'); ">' +
					'</td>' +
				'</tr>';*/
				
				//alertBox(strMessage, strButtonAction);
				//objForm.txtEmailAdd.value = "";
				//objForm.txtPwd.value = "";
				//objForm.chkRememberPass.checked = false;
				return false;
			}
			else if (strReponseResult == "ExpirePassword")
			{
				hiddenFloatingDiv('alertDiv');
				document.getElementById("trExpirePass").style.display = "";
				document.getElementById("NewPassword").style.display = "none"; 
				document.getElementById("noNewPass").style.display = "none";	
				document.getElementById("PasswordSent").style.display = "none";	
				document.getElementById("lnkforgotPass").style.display = "none";
				objForm.hidRequest.value = '';				
				return false;
			}
			else if(strReponseResult == "PasswordSent")
			{
				hiddenFloatingDiv('alertDiv');				
				document.getElementById("NewPassword").style.display = "none"; 
				document.getElementById("noNewPass").style.display = "none";
				document.getElementById("trExpirePass").style.display = "none";
				document.getElementById("PasswordSent").style.display = "";
				document.getElementById("lnkforgotPass").style.display = "none";								
				return false;				
			}
			else if (strReponseResult == "NewPassword")
			{
				hiddenFloatingDiv('alertDiv');
				document.getElementById("NewPassword").style.display = ""; 
				document.getElementById("noNewPass").style.display = "";
				document.getElementById("PasswordSent").style.display = "none";
				document.getElementById("trExpirePass").style.display = "none";
				document.getElementById("lnkforgotPass").style.display = "none";				
				objForm.hidRequest.value = 'newPass';
				return false;
			}
			else if (arrResult[0].toLowerCase() == "employee".toLowerCase() || arrResult[0].toLowerCase() == "member".toLowerCase() || arrResult[0].toLowerCase() == "student".toLowerCase() )
			{
				//alert(arrResult[2])
				if(arrResult[3] == 'Active')
				{
					//location.href = 'admin/index.php';
					if(arrResult[2] != '')
					{						
						if(objForm.hidLoc.value)
						{
							location.href="shoppingCart/shoppingCartBrowseClassesStep2.php?LocationID=" + objForm.hidLoc.value+"&classStatus=Active";
						}
						else
						{							
							location.href = arrResult[2];							
						}
					}
					else
					{
						//location.href = DIRSERVERNAME+"editUserProfile.php?strUserID=" + arrResult[1] + "";
						location.href = DIRSERVERNAME+"shoppingCart/shoppingCartBrowseClassesStep1.php";
					}
					return false;
				}
				else
				{
					hiddenFloatingDiv('alertDiv');	
				//	/* Error message to display blank password */
					var strMessage = STATUSINACTIVE;

					var strButtonAction = '<tr>' +
						'<td valign="top" align="center">' +
						'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\'); setFocus('+ objForm.name +', \'txtEmailAdd\')">&nbsp;' +
						'</td>' +
					'</tr>';
					
					alertBox(strMessage, strButtonAction);
				}
				//location.href = "usersHomePage.php?strUserID=" + arrResult[1] + "";
				return false;
			}
			/*else if ( arrResult[0].toLowerCase() == "employee".toLowerCase())
			{
				if(arrResult[3] == 'Active')
				{
					location.href = 'admin/index.php';
					return false;
				}
				else
				{
				//	/* Error message to display blank password */
				/*	var strMessage = STATUSINACTIVE;

					var strButtonAction = '<tr>' +
						'<td valign="top" align="center">' +
						'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\'); setFocus('+ objForm.name +', \'txtEmailAdd\')">&nbsp;' +
						'</td>' +
					'</tr>';
					
					alertBox(strMessage, strButtonAction);
				}
			}
			else if (arrResult[0].toLowerCase() == "member".toLowerCase() || arrResult[0].toLowerCase() == "student".toLowerCase() )
			{	
				if(arrResult[2] != '')
				{
					location.href = arrResult[2];
				}
				else
				{
					location.href = "editUserProfile.php?strUserID=" + arrResult[1] + "";
				}
				//location.href = "usersHomePage.php?strUserID=" + arrResult[1] + "";
				return false;
			}*/

		}
	});
}

function getRegister( objForm )
{
	if ( trimAll(objForm.txtEmailAdd.value) != "") 
	{
		/* Assign params to the memory variables */
		strParams = "strMode=dupEmailID&EmailID=" + objForm.txtEmailAdd.value;
		/* Initial Ajax class */
		$.ajax({
			/* Assign ajax processing file to class variable */
			url: DIRSERVERNAME+'registrationSql.php',
			/* Assign ajax type to class variable */
			type: 'GET',
			/* Assign ajax parameters to class variable */
			data: strParams,
			/* Assign ajax time out value to class variable */
			timeout: 6000,
			/* Assign on error function to class variable */
			error: function(strReponseResult){ showAjaxError(); },
			/* Assign on success function to class variable */
				success: function(strReponseResult)
				{ 
					if(strReponseResult == "0")
					{
					/* Assign success message to the memory variable */
						var strMessage = objForm.txtEmailAdd.value + ' ' + REGEMAILIDALREADYEXISTS;
						/* Assign alert box buttons with action to the memory variable */
						var strButtonAction = '<tr>' +
															'<td valign="top" align="center">' +
															'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\');setFocus('+ objForm.name +', \'txtEmailID\')">&nbsp;' +
															'</td>' +
														'</tr>';
						/* Display customize alert box */
						alertBox(strMessage, strButtonAction);
						
					}
					else if(strReponseResult == "1")
					{
							var strEmailID = objForm.txtEmailAdd.value;
							location.href = DIRSERVERNAME+'registration.php?EmailID='+strEmailID;
					}
			}
		});
	}
	else
	{
		location.href = 'registration.php';
	}
}
function signupEvent(e)
{
	var characterCode;
	 if(e && e.which)
	 {
		 e = e;
		 characterCode = e.which;
	}
	else
	{
		 e = event;
		 characterCode = e.keyCode;
	}
	 if(characterCode == 13)
	 {
		submitLogin(frmLoginScreen);
		 return false;
	}
	return true;
}


function loggedOut( objForm,loginID )
{
	
	/* Assign params to the memory variables */
	strParams = "strMode=loggedout&loginID="+loginID;
	/* Initial Ajax class */
	$.ajax({
		/* Assign ajax processing file to class variable */
		url: DIRSERVERNAME+'automaticLogout.php',
		/* Assign ajax type to class variable */
		type: 'GET',
		/* Assign ajax parameters to class variable */
		data: strParams,
		/* Assign ajax time out value to class variable */
		timeout: 6000,
		/* Assign on error function to class variable */
		error: function(strReponseResult){ showAjaxError(); },
		/* Assign on success function to class variable */
			success: function(strReponseResult)
			{ 
				//alert(strReponseResult);
				var arrResult = strReponseResult.split("$#$");
				if (arrResult[0].toLowerCase() == "employee".toLowerCase() || arrResult[0].toLowerCase() == "member".toLowerCase() || arrResult[0].toLowerCase() == "student".toLowerCase() )
				{	
					//alert(arrResult[2])
					if(arrResult[3] == 'Active')
					{
						//location.href = 'admin/index.php';
						if(arrResult[2] != '')
						{
							if(objForm.hidLoc)							
							{
								if(objForm.hidLoc.value)
								{
									location.href="shoppingCart/shoppingCartBrowseClassesStep2.php?LocationID=" + objForm.hidLoc.value+"&classStatus=Active";
								}
							}
							else
							{							
								//location.href = arrResult[2];			
								location.href = DIRSERVERNAME+"shoppingCart/shoppingCartBrowseClassesStep1.php";
							}
						}
						else
						{
							//location.href = DIRSERVERNAME+"editUserProfile.php?strUserID=" + arrResult[1] + "";
							location.href = DIRSERVERNAME+"shoppingCart/shoppingCartBrowseClassesStep1.php";
						}
						return false;
					}
					else
					{
					//	/* Error message to display blank password */
						var strMessage = STATUSINACTIVE;

						var strButtonAction = '<tr>' +
							'<td valign="top" align="center">' +
							'<input type="button" name="cmdOk" id="cmdOk" value="Ok" class="btn" onClick="javascript: hiddenFloatingDiv(\'alertDiv\'); setFocus('+ objForm.name +', \'txtEmailAdd\')">&nbsp;' +
							'</td>' +
						'</tr>';
						
						alertBox(strMessage, strButtonAction);
					}
					//location.href = "usersHomePage.php?strUserID=" + arrResult[1] + "";
					return false;
				}
			}
		});	
}
