﻿var currentModalPrefix = '';
function getingformular() {
    currentModalPrefix = $('.getingformular').attr('lang');
    $.fn.colorbox({
        scrolling: false,
        transition: "none",
        width: "700px",
        overlayClose: true,
        inline: true,
        href: "#" + currentModalPrefix,
        onClosed: cleanUpForm
    });
    return false;
}

function kontaktformular(officeId) {
    currentModalPrefix = $('.kontaktformular').attr('lang');

    if (officeId != null) {
        $('#' + currentModalPrefix + ' input[name=officeId]').val(officeId);
    }
    else {
        $('#' + currentModalPrefix + ' input[name=officeId]').val('');
    }
    
    $.fn.colorbox({
        scrolling: false,
        transition: "none",
        width: "700px",
        overlayClose: true,
        inline: true,
        href: "#" + currentModalPrefix,
        onClosed: cleanUpForm
    });
    return false;
}


function openEducationForm(targetId) {
    currentModalPrefix = targetId;
    $.fn.colorbox({
        scrolling: false,
        transition: "none",
        width: "700px",
        overlayClose: true,
        inline: true,
        href: "#" + currentModalPrefix,
        onClosed: cleanUpForm
    });
}

function cleanUpForm() {
    if (currentModalPrefix != null && currentModalPrefix != "") {
        $('#' + currentModalPrefix + ' .formErrors').html('');
        $('#' + currentModalPrefix + ' input[type=text]').val('');
        $('#' + currentModalPrefix + ' textarea').val('');
    }
    $('.inlineform input[type=text]').val('');
    $('.inlineform textarea').val('');
}

function showMessageModal(h2, p) {
    $('.generalInfoModal h2').html(h2);
    $('.generalInfoModal p').html(p);
    $.fn.colorbox({
        scrolling: false,
        transition: "none",
        width: "500px",
        overlayClose: false,
        inline: true,
        href: ".generalInfoModal"
    });
}


$('.buttonOpenForm').live('click', function() {
    currentModalPrefix = $(this).attr('lang');
    $.fn.colorbox({
        scrolling: false,
        transition: "none",
        width: "700px",
        overlayClose: true,
        inline: true,
        href: "#" + currentModalPrefix,
        onClosed: cleanUpForm
    });
});

$('.buttonOpenInlineForm').live('click', function() {
    $('.inlineform').toggleClass('hiddenForm');
    if ($('.inlineform').hasClass('hiddenForm')) {
        $('.inlineformval').val('inlineform hiddenForm');
        cleanUpForm();
    }
    else {
        $('.inlineformval').val('inlineform');
    }
});


$('.ButtonCancel').live('click', function() {
    $.fn.colorbox.close();
    return false;
});
$('.ButtonCancelInline').live('click', function() {
    $('.inlineform').toggleClass('hiddenForm');
    if ($('.inlineform').hasClass('hiddenForm')) {
        $('.inlineformval').val('inlineform hiddenForm');
        cleanUpForm();
    }
    else {
        $('.inlineformval').val('inlineform');
    }
    return false;
});


$('.ButtonSend').live('click', function() {
    if ($(this).hasClass('ContactForm')) {
        sendContactForm();
    }
    else if ($(this).hasClass('EducationForm')) {
        sendEducationForm();
    }
    else if ($(this).hasClass('GetingForm')) {
        sendGetingForm();
    }
    return false;
});

