﻿FormElement = function(IDElement,Length,NiveauObligation,Type){
    this.element = document.getElementById(IDElement);
    this.length = (Length && Length.trim != '') ? parseInt(Length,10) : -1 ;
    this.niveau = NiveauObligation;
    this.type = Type;
    if(this.element.type && this.element.type == "text"){
        this.element.formElement = this;
        this.element.onkeyup = TBValideFormElement;
    }    
}
function TBValideFormElement(){
    if(this.formElement){
        this.formElement.Validate();
    }
}
FormElement.prototype = {
    Validate : function(){
        return this.ValidateLength() && this.ValidateNiveau() && this.ValidateType();
    },
    ValidateLength : function(){
        var val = this.element.value;
        return this.length < 0 || val.length <= this.length;
    },
    ValidateNiveau : function(){
        var val = this.element.value;
        switch(this.niveau){
            case 'Obligatory' :
                return val != '';
                break;
            case 'Optional' :
                return true;
                break;
            case 'Forbidden' :
               return val == ''; 
               break;              
        }
        return false;
    },
    GetValue : function(){
        if(this.element && this.element.value && this.element.value.trim() != ''){
            var val = this.element.value.trim();            
            switch(this.type){
                case 'Single' :
                return parseFloat(val);
                    break;
                case 'Integer' :
                    return parseInt(val);
                    break;
                case 'Email' :
                return val;
                    break;
                case 'AlphaNumeric' :
                    return val;
                    break;
                case 'Date' :
                    return val;
                    break;
                case 'Mouse' :
                    return val;
                    break;            
            }
        }
        return '';
    },
    ValidateType : function(){
        var val = this.element.value;
        if(val.trim() == '') return true;
        switch(this.type){
            case 'Single' :
            return this.ValidateSingle();
                break;
            case 'Integer' :
                return this.ValidateInt();
                break;
            case 'Email' :
            return this.ValidateEmail();
                break;
            case 'AlphaNumeric' :
                return true;
                break;
            case 'Date' :
                return this.ValidateDate();
                break;
            case 'Mouse' :
                return true;
                break;
            
        }
        return false;
    },
    ValidateInt : function(){
	    var test=this.element.value;
	    if(test == '')
	        return true;
	    var z='';
	    var est_ok = true;
	    for (var i = 0; i < test.length ; i++) 
	    {
	        if ( (test.substring(i,i+1)>="0") && (test.substring(i,i+1)<="9") ) 
	        {
	            z = z + test.substring(i,i+1)
	         }
	         else{
	            est_ok = false;
	         }
	    }
	     //this.element.value=z;
	    return est_ok;
    },
    ValidateSingle : function(){
        var test = this.element.value;
	    z='';
	    var est_ok = true;
	    var virgule_ajout = false;
	    for (i = 0; i < test.length ; i++)
	    {
	        var char_test = test.substring(i,i+1);
	        if ( ( (char_test>="0") && (char_test<="9") ) || (char_test==".") || (char_test==",")  )
		    {
		        if(((char_test==".") || (char_test==",")) && !virgule_ajout)
		        {
		            char_test = ",";
		            virgule_ajout = true;
		        }
		        else if(((char_test==".") || (char_test==",")) && virgule_ajout){
		            char_test = '';
		            est_ok = false;
		        }
    		    
		        z = z+char_test;
		    }
		    else{
		        est_ok = false;
		    }
	    }
        this.element.value=z;
        return est_ok;	
    },
    ValidateEmail:function(){
        var email = this.element.value;
        invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;<>";
	    for (var i=0; i<invalidChars.length; i++) 
	    {
	        badChar = invalidChars.charAt(i);
	        if (email.indexOf(badChar,0) > -1) {return false;}
	    }
	    lengthOfEmail = email.length;
	    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {return false}
	    Pos = email.indexOf("@",1);
	    if (email.charAt(Pos + 1) == ".") {return false}
	    while ((Pos < lengthOfEmail) && ( Pos != -1)) 
	    {
	        Pos = email.indexOf(".",Pos);
	        if (email.charAt(Pos + 1) == ".") {return false}
	        if (Pos != -1) {Pos++}
	    }
	    atPos = email.indexOf("@",1);
	    if (atPos == -1) {return false}
	    if (email.indexOf("@",atPos+1) != -1) {return false}
	    periodPos = email.indexOf(".",atPos);
	    if (periodPos == -1) {return false}
	    if (periodPos+3 > email.length) {return false}
	    return true
    },
    ValidateDate:function(){
        var date = this.element.value;
        var date_split = date.split('/');
        try{
           var aiDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
            var iDay = parseInt(date_split[0],10);
            var iMonth = parseInt(date_split[1],10);
            var iYear = parseInt(date_split[2],10);
                
            if (iDay < 1 || iMonth < 1 || iYear < 0)
			    return false;

            if (iMonth > 12)
			return false;

            iYear += iYear < 100 ? iYear > 10 ? 1900 : 2000 : 0;
            aiDays[1] += (iYear % 4 ? 0 : iYear % 100 ? 1 : iYear % 400 ?
            iYear == 200 ? 1 : 0 : 1);

            return (iDay <= aiDays[iMonth - 1]);
            
        }
        catch(err){
            return false;
        }
        return false;
    }
}
var meCustomerForm ;
CustomerForm = function(infoResa){
    this.infoResa = infoResa;
    this.tab_tb = new Array();    
    this.tab_tb_persons = new Array();
    this.nb_accompanying_person = 0;
}
CustomerForm.prototype = {
    AddTextBox : function(propertyName,IDTb){
        if(document.getElementById(IDTb)){
            
            this[propertyName] = this.GetFormElement(IDTb);
            this.tab_tb.push(propertyName);
        }
    },
    GetFormElement: function(ID,level){
        if(document.getElementById(ID)){
            var elem = document.getElementById(ID);
            
            var form_e = new FormElement(
                ID,
                (elem.attributes.maxlength) ? elem.attributes.maxlength.nodeValue : null,
                (elem.attributes.levelObligationValue) ? elem.attributes.levelObligationValue.nodeValue : "Optional",
                (elem.attributes.typedata) ? elem.attributes.typedata.nodeValue : "AlphaNumeric");
           if(level)
                form_e.niveau = level;
           return form_e;
        }
        return null;
    },
    SetForPitch  : function(classCBInfoForPitch,IDTbXDmension,IDTbYDimension){
        this.tab_cb_infos_pitch = getElementsByClassName(classCBInfoForPitch);
        this.AddTextBox('x_dimension',IDTbXDmension);
        this.AddTextBox('y_dimension',IDTbYDimension);
    },
    SetForAirport : function(IDSelectAirport,IDTbCommentAirport){
        this.select_airport = document.forms[0][IDSelectAirport];
        this.AddTextBox('comment_airport',IDTbCommentAirport);
    },
    SetForInfoPerso : function(IDTbSurName,IDTbFirstName,IDTbBirthDate,IDTbAdress,IDTbPostCode,IDTbTown,IDTbCountry,IDTbTel,IDTbFax,IDTbEMail,IDTbSpecialRequest){
        this.AddTextBox('sur_name',IDTbSurName);
        this.AddTextBox('first_name',IDTbFirstName);
        this.AddTextBox('birth_date',IDTbBirthDate);
        this.AddTextBox('adress',IDTbAdress);
        this.AddTextBox('post_code',IDTbPostCode);
        this.AddTextBox('town',IDTbTown);
        this.AddTextBox('country',IDTbCountry);
        this.AddTextBox('tel',IDTbTel);
        this.AddTextBox('fax',IDTbFax);
        this.AddTextBox('email',IDTbEMail);
        this.AddTextBox('special_request',IDTbSpecialRequest);
    },
    SetAccompanyingPerson : function(BaseSurName,BaseFirstName,BaseBirthDate,IDSelectNbPerson){
        this.base_sur_name =BaseSurName;
        this.base_first_name = BaseFirstName;
        this.base_birth_date = BaseBirthDate;      
        this.select_nb_person = document.getElementById(IDSelectNbPerson);
    },
    SetAcompanyingPerson : function(){
        
        this.nb_accompanying_person = 0;
        this.tab_tb_person = null;
        if(this.select_nb_person && this.select_nb_person.value != ''){
            this.tab_tb_person = new Array();
            var nb = parseInt(this.select_nb_person.value,10);
            for(var i = 1;i<=nb;i++){
                if(document.getElementById(this.base_sur_name+''+i)){
                    this.tab_tb_person.push({
                    sur_name:this.GetFormElement(this.base_sur_name+''+i,"Obligatory"),
                    first_name:this.GetFormElement( this.base_first_name+''+i,"Obligatory"),
                    birth_date:this.GetFormElement( this.base_birth_date+''+i,"Obligatory")});
                    this.nb_accompanying_person++;            
                }
            }
        }
    },
    GetSelectedAirport : function(){
         if( typeof(this.select_airport) == "undefined" ) return '';
         if(this.select_airport.value) return this.select_airport.value;
        for(var i = 0;i<this.select_airport.length;i++){
            if(this.select_airport[i].checked){
                return this.select_airport[i].value;
            }
        }
        return '';
    }    ,
    Check:function(callBack,callBackWS){
        if(this.ValidateFields(callBack)){
        
            meCustomerForm = this;
            this.callBackWS = callBackWS;
            PageMethods.CheckCustomerFormInformations(
                this.infoResa.idEngine,
                this.infoResa.idEtablissement,
                this.infoResa.isoLanguageCode,
                this.infoResa.tac,
                this.infoResa.spec,
                this.infoResa.crcChoosenOption,
                this.infoResa.StringIdAssurance,
                this.GetValue('sur_name'),
                this.GetValue('first_name'),
                this.GetDate(this.GetValue('birth_date')),
                this.GetValue('adress'),
                this.GetValue('post_code'),
                this.GetValue('town'),
                this.GetValue('country'),
                this.GetValue('tel'),
                this.GetValue('fax'),
                this.GetValue('email'),
                this.nb_accompanying_person,
                this.GetAccompanyingPersons(),
                this.GetValue('special_request'),
                this.GetIdInfoForPitch(),
               this.GetValue('x_dimension'),
             this.GetValue('y_dimension'),         
               this.GetSelectedAirport(),
                this.GetValue('comment_airport'),
                OnCompleteCF,
                OnErrorCF,
                OnTimeoutCF);
         }
            
    },
    ValidateFields:function(callBack)   
    {      
    this.SetAcompanyingPerson();
        var est_ok = true;   
        for(var i = 0;i<this.tab_tb.length;i++){
            if(!this[this.tab_tb[i]].Validate()){
                if(callBack)
                    callBack(this[this.tab_tb[i]].element);
                est_ok = false;
            }
        }
        if(this.tab_tb_person ){
            for (var i = 0;i < this.tab_tb_person.length;i++){
                var elem = this.tab_tb_person[i];
                 if(!elem.sur_name.Validate()){
                    if(callBack)
                        callBack(elem.sur_name.element);
                    est_ok = false;
                 }
                 if(!elem.first_name.Validate()){
                    if(callBack)
                        callBack(elem.first_name.element); 
                    est_ok = false;
                 }
                 if(!elem.birth_date.Validate()){
                    if(callBack)
                        callBack(elem.birth_date.element);     
                    est_ok = false;
                 }   
            }
        }
        else{
            if(this.select_nb_person){
                if(callBack)
                    callBack(this.select_nb_person);
                est_ok = false;
            }
        }
        return est_ok;
    },
    GetValue:function(fieldName){
        if(this[fieldName]){
            return this[fieldName].GetValue();
        }
        return '';
    },
    GetAccompanyingPersons:function(){
        var res = new Array();
        
        if(this.tab_tb_person){
            for(var i = 0; i<this.tab_tb_person.length;i++){
                var un_form = this.tab_tb_person[i];
                var un_person = new ControlSkin3.miniserv.accompanyingPersons();
                un_person.surName = un_form.sur_name.element.value;
                un_person.firstName = un_form.first_name.element.value;
                un_person.birthDate = this.GetDate(un_form.birth_date.element.value);
                res.push(un_person);
            }
        }
        return res;
    },
    GetIdInfoForPitch : function(){
        var res = new Array();
        for(var i = 0; i<this.tab_cb_infos_pitch.length;i++){
            if(this.tab_cb_infos_pitch[i].checked)
                res.push(this.tab_cb_infos_pitch[i].value);
        }
        return res;
    },
    GetDate:function(chaine){
        if(chaine && chaine != ''){
            var chaine_split = chaine.split('/');
            return new Date(
            parseInt(chaine_split[2],10),
            (parseInt(chaine_split[1],10) -1),
            parseInt(chaine_split[0],10));
                
        }
        return null;
    },
    RetourWS:function(result){
     //alert('dehors');
        if(result.isCorrect == true){   
            //alert('dedans');
            document.getElementById("bscrc").value = result.crc;
            document.forms[0].action = domaineSSL+"booking_summary.aspx";
            document.forms[0].submit();
        }
        else{
            this.callBackWS(result);
        }
    },
    Precedent:function(){
        
        
        document.forms[0].action = "options.aspx";
       document.forms[0].submit(); 
       }
}

function OnCompleteCF(result){

    meCustomerForm.RetourWS(result);
}
function OnErrorCF(){
alert('error');
}
function OnTimeoutCF(){
alert('timeout');
}
function InitInfoResaFormCustomerForm(idEngine,idEtablissement,isoLanguageCode,tac,spec,crcChoosenOption,StringIdAssurance)
{
    var inforesa = new InfosResa();
    inforesa.idEngine = idEngine;
    inforesa.idEtablissement = idEtablissement;
    inforesa.isoLanguageCode = isoLanguageCode;
    inforesa.tac = tac;
    inforesa.spec = spec;
    inforesa.crcChoosenOption = crcChoosenOption;
    inforesa.StringIdAssurance = StringIdAssurance;
    return inforesa;
}

