﻿// -- GLOBALS

var SERVICE_URL = "/KPI_retraite/sendRequest";
var DEFAULT_METHOD = "GET";


// MAPPING FOR SOCIAL STATUSES LABELS

var SOCIAL_STATUS_LABELS_MAP = {
	"Régime général": "SALARIE",
	"Travailleur non-salarié": "TNS",
	"Alsace-Moselle": "ALSACE",
	"Exploitant agricole": "AGRI",
	"Sans profession": "UNEMPLOYED",
	"Sénior": "SENIOR",
	"Etudiant": "STUDENT"
}


// -- Links handlers

$(document).ready(function() {

    $(".link_home").click(function() { return request("/"); });
	$(".link_contact").click(function() { return request("/contact.html"); });
    $(".link_who").click(function() { return request("/help/qui.html"); });
    $(".link_legal").click(function() { return request("/help/mention_legale.html"); });
    $(".link_links").click(function() { return request("/help/liens.html"); });
    $(".link_expl").click(function() { return request("/help/explications_generales.html"); });
	$(".link_partenaires").click(function() { return request("/help/nos-assureurs-santes.html"); });
	$(".link_profiles").click(function() { return request("/help/profils-mutuelle-complementaire-sante.html"); });
	$(".link_profil_unesco").click(function() { return request("/unesco/contrat-collectif.html"); });
	$(".link_offre").click(function() { return request("/offre.html"); });
    $(".link_faq").click(function() { return request("/help/faq.html"); });
    $(".link_lexique").click(function() { return request("/help/lexique.html"); });
    $(".link_txt_legaux").click(function() { return request("/help/textes_legaux.html"); });
	$(".link_map").click(function() { return request("/help/plan-du-site.html"); });
	$(".link_souscription").click(function() { return request("/help/souscription.html"); });
	$(".link_resiliation").click(function() { return request("/help/resiliation.html"); });
	
	$(".link_chat").click(function() { runChat(); });$(".link_simu").click(function() {
		var params = { servicename: "inputHealthcareSimu" };
		return request(SERVICE_URL, params);
    });
	
    $(".link_lvg").click(function() {
       var params = { servicename: "inputHealthLVGProfil" };
       return request(SERVICE_URL, params);
	});
	
	$(".link_student").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "STUDENT",
						stylesheetname: "profile_student" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_tns").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "TNS",
						stylesheetname: "profile_selfemployed" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_unemployed").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "UNEMPLOYED",
						stylesheetname: "profile_unemployed" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_senior").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "SENIOR",
						stylesheetname: "profile_senior" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_salarie").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "SALARIED",
						stylesheetname: "profile_salaried" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_single").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "SINGLE",
						stylesheetname: "profile_single" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_pair").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "PAIR",
						stylesheetname: "profile_pair" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_familysingle").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "FAMILY_SINGLE",
						stylesheetname: "profile_familysingle" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_senior").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "SENIOR",
						stylesheetname: "profile_senior" };
		return request(SERVICE_URL, params);
	});
	
	$(".link_family").click(function() { 
		var params = { 	servicename: "getHealthFormulaeByProfile",
						profile: "FAMILY",
						stylesheetname: "profile_family" };
		return request(SERVICE_URL, params);
	});
	$("a[id^='link_company']").click(function() { 
	
		var id = $(this).attr("id");
		var companyId = id.substring(id.lastIndexOf('_')+1);
		
		var params = { 	
					servicename: "getHealthFormulaeByCompany",
					company_id: companyId
				};
		return request(SERVICE_URL, params);
	});

    // INFOS DIALOG ---------------------------------------
    if($("#infos_dialog").length) {
        $("#infos_dialog").dialog({
            bgiframe: true,
            autoOpen: false,
            width: 360,
            height: 270,
            modal: true,
            draggable: false,
            resizable: false,
            closeOnEscape: false,
            open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },		// hide the 'X' close button in the top-right corner
            buttons: {
                'OK': function() {
                    valid = $("#infos_form").valid();
                    if(valid == false) {
                        return false;
                    }
                    $("#infos_form").submit();
                }
            }
        });

         $("#infos_form").validate({
            rules: {
                tr_customer_email: {
                   required: true,
                   email: true
                },
                tr_customer_nom: "required",
                tr_customer_tel: {
                    required: true,
                    digits: true
                }
            },

            messages: {
                tr_customer_email: "",
                tr_customer_tel: "",
                tr_customer_nom: ""
            }
        });
    }

	// TRANSITION PUBS
	/*if($("#partenaires").size() > 0 ) {
		var state = 0;
		$.timer(10000, function (timer) {
			if(state == 0) {
				$("#partenaires").html('<a href="/KPI_retraite/sendRequest?servicename=getHealthFormulaeByCompany&company_id=5"><img src="/images/pub-senior.gif" /></a>');
				state = 1;
			}
			else {
				$("#partenaires").html('<a href="/offre.html"><img src="/images/pub.gif" /></a>');
				state = 0;
			}
		});
	}*/

// END OF jQuery(document).ready()
});


function request(path, params, method) {
    method = method || DEFAULT_METHOD;

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", 'http://' + window.location.hostname+path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key].toString());

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
   
    return false;
}


function openPopup(name, html, w, h, scollabal) {

	var x = (screen.width-w)/2;
	var y = (screen.height-h)/2;
	var win = window.open('http://' + window.location.hostname+html, name,
					"height="+h+",width="+w+",menubar=no,scrollbars="+scollabal+ ",resizable=no,location=no,directories=no,status=no");

	return win;
}


function runChat() {
	openPopup("LiveChat","/KPI_retraite/sendRequest?servicename=checkEmail&tr_customer_email=" + $("#contact_email").val() + "&stylesheetname=mcs", 
							800, 600, "no");
	return false;
}

// Date functions
Date.prototype.isBissextile = function() {
    var y = this.getUTCFullYear();
    return (( y%400==0 || ( (y%4==0) && (y%100 != 0) ) ) ? true : false);
}

Date.prototype.nextDay = function() {
    var nextDay = this.getUTCDate()+1;
    var month = this.getUTCMonth();
    var year = this.getUTCFullYear();

    var februaryLastDay = this.isBissextile() ? 29 : 28;

    var bigMonth = (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) ? true : false;

    this.setUTCDate(nextDay);

    if((month == 1 && nextDay > februaryLastDay) || (bigMonth && nextDay > 31) || (!bigMonth && nextDay > 30)) {
        this.setUTCDate(1);
        if(month == 11) {
            this.setUTCMonth(0);
            this.setUTCFullYear(year+1);
        }
        else {
            this.setUTCMonth(month+1);
        }
    }
    return this;
}


Date.prototype.nextMonth = function(day) {

    if(typeof day === 'undefined') {  day = 1; }

    if (this.getMonth() == 11) {
        this.setFullYear(this.getFullYear() + 1, 0, day);
    }
    else {
        this.setMonth(this.getMonth() + 1, day);
    }

    return this;
}

// Date utils

function formatDateNumber(nb) {
    var nb = new Number(nb);
    if(nb.toString().length == 1)
        nb = 0 + nb.toString();

    return nb;
}
function goToHttpPage(page) {
    window.location='http://' + window.location.hostname+page;
}
function requestHttps(path, params, method) {
    method = method || DEFAULT_METHOD;

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", 'https://' + window.location.hostname+path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key].toString());

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
   
    return false;
}
function souscrire (formulaId){
					
					var params = {
						servicename: "newHealthSubscriptionPersonal",
						stylesheetname: "mcs",
						formulaId: formulaId
					};
					return requestHttps(SERVICE_URL, params);
				}
