﻿
var Registration = {
    companyName: "",
    country: '',
    city: '',
    postCode: '',
    address: '',
    telephone: '',
    fax: '',
    mobile: '',
    email: '',
    website: '',
    companyType: '',
    taxFileNumber: '',
    typeofTransportActivity: '',
    opActivityInfo: '',
    contactPerson: '',
    JobTitle: '',
    languages: '',
    mobPhone: '',
    ext:''
};

var activeStep = 1;

var loaderDiv = "<div id='loader' class='loading' align='center'><b>Loading...</b> </div>";

function Validate() {
    var returnVal = true;
    $("#header input[validate='true']").each(function(val, i) {
        $("#" + i.id + "_rfv").css('color', '#000000');
        if ($("#" + i.id).val() == "") {
            returnVal = false;
            $("#span" + i.id).remove();
            $("#" + i.id + "_rfv").attr('style', 'color:red;font-weight:bold');
            $("#" + i.id).css('border-style', 'solid 1px');
            $("#" + i.id).css('border-color', 'red');
            $("#" + i.id).blur(function() {
                if ($(this).val() != "") {
                    $("#" + i.id).css('border-style', 'solid 1px');
                    $("#" + i.id).css('border-color', 'black');
                    $("#" + i.id + "_rfv").css('color', '#000000');
                }
            });
        }
    });
    if ($("#step11_tbEmail").val() != undefined) {
        returnVal = validateEmail($("#step11_tbEmail").val());
        if (returnVal == false) {
            
        }
    }
    $("#header textarea").each(function(val, i) {
        if ($("#" + i.id).val().length>199) {
            returnVal = false;
            $("#span" + i.id).remove();
            $("#" + i.id).after("<span id='span" + i.id + "' style='color:red;margin-left:2px;font-weight:bold;font-size:10px'>Max Length(200)</span>");
            $("#" + i.id).css('border-style', 'solid 1px');
            $("#" + i.id).css('border-color', 'red');
            $("#" + i.id).blur(function() {
                if ($(this).val() != "") {
                    $("#" + i.id).css('border-style', 'solid 1px');
                    $("#" + i.id).css('border-color', 'black');
                }
            });
        }
    });
    var ddltransport = $("#step21_ddlTypeTransport").val();
    if (activeStep == 2) {
        if (ddltransport != null) {
            $("#header span").css('border-style', 'solid 1px');
            $("#header span").css('border-color', 'black !important');
            $("#step21_ESi18nLabel5").css("color", "black");
        }
        else {
            $("#step21_ESi18nLabel5").css("color", "red");
            $("#header span").css('border-style', 'solid 1px');
            $("#header span").css('border-color', 'red !important');
            returnVal = false;
        }
    }
    var ddlLanStep3 = $("#step31_tbRegLang").val();
    if (activeStep == 3) {
        if (ddlLanStep3 != null) {
            $("#header span").css('border-style', 'solid 1px');
            $("#header span").css('border-color', 'black !important');
            $("#step31_ESi18nLabel3").css("color", "black");
        }
        else {
            $("#step31_ESi18nLabel3").css("color", "red");
            $("#header span").css('border-style', 'solid 1px');
            $("#header span").css('border-color', 'red !important');
            returnVal = false;
        }
    }
    $("#header select[validate='true']").each(function(val, i) {
        $("#" + i.id + "_rfv").css('color', '#000000');
        if ($("#" + i.id).val() == 0) {
            returnVal = false;
            $("#span" + i.id).remove();
            $("#" + i.id + "_rfv").attr('style', 'color:red;font-weight:bold');
            $("#" + i.id).css('border-style', 'solid 1px');
            $("#" + i.id).css('border-color', 'red');
            $("#" + i.id).blur(function() {
                if ($(this).val() != "") {
                    $("#" + i.id).css('border-style', 'solid 1px');
                    $("#" + i.id).css('border-color', 'black');
                    $("#" + i.id + "_rfv").css('color', '#000000');
                }
            });
        }
    });
    return returnVal;
}

