
// This is to check that we can check after the person hits enter for member login
function checkEnterMemberLogin(event){

	
	if(event.keyCode==13){
		$("#clickToSubmit").click();
	}
}

// This is to submit a request to email the member their password
function resetPassword()
{
  
	// Get the base url and then send the details out  
	var baseurl = window.location.protocol + "//" +  window.location.host;
	
	var url= baseurl + "/cmmem/ajaxmemlib.php";
	
	
	
	$.post(
		url, // this is the url to send
		{ 
		email: 		$("#userid").val(),
		functionName:	"reset password"
		},
		function (returndata){
		
			alert (returndata);
			
		}
		);

}

function changeIfArtist()
{
	
	// Get the base url and then send the details out  
	var baseurl = window.location.protocol + "//" +  window.location.host;
	
	var url= baseurl + "/cmmem/ajaxmemlib.php";	    

	$.post(
		url, // this is the url to send
		{ 
		userid: 		$("#userid").val(),
		functionName:		"get membership type" 
		},
		
		function (returnData){
		
		  	var str = returnData;
	      	var pos = str.indexOf("artist");

			if (pos >= 0)
			{
	    		$("#artistProfile").show();
	    		$("#artistProfile").html('<a class="headerMenu" href="smartyindex.php?functionname=pubArt">Artist Profile </a><br>');
				
			}       
			else{
				$("#artistProfile").html("");
			}
			
		}
	);	    	
   	
	
}

function setMusicDirectory()
{
	
	// Get the base url and then send the details out  
	var baseurl = window.location.protocol + "//" +  window.location.host;
	
	var url= baseurl + "/wbsea/ajaxsealib.php";	    
	var pagename = "Music Directory";
	
	
	
	$.post(
		url, // this is the url to send
		{ 
		pagename: 		pagename,
		functionName:		"get page seq" 
		},
		
		function (returnData){
		
			var htmlMusicDirectory = '<a class="headerMenu" href="smartyindex.php?pageseq=' + returnData + '">Music Directory </a><br>';
	    	$("#musicDirectory").show();
	    	$("#musicDirectory").html(htmlMusicDirectory);		
			
		}
	);	    	
   	
	
}


function setChangePassword()
{
	
	// Get the base url and then send the details out  
	var baseurl = window.location.protocol + "//" +  window.location.host;
	
	var url= baseurl + "/wbsea/ajaxsealib.php";	    
	var pagename = "Change Password" ;
	$.post(
		url, // this is the url to send
		{ 
		pagename:	pagename,
		functionName: 	"get page seq" 
				
		},
		
		function (returnData){
		
			var htmlChangePassword = '<a class="headerMenu" href="smartyindex.php?pageseq=' + returnData + '">Change Password </a><br>';
			$("#changePassword").show();
	    	$("#changePassword").html(htmlChangePassword);		
		}
	);	    	
   	
	
}


$(document).ready(function(){
     
     	
 		$("#spanUsername").hide();
 		$("#memberMenu").hide();
 		$("#tableWelcome").hide();
 		$("#artistProfile").hide();
 		
 		$("#password").keydown (checkEnterMemberLogin);
		
		$("#clickToSubmit").click(function() {
	     	
	    	//alert("test - pretending it logged in");
	    	
	    	
			// Get the base url and then send the details out  
			var baseurl = window.location.protocol + "//" +  window.location.host;
			
			var url= baseurl + "/cmlog/sessionae.php";	    	
	    	
	    	
	    	
	    	
			$.post(
				url, // this is the url to send
				{ 
				userid: 		$("#userid").val(), 
				password: 		$("#password").val(),
				ajax:			"yes" 
				},
				function (returnData){
				
					var firstName = returnData.substring(9,returnData.length);
					
					lastChars = firstName.length - 11;
					
					firstName = firstName.substring(0,lastChars);
					
			      	
			      	var str = returnData;
			      	var pos = str.indexOf("SUCCESS");
					if (pos >= 0)
					{

						$("#login").hide();
						$("#tableMemberLogin").hide();
						
						var htmlTableWelcome = '<table cellspacing="0px" style="width:248px">';
						htmlTableWelcome = htmlTableWelcome + '<tr>';
						htmlTableWelcome = htmlTableWelcome + '	<td class="headerCnr" style="background-image: url(\'/images/leftHeadCnr.gif\');background-repeat:no-repeat;"></td>';
						htmlTableWelcome = htmlTableWelcome + '	<td class="sideHeaderBody">WELCOME...</td>';
						htmlTableWelcome = htmlTableWelcome + '	<td class="headerCnr" style="background-image: url(\'/images/rightHeadCnr.gif\');background-repeat:no-repeat;"></td>';
						htmlTableWelcome = htmlTableWelcome + '	</tr>';
						htmlTableWelcome = htmlTableWelcome + '</table>';

						$("#tableWelcome").show();
				    	$("#tableWelcome").html(htmlTableWelcome);
				    	
				    	$("#spanUsername").show();
				    	$("#spanUsername").html(firstName);
				    	
				    	
				    	$("#memberMenu").show();
				    	
				    	// have to use ajax to find the pagseq for music directory and then set the html
				    	setMusicDirectory();
				    	
				    	// have to use ajax to find the pagseq for change password and then set the html
				    	setChangePassword();
				    	
				    	var htmlLogout = '<a class="headerMenu" href="/cmlib/smartyindex.php?loginAction=out">Logout </a>';
     					htmlLogout = htmlLogout + '<br>';
     					
				    	$("#logout").show();
				    	$("#logout").html(htmlLogout);
										    						
					
						

				    	
				    	// show artist profile if an artist
				    	changeIfArtist();
				    	
				    	
					}       
					else{
						var posFail = str.indexOf("FAILURE");
						if (posFail >= 0){
							alert (returnData);
						}else{
							window.location = returnData;
						}
					}
					
				}
			);	    	
	    	
	    	
	    	
	    	
	    	
	   });

   
 	});
