// JavaScript Document

function setProfile(value){
	$('#profile').val(value);
	toggle_light_box();
}

function updateProfile(){
	var movie = getFlashMovieObject('flash-box');
	var infos_xml = movie.getRevisedData();
	
	//    alert(infos_xml);
	$.ajax({
		type: 'POST',
		url: base_url + 'retraite/data/updateprofile.php',
		data: 'data=' + infos_xml,
		async: false,
		success: function(data) {
			//alert(data);
			if(data == 'success') window.location.href = 'agir.php';
		}
	});
}

function pageTracker(param){
	//alert(param);
	_gaq.push(['_trackPageview', param]);
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  } 	
}

function stripAlphaChars(pstrSource) {
    var m_strOut = new String(pstrSource);
    m_strOut = m_strOut.replace(/[^0-9]/g, '');

    return m_strOut;
}
function stripAlphaCharsDecimal(pstrSource) {
    var m_strOut = new String(pstrSource);
    m_strOut = m_strOut.replace(/[^0-9.,]/g, '');

    return m_strOut.substr(0, 4);
}

function checkUser(){
	var e = $('#save-email').val();
	if(e == ''){
		validateSaveResults();
	} else {
		$.ajax({
			type: 'POST',
			url: base_url + 'retraite/data/is-user.php',
			data: 'e=' + e,
			success: function(msg) {
				//alert(msg);
				if(msg == 'false') validateSaveResults();
				else{
					toggle_light_box(base_url + 'retraite/login-and-save.php?e='+$('#save-email').val()+'&id='+msg, 300, 440);
				}
			}
		});	
	}
}

function validateSaveResults(){
	var valid = true;
	$('.error').removeClass('error');
	$('.error-msg').css('visibility', 'hidden');
	$('input').each(function(){
		if($(this).val() == ''){
			valid = false;
			$(this).addClass('error');
		}
	});
	
	if(!validEmail($('#save-email').val())){
		valid = false;
		$('#save-email').addClass('error');
		$('#save-email-error').css('visibility', 'visible');
	}
	
	if($('#save-password').val() != $('#save-password-confirmation').val()){
		valid = false;
		$('#save-password-confirmation').addClass('error');
		$('#save-password-confirmation-error').css('visibility', 'visible');
	}
	
	if(valid){
		saveResults();
	}
}

function validateLogin(id){
	var valid = true;
	$('.error').removeClass('error');
	$('.error-msg').css('visibility', 'hidden');
	
	if(!validEmail($('#save-email').val()) || $('#save-email').val() == ''){
		valid = false;
		$('#save-email').addClass('error');
		$('#save-email-error').css('visibility', 'visible');
	}
	
	if($('#save-password').val() == ''){
		valid = false;
		$('#save-password').addClass('error');
		$('#save-password-error').css('visibility', 'visible');
	}
	
	if(valid){
		if(id != undefined) logAndSave(id);
		else checkLogin();
	}
}

function validateForgottenPassword(){
	var valid = true;
	$('.error').removeClass('error');
	$('.error-msg').css('visibility', 'hidden');
	
	if(!validEmail($('#save-email').val()) || $('#save-email').val() == ''){
		valid = false;
		$('#save-email').addClass('error');
		$('#save-email-error').css('visibility', 'visible');
	}
	
	if(valid){
		sendPassword();
	}	
}

function validEmail(e){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(!reg.test(e)) return false;
   else return true;
}

function saveResults(id){
	var data = $('#save-form').serialize();
	if(id != undefined) data = 'id=' + id;
    $.ajax({
        type: 'POST',
        url: base_url + 'retraite/data/save-results.php',
        data: data,
        success: function(msg) {
			//alert(msg);
            $('#content.save-results').html(msg);
        }
    });
}

function logAndSave(id){
	var email = $('#save-email').val();
    $.ajax({
        type: 'POST',
        url: base_url + 'retraite/data/log-and-save.php',
        data: $('#save-form').serialize()+'&id='+id,
        success: function(msg) {
			if(msg == 'No user'){
				$('#login-error-msg').css('visibility', 'visible');
				$('#save-email').addClass('error');
				$('#save-password').addClass('error');	
			} else {
				 $('#content.save-results').html(msg);
				 
				 var userInfos = '<span id="user-infos">[ ';
				 userInfos += email
				 userInfos += ' ] - <a href="disconnect.php">';
				 if(lang == 'fr') userInfos += 'D&eacute;connexion';
				 else userInfos += 'Disconnect';
				 userInfos += '</a></span>'
				 $('#login').replaceWith(userInfos);
			}
        }
    });
}

