$(document).ready(function(){
	
	
	$(".signInForm_button").live('click',function(){checkEmail_popup(1)}).live('mouseover',function(){ToolTip('Click to subscribe');}).live('mouseout',function(){UnTip();});;
	
    // bind to the form's submit event 
    $('#signInForm').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(optionsSignIn); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
	
	
	
	var optionsSignIn = { 
	
		data: { joinList: joinClick },
        //target:        '#output2',   // target element(s) to be updated with server response 
        //beforeSubmit:  showRequest,  // pre-submit callback 
        //success:       showResponse  // post-submit callback 
 		beforeSubmit: function() {
			if(fb_tab!=1){ 
				$.blockUI({ 
				css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: .5, 
					color: '#fff' 
					},
				message: '<img src="images/active_bar_trans.gif" /><br /> Saving Changes...'	
				}); 
				$.colorbox.close();
			   // $.blockUI({ message: '<h1><img src="images/active_bar_trans.gif" /><br /> Saving Changes...</h1>' });
			} else {
				window.resizeTo(500,250);
				RemoveContent('checkEmailForm');
				RemoveContent('signInForm');
				document.getElementById('signInForm_msg').innerHTML='<br /><br /><img src="../images/active_bar_fb.gif" />  Submitting...';	
			}
        },
		success:  signIn_response
		
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	
	var optionCheckEmail = { 
		data: { joinList: joinClick },
        //target:        '#output2',   // target element(s) to be updated with server response 
        //beforeSubmit:  showRequest,  // pre-submit callback 
        //success:       showResponse  // post-submit callback 
 		beforeSubmit: function() {
			if(fb_tab!=1){ 
			$.blockUI({ 
				css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: .5, 
					color: '#fff' 
					},
				message: '<img src="images/active_bar_trans.gif" /><br /> Checking Email Address...'	
				}); 
			   // $.blockUI({ message: '<h1><img src="images/active_bar_trans.gif" /><br /> Saving Changes...</h1>' });
			   $.colorbox.close();
			} else {
				document.getElementById('signInForm_msg').innerHTML='<br /><br /><img src="../images/active_bar_fb.gif" />  Checking Email Address...';	
				RemoveContent('checkEmailForm');
				RemoveContent('signInForm');
			}
        },
		success:  checkEmail_response
		
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind to the form's submit event 
    $('#checkEmailForm').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(optionCheckEmail); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
	
	
});



 
// post-submit callback 
function signIn_response(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
   //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
	
	if (responseText === 'OK'){
		var msg;
		var doormsg;
		var request = document.getElementById('requestPending').value;
		var roomSignup = document.getElementById('roomSignup');
		
		
		if (request != 1){
			//alert(roomSignup)
			if (roomSignup){
				msg = '<img src="images/icons/tick_lrg.png"  /> Sign Up Successful<span style="font-size: 16px;" class="note"><br /><br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="$.unblockUI();welcomeRedirect();" class="button" />';
				doormsg = '<strong>SIGN UP SUCCESSFUL</strong>';
				fb_msg = '<img src="../images/icons/tick.png"  /> Sign Up Successful<span style="font-size: 16px;" class="note"><br /><br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="window.close();welcomeRedirect();" class="button" />';
			} else {
				msg = '<img src="images/icons/tick_lrg.png"  /> Sign Up Successful<span style="font-size: 16px;" class="note"><br /><br />Please click on the Site Link in the email we have just sent<br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="$.unblockUI();welcomeRedirect();" class="button" />';
				doormsg = '<strong>SIGN UP SUCCESSFUL</strong><br /><span style="font-size: 12px;" class="note">Please click on the Site Link in the email we have just sent';
				fb_msg = '<img src="../images/icons/tick.png"  /> Sign Up Successful<span style="font-size: 16px;" class="note"><br /><br />You should receive a welcome email in a few moments<br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="window.close();welcomeRedirect();" class="button" />';	
			}
			
		} else {
			if(joinClick==1){
			msg = 'Signup Successful<span style="font-size: 16px;" class="note"><br /><br />Your Request Has Been Submitted.<br /><br /></span><input style="font-size: 14px;" type="button" value="OK"  onclick="$.unblockUI();welcomeRedirect();" class="button" />';
			doormsg = '<strong>SIGNUP SUCCESSFUL</strong><br /><span style="font-size: 12px;" class="note">';
			fb_msg = 'Signup Successful<span style="font-size: 16px;" class="note"><br /><br />Your Request Has Been Submitted.<br /><br /></span><input style="font-size: 14px;" type="button" value="OK"  onclick="window.close();welcomeRedirect();" class="button" />';
			} else {
			msg = 'Request Submitted<span style="font-size: 16px;" class="note"><br /><br />Your Request Has Been Submitted. You will be notified by email if access is granted.<br /><br /></span><input style="font-size: 14px;" type="button" value="OK"  onclick="$.unblockUI();welcomeRedirect();" class="button" />';
			doormsg = '<strong>REQUEST SENT SUCCESSFULLY</strong><br /><span style="font-size: 12px;" class="note">You Will Be Notified By Email With A Site Login Link If Access Is Granted';
			fb_msg = 'Request Submitted<span style="font-size: 16px;" class="note"><br /><br />Your Request Has Been Submitted. You will be notified by email if access is granted.<br /><br /></span><input style="font-size: 14px;" type="button" value="OK"  onclick="window.close();welcomeRedirect();" class="button" />';
				
			}
		}
		
		
		if(fb_tab!=1){ 
		$.blockUI({ 
			css: { 
				border: 'none', 
				padding: '15px', 
				backgroundColor: '#000', 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .8, 
				color: '#fff',
				fontSize: '2em'
				},
			message: msg	
		}); 
			makeTxt('door_msg',doormsg);
			document.getElementById('signInForm').reset();
			document.getElementById('checkEmailForm').reset();
			InsertContent('checkEmailForm');
			RemoveContent('signInForm');
			//setTimeout($.unblockUI, 2000); 
			$.colorbox.close();
			if (request != 1){
			RemoveContent('doorEntry');	
			InsertContent('emailEntry');
			}
		}  else {	
			window.resizeTo(500,250);
			RemoveContent('checkEmailForm');
			RemoveContent('signInForm');
			document.getElementById('signInForm_msg').innerHTML=fb_msg;
		}
			//IF NO A REQUEST
			//IF LOGGED IN REDIRECT INTO ROOM HERE
			//var loggedinuser = <?php // echo $_SESSION['kt_login_user']; ?>;
			
			//alert(responseText)
			//alert('Sent You A Signed Up Email');
	} else {
		if(fb_tab!=1){ 
			$.blockUI({ 
				css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: .8, 
					color: '#fff',
					fontSize: '2em'
					},
				message: '<img src="images/icons/cross.png"  /> Sign Up Failed'	
			}); 
			setTimeout('$.unblockUI();signIn_popup();', 3000);
		}  else {	
			window.resizeTo(500,250);
			RemoveContent('checkEmailForm');
			RemoveContent('signInForm');
			document.getElementById('signInForm_msg').innerHTML='<br /><br /><img src="../images/icons/cross.png"  /> Sign Up Failed';
			setTimeout("document.getElementById('signInForm_msg').innerHTML='';window.resizeTo(500,500);InsertContent('signInForm');", 3000); 
		}
		//setTimeout('window.location.reload();', 2000); 
		
	}
	
} 


