﻿//Purpose : Allow control GUI manipulations

//GridView global variable, used to preserve the original row color when changed in a mouse over event
//See GridViewAssist Class ,OnMouseOverRow  Method
var originalGvRowColor;

//--------------------------------------------------------------------------
// Multiplies the data in two controls
// Data must be an inter
// Used to calculate total price when quantity and price per unit are given
//--------------------------------------------------------------------------
function MultiplyControlValues(control1, control2) {
    var num1 = control1.value;
    var num2 = control2.value;

    if (num1 != null && num2 != null && !isNaN(num1) && !isNaN(num2) && num1 != "" && num2 != "") {

        return (num1 * num2).toFixed(2);
    }
    else {
        return "";
    }
}


//------------------------------------------------------------------------
//  Change an image in an image control in accordance to
//  changes in page controls changes 
//  If the validating controls are empty assigns the first image in the array (index 0) & disables the button
//  if v aren't empty assigns the second image in the array (index 1) & enables the button
//-----------------------------------------------------------------------
//imageNamesArray - array containing the path for images
//textboxControsArray - array with all controls to validate Ids
//imageControlId - image control to change
//checkValues - Values to validate in check controls (controlsNameArray)
//isCheckValuesExist - Is check for existing values or non existing
function ToggleImageOnControlsChange(imagesPathArray, imageControl, controlsNameArray)
{
    for (var controlIdIndex in controlsNameArray) 
    {
        //Get control
        var textboxControlObj = document.getElementById(controlsNameArray[controlIdIndex]);

        if (ValidateControl(textboxControlObj, ["", "false"])) 
        {
            ToggleImage(imagesPathArray, imageControl, false);
        }
        else 
        {
            ToggleImage(imagesPathArray, imageControl, true);
        }       
    }
}

//-------------------------------------------------------------------------
//  Toggles control enable and disable state in accordance to a value
//  in another control 
//-------------------------------------------------------------------------
//controlToActivate - The control name to enable/disable
//ControlCheck - The control name which holds the enable values
//validateTextArray - String array values the if apear on the ControlCheck enable the controlToActivate
function ToggleControlStateByControlValue(controlToActivate, checkValueControl, validateTextArray) 
{
    if (ValidateControl(checkValueControl, validateTextArray)) 
    {
        DisableEnableControl(controlToActivate, false);
    }
    else
    {
        DisableEnableControl(controlToActivate, true);
        ClearControl(controlToActivate);
    }      	
}

//------------------------------------------------------------------------
// Changes a control disable property to true
// (the property in ASP.NET is Enabled)
//------------------------------------------------------------------------
//controlObj - The control object to change disable property of
//isDisable - Boolean indicating whther to disable or enable the control
function DisableEnableControl(controlObj, isDisable)
{
    if (controlObj.type == "text") 
    {
        //This is a textbox
        controlObj.disabled = isDisable;
        
    }
    else if (controlObj.type == "checkbox")
    {
        //This is a checkbox
        controlObj.disabled = isDisable;
    }
    else if (controlObj.type == "radion") 
    {
        //This is a checkbox
        controlObj.disabled = isDisable;
    }
    else if (controlObj.type == "select-one") 
    {
        //This is a checkbox
        controlObj.disabled = isDisable;
    }
    else if (controlObj.localName == "div") {
        //This is a div
        controlObj.disabled = isDisable;
    }
}

//------------------------------------------------------------------------
//  Clears a control's visual property
//  ie - clears a textbox value / sets select index to 0 etc.
//------------------------------------------------------------------------
//controlObj - The control object to change value property of
function ClearControl(controlObj) 
{
    if (controlObj.type == "text") 
    {
        //This is a textbox
        controlObj.value = "";
    }
    else if (controlObj.type == "select-one")
    {
        //This is a list
        controlObj.selectedIndex = 0;
    }
    else if (controlObj.type == "radio") 
    {
        //This is a checkbox
        controlObj.selected = false;
    }
    else if (controlObj.type == "checkbox") 
    {
        //This is a checkbox
        controlObj.checked = false;
    }
    else if (controlObj.localName == "div")
    {
        //This is a checkbox
        controlObj.disabled = "";
    }
}

//-------------------------------------------------------------------------
//  Validates a control's data
//-------------------------------------------------------------------------
//controlObj - The control object to validate the value property of
//valuesArray - Values the control validates "against". If one of them matches, returns true
//RETURN VALUE - Boolean whether the control vas validated
//rangeMin - Minimun int value to check
//rangeMax = Max int values to check
function ValidateControlByRange(controlObj, rangeMin, rangeMax) 
{
    if (controlObj.type == "text") 
    {
        //This is a textbox
        if (rangeMax > controlObj.value && rangeMin < controlObj.value)
        {
            return true;
        }
        
    }
    else if (controlObj.type == "select-one") 
    {
        //This is a list
        if (rangeMax > controlObj[controlObj.selectedIndex].value && rangeMin < controlObj[controlObj.selectedIndex].value) 
        {
            return true;
        }        
    }
    return false;
}

