/******************************************************************************
* DETAILS
* File            : api functions
* Author      : AID
* ___________________________________________________
* CHANGELOG
* 081208 - Creation
* ___________________________________________________
* NOTES
*
* ___________________________________________________
* function LIST

* rebuild_url = function(step)
* api_request = function(is_init)
* api_request_2 = function(date, origin)
* back_step_2 = function()
* api_request_3 = function(page)
* send_password = function ()
* resa_group_submit = function ()
* customer_submit = function(page)
* customer_confirmation_submit = function()
* send_friends = function()
* send_friends_submit = function ()
* resa_cancel = function ()
* init_nb_people_promo = function(id, nb_people_min, nb_people_max)
* clean_nb_people_promo = function(id)
* clean_nb_people_promo = function(id)
* resa_vaucher = function (url_vaucher)
* back_times = function (id_lcfg)
* gen_vcs = function ()
* toggle = function(elt, display_value)
* show = function(elt)
* hide = function(elt)
* save_time = function(id_time)
* save_nb_people = function(nb_people)
* get_resa_params = function()
* get_global_params = function ()
******************************************************************************/

var is_reset_promotion = false;
var is_first_click_reservation = true;

//__________________________________________________________________________________________________

rebuild_url = function(step) {
//	return;
	var url = window.location;
	var s = new String(url);
	var i = s.indexOf("#");
	if (i > 0) {	
		t_url = s.split('#');
	//	prev_url = t_url[0] + "#resa_step_" + step;
		window.location = t_url[0] + "#resa_step_" + step;
	} else {
	//	prev_url = url + "#resa_step_" + step;
		window.location = url + "#resa_step_" + step;
	}
}

//__________________________________________________________________________________________________
/*
function check_url() {
	var url = window.location;
	if (url != prev_url) {
		alert(url);
		prev_url = url;
	}
	setTimeout(check_url, 100);
}
check_url();
*/
//__________________________________________________________________________________________________

