$(document).ready(function () {
		//profileConfirmation();
        addDriverForm();
		//profileConfirmation();
		//mediaForm(7);
		//addDriverDocumentForm();
		//addVehicleForm();
		//addVehicleDocumentForm(11);
            
});

(function () {
	
	var tabRegistration = "#tabRegistration";
	
	addDriverForm = function (id) {
        driverForm(id);
    };

    driverForm = function (id) {
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'form', [id]), '', function (t) {
            $(tabRegistration).html(t);
        });
    };

    editDriverFormNew = function (driverId) {
        editDriverForm(driverId);
    }
	
    editDriverForm = function (driverId) {
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'form', [driverId]), '', function (t) {
            $(tabRegistration).html(t);
        });
    };

	
    setupDriver = function (frm) {
		if (!$(frm).validate())
            return;
		fcom.ajax(fcom.makeUrl('DriverRegistration', 'setupDriver'), fcom.frmData(frm), function(t) {
            var ans = JSON.parse(t);
			if(ans.status == 1){
				if (ans.openMediaForm) {
			   	 	mediaForm(ans.driverId);
               	 	return;
            	}
		        if (ans.driverId > 0) {
                	addDriverDocumentForm(ans.driverId);
                	return false;
            	}
			}else{
				//alert(ans.msg);
				$.mbsmessage(ans.msg, true, 'alert alert--danger');
			}
		});
		return false;
	};


    mediaForm = function (driverId) {
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'mediaForm', [driverId]), '', function (res) {
            $(tabRegistration).html(res);
        });
    };
	
	setupMedia = function () {
        addDriverDocumentForm();
    };

    removeImg = function (driverId) {
        if (!confirm(langLbl.confirmDeleteImage)) {
            return;
        }
        fcom.updateWithAjax(fcom.makeUrl('DriverRegistration', 'removeImage', [driverId]), '', function (t) {
            mediaForm(driverId);
        });
    };


    addDriverDocumentForm = function () {
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'driverDocumentsForm'), '', function (t) { 
            $(tabRegistration).html(t);
        });

    };

    setupDriverDocuments = function (frm) {
        if (!$(frm).validate())
            return;
        var data = fcom.frmData(frm);
		
		fcom.ajax(fcom.makeUrl('DriverRegistration', 'setupDriverDocuments'), fcom.frmData(frm), function(t) {
			var ans = JSON.parse(t);
			
			if(ans.status == 1){
				addVehicleForm();
			}else{
				$.mbsmessage(ans.msg, true, 'alert alert--danger');
			}
		});
		
        /*fcom.updateWithAjax(fcom.makeUrl('DriverRegistration', 'setupDriverDocuments'), data, function (t) {
            addVehicleForm();
        });*/
    };
	
	addVehicleForm = function (id) {
		//alert(fcom.makeUrl('DriverRegistration', 'vehicleForm', [id]));
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'vehicleForm', [id]), '', function (t) { 
            $(tabRegistration).html(t);
        });

    };
	
	setupVehicle = function (frm) {
		if (!$(frm).validate())
            return;
			fcom.ajax(fcom.makeUrl('DriverRegistration', 'setupVehicle'), fcom.frmData(frm), function(t) {
				var ans = JSON.parse(t);
				if(ans.status == 1){
					if (ans.vehicleId > 0) {
						addVehicleDocumentForm(ans.vehicleId);
                		return false;
            		}
			}else{
				$.mbsmessage(ans.msg, true, 'alert alert--danger');
			}
		});
		return false;
	
	}
	
	
	
	addVehicleDocumentForm = function (id) {
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'vehicleDocumentsForm', [id]), '', function (t) { 
            $(tabRegistration).html(t);
        });
    };

    setupVehicleDocuments = function (frm) {
        if (!$(frm).validate())
            return;
        var data = fcom.frmData(frm);
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'setupVehicleDocuments'), data, function (t) {
			profileConfirmation(t.userId);
        });
    };
	
	profileConfirmation = function (id) {
        fcom.ajax(fcom.makeUrl('DriverRegistration', 'profileConfirmation', [id]), '', function (t) { 
            $(tabRegistration).html(t);
        });
    };


})();

