/* INTERFACCIA JS PER GESTIRE LA REGISTRAZIONE DEL CLIENTE */
// Variabili Globali
var url_xml_rpc;
var form_obj;
var div_form;
var div_loading;
var div_form_close;
var struct_param = 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_form = document.getElementById('data_form');
	div_loading = document.getElementById('loading');
	div_form_close = document.getElementById('form_client_close');
	struct_param['header'] = new Array();
	struct_param['query'] = new Array();
	struct_param['header']['version'] = '1.0.0';
	struct_param['header']['product'] = 'customers';
	struct_param['header']['sessid'] = sessid;
	iso_encoding = 'utf-8';
}

// trova le città di una nazione
function cities_of_country(country) {
    if(country == 'I')
        document.getElementById('compulsoryadvise').style.display = display_for_browser();
    else
        document.getElementById('compulsoryadvise').style.display = 'none';
	// controlla che leggi sulla privacy mostrare in base alla nazione
    if(document.getElementById('accept_privacy') && country == 'I')
        document.getElementById('accept_privacy').innerHTML = js_dic_ACCEPTPRIVACYITA;
    else if(document.getElementById('accept_privacy'))
        document.getElementById('accept_privacy').innerHTML = js_dic_ACCEPTPRIVACY;
    
    if(country == 'other') {
		document.getElementById('suggested_country_span').style.display = 'inline';
		document.getElementById('suggested_city_span').style.display = 'inline';
		document.getElementById('city_span').innerHTML = '';
	}
	else {
		document.getElementById('suggested_country_span').style.display = 'none';
		document.getElementById('suggested_city_span').style.display = 'none';
		
		if(country == '***')
			return;
		else {
			document.getElementById('city_span').innerHTML = js_dic_LOADING+'...';
			
			struct_param['header']['type'] = 'lcities_of_country';
			struct_param['header']['cache'] = true;
			struct_param['query'] = new Array();
			struct_param['query']['country'] = country;
			struct_param['query']['language'] = js_language;
			
			xml_request(struct_param,url_xml_rpc,iso_encoding,print_select_cities);
		}
	}
}

// Stampa la lista delle città
function print_select_cities(response, type) {
	var select_of_city = '<select id="city" onChange="show_suggested_city(this.value);">';
	select_of_city+= '<option value="***">'+js_dic_SELECT+'...</option>';
	select_of_city+= '<option value="other">'+js_dic_OTHER+'</option>';
	for(var c in response){
		var code = response[c]['code'];
		var name = response[c]['names'][js_language];
		select_of_city+= '<option value="'+code+'">'+capitalize(name)+'</option>';
	}
	select_of_city+= '</select> &nbsp;';
	
	document.getElementById('city_span').innerHTML = select_of_city;
}

// decide se mostrare l'input per suggerire una città da inserire
function show_suggested_city(city) {
	if(city == 'other')
		document.getElementById('suggested_city_span').style.display = 'inline';
	else
		document.getElementById('suggested_city_span').style.display = 'none';
}

function areas_of_country(country) {
    if(country != 'I')
        document.getElementById('area_tr').style.display = 'none';
    else {
        document.getElementById('area_tr').style.display = display_for_browser();
        document.getElementById('area_span').innerHTML = js_dic_LOADING+'...';
        
        struct_param['header']['type'] = 'areas_of_country';
        struct_param['query'] = new Array();
        struct_param['query']['country'] = country;
        struct_param['query']['language'] = js_language;
        
        xml_request(struct_param,url_xml_rpc,iso_encoding,print_select_areas);
    }
}

function print_select_areas(response, type) {
	var select_of_area = '<select id="area">';
	
	select_of_area+= '<option value="***">'+js_dic_SELECT+'...</option>';
	for(var c in response){
		var code = response[c]['code'];
		var name = response[c]['names'][js_language];
		select_of_area+='<option value="'+code+'">'+capitalize(name)+'</option>';
	}
	select_of_area+= '</select> &nbsp;';
	
	document.getElementById('area_span').innerHTML = select_of_area;
}

function provinces_of_country(country) {
    if(country != 'I')
        document.getElementById('province_tr').style.display = 'none';
    else {
        document.getElementById('province_tr').style.display = display_for_browser();
        document.getElementById('province_span').innerHTML = js_dic_LOADING+'...';
        
        struct_param['header']['type'] = 'provinces_of_country';
        struct_param['query'] = new Array();
        struct_param['query']['country'] = country;
        struct_param['query']['language'] = js_language;
        
        xml_request(struct_param,url_xml_rpc,iso_encoding,print_select_provinces);
    }
}