function sendContactForm() {
    var subjectTarget = $('#' + currentModalPrefix + ' select[name=subject]').val();
    var subject = $('#' + currentModalPrefix + ' select[name=subject] :selected').text();
    var firstName = $('#' + currentModalPrefix + ' input[name=firstName]').val();
    var sureName = $('#' + currentModalPrefix + ' input[name=sureName]').val();
    var street = $('#' + currentModalPrefix + ' input[name=street]').val();
    var zip = $('#' + currentModalPrefix + ' input[name=zip]').val();
    var city = $('#' + currentModalPrefix + ' input[name=city]').val();
    var phoneDayTime = $('#' + currentModalPrefix + ' input[name=phoneDayTime]').val();
    var phoneEvening = $('#' + currentModalPrefix + ' input[name=phoneEvening]').val();
    var email = $('#' + currentModalPrefix + ' input[name=email]').val();
    var mobilePhone = $('#' + currentModalPrefix + ' input[name=mobilephone]').val();
    var company = $('#' + currentModalPrefix + ' input[name=company]').val();


    var message = $('#' + currentModalPrefix + ' textarea[name=message]').val();

    var boolAccepts = $('#' + currentModalPrefix + ' input[name=acceptTerms]').is(':checked');
    var officeId = $('#' + currentModalPrefix + ' input[name = officeId]').val();

    if (officeId == "") {
        officeId = 0;
    }

    Anticimex.Jupiter.PublicWeb.CMS.Services.JUPITER.AjaxScriptService.ContactForm(
        subjectTarget, subject, firstName, sureName, street, zip, city, phoneDayTime,
        phoneEvening, email, mobilePhone, company, message, boolAccepts, officeId, ContactForm_onSuccess, ContactForm_onFail);
}

function ContactForm_onSuccess(result) {

    if (result) {
        console.log("svar: %o", result);
        if (result.Success) {
            if (countProperties(result.validationErrors) == 0) {
                $('#' + currentModalPrefix + ' .formErrors').html('');
                showMessageModal('Skickat!', result.Message);
            }
            else {
                var html = '';
                var prop;
                for (prop in result.validationErrors) {
                    html += result.validationErrors[prop] + '<br />';
                }
                $('#' + currentModalPrefix + ' .formErrors').html(html);
                $.fn.colorbox.resize()
            }
        }
        else {
            ContactForm_onFail();
        }
    }
    else {
        ContactForm_onFail();
    }
}
function ContactForm_onFail() {
    //alert('some kind of error');
    //DisplaySystemError();
}



function sendGetingForm() {
    var firstName = $('#' + currentModalPrefix + ' input[name=firstName]').val();
    var sureName = $('#' + currentModalPrefix + ' input[name=sureName]').val();
    var street = $('#' + currentModalPrefix + ' input[name=street]').val();
    var zip = $('#' + currentModalPrefix + ' input[name=zip]').val();
    var city = $('#' + currentModalPrefix + ' input[name=city]').val();
    var phoneDayTime = $('#' + currentModalPrefix + ' input[name=phoneDayTime]').val();
    var phoneEvening = $('#' + currentModalPrefix + ' input[name=phoneEvening]').val();
    var email = $('#' + currentModalPrefix + ' input[name=email]').val();
    var mobilePhone = $('#' + currentModalPrefix + ' input[name=mobilephone]').val();
    var ensuransCompany = $('#' + currentModalPrefix + ' input[name=ensuransCompany]').val();
    var ensuransNumber = $('#' + currentModalPrefix + ' input[name=ensuransNumber]').val();
    var message = $('#' + currentModalPrefix + ' textarea[name=message]').val();
    var wayDescription = $('#' + currentModalPrefix + ' textarea[name=wayDescription]').val();

    
    

    console.log("firstName: %s", firstName);
    console.log("sureName: %s", sureName);
    console.log("street: %s", street);
    console.log("zip: %s", zip);
    console.log("city: %s", city);
    console.log("phoneDayTime: %s", phoneDayTime);
    console.log("phoneEvening: %s", phoneEvening);
    console.log("email: %s", email);
    console.log("mobilePhone: %s", mobilePhone);
    console.log("ensuransCompany: %s", ensuransCompany);
    console.log("ensuransNumber: %s", ensuransNumber);
    console.log("message: %s", message);
    console.log("wayDescription: %s", wayDescription);
    
    
    
    Anticimex.Jupiter.PublicWeb.CMS.Services.JUPITER.AjaxScriptService.GetingForm(
        firstName, sureName, street, zip, city, phoneDayTime,
        phoneEvening, email, mobilePhone, ensuransCompany, ensuransNumber, message, wayDescription, GetingForm_onSuccess, GetingForm_onFail);
}

