﻿function NavProd()
{
   fnDisplayNavProduct('LOAD');
}

function fnOnSwatchClick(oAttributeId, oAttributeValueId,oObj)
{
    var HidAttIdName = 'HidSCAttId_' + String(oAttributeId);

    
    if(ExistsInHC(HidAttIdName)) 
    {
        SetValueOfHC(HidAttIdName, oAttributeId + '~!~' + oAttributeValueId);
    }
    else
    {
        AddToHC(HidAttIdName, oAttributeId + '~!~' + oAttributeValueId);
    }
    
    if(ExistsInHC('HCNames'))
    {
        SetValueOfHC('HCNames', HidAttIdName);
    }
    else
    {
        AddToHC('HCNames', HidAttIdName);
    }
    
//    alert(GetValueOfHC('HCNames'));
//    alert(GetValueOfHC(HidAttIdName));
    //alert();
    var olst = oObj.parentNode.parentNode;
    //alert(olst.tagName);
    for (var i=0;i<olst.childNodes.length;i++)
    {
        //alert(olst.childNodes(i));
        olst.childNodes[i].style.border = '0';
        olst.childNodes[i].style.padding = '0px';
    }
    oObj.parentNode.style.border = 'solid 1px black';
    oObj.parentNode.style.padding = '1px';
    //oObj.style.border = 1px;
}

function fnSubmit()
{
    
    var oSel;
    var oArrControlNm;
    var oHidSelAttribute;
    var oSelValue='';
    
  //  alert(GetValueOfHC('SCNm'));
   
    if(ExistsInHC('SCNm'))
    {
//      alert(ExistsInHC('HCNames'));  
        if(!ExistsInHC('HCNames'))
        {
            oJSFW.fnAlert("Please select a color.");
            return false;
        }
    }
    
    
    if(ExistsInHC('DynamicControlNm')) 
    {
        oArrControlNm = GetValueOfHC('DynamicControlNm').split('~!~');
        
        for(var cnt=0; cnt < oArrControlNm.length; cnt++)
        {
            oSel = oJSFW.fnGetElementById(oArrControlNm[cnt]);
            //var HidAttIdName = 'HidSCAttId_' + oArrControlNm[cnt].substr(oArrControlNm[cnt].lastIndexOf('_') + 1,oArrControlNm[cnt].length);
            if(oSel != null && oSel != 'undefined')
            {
                if(oSelValue=='')
                {
                    oSelValue = oArrControlNm[cnt].substr(oArrControlNm[cnt].lastIndexOf('_') + 1,oArrControlNm[cnt].length) + ',' + oSel.value;    
                }
                else
                {
                    oSelValue = oSelValue + '~!~' + oArrControlNm[cnt].substr(oArrControlNm[cnt].lastIndexOf('_') + 1,oArrControlNm[cnt].length) + ',' + oSel.value;    
                }
            }
        }
        
        if(ExistsInHC('HidSelAtt'))
        {
            SetValueOfHC('HidSelAtt',oSelValue);
        }
        else
        {
            AddToHC('HidSelAtt',oSelValue);
        }
    }
    if(ExistsInHC('Action'))
    {
        SetValueOfHC('Action','ADD');
    }
    else
    {
        AddToHC('Action','ADD');
    }
    
    if(ExistsInHC('Submitted'))
    {
        SetValueOfHC('Submitted','true');
    }
    else
    {
        AddToHC('Submitted','true');
    }
    
    //Display product added message
    dom.get('ProductAdded').fade(0, 100);
    
    var oXhttp = oJSFW.fnObjectInitializer('cACTLXmlHttp','',[]);
    var oConfig = oJSFW.fnObjectInitializer('cXMLHttpConfig','',[]);
    var sRequest = "IsXMLHTTPPostBack=true&Action=Submit";

    var sFormElementValues = "" ;
    var strFormElementNames = "" ;

    strFormElementNames += ((oJSFW.fnGetElementById("HidClient") != null) ? ((strFormElementNames.length > 0) ? "," : "") + "HidClient" : "") ;
    strFormElementNames += ((oJSFW.fnGetElementById("HidSession") != null) ? ((strFormElementNames.length > 0) ? "," : "") + "HidSession" : "") ;
    strFormElementNames += ((oJSFW.fnGetElementById("HidPage") != null) ? ((strFormElementNames.length > 0) ? "," : "") + "HidPage" : "") ;

    sFormElementValues = oJSFW.fnSerializeFormForQuerystring(document.forms['frmShop'], strFormElementNames, true) ;
    
    sRequest = sFormElementValues + ((sRequest.length > 0 && sFormElementValues.length > 0) ? "&" : "") + sRequest ;
    oXhttp.fnSendRequest('Shop.aspx',sRequest,"POST","RequestAddProduct",'fnXmlHttpShopProductRequestHandler',oConfig);
    return false;
    
}