function print_select_provinces(response, type) {
	var select_of_province = '<select id="province">';
	
	select_of_province+= '<option value="***">'+js_dic_SELECT+'...</option>';
	for(var c in response){
		var code = response[c]['code'];
		var name = response[c]['names'][js_language];
		select_of_province+='<option value="'+code+'">'+capitalize(name)+'</option>';
	}
	select_of_province+= '</select> &nbsp;';
	
	document.getElementById('province_span').innerHTML = select_of_province;
}

// Check del form di richiesta registrazione
function client_control(callback) {
	reset_asterisk();
	
    // controllo accettazione privacy e condizioni
    if(document.getElementById('privacy') && document.getElementById('terms')) {
        if(!document.getElementById('privacy').checked || !document.getElementById('terms').checked) {
            div_loading.style.display = 'none';
            div_form.style.display = 'block';
            print_error(js_dic_ERRORACCEPTTOREGISTER);
            return false;
        }
    }
    
	var test;
	var pwd1;
	var pwd2;
	var arraycampi=form_obj.getElementsByTagName('input');
	div_form.style.display = 'none';
	div_loading.style.display = 'block';
	for (i = 0; i < arraycampi.length; i++) {
		var elem_type=arraycampi[i].getAttribute('type');
		if(elem_type!='button' && elem_type!='hidden' && elem_type!='reset' && elem_type!='null') {
			var elem_id = arraycampi[i].getAttribute('id');
			var elem_name = arraycampi[i].getAttribute('name');
			var elem_value = arraycampi[i].value;
			if(elem_id=='u_password')
				pwd1=elem_value;
			if(elem_id=='c_mailstaff' || elem_id=='c_mailaccountability' || elem_id=='u_email') {
				if (elem_value.indexOf('@',0) == -1) {
					div_loading.style.display = 'none';
					document.getElementById('ast_'+elem_name).innerHTML = '*';
					div_form.style.display = 'block';
					print_error(js_dic_NOEMAIL);
					return false;
				}
				var _ok = false;
				for (var o = 0; o < js_global_email_ext.length; o++) {
					var ap = elem_value.split('.');
					var domain = ap[ap.length - 1];
					if (domain == js_global_email_ext[o])
						_ok = true;
				}
				if (!_ok) {
					div_loading.style.display = 'none';
					div_form.style.display = 'block';
					document.getElementById('ast_'+elem_name).innerHTML = '*';
					print_error(js_dic_NOEMAIL);
					return false;
				}
			}
			else if (elem_id != 'suggested_city' && elem_id != 'suggested_country' && elem_id != 'c_website' && elem_id != 'c_accountability' && elem_id != 'tax_code') {
				if(elem_value=='') {
					div_loading.style.display = 'none';
					div_form.style.display = 'block';
					document.getElementById('ast_'+elem_name).innerHTML = '*';
					print_error(js_dic_NOALLFIELDS);
					return false;
				}
			}
            // in italia vige l'obbligo di partita iva e codice fiscale
            if(get_value('country') == 'I'){
                if (elem_id == 'c_accountability'){
                    var chk='';
                    if(elem_value.length!=11)
                        chk = js_dic_ERRORPILENGTH;
                    //var chk = controllaPIVA(elem_value);
                    if(chk!=""){
                        div_loading.style.display = 'none';
                        div_form.style.display = 'block';
                        document.getElementById('ast_'+elem_name).innerHTML = '*';
                        print_error(chk);
                        return false;
                    }
                }
            }
		}
	}
	
	// Controllo sulle password
	pwd2=document.getElementById('u_repwd').value
	if(pwd1!=pwd2) {
		div_loading.style.display = 'none';
		div_form.style.display = 'block';
		document.getElementById('ast_u_repwd').innerHTML = '*';
		document.getElementById('ast_u_password').innerHTML = '*';
		print_error(js_dic_PWDNOMATCH);
		return false;
	}
	else if(pwd1.length<5 || pwd2.length<5 || pwd1.length>12 || pwd2.length>12 || document.getElementById('u_login').value.length<5 || document.getElementById('u_login').value.length>12) {
			div_loading.style.display = 'none';
			div_form.style.display = 'block';
			document.getElementById('ast_u_password').innerHTML = '*';
			document.getElementById('ast_u_repwd').innerHTML = '*';
			document.getElementById('ast_u_login').innerHTML = '*';
			print_error(js_dic_PWDUSERWRONG);
			return false;
	}
	
	// controllo città, nazione e regione
	if(document.getElementById('country').value == '***' || (document.getElementById('country').value == 'other' && document.getElementById('suggested_country').value == '')) {
		div_loading.style.display = 'none';
		div_form.style.display = 'block';
		print_error(js_dic_ERRORREGISTRATIONCOUNTRY);
		return false;
	}
	else if((document.getElementById('city') && document.getElementById('city').value == '***') || ((document.getElementById('country').value == 'other' || (document.getElementById('city') && document.getElementById('city').value == 'other')) && document.getElementById('suggested_city').value == '')) {
		div_loading.style.display = 'none';
		div_form.style.display = 'block';
		print_error(js_dic_ERRORREGISTRATIONCITY);
		return false;
	}
    else if(document.getElementById('country').value == 'I' && document.getElementById('area').value == '***') {
        div_loading.style.display = 'none';
		div_form.style.display = 'block';
        document.getElementById('ast_area').innerHTML = '*';
		print_error(js_dic_NOALLFIELDS);
		return false;
    }
    else if(document.getElementById('country').value == 'I' && document.getElementById('province').value == '***') {
        div_loading.style.display = 'none';
		div_form.style.display = 'block';
        document.getElementById('ast_province').innerHTML = '*';
		print_error(js_dic_NOALLFIELDS);
		return false;
    }
    else if(get_value('country') == 'I'){
        var elem = document.getElementById('tax_code');
        if(elem.value == ''){
            div_loading.style.display = 'none';
            div_form.style.display = 'block';
            document.getElementById('ast_tax_code').innerHTML = '*';
            print_error(js_dic_NOALLFIELDS);
            return false;
        }
        if(elem.value.length != 16 && elem.value.length != 11)
            var chk = js_dic_ERRORCFLENGTH;
        else if(elem.value.length == 16)
            var chk = controllaCF(elem.value);
        else if(elem.value.length == 11) 
            var chk = '';
            //var chk = controllaPIVA(elem.value);
            
        if(chk!=""){
            if(chk == js_dic_ERRORPICHAR) chk = js_dic_ERRORCFCHAR;
            else if(chk == js_dic_ERRORPIINVALID) chk = js_dic_ERRORCFINVALID;
            div_loading.style.display = 'none';
            div_form.style.display = 'block';
            document.getElementById('ast_tax_code').innerHTML = '*';
            print_error(chk);
            return false;
        }
    }
    
	callback();
}

