<!-- RCSid: $Header: /opt1/utm/dump101119/src/app/logistik/war/src/main/webapp/online/bookPickUp/sv/js/online.js,v 1.1.6.8 2010-02-22 16:28:06 pelj707 Exp $ -->
//Function for opening the help-page.
function openHelpWin(url, a, title){
    var winWidth, winHeight, winName;
    winWidth = 400;
    winHeight = 450;
    winName = "onlineHelpWin";
    var complUrl;
    complUrl = url;
    complUrl += "?action=" + a;
    openWin(pseWin.SCROLLRESIZE,complUrl,winWidth,winHeight,winName,10,10);
}

function emptyContent(obj, defaultValue) {
    if (obj.value == defaultValue) {
        obj.value = "";
    }
}

//Use when submiting page
function setActionAndSubmit(theForm, action){

    theForm.action.value = action;
    theForm.submit();
}

//check if form exists
function isValidFormObj(objForm){
    if(!objForm){
        alert("Error: Form does not exist.");
        return false;
    }
    return true;
}
//check if action-field exists
function isValidFieldObj(objField){
    if(!objField){
        alert("Error: Field does not exist.");
        return false;
    }
    return true;
}



function showIt (arg) {
    document.getElementById(arg).style.visibility = "visible";
}

function hideIt (arg) {
    document.getElementById(arg).style.visibility = "hidden";
}

// Prefill the fields for contactperson with information from the select box
function fillContact(form) {
    var iSelectedIndex = form.contactPerson.selectedIndex;

    if (form.contactPerson.options[iSelectedIndex].value == "0") {
        form.contactName.value = "";
        form.contactPhone.value = "";

        unsetReadOnly(form);
    } else {
        form.contactName.value = contactNames[iSelectedIndex];
        form.contactPhone.value = contactPhones[iSelectedIndex];

        setReadOnly(form);
    }
}

//Prefill the fields for pickup address with information from the select box
function fillCollectionaddress(form) {
    var iSelectedIndex = form.pickUpAddress.selectedIndex;
    // Om ny adress är vald så töm fälten
    if (form.pickUpAddress.options[iSelectedIndex].value == "0") {
        form.pickUpPlaceName.value = "";
        form.pickUpStreet.value = "";
        form.pickUpZipcode.value = "";
        form.pickUpCity.value = "";

        unsetReadOnly(form);
    } else {
        var fm = new Formatter();

        if (pickUpPlaceNames[iSelectedIndex].length > 1 ) {
            form.pickUpPlaceName.value = pickUpPlaceNames[iSelectedIndex];
        } else {
            form.pickUpPlaceName.value = "";
        }
        form.pickUpStreet.value = pickUpStreets[iSelectedIndex];
        form.pickUpZipcode.value = fm.formatZipCode(pickUpZipcodes[iSelectedIndex]);
        form.pickUpCity.value = collectionAddressCities[iSelectedIndex];

        setReadOnly(form);
    }
}

/*  Selection of type of booking */
function bookPickUp (theForm) {

    var va = new Validator();
    var er = new ErrorRenderer();

    if (!va.isRadioSelected(theForm.shipmenttype)) {
        var err = new Error(theForm.shipmenttype,'Här måste du välja vad du vill boka hämtning av');
        va.addError(err);
    } else {
        theForm.action.value = va.getRadioValue();
    }

    return er.displayErrors(theForm,va.getErrors());
}

/*  Pick up-booking for Pallet and Parcel */
function bookPalletParcel (theForm) {
    var va = new Validator();
    var er = new ErrorRenderer();
    var fm = new Formatter();

    fm.trimAllFields(theForm);

    if (va.checkPhone(theForm.contactPhone.value)) {
        theForm.contactPhone.value = fm.formatPhoneNumber(theForm.contactPhone.value);
    }

    return true;
}

/*  Pick up-booking for Express */
function bookExpress (theForm) {
    var fm = new Formatter();
    var va = new Validator();

    fm.trimAllFields(theForm);
    if (va.checkZipcode(theForm.senderPostalNo.value)) {
        theForm.senderPostalNo.value = fm.formatZipCode(theForm.senderPostalNo.value);
    }

    if (va.checkZipcode(theForm.receiverPostalNo.value)) {
        theForm.receiverPostalNo.value = fm.formatZipCode(theForm.receiverPostalNo.value);
    }
    if (va.checkPhone(theForm.senderPhone.value)) {
        theForm.senderPhone.value = fm.formatPhoneNumber(theForm.senderPhone.value);
    }
    if (va.checkPhone(theForm.receiverPhone.value)) {
        theForm.receiverPhone.value = fm.formatPhoneNumber(theForm.receiverPhone.value);
    }

    return true;
}

/* Remove pickup-address */
function removeAddressAction (theForm,action) {
    var test = confirm("Vill du verkligen ta bort adressen ?");
    if (test) {
        theForm.pickUpAddressId.value = theForm.pickUpAddress.options[theForm.pickUpAddress.selectedIndex].value;
        setActionAndSubmit(theForm,action);
    }
}