function fnDisplayNavProduct(sAction)
{
    var oDiv;
    var MCount = 0;
    var iCnt;
    var LastL;
    var LastF;
    //if(GetValueOfHC('bIsFlash').toUpperCase() == 'FALSE')
    //{
        if (!ExistsInHC('TotalProducts'))
        {
            return false;
        }
        
        var TotalProducts = parseInt(GetValueOfHC('TotalProducts'));
        //alert(sAction);
        switch(sAction)
        {
            case "NEXT":
                oDiv = oJSFW.fnGetElementById("div_Previous");
                if(oDiv != null && oDiv != 'undefined')
                {
                    oDiv.style.display = "block";
                }
                
                LastF = parseInt(GetValueOfHC('LastF'));
                LastL = parseInt(GetValueOfHC('LastL'));
                
                LastF = parseInt(LastL) ;
                LastL = parseInt(LastF) + 1;
                
                if (parseInt(LastF) >= parseInt(TotalProducts))
                {
                    LastF = parseInt(TotalProducts) - 1;
                }
                
                if (parseInt(LastL) > parseInt(TotalProducts) - 1)
                {
                    LastL = parseInt(TotalProducts) - 1;
                    oDiv = oJSFW.fnGetElementById("div_Next");
                    if(oDiv != null && oDiv != 'undefined')
                    {
                        oDiv.style.display = "none";
                    }    
                }
                
                
                
                
                MCount = 0;
                
                for(iCnt = 0; iCnt < 2; iCnt++)
                {
                    oDiv = oJSFW.fnGetElementById("div_M" + iCnt);
                    if(oDiv != null && oDiv != 'undefined')
                    {
                        oDiv.innerHTML = "";
                    }    
                }
                
                for (iCnt = LastF; iCnt <= LastL; iCnt++)
                {
                    oDiv = oJSFW.fnGetElementById("div_M" + MCount);
                    if(oDiv != null && oDiv != 'undefined')
                    {
                        oDiv.innerHTML = oJSFW.fnGetElementById("div_" + iCnt).innerHTML;
                    }    
                        
                    MCount++;
                }
                
                MCount = 0;
                
                if(ExistsInHC('LastF'))
                {
                    SetValueOfHC('LastF',LastF);
                }
                else
                {
                    AddToHC('LastF',LastF);
                }
                
                if(ExistsInHC('LastL'))
                {
                    SetValueOfHC('LastL',iCnt);
                }
                else
                {
                    AddToHC('LastL',iCnt);
                }
                
                break;
            case "PREVIOUS":
                
                oDiv = oJSFW.fnGetElementById("div_Next");
                if(oDiv != null && oDiv != 'undefined')
                {
                    oDiv.style.display = "block";
                }
                
                if (TotalProducts < 3)
                {
                    LastF = 0;
                    LastL = parseInt(TotalProducts) - 1;
                }
                else
                {
                    
                    LastF = parseInt(GetValueOfHC('LastF')) - 2;
                    LastL = parseInt(GetValueOfHC('LastF')) - 1;
                    
                    if (LastF < 1)
                    {
                        LastF = 0;
                        LastL = 2;
                    }
                }
                
                if(LastF == 0)
                {
                    oDiv = oJSFW.fnGetElementById("div_Previous");
                    if(oDiv != null && oDiv != 'undefined')
                    {
                        oDiv.style.display = "none";
                    }    
                }
                
                MCount = 0;
                
                for(iCnt = 0; iCnt < 2; iCnt++)
                {
                    oDiv = oJSFW.fnGetElementById("div_M" + iCnt);
                    if(oDiv != null && oDiv != 'undefined')
                    {
                        oDiv.innerHTML = "";
                    }
                }
                
                for (iCnt = LastF; iCnt <= LastL; iCnt++)
                {
                    oDiv = oJSFW.fnGetElementById("div_M" + MCount);
                    if(oDiv != null && oDiv != 'undefined')
                    {
                        oDiv.innerHTML = oJSFW.fnGetElementById("div_" + iCnt).innerHTML;
                    }
                    MCount++;
                }
                
                MCount = 0;
                
                if(ExistsInHC('LastF'))
                {
                    SetValueOfHC('LastF',LastF);
                }
                else
                {
                    AddToHC('LastF',LastF);
                }
                
                if(ExistsInHC('LastL'))
                {
                    SetValueOfHC('LastL',iCnt);
                }
                else
                {
                    AddToHC('LastL',iCnt);
                }
                
                break;
            case "LOAD":
                oDiv = oJSFW.fnGetElementById("div_Previous");
                if(oDiv != null && oDiv != 'undefined')
                {
                    oDiv.style.display = "none";
                }
                
                if(TotalProducts <= 2)
                {
                    oDiv = oJSFW.fnGetElementById("div_Next");
                    if(oDiv != null && oDiv != 'undefined')
                    {
                        oDiv.style.display = "none";
                    }
                    
                    for(iCnt = 0; iCnt < TotalProducts; iCnt++)
                    {
                        oDiv = oJSFW.fnGetElementById("div_M" + iCnt);
                        
                        if(oDiv != null && oDiv != 'undefined')
                        {
                            oDiv.innerHTML = oJSFW.fnGetElementById("div_" + iCnt).innerHTML;      
                        }
                    }
                }
                else
                {
                    for(iCnt = 0; iCnt < 2; iCnt++)
                    {
                        oDiv = oJSFW.fnGetElementById("div_M" + iCnt);
                        if(oDiv != null && oDiv != 'undefined')
                        {
                            oDiv.innerHTML = oJSFW.fnGetElementById("div_" + iCnt).innerHTML;      
                        }
                    }
                }
                
                if(ExistsInHC('LastF'))
                {
                    SetValueOfHC('LastF','0');
                }
                else
                {
                    AddToHC('LastF','0');
                }
                
                if(ExistsInHC('LastL'))
                {
                    SetValueOfHC('LastL',iCnt);
                }
                else
                {
                    AddToHC('LastL',iCnt);
                }
                
                break;
        }
    //}   
}

