﻿// JScript File
function fnAddProductInGiftBasket(p_strSessId, p_intCustId, p_intPID, p_intQTY, p_dblPrice, p_intBasePID, p_dblOfferPrice)
{
    if(trim(p_intCustId) == '') p_intCustId = 0;
    var strMsg = '';
    // The below block of code is just to ensure that the product's special offer price is applicable. 
    if(p_intBasePID != undefined) 
    {
        var requestUrl = 'XMLSupportProject.aspx?strAction=IsAlldAssociateSplOfferForGftBasket&SessId=' + p_strSessId + '&BasePrdID=' + p_intBasePID;
        requestUrl += '&PrdID=' + p_intPID;

        var objAjax = new clsAjax(requestUrl);
        var strResult = objAjax.getXMLData();
        if(strResult == 'False')
        {
            strMsg = 'Spl Price is only applicable along with main product.<br>';
        }
        else
            p_dblPrice = p_dblOfferPrice;
    }
    
    var requestUrl = 'XMLSupportProject.aspx?strAction=AddPrdInGiftBasket&SessId=' + p_strSessId + '&CustId=' + p_intCustId;
        requestUrl += '&PrdID=' + p_intPID + '&Qty=' + p_intQTY + '&Price=' + p_dblPrice;
    
    var objAjax = new clsAjax(requestUrl);
    var intResult = objAjax.getXMLData();
    
    var arrResult = intResult.split(',');
    if(eval(arrResult[0]) == 1)
    {
        window.parent.document.getElementById('spanGiftToFriend').innerHTML = arrResult[1]; // have the Gift Basket items count
        window.parent.fnManageMessage('OPEN',strMsg + 'Product has been added in the gift basket.');
        //alert('Product has been added in the gift basket.')
    }
    else
        window.parent.fnManageMessage('OPEN','Product has not been added due to an error.');
        //alert('Product has not been added due to an error.');
}

function trim(p_strValue) 
{
    return p_strValue.replace(/^\s+/,'').replace(/\s+$/,'');
}

function fnUpdateBasket()
{
    var frm = document.forms[0];

    for (i=0; i<frm.length; i++)
    {
        if(frm.elements[i].type == 'text' && frm.elements[i].id.indexOf('txtQty') != -1)
        {
            var strTextValue = trim(frm.elements[i].value);
            if(strTextValue == '')
            {
                alert('Please enter quantity.')
                frm.elements[i].select(); 
                return false;
            }
            if(!isInteger(strTextValue))
            {
                alert('Please enter valid quantity.')
                frm.elements[i].select(); 
                return false;
            }
        }
    }
    return true;
}

function isInteger(s)
{
    var validChars = "1234567890";
    for(var iLoop=0; iLoop < s.length; iLoop++)
    {
        if(validChars.indexOf(s.charAt(iLoop)) == -1) return false;
    }
    if(eval(s) == 0) return false;
  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}