/*************************************************************************************************************************
// File:	Registration.js
// Author: 	Matthew Hammond Mah, CondeNet
// Date: 	4/2/2007
// Summary: This javascript library is intended for the generic registration process that is used across all of CondeNet's
// 			desitnation sites (Style.com,Mens.Style.com,Concierge.com,Epicurious.com)
//
*************************************************************************************************************************/

var currEntryId;

function UserInfo () {
}

UserInfo.USERNAME_COOKIE = 'amg_user_info';

UserInfo.prototype.getUserName =
	function () {
		var cookieVal = ReadCookie(UserInfo.USERNAME_COOKIE);
		if (cookieVal == '') {
			return null;
		}

		return cookieVal;
        };

UserInfo.prototype.isLoggedIn =
	function () {
		return this.getUserName() != null;
	};

var agt=navigator.userAgent.toLowerCase();
/*var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;*/
var is_safari = true;

/*
YAHOO.condenet.registration.init = function() {
	this.userInfo = new UserInfo();
	
	/*
	// toggleSignIn() is responsible for hiding/showing the hidden sign in module
	toggleSignIn = function (){		
		if(signInMod.cfg.getProperty("visible"))
		{
			signInMod.hide(); 
		}
		else
		{		
			if(is_safari)
			{
				window.location = "/user/login"
			}
			else
			{
				signInMod.show(); 
				showLoginButton();
				//signInMod.cfg.setProperty("context", ["signedOutMod", "tl", "bl"]);
			}
		}					
	}
	
	
	// toggleRegLite() is responsible for hiding/showing the hidden reg lite module
	toggleRegLite =  function(){
		
		if(regLiteMod.cfg.getProperty("visible"))
			regLiteMod.hide(); 
		else
		{
			regLiteMod.show(); 
			//regLiteMod.cfg.setProperty("fixedcenter", true);
		}
	}
	
	showJoinNowAndComment = function() {
		signinOrRegisterMod.hide();
		
		if (regLiteMod != null) {							
			regLiteMod.show();	
			regLiteMod.cfg.setProperty("fixedcenter", true);			
			
		} else {
			alert('Please wait until the page completes loading');
		}						
	}
	*/
	/*
	var showLoginButton = function () {
		$('logInBtn').show();
	}
	
	this.showLoginButton = showLoginButton;
	
	
	var hideLoginButton = function () {
		$('logInBtn').hide();
	}
	
	this.hideLoginButton = hideLoginButton;
	
	
	sorClose = function() {
		signinOrRegisterMod.hide();			
		commentCommand.cleanup();
	}
	
	socClose = function() {
		signinAndCommentMod.hide();
				
		signInAndCommentCommand.cleanup();
	}
	
	this.showSignInAndComment = function() {
		signinOrRegisterMod.hide();
		signinAndCommentMod.show();
		//signinAndCommentMod.cfg.setProperty("fixedcenter", true);
				
		signInAndCommentCommand.setup();				
	}
			
	this.successfulSignIn = function () {
		toggleSignIn();
		this.displaySignedIn();
	}

	// If the user successfully signs in, this method performs the UI changes
	this.displaySignedIn = function () {				
		//var username = document.getElementById('user').value;		
		var username = this.userInfo.getUserName();
		var signedOutMod = document.getElementById('signedOutMod');
		//var signedInMod = document.getElementById('signInMod');
		var signedInMod = document.getElementById('signedInMod');

		if (signedOutMod != null) {
			//signedOutMod.hide();
			signedOutMod.style.display = 'none';
		}
		
		signedInMod.style.display = 'block';

		document.getElementById('userId').innerHTML = username;				
	}

	// signIn() is responsible for signing the user in and changing the display of the sign in module
	this.signIn = function (){		
		loginCommand.execute();				
	}

	// signOut() is responsible for signing the user out and changing the display of the sign in module
	this.signOut = function (){	
		hideLoginButton();
		logoutCommand.execute();				
	}

	this.successfulSignOut = function () {
		this.displaySignedOut();
	}
	
	this.clearLoginFields = function () {
		$('user').value = '';
		$('pass').value = '';
	}

	
}

// register the init() funciton so it is called on load

YAHOO.util.Event.addListener(window, 'load', YAHOO.condenet.registration.init);

*/