function fnNext()
{
    fnDisplayNavProduct("NEXT");
    return false;
}

function fnPrev()
{
    fnDisplayNavProduct("PREVIOUS");
    return false;
}

function fnShowProduct(oProductId)
{                                     
    if(ExistsInHC('ProductId'))
    {
        SetValueOfHC('ProductId',oProductId);
    }
    else
    {
        AddToHC('ProductId',oProductId);
    }
    
    if(ExistsInHC('CategoryId'))
    {
        oJSFW.fnGetElementById("frmShop").action = "shop.aspx?catid=" + GetValueOfHC('CategoryId');
    }    
        
    oJSFW.fnSubmitForm(oJSFW.fnGetElementById("frmShop"));
    return false;
}

function fnXmlHttpNextRequestHandler(oXmlHttp, oError,strRequestId,nResponseCode)
{
    try
    {
        switch(nResponseCode)
	    {
		    case 1:	
				    var strResponseString = oXmlHttp.responseText ;
				    //alert(strResponseString);
				    var oArrResponesStr;
				    oArrResponesStr = strResponseString.split('~^!^~');
				    if(oArrResponesStr.length = 3)
				    {
				        window.document.getElementById("flashnav").innerHTML = oArrResponesStr[0];
				        SetValueOfHC('LastF',oArrResponesStr[1]);
				        SetValueOfHC('LastL',oArrResponesStr[2]);
				    }
				    else
				    {
				        window.document.getElementById("flashnav").innerHTML = '';
				    }
				    break;
		    case 2:	
				    fnSetRequestStatus("Unsuccessful Request : " + oError +" : "  + strRequestId );
				    break;
    				
		    case 3:	
				    fnSetRequestStatus("Request Pool Full : "  + strRequestId);
				    break;
    				
		    case 4:	
				    fnSetRequestStatus("Duplicate Request : " + strRequestId);
				    break;
	    }
    }
    catch(ex)
    {
        throw new cACTLException(ex.message,'fnXmlHttpShopProductRequestHandler()');
    }
}