// post-submit callback 
function checkEmail_response(responseText, statusText, xhr, $form)  { 
  responseSplit = responseText.split(',');
  
	var roomSignup = document.getElementById('roomSignup');
	
	if (responseText === 'OK'){
						
			//Email Addres Found
			if (roomSignup){
				msg = '<img src="images/icons/tick_lrg.png"  /> Signup Successful<span style="font-size: 16px;" class="note"><br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="$.unblockUI();welcomeRedirect();" class="button" />'
				fb_msg = '<img src="../images/icons/tick.png"  /> Signup Successful<span style="font-size: 16px;" class="note"><br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="window.close();welcomeRedirect();" class="button" />';
			doormsg = '<strong>SIGNUP SUCCESSFUL</strong>';
			} else {
				msg = '<img src="images/icons/tick_lrg.png"  /> Signup Successful<span style="font-size: 16px;" class="note"><br /><br />Please click on the Site Link in the email we have just sent<br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="$.unblockUI();welcomeRedirect();" class="button" />'
				fb_msg = '<img src="../images/icons/tick.png"  /> Signup Successful<span style="font-size: 16px;" class="note"><br /><br />You should receive a welcome email in a few moments<br /><br /></span><input style="font-size: 14px;" type="button" value="OK" onclick="window.close();welcomeRedirect();" class="button" />';
			doormsg = '<strong>SIGNUP SUCCESSFUL</strong><br /><span style="font-size: 12px;" class="note">Please click on the Site Link in the email we have just sent';	
			}
		if(fb_tab!=1){
			$.blockUI({ 
			css: { 
				border: 'none', 
				padding: '15px', 
				backgroundColor: '#000', 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .8, 
				color: '#fff',
				fontSize: '2em'
				},
			message: msg	
			}); 
			makeTxt('door_msg',doormsg);
			InsertContent('checkEmailForm');
			RemoveContent('signInForm');
		} else {
			
			window.resizeTo(500,250);
			RemoveContent('checkEmailForm');
			RemoveContent('signInForm');
			document.getElementById('signInForm_msg').innerHTML=fb_msg;	
		}
			//setTimeout($.unblockUI, 4000); 
			document.getElementById('signInForm').reset();
			document.getElementById('checkEmailForm').reset();
			
			RemoveContent('doorEntry');	
			InsertContent('emailEntry');
			
			
			//$.colorbox.close();
			//alert(responseText)
			//alert('Already Regd - Sent You A Login Email');
	} else
	if (responseText === 'PENDING'){
			msg = 'Request Submitted<span style="font-size: 14px;" class="note"><br /><br />Your request has been submitted. You will be notified by email if access is granted to the Site.<br /><br /></span><input style="font-size: 18px;" type="button" value="OK" onclick="$.unblockUI();" class="button" />';
			fb_msg = 'Request Submitted<span style="font-size: 14px;" class="note"><br /><br />Your request has been submitted. You will be notified by email if access is granted to the Site.<br /><br /></span><input style="font-size: 18px;" type="button" value="OK" onclick="window.close();" class="button" />';
			doormsg = '<strong>REQUEST SENT SUCCESSFULLY</strong><br /><span style="font-size: 12px;" class="note">You Will Be Notified By Email With A Login Link If Access to Is Granted';
		if(fb_tab!=1){
			$.blockUI({ 
			css: { 
				border: 'none', 
				padding: '15px', 
				backgroundColor: '#000', 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .8, 
				color: '#fff',
				fontSize: '2em'
				},
			message: msg	
			}); 
		} else {
			window.resizeTo(500,250);
			RemoveContent('checkEmailForm');
			RemoveContent('signInForm');
			document.getElementById('signInForm_msg').innerHTML=fb_msg;	
		}
			//setTimeout($.unblockUI, 2000); 
			document.getElementById('signInForm').reset();
			document.getElementById('checkEmailForm').reset();
			makeTxt('door_msg',doormsg);
			InsertContent('checkEmailForm');
			RemoveContent('signInForm');
			//$.colorbox.close();
			//alert(responseText)
			//alert('Already Regd - Sent You A Login Email');
			
	
	} else {
		//setTimeout($.unblockUI, 500);

		//$('#email').val($('#checkEmail').val);
		document.getElementById('email').value=document.getElementById('checkEmail').value;
		
		//If an existing user
		if (responseSplit[0] === 'USER'){
			//alert('USER')
			
			document.getElementById('forename').value=responseSplit[1];
			document.getElementById('surname').value=responseSplit[2];
			document.getElementById('company').value=responseSplit[3];
			document.getElementById('url').value=responseSplit[4];
			document.getElementById('tel').value=responseSplit[5];
		}
		
		 
		document.getElementById('signInForm_msg').innerHTML='';	
		
		RemoveContent('checkEmailForm');
		InsertContent('signInForm');
		if(fb_tab!=1){
		signIn_popup();
		$.unblockUI();
		} else {
		window.resizeTo(500,500);	
		}
		//setTimeout('signIn_popup();', 1000); 
		//alert(responseText)
	}
	
} 


function signIn_popup(){
	//alert('ff')
	
	$.colorbox({width:"500px", maxHeight:"500px", opacity:0.1, inline:true, href:"#signInForm_box"})
	
	
}
var joinClick = 0;
function checkEmail_popup(signup){
	if(signup==1){joinClick = 1;}
	if(fb_tab!=1){ 
	$.colorbox({width:"500px", maxHeight:"400px", opacity:0.1, inline:true, href:"#signInForm_box"})
	
	} else { 
	//wopen (""+eventkey+"/JOIN","mywindow",450,400);
	//window.open(""+eventkey+"/JOIN");
	wopen ("facebook_tab/fb_signupBox.php?fb_popup&id=email&e="+eventkey+"","mywindow",500,250);
	} 
}

function welcomeRedirect(){
	if(document.getElementById('welcomeURL').value!=''){
		var welcomeURL = document.getElementById('welcomeURL').value;
		if(fb_tab!=1){ 
			window.open(welcomeURL);
		}else{ 
			parent.open(welcomeURL);
		}
	}
}