function validateEmail(email) {
    var returnVal = true;
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(email) == false) {
        
        $("#step11_tbEmail_rfv").after("<span id='spanstep11_tbEmail' style='color:red;margin-left:2px;font-weight:bold;font-size:10px'>invalid format</span>");
        $("#step11_tbEmail").css('border-style', 'solid 1px');
        $("#step11_tbEmail").css('border-color', 'red');
        returnVal = false;
    }
    else {//validate against database
        $("#spanstep11_tbEmail").remove();
        $.ajax
        ({
            url: "webservices/Company.asmx/ValidateEmail",
            data: '{"email":"' + email + '"}',
            dataType: "json",
            async:false,
            contentType: "application/json; charset=utf-8",
            type: "POST",
            complete: function(json1) {
                var c = JSON.parse(json1.responseText);
                var d = JSON.parse(c.d);
                if (d.status == "1") {
                    returnVal = false;
                    $("#spanstep11_tbEmail").remove();
                    $("#step11_tbEmail_rfv").after("<span id='spanstep11_tbEmail' style='color:red;margin-left:2px;font-weight:bold;font-size:10px'>Email Exists</span>");
                    $("#step11_tbEmail").css('border-style', 'solid 1px');
                    $("#step11_tbEmail").css('border-color', 'red');
                }
            }
        });
    }
    return returnVal;
}

function modalShow(dialog)//function for seting size on the modal
{
    dialog.container.css({
        width: 737
    });
    LoadPage(4); //load terms
}
function modalClose(dialog) {
    
}
function modalOpen(dialog) {
    
    dialog.overlay.fadeIn('fast', function() {
        dialog.container.fadeIn('fast', function() {
            dialog.data.hide().slideDown('fast');
            
        });
    });
}
//alert(Registration.handle(3, 5));
$(document).ready(function () {
    $("#signIn").click(function () {
        $('#formDiv').modal({
            onShow: modalShow,
            position: ["25%", "25%"]
        });
    });
    $("#_reg_free").click(function() {
        $('#formDiv').modal({
            onShow: modalShow,
            position: ["25%", "25%"]
        });
    });
});

function closeModal() {
    $.modal.close();
    window.location = document.URL;
}
function gotoStep2() {
    LoadPage(2);
    activeStep = 2;
}

function step4toStep5() {
    alert("Accepted");
    activeStep = 5;
}

function step1toStep2() {
    if (Validate()) {
        Registration.companyName = $("#step11_tbCompName").val();
        Registration.country = $("#step11_ddlCountry").val();
        Registration.city = $("#step11_tbCity").val();
        Registration.postCode = $("#step11_tbPostCode").val();
        Registration.address = $("#step11_tbAddress").val();
        Registration.telephone = $("#step11_tbTelephone").val();
        Registration.ext = $("#step11_tbExt").val();
        Registration.fax = $("#step11_tbFax").val();
        Registration.mobile = $("#step11_tbMobile").val();
        Registration.email = $("#step11_tbEmail").val();
        Registration.website = $("#step11_tbWebsite").val();
        LoadPage(2);
        activeStep = 2;
    }
}
function step2toStep3() {
    if (Validate()) {
        Registration.companyType = $("#step21_ddlCompanyType").val();
        Registration.taxFileNumber = $("#step21_tbTaxNumber").val();
        //da se stavi za operating licence
        Registration.typeofTransportActivity = $("#step21_ddlTypeTransport").val();
        Registration.opActivityInfo = $("#step21_tbOpActivityInfo").val();
        LoadPage(3);
        activeStep = 3;
    }
}

function step2toStep1() {
    LoadPage(1);
    activeStep = 1;
}

function step3toStep4() {
    if (Validate()) {
        Registration.contactPerson = $("#step31_tbContactPerson").val();
        Registration.JobTitle = $("#step31_tbJobTitle").val();
        Registration.languages = $("#step31_tbRegLang").val();
        Registration.mobPhone = $("#step31_tbMobile").val();
        SaveCompany();
        activeStep = 4;
    }
}