function fnXmlHttpShopProductRequestHandler(oXmlHttp, oError,strRequestId,nResponseCode)
{
    try
    {
        switch(nResponseCode)
	    {
		    case 1:	
				    var strResponseString = oXmlHttp.responseText ;
				    /********************************************************************************
                     * Below code has been commented by Nirav Patel Dt. 01-Jun-2007,
                     * To, resolve the Session End related issue.
                     * Start : NBP010620070305
                     * ******************************************************************************/
				    //if (strResponseString == "1" || strResponseString == "0")
			        //{
			        //    window.document.getElementById("NoOfItems").innerHTML = strResponseString + " item";
			        //}
			        //else
			        //{
			        //    window.document.getElementById("NoOfItems").innerHTML = strResponseString + " items";
			        //}
				    
			        //if(ExistsInHC('NoOfCartItems'))
                    //{
                    //    SetValueOfHC('NoOfCartItems',strResponseString);
                    //}
                    //else
                    //{
                    //    AddToHC('NoOfCartItems',strResponseString);
                    //}
				    /*********************************************************************************
                     * End : NBP010620070305
                     * ******************************************************************************/
				    
				    
				    /********************************************************************************
                     * Below code has been added by Nirav Patel Dt. 01-Jun-2007,
                     * To, resolve the Session End related issue.
                     * Start : NBP010620070305
                     * ******************************************************************************/
				    if(strResponseString.fnIsInteger())
				    {
				        if (strResponseString == "1" || strResponseString == "0")
				        {
				            window.document.getElementById("NoOfItems").innerHTML = strResponseString + " item";
				        }
				        else
				        {
				            window.document.getElementById("NoOfItems").innerHTML = strResponseString + " items";
				        }
    				    
				        if(ExistsInHC('NoOfCartItems'))
                        {
                            SetValueOfHC('NoOfCartItems',strResponseString);
                        }
                        else
                        {
                            AddToHC('NoOfCartItems',strResponseString);
                        }
                    }
                    else
                    {
                        alert("It seems that some error has been occurred. We are sorry for inconvenience.");
                    }
                    /*********************************************************************************
                     * End : NBP010620070305
                     * ******************************************************************************/
				    break;
		    case 2:	
				    fnSetRequestStatus("Unsuccessful Request : " + oError +" : "  + strRequestId );
				    break;
    				
		    case 3:	
				    fnSetRequestStatus("Request Pool Full : "  + strRequestId);
				    break;
    				
		    case 4:	
				    fnSetRequestStatus("Duplicate Request : " + strRequestId);
				    break;
	    }
    }
    catch(ex)
    {
        throw new cACTLException(ex.message,'fnXmlHttpShopProductRequestHandler()');
    }
}

function fnShowLargeImg(sImageName)
{
    var oDiv = oJSFW.fnGetElementById("flashzoom");
    var strHTML = "";
    var sImagePath = GetValueOfHC('ProductImagePath');
    strHTML = "<img src=\"" + sImagePath + sImageName + "\" width=\"443\" height=\"410\" border=\"0\" class=\"img\" />";
    oDiv.innerHTML = strHTML;
    //return false;
}

function fnCheckOut()
{
    var iCartItems = parseInt(GetValueOfHC('NoOfCartItems'));
    if(iCartItems > 0)
    {
        window.location.href = "basket.aspx";
    }
    else
    {
        oJSFW.fnAlert('There is no item in basket.');
    }
}


Math.nativeRound = Math.round;
Math.round = function(number, scale) {

	if (isNaN(scale) || scale < 0) {scale = 0;}

	number = this.nativeRound(number * this.pow(10, scale)) / this.pow(10, scale);
	
	if (scale > 0) {
		number = number.toString();
		if (number.indexOf('.') == -1) {number += '.0';}
		while (number.length - number.indexOf('.') - 1 < scale) {
			number += '0';
		}
	}

	return number.toString();

}

var dom = new function(instance) {
	
	var o = this;
	var nodes = {};
	var step = 0.1;

	o.node = function(obj, nodes) {
	
		var o = this;
		var onFadeEnd = null;
		
		o.fade = function(set, end, onEnd, started) {
		
			if (!started) {
				set = set / 100;
				end = end / 100;
				if (onEnd) {onFadeEnd = onEnd;}
			}

			if (obj && typeof obj.style != 'undefined') {
				if (typeof obj.style.opacity != 'undefined') {
					obj.style.opacity = Math.round(set, 2);
					started = true;
				} else if (typeof obj.style.filter != 'undefined') {
					obj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + Math.round(set * 100) + ')';
					started = true;
				}
			}
			
			if (started) {
				obj.style.visibility = (set > 0 ? 'visible' : 'hidden');
			}
			
			if (!started || set == end) {
				if (onFadeEnd) {onFadeEnd();}
				onFadeEnd = null;
				delete dom.nodes;
			} else {
				set = Math.max(0, Math.min(1, set + (set < end ? step : step * -1)));
				window.setTimeout(instance + '.get(\'' + obj.id + '\').fade(' + set + ', ' + end + ', false, true);', 100);
			}
			
		}
		
	}
	
	o.get = function(id) {
		if (!nodes[id]) {
			nodes[id] = new o.node(document.getElementById(id), o);
		}
		return nodes[id];
	};
	
} ('dom');