function CallBusToFillCombo(BasePath,BusName,ParentID,Control_id,Lable)
{
    var sURL;
    sURL=BasePath+"/DataBus.aspx?ParentID="+ParentID+"&BusName="+BusName+"&Lable=" + Lable;
  //alert(sURL);
	makeAjaxRequest(sURL,"FillCombo('"+Control_id+"',XML_OBJECT)");
}
/////////////////////////////////////////////////////////////////////////////////////////
function FillCombo(Control_id,xmlObj)
{
//alert(Control_id);
//var Control_id="ddlSubCategory";
	var oCombo = document.getElementById(Control_id);
	
	if (oCombo!=null){
	    for (var i=oCombo.options.length;i>=0;i--)
		    oCombo.options[i] = null;
    		
	    var oCombo = document.getElementById(Control_id);
    	
	    var busData = xmlObj.selectNodes("//BusRows/BusRow");
    	if (busData!=null){//alert(busData.length);
            //alert("BusRows" + getSingleNode(getNode(busData,0),"Name"));
            for (var i=0;i<busData.length;i++){ //alert();
                var sText = getSingleNode(getSingleNodeByPosition(busData,i),"Name");
                var sValue = getSingleNode(getSingleNodeByPosition(busData,i),"ID");
           
                //alert("sText=" + sText + "  sText=" + sValue );
                if (sText!=null && sValue !=null && sText!='' && sValue !=''){
                 //alert(i + i);
	                oCombo.options[oCombo.options.length] = new Option(sText,sValue);
	            }
            }
	    }
	}
}
//////////////////////////////////////////////////////////////////////////////////////////
function CallBusToFillProductDetails(BasePath,BusName,ParentID,Control_Tag_CSV)
{
    var sURL;
    sURL=BasePath+"/DataBus.aspx?ParentID="+ParentID+"&BusName="+BusName;
  //alert("sURL" + sURL);
	makeAjaxRequest(sURL,"FillProductDetails('"+Control_Tag_CSV+"',XML_OBJECT)");
}
//////////////////////////////////////////////////////////////////////////////////////////
function FillProductDetails(Control_Tag_CSV,xmlObj)//FillRepeter(Repeater_control_id, Control_id_csv,PageNo,PageNo_CtrlID,xmlObj)
{
    var busData = xmlObj.selectNodes("//BusRows/BusRow");
    //alert(busData.length);
    if (busData!=null){
   //alert(Control_Tag_CSV);
        var oRow = getSingleNodeByPosition(busData,0);
        if (oRow !=null){
            FillDetailsFromCSV(Control_Tag_CSV, oRow);
        }
    }
}
//////////////////////////////////////////////////////////////////////////////////////////
function FillDetailsFromCSV(Control_Tag_CSV, DataRow)
{
    // alert("->" + Control_Tag_CSV);
    var sControlCSVArray = Control_Tag_CSV.split(","); 
    for (var j=0;j<sControlCSVArray.length;j++){
        //alert("sControlCSVArray[j]:" + sControlCSVArray[j]);
        var arrCtrlAndTag = sControlCSVArray[j].split(':');
        //alert("arrCtrlAndTag[1] : " + arrCtrlAndTag[1]);
        if (arrCtrlAndTag !=null && arrCtrlAndTag.length==2){
         //   if (DataRow!=null){
                 var sValue = getSingleNode(DataRow,arrCtrlAndTag[1]);
                 var oCtrl = document.getElementById(arrCtrlAndTag[0]);
                 //alert("sValue:" + sValue);
                if (oCtrl!=null ){
                    if (oCtrl.id.indexOf('imgWineBig') > -1){
                        oCtrl.src=sValue;
                    }
                    else{
                        //alert(sValue);
                        oCtrl.innerHTML = sValue;
                    }
                }
         //   }
        }
    }
}
//////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////// For display oreder /////////////////////////////////