//-------------------------------------------------------------------------
//  Validates a control's data
//-------------------------------------------------------------------------
//controlObj - The control object to validate the value property of
//valuesArray - Values the control validates "against". If one of them matches, returns true
//RETURN VALUE - Boolean whether the control vas validated - validation is values are NOT equal
function ValidateControlFalse(controlObj, valuesArray) {
    if (controlObj.type == "text") 
    {
        //This is a textbox
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.value) 
            {
                return false;
            }
        }
    }
    else if (controlObj.type == "select-one") 
    {
        //This is a list
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj[controlObj.selectedIndex].value) 
            {
                return false;
            }
        }
    }
    else if (controlObj.type == "radio") 
    {
        //This is a checkbox
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.selected.toString()) 
            {
                return false;
            }
        }
    }
    else if (controlObj.type == "checkbox") 
    {
        //This is a checkbox
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.checked.toString())
            {
                return false;
            }
        }
    }
    else if (controlObj.localName == "div") 
    {
        //This is a checkbox
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.selected.toString())
            {
                return false;
            }
        }
    }
    return true;
}

//-------------------------------------------------------------------------
//  Validates a control's data
//-------------------------------------------------------------------------
//controlObj - The control object to validate the value property of
//valuesArray - Values the control validates "against". If one of them matches, returns true
//RETURN VALUE - Boolean whether the control vas validated
function ValidateControl(controlObj, valuesArray) 
{
    if (controlObj.type == "text") 
    {
        //This is a textbox
       for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.value) 
            {
                return true;
            }
        }
    }
    else if (controlObj.type == "select-one")
    {
        //This is a list
        for (var valuesArrayIndex in valuesArray)
        {
            if (valuesArray[valuesArrayIndex] == controlObj[controlObj.selectedIndex].value) 
            {
                return true;
            }
        }
    }
    else if (controlObj.type == "radio") 
    {
        //This is a checkbox
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.selected.toString()) 
            {
                return true;
            }
        }
    }
    else if (controlObj.type == "checkbox") 
    {
        //This is a checkbox
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.checked.toString())
            {
                return true;
            }
        }
    }
    else if (controlObj.localName == "div") 
    {
        //This is a checkbox
        for (var valuesArrayIndex in valuesArray) 
        {
            if (valuesArray[valuesArrayIndex] == controlObj.selected.toString())
            {
                return true;
            }
        }
    }
    return false;
}

//-------------------------------------------------------------------------
//  Validates a set of textboxes, checks if they are ALL not empty 
//-------------------------------------------------------------------------
//controlsToValidateArray - An array with all textboxes objects
//valuesArray - Values the control validates "against". If one of them matches, returns true
//RETURN VALUE - Boolean whether the controls were validated
function ValidateNonEmptyTextBoxes(controlsToValidateArray)
{
    var isValidated = true;
    for (var controlsIndex in controlsToValidateArray)
    {
        if (controlsToValidateArray[controlsIndex].value == "")
        {
            isValidated = false;
        }
    }
    return isValidated;
}

//------------------------------------------------------------------------------------------
//  Trigger a button click event automatically
//------------------------------------------------------------------------------------------
function ClickButton(buttonToClick) 
{
    buttonToClick.click();
}

//------------------------------------------------------------------------------------------
//  Toggles the hide attribute of a control
//------------------------------------------------------------------------------------------
function ToggleControlHideAttribute(controlObj) 
{
    if (controlObj.style.visibility != 'hidden') 
    {
        controlObj.style.visibility = 'hidden';
    }
    else 
    {
        controlObj.style.visibility = 'visible';
    }
}

//------------------------------------------------------------------------------------------
//  Toggles control image and operation mode
//------------------------------------------------------------------------------------------
function ToggleImage(imageNamesArray, imageControl, isEnable) 
{
    if (!isEnable) 
    {
        imageControl.src = imageNamesArray[0];
        imageControl.disabled = true;
    }
    else
    {
        imageControl.src = imageNamesArray[1];
        imageControl.disabled = false;            
    }
}