function checkLogin(){
    $.ajax({
        type: 'POST',
        url: base_url + 'retraite/check-login.php',
        data: $('#save-form').serialize(),
        success: function(msg) {
            //alert(msg);
			if(msg == 'success'){
				window.location.href = 'analyser.php';
			} else {
				$('#login-error-msg').css('visibility', 'visible');
				$('#save-email').addClass('error');
				$('#save-password').addClass('error');
			}
        }
    });	
}

function sendPassword(){
    $.ajax({
        type: 'POST',
        url: base_url + 'retraite/send-password.php',
        data: $('#forgot-pass-form').serialize(),
        success: function(msg) {
            $('#content.forgot-pass').html(msg);
        }
    });	
}

$(document).ready(function() {


   

    $('#current_age').change(function() {
        if ($(this).val() > 0) {
            $(this).css('color', '#333');
        }
    });
    $('#retirement_age').change(function() {
        if ($(this).val() > 0) {
            $(this).css('color', '#333');
        }
    });
    $('#economies').focus(function() {
        if ($(this).val() == 0) {
            $(this).val('');
        } else {
            $(this).select();
        }
    }).blur(function() {
        tmp = $(this).val();
        if (tmp != '') {
            $(this).css('color', '#333');
        } else {
            $(this).val(0);
        }
        tmp = stripAlphaChars(tmp);
        if (tmp > 2000001) {
            tmp = 2000001;
        }
        if (lang == 'fr') {
            $(this).val(tmp + ' $');
        } else {
            $(this).val('$ ' + tmp);
        }
    });
    $('#savings').focus(function() {
        if ($(this).val() == 0) {
            $(this).val('');
        } else {
            $(this).select();
        }
    }).blur(function() {
        tmp = $(this).val();
        if (tmp != '') {
            $(this).css('color', '#333');
        } else {
            $(this).val(0);
        }
        tmp = stripAlphaChars(tmp);
        if (tmp > 3000) {
            tmp = 3000;
        }
        if (lang == 'fr') {
            $(this).val(tmp + ' $');
        } else {
            $(this).val('$ ' + tmp);
        }
    });
    $('#main_income').focus(function() {
        if ($(this).val() == 0) {
            $(this).val('');
        } else {
            $(this).select();
        }
    }).blur(function() {
        tmp = $(this).val();
        if (tmp != '') {
            $(this).css('color', '#333');
        } else {
            $(this).val(0);
        }
        tmp = stripAlphaChars(tmp);
        if (tmp > 500000) {
            tmp = 500000;
        }
        if (lang == 'fr') {
            $(this).val(tmp + ' $');
        } else {
            $(this).val('$ ' + tmp);
        }
    });
    $('#other_incomes').focus(function() {
        if ($(this).val() == 0) {
            $(this).val('');
        } else {
            $(this).select();
        }
    }).blur(function() {
        tmp = $(this).val();
        if (tmp != '') {
            $(this).css('color', '#333');
        } else {
            $(this).val(0);
        }
        tmp = stripAlphaChars(tmp);
        /*if (tmp > 999999) {
124124 $            tmp = 999999;
        }*/
        if (lang == 'fr') {
            $(this).val(tmp + ' $');
        } else {
            $(this).val('$ ' + tmp);
        }
    });
    $('#profile').change(function() {
        if ($(this).val() != 'null') {
            $(this).css('color', '#333');
        }
    });

    $('.currency_field').each(function() {
        if ($(this).val().indexOf('$') === -1) {
            if (lang == 'fr') {
                $(this).val($(this).val() + ' $');
            } else {
                $(this).val('$ ' + $(this).val());
            }
        }
    });
    
    /* Projets */
    
    $('#rendement,#temps').each(function() {
        if ($(this).val().indexOf($(this).attr('title')) == -1) {
            $(this).val($(this).val() + ' ' + $(this).attr('title'));
        }
    });
    
    $('#montant').focus(function() {
        if ($(this).val() == " $" || $(this).val() == "$ ") {
            $(this).val('');
        } else {
            $(this).select();
        }
    }).blur(function() {
        tmp = $(this).val();
        tmp = stripAlphaChars(tmp);
        if (tmp > 5000000) {
            tmp = 5000000;
        }
        if (lang == 'fr') {
            $(this).val(tmp + ' $');
        } else {
            $(this).val('$ ' + tmp);
        }
    });
    
    $('#rendement').focus(function() {
        if ($(this).val() == ' %') {
            $(this).val('');
        } else {
            $(this).select();
        }
    }).blur(function() {
        tmp = $(this).val();
        tmp = stripAlphaCharsDecimal(tmp);
        if (tmp > 5) {
            tmp = 5;
        }
        $(this).val(tmp + ' ' + $(this).attr('title'));
    });
    
    $('#temps').focus(function() {
        if ($(this).val() == ' mois' || $(this).val() == " months") {
            $(this).val('');
        } else {
            $(this).select();
        }
    }).blur(function() {
        tmp = $(this).val();
        tmp = stripAlphaChars(tmp);
        if (tmp > 300) {
            tmp = 300;
        }
        $(this).val(tmp + ' ' + $(this).attr('title'));
    });



});


