﻿    // Client code to handle Email, Print, Inquire
    
    // This variable is used for validation while gathering the parameters
    var stopPopup;
    
    function print()
    {       
        stopPopup = false;
        var url = buildUrl('print');
        // Check if all data is validated
        if (stopPopup)
        {
            return false;
        }
        openPopup(url, 500, 550);
    }
    
    function email()
    {
        stopPopup = false;
        var url = buildUrl('email');
        // Check if all data is validated
        if (stopPopup)
        {
            return false;
        }
        openPopup(url, 670, 550);
    }
    
    function inquiry()
    {
        stopPopup = false;
        var url = buildUrl('inquiry');
        // Check if all data is validated
        if (stopPopup)
        {
            return false;
        }
        openPopup(url, 780, 550);
    }
    
    function buildUrl(mode)
    {
        var elems = document.forms[0].elements;
        var url = "FreeLicenseCalculatorPopup.aspx?mode=" + mode + '&id=';
        
        // seller ID
        var sellerID = getControlValue(elems, 'SellerIDHidden');
        url = url + sellerID + '&params=';
        
        // license type
        var licenseTypeID = getSelectValue(elems, 'OnlineLicensing1_ddlCategories');
        if (licenseTypeID == 0)
        {
            alert('Please select a License Type.');
            stopPopup = true;
            return;
        }
        url = url + licenseTypeID;
        
        // license parameters
        var licenseParameters = getControls(elems, 'OnlineLicensing1_rptProperties', 20);
        for (var i=0; i<licenseParameters.length; i++)
        {
            var licenseParameterSelectControl = licenseParameters[i];
            if (licenseParameterSelectControl)
            {
                if(licenseParameterSelectControl.type=='text')
                {                    
                    if (licenseParameterSelectControl.value == '')
                    {
                        alert('Please enter a positive number of copies within your selected pricing tier.');
                        stopPopup = true;
                        return;
                    }
                }else
                {
                    var param = getSelectValue(elems, licenseParameterSelectControl.id);
                    if (param == 0)
                    {
                        alert('Please select all license parameters.');
                        stopPopup = true;
                        return;
                    }
                }
                
            }
        }
        
        // bid value
        var isPlaceBidChecked = getCheckboxChecked(elems, 'OnlineLicensing1_PlaceBid');
        if (isPlaceBidChecked)
        {
            // validate that bid value is introduced
            if (!validateRequiredNoMessage('OnlineLicensing1_BidValue'))
            {
                alert('Please enter the bid value.');
                stopPopup = true;
                return;
            }
            // validate that bid value is numeric
            if (!validateNumericNoMessage('OnlineLicensing1_BidValue'))
            {
                alert('Please enter a numerical bid value.');
                stopPopup = true;
                return;
            }
        }
        
        // Put the widget type
        var widgetType = encodeURIComponent(getControlValue(elems, 'WidgetTypeHidden'));
        url = url + '&widgetType=' + widgetType;
        
        // Put the parent URL as a parameter
        //var parentURL = window.parent.document.URL;
        //var parentURL = window.parent.location.href;
        var parentURL = 'N/A';
        var encodedParentURL = encodeURIComponent(parentURL);
        url = url + '&parentURL=' + encodedParentURL;
        
        // Put the track name
        var trackName = encodeURIComponent(getControlValue(elems, 'OnlineLicensing1_TrackName'));
        url = url + '&trackName=' + trackName;
        
        // Put the request / bid flag
        var bidFlag = 1;
        if (getCheckboxChecked(elems, 'OnlineLicensing1_PlaceBid'))
        {
            bidFlag = 2;
        }
        url = url + '&bidFlag=' + bidFlag;
        
        // Put the bid value
        var bidValue = encodeURIComponent(getControlValue(elems, 'OnlineLicensing1_BidValue'));
        url = url + '&bidValue=' + bidValue;
        
        return  cleanString(url);
    }
    
    function openPopup(url, popupHeight, popupWidth)
    {
        popupWindow = window.open(url, 'LicenseQuote','height=' + popupHeight + ',width=' + popupWidth + 
            ',toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=no');
        if (window.focus) 
        {
            popupWindow.focus();
        }
        return false;
    }    
    
    function cleanString(_value)
    {
        return _value.replace('\'','');
    }