/*************************************************************************************************************************
// Function: 	validateUsername(checkId)
// Parameters:	checkId - used to determine which username field is being validated
// Author: 		Matthew Hammond Mah, CondeNet
// Date: 		4/2/2007
// Summary:		This function is used whereever a user name must be validated (reg full, reg lite).  It checks min length,
//				then makes an AJAX call to the backend to determine if the user name is available.  Once the AJAX call
//				returns, it updates the appropriate error/success message div.  The AJAX call is made using Prototype.
//
**************************************************************************************************************************/

function validateUsername(checkId) {
	var userName = document.getElementById('userName' + checkId).value;
	var userNameErrorMsg = document.getElementById('userNameErrorMsg' + checkId);
	
	userName
	
	if(userName.length < 4)
	{
		userNameErrorMsg.className = 'unavailable';
		userNameErrorMsg.innerHTML = "user name must be at least 4 characters!";
		userNameErrorMsg.style.display = 'inline';
	}
	
	
	
	
	/**
	 *************************************************************************************************************************
	 **  Old code BP 06/04/2007
	 *************************************************************************************************************************	
	else
	{
		function displayAppropriateMessage(usernameTaken) {
			if (usernameTaken) {
				userNameErrorMsg.className = 'unavailable';
				userNameErrorMsg.innerHTML = "user name is not available!";
				userNameErrorMsg.style.display = 'inline';		
			} else {
				userNameErrorMsg.className = 'available';
				userNameErrorMsg.innerHTML = "user name is available!";
				userNameErrorMsg.style.display = 'inline';
			}
		}

		function handleUsernameTakenStatus(registrationInfo) {
			UserOperationAndCommentService.isUsernameTaken(registrationInfo.username, 
								displayAppropriateMessage);
		}


		createRegistrationInfo(handleUsernameTakenStatus);			
	}
	******* End Old code BP 06/04/2007
	*************************************************************************************************************************/

}

/*************************************************************************************************************************
// Function: 	validateUsernameFull(checkId)
// Parameters:	checkId - used to determine which username field is being validated
// Author: 	Paul Lee, CondeNet
// Date: 	5/24/2007
// Summary:	This function is similar to the above only for the full registration page. 
**************************************************************************************************************************/

/**
 *************************************************************************************************************************
 **  Old code BP 06/04/2007
 *************************************************************************************************************************
function validateUsernameFull(checkId) {
	var userName = document.getElementById('userName' + checkId).value;
	var userNameErrorMsg = document.getElementById('userNameErrorMsg' + checkId);
	if(userName.length < 4) {
		userNameErrorMsg.className = 'unavailable';
		userNameErrorMsg.innerHTML = "user name must be at least 4 characters!";
		userNameErrorMsg.style.display = 'inline';
	} else {
		function displayAppropriateMessage(usernameTaken) {
			if (usernameTaken) {
				userNameErrorMsg.className = 'unavailable';
				userNameErrorMsg.innerHTML = "user name is not available!";
				userNameErrorMsg.style.display = 'inline';		
			} else {
				userNameErrorMsg.className = 'available';
				userNameErrorMsg.innerHTML = "user name is available!";
				userNameErrorMsg.style.display = 'inline';
			}
		}

		function handleUsernameTakenStatus(userName) {
			UserOperationAndCommentService.isUsernameTaken(userName, 
								displayAppropriateMessage);
		}
		handleUsernameTakenStatus(userName);

	}
}
******* End Old code BP 06/04/2007
*************************************************************************************************************************/

