// directory for ajax scripts
var ajax_script_directory = "/phplib/ajax_scripts/";

// Hexa codes for correct & non correct colors
var red_RGB = "#FFCCCC";
var green_RGB = "#CCFFCC";
var settimeout = 0; // display the object whenever we receive the answer from the server
function getHTTPObject(mode){
    var xmlhttp = false;
    var real_mode = mode.substring(0, mode.indexOf("_", 0));
    var field_short_name = mode.substring(mode.lastIndexOf("_") + 1);
    
    /* Compilation conditionnelle d'IE */
    if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }  catch (e) {
            try {
                xmlhttp = new XMLHttpRequest();
            } 
            catch (e) {
                xmlhttp = false;
            }
        }
    }
    
    if (xmlhttp) {
    
        var status = -1;
        /* on définit ce qui doit se passer quand la page répondra */
        xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4) /* 4 : état "complete" */ {
                if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */ {
                
                    /* Traitement de la réponse.*/
                    
                    /////////// MODE SUBSCRIBE /////////////////////////////////////////////////////////////////////
                    if (real_mode == 'subscribe') {
                        if (xmlhttp.responseText == "") {
                            bgcolor_RGB = green_RGB;
                            if (settimeout == 0) 
                                display_field_comment(field_short_name, ok_message);
                            else {
                                fields_name.push(mode);
                                fields_error.push(ok_message);
                                fields_color.push(bgcolor_RGB);
                            }
                            results[field_short_name] = 1;
                        } else {
                            bgcolor_RGB = red_RGB;
                            if (settimeout == 0) 
                                display_field_comment(field_short_name, xmlhttp.responseText);
                            else {
                                fields_name.push(mode);
                                fields_error.push(xmlhttp.responseText);
                                fields_color.push(bgcolor_RGB);
                            }
                            results[field_short_name] = 0;
                        }
                        if (settimeout == 0) 
                            change_field_color(mode, bgcolor_RGB, 'fsubs');
                        
                        processing[field_short_name] = 0; // the field is processed !
                    }
                }
            }
        }
    }
    return xmlhttp;
}

// PARAMS :
//
// Mode = a concat text between the real mode (subscribe/account) and the concerned field
// bgcolor_RGB = the color to display : red on error, green on OK
// prefix : a short text containing the prefix for the field to modify
//
// PURPOSE :
//
// form fields blinking function

function change_field_color_old(mode, bgcolor_RGB, prefix){
    var field_medium_name = mode.substring(mode.indexOf("_"));
    
    document.getElementById(prefix + mode.substring(mode.indexOf("_"))).style.backgroundColor = bgcolor_RGB;
    
}


function display_field_comment(field_short_name, ok_message){
    document.getElementById(field_short_name + "_error").innerHTML = ok_message;
}


/**
 * Envoie des données à l'aide d'XmlHttpRequest?
 * @param string methode d'envoi ['GET'|'POST']
 * @param string url
 * @param string données à envoyer sous la forme var1=value1&var2=value2...
 */
function sendData(method, url, data, mode){
    var xmlhttp = getHTTPObject(mode);
    if (!xmlhttp) {
        return false;
    }
    
    if (method == "GET") {
        if (data == 'null') {
            xmlhttp.open("GET", url, true); //ouverture asynchrone
        } else {
            xmlhttp.open("GET", url + "?" + data, true);
        }
        xmlhttp.send(null);
    } else if (method == "POST") {
        xmlhttp.open("POST", url, true); //ouverture asynchrone
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(data);
    }
    
    return true;
}

// Check at once the validity of any field for SUBSCRIBE FORM
function subscribe_check_field(field_name, field_value, field_comparison, settimeoutok){
    if (typeof field_comparison == "undefined") 
        field_comparison = field_value;
    var mode = "subscribe" + field_name.substring(field_name.indexOf("_"));
    if (field_value == true && field_name == 'fsubs_chk_confirm') 
        field_value = 'on';
    
    if (typeof settimeoutok != "undefined") 
        settimeout = settimeoutok;
    else 
        settimeout = 0;
    
    return !sendData('GET', ajax_script_directory + 'subscribe_check_field.php', 'name=' + field_name + "&value=" + field_value + "&comparison_value=" + field_comparison, mode);
}

 function setInvoiceRenouv(id, subscription_id, code_promo)
{
	$.get(ajax_script_directory + 'share_information_by_query.php?type=SetInvoiceRenouv&id=' + id + '&subscription=' + subscription_id, function(retour){
        if (retour == 'migration' || retour == 'desabomensuel' || retour == 'desaboannuel') {
            var url = '/phplib/php_ajax/setInvoice.php?id=' + id + '&subscription=' + subscription_id + '&raison=' + retour;
			if(code_promo != undefined && code_promo != '')
				url = url+'&code_promo='+code_promo;
            $.get(url, function(retour){
                if (retour != '') {
                    $(document.getElementById('form' + id)).empty();
                    $(document.getElementById('form' + id)).append(retour);
                    document.forms["PaymentRequest" + id].submit();
                }
            });
        } else {
            $('.contenu_cadre p.response').empty();
            $('.contenu_cadre p.response').append(retour);
        }
    });
}

function RedirectToOffer(id){

    $.get(ajax_script_directory + 'share_information_by_query.php?type=RedirectToOffer&id=' + id, function(retour){
        window.location.replace(retour);
    });
}

function getDescriptionGps(identifiant){

    $("#spacial_tomtom").load(ajax_script_directory + "share_information_by_query.php", {
        type: "getDescriptionGps",
        value: identifiant
    });
}

function isCompatibilityBot(identifiant){
	$.get(ajax_script_directory + 'share_information_by_query.php?type=isCompatibilityBot&value=' + identifiant, function(retour){
        if(retour == "true"){
			return true;
		}
		return false;
    });
}

function checkCompatibilityBot(identifiant){
	$("#compatibilityBot").load(ajax_script_directory + "share_information_by_query.php", {
        type: "checkCompatibilityBot",
        value: identifiant
    });
}

function setInvoice(id, code_promo){
	var url = '/phplib/php_ajax/setInvoice.php?id='+id;
	if(code_promo != undefined && code_promo != '')
		url = url+'&code_promo='+code_promo;
    $.get(url, function(retour){
        if (retour != '') {
            $(document.getElementById('form' + id)).empty();
            $(document.getElementById('form' + id)).append(retour);
            document.forms["PaymentRequest" + id].submit();
        }
    });
}
