var popupLoginBoxHtml = '';
popupLoginBoxHtml += 
'<form id="popupFancyForm" method="post" action="/login/validate" name="popupFancyForm">'+
'	<p id="popFancyForm"><span class="text13">Please Enter Email and Password</span></p>'+

'	<div class="listing_fancy" style = "display:none;" id = "loginMessage"></div>'+

'	<div style = "width:100px;" class="text13">Email: </div>'+
'	<div class = ""><input type="text" id="popup_email" name="popup_email" size="30" /></div>'+
'	<div class = "clear"></div>'+

'	<div style = "width:100px;" class="text13">Password: </div>'+
'	<div class = ""><input type="password" id="popup_password" name="popup_password" size="30" /></div>'+
'	<div class = "clear"></div>'+

'	<div class="button3 flt top_mar1">'+
'		<span>'+
'			<input type = "submit" value = "Login">'+
'		</span>'+
'	</div>';
'</form>';

function openLoginPoup(method, url) {
	$.fancybox(
		popupLoginBoxHtml,
		{
        	'hideOnOverlayClick': false,	
        	'modal'				: false,
        	'enableEscapeButton' : false,
        	'autoDimensions'	: false,
			'width'         	: 300,
			'height'        	: 180,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			onComplete	:	function() {
           		reinitializeLoginPopup(method, url);
			}
		}
	);
}
				
function reinitializeLoginPopup(method, url) {

	var formValidated = true;
	$("#popupFancyForm").validationEngine({
		scroll:false,
		unbindEngine:false,
		success :  function() {formValidated = true;},
		failure : function() {formValidated = false; }
	});
	$("#popupFancyForm").submit(function(e) {
		e.preventDefault();
		if (formValidated == false) {
			// Don't post the form.
		} else {
			
			var formAction = '/login/validate';
			postArray = {
						  email:$('#popup_email').val(),
						  password:$('#popup_password').val()						 
						};
			$.post(formAction, postArray,function(data) {
				var $alert = $("#loginMessage");
				if(data=='yes') {
					$.fancybox.close();
					if (method != '') {
						eval(method);
					} else if (url != '') {
						document.location = url;
					} else {
						document.location = '/';
					}
					
				} if(data=='verified') {
					document.location = '/user/register/step3';
				} else if(data == 'no') {
					toggleMessage($alert, "Please Enter Valid Email & Password!", "", "");
				} else if(data == 'email_not_verified') {
					toggleMessage($alert, "Email not verified yet...", "", "");
				} else if(data == 'inactive') {
					toggleMessage($alert, "Your access is blocked...", "", "");
				}
			});
			
		}
	});
}