$(document).on('click', '.fileType-Js', function () {
    var node = this;
    $('#form-upload').remove();
    var driverId = document.frmDocuments.driverId.value;
    var documentId = $(node).attr('data-field_id');
    var frm = '<form enctype="multipart/form-data" id="form-upload" style="position:absolute; top:-100px;" >';
    frm = frm.concat('<input type="file" name="file" />');
    frm = frm.concat('<input type="hidden" name="document_id" value="' + documentId + '"></form>');
    $('body').prepend(frm);
    $('#form-upload input[name=\'file\']').trigger('click');
    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }
    timer = setInterval(function () {
        if ($('#form-upload input[name=\'file\']').val() != '') {
            clearInterval(timer);
            $val = $(node).val();
            $.ajax({
                url: fcom.makeUrl('DriverRegistration', 'uploadDriverDocuments', [driverId]),
                type: 'post',
                dataType: 'json',
                data: new FormData($('#form-upload')[0]),
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function () {
                    $(node).val('Loading..');
					$.mbsmessage('Loading..', true, 'alert alert--process');
                },
                complete: function () {
                    $(node).val($val);
                },
                success: function (ans) {
					
                    if (!ans.status) {
						$.mbsmessage(ans.msg, true, 'alert alert--danger');
                        //fcom.displayErrorMessage(ans.msg);
                        return;
                    }
					$.mbsmessage(ans.msg, true, 'alert alert--success');
                    //fcom.displaySuccessMessage(ans.msg);
                    $('.text-danger').remove();
                    $('#input-formfield' + documentId).html(ans.msg);
                    $('#formfield_' + documentId).val(ans.file);
                    if (ans.status == true) {
                        $('#input-formfield' + documentId).removeClass('text-danger');
                        $('#input-formfield' + documentId).addClass('text-success');
                    } else {
                        $('#input-formfield' + documentId).removeClass('text-success');
                        $('#input-formfield' + documentId).addClass('text-danger');
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }
    }, 500);
});


$(document).on('click', '.fileType-vehicle-doc-Js', function () {
    var node = this;
    $('#form-upload').remove();
    var vehicleId = document.frmDocuments.vehicleId.value;
    var documentId = $(node).attr('data-field_id');
    var frm = '<form enctype="multipart/form-data" id="form-upload" style="position:absolute; top:-100px;" >';
    frm = frm.concat('<input type="file" name="file" />');
    frm = frm.concat('<input type="hidden" name="document_id" value="' + documentId + '"></form>');
    $('body').prepend(frm);
    $('#form-upload input[name=\'file\']').trigger('click');
    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }
    timer = setInterval(function () {
        if ($('#form-upload input[name=\'file\']').val() != '') {
            clearInterval(timer);
            $val = $(node).val();
            $.ajax({
                url: fcom.makeUrl('DriverRegistration', 'uploadVehicleDocuments', [vehicleId]),
                type: 'post',
                dataType: 'json',
                data: new FormData($('#form-upload')[0]),
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function () {
                    $(node).val('Loading..');
					$.mbsmessage('Loading..', true, 'alert alert--process');
                },
                complete: function () {
                    $(node).val($val);
                },
                success: function (ans) {

                    if (!ans.status) {
                        //fcom.displayErrorMessage(ans.msg);
						$.mbsmessage(ans.msg, true, 'alert alert--danger');
                        return;
                    }
					$.mbsmessage(ans.msg, true, 'alert alert--success');
                    $('.text-danger').remove();
                    $('#input-formfield' + documentId).html(ans.msg);
                    $('#formfield_' + documentId).val(ans.file);
                    if (ans.status == true) {
                        $('#input-formfield' + documentId).removeClass('text-danger');
                        $('#input-formfield' + documentId).addClass('text-success');
                    } else {
                        $('#input-formfield' + documentId).removeClass('text-success');
                        $('#input-formfield' + documentId).addClass('text-danger');
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }
    }, 500);
});

$(document).on('click', '.File-Js-Profile', function () {
    var node = this;
    $('#form-upload').remove();
    var driver_id = $(node).attr('data-driver_id');
    var frm = '<form enctype="multipart/form-data" id="form-upload" style="position:absolute; top:-100px;" >';
    frm = frm.concat('<input type="file" name="file" />');
    $('body').prepend(frm);
    $('#form-upload input[name=\'file\']').trigger('click');
    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }
    timer = setInterval(function () {
        if ($('#form-upload input[name=\'file\']').val() != '') {
            clearInterval(timer);
            $val = $(node).val();
            $.ajax({
                url: fcom.makeUrl('DriverRegistration', 'setUpImage', [driver_id]),
                type: 'post',
                dataType: 'json',
                data: new FormData($('#form-upload')[0]),
                cache: false,
                contentType: false,
                processData: false,
                beforeSend: function () {
					$.mbsmessage('Loading..', true, 'alert alert--process');
                },
                complete: function () {
                    $(node).val($val);
                },
                success: function (ans) {
                    if (ans.status == 1) {
                        //fcom.displaySuccessMessage(ans.msg);
                        mediaForm(ans.driver_id);
                    } else {
						$.mbsmessage(ans.msg, true, 'alert--danger alert');
                    }
                    //reloadList();

                    /*
                     alert(ans);	
                     mediaForm(ans.driver_id);		
                     reloadList();						
                     */},
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }
    }, 500);
});