// Inserimento di un cliente
function client_insert() {
	var arraycampi=form_obj.getElementsByTagName('input');
	
	struct_param['header']['type'] = 'client_insert';
	struct_param['query'] = new Array();
	
	for (i = 0; i < arraycampi.length; i++) {
		var elem_type = arraycampi[i].getAttribute('type');
		var elem_id = arraycampi[i].getAttribute('id');
		if(elem_type!='reset' && elem_type!='button' && elem_type!='hidden' && elem_id!='suggested_city' && elem_id!='suggested_country' && elem_type!='null') {
		    var elem_name = arraycampi[i].getAttribute('name');
			var elem_value = arraycampi[i].value;
			if(elem_name!='c_outgoing' && elem_name!='c_incoming')
			    struct_param['query'][elem_name] = elem_value;
		}
	}
	
    struct_param['query']['c_tax_code'] = get_value('tax_code');
	
	// nazione e città
	if(get_value('country') == 'other')
		struct_param['query']['c_country'] = get_value('suggested_country');
	else
		struct_param['query']['c_country_code'] = get_value('country');
	if(!get_value('city') || get_value('city') == 'other')
		struct_param['query']['c_city'] = get_value('suggested_city');
	else
		struct_param['query']['c_city_code'] = get_value('city');
    
    if(get_value('area'))
        struct_param['query']['c_area'] = get_value('area');
    if(get_value('province'))
        struct_param['query']['c_province'] = get_value('province');
    // mi salvo il dominio di appartenenza del cliente
	struct_param['query']['c_domain'] = get_value('clientdomain');
	
	struct_param['query']['c_outgoing'] = false;
	struct_param['query']['c_incoming'] = false;
	
	if(hg('c_outgoing').checked==true)
	    struct_param['query']['c_outgoing'] = true;
	if(hg('c_incoming').checked==true)
	    struct_param['query']['c_incoming'] = true;	
		
    xml_request(struct_param,url_xml_rpc,iso_encoding,callbacks_xml_request);
}

function callbacks_xml_request(response,type)	{
	switch(type) {
		case 'client_insert' :
			if(response == 'ok') {
			  div_loading.style.display = 'none';
			  div_form.style.display = 'none';
			  //div_form_close.style.display = 'block';
			  //document.getElementById('client_name').innerHTML = js_dic_DEAR+' '+document.getElementById('u_name').value+', '+js_dic_THANKCHOSE+' '+sitename+'.';
			  
			  var params_to_post = new Array();
			  params_to_post['returner'] = 'return_ok';
			  params_to_post['greets'] = js_dic_DEAR+' '+document.getElementById('u_name').value+', '+js_dic_THANKCHOSE+' '+sitename+'.';
			  post_to_url('index.php',params_to_post,'post');
			}
			else {
			  div_loading.style.display = 'none';
			  div_form.style.display = 'block';
			  print_error(js_dic_ERRORTECHNICAL);
			}
		break;
	}
}