/*************************************************************************************************************************
// Function: 	validatePassword(passId)
// Parameters:	passId - used to determine the correct password field and error/success message div id
// Author: 		Matthew Hammond Mah, CondeNet
// Date: 		4/2/2007
// Summary:		This function is used to make sure the password is at least six chars and only consists of alpha numeric	// 				chars (plus the underscore char)
//
**************************************************************************************************************************/
/**
 *************************************************************************************************************************
 **  Old code BP 06/04/2007
 *************************************************************************************************************************
function validatePassword(passId)
{
	var alphaNumRegEx = /\W/;
	var passwordTxtId = document.getElementById(passId);
	var passwordTxt = passwordTxtId.value;
	var passwordErrorMsg = document.getElementById(passId + 'ErrorMsg');
	var theMsg = '';
	
	if(passwordTxt.length < 6)
	{
		passwordErrorMsg.className = 'unavailable';
		passwordErrorMsg.innerHTML = "must be at least 6 characters!";
		passwordErrorMsg.style.display = 'inline';
	}
	else
	{
		if(alphaNumRegEx.test(passwordTxt))
		{
			passwordErrorMsg.className = 'unavailable';
			passwordErrorMsg.innerHTML = "please only enter alpha numeric characters!";
			passwordErrorMsg.style.display = 'inline';
		}
		else
		{
			passwordErrorMsg.className = '';
			passwordErrorMsg.innerHTML = "must be at least 6 characters";
		}

	}

	
}
******* End Old code BP 06/04/2007
*************************************************************************************************************************/


/*************************************************************************************************************************
// Function: 	confirmPassword(passId)
// Parameters:	passId - used to determine the correct conf password field and conf error/success message div id
// Author: 		Matthew Hammond Mah, CondeNet
// Date: 		4/2/2007
// Summary:		This function is used to make sure the conf password matches the original password
//
**************************************************************************************************************************/
/**
 *************************************************************************************************************************
 **  Old code BP 06/04/2007
 *************************************************************************************************************************
function confirmPassword(passId,confPassId)
{
	var password = document.getElementById(passId);
	var confPassword = document.getElementById(confPassId);
	var confPasswordErrorMsg = document.getElementById(confPassId + 'ErrorMsg');
	
	if(password.value.toUpperCase() != confPassword.value.toUpperCase())
	{
		confPasswordErrorMsg.className = 'unavailable';
		confPasswordErrorMsg.innerHTML = "passwords do not match!";
		confPasswordErrorMsg.style.display = 'inline';
	}
	else
	{
		confPasswordErrorMsg.className = 'available';
		confPasswordErrorMsg.innerHTML = "passwords match!";
		confPasswordErrorMsg.style.display = 'inline';
	}
}
******* End Old code BP 06/04/2007
*************************************************************************************************************************/


/*************************************************************************************************************************
// Function: 	validateEmail(emailFieldId)
// Parameters:	emailFieldId - used to determine the correct email field and email error/success message div id
// Author: 		Matthew Hammond Mah, CondeNet
// Date: 		4/2/2007
// Summary:		This function is used to make sure email is of correct form
//
**************************************************************************************************************************/

/**
 *************************************************************************************************************************
 **  Old code BP 06/04/2007
 *************************************************************************************************************************
function validateEmail(emailFieldId)
{
	var emailId = document.getElementById('email' + emailFieldId);
	var emailTxt = emailId.value;
	var emailErrorMsg = document.getElementById('emailMsg' + emailFieldId);
	
	var emailFilter=/^.+@.+\..{2,3}$/;
	//var emailFilter=/^.+@.+\..+$/;
	if (!(emailFilter.test(emailTxt))) 
	{				 
		emailErrorMsg.className = 'unavailable';
		emailErrorMsg.innerHTML = "Please enter a valid email address!";
		emailErrorMsg.style.display = 'inline';
	}
	else
	{
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
		if (emailTxt.match(illegalChars)) {
			emailErrorMsg.className = 'unavailable';
			emailErrorMsg.innerHTML = "The email address contains illegal characters.!";
			emailErrorMsg.style.display = 'inline';
	
		}
		else
		{
			emailErrorMsg.className = '';
			emailErrorMsg.innerHTML = "valid format: yourname@yoursp.com";
			emailErrorMsg.style.display = 'inline';
		}
	}
	

}
******* End Old code BP 06/04/2007
*************************************************************************************************************************/

























