function GetingForm_onSuccess(result) {

    if (result) {
        console.log("svar: %o", result);
        if (result.Success) {
            if (countProperties(result.validationErrors) == 0) {
                $('#' + currentModalPrefix + ' .formErrors').html('');
                showMessageModal('Skickat', result.Message);
            }
            else {
                var html = '';
                var prop;
                for (prop in result.validationErrors) {
                    html += result.validationErrors[prop] + '<br />';
                }
                $('#' + currentModalPrefix + ' .formErrors').html(html);
                $.fn.colorbox.resize()
            }
        }
        else {
            GetingForm_onFail();
        }
    }
    else {
        GetingForm_onFail();
    }
}
function GetingForm_onFail() {
    //alert('some kind of error');
    //DisplaySystemError();
}






function sendEducationForm() {
    var subject = $('#' + currentModalPrefix + ' h2').html();
    var company = $('#' + currentModalPrefix + ' input[name=company]').val();
    var contactPerson = $('#' + currentModalPrefix + ' input[name=contactPerson]').val();
    var numPeople = $('#' + currentModalPrefix + ' input[name=numPeople]').val();
    var participantNames = $('#' + currentModalPrefix + ' textarea[name=participantNames]').val();
    var AdressStreet = $('#' + currentModalPrefix + ' input[name=AdressStreet]').val();
    var AdressZip = $('#' + currentModalPrefix + ' input[name=AdressZip]').val();
    var AdressCity = $('#' + currentModalPrefix + ' input[name=AdressCity]').val();
    var InvoiceStreet = $('#' + currentModalPrefix + ' input[name=InvoiceStreet]').val();
    var InvoiceZip = $('#' + currentModalPrefix + ' input[name=InvoiceZip]').val();
    var InvoiceCity = $('#' + currentModalPrefix + ' input[name=InvoiceCity]').val();
    var OrganisationNumber = $('#' + currentModalPrefix + ' input[name=OrganisationNumber]').val();
    var phone = $('#' + currentModalPrefix + ' input[name=phone]').val();
    var mobilephone = $('#' + currentModalPrefix + ' input[name=mobilephone]').val();
    var email = $('#' + currentModalPrefix + ' input[name=email]').val();
    var InvomessageceCity = $('#' + currentModalPrefix + ' textarea[name=message]').val();
    var LiteralEducationId = $('#' + currentModalPrefix + ' input.coursePageId').val();

    Anticimex.Jupiter.PublicWeb.CMS.Services.JUPITER.AjaxScriptService.EducationForm(
        subject, company, contactPerson, numPeople, participantNames, AdressStreet, AdressZip, AdressCity,
        InvoiceStreet, InvoiceZip, InvoiceCity, OrganisationNumber,
        phone, mobilephone, email, InvomessageceCity, LiteralEducationId, EducationForm_onSuccess, EducationForm_onFail);
}

function EducationForm_onSuccess(result) {

    if (result) {
        console.log("svar: %o", result);
        if (result.Success) {
            if (countProperties(result.validationErrors) == 0) {
                $('#' + currentModalPrefix + ' .formErrors').html('');
                showMessageModal('Skickat!', result.Message);
            }
            else {
                var html = '';
                var prop;
                for (prop in result.validationErrors) {
                    html += result.validationErrors[prop] + '<br />';
                }
                $('#' + currentModalPrefix + ' .formErrors').html(html);
                $.fn.colorbox.resize()
            }
        }
        else {
            EducationForm_onFail();
        }
    }
    else {
        EducationForm_onFail();
    }
}
function EducationForm_onFail() {
    //alert('some kind of error');
    //DisplaySystemError();
}



function countProperties(obj) {
    var prop;
    var propCount = 0;
    for (prop in obj) {
        propCount++;
    }
    return propCount;
}