//var server = "http://localhost/sumopix/docs/scripts/php/";

var this_path = server + "scripts/php/";

var success_page;


$(document).ready(function() { 

    $('#new_email').bind('keyup blur', function() {
        var email =  $(this).val();
        if(email != "")
        {
            if(isValidEmail(email))
            {
                $("#validNewEmail").css({ "background-image": "url('/images/icons/validyes.png')" });
            } else {
                $("#validNewEmail").css({ "background-image": "url('" + server + "/images/icons/validno.png')" });
            }
        } else {
            $("#validNewEmail").css({ "background-image": "none" });
        }
    });
    $('#email').bind('keyup blur', function() {
        var email =  $(this).val();
        if(email != "")
        {
            if(isValidEmail(email))
            {
                $("#validEmail").css({ "background-image": "url('/images/icons/validyes.png')" });
            } else {
                $("#validEmail").css({ "background-image": "url('" + server + "/images/icons/validno.png')" });
            }
        } else {
            $("#validEmail").css({ "background-image": "none" });
        }
    });

    var obj = document.getElementById("load_login");
    
    success_page = $('#open_page').html();

    if(obj != null){
        //var server = $('#server_path').val();
        //startValidating();
        initLogin();
    }
    //alert('Servicen er ikke tilgængelig i øjeblikket. Vi beklager ulejligheden');
    create_user = document.getElementById("show_create_user");
    if(create_user != null){
        create_user = $('#show_create_user').html();  
        if(create_user == "true"){
            showCreateUser();
        }
    }


});
/*
 * email adrees valid
 */
function isValidEmail(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

function initLogin (){
    var obj = document.getElementById('login');

    var form_action = obj.action;
    obj.action = 'javascript:login();';
    
    var obj2 = document.getElementById('create');
    var form_create_action = obj.action2;
    
    if(obj2 != null){
        obj2.action = 'javascript:createUser();';
    }
    
    
}

function showCreateUser (){
    $('#create_new_user_content').show();
    $('#show_create_user_button').hide();
//    fitUI();
}

function changeDimensions(obj, new_width, new_height){
    $(obj).animate({ 
        width: new_width,
        height: new_height
  }, 1000 );
}



function login(){
    do_action = "login";
	
    checkLogin("email", "password", do_action);
    
    // Do not continue
    return void(0);
}

function createUser(){
    do_action = "create";
	
    doCreateUser("new_email", "pass", "pass2", do_action);
    
    // Do not continue
    return void(0);
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
function startValidating(){
    $("#new_email").keyup(function(){

	var email = $("#new_email").val();
	if(email != 0)
	{
    	if(isValidEmailAddress(email))
    	{
    	
    	$("#validEmail").css({ "background-image": "url('../../images/icons/validyes.png')" });
    	
    	} else {
    	$("#validEmail").css({ "background-image": "url('../../images/icons/validno.png')" });
    	
    	}
    	} else {
    	$("#validEmail").css({ "background-image": "none" });
	
	}
	});
 }
 
// Ajax Funtions_
function checkLogin(email_id, password_id, do_action){
	// First to check whther the email or password are correct or not..

		var isValid= 1;
        
        var email = document.getElementById(email_id).value;
	   var password = document.getElementById(password_id).value;

		if ( email != ""){					
			if( !isValidEmailAddress(email) ){
				isValid = 0;
				document.getElementById(email_id).focus();
				alert("Invalid email");
				return;
			}
		}else{
			isValid = 0;
			message = "Invalid email";
			document.getElementById(email_id).focus();
			alert("Invalid email");
			return;
		}		
		if( password.length < 2 || password.length > 20 ){
			isValid = 0;
			message = "Password between 5 and 10 characters";
			alert("Password should be between 5 and 10 characters");
			document.getElementById(password_id).focus();
			return;
		}
        

		if(isValid == 0) return;
		// Now remove login form
		
		$('#login_content').hide();	
		$("#message").html("Verifying login......please wait");
        //alert('get: ' + server + "checkLogin.php");
        
        var email_val = document.getElementById(email_id).value;
        var password_val = document.getElementById(password_id).value;
        
		//alert("open" + server + "checkLogin.php" + password_val + email_val + do_action);
		$.post(this_path + "checkLogin.php",{
		   action: do_action,   
		   email: email_val,
		   password: password_val
		  }, function(xml){
		      $(xml).find('label').each(function(){
                 var action = $(this).find('action').text();
                 var result = $(this).find('result').text();
                 var redirect = $(this).find('redirect').text();
                 var message = $(this).find('message').text();
				// as we have gotten data now chec kwhat to display
				if( action == "login" && redirect== 0){
					$('#login_content').show();	
					$('#message').html(message);
                    fitUI();	
				}						 		
				if( action == "login" && redirect== 1){
					//$('#login').show();	
					$('#message').html(message);
                    loadPage(success_page);
                    return;
				}						 		
            });	      
        }, 
        "xml");					
	}
    
function loadPage(page){
    $.post(page, 
        {
            first_parent: "true",
            vars: "something"
        }, function(value){
       	    $('#message').html("");
            $("#login_content").html(value);
            $('#login_content').show();
            loggedIn();
        }
    ); 
}

function doCreateUser(email_id, password_id, pass2_id, do_action){
		var nu_email = document.getElementById(email_id).value;
		var nu_password = document.getElementById(password_id).value;
		var re_password = document.getElementById(pass2_id).value;
		
		var isValid= 1;
		var message = "";

		if ( nu_email != ""){					
			if( !isValidEmailAddress(nu_email) ){
				isValid = 0;
				message = "Invalid email";
				document.getElementById(email_id).focus();
				alert("Invalid email");
				return;
			}
		}else{
			isValid = 0;
			message = "Invalid email";
			document.getElementById(email_id).focus();
			alert("Invalid email");
			return;
		}		
		// Password match
		if( re_password != nu_password ){
			message = "Password does not match";
			isValid = 0;
			alert("Password does not match");
			document.getElementById(pass2_id).focus();
			return;
		}	
		if( nu_password.length < 2 || nu_password.length > 20 ){
			isValid = 0;
			message = "Password between 5 and 10 characters";
			alert("Password between 5 and 10 characters");
			document.getElementById(password_id).focus();
			return;
		}
		
		if(isValid == 0)
			return;
            
		$('#login_content').hide();	
		$("#message").html("Creating user... Please wait");	
        
        var email_val = document.getElementById(email_id).value;
        var password_val = document.getElementById(password_id).value;
        
		//$("#message").html("Creating profile......please wait");
		$.post(this_path + "checkLogin.php", {
		   action: do_action,   
		   email: email_val,
           no_email: 'true',     
		   password: password_val   
			},  function(xml){
                 $(xml).find('label').each(function(){
                     var action = $(this).find('action').text();
                     var result = $(this).find('result').text();
                     var redirect = $(this).find('redirect').text();
                     var message = $(this).find('message').text();
				
					if(redirect== 0){
    	               $('#login_content').show();	
					   $('#message').html(message);	
                       fitUI();
    				}						 		
    				if(redirect== 1){
    					$('#message').html(message);
                        loadPage(success_page);
                        return;
    				}		
                }); //close each(
		  }, 
		  "xml");						
	
	
		// Remove new user					
	} 
function send_mesg(){
	window.opener.document.getElementById('email_login').value = 	document.getElementById('nu_email').value;
	window.opener.document.getElementById('password_login').value = 	document.getElementById('nu_password').value;
	window.opener.document.getElementById('action').value = "add";
	window.opener.document.getElementById('do').value = "user";
	window.opener.document.getElementById('frm_login').submit();
}