function SaveCompany() {
    $.ajax
    ({
        url: "webservices/Company.asmx/SaveSignups",
        data: '{values: ' + $.toJSON(Registration) + '}',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        type: "POST",
        complete: function(json1) {
            var c = JSON.parse(json1.responseText);
            var d = JSON.parse(c.d);

            $("#formDiv").html('');
            $("#formDiv").html(loaderDiv);
            $("#formDiv").load("registration/result.aspx", {}, function() {
                if (status == 'error') {
                    alert('there was an error in file loading');
                }
                $("#loader").remove();
                setTimeout("$('#formDiv').show('slow'); ", 2000);
                $("#result1_lblShortText").html(d.msg);
                //setTimeout("closeModal(); ", 5000); 
            });
        }
    });
}
function step4toStep3() {
    LoadPage(3);
    activeStep = 3;
}


function LoadPage(step) {
    switch (step) {
        case 1:
            $("#formDiv").html('');
            $("#formDiv").html(loaderDiv);
            $("#formDiv").load("registration/step1.aspx", {}, function() {
                if (status == 'error') {
                    alert('there was an error in file loading');
                }
                $("#loader").remove();
                setTimeout("$('#formDiv').show('slow'); ", 2000);
                $("#step11_tbCompName").val(Registration.companyName);
                $("#step11_ddlCountry").val(Registration.country);
                $("#step11_tbCity").val(Registration.city);
                $("#step11_tbPostCode").val(Registration.postCode);
                $("#step11_tbAddress").val(Registration.address);
                $("#step11_tbTelephone").val(Registration.telephone);
                $("#step11_tbExt").val(Registration.ext);
                $("#step11_tbFax").val(Registration.fax);
                $("#step11_tbMobile").val(Registration.mobile);
                $("#step11_tbEmail").val(Registration.email);
                $("#step11_tbWebsite").val(Registration.website);
            });
            break;
        case 2:
            $("#formDiv").html('');
            $("#formDiv").html(loaderDiv);
            $("#formDiv").load("registration/step2.aspx", {}, function() {
                if (status == 'error') {
                    alert('there was an error in file loading');
                }
                $("#loader").remove();
                setTimeout("$('#formDiv').show('slow'); ", 2000);
                $("#step21_ddlCompanyType").val(Registration.companyType);
                $("#step21_tbTaxNumber").val(Registration.taxFileNumber);
                //da se stavi za operating licence
                $("#step21_ddlTypeTransport").val(Registration.typeofTransportActivity);
                $("#step21_tbOpActivityInfo").val(Registration.opActivityInfo);
                $("#step21_ddlTypeTransport").dropdownchecklist({ width: 195,initialMultiple:false });
            });
            break;
        case 3:
            $("#formDiv").html('');
            $("#formDiv").html(loaderDiv);
            $("#formDiv").load("registration/step3.aspx", {}, function() {
                if (status == 'error') {
                    alert('there was an error in file loading');
                }
                $("#loader").remove();
                setTimeout("$('#formDiv').show('slow'); ", 2000);
                //$('#loader').fadeOut(1800);
                $("#step31_tbContactPerson").val(Registration.contactPerson);
                $("#Registration.opActivityInfo").val(Registration.JobTitle);
                $("#step31_tbRegLang").val(Registration.languages);
                $("#step31_tbMobile").val(Registration.mobPhone);
                $("#step31_tbRegLang").dropdownchecklist({ width: 195, initialMultiple: false });
            });
            break;
        case 4:
            $("#formDiv").html('');
            $("#formDiv").html(loaderDiv);
            $("#formDiv").load("registration/terms.aspx", {}, function() {
                if (status == 'error') {
                    alert('there was an error in file loading');
                }
                $("#loader").remove();
                //$('#loader').fadeOut(1800);
            });
            break;
        default: break;
    }
}

function ShowMessage(msg) {
    $('#message_info').modal({
    onShow: _modalShow,
        onOpen:_modalOpen,
        position: ["35%", "35%"]
    });
    $("#_msg").text(msg);
}

function _modalShow(dialog)//function for seting size on the modal
{
    dialog.container.css({
        width: 537,
        height:100
    });
}
function _modalClose(dialog) {

}
function _modalOpen(dialog) {

    dialog.overlay.fadeIn('fast', function() {
        dialog.container.fadeIn('fast', function() {
            dialog.data.hide().slideDown('fast');

        });
    });
}
