var mdvProfile = null;

// onload events 
window.addEvent('load', function(){

	mdvProfile = new MDVProfile();
	
}); 

// MDVProfile 
function MDVProfile() {
    // ids über  mdvLib ???
    if($('sessionID')){
        this.sessionID = $('sessionID').value;
        this.requestID = $('requestID').value;
        this.lang = $('language').value;
        this.route = $('choosenRoute').value;
        this.execInst = '';
        this.identifier = '';
    }
}

// this function insert the content 
MDVProfile.prototype.reloadPage = function (response) {
    
    if(currentPage=='enquiry'){
        this.execInst = 'verifyOnly'
    }
    
    if(this.sessionID == '0'){
       location.href='XSLT_TRIP_REQUEST2?language=' + this.lang 
                        +'&itdLPxx_currentPage=' + currentPage
                        +'&itdLPxx_currentMap=' + currentMap 
                        +'&itdLPxx_choosenRoute=' + this.route;
       
    }
    else{
    
       location.href='XSLT_TRIP_REQUEST2?language=' + this.lang 
                        +'&sessionID=' + this.sessionID 
                        +'&requestID=' +this.requestID 
                        +'&execInst=' + this.execInst
                        +'&itdLPxx_currentPage=' + currentPage
                        +'&itdLPxx_currentMap=' + currentMap 
                        +'&itdLPxx_choosenRoute=' + this.route;
    }
                        
}

// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.login = function () {

    $('loader').style.display='block';

	var _params = {
		language: this.lang,
        netUsername: $('user').value,
        netPassword: $('passwort').value
	}; 

	var host = 'XSLT_PROFILE_LOGIN_REQUEST';
	var _ajax = mdvLib.ajax({ host: host, parameters: _params, onComplete: mdvProfile.loginOnComplete.bind(this)});

}

// this function insert the content  or reload the page after the login request
MDVProfile.prototype.loginOnComplete = function (response) {
    
    if(response =='on'){
        this.reloadPage()
    }
    else{
        $('login').innerHTML = response
        $('loader').style.display='none';
        //box neue laden mit fehlermeldung
    }
}


// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.logoff = function () {

	 var _params = {
		language: this.lang
	}; 

	var host = 'XSLT_PROFILE_LOGOFF_REQUEST';
	var _ajax = mdvLib.ajax({ host: host, parameters: _params, onComplete: mdvProfile.logoffOnComplete.bind(this)});

}

// this function reload the page content after the logoff request
MDVProfile.prototype.logoffOnComplete = function (response) {
     this.reloadPage()
}

// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.register = function () {
                    
                    
    this.changeRegisterProfile('register')

}

// this function insert the content 
MDVProfile.prototype.registerOnComplete = function (response) {
    
    if(response =='on'){
         this.reloadPage()
    }
    else{
        $('register').innerHTML = response
    }
}


// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.saveRoute = function () {
    
    var routeName = $('nameForPersonalProfile').value;
        routeName = escape(routeName);
        routeName = routeName.replace(/%25/g, '%').replace(/%21/g, '!').replace(/%20/g, ' ');
    

	 var _params = {
		language: this.lang,
        sessionID: this.sessionID,
        requestID: this.requestID,
        command: 'addNamedTrip', 
        nameForPersonalProfile: routeName
	}; 

	var host = 'XSLT_TRIP_REQUEST2?';
	var _ajax = mdvLib.ajax({ host: host, parameters: _params, onComplete: mdvProfile.saveRouteOnComplete.bind(this)});

}

// this function insert the content 
MDVProfile.prototype.saveRouteOnComplete = function (response) {
    
    if(response=='error'){
        $('nameExist').style.display ='block'
    }
    else{
        hideOverlay('overlayRouteSpeichern');
        $('namedTrips').innerHTML = response;
    }
    
}

// this function save the route identifier and display the right overlay 
MDVProfile.prototype.changeRouteOverlay = function (identifier) {
   
    this.identifier = identifier;
    $('nameForPersonalProfileRename').value = identifier;
    hideOverlay('overlayMeineRouten');
    showOverlay('overlayRouteChange');
}

// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.changeRoute = function (identifier) {

	var _params = {
		language: this.lang,
        sessionID: 0,
        requestID: 0,
        command: 'renameNamedTrip', 
        nameForPersonalProfile: this.identifier,
        newName: $('nameForPersonalProfileRename').value
	}; 

	var host = 'XSLT_PROFILE_REQUEST';
	var _ajax = mdvLib.ajax({ host: host, parameters: _params, onComplete: mdvProfile.changeRouteOnComplete.bind(this)});

}

// this function insert the content 
MDVProfile.prototype.changeRouteOnComplete = function (response) {
    
    //this.reloadPage()
    //container meine routen austauschen
    if(response=='error'){
        $('nameExist').style.display ='block'
    }
    else{
        hideOverlay('overlayRouteChange');
        $('namedTrips').innerHTML = response;
        showOverlay('overlayMeineRouten');
    }
    
}


// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.changeProfile = function (identifier) {
    
    this.changeRegisterProfile('change')
    
}

// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.changeRegisterProfile = function (type) {
    
    var expression = /^[_a-zA-Z0-9-](\.{0,1}[_a-zA-Z0-9-])*@([a-zA-Z0-9-]{1,}\.){0,}[a-zA-Z0-9-]{2,}(\.[a-zA-Z]{2,6}){1,2}$/;
    var mail = $('ppPersonEmail').value;
    var error = false;
    
    //check password
    if($('ppPersonNetPassword').value!=$('ppPersonConfirmNetPassword').value){
        $('errorRegister').innerHTML = 'Bitte kontrollieren Sie Ihr Passwort!'
        error = true;
    }
    else if($('ppPersonNetPassword').value.length < 6){
        $('errorRegister').innerHTML = 'Das eingegebene Passwort ist zu kurz!';
        error = true;
    }
    // check email
    else if(!expression.exec(mail)){
        $('errorRegister').innerHTML = 'Die eingegebene E-Mail Adresse ist ungültig!';
         error = true;
    }
    
    if(error == true){
        $('errorRegister').style.display='block'
        return false;
    }

	var _params = {
        command: 'ppPersonSubmit',
        showPpPerson: '1',
		ppPersonFirstName: escape($('ppPersonFirstName').value),
        ppPersonName: escape($('ppPersonName').value),
        ppPersonEmail: escape($('ppPersonEmail').value),
        ppPersonNetUsername: escape($('ppPersonNetUsername').value), 
        ppPersonNetPassword: escape($('ppPersonNetPassword').value),
        ppPersonConfirmNetPassword: escape($('ppPersonConfirmNetPassword').value)
	}; 

    if(type=='change'){
    	var host = 'XSLT_PROFILE_REQUEST';
    	var _ajax = mdvLib.ajax({ host: host, parameters: _params, 
                        onComplete: mdvProfile.changeProfileOnComplete.bind(this)});
    }
    else{
    	var host = 'XSLT_PROFILE_CREATE_REQUEST';
    	var _ajax = mdvLib.ajax({ host: host, parameters: _params, 
                        onComplete: mdvProfile.registerOnComplete.bind(this)});
    }

}



// this function insert the content 
MDVProfile.prototype.changeProfileOnComplete = function (response) {
    
    //this.reloadPage()
    //container meine routen austauschen
    if(response=='error'){
        //$('nameExist').style.display ='block'
    }
    else{
        hideOverlay('overlayChangeProfile')
        //$('namedTrips').innerHTML = response;
        this.reloadPage()
    }
    
}


// this function delete the current profile
MDVProfile.prototype.deleteProfile = function () {

	var _params = {
        language: this.lang
	}; 

	var host = 'XSLT_PROFILE_DELETE_REQUEST';
	var _ajax = mdvLib.ajax({ host: host, parameters: _params, onComplete: mdvProfile.deleteProfileOnComplete.bind(this)});

}

// this function reload the page after delete the profile
MDVProfile.prototype.deleteProfileOnComplete = function (response) {
    
    this.reloadPage()
    
}

// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.deleteRoute = function (identifier) {

    document.body.style.cursor = "wait";
    $('loader-route').style.display='block';
    
    var routeName = identifier;
        routeName = routeName.replace(/%20/g, 'WRONGBLANK')
        routeName = escape(routeName);
        routeName = routeName.replace(/%25/g, '%').replace(/%21/g, '!').replace(/%20/g, ' ');
        routeName = routeName.replace(/WRONGBLANK/g, '%20')

        
	var _params = {
		language: this.lang,
        sessionID: 0,
        requestID: 0,
        command: 'deleteNamedTrip', 
        nameForPersonalProfile: routeName
	}; 

	var host = 'XSLT_PROFILE_REQUEST';
	var _ajax = mdvLib.ajax({ host: host, parameters: _params, onComplete: mdvProfile.deleteRouteOnComplete.bind(this)});

}

// this function insert the content 
MDVProfile.prototype.deleteRouteOnComplete = function (response) {
    
    //container meine routen austauschen
    $('namedTrips').innerHTML = response;
    document.body.style.cursor = "default";
    $('loader-route').style.display='none';

}

// this function check the content of the request -> route request or origin destination handler
MDVProfile.prototype.personData = function () {

	 var _params = {
        requestID: '0',
        sessionID: '0',
		ppPersonBirthYear: $('ppPersonBirthYear').value,
        ppPersonWeight: $('ppPersonWeight').value,
        ppPersonHeight: $('ppPersonHeight').value,
        ppPersonSex: $$('input[name=ppPersonSex]:checked')[0].value,
        command: 'ppPersonSubmit'
	}; 

	var host = 'XSLT_PROFILE_REQUEST';
	var _ajax = mdvLib.ajax({ host: host, parameters: _params, onComplete: mdvProfile.personDataOnComplete.bind(this)});

}

// this function insert the content 
MDVProfile.prototype.personDataOnComplete = function (response) {

   this.reloadPage()
    
}



function saveRouteOverlay(){

    $('nameExist').style.display='none';
    $('nameForPersonalProfile').value='';
    showOverlay('overlayRouteSpeichern');
    $('nameForPersonalProfile').focus(); 
    
}


function myRoutesOverlay(){

    $('personUpdate').style.display='none';
    showOverlay('overlayMeineRouten');
    
}



