
//Global XMLHTTP Request object
var XmlHttpCategory;
var pageId;
//var AjaxServerPageName = "/AjaxServer/GetDataServer.aspx";


function GetB2CFeaturedProducts(CatId) 
{  
    
//    var requestUrl = AjaxServerPageName + "?lookupType=B2CBigBanner&SelectedCategory=" + encodeURIComponent(CatId);
//	CreateXmlHttpBanner();
//    if(XmlHttpBanner)
//    {
//	    XmlHttpBanner.onreadystatechange = HandleResponseBanner;
//	    XmlHttpBanner.open("GET", requestUrl,  true);
//	    XmlHttpBanner.send(null);		
//    }

    var requestUrl = AjaxServerPageName + "?lookupType=B2CFeaturedProducts&SelectedCategory=" + encodeURIComponent(CatId);
	CreateXmlHttpCategory();
    if(XmlHttpCategory)
    {
	    XmlHttpCategory.onreadystatechange = HandleResponseCategory;
	    XmlHttpCategory.open("GET", requestUrl,  true);
	    XmlHttpCategory.send(null);		
    }

//    var requestUrl = AjaxServerPageName + "?lookupType=B2CRightBanner&SelectedCategory=" + encodeURIComponent(CatId);
//	CreateXmlHttpRightBanner();
//    if(XmlHttpRBanner)
//    {
//	    XmlHttpRBanner.onreadystatechange = HandleResponseRightBanner;
//	    XmlHttpRBanner.open("GET", requestUrl,  true);
//	    XmlHttpRBanner.send(null);		
//    }
    
    var requestUrl = AjaxServerPageName + "?lookupType=B2CFeaturedStore&SelectedCategory=" + encodeURIComponent(CatId);
	CreateXmlHttpFeaturedStore();
    if(XmlHttpFeaturedStore)
    {
	    XmlHttpFeaturedStore.onreadystatechange = HandleResponseFeaturedStore;
	    XmlHttpFeaturedStore.open("GET", requestUrl,  true);
	    XmlHttpFeaturedStore.send(null);		
    }
}

function CreateXmlHttpCategory()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttpCategory = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpCategory = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			  XmlHttpCategory = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttpCategory && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpCategory = new XMLHttpRequest();
	}
}

function CreateXmlHttpRightBanner()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttpRBanner = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpRBanner = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			  XmlHttpRBanner = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttpRBanner && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpRBanner = new XMLHttpRequest();
	}
}

function CreateXmlHttpBanner()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttpBanner = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpBanner = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			  XmlHttpBanner = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttpBanner && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpBanner = new XMLHttpRequest();
	}
}

function CreateXmlHttpFeaturedStore()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttpFeaturedStore = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpFeaturedStore = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			  XmlHttpFeaturedStore = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttpFeaturedStore && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpFeaturedStore = new XMLHttpRequest();
	}
}