//------------------------------------------------------------------------------------------
//  Searches for a FileUploaderAJAX control id & launchs its click method
//------------------------------------------------------------------------------------------
function BidUploadOnClick(fileUploaderAjaxId)
{
    try
    {
        var frameIndex = GetFrameIndex(fileUploaderAjaxId);
        
        // Get the file uploader input object & launch its click method
        var fileUploader = window.frames[frameIndex].document.iform.file;
        fileUploader.click();
    }
    catch (err)
    {
        //alert(err.message + " " + err.code);
        alert("File uploading has encountred an unexpected error, Please try again.");
    }
}

//------------------------------------------------------------------------------
//  Searches for the framIndex that contains the FileUploaderAJAX control id
//  fileUploaderAjaxId - the id of the FileUploaderAJAX
//------------------------------------------------------------------------------
function GetFrameIndex(fileUploaderAjaxId) 
{
    // Here we search for the framIndex that contains the FileUploaderAJAX control id that we want to launch its click method
    var frameIndex = 0;
    for (var i = 0; i < window.frames.length; i++)
    {
        if (window.frames[i].location.search.search(fileUploaderAjaxId) != -1)
        {
            return frameIndex = i;
        }
    }
    return 0;
}

//--------------------------------------------------------------------------------------
//  Sets the Fileupload filename & calls server side method btnResetFileUploader_Click
//  This method is needed to launch the btnResetFileUploaderClientId click for reseting the fileuploader
//  txtFileNameClientId is a global variable set in buyer/seller Transaction
//--------------------------------------------------------------------------------------
function UpdateResultAndFileName(fileName, btnResetFileUploaderClientId)
{
    document.getElementById(txtFileNameClientId).value = fileName;
    // Runs SellerTransaction/BuyerTransactio validations
    if (RunPageValidation != null)
    {
        RunPageValidation();
    }
    document.getElementById(btnResetFileUploaderClientId).click();
}

//--------------------------------------------------------------------------------------
//  Sets the Fileupload filename & calls server side method btnResetFileUploader_Click
//  This method is needed to launch the btnResetFileUploaderClientId click for reseting the fileuploader
//  txtFileNameClientId is a global variable set in buyer/seller Transaction
//--------------------------------------------------------------------------------------
function ShowFileTooBigErrorMessage(btnResetFileUploaderClientId)
{
    document.getElementById(lblErrorMessageClientId).innerHTML = "* Upload error, 3Mb file limit";
    // Runs SellerTransaction/BuyerTransaction validations
    if (RunPageValidation != null)
    {
        RunPageValidation();
    }
    document.getElementById(btnResetFileUploaderClientId).click();
}

//--------------------------------------------------------------------------------------
//  Disables validations on an input list of control names when a button is clicked
//  used to exit bid windows (without disable the validators prevent the window from
//  postbacking)
//--------------------------------------------------------------------------------------
//validatorsNameArray - String Array of control validators onblur page that need top be disabled
//button - The button to trigger to code behind 
function PassValidators(validatorsNameArray, button)
{
    //Disable all validatore
    for (var controlIndex in validatorsNameArray)
    {
        var validateObj = document.getElementById(validatorsNameArray[controlIndex]);
        ValidatorEnable(validateObj, false);
    }
    //reinitiate the button click
    document.getElementById(button).click;
}

//--------------------------------------------------------------------------------------
//  Validates AcceptedTerms checkbox in BuyerTransactionTerms
//  sender - the custom validator object
//  args - used to set the IsValid param
//--------------------------------------------------------------------------------------
function ValidateAcceptedTerms(sender, args)
{
    args.IsValid = document.getElementById(sender.cbAcceptedTermsClientId).checked;
}

//--------------------------------------------------------------------------------------
//  Validates CourierName textbox in BuyerTransactionTerms
//  sender - the custom validator object
//  args - used to set the IsValid param
//--------------------------------------------------------------------------------------
function ValidateCourierName(sender, args)
{
    var selectedCourier = document.getElementById(sender.ddlCourierClientId).value;
    var courierName = document.getElementById(sender.txtCourierNameClientId).value;

    if (selectedCourier == "Other" && courierName == "")
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}

//--------------------------------------------------------------------------------------
//  Changes an image button source once the validation group is valid
//--------------------------------------------------------------------------------------
function ChangeButtonOnceGroupIsValid(imgBtn, blurredImageSrc, unblurredImageSrc, validationGroup)
{
    if (Page_ClientValidate(validationGroup))
    {
        document.getElementById(imgBtn).src = unblurredImageSrc;
    }
    else
    {
        document.getElementById(imgBtn).src = blurredImageSrc;
    }
}

//-------------------------------------------------------------------
//    Trigger all page validations
//-------------------------------------------------------------------
function TriggerPageValidations(validationGroup)
{
    for(var validatorIndex in Page_Validators)
    {
        var isValidated = ValidatorValidate(Page_Validators[validatorIndex], validationGroup);
    }
}
