/*globals  $$, window, document, location, , portlet_with_empty_content,browserType,displaySignInfo1, setTimeout,  checkUniqueOmnitureCookie, dojo, dijit, clearSelectedCell, console,formOmnitureCode, gridProduct,gridProductFamily,gridProductLine,displayProductLinesByFamily, displayProductByLine,displayProductLinks, getSelectedProductFamily, replaceAll, hidePane, getproductLineByFamily, resetPane,getSelectedProductLine,alignText,getProductByLine,showPane,widgetHeight*/

"use strict";
dojo.addOnLoad(function () {
    dojo.connect(dijit.byId("gridProductFamily"), "onRowClick", displayProductLinesByFamily);
    dojo.connect(dijit.byId("gridProductFamily"), "onRowClick", checkUniqueOmnitureCookie);
    dojo.connect(dijit.byId("gridProductLine"), "onRowClick", displayProductByLine);
    dojo.connect(dijit.byId("gridProduct"), "onRowClick", displayProductLinks);
    
    dojo.connect(dijit.byId("gridProductFamily"), "onKeyPress",productSelectorKeyPress);
    dojo.connect(dijit.byId("gridProductFamily"), "onCellMouseOver",productSelectorCellMouseOver);
    dojo.connect(dijit.byId("gridProductFamily"), "onCellMouseOut",productSelectorCellMouseOut);
    
    dojo.connect(dijit.byId("gridProductLine"), "onKeyPress",productSelectorKeyPress);
    dojo.connect(dijit.byId("gridProductLine"), "onCellMouseOver",productSelectorCellMouseOver);
    dojo.connect(dijit.byId("gridProductLine"), "onCellMouseOut",productSelectorCellMouseOut);
    
    dojo.connect(dijit.byId("gridProduct"), "onKeyPress",productSelectorKeyPress);
    dojo.connect(dijit.byId("gridProduct"), "onCellMouseOver",productSelectorCellMouseOver);
    dojo.connect(dijit.byId("gridProduct"), "onCellMouseOut",productSelectorCellMouseOut);
    
});

dojo.addOnLoad(startOver);

dojo.requireLocalization("dojoc.productselector","locale_ps",null,"en,en-us,zh,zh-cn,ROOT,jp,ar-ar,ar-xr,cs-cz,de-de,es-es,es-la,es-xl,hu-hu,fr-fr,he-il,id-id,it-it,ja-jp,ko-kr,nl-nl,pl-pl,pt-br,ru-ru,sv-se,th-th,tr-tr,uk-ua,vi-vn,zh-tw");
this.i18n=dojo.i18n.getLocalization("dojoc.productselector","locale_ps",this.lang);

var gridLayoutFamily = [{
		name: this.i18n.gridproductFamilyheader,
		field: "name",
		headerClasses: "gridhead",
		cellClasses: "gridcell",
		width:"222px",		
		noresize:"true",
		formatter:alignText
}];
var gridLayoutLine = [{
		name: this.i18n.gridproductLineheader,
		field: "name",
		headerClasses: "gridhead",
		cellClasses: "gridcell",
		width:"222px",
		noresize:"true",
		formatter:alignText
}];
var gridLayoutProduct = [{
		name: this.i18n.gridproductproductheader,
		field: "name",
		headerClasses: "gridhead",
		cellClasses: "gridcell",
		width:"222px",
		noresize:"true",
		formatter:alignText
}];
var gridLayoutProductLinks = [{
		name: this.i18n.gridproductlinksheader,
		field: "name",
		headerClasses: "gridhead",
		width:"222px",
		noresize:"true",
		formatter:alignText
}];

var selectedLevel = 1;
var mouseout = 0;