/* Remove contact person */
function removeContactPersonAction (theForm,action) {
    var test = confirm("Vill du verkligen ta bort kontaktpersonen ?");
    if (test) {
        theForm.contactPersonId.value = theForm.contactPerson.options[theForm.contactPerson.selectedIndex].value;
        setActionAndSubmit(theForm,action);
    }
}

function setReadOnly(theForm) {
    if (theForm.contactPerson.options[theForm.contactPerson.selectedIndex].value != "0") {
        showIt("removecontactperson");

        theForm.contactName.className = theForm.contactName.className + " readonly";
        theForm.contactPhone.className = theForm.contactPhone.className + " readonly";

        theForm.contactName.className = theForm.contactName.className.replace(/\s?readonly/i,"") + " readonly";
        theForm.contactPhone.className = theForm.contactPhone.className.replace(/\s?readonly/i,"") + " readonly";

        theForm.contactName.readOnly = "readonly";
        theForm.contactPhone.readOnly = "readonly";
    }
    if (theForm.pickUpAddress.options[theForm.pickUpAddress.selectedIndex].value != "0") {
        showIt("removeaddress");
        if (theForm.pickUpPlaceName.value != '') {
            theForm.pickUpPlaceName.className = theForm.pickUpPlaceName.className.replace(/\s?readonly/i,"") + " readonly";
        } else {
            theForm.pickUpPlaceName.className = theForm.pickUpPlaceName.className.replace(/\s?readonly/i,"");
        }
        theForm.pickUpStreet.className = theForm.pickUpStreet.className.replace(/\s?readonly/i,"") + " readonly";
        theForm.pickUpZipcode.className = theForm.pickUpZipcode.className.replace(/\s?readonly/i,"") + " readonly";
        theForm.pickUpCity.className = theForm.pickUpCity.className.replace(/\s?readonly/i,"") + " readonly";
    }
}

function unsetReadOnly(theForm) {
    if (theForm.contactPerson.options[theForm.contactPerson.selectedIndex].value == "0") {
        hideIt("removecontactperson");
        theForm.contactName.readOnly = "";
        theForm.contactPhone.readOnly = "";

        theForm.contactName.className = theForm.contactName.className.replace(/\s?readonly/gi,"");
        theForm.contactPhone.className = theForm.contactPhone.className.replace(/\s?readonly/gi,"");
    }
    if (theForm.pickUpAddress.options[theForm.pickUpAddress.selectedIndex].value == "0") {
        hideIt("removeaddress");

        theForm.pickUpPlaceName.readOnly = "";
        theForm.pickUpStreet.readOnly = "";
        theForm.pickUpCity.readOnly = "";
        theForm.pickUpZipcode.readOnly = "";

        theForm.pickUpPlaceName.className = theForm.pickUpPlaceName.className.replace(/\s?readonly/gi,"");
        theForm.pickUpStreet.className = theForm.pickUpStreet.className.replace(/\s?readonly/gi,"");
        theForm.pickUpZipcode.className = theForm.pickUpZipcode.className.replace(/\s?readonly/gi,"");
        theForm.pickUpCity.className = theForm.pickUpCity.className.replace(/\s?readonly/gi,"");
    }
}

function bookDelivery(theForm) {

    var fm = new Formatter();
    var va = new Validator();

    fm.trimAllFields(theForm);

    if (va.checkPhone(theForm.senderPhone.value)) {
        theForm.senderPhone.value = fm.formatPhoneNumber(theForm.senderPhone.value);
    }

    if (va.checkZipcode(theForm.senderPostalNo.value)) {
        theForm.senderPostalNo.value = fm.formatZipCode(theForm.senderPostalNo.value);
    }

    if (va.checkZipcode(theForm.receiverPostalNo.value)) {
        theForm.receiverPostalNo.value = fm.formatZipCode(theForm.receiverPostalNo.value);
    }

    if (va.isListSelected(theForm.pickupHour)) {
       selectedValue = va.listValue;
        if (selectedValue == "19"){
            theForm.pickupHighHour.value = "true";
        }
    }
    if (va.isListSelected(theForm.deliveryHour)) {
       selectedValue = va.listValue;
        if (selectedValue == "19"){
            theForm.deliveryHighHour.value = "true";
        }
    }
}

function handleDeliveryDate(theForm) {

    var va = new Validator();

    if (va.isRadioSelected(theForm.deliveryDate)) {
        var rv = va.radioValue;

        if (rv == "true") {
           theForm.deliveryAnotherDay.disabled = true;
           theForm.deliveryAnotherDay.value = "";
        } else if (rv == "false") {
            theForm.deliveryAnotherDay.disabled = false;
        }
    }
}

function handlePickupDate(theForm) {

    var va = new Validator();

    if (va.isRadioSelected(theForm.pickupDate)) {
        var rv = va.radioValue;

        if (rv == "true") {
           theForm.pickupAnotherday.disabled = true;
           theForm.pickupAnotherday.value = "";
        } else if (rv == "false") {
            theForm.pickupAnotherday.disabled = false;
        }
    }
}