function CallBusToUpdateDisplayOrder(BasePath,PageID,MediaID,direction,ItemIndex, TotalItems)
{//alert("Test");
    //alert("BasePath:" + BasePath + "\n PageID:" + PageID + "\n direction:" + direction + "\n ItemIndex:" + ItemIndex + "\n TotalItems:" + TotalItems);
    //alert('yes');
    var GroupID = document.getElementById("FieldGroupID" + ItemIndex).value;
    var SwapWithItemIndex = 0;
    if(direction == "up")    
        SwapWithItemIndex = ItemIndex-1;
    else if(direction == "down")    
        SwapWithItemIndex = ItemIndex+1;
    SwapWithGroupID = document.getElementById("FieldGroupID" + SwapWithItemIndex).value;
    //alert('GroupID:'+GroupID+',MediaID:'+MediaID+',Dir:'+direction+',Index:'+ItemIndex+',PageID:'+PageID+',SwapWithGroupID:'+SwapWithGroupID);
    var sURL;   
    if(GroupID!=null)
    {
        if(GroupID!="")
        {
            //alert(direction);
            sURL=BasePath+"/UpdateBus.aspx?id="+PageID+"&Group_ID="+GroupID+"&Media_ID="+MediaID+"&SwapWithGroup_ID="+SwapWithGroupID+"&BusName="+direction;
            //alert("URL:" + sURL + "\n Dir:" + direction);
            makeAjaxRequest(sURL,"ReplaceRows(XML_OBJECT,'" + direction +"'," + ItemIndex + "," +  TotalItems + ")");
            
        }   
	 }
	 else
	 {
	    alert("Unable to update display order");
	 }
	 //window.opener.location.reload();
}

function ReplaceRows(xmlObj,direction,ItemIndex,TotalItems)
{
    //alert("xmlObj:" + xmlObj + "\n direction:" + direction + "\n ItemIndex:" + ItemIndex + "\n TotalItems:" + TotalItems);
    //alert('yes');
    if (direction=="up")
    {
        if (ItemIndex > 0)
        { 
            for(var i = 1; i<5; i++)
            {       //alert(i.toString());        
                var oCtrlTop = this.document.getElementById('Field' + i + ItemIndex);    
                //alert(oCtrlTop);
                var iNextItemIndex = parseInt(ItemIndex) - 1;                
                var oCtrlNext =this.document.getElementById('Field'+ i + iNextItemIndex);   
                
                //alert("field" + i.toString() + ItemIndex);         
                //alert("field"+ i.toString() + iNextItemIndex.toString());    
                ReplaceValues(oCtrlTop,oCtrlNext);
            }
            var oCtrlHdnTop = document.getElementById("FieldGroupID" + ItemIndex);
            var oCtrlHdnNext = document.getElementById("FieldGroupID" + iNextItemIndex);            
            //alert('BeforeSwap-' + oCtrlHdnTop.value + ':' + oCtrlHdnNext.value);
            ReplaceHdnValues(oCtrlHdnTop,oCtrlHdnNext); 
            //alert('AfterSwap-' + oCtrlHdnTop.value + ':' + oCtrlHdnNext.value); 
        }
    }
    else //for down
    { 
        if (ItemIndex < TotalItems)
        {   
            for(var i = 1; i<5 ; i++)
            {
                //alert('yes');                 
                var oCtrlTop = document.getElementById("Field" + i + ItemIndex);               
                var iNextItemIndex = parseInt(ItemIndex) + 1;
                var oCtrlNext =document.getElementById("Field"+ i + iNextItemIndex);
                ReplaceValues(oCtrlTop,oCtrlNext);
            }
            var oCtrlHdnTop = document.getElementById("FieldGroupID" + ItemIndex);
            var oCtrlHdnNext = document.getElementById("FieldGroupID" + iNextItemIndex);            
            //alert('BeforeSwap-' + oCtrlHdnTop.value + ':' + oCtrlHdnNext.value);
            ReplaceHdnValues(oCtrlHdnTop,oCtrlHdnNext); 
            //alert('AfterSwap-' + oCtrlHdnTop.value + ':' + oCtrlHdnNext.value);           
        }
    }    
}