//method which displays the final set of pages
function alignText(value) {

	value+= "&nbsp;&nbsp;<font class='support_widget_arrow'>&gt;</font>";	
	return value;

}
//method called when the Start over link is clicked
function startOver() {
    var grid, jsonStore, status, locale, gotItems;
    	
    	widgetHeight = document.getElementById("support-browse4ps").offsetHeight-26;

    	document.getElementById("productFamilyGrid").style.visibility = "visible";
	
	grid = dijit.byId("gridProductFamily");
	
	dojo.byId('rootDiv').style.height = widgetHeight + 232 + 'px';
	
	status='active';
	locale = javeLocale;
		
		//code to call the server side and form the productLine.JSON file
        jsonStore = new dojo.data.ItemFileReadStore({
            url: "/p/ProductSelectionServlet/productselection?level=1&locale=" + locale + "&status=" + status
        });

	gotItems = function(items, request){
	    console.log("Number of items loaded: " + items.length);
    	if (items.length === 0) {
    		document.getElementById("pswidget").style.display = "none";
    		document.getElementById("widgeterrorspace").style.display = "block";		
	    }    	
	};
        
	grid.setStore(jsonStore);		
	grid.store.fetch({serverQuery: {start: 0,count: 1},onComplete: gotItems});
	grid.startup(); 	
	
	document.getElementById("defaultloading").style.display = "none"; 
	
	//clearSelectedCell(grid,1);
	
}
//method called when the Start over link is clicked
function startOverAgain() {
    var grid, jsonStore, status, locale;
    
    	document.getElementById("productFamilyGrid").style.visibility = "visible";

	//reset the second pane
	document.getElementById("loadMessageForProductLine").style.display = "none";		
	//document.getElementById("productLineGrid").style.display = "none";	
	document.getElementById("productLineGrid").style.visibility = "hidden";
	document.getElementById("loadTextForproductLine").style.display = "block";
		
	//reset the third pane	
   	document.getElementById("loadTextForProduct").style.display = "none";   	
   	//document.getElementById("productGrid").style.display = "none";
   	document.getElementById("productGrid").style.visibility = "hidden";
   	document.getElementById("loadMessageForProduct").style.display = "none";	
   	document.getElementById("loadEmptyTextForProduct").style.display = "block";
		
	//hide the last pane
	hidePane("pane4");
	
	dojo.byId('rootDiv').style.height = widgetHeight + 232 +'px';
	
	//hiding the start over
	document.getElementById("supportstartover").style.visibility = "hidden";
	
	grid = dijit.byId("gridProductFamily");
	
	//code to check whether the eois is selected or not
    if (dojo.byId("eoisproducts").checked) {
		status='active';
		locale=javeLocale;
		
		//code to call the server side and form the productLine.JSON file
        jsonStore = new dojo.data.ItemFileReadStore({
            url: "/p/ProductSelectionServlet/productselection?level=1&locale=" + locale + "&status=" + status
        });
        
		grid.setStore(jsonStore);		
		grid.startup(); 
	}
	
	document.getElementById("activeproducts").checked = true;
	document.getElementById("eoisproducts").checked = false;	
	
	document.getElementById("defaultloading").style.display = "none"; 
	
	clearSelectedCell(grid,0);   	 	
   	
   	selectedLevel = 1;
}
//method called when - onclick of the product Family cell
function displayProductLinesByFamily(e) {

    var selectedValue, selectedEOISFlagValue, selectedActiveFlagValue;

	selectedValue = gridProductFamily.store.getValue(gridProductFamily.getItem(e.rowIndex),"id");
	selectedEOISFlagValue = gridProductFamily.store.getValue(gridProductFamily.getItem(e.rowIndex),"EOISFlag");
	selectedActiveFlagValue = gridProductFamily.store.getValue(gridProductFamily.getItem(e.rowIndex),"ActiveFlag");
	
	console.log("selected product Family selectedValue:"+selectedValue);
	console.log("selected product Family selectedEOISFlagValue:"+selectedEOISFlagValue);
	console.log("selected product Family selectedActiveFlagValue:"+selectedActiveFlagValue);
		
	getproductLineByFamily(selectedValue,selectedEOISFlagValue,selectedActiveFlagValue);
}
//method called to display the product Line based on product Family
function getproductLineByFamily(productFamily, selectedEOISFlagValue, selectedActiveFlagValue) {
    var grid, status, locale, jsonStore, gotItems;
	
	grid = dijit.byId("gridProductLine");
	
	//reset the third pane
	resetPane("loadTextForProduct");	
	
	//document.getElementById("productGrid").style.display = "none";
	document.getElementById("productGrid").style.visibility = "hidden";
	document.getElementById("loadMessageForProduct").style.display = "none";
	document.getElementById("loadEmptyTextForProduct").style.display = "none";
	
	document.getElementById("productLineGrid").style.visibility = "visible";
	document.getElementById("loadTextForproductLine").style.display = "none";
	document.getElementById("loadMessageForProductLine").style.display = "none";
	
	//hide the last pane
	hidePane("pane4");
	
	//code to check whether the eois is selected or not
	status='active';
	locale=javeLocale;
		
	//code to check whether the eois is selected or not
    if (dojo.byId("eoisproducts").checked) {
		status="eois";
	}	

    if ((status == "active" && selectedActiveFlagValue == "Y") || (status == "eois" && selectedEOISFlagValue == "Y")) {
	
	//code to call the server side and form the productLine.JSON file
        jsonStore = new dojo.data.ItemFileReadStore({
            url: "/p/ProductSelectionServlet/productselection?level=2&parentid=" + productFamily + "&status=" + status + "&locale=" + locale
        });

		gotItems = function(items, request){
		    console.log("Number of items loaded: " + items.length);
            if (items.length === 0) {
			clearSelectedCell(grid,1);
				
			document.getElementById("loadTextForproductLine").style.display = "none";
			//document.getElementById("productLineGrid").style.display = "none";		
			document.getElementById("productLineGrid").style.visibility = "hidden";
			document.getElementById("loadMessageForProductLine").style.display = "block";
		    }
            else {
			document.getElementById("loadTextForproductLine").style.display = "none";
			document.getElementById("loadMessageForProductLine").style.display = "none";		
			//document.getElementById("productLineGrid").style.display = "block";
			
			if(document.getElementById("productLineGrid").style.visibility != "visible"){			                    
	                    	
				document.getElementById("productLineGrid").style.visibility = "visible";			                 
                        }
			clearSelectedCell(grid,1);		    
		    }
		};		
		
	grid.setStore(jsonStore);	
        grid.store.fetch({serverQuery: {start: 0,count: 1},onComplete: gotItems});

        // Call startup, in order to render the grid:
        grid.startup();       

    }
    else {
        clearSelectedCell(grid,1);
	
	document.getElementById("loadTextForproductLine").style.display = "none";
	//document.getElementById("productLineGrid").style.display = "none";		
	document.getElementById("productLineGrid").style.visibility = "hidden";
	document.getElementById("loadMessageForProductLine").style.display = "block";
	
    }		
        //document.getElementById("pane2").style.left = "0px";		
	dojo.byId('rootDiv').style.height = widgetHeight + 232 + 'px';

	document.getElementById("loadEmptyTextForProduct").style.display = "none";
	document.getElementById("loadMessageForProduct").style.display = "none";
	//document.getElementById("productGrid").style.display = "none";		
	document.getElementById("productGrid").style.visibility = "hidden";
	document.getElementById("loadTextForProduct").style.display = "block";		

	//sliding the component
	//slideComponent("pane2");		
	
	document.getElementById("supportstartover").style.visibility = "visible";	
	
	selectedLevel = 2;
		
}
//method called when - onclick of the product Line cell
function displayProductByLine(e) {

    var selectedValue, selectedValueEOISFlag, selectedValueActiveFlag;

	selectedValue = gridProductLine.store.getValue(gridProductLine.getItem(e.rowIndex),"id");
	selectedValueEOISFlag = gridProductLine.store.getValue(gridProductLine.getItem(e.rowIndex),"EOISFlag");
	selectedValueActiveFlag = gridProductLine.store.getValue(gridProductLine.getItem(e.rowIndex),"ActiveFlag");	
	
	console.log("selected product Line selectedValue:"+selectedValue);	
	console.log("selected product Line selectedValueEOISFlag:"+selectedValueEOISFlag);
	console.log("selected product Line selectedValueActiveFlag:"+selectedValueActiveFlag);
	
	//hidePane("pane4");
	
    if (selectedValue !== -1 && selectedValue !== "") {
		getProductByLine(selectedValue,e,selectedValueEOISFlag,selectedValueActiveFlag);
	}
	
}
//method called to display the product based on product Line
function getProductByLine(productLine, e, selectedValueEOISFlag, selectedValueActiveFlag) {

    var grid, status, locale, jsonStore, gotItems;
	
	grid = dijit.byId("gridProduct");	
	
	document.getElementById("productGrid").style.visibility = "visible";
	document.getElementById("loadTextForProduct").style.display = "none";
	document.getElementById("loadEmptyTextForProduct").style.display = "none";   				   				
	document.getElementById("loadMessageForProduct").style.display = "none";	
	
	//code to check whether the eois is selected or not
	status='active';
	locale=javeLocale;
	
	//hiding the fourth pane if shown to the user
	hidePane("pane4");
	
    if (dojo.byId("eoisproducts").checked) {
        status = 'eois';
	}
	
    if ((status == "active" && selectedValueActiveFlag == "Y") || (status == "eois" && selectedValueEOISFlag == "Y")) {

        //code to call the server side and form the productLine.JSON file
        jsonStore = new dojo.data.ItemFileReadStore({
            url: "/p/ProductSelectionServlet/productselection?level=3&parentid=" + productLine + "&status=" + status + "&locale=" + locale
        });		
		
	gotItems = function(items, request){
	    console.log("Number of items loaded: " + items.length);
        if (items.length === 0) {	
				
		clearSelectedCell(grid,2);		
		
		document.getElementById("loadTextForProduct").style.display = "none";
		document.getElementById("loadEmptyTextForProduct").style.display = "none";   				   			
		//document.getElementById("productGrid").style.display = "none";
		document.getElementById("productGrid").style.visibility = "hidden";
		document.getElementById("loadMessageForProduct").style.display = "block";
            }
            else {
            
		document.getElementById("loadTextForProduct").style.display = "none";
		document.getElementById("loadEmptyTextForProduct").style.display = "none";   			
		document.getElementById("loadMessageForProduct").style.display = "none";   			
		//document.getElementById("productGrid").style.display = "block";
		
		if(document.getElementById("productGrid").style.visibility != "visible"){			                    	                    	
			document.getElementById("productGrid").style.visibility = "visible";			                                    
		}
		clearSelectedCell(grid,2);		    
	    }
	};		
		
	grid.setStore(jsonStore);				
		
	grid.store.fetch({serverQuery: {start: 0,count: 1},onComplete: gotItems});		
	grid.startup();		
		
	}
    else {
		clearSelectedCell(grid,2);		
		
		document.getElementById("loadTextForProduct").style.display = "none";
		document.getElementById("loadEmptyTextForProduct").style.display = "none";   				   			
		//document.getElementById("productGrid").style.display = "none";
		document.getElementById("productGrid").style.visibility = "hidden";
		document.getElementById("loadMessageForProduct").style.display = "block";

	}
	
	//document.getElementById("pane3").style.left = "259px";	
	dojo.byId('rootDiv').style.height = widgetHeight + 232 + 'px';
			
	//sliding the component
	//slideComponent("pane3");	
	
	selectedLevel = 3;

}
//method called when - onclick of the product cell
function displayProductLinks(e) {
    var productItem, gridProductStore, gridProductFamilyStore, selectedProductFamily, selectedProductLine,selectedProductFamilyId, selectedProductLineId, selectedProductValue, selectedValue, panel4InnerText, linkExistFlag, supportURL, warrantyURL, langCode, downloadInd, linkURL, finalOmnitureCode,offsetHeight,linksHeaderHeight;
	
	dojo.query(".dojoxGridCellFocus").removeClass("dojoxGridCellFocus");
	console.log("Calling displayProductLinks");
	productItem = gridProduct.getItem(e.rowIndex);
	gridProductStore = gridProduct.store;
	selectedValue = gridProductStore.getValue(productItem,"id");
	console.log("selected product:"+selectedValue);
	
	//getting the selected product family and line
	selectedProductFamily = getSelectedProductFamily("name");
	selectedProductLine = getSelectedProductLine("name");	
	selectedProductValue = gridProductStore.getValue(productItem,"name");
	
	//getting the selected product family and line ids
	selectedProductFamilyId = getSelectedProductFamily("id");
	selectedProductLineId = getSelectedProductLine("id");	
	
	console.log("selected product family id:"+selectedProductFamilyId);
	console.log("selected product line id:"+selectedProductLineId);
	
	gridProductFamilyStore = gridProductFamily.store;
	
	dojo.byId("pane4").innerHTML = "";
	
	panel4InnerText = "";	
	
	dojo.requireLocalization("dojoc.productselector","locale_ps",null,"en,en-us,zh,zh-cn,ROOT,jp,ar-ar,ar-xr,cs-cz,de-de,es-es,es-la,es-xl,hu-hu,fr-fr,he-il,id-id,it-it,ja-jp,ko-kr,nl-nl,pl-pl,pt-br,ru-ru,sv-se,th-th,tr-tr,uk-ua,vi-vn,zh-tw");
	this.i18n=dojo.i18n.getLocalization("dojoc.productselector","locale_ps",this.lang);

	
        panel4InnerText += "<div id= 'productlinksheader' class='listhead'>"+this.i18n.gridproductlinksheader+"</div>";
        panel4InnerText += "<div class='support-productlinkarea'><h4 class='support-linkh4header'>" + gridProductStore.getValue(productItem, "name") + "</h4><ul class='support-ul'>";
                
        linkExistFlag = false;
	supportURL = gridProductStore.getValue(productItem, "supportURL");
	console.log("supportURL=" + supportURL);
    if (supportURL) {
        finalOmnitureCode = formOmnitureCode(selectedProductFamily, selectedProductLine, selectedProductValue, "se_cust01", "::PAGE_NAME::prod_sel_supp_link");
	    
	    panel4InnerText += "<li class='support-li'><a onClick=\""+finalOmnitureCode+"\" href='" + supportURL + "'>"+this.i18n.productlinksproduct+"</a></li>";
	    linkExistFlag = true;
	}

	downloadInd = gridProductStore.getValue(productItem, "downloadInd");
	langCode = gridProductStore.getValue(productItem, "langCode");
	console.log("downloadInd=" + downloadInd);
    if (downloadInd && downloadInd === "Y") {
        finalOmnitureCode = formOmnitureCode(selectedProductFamily, selectedProductLine, selectedProductValue, "se_cust02", "::PAGE_NAME::prod_sel_driver_link");
	    
	    linkURL = "http://downloadcenter.intel.com/Product_Filter.aspx?ProductID=" + selectedValue + "&lang="+langCode+ "&FamilyId="+selectedProductFamilyId+ "&LineId="+selectedProductLineId;	    
	    panel4InnerText += "<li class='support-li'><a onClick=\""+finalOmnitureCode+"\" href='" + linkURL + "'>"+this.i18n.productlinksdownloaddriver+"</a></li>";
	    linkExistFlag = true;
	}	
	
	warrantyURL = getSelectedProductFamily("warrantyURL");

	console.log("warranty URL "+warrantyURL);
	
    if (warrantyURL) {
        finalOmnitureCode = formOmnitureCode(selectedProductFamily, selectedProductLine, selectedProductValue, "se_cust03", "::PAGE_NAME::prod_sel_warranty_link");

	    //console.log("final Warranty URL is "+warrantyURL);
	    panel4InnerText += "<li class='support-li'><a onClick=\""+finalOmnitureCode+"\" href='" + warrantyURL + "'>"+this.i18n.productlinksWarranty+"</a></li>";
	    linkExistFlag = true;
	}
	
	panel4InnerText += "</ul></div>";
	
	dojo.byId("pane4").innerHTML  = panel4InnerText;
	
    if (linkExistFlag) {
		showPane("pane4");
		dojo.byId('scroller').scrollLeft = '290';
		
			dojo.byId('rootDiv').style.height = widgetHeight + 250 +'px';		
		
		document.getElementById("pane4").style.display = "block";				
		
		//sliding the component		
        if (browserType !== 'ie') {
			//document.getElementById("pane4").style.left = "518px";							
			//slideComponent("pane4");		   
	  	}		
	}
    else {
		hidePane("pane4");
		dojo.byId('rootDiv').style.height = widgetHeight + 232 + 'px';
	}
	
	
	selectedLevel = 4;
	offsetHeight = document.getElementById("productlinksheader").offsetHeight;
	linksHeaderHeight = offsetHeight-23;	
	document.getElementById("productlinksheader").style.height = 22 + linksHeaderHeight + "px";
	
}
//method called to hide a pane
function showPane(pane) {
    dojo.byId("splitContainer").style.width = "1040px";
	dojo.byId(pane).sizeMin = "258px";
	dojo.byId(pane).sizeShare = "258px";
	dojo.byId(pane).style.width = "258px";
}
//method called to show a pane
function hidePane(pane) {
    dojo.byId(pane).innerHTML = "";
	dojo.byId("splitContainer").style.width = "780px";
	dojo.byId(pane).sizeMin = "0";
	dojo.byId(pane).sizeShare = "0";
	dojo.byId(pane).style.width = "0";
}
//method called to reset the pane
function resetPane(divTagName) {
    document.getElementById(divTagName).style.display = "block";
	
}
//method called when the check box for the discontinued products is checked or unchecked
function showDiscontinuedProducts() {

    var status, locale, jsonStore, grid, productFamily, productLine, productFamilyEOISFlag, productFamilyActiveFlag, productLineEOISFlag, productLineActiveFlag, gotItems;

    //hiding the last pane
    hidePane("pane4");

    //calling startoveragain method to reset all the panes
    //startOverAgain();	
    status = 'active';
    locale = javeLocale;

    //code to check whether the eois is selected or not
    if (dojo.byId("eoisproducts").checked) {
        status = 'eois';
    }

    if (selectedLevel === 1) {
        //code to call the server side and form the productLine.JSON file
        jsonStore = new dojo.data.ItemFileReadStore({
            url: "/p/ProductSelectionServlet/productselection?level=1&locale=" + locale + "&status=" + status
        });
        grid = dijit.byId("gridProductFamily");
        grid.setStore(jsonStore);
        
        grid.startup(); 

	clearSelectedCell(grid,1);
		
    }	

    if (selectedLevel === 2) {
        productFamily = getSelectedProductFamily("id");
        productFamilyEOISFlag = getSelectedProductFamily("EOISFlag");
        productFamilyActiveFlag = getSelectedProductFamily("ActiveFlag");

        console.log("product family name :" + productFamily);
        console.log("product family productFamilyEOISFlag :" + productFamilyEOISFlag);
        console.log("product family productFamilyActiveFlag :" + productFamilyActiveFlag);

        grid = dijit.byId("gridProductLine");

        if ((status == "active" && productFamilyActiveFlag == "Y") || (status == "eois" && productFamilyEOISFlag == "Y")) {
        
            document.getElementById("productLineGrid").style.visibility = "visible";
            document.getElementById("loadTextForproductLine").style.display = "none";
            document.getElementById("loadMessageForProductLine").style.display = "none";

            jsonStore = new dojo.data.ItemFileReadStore({
                url: "/p/ProductSelectionServlet/productselection?level=2&parentid=" + productFamily + "&status=" + status + "&locale=" + locale
            });

            gotItems = function (items, request) {
                console.log("Number of items loaded: " + items.length);
                if (items.length === 0) {
                    console.log("data not available for this status.");

                    clearSelectedCell(grid,1);

                    //document.getElementById("productLineGrid").style.display = "none";
                    document.getElementById("productLineGrid").style.visibility = "hidden";
                    document.getElementById("loadTextForproductLine").style.display = "none";
                    document.getElementById("loadMessageForProductLine").style.display = "block";

                }
                else {
                    document.getElementById("loadTextForproductLine").style.display = "none";
                    document.getElementById("loadMessageForProductLine").style.display = "none";
                    //document.getElementById("productLineGrid").style.display = "block";
                    
                    if(document.getElementById("productLineGrid").style.visibility != "visible"){                    
                    	
                    	document.getElementById("productLineGrid").style.visibility = "visible";
                    
                    }

                    clearSelectedCell(grid,1);
                }
            };            
            
            grid.setStore(jsonStore);
            grid.store.fetch({serverQuery: {start: 0,count: 1},onComplete: gotItems});                      
            grid.startup(); 
            
        }
        else {
            console.log("data not available for this status.");

            clearSelectedCell(grid,1);

            //document.getElementById("productLineGrid").style.display = "none";
            document.getElementById("productLineGrid").style.visibility = "hidden";
            document.getElementById("loadTextForproductLine").style.display = "none";
            document.getElementById("loadMessageForProductLine").style.display = "block";
        }

    }

    if (selectedLevel === 3 || selectedLevel === 4) {
        productLine = getSelectedProductLine("id");
        productLineEOISFlag = getSelectedProductLine("EOISFlag");
        productLineActiveFlag = getSelectedProductLine("ActiveFlag");

        console.log("product family name :" + productLine);
        console.log("product family productFamilyEOISFlag :" + productLineEOISFlag);
        console.log("product family productFamilyActiveFlag :" + productLineActiveFlag);

        grid = dijit.byId("gridProduct");

        if ((status == "active" && productLineActiveFlag == "Y") || (status == "eois" && productLineEOISFlag == "Y")) {


		document.getElementById("productGrid").style.visibility = "visible";
		document.getElementById("loadTextForProduct").style.display = "none";
		document.getElementById("loadEmptyTextForProduct").style.display = "none";   				   				
		document.getElementById("loadMessageForProduct").style.display = "none";

            	jsonStore = new dojo.data.ItemFileReadStore({
                	url: "/p/ProductSelectionServlet/productselection?level=3&parentid=" + productLine + "&status=" + status + "&locale=" + locale
            	});


		gotItems = function(items, request){
	   	 console.log("Number of items loaded: " + items.length);
        	if (items.length === 0) {	
				
			clearSelectedCell(grid,2);		
		
			document.getElementById("loadTextForProduct").style.display = "none";
			document.getElementById("loadEmptyTextForProduct").style.display = "none";   				   			
			//document.getElementById("productGrid").style.display = "none";
			document.getElementById("productGrid").style.visibility = "hidden";
			document.getElementById("loadMessageForProduct").style.display = "block";
            	}
            	else {
            
			document.getElementById("loadTextForProduct").style.display = "none";
			document.getElementById("loadEmptyTextForProduct").style.display = "none";   			
			document.getElementById("loadMessageForProduct").style.display = "none";   			
			//document.getElementById("productGrid").style.display = "block";
		
			if(document.getElementById("productGrid").style.visibility != "visible"){			                    	                    	
				document.getElementById("productGrid").style.visibility = "visible";			                                    
			}
			clearSelectedCell(grid,2);		    
	    	}
		};		
		
		grid.setStore(jsonStore);				
		
		grid.store.fetch({serverQuery: {start: 0,count: 1},onComplete: gotItems});		
		grid.startup();	
 
        }
        else {

   		console.log("data not available for this status.");
   			
   		clearSelectedCell(grid,2);
   			
   		document.getElementById("loadTextForProduct").style.display = "none";
   		document.getElementById("loadEmptyTextForProduct").style.display = "none";   			
   		//document.getElementById("productGrid").style.display = "none";
   		document.getElementById("productGrid").style.visibility = "hidden";
   		document.getElementById("loadMessageForProduct").style.display = "block";
   		
   		}		
	}
}
//method to get the selected product family
function getSelectedProductFamily(attr) {
    var retutnValue, items, value;

    retutnValue = "";
    items = gridProductFamily.selection.getSelected();

    if (items.length) {
        // Iterate through the list of selected items.
        // The current item is available in the variable
        // "selectedItem" within the following function:
        dojo.forEach(items, function (selectedItem) {
            if (selectedItem !== null) {
                // Iterate through the list of attributes of each item.
                // The current attribute is available in the variable
                // "attribute" within the following function:
                dojo.forEach(gridProductFamily.store.getAttributes(selectedItem), function (attribute) {
                    // Get the value of the current attribute:
                    value = gridProductFamily.store.getValues(selectedItem, attribute);
                    if (attribute === attr) {
                        retutnValue = value;
                    }

                }); // end forEach
            } // end if
        }); // end forEach
    } // end if
    return retutnValue;
}
//method to get the selected product Line
function getSelectedProductLine(attr) {
    var retutnValue, items, value;

    retutnValue = "";
    items = gridProductLine.selection.getSelected();
    console.log("item is " + items.length);

    if (items.length) {
        // Iterate through the list of selected items.
        // The current item is available in the variable
        // "selectedItem" within the following function:
        dojo.forEach(items, function (selectedItem) {
            if (selectedItem !== null) {
                // Iterate through the list of attributes of each item.
                // The current attribute is available in the variable
                // "attribute" within the following function:
                dojo.forEach(gridProductLine.store.getAttributes(selectedItem), function (attribute) {
                    // Get the value of the current attribute:
                    value = gridProductLine.store.getValues(selectedItem, attribute);
                    if (attribute === attr) {
                        retutnValue = value;
                    }

                }); // end forEach
            } // end if
        }); // end forEach
    } // end if
    return retutnValue;
}
//method which forms the omniture string
function formOmnitureCode(productFamily, productLine, Product, event, waiid) {
    var returnStr, familyStr, lineStr, prod, pagename, urlParameters, friendlyURL, urlval, loopcnt, corefriendlyURLarray, corefriendlyURL, urlpartsarray, localecode, linksecondparam;

    familyStr = "" + productFamily;
    lineStr = "" + productLine;
    prod = "" + Product;

    familyStr = replaceAll(familyStr, "&reg;", "");
    lineStr = replaceAll(lineStr, "&reg;", "");
    prod = replaceAll(prod, "&reg;", "");

    pagename = location.href;
    urlval = "";
    urlParameters = pagename.split('?');
    friendlyURL = urlParameters[0];
    corefriendlyURLarray = friendlyURL.split('/p/');
    corefriendlyURL = corefriendlyURLarray[1];
    urlpartsarray = corefriendlyURL.split('/');
    localecode = urlpartsarray[0];

	for (loopcnt=1;loopcnt<urlpartsarray.length;loopcnt=loopcnt+1){
		urlval = urlval+ urlpartsarray[loopcnt];
	       	if (loopcnt +1 <= urlpartsarray.length-1) {
		 	urlval = urlval +":";
	       }
	}
	
	linksecondparam = urlval+":links";    

    waiid = replaceAll(waiid, "::PAGE_NAME::", urlval + ":");
    //alert(waiid);
    if (familyStr !== "" && familyStr.length > 99) {
        familyStr = familyStr.substring(0, 99);
    }
    if (lineStr !== "" && lineStr.length > 99) {
        lineStr = lineStr.substring(0, 99);
    }
    if (prod !== "" && prod.length > 99) {
        prod = prod.substring(0, 99);
    }
    returnStr = "waCustomLink (this,'" + urlval + ":links','o','wa_custom42=" + familyStr + "&wa_eCustom42=" + familyStr + "&wa_custom43=" + lineStr + "&wa_eCustom43=" + lineStr + "&wa_custom45=" + localecode + "&wa_eCustom45=" + localecode + "&wa_custom44=" + prod + "&wa_eCustom44=" + prod + "&wa_events=" + event + "&wa_iid=" + waiid + "');";

    return returnStr;
}
//which replaces all the occurences of the string
function replaceAll( str, from, to ) {

    var idx = str.indexOf( from );

    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }

    return str;
}
//method which does the sliding
function slideComponent(pane) {
    var slidePane2, slidePane3, slidePane4, slideDuration;

    slideDuration = 750;
    slidePane2 = {
        node: "pane2",
        duration: slideDuration,
        top: (dojo.coords("pane2").t).toString(),
        left: (dojo.coords("pane2").l + 259).toString(),
        unit: "px"
    };

    slidePane3 = {
        node: "pane3",
        duration: slideDuration,
        top: (dojo.coords("pane3").t).toString(),
        left: (dojo.coords("pane3").l + 259).toString(),
        unit: "px"
    };

    slidePane4 = {
        node: "pane4",
        duration: slideDuration,
        top: (dojo.coords("pane4").t).toString(),
        left: (dojo.coords("pane4").l + 259).toString(),
        unit: "px"
    };
    if (pane === "pane2") {
        dojo.query("#" + pane + " .dojoxGridScrollbox").addClass("noscroll");
        dojo.fx.slideTo(slidePane2).play();
    }
    else if (pane === "pane3") {
        dojo.query("#" + pane + " .dojoxGridScrollbox").addClass("noscroll");
        dojo.fx.slideTo(slidePane3).play();
    }
    else if (pane === "pane4") {
        dojo.query("#" + pane + " .support-productlinkarea").addClass("noscroll");
        dojo.fx.slideTo(slidePane4).play();
    }
    setTimeout('addScroll(\"' + pane + '\")', slideDuration);
}
//method which removes the slide at the run time
function addScroll(pane) {
    if (pane === "pane2") {
        dojo.query("#" + pane + " .dojoxGridScrollbox").removeClass("noscroll");
    }
    else if (pane === "pane3") {
        dojo.query("#" + pane + " .dojoxGridScrollbox").removeClass("noscroll");
    }
    else if (pane === "pane4") {
        dojo.query("#" + pane + " .support-productlinkarea").removeClass("noscroll");
    }

}
//function to clear the selected cell value
function clearSelectedCell(grid,level){

    var prevSelectedValue;
    
    dojo.query(".dojoxGridCellFocus").removeClass("dojoxGridCellFocus");
    
    if(grid.selection != null){   

    	prevSelectedValue = grid.selection.selectedIndex;
    	console.log("prevSelectedValue " + prevSelectedValue);

    	if (prevSelectedValue != -1) {
    	    console.log("selected product family index " + grid.selection.selectedIndex);
    	    //grid.focus.focusHeader();
    	    grid.selection.deselect(grid.selection.selectedIndex);        
    	    grid.scrollToRow(0);
    	    grid.focus.focusHeader();  	    
    	    
    	}    	    	
    	   	
    	//getting the focus back to the grid
    	if(level == 1)
    	{
    		console.log("reverting the focus back for the level"+level); 
    		var gridToFocus1 = dijit.byId("gridProductFamily");
    		gridToFocus1.focus.focusGrid();
    	}
    	if(level == 2)
    	{
    		console.log("reverting the focus back for the level"+level); 
    		var gridToFocus2 = dijit.byId("gridProductLine");
    		gridToFocus2.focus.focusGrid();
    	}
    	if(level == 3)
    	{
    		console.log("reverting the focus back for the level"+level); 
    		var gridToFocus3 = dijit.byId("gridProduct");
    		gridToFocus3.focus.focusGrid();
    	}
    	    
    }
           
}

