﻿// JScript File
 var p_strSessionPIds = '';
function fnAddProductInCart(p_strSessId, p_intCustId, p_intPID, p_intQTY, p_dblPrice, p_intBasePID, p_dblOfferPrice)
{

    var strMsg = '';
    if(trim(p_intCustId) == '') p_intCustId = 0;
    var PIds = '';
    if(PIds != '')
    {
        PIds = PIds + ',' + p_intPID;
    }
    
    p_strSessionPIds = PIds;
 
    // 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=IsAlldAssociateSplOfferForCart&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=AddPrd&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)
    {
        document.getElementById('spanShoppingCart').innerHTML = arrResult[1]; // have the shopping cart items count
        fnManageMessage('OPEN',strMsg + 'Product has been added to the cart.');
        //alert('Product has been added in the cart.')
    }
    else
        fnManageMessage('OPEN','Product has not been added due to an error.');
        //alert('Product has not been added due to an error.');
}

function fnAddProductToFavorite(p_intCustId, p_intPID, p_strURL)
{
//    if(trim(p_intCustId) == '') 
//    {
//        window.parent.document.getElementById('iFrameSrc').src = 'login.aspx?returnUrl=' + p_strURL;
//        return;
//    }

    if(trim(p_intCustId) == '') 
        p_intCustId = 0;

    var requestUrl = 'XMLSupportProject.aspx?strAction=AddPrdToFavorite&CustId=' + p_intCustId + '&PrdID=' + p_intPID;

    var objAjax = new clsAjax(requestUrl);
    var intResult = objAjax.getXMLData();

    var arrResult = intResult.split(',');

    if(eval(arrResult[0]) == 1)
    {
        document.getElementById('spanFavoriteList').innerHTML = arrResult[1]; // have the shopping cart items count
        //alert('Product has been added in your favorite list.');   
        fnManageMessage('OPEN','Product has been added in your favorite list.');
    }
    else if(eval(arrResult[0]) == 2)
        fnManageMessage('OPEN','Product already added in your favorite list.');
        //alert('Product already added in your favorite list.');
    else
        fnManageMessage('OPEN','Product has not been added due to an error.');
        //alert('Product has not been added due to an error.');
}

//function fnUpdateCartItemCount(p_intCount)
//{
//    window.parent.document.getElementById('spanShoppingCart').innerHTML = p_intCount;
//}

function trim(p_strValue) 
{
    return p_strValue.replace(/^\s+/,'').replace(/\s+$/,'');
}

function fnUpdateCart()
{
    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))
}