﻿// JScript File
var executer,email,url;

function RegisterUB(box){
	$(box).value="good";
}

function ValidateUrl(updateButtonId){
	var url=$F('crawlUrl');
	if(url.blank() || url=='http://'){
		return;
	}
	var editBox = {			
			ErrorBoxId:'validateError',
			LoadingImg:'crawlLoadingContainer',			
			UpdateButtonId:updateButtonId
		}	
	$('crawlerrordiv').hide();
	$('crawlfaildiv').hide();
	$('crawlsuccessdiv').hide();
	$(editBox.LoadingImg).show();
	EmailValidator.ValidateUrl(url,ValidateUrlOnSucceeded, OnError, editBox);
}
function ValidateUrlOnSucceeded(result, editBox)
{		
	result=result.evalJSON(); 
	if(result.IsError){
		$('crawlerrordiv').show();		
		$('crawlerrorinfo').value=result.ErrorMessage;
	}else{
		if(result.IsValid){
			$('crawlsuccessdiv').show();
			$('crawlfaildiv').hide();
		}else{
			$('crawlsuccessdiv').hide();
			$('crawlfaildiv').show();
		}
	}
	$("numberOfEmailsLeftSpan").update(result.ValidationsLeft);
	if(result.ValidationsLeft<1){
		$('canValidateMessageSpan').hide();
		$('cantValidateMessageSpan').show();
	}else{
		$('canValidateMessageSpan').show();
		$('cantValidateMessageSpan').hide();
	}
	if(editBox.UpdateButtonId){__doPostBack($(editBox.UpdateButtonId).id,'');}
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
}
function RemoveLink(linkId,updateButtonId){
	var editBox = {			
			ErrorBoxId:'removeLinkError',
			LoadingImg:'removeLinkLoadingContainer',			
			UpdateButtonId:updateButtonId
	}
	$(editBox.LoadingImg).show();	
	EmailValidator.RemoveLink(linkId,RemoveLinkOnSucceeded, OnError, editBox);
}

function RemoveLinkOnSucceeded(result, editBox){
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
	if(editBox.UpdateButtonId){__doPostBack($(editBox.UpdateButtonId).id,'');}
	$("numberOfEmailsLeftSpan").update(result);
	if(result<1){
		$('canValidateMessageSpan').hide();
		$('cantValidateMessageSpan').show();
	}else{
		$('canValidateMessageSpan').show();
		$('cantValidateMessageSpan').hide();
	}
}

function ValidateEmail(){	
	if(!$('EmailAddressInput').value.blank()){
		email=$('EmailAddressInput').value;
		if(executer){
			executer.stop();
			executer=null;
		}
	}	
	if(email && !email.blank())	{	
		var editBox = {			
			ErrorBoxId:'validateError',
			LoadingImg:'loadingImg'
		}
		$(editBox.LoadingImg).show();
		EmailValidator.ValidateEmail(email,ValidateEmailOnSucceeded, OnError, editBox);
	}
}

function ValidateEmailOnSucceeded(result, editBox)
{		
	result=result.evalJSON(); 
	
	$('ValidationResults').show();	
	var resultState = '<span style="font-weight:bold;">'+result.State+'</span>' + ' <br/><span style="font-size:xx-small;">(Started at:'+result.StartDate+')</span>';
	if(result.State=='Finished'){
	resultState+= ' <br/><span style="font-size:xx-small;">(Finished at:'+result.EndDate+' )';
	}
	$('validationState').update(resultState);
	$('emailSyntaxResult').update(result.SyntaxResult);
	$('emailDomainResult').update(result.DomainResult);
	$('emailAccountResult').update(result.AccountResult);
	$('validationLog').update(result.Log);
	$('numberOfEmailsLeftSpan').update(result.Validations);	
	if(result.Validations<1){
		$('canValidateMessageSpan').hide();
		$('cantValidateMessageSpan').show();
	}else{
		$('canValidateMessageSpan').show();
		$('cantValidateMessageSpan').hide();
	}
	if(result.State=='Finished'){
		if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}		
		if(executer){executer.stop();}
	}else{
		executer=new PeriodicalExecuter(ValidateEmail, 3);
	}
}



//--------------------------------------------------------------------------
 // Hook up the click events of the log in and log out buttons.
//$addHandler($get('btnLogIn'), 'click', loginHandler);
//$addHandler($get('btnLogOut'), 'click', logoutHandler);
Event.observe(window, 'load', initAuthentication);
var ssa;
function initAuthentication(){
	if(typeof(Sys) !== 'undefined'){
		ssa = Sys.Services.AuthenticationService;
		if($('LoggedInView') && $('AnonymousView')){
			if (ssa.get_isLoggedIn()) {
				$('LoggedInView').show();
			} else {
				$('AnonymousView').show();    
			}
		}
	}
}
function loginHandler() {
    var username = $F('UserName');
    var password = $F('Password');
    var isPersistent = $('RememberMe').checked;
    var customInfo = null;
    var redirectUrl = null;
    $("LogInLoading").show();
    // Log them in.
    
    ssa.login(username,password,isPersistent,customInfo,redirectUrl,onLoginComplete,onLoginError);
}

function logoutHandler() {
    // Log them out.
    var redirectUrl = null;
    var userContext = null;     
    ssa.logout(redirectUrl,onLogoutComplete,OnError,userContext);
}



function onLogoutComplete(result, context, methodName) {
    $('LoggedInView').show();
    $('AnonymousView').hide();
     
}

function onLoginError(error, context, methodName) {
	$("LogInLoading").hide();
	if($('registerLoadingImg')){$('registerLoadingImg').hide();}
	$('FailureRow').show();
	$('LoginErrorMessage').update(error.get_message());	 
}
//-----------
function register(){
	var emailPattern = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	$('RegisterFirstNameRequired').hide();
	$('RegisterLastNameRequired').hide();
	$('RegisterEmailRequired').hide();
	$('RegisterEmailFormat').hide();
	$('RegisterPasswordRequired').hide();
	$('RegisterPasswordMismatch').hide();
	var isValid=true;
	if($F('RegisterFirstNameInput').blank()){
		$('RegisterFirstNameRequired').show();
		isValid=false;
	}
	if($F('RegisterLastNameInput').blank()){
		$('RegisterLastNameRequired').show();
		isValid=false;
	}
	if($F('RegisterEmail').blank()){
		$('RegisterEmailRequired').show();
		isValid=false;
	}
	if(!emailPattern.test($F('RegisterEmail'))){
		$('RegisterEmailFormat').show();
		isValid=false;
	}
	if($F('RegisterPassword').blank()){
		$('RegisterPasswordRequired').show();
		isValid=false;
	}
	if($F('RegisterConfirmPassword')!=$F('RegisterPassword')){
		$('RegisterPasswordMismatch').show();
		isValid=false;
	}
	if(isValid){
		var editBox = {			
			ErrorBoxId:'registerError',
			LoadingImg:'registerLoading'
		}
		if(editBox.LoadingImg){$(editBox.LoadingImg).show();}
		RegisterUser.Register($F('RegisterEmail'),$F('RegisterPassword'),$F('RegisterFirstNameInput'),$F('RegisterLastNameInput'),RegisterOnSuccess,OnError,editBox);
	}
}

function RegisterOnSuccess(result, editBox){	
	result=result.evalJSON(); 
	if(result.isError){
		$(editBox.ErrorBoxId).update(result.errorMessage);
		$(editBox.ErrorBoxId+'Container').show();	
		if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
	}else{
		var isPersistent = false;
		var customInfo = null;
		var redirectUrl = null;
		ssa.login(result.userEmail,result.password,isPersistent,customInfo,redirectUrl,onLoginComplete,onLoginError);
	}
}

function OnError(result, editBox)
{	
	$(editBox.ErrorBoxId).update(result._message);
	$(editBox.ErrorBoxId+'Container').show();					
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
}
