// Directory for ajax scripts
var ajax_script_dir = "/phplib/ajax_scripts/";

// Colors for form processing
var white_RGB = "#FFFFFF";
var red_RGB = "#FFCCCC";
var green_RGB = "#CCFFCC";

// Arrays for form processing
var results = new Array(5);
results["login"]   = 1; 	//Login/Email OK par dï¿½faut
results["pseudo"]  = 1; 	//Pseudo OK par dï¿½faut
results["passold"] = 0;
results["pass1"]   = 0;
results["pass2"]   = 0;

var processing = new Array(5);
processing["login"]   = 0;
processing["pseudo"]  = 0;
processing["passold"] = 0;
processing["pass1"]   = 0;
processing["pass2"]   = 0;

// Change the backgroundColor of a field
function change_field_color(field_name, bgcolor_RGB){
    document.getElementById(field_name).style.backgroundColor = bgcolor_RGB;
}

// Clear the value & backgroundColor of a field
function clear_field(field_name, color_mode, value_mode){
    if (color_mode)
        document.getElementById(field_name).style.backgroundColor = white_RGB;
    if (value_mode)
        document.getElementById(field_name).value = "";

    field_error_name = field_name.substring(field_name.lastIndexOf("_") + 1) + "_error";
    $(field_error_name).html('');
}

// Check at once the validity of any field for ACCOUNT FORM
function account_check_field(field_name, field_value, field_comparison){
    var field_short_name = field_name.substring(field_name.lastIndexOf("_") + 1);
    if (typeof field_comparison == "undefined") {
		field_comparison = field_value;
	}
    $.get(ajax_script_dir + 'account_check_field.php?name=' + field_name + '&value=' + field_value + '&comparison_value=' + field_comparison, function(retour){
        if (retour != "") {
            $("#error_message").html(retour);
            change_field_color(field_name, red_RGB);
            results[field_short_name] = 0;
        } else {
            results[field_short_name] = 1;
            change_field_color(field_name, green_RGB);
        }
        processing[field_short_name] = 0;
    });
}

// Process the changes for account without reloading the page
function account_process_change(field_name, field_value, field_comparison){
    if (typeof field_comparison == "undefined")
        field_comparison = field_value;

    $.get(ajax_script_dir + 'account_check_field.php?name=' + field_name + '&value=' + field_value + '&comparison_value=' + field_comparison, function(retour){
		var retour1 = retour.substring(0, retour.indexOf(":"));
        if (retour1 == "change_pseudo_ok") {
			var retour2 = retour.substring((retour.indexOf(":")+1), retour.length);
			$("#header_strong_pseudo").html(retour2);
        }
    });
}

// field_login_change
function field_login_change(objet){
    processing["login"] = 1;
    account_check_field(objet.name, objet.value);
}

// field_pseudo_change
function field_pseudo_change(objet){
    processing["pseudo"] = 1;
    account_check_field(objet.name, objet.value);
}

// field_passold_change
function field_passold_change(objet){
    processing["passold"] = 1;
    account_check_field(objet.name, objet.value);
}

// field_passold_focus
function field_passold_focus(){
    var white_RGB = "#FFFFFF";
    $('#pass1_error').html('');
    document.getElementById('facnt_txt_pass1').style.backgroundColor = white_RGB;
    $('#pass2_error').html('');
    document.getElementById('facnt_txt_pass2').style.backgroundColor = white_RGB;
}

// field_pass1_change
function field_pass1_change(objet){
    var obj_pass2 = document.getElementById('facnt_txt_pass2');
    processing["pass1"] = 1;
    account_check_field(objet.name, objet.value);
    if (obj_pass2.value != '') {
		field_pass2_change(obj_pass2);
	}
}

// field_pass2_change
function field_pass2_change(objet){
    processing["pass2"] = 1;
    account_check_field(objet.name, objet.value, $('#facnt_txt_pass1').val());
}

