﻿//A place holder for all methods used in validations of the bidding User Controls
//(see ~/Controls/Bidding/ folder)

//-------------------------------------------------------------------------
//  Helper method, used to convert image array names relative names path
//  to absolute path given the path...
//-------------------------------------------------------------------------
function AddPathToImageArray(path) 
{
    var realImagePathArray = {};
    for (var pathIndex in imagesArray) 
    {
        realImagePathArray[pathIndex] = path + imagesArray[pathIndex];
    }
    return realImagePathArray;
}


//-------------------------------------------------------------------------
//  Controls validations
//-------------------------------------------------------------------------
//Make and offer control validations
function MakeAnOffer(txtTotalPrice, btnAccept, imagesArray)
{
    if (txtTotalPrice.value != "") 
    {

        ToggleImage(imagesArray, btnAccept, true);
    }
    else 
    {
        ToggleImage(imagesArray, btnAccept, false);
    }
}

//Buyer Transaction Terms control validations
function BuyerTransactionTerms(cbAcceptedTerms, txtCourierAccountNumber, txtCourierName, ddlCourier, btnAccept, imagesArray)
{
    var isValidated = true;
    if (!cbAcceptedTerms.checked || txtCourierAccountNumber.value == "")
    {
        //Both mandatory controls do not validate
        isValidated = false ;
    }
    if (ValidateControl(ddlCourier, ["Other"]))
    {
        //Courier selected as Other, check free text
        if (txtCourierName.value != "" && txtCourierAccountNumber.value != "")
        {
            isValidated = (true && isValidated);
        }
        else
        {
            isValidated = false;
        }
    }
    else
    {
        if (txtCourierAccountNumber.value != "")
        {
            isValidated = (true && isValidated);
        }
    }

    //Change image state
    ToggleImage(imagesArray, btnAccept, isValidated);
}

//Respond To Offer Control validations
function RespondToOffer(imageButtonOption1, imageButtonOption2, radioButton)
{
    //ASP radion is built from table structure, hence the long child dig
    if (radioButton.rows[0].cells[0].childNodes[0].checked)
    {
        imageButtonOption1.style.display = 'inline';
        imageButtonOption2.style.display = 'none';
    }
    else
    {
        imageButtonOption1.style.display = 'none';
        imageButtonOption2.style.display = 'inline';                
    }
}

//Buyer transaction Control validations
function ValidateTransactionControls(buttonToChange, imagesArray, controlsToValidateArray)
{
    if (ValidateNonEmptyTextBoxes(controlsToValidateArray))
    {
        ToggleImage(imagesArray, buttonToChange, true);
    }
    else
    {
        ToggleImage(imagesArray, buttonToChange, false);
    }
}

// Seller transaction Control validations
//dateTimeErrorLabel is used to validate past dates, past dates on pickup date are not allowed 
// As Range validators cause all sort of Javascript issues, this solution is chosen
function ValidateSellerTransactionControls(buttonToChange, imagesArray, controlsToValidateArray, dateTimeTextbox, dateTimeErrorLabel)
{
    //Convert the textbox string date to date format
    var dateStringArray = dateTimeTextbox.value.split('/');
    var selectedDate = new Date(dateStringArray[2], dateStringArray[1] - 1, dateStringArray[0]);
    //Get the current date
    var date = new Date();
    date.setDate(date.getDate());

    //Validator
    if (date >= selectedDate)
    {
        dateTimeErrorLabel.style.display = "inline";
    }
    else
    {
        dateTimeErrorLabel.style.display = "none";
    }


    //Validate both past date and non empty data in the rest of the controls
    if (date < selectedDate && ValidateNonEmptyTextBoxes(controlsToValidateArray))
    {
        ToggleImage(imagesArray, buttonToChange, true);
    }
    else
    {
        ToggleImage(imagesArray, buttonToChange, false);
    }
}