function ReplaceValues(oCtrlTop,oCtrlNext)
{
    //alert("oCtrlTop:" + oCtrlTop.innerHTML + "\n oCtrlNext:" + oCtrlNext.innerHTML);
    //alert('yes');
    var sTempInnerHtml = oCtrlTop.innerHTML;
    oCtrlTop.innerHTML = oCtrlNext.innerHTML;
    oCtrlNext.innerHTML = sTempInnerHtml;
}
function ReplaceHdnValues(oCtrlTop,oCtrlNext)
{
    //alert("oCtrlTop:" + oCtrlTop.innerHTML + "\n oCtrlNext:" + oCtrlNext.innerHTML);
    //alert('yes');
    var sTempValue = oCtrlTop.value;
    oCtrlTop.value = oCtrlNext.value;
    oCtrlNext.value = sTempValue;
}
////////////////////////////
var iStatus = 2;
function validateCaptcha(BasePath,elem,type)
{
    iStatus = 2;
    
    if (typeof(elem) == 'string')
        elem = document.getElementById(elem);
    if(elem == null || elem == 'null')
        return false;
    else
    {
        var sURL;
        sURL=BasePath+"/DataBus.aspx?text=" + elem.value + "&BusName=captcha&type=" + type;
        //alert(sURL);
	    makeAjaxRequest(sURL,"checkStatus(XML_OBJECT)");
        
        if(iStatus == 1){
            return true;
        }
        else{
            return false;
        }
    }
}

function checkStatus(xmlObj)
{
    var busData = xmlObj.selectNodes("//BusRows/BusRow");    	
    if (busData!=null){  //alert('busData.length:'+busData.length);
        //var oRow = getNode(busData,0);
        //if (oRow !=null){
        if(busData.length > 0){
            //var sState = getSingleNode(getNode(busData,0),"State");            
            iStatus = 1;
        }
    }
    if(iStatus != 1)
        iStatus = 0;
}



///////////////////////////
function GetValueByProductAndCompany(BasePath,sCompany, sProduct)
{
    var sURL;
     //alert("company :" + sCompany);
     //alert("product :" + sProduct);
    sURL=encodeURI(BasePath+"/DataBus.aspx?BusName=GetValueByProductAndCompany&lable1=" + sCompany + "&lable2="+ sProduct);
    //alert(sURL);
    makeAjaxRequest(sURL,"setValuesToControls(XML_OBJECT)");
    if(iStatus == 1){
        return true;
    }
    else{
        return false;
    }
}
function GetCompanyListByProductID(BasePath, sProduct,sCompanyId)
{
    var sURL;
     //alert("company :" + sCompany);
     //alert("product :" + sProduct);
    sURL=encodeURI(BasePath+"/DataBus.aspx?BusName=GetCompanyListByProductID&lable1=" + sProduct+ "&lable2="+sCompanyId);
    //alert(sURL);
    makeAjaxRequest(sURL,"setComaplyValuesToDropdown(XML_OBJECT)");
 
}
function setComaplyValuesToDropdown(xmlObj)
{
    var busData = xmlObj.selectNodes("//BusRows/BusRow");    	
    var elemAllComapny = getE('divAllCompany');
    if (busData!=null){
       if(busData.length > 0){
       if(elemAllComapny.innerHTML != "")
        {
          elemAllComapny.innerHTML = "";
        }
        for (var i=0;i<busData.length;i++){
          var sCompanyName = getSingleNode(getSingleNodeByPosition(busData,i),"CompanyName");
          var sCompanyId = getSingleNode(getSingleNodeByPosition(busData,i),"CompanyID");
          var sInneDiv =  "<div id='divCompanyName" + [i] + "' onClick=\"javascript:assignValueToTextBox(this,'" + sCompanyId+ "')\">" + sCompanyName + "</div>";
          elemAllComapny.innerHTML += sInneDiv;
          sInneDiv = "";
        }
         if(elemAllComapny.style.display=="none")
        {
          elemAllComapny.style.display = "block";
        }
     }
     else 
     {
        elemAllComapny.innerHTML = ""; 
        if(elemAllComapny.style.display=="block")
        {
          elemAllComapny.style.display = "none";
        }
     }
   }
   else 
   {
    elemAllComapny.innerHTML = ""; 
     if(elemAllComapny.style.display=="block")
        {
          elemAllComapny.style.display = "none";
        }
    }
}