// Fonction pour la validation des champs "Mes infos personnelles"
function submit_account_infos(){
	if ((processing["login"] == 1) || (processing["pseudo"] == 1) || (processing["passold"] == 1) || (processing["pass1"] == 1) || (processing["pass2"] == 1)) {
		setTimeout("submit_account_infos()", 0); // The ajax process isn't finished so we wait for it.
		return false;
	}
	var error_message = 0;
	if ($('#facnt_txt_passold').val() != '') {
		if (results['passold'] == 0) {
			error_message = 1;
		}
		if ((results['passold'] == 1) && ((results['pass1'] == 0) || (results['pass2'] == 0))) {
			error_message = 1;
			field_pass1_change(document.getElementById('facnt_txt_pass1'));
			field_pass2_change(document.getElementById('facnt_txt_pass2'));
		}
	}

	if (results['login'] == 0) {
		error_message = 1;
	}
	if (results['pseudo'] == 0) {
		error_message = 1;
	}

	// if there is an error, it returns false and it shows an alert window
	if (error_message == 1) {
		$("#error_message").html("Merci de remplir correctement le formulaire");
		return false;
	}
	// Here everything is alright so the changes are performed by an AJAX method

    account_process_change('change_login', $('#facnt_txt_login').val());
    account_process_change('change_pseudo', $('#facnt_txt_pseudo').val());
    account_process_change('change_password', $('#facnt_txt_pass1').val(), $('#facnt_txt_passold').val());

    if($('#fsubs_chk_promo').attr('checked'))
		var optin_navx = true;
		else
			var optin_navx = false;

	if($('#fsubs_chk_promo_partner').attr('checked'))
		var optin_partner = true;
		else
			var optin_partner = false;

    account_process_change('change_navx_optin', optin_navx);
    account_process_change('change_partner_optin', optin_partner);

    $("#error_message").html("Vos changements ont bien été pris en compte");

    // All fields are cleared
    clear_field('facnt_txt_login', true, false);
    clear_field('facnt_txt_pseudo', true, false);
    clear_field('facnt_txt_passold', true, true);
    clear_field('facnt_txt_pass1', true, true);
    clear_field('facnt_txt_pass2', true, true);

	//TODO : JJ : stil used?
    /*if ($('#facnt_txt_redirect').val() != "") {
        window.location=$('#facnt_txt_redirect').val();
    }*/
    return false;
}

//##############################
//##### DEVICE GPS BLOCK #######
//##############################
function submit_my_device(){
    document.getElementById('facnt_validationDevice_message').innerHTML = "";

    if (document.getElementById('facnt_sel_device').value == -1 || document.getElementById('facnt_sel_model').value == -1) {
        document.getElementById('facnt_validationDevice_message').innerHTML = "Merci de remplir correctement le formulaire";
        return false;
    }

    if (document.getElementById('facnt_sel_device').value == 1 || document.getElementById('facnt_sel_model').value == 1) {
        account_process_change('add_new_device', document.getElementById('facnt_txt_miscmodel').value);
    }
    else {
        account_process_change('change_device_id', document.getElementById('facnt_sel_model').value);
    }

    document.getElementById('facnt_validationDevice_message').innerHTML = "Vos changements ont bien été pris en compte";
    return false;
}

//##############################
//####### IDEA  BLOCK #########
//##############################
function submit_idea(){

    if ($('#facnt_txt_idea').val() == "") {
        return false;
    }
    return true;
}

$(document).ready(function(){

    $("#bloc-savoir").css("display", "none");
	$("#bloc-savoir-offer").css("display", "none");
    $("#savoir-plus").css("cursor", "pointer");
    $("#savoir-plus").click(function(){
        $("#bloc-savoir").slideToggle("slow");
    });
	$("#savoir-plus-offer").css("cursor", "pointer");
    $("#savoir-plus-offer").click(function(){
        $("#bloc-savoir-offer").slideToggle("slow");
    });
    $('#facnt_sel_device').change(function(){

		var str = "";
		$("#facnt_sel_model option:selected").each(function () {
			str += $(this).val();
		});
		checkCompatibilityBot('');

        if ($(this).val() == '1') {
            $('#p_facnt_sel_model').css('display', 'none');
            $('#p_facnt_txt_miscmodel').css('display', 'block');
        } else if ($(this).val() == '-1') {
            $('#p_facnt_sel_model').css('display', 'none');
            $('#p_facnt_txt_miscmodel').css('display', 'none');
        } else {

            $.get("/phplib/ajax_scripts/share_information_by_query.php?type=GetModelFromBrand&brand=" + $(this).val(), function(retour){
                $('#p_facnt_txt_miscmodel').css('display', 'none');
                $('#p_facnt_sel_model').css('display', 'block');
                $('#facnt_sel_model').html(retour);
            });
            // change gps device image with the brand
            $.get("/phplib/ajax_scripts/share_information_by_query.php?type=GetImgFromBrand&brand=" + $(this).val(), function(retour){
                $('#p_facnt_txt_miscmodel').css('display', 'none');
                $('#p_facnt_sel_model').css('display', 'block');
                $('#facnt_div_img_sel_device').html(retour);
            });
        }
    }).change();

    $('#facnt_sel_model').change(function(){

		var str = "";
		$("#facnt_sel_model option:selected").each(function () {
			str += $(this).val();
		});
		checkCompatibilityBot(str);

        if ($(this).val() == '1') {
            $('#p_facnt_txt_miscmodel').css('display', 'block');
        } else if ($(this).val() != '-1') {
            $('#p_facnt_txt_miscmodel').css('display', 'none');
        }
    }).change();
});