//method which sets the text displayed for the passed locale
function setTextBasedOnLocale(locale) {
    var name;
    dojo.requireLocalization("dojoc.productselector", "locale_ps");
    name = dojo.i18n.getLocalization("dojoc.productselector", "locale_ps");

    //alert(name.findproduct);
    document.getElementById("support-browse4ps").innerHTML = name.findproduct;
}
//method which converts the java/browser locale to dojo locale
function convertJavaBrowserLocaleToDojoLocale(locale) {
    var formattedLocale;

    //alert("locale passed "+locale);
    return formattedLocale;
}


function productSelectorKeyPress(e) {
    
    if(e.keyCode == 38 || e.keyCode == 40) // Up & Down Arrows
    {	
	dojo.query(".dojoxGridCellFocus").addClass("psKeyUpDown");		
	dojo.query(".dojoxGridCellOver").removeClass("psMouseOver");	
	dojo.query(".dojoxGridCellOver").removeClass("dojoxGridCellOver");
	dojo.query(".dojoxGridCellOver").addClass("defaultcell");
	dojo.query(".dojoxGridCellFocus").removeClass("psMouseOver");	
    }	
}

function productSelectorCellMouseOver(e) {

    if(mouseout == 1){    	
    	//alert("rowIndex : " + e.rowIndex + " -- colIndex : " + e.cell.index);
    	dojo.query(".dojoxGridCellOver").addClass("psMouseOver");    
    	dojo.query(".dojoxGridCellOver").removeClass("psKeyUpDown");
    	dojo.query(".psMouseOut").removeClass("psKeyUpDown"); 
    	dojo.query(".dojoxGridCellOver").removeClass("psMouseOut");
    	mouseout = 0;
    }
}

function productSelectorCellMouseOut(e) {
    //alert("mouse out");    
    dojo.query(".dojoxGridCell").removeClass("psMouseOver");
    dojo.query(".dojoxGridCellFocus").addClass("psMouseOut");
    mouseout = 1;
}