function setValuesToControls(xmlObj)
{
    var busData = xmlObj.selectNodes("//BusRows/BusRow");    	
    if (busData!=null){
      //alert('busData.length:'+busData.length);
     if(busData.length > 0){
        var elemDetails = getE('divInnerResult');
        var elemNoResult = getE('divNoResult');
        var elemComment = getE('divComments');
        var elemGlutoneDivAvailable = getE('divGlutoneVisible');
        var elemGlutoneDivNotAvailable = getE('divGlutoneNotVisible');
        var elemSugarDivAvailable = getE('divSuagarVisible');
        var elemSugarDivNotAvailable = getE('divSuagarNotVisible');
       
        if (elemSugarDivAvailable!=null && elemSugarDivNotAvailable!=null && elemGlutoneDivAvailable!=null && elemGlutoneDivNotAvailable!=null && elemComment!=null){
        
             //alert("elemComment: " + elemComment.id);
             var oRow = getSingleNodeByPosition(busData,0);
             //alert(oRow);
             if (oRow !=null)
             {
           
                elemDetails.style.display = "block";
                elemDetails.style.visibility = "visible";
                elemNoResult.style.display = "none";
                elemNoResult.style.visibility = "hidden";  
                
                
                iStatus = 1;
                var sComment = getSingleNode(oRow,"Comment");
                var sGlutone = getSingleNode(oRow,"Glutone");
                var sSugar = getSingleNode(oRow,"Sugar");
                //setting comment
                elemComment.innerHTML = sComment;
                //alert("sComment: " + sSugar + "  sGlutone" + sGlutone);
                //setting glotone
                
                if (sGlutone.toLowerCase()=="false"){ // If valaue is false
                    elemGlutoneDivAvailable.style.display="none";
                    elemGlutoneDivAvailable.style.visibility="hidden";
                    elemGlutoneDivNotAvailable.style.display="block";
                    elemGlutoneDivNotAvailable.style.visibility="visible";
                }
                else{ //If value is true
                    elemGlutoneDivAvailable.style.display="block";
                    elemGlutoneDivAvailable.style.visibility="visible";
                    elemGlutoneDivNotAvailable.style.display="none";
                    elemGlutoneDivNotAvailable.style.visibility="hidden";
                }
                //For sugar
                if (sSugar.toLowerCase()=="false"){ // If valaue is false
                    elemSugarDivAvailable.style.display="none";
                    elemSugarDivAvailable.style.visibility="hidden";
                    elemSugarDivNotAvailable.style.display="block";
                    elemSugarDivNotAvailable.style.visibility="visible";
                }
                else{ //If value is true
                    elemSugarDivAvailable.style.display="block";
                    elemSugarDivAvailable.style.visibility="visible";
                    elemSugarDivNotAvailable.style.display="none";
                    elemSugarDivNotAvailable.style.visibility="hidden";
                }
              }
            }
            else
            {
                elemNoResult.style.display = "block";
                elemNoResult.style.visibility = "visible";  
                
            
                elemDetails.style.display = "none";
                elemDetails.style.visibility = "hidden";  
                
             iStatus = 0;
            }
        }
    }
//    if(iStatus != 1)
//        iStatus = 0;
}


///////////////////////////