api_request = function(is_init) {
	if (is_init == undefined) is_init = false;
	if (is_init) {
		if (! is_first_click_reservation) return;
		is_first_click_reservation = false;
	}
	rebuild_url("1");
	var url = "/w/api/request.php";
	var f = $('f_resa_params');
	var param  = "";
    param+= "&uid_restaurant=" + ajax_code(f.uid_restaurant.value);
	param+= "&id_restaurant=" + ajax_code(f.id_restaurant.value);
	param+= "&id_server=" + ajax_code(f.id_server.value);
    param+= "&zipcode_restaurant=" + ajax_code(f.zipcode_restaurant.value);
	param+= "&name_restaurant=" + ajax_code(f.name_restaurant.value);
	param+= "&phone_restaurant=" + ajax_code(f.phone_restaurant.value);
    param+= "&id_group_menu=" + ajax_code(f.id_group_menu.value);
    param+= "&step=1";
    run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

api_request_1 = function(id_promo) {
    if (id_promo == undefined) id_promo = 0;
	var url = "/w/api/request.php";
	var param = "step=2";
	var f = $('f_resa_params');
    param+= "&uid_restaurant=" + ajax_code(f.uid_restaurant.value);
    param+= "&id_restaurant=" + ajax_code(f.id_restaurant.value);
    param+= "&id_server=" + ajax_code(f.id_server.value);
    param+= "&zipcode_restaurant=" + ajax_code(f.zipcode_restaurant.value);
    param+= "&name_restaurant=" + ajax_code(f.name_restaurant.value);
    param+= "&phone_restaurant=" + ajax_code(f.phone_restaurant.value);
	param+= "&id_group_menu=" + ajax_code(f.id_group_menu.value);
    if (id_promo == 0) id_promo = ajax_code(f.id_promotion.value);
	if (id_promo == 0) id_promo = ajax_code(f.id_promotion_init.value);
	param+= "&id_promotion=" + id_promo;
	rebuild_url("2");
	if ($('span_res_promo') != null) $('span_res_promo').style.display = "block";
	is_reset_promotion = false;
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

api_request_2 = function(date, origin) {
	if (date == undefined) return;
	if (origin == undefined) origin = '';
	t_date = date.split("-");
	date = t_date[2] + "/" + t_date[1] + "/" + t_date[0];
	var url = "/w/api/request.php";
	var param = "";
	param+= "step=3";
	param+= "&date=" + date;
	
	var f = $('f_resa_params');
	param+= "&uid_restaurant=" + ajax_code(f.uid_restaurant.value);
	param+= "&id_restaurant=" + ajax_code(f.id_restaurant.value);
	param+= "&id_server=" + ajax_code(f.id_server.value);
	param+= "&zipcode_restaurant=" + ajax_code(f.zipcode_restaurant.value);
	param+= "&name_restaurant=" + ajax_code(f.name_restaurant.value);
	param+= "&phone_restaurant=" + ajax_code(f.phone_restaurant.value);
	param+= "&id_group_menu=" + ajax_code(f.id_group_menu.value);
    if (origin == 'on_promo') param+= "&id_promotion=" + ajax_code(f.id_promotion.value);
	param+= "&id_promotion_init=" + ajax_code(f.id_promotion_init.value);
    param+= "&id_restaurant_menu=" + ajax_code(f.id_restaurant_menu.value);
    param+= "&lunch_type=" + ajax_code(f.lunch_type.value);
    param+= "&id_lcfg=" + ajax_code(f.id_lcfg.value);
	rebuild_url("3");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

back_step_2 = function() {
	var url = "/w/api/request.php";
	var param = "";
	param+= "step=3";
	var f = $('f_resa_params');
	t_date = f.date.value.split("-");
	date = t_date[2] + "/" + t_date[1] + "/" + t_date[0];
	param+= "&date=" + date;
	param+= "&uid_restaurant=" + ajax_code(f.uid_restaurant.value);
	param+= "&id_server=" + ajax_code(f.id_server.value);
	param+= "&id_restaurant=" + ajax_code(f.id_restaurant.value);
	param+= "&zipcode_restaurant=" + ajax_code(f.zipcode_restaurant.value);
	param+= "&name_restaurant=" + ajax_code(f.name_restaurant.value);
	param+= "&phone_restaurant=" + ajax_code(f.phone_restaurant.value);
	param+= "&id_promotion=" + ajax_code(f.id_promotion.value);
	param+= "&id_promotion_init=" + ajax_code(f.id_promotion_init.value);
	param+= "&id_group_menu=" + ajax_code(f.id_group_menu.value);
    param+= "&id_restaurant_menu=" + ajax_code(f.id_restaurant_menu.value);
//    param+= "&lunch_type=" + ajax_code(f.lunch_type.value);
    rebuild_url("3");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

api_request_3 = function(page) {
    if (page == undefined) page = "";
    var url = "/w/api/request.php";
	var param = "step=4";
	param+= get_resa_params();
	if (page != "") param+= "&page=" + page;
	switch (page) {
		case "lf_identify_submit" : 
		case "lf_password_submit" :	
			var f_customer = $('f_customer_identify');
			param+= "&email=" + ajax_code(f_customer.identify_email.value);
			param+= "&password=" + ajax_code(f_customer.password.value);
			if (f_customer.remember.checked) param+="&remember=YES";
			break;	
			
		case "lf_account_submit" :
			//customer informations
			var f_customer = $('f_customer_lf');
			param+="&civility=" + get_radios_value('civility');
			param+="&first_name=" + ajax_code(f_customer.first_name.value);
			param+="&last_name=" + ajax_code(f_customer.last_name.value);
			param+="&email=" + ajax_code(f_customer.email.value);
			param+="&phone=" + ajax_code(f_customer.phone.value);
			param+="&password=" + ajax_code(f_customer.password.value);
			param+="&zipcode=" + ajax_code(f_customer.zipcode.value);
			if (f_customer.newsletter.checked) param+="&newsletter=YES";
			if (f_customer.promo.checked) param+="&promo=YES";
			break;	
		
		case "lf_lost_password" :
			var f_customer = $('f_customer_identify');
			param+= "&email=" + ajax_code(f_customer.identify_email.value);
			break;
			
		case "lf_lost_password_submit" :
			var f_customer = $('f_customer_lost_password');
			param+= "&email=" + ajax_code(f_customer.email.value);
			break;
			
		case "group":
		case "lf_disconnect" :	
        case "lf_identify" :	
		case "lf_account":
        case "waiting":
        case "customer_account" :
        default : 
            break;
	}
	rebuild_url("4");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

send_password = function () {
	var f_customer = $('f_customer_identify');
	var url = "/w/api/send_password.php";
	param = "email=" + f_customer.identify_email.value;
	run_ajax("send_password_transparent", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

resa_group_submit = function () {
	var f_group = $('f_resa_group');
	var url = "/w/api/request.php";
	var param = "step=5";
	param+="&page=group_submit";

	//global parameters
	param+= get_global_params();

	param+="&date=" + ajax_code(f_group.month.value) + "-" + ajax_code(f_group.day.value);
	param+="&time=" + ajax_code(f_group.time.value);
	param+="&nb_people=" + ajax_code(f_group.nb_people.value);
	param+="&budget=" + ajax_code(f_group.budget.value);
	param+="&civility=" + get_radios_value('civility');
	param+="&first_name=" + ajax_code(f_group.first_name.value);
	param+="&last_name=" + ajax_code(f_group.last_name.value);
	param+="&email=" + ajax_code(f_group.email.value);
	param+="&phone=" + ajax_code(f_group.phone.value);
	param+="&company=" + ajax_code(f_group.company.value);
	param+="&notes=" + ajax_code($('notes').value);
	rebuild_url("5");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

customer_submit = function(page) {
	if (page == undefined) page = "";
	var f_customer = $('f_customer');
	var url = "/w/api/request.php";
	var param = "step=5";
	param+="&page=" + page;
	param+= get_resa_params();

    //customer informations
	if (get_radios_value('civility') != '') param+="&civility=" + get_radios_value('civility');
	else param+="&civility=" + ajax_code(f_customer.civility.value);
	param+="&first_name=" + ajax_code(f_customer.first_name.value);
	param+="&last_name=" + ajax_code(f_customer.last_name.value);
	param+="&email=" + ajax_code(f_customer.email.value);
	param+="&phone=" + ajax_code(f_customer.phone.value);
	if (f_customer.password != null && f_customer.password != undefined) param+="&password=" + ajax_code(f_customer.password.value);
	param+="&zipcode=" + ajax_code(f_customer.zipcode.value);
	param+="&smoking=" + ajax_code(f_customer.smoking.value);
	param+="&notes=" + ajax_code($('lf_notes').value);
	param+="&company=" + ajax_code(f_customer.company.value);
 
    if (f_customer.lf_newsletter.type == "checkbox") {
        if (f_customer.lf_newsletter.checked) param+="&lf_newsletter=YES";
        else param+="&lf_newsletter=NO";
    } else {
        param+="&lf_newsletter=" + f_customer.lf_newsletter.value;
    }
    
    if (f_customer.lf_promo.type == "checkbox") {
        if (f_customer.lf_promo.checked) param+="&lf_promo=YES";
        else param+="&lf_promo=NO";
    } else {
        param+="&lf_promo=" + f_customer.lf_promo.value;
    }
  
	rebuild_url("5");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

customer_prescriber_submit = function() {
    var page = "";
	var f_customer = $('f_customer');
	var url = "/w/api/request.php";
	var param = "step=5";
	param+="&page=" + page;
	param+= get_resa_params();

    //customer information
    param+="&civility=" + get_radios_value('customer_civility');
    param+="&first_name=" + ajax_code(f_customer.customer_first_name.value);
	param+="&last_name=" + ajax_code(f_customer.customer_last_name.value);
    
    param+="&notes=" + ajax_code($('lf_notes').value);
    
    //prescriber informations
    if (f_customer.prescriber_identify != null)  param+="&prescriber_identify=" + ajax_code(f_customer.prescriber_identify.value);
    if (f_customer.prescriber_password != null)  param+="&prescriber_password=" + ajax_code(f_customer.prescriber_password.value);
    
    rebuild_url("5");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

customer_confirmation_submit = function() {
	var f_customer = $('f_customer_confirmation');
	var url = "/w/api/request.php";
	var param = "step=5";
	param+= "&page=customer_confirmation_submit";
	param+= get_resa_params();
	//customer informations
	param+="&prev_email=" + ajax_code(f_customer.prev_email.value);
	param+="&prev_password=" + ajax_code(f_customer.prev_password.value);
	param+="&prev_zipcode=" + ajax_code(f_customer.prev_zipcode.value);
	param+="&email=" + ajax_code(f_customer.email.value);
	param+="&zipcode=" + ajax_code(f_customer.zipcode.value);
	param+="&password=" + ajax_code(f_customer.password.value);
	param+="&is_request=" + ajax_code(f_customer.is_request.value);
    rebuild_url("5");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

send_friends = function() {
	var url = "/w/api/request.php";
	var param = "step=5";
	param+="&page=send_friends";
	param+= get_resa_params();
	rebuild_url("5");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

send_friends_submit = function () {
	var f_friends = $('f_send_friends');
	var url = "/w/api/request.php";
	var param = "step=5";
	param+="&page=send_friends_submit";
	param+= get_resa_params();
	param+="&message=" + ajax_code($('message_friends').value);	
	param+="&email_1=" + ajax_code(f_friends.email_1.value);
	param+="&email_2=" + ajax_code(f_friends.email_2.value);
	param+="&email_3=" + ajax_code(f_friends.email_3.value);
	param+="&email_4=" + ajax_code(f_friends.email_4.value);
	param+="&email_5=" + ajax_code(f_friends.email_5.value);
	param+="&email_6=" + ajax_code(f_friends.email_6.value);
	rebuild_url("5");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

resa_cancel = function () {
	var url = "/w/api/request.php";
	var param = "step=5";
	param+= "&page=cancel_resa";
	param+= get_resa_params();
	rebuild_url("5");
	run_ajax("div_resa_main", url, {method: 'post', evalScripts: true, parameters: param});
}

//__________________________________________________________________________________________________

init_nb_people_promo = function(id, nb_people_min, nb_people_max) {
	for (i = nb_people_min; i <= nb_people_max; i++) {
		if ($("nb_p_" + i + "_" + id) != null) {
			$("nb_p_" + i + "_" + id).className = 'but3';
		}
	}
}

//__________________________________________________________________________________________________

clean_nb_people_promo = function(id) {
	for (i = 1; i < 23; i++) {
		if ($("nb_p_" + i + "_" + id) != null) {
			$("nb_p_" + i + "_" + id).className = 'but2';
		}
	}
	if ($('span_res_promo') != null) $('span_res_promo').style.display = "none";
	is_reset_promotion = true;
}

//__________________________________________________________________________________________________

resa_vaucher = function (url_vaucher) {
	window.open(url_vaucher, '_blank', 'width=600, height=580, resize=yes, menubar=yes');
}

//__________________________________________________________________________________________________

back_times = function (id_lcfg) {
	$('span_res_time').innerHTML = '';
	save_time('');
	show('tab_0');
	hide("tab_1_" + id_lcfg);
	show('span_res_promo');
	is_reset_promotion = false;
}

//__________________________________________________________________________________________________

gen_vcs = function(uid, ir, is, id_reservation, h) {
	var params = "uid_restaurant=" + uid;
	params+= "&id_restaurant=" + ir;
	params+= "&id_server=" + is;
	params+= "&id_reservation=" + id_reservation;
    params+= "&h=" + h;
    window.location.href = "/w/api/gen_vcs.php?" + params;
}

//__________________________________________________________________________________________________

toggle = function(elt, display_value) {
	var elt = document.getElementById(elt);
	elt.style.display = display_value;
}

//__________________________________________________________________________________________________
		
show = function(elt) { toggle (elt, ''); }

//__________________________________________________________________________________________________
		
hide = function(elt) {toggle (elt, 'none');}
	
//__________________________________________________________________________________________________
	
save_time = function(id_time){$('f_resa_params').time.value = id_time;}

//__________________________________________________________________________________________________
		
save_nb_people = function(nb_people){ $('f_resa_params').nb_people.value = nb_people;}

//__________________________________________________________________________________________________

save_group_menu = function(id_group_menu) {$('f_resa_params').id_group_menu.value = id_group_menu;}

//__________________________________________________________________________________________________

get_resa_params = function() {
	var f = $('f_resa_params');
	var param  = "";
	param+= get_global_params();
	param+= "&time=" + ajax_code(f.time.value);
	param+= "&nb_people=" + ajax_code(f.nb_people.value);
	param+= "&date=" + ajax_code(f.date.value);
	param+= "&id_lcfg=" + ajax_code(f.id_lcfg.value);
	param+= "&id_reservation=" + ajax_code(f.id_reservation.value);
	param+= "&id_group_menu=" + ajax_code(f.id_group_menu.value);
    param+= "&id_restaurant_menu=" + ajax_code(f.id_restaurant_menu.value);
    param+= "&lunch_type=" + ajax_code(f.lunch_type.value);
    return param;
}

//__________________________________________________________________________________________________

get_global_params = function () {
	var f = $('f_resa_params');
	var param  = "";
	param+= "&uid_restaurant=" + ajax_code(f.uid_restaurant.value);
	param+= "&id_restaurant=" + ajax_code(f.id_restaurant.value);
	param+= "&id_server=" + ajax_code(f.id_server.value);
	param+= "&zipcode_restaurant=" + ajax_code(f.zipcode_restaurant.value);
	param+= "&name_restaurant=" + ajax_code(f.name_restaurant.value);
	param+= "&phone_restaurant=" + ajax_code(f.phone_restaurant.value);
	if (! is_reset_promotion) param+= "&id_promotion=" + ajax_code(f.id_promotion.value);
	param+= "&id_promotion_init=" + ajax_code(f.id_promotion_init.value);
	param+= "&id_lf_customer=" + ajax_code(f.id_lf_customer.value);
	return param;
}