//Called when response comes back from server for Category
function HandleResponseCategory()
{
	if(XmlHttpCategory.readyState == 4)
	{
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpCategory.status == 200)
		{	
		    SetB2CFeaturedProducts(XmlHttpCategory.responseXML);
		}
		else
		{
		    
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function SetB2CFeaturedProducts(responseXML)
{
	var ProductID = responseXML.getElementsByTagName('ProductID');
	var ProductName = responseXML.getElementsByTagName('ProductName');
	var ImagePrimary =  responseXML.getElementsByTagName('RegularImage');
	var SubDomain =  responseXML.getElementsByTagName('SubDomain');
	var totalrecords = ProductID.length;


	var strproducts;
	strproducts = "<table border='0' cellspacing='4' cellpadding='3'><tr>";

	//declaration of local variables.
	var PID;
	var PName;
	var ImgPath;
	var subdomainname;
	var shortdesc;
	if(totalrecords >= 1)
	{
       for (var count = 0; count < totalrecords; count++)
	    {
	       
	       PID = GetInnerTextCategory(ProductID[count]);
	       PName = GetInnerTextCategory(ProductName[count]);
	       PName = PName.substring(0,10)+'...';
	       ImgPath = GetInnerTextCategory(ImagePrimary[count]);
	       subdomainname = GetInnerTextCategory(SubDomain[count]);
	       strproducts = strproducts + "<td align='left'>";
	       strproducts = strproducts + "<table border='0' cellspacing='0' cellpadding='0'><tr><td><table cellpadding='3' class='CartImageBorderB2C'><tr><td style='text-align: center;'>";
	       strproducts = strproducts + "<a href='B2CProductDescription.aspx?ProductId="+PID+"'><img src='Member/"+subdomainname+"/"+ImgPath+"' width='70' height='70' border='0' /></a></td></tr></table></td>";
	       strproducts = strproducts + "<tr><td align='center'><a class='itemMatter' href='B2CProductDescription.aspx?ProductId="+PID+"'>"+PName+"</a>";
	       strproducts = strproducts + "</td></tr></table><td>";
	    }
	}
	else
	{
	  strproducts = strproducts+"<td align='right'><img src=imagesV2/icon-noFeaPrd-avail-B2C.png /></td>";
	}
	
	strproducts = strproducts + "</tr></table>";
	var textValue = document.getElementById("maindiv"); 
	textValue.innerHTML = strproducts;
}


//Called when response comes back from server for Category
function HandleResponseBanner()
{
	if(XmlHttpBanner.readyState == 4)
	{
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpBanner.status == 200)
		{	
		    SetB2CBannerImages(XmlHttpBanner.responseXML);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function SetB2CBannerImages(responseXML)
{
	var CatId = responseXML.getElementsByTagName('CatId');
	var ImageName = responseXML.getElementsByTagName('ImageName');
	var ToolTip =  responseXML.getElementsByTagName('ToolTip');
	var LinkTarget =  responseXML.getElementsByTagName('LinkTarget');
    var totalrecords = CatId.length;

//This code is for displaying same images if user enters less than 3 banner images.
    var name1;
    var name2;
    var name3;
    
    var lnk1;
    var lnk2;
    var lnk3;
    
    if(totalrecords == 1)
	{
	  name1 = GetInnerTextCategory(ImageName[0]);
	  name2 = GetInnerTextCategory(ImageName[0]);
	  name3 = GetInnerTextCategory(ImageName[0]);
	  
	  lnk1 = GetInnerTextCategory(LinkTarget[0]);
	  lnk2 = GetInnerTextCategory(LinkTarget[0]);
	  lnk3 = GetInnerTextCategory(LinkTarget[0]);
	}
	  
	if(totalrecords == 2)
	{
	  name1 = GetInnerTextCategory(ImageName[0]);
	  name2 = GetInnerTextCategory(ImageName[1]);
	  name3 = GetInnerTextCategory(ImageName[0]);
	  
	  lnk1 = GetInnerTextCategory(LinkTarget[0]);
	  lnk2 = GetInnerTextCategory(LinkTarget[1]);
	  lnk3 = GetInnerTextCategory(LinkTarget[0]);
	  
	}
	
	if(totalrecords == 3)
	{
	  name1 = GetInnerTextCategory(ImageName[0]);
	  name2 = GetInnerTextCategory(ImageName[1]);
	  name3 = GetInnerTextCategory(ImageName[2]);
	  
	  lnk1 = GetInnerTextCategory(LinkTarget[0]);
	  lnk2 = GetInnerTextCategory(LinkTarget[1]);
	  lnk3 = GetInnerTextCategory(LinkTarget[2]);
    } 
	 
	  var ImgFirst = document.getElementById("ImgFirst"); 
	  ImgFirst.src = "imagesV2/"+ name1;
	  var ImgSecond = document.getElementById("ImgSecond"); 
	  ImgSecond.src = "imagesV2/"+name2;
	  var ImgThird = document.getElementById("ImgThird"); 
	  ImgThird.src = "imagesV2/"+name3;
	  
	  var lnkFirst = document.getElementById("lnkFirst"); 
	  lnkFirst.href = lnk1;
	  
	  var lnkSecond = document.getElementById("lnkSecond"); 
	  lnkSecond.href = lnk2;
	  
	  var lnkThird = document.getElementById("lnkThird"); 
	  lnkThird.href = lnk3;
	  
    
}

function HandleResponseRightBanner()
{
	if(XmlHttpRBanner.readyState == 4)
	{
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpRBanner.status == 200)
		{	
		    SetB2CRightBannerImages(XmlHttpRBanner.responseXML);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function SetB2CRightBannerImages(responseXML)
{
	var CatId = responseXML.getElementsByTagName('CatId');
	var ImageName = responseXML.getElementsByTagName('ImageName');
	var ToolTip =  responseXML.getElementsByTagName('ToolTip');
	var LinkTarget =  responseXML.getElementsByTagName('LinkTarget');
    var totalrecords = CatId.length;

//This code is for displaying images if user enters less than 3 banner images (hardcoded, can be optimised).
    var name1;
    var name2;
    var name3;
    
    var lnk1;
    var lnk2;
    var lnk3;

    if(totalrecords == 1)
	{
	  name1 = GetInnerTextCategory(ImageName[0]);
	  lnk1 = GetInnerTextCategory(LinkTarget[0]);
	  
	  var RightBanner1 = document.getElementById("RightBanner1"); 
	  RightBanner1.src = "imagesV2/"+ name1;
	  var RightBanner2 = document.getElementById("RightBanner2"); 
	  RightBanner2.src = "";
 	  var RightBanner3 = document.getElementById("RightBanner3"); 
	  RightBanner3.src = "";
	  
	  var lnkRightBanner1 = document.getElementById("lnkRightBanner1"); 
	  lnkRightBanner1.href = lnk1;
	  
	  var lnkRightBanner2 = document.getElementById("lnkRightBanner2"); 
	  lnkRightBanner2.href = "";
	  
	  var lnkRightBanner3 = document.getElementById("lnkRightBanner3"); 
	  lnkRightBanner3.href = "";
	  
	}
	  
	if(totalrecords == 2)
	{
	  name1 = GetInnerTextCategory(ImageName[0]);
	  name2 = GetInnerTextCategory(ImageName[1]);

	  lnk1 = GetInnerTextCategory(LinkTarget[0]);
	  lnk2 = GetInnerTextCategory(LinkTarget[1]);
	  
	  var RightBanner1 = document.getElementById("RightBanner1"); 
	  RightBanner1.src = "imagesV2/"+ name1;
	  var RightBanner2 = document.getElementById("RightBanner2"); 
	  RightBanner2.src = "imagesV2/"+name2;
 	  var RightBanner3 = document.getElementById("RightBanner3"); 
	  RightBanner3.src = "";

	  
	  var lnkRightBanner1 = document.getElementById("lnkRightBanner1"); 
	  lnkRightBanner1.href = lnk1;
	  
	  var lnkRightBanner2 = document.getElementById("lnkRightBanner2"); 
	  lnkRightBanner2.href = lnk2;
	  
	  var lnkRightBanner3 = document.getElementById("lnkRightBanner3"); 
	  lnkRightBanner3.href = "";
	  
	}
	
	if(totalrecords == 3)
	{
	  name1 = GetInnerTextCategory(ImageName[0]);
	  name2 = GetInnerTextCategory(ImageName[1]);
	  name3 = GetInnerTextCategory(ImageName[2]);
	  
	  lnk1 = GetInnerTextCategory(LinkTarget[0]);
	  lnk2 = GetInnerTextCategory(LinkTarget[1]);
	  lnk3 = GetInnerTextCategory(LinkTarget[2]);
   
	 
	  var RightBanner1 = document.getElementById("RightBanner1"); 
	  RightBanner1.src = "imagesV2/"+ name1;
	  var RightBanner2 = document.getElementById("RightBanner2"); 
	  RightBanner2.src = "imagesV2/"+name2;
	  var RightBanner3 = document.getElementById("RightBanner3"); 
	  RightBanner3.src = "imagesV2/"+name3;
	  
	  var lnkRightBanner1 = document.getElementById("lnkRightBanner1"); 
	  lnkRightBanner1.href = lnk1;
	  
	  var lnkRightBanner2 = document.getElementById("lnkRightBanner2"); 
	  lnkRightBanner2.href = lnk2;
	  
	  var lnkRightBanner3 = document.getElementById("lnkRightBanner3"); 
	  lnkRightBanner3.href = lnk3;
	}
    
}

function HandleResponseFeaturedStore()
{
	if(XmlHttpFeaturedStore.readyState == 4)
	{
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpFeaturedStore.status == 200)
		{	
		    SetB2CFeaturedStores(XmlHttpFeaturedStore.responseXML);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function SetB2CFeaturedStores(responseXML)
{
//debugger;
    var Catname = responseXML.getElementsByTagName('Catname');
    var catid = responseXML.getElementsByTagName('catid');
    var pcatid = responseXML.getElementsByTagName('parentid');
   
	var CompanyName = responseXML.getElementsByTagName('comp_companyname');
	var Webaddress  =  responseXML.getElementsByTagName('comp_webadress');
	var totalrecords = catid.length;
	var strproducts ="&nbsp;";
	var strproducts1 ="&nbsp;";
	var strproducts2 = "&nbsp;";
	var category;
	var category1;
	var category2;
	var CName;
	var WebURL;
	var categoryid;
	var categoryid1;
	var categoryid2;
	var pid;
	var qstr;

	//alert(pcatid);
	strproducts = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr>";
    strproducts = strproducts +"<td class='txtlightBlueBold' height='20' valign='top'>";
	
	//declaration of local variables.
	
	if(totalrecords >= 1)
    {
       category = GetInnerTextCategory(Catname[0]);
       strproducts = strproducts + category+"</td></tr>"; 
       var categoryid = GetInnerTextCategory(catid[0]);
       var i =0;
       for (var count = 0; count < totalrecords; count++)
	   {
	       if (categoryid == GetInnerTextCategory(catid[count]))
	       {
	           //if(i==5) break;
               CName = GetInnerTextCategory(CompanyName[count]);
               WebURL =  GetInnerTextCategory(Webaddress[count]);
               strproducts = strproducts +"<tr><td><a href='"+WebURL+"' class='featureStore' target='_blank'>"+CName+"</a></td></tr>";
               i = i+1;
            }
            if(pcatid != null && pid == null)
            {
                pid = GetInnerTextCategory(pcatid[0])
                //alert(a);
             }
        }      
         
         for (var count = 0; count < (5-i); count++)
         {
             strproducts = strproducts +"<tr><td style='padding:3px 0px 3px 0px;' align='left'>&nbsp;</td></tr>";
             
         }
         
         qstr="Company/Company.aspx?SrchFrm=Home&amp;BType=B2C&amp;CatId=" + pid;
        
         if(i <= 5)
	        strproducts = strproducts +"<tr><td align='right'><a href='"+qstr+"' class='lnkgrey'>View All</a></td></tr>";
	      strproducts = strproducts + "</table>";
     }
     else
	  strproducts = "&nbsp;";
    if(totalrecords > i)
    {  
    //Second SubCategory Featured Stores
	strproducts1 = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr>";
    strproducts1 = strproducts1 +"<td class='txtlightBlueBold' height='20' valign='top'>";
    category1 = GetInnerTextCategory(Catname[i]);
    var categoryid1 = GetInnerTextCategory(catid[i]);
    strproducts1 = strproducts1 + category1+"</td></tr>"; 
    for (var count = i; count < totalrecords; count++)
	{
	    if (categoryid1 == GetInnerTextCategory(catid[count]))
	    {
	           //if(i==5) break;
               CName = GetInnerTextCategory(CompanyName[count]);
               WebURL =  GetInnerTextCategory(Webaddress[count]);
               strproducts1 = strproducts1 +"<tr><td><a href='"+WebURL+"' class='featureStore' target='_blank'>"+CName+"</a></td></tr>";
               i = i+1;
         }
     } 
     
     for (var count = 4; count < (10-i); count++)
      {
         strproducts1 = strproducts1 +"<tr><td style='padding:3px 0px 3px 0px;' align='left'>&nbsp;</td></tr>";
      }
     var qstr1="Company/Company.aspx?SrchFrm=Home&amp;BType=B2C";//&amp;CatId="+categoryid1;
     if(i <= 10)
        strproducts1 = strproducts1 +"<tr><td align='right'><a href='"+qstr+"' class='lnkgrey'>View All</a></td></tr>";

    strproducts1 = strproducts1 + "</table>";
    //=========Upto Here
    }
    else
	  strproducts1 = "&nbsp;";
    
    if(totalrecords > i)
    {
    //Third SubCategory Featured Stores
	strproducts2 = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr>";
    strproducts2 = strproducts2 +"<td class='txtlightBlueBold' height='20' valign='top'>";
    category2 = GetInnerTextCategory(Catname[i]);
    var categoryid2 = GetInnerTextCategory(catid[i]);
    strproducts2 = strproducts2 + category2+"</td></tr>"; 
    for (var count = i; count < totalrecords; count++)
	{
	    if (categoryid2 == GetInnerTextCategory(catid[count]))
	    {
	           //if(i==5) break;
               CName = GetInnerTextCategory(CompanyName[count]);
               WebURL =  GetInnerTextCategory(Webaddress[count]);
               strproducts2 = strproducts2 +"<tr><td><a href='"+WebURL+"' class='featureStore' target='_blank'>"+CName+"</a></td></tr>";
               i = i+1;
         }
     } 
     
     for (var count = 9; count < (15-i); count++)
      {
         strproducts2 = strproducts2 +"<tr><td style='padding:3px 0px 3px 0px;' align='left'>&nbsp;</td></tr>";
      }
      var qstr2="Company/Company.aspx?SrchFrm=Home&amp;BType=B2C&amp;CatId="+categoryid2;
     if(i <= 15)
        strproducts2 = strproducts2 +"<tr><td align='right'><a href='"+qstr+"' class='lnkgrey'>View All</a></td></tr>";

    strproducts2 = strproducts2 + "</table>";
    //=========Upto Here
    

	}
	else
	  strproducts2 = "&nbsp;";
	
	if(strproducts == "&nbsp;" && strproducts1=="&nbsp;" && strproducts2=="&nbsp;")
	{
	    strproducts = strproducts+"<br/><br/><br/>&nbsp;&nbsp;<img src=imagesV2/icon-noFeat-Storel.png />";
	    var textValue = document.getElementById("divNoFS"); 
	    textValue.style.valign = "middle";
	    textValue.innerHTML = strproducts;
	}
	else
	{
	        var result = "<table width='100%' border='0' cellspacing='7' cellpadding='0'><tr><td align='left' valign='top' width='32%'>"
                result += strproducts;
                result += "</td><td width='1%' style='background-repeat: repeat-y; background-image: url(ImagesV2/img-v-dot.gif);'> </td>"     
                result += "<td align='left' valign='top' width='33%'>"  + strproducts1;  +"</td>";
                result += "<td width='1%' style='background-repeat: repeat-y; background-image: url(ImagesV2/img-v-dot.gif);'> </td>";    
                result += "<td align='left' valign='top' width='33%'>" + strproducts2 +"</td></tr></table>";    
                    
                 var textValue = document.getElementById("divNoFS"); 
	             textValue.innerHTML = result;      
                    
                    
                   
                    
                      
                    
                
            
//	    var textValue = document.getElementById("FirstRow"); 
//	    textValue.innerHTML = strproducts;
//    	

//	    var textValue = document.getElementById("SecondRow"); 
//	    textValue.innerHTML = strproducts1;
//    	
//        var textValue = document.getElementById("ThirdRow"); 
//	    textValue.innerHTML = strproducts2;
	}
}



//Returns the node text value 
function GetInnerTextCategory (node)
{
     if(node != null)
	 return (node.textContent || node.innerText || node.text || node.value || node.innerHTML || node.firstChild.nodeValue) ;
	 else
	 return null;
}