function checkUserProjet(){
    var e = $('#save-email').val();
    if(e == ''){
        validateSaveResultsProjet();
    } else {
        $.ajax({
            type: 'POST',
            url: base_url + 'projet/data/is-user.php',
            data: 'e=' + e,
            success: function(msg) {
                //alert(msg);
                if(msg == 'false') validateSaveResultsProjet();
                else{
                    toggle_light_box(base_url + 'projet/login-and-save.php?e='+$('#save-email').val()+'&id='+msg, 300, 440);
                }
            }
        }); 
    }
}

function validateSaveResultsProjet(){
    var valid = true;
    $('.error').removeClass('error');
    $('.error-msg').css('visibility', 'hidden');
    $('input').each(function(){
        if($(this).val() == ''){
            valid = false;
            $(this).addClass('error');
        }
    });
    
    if(!validEmail($('#save-email').val())){
        valid = false;
        $('#save-email').addClass('error');
        $('#save-email-error').css('visibility', 'visible');
    }
    
    if($('#save-password').val() != $('#save-password-confirmation').val()){
        valid = false;
        $('#save-password-confirmation').addClass('error');
        $('#save-password-confirmation-error').css('visibility', 'visible');
    }
    
    if(valid){
        saveResultsProjet();
    }
}

function saveResultsProjet(id){
    var data = $('#save-form').serialize();
    if(id != undefined) data = 'id=' + id;
    $.ajax({
        type: 'POST',
        url: base_url + 'projet/data/save-results.php',
        data: data,
        success: function(msg) {
            //alert(msg);
            $('#content.save-results').html(msg);
        }
    });
}

    function logAndSaveProjet(id){
    var email = $('#save-email').val();
    $.ajax({
        type: 'POST',
        url: base_url + 'projet/data/log-and-save.php',
        data: $('#save-form').serialize()+'&id='+id,
        success: function(msg) {
            if(msg == 'No user'){
                $('#login-error-msg').css('visibility', 'visible');
                $('#save-email').addClass('error');
                $('#save-password').addClass('error');  
            } else {
                 $('#content.save-results').html(msg);
                 
                 var userInfos = '<span id="user-infos">[ ';
                 userInfos += email
                 userInfos += ' ] - <a href="disconnect.php">';
                 if(lang == 'fr') userInfos += 'D&eacute;connexion';
                 else userInfos += 'Disconnect';
                 userInfos += '</a></span>'
                 $('#login').replaceWith(userInfos);
            }
        }
    });
}

function checkLoginProjet(){
    $.ajax({
        type: 'POST',
        url: base_url + 'projet/check-login.php',
        data: $('#save-form').serialize(),
        success: function(msg) {
            //alert(msg);
            if(msg == 'success'){
                window.location.href = 'analyser.php';
            } else {
                $('#login-error-msg').css('visibility', 'visible');
                $('#save-email').addClass('error');
                $('#save-password').addClass('error');
            }
        }
    }); 
}


function validateForgottenPasswordProjet(){
    var valid = true;
    $('.error').removeClass('error');
    $('.error-msg').css('visibility', 'hidden');
    
    if(!validEmail($('#save-email').val()) || $('#save-email').val() == ''){
        valid = false;
        $('#save-email').addClass('error');
        $('#save-email-error').css('visibility', 'visible');
    }
    
    if(valid){
        sendPasswordProjet();
    }   
}

function sendPasswordProjet(){
    $.ajax({
        type: 'POST',
        url: base_url + 'projet/send-password.php',
        data: $('#forgot-pass-form').serialize(),
        success: function(msg) {
            $('#content.forgot-pass').html(msg);
        }
    }); 
}

function validateLoginProjet(id){
    var valid = true;
    $('.error').removeClass('error');
    $('.error-msg').css('visibility', 'hidden');
    
    if(!validEmail($('#save-email').val()) || $('#save-email').val() == ''){
        valid = false;
        $('#save-email').addClass('error');
        $('#save-email-error').css('visibility', 'visible');
    }
    
    if($('#save-password').val() == ''){
        valid = false;
        $('#save-password').addClass('error');
        $('#save-password-error').css('visibility', 'visible');
    }
    
    if(valid){
        if(id != undefined) logAndSaveProjet(id);
        else checkLoginProjet();
    }
}
