/* INTERFACCIA JS PER GESTIRE LA REGISTRAZIONE DEL CLIENTE */
// Variabili Globali
var url_xml_rpc;
var form_obj;
var div_loading;
var struct_param = new Array();
var field_names = new Array();
var iso_encoding;

// Inizializzazione delle variabili globali
function init()	{
	url_xml_rpc = js_global_root_url+'xml_rpc/call.php';
	form_obj = document.getElementById('mainform');
	div_loading = document.getElementById('loading');
    get_field_names();
	struct_param['header'] = new Array();
	struct_param['query'] = new Array();
	struct_param['header']['version'] = '1.0.0';
	struct_param['header']['product'] = 'agent';
	struct_param['header']['sessid'] = sessid;
	iso_encoding = 'utf-8';
}

/*
*   Carica nella variabile globale "field_names" tutti i nomi dei campi presenti nel form
*/
function get_field_names() {
    
    tags = form_obj.getElementsByTagName("input");
    for(var i in tags) {
        if(tags[i].getAttribute && tags[i].getAttribute("type") != "button") {
            id = tags[i].getAttribute("id");
            field_names[id] = id;
        }
    }
        
    tags = form_obj.getElementsByTagName("textarea");
    for(var i in tags) {
        if(tags[i].getAttribute) {
            id = tags[i].getAttribute("id");
            field_names[id] = id;
        }
    }
    
    tags = form_obj.getElementsByTagName("select");
    for(var i in tags) {
        if(tags[i].getAttribute)
        id = tags[i].getAttribute("id");
        field_names[id] = id;
    }
       
}

/*
*   Ripulisce il form da eventuali errori segnalati
*/
function clear_form_errors() {
    
    for(var i in field_names)
        if(document.getElementById('ast_'+field_names[i]))
            document.getElementById('ast_'+field_names[i]).style.background = "#fff";
        
}

/*
*   Prende i dati per la registrazione dell'agente
*/
function register_agent() {
    
    clear_form_errors();
    
    struct_param['header']['type'] = 'register_agent';
    
    var details = new Array();
    var errors = new Array();
    
    // Controllo dei campi obbligatori
    for(var i in field_names) { 
        details[field_names[i]] = document.getElementById(field_names[i]).value;
        if(document.getElementById('ast_'+field_names[i]) && (details[field_names[i]]=='' || details[field_names[i]]=='***'))
            errors.push(field_names[i]);
    }
    
    struct_param['query'] = details;
    
    // Se alcuni campi obbligatori non sono stati compilati, blocco l'esecuzione
    if(errors.length>0) {
        for(var i in errors)
            document.getElementById('ast_'+errors[i]).style.background = "#ffff66";
        alert(js_dic_NOALLFIELDS);
        return false;
    }
    
    // Controllo le mail
    if(!emailControl(details['email']) || !emailControl(details['email2']) || details['email']!=details['email2']) {
        document.getElementById('ast_email').style.background = "#ffff66";
        document.getElementById('ast_email2').style.background = "#ffff66";
        alert(js_dic_ERRORVALIDEMAIL);
        return false;
    }
    
    form_obj.style.display = 'none';
    div_loading.style.display = 'block';
    
    xml_request(struct_param,url_xml_rpc,iso_encoding,callbacks_xml_request);
}

function callbacks_xml_request(response,type)	{
	switch(type) {
		case 'register_agent' :
			if(response == 'ok') { 
			  div_loading.style.display = 'none';
			  alert('Thankyou for registering with Albatravel Group Travel. We will be in contact with you very shortly.');
              window.location = js_global_root_url;
			}
			else {
			  div_loading.style.display = 'none';
              form_obj.style.display = 'block';
			  print_error(js_dic_ERRORTECHNICAL);
			}
		break;
	}
}

