

$(document).ready(function() {





    // smart info
    $(":input").focus(function() {
        var msg = $(".input-information span.name." + $(this).attr("name")).parent().eq(0);

        if ($(msg).next().css('display') == 'none') {
            $(msg).fadeIn("fast");
        }

        $(".input-information").not(msg).not(".input-information-error").hide();

    });

    $(":input").blur(function() {
        $(".input-information span.name." + $(this).attr("name")).parent().not(".input-information-error").hide();
    });


    // watermarking
    $(":text").each(function(i, item) {

        if ($(item).val().length == 0) {

            $(item).val($(item).attr("title")).addClass("watermarked");

            $(item).focus(function() {
                if ($(this).is(".watermarked")) {
                    $(this).removeClass("watermarked").val("");
                }
            });

            $(item).blur(function() {
                if (!$(this).is(".watermarked") && $(this).val().length == 0) {
                    $(this).addClass("watermarked").val($(this).attr("title"));
                }
            });
        }
    });

    /*handle it if javascript is unavailable*/
    $('#submitbtn1').show();
    $('#submitbtn2').hide();
    $('#bookingform').removeAttr('action');
    $('#action').val('send');

    /*toggle dangerclass on pastback*/
    if ($('#dangerousgoods').is(':checked'))
        $('#dangerclass').parent().parent().show();
    else
        $('#dangerclass').parent().parent().hide();


    /*toggle dangerclass*/
    $('#dangerousgoods').click(function() {
        if ($(this).is(':checked')) {
            $('#dangerclass').parent().parent().show();
        }
        else {
            $('#dangerclass').parent().parent().hide();
        }
    });

});
// watermarking end


function submitform() {


    /*var field = new Array("Type", "Booking office", "Shipper", "Shipper address", "Shipper contacts", "Pickup address", "Cargo ready/date", "Port of Loading", "Port of discharge", "Consignee", "Pieces/package", "Weight in kgs" +
    "Volume in cbm", "Inco terms", "Dangerous goods", "Danger-class", "Comments" ,"Name", "Company", "Email", "Telephone");
    */

    var req = new Array("pickupAddress", "shipper", "address", "contacts", "cargoReady", "portLoad", "consignee", "piecesPackage", "kgs", "cbm", "incoterms", "dangerclass", "customer", "email", "company", "telephone");
    var title = new Array("", "", "", "", "", "", "Company name, address, city", "", "", "", "For example: EXW, FOB, CFR", "For example: 3.3 (paint)", "", "", "", "");
    var proceed = new Boolean('true');
    var sf = document.forms.bookingform;

    //request type has an issue in chrome. It appears not to have either option selected, but still submits 1st option!
    $.each(req, function(index, value) {
        if ($('#' + value).parent().parent().is(':visible') && ($('#' + value).val().length == 0 || $('#' + value).hasClass('watermarked') || $('#' + value).val() == $('#' + value).attr("title"))) {
            $('#' + value).parent().next().find('p.' + value).show();
            proceed = false;
        }
        else {
            $('#' + value).parent().next().find('p.' + value).hide();
        }
    });


    if (proceed) {

        var noop = "–";
        var type = $('#type :selected').val() == null ? noop : $('#type :selected').val().length > 0 ? $('#type :selected').val() : noop;
        var country = $('#country :selected').val() == null ? noop : $('#country :selected').val().length > 0 ? $('#country :selected').val() : noop;
        var shipper = $('#shipper').val() == null ? noop : $('#shipper').val().length > 0 ? $('#shipper').val() : noop;
        var address = $('#address').val() == null ? noop : $('#address').val().length > 0 ? $('#address').val() : noop;
        var contacts = $('#contacts').val() == null ? noop : $('#contacts').val().length > 0 ? $('#contacts').val() : noop;
        var pickupAddress = $('#pickupAddress').val() == null ? noop : $('#pickupAddress').val().length > 0 ? $('#pickupAddress').val() : noop;
        var cargoReady = $('#cargoReady').val() == null ? noop : $('#cargoReady').val().length > 0 ? $('#cargoReady').val() : noop;
        var portLoad = $('#portLoad').val() == null ? noop : $('#portLoad').val().length > 0 ? $('#portLoad').val() : noop;
        var portDischarge = $('#portDischarge').val() == null ? noop : $('#portDischarge').val().length > 0 ? $('#portDischarge').val() : noop;
        var consignee = $('#consignee').val() == null ? noop : ($('#consignee').val().length > 0 && $('#consignee').val() != title[6]) ? $('#consignee').val() : noop;
        var piecesPackage = $('#piecesPackage').val() == null ? noop : $('#piecesPackage').val().length > 0 ? $('#piecesPackage').val() : noop;
        var kgs = $('#kgs').val() == null ? noop : $('#kgs').val().length > 0 ? $('#kgs').val() : noop;
        var cbm = $('#cbm').val() == null ? noop : $('#cbm').val().length > 0 ? $('#cbm').val() : noop;
        var incoterms = $('#incoterms').val() == null ? noop : ($('#incoterms').val().length > 0 && $('#incoterms').val() != title[10]) ? $('#incoterms').val() : noop;
        var dangerousgoods = $('#dangerousgoods').attr('checked') ? "YES" : "NO";
        var dangerclass = $('#dangerclass').val() == null ? noop : ($('#dangerclass').val().length > 0 && $('#dangerclass').val() != title[11]) ? $('#dangerclass').val() : noop;
        var comments = $('#comments').val() == null ? noop : $('#comments').val().length > 0 ? $('#comments').val() : noop;
        var customer = $('#customer').val() == null ? noop : $('#customer').val().length > 0 ? $('#customer').val() : noop;
        var company = $('#company').val() == null ? noop : $('#company').val().length > 0 ? $('#company').val() : noop;
        var email = $('#email').val() == null ? noop : $('#email').val().length > 0 ? $('#email').val() : noop;
        var telephone = $('#telephone').val() == null ? noop : $('#telephone').val().length > 0 ? $('#telephone').val() : noop;


        var msg = "Please review the booking request you have made.\n" +
                    "\nType: " + type +
                    "\nBooking office: " + country +
                    "\n" +
                    "\nShipper: " + shipper +
                    "\nShipper address: " + address +
                    "\nShipper contacts: " + contacts +
                    "\n" +
                    "\nPickup address: " + pickupAddress +
                    "\nCargo ready/date: " + cargoReady +
                    "\nPort of Loading: " + portLoad +
                    "\nPort of discharge: " + portDischarge +
                    "\nConsignee: " + consignee +
                    "\n" +
                    "\nPieces/package: " + piecesPackage +
                    "\nWeight in kgs: " + kgs +
                    "\nVolume in cbm: " + cbm +
                    "\n" +
                    "\nInco terms: " + incoterms +
                    "\nDangerous goods: " + dangerousgoods +
                    "\nDanger-class: " + dangerclass +
                    "\n" +
                    "\nComments: " + comments +
                    "\n" +
                    "\nName: " + customer +
                    "\nCompany: " + company +
                    "\nEmail: " + email +
                    "\nTelephone: " + telephone;


        if (confirm(msg)) {
            sf.action = 'sendmail.aspx';
            sf.submit();
            sf.reset();
        }
        else
            proceed = false;
    }
    return proceed;
}

