﻿var url="AjaxRequest.aspx";
var xmlHttp;
var i;
var requestURL ='AjaxRequest.aspx';
var is_ie =   (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
var is_ie5 =   (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0;
var is_opera =  ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0;  //netscape, safari, mozilla behave the same???
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;
			
var varGroupName = "";
var varOptionID = "";
var varOptionName = "";
var varAllOptions = "";
var ar = new Array(10)
var newOption;

function InitializeRequest()
{			
		//Creating object of XMLHTTP in Mozilla and Safari
	    request = "" 
	   
		if(!request && typeof XMLHttpRequest != "undefined") 
		{   
			request = new XMLHttpRequest();
		}
		else //Creating object of XMLHTTP in IE
		{
			try
			{
				request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch(oc)
				{
					request = null;
				}
			}
		}
		
}
		   
function GetVariations(VariationID,GroupName)
{	
    
       varOptionID= document.getElementById('ctl00_MainContent_'+GroupName).value;
       if(varOptionID!=0)
       {
            
           var w = document.getElementById('ctl00_MainContent_'+GroupName).selectedIndex;
           varOptionName = document.getElementById('ctl00_MainContent_'+GroupName).options[w].text;

           //Extra Lines of Code
           varAllOptions=document.getElementById("ctl00_MainContent_hdnOptions").length;
           var Exists=0;
        
           if(varAllOptions==0)
            {
                newOption =new Option(varOptionName,GroupName)
                document.getElementById("ctl00_MainContent_hdnOptions").options[0] = newOption;
                 
            }
            else
            {   
                for(var i=0;i<document.getElementById("ctl00_MainContent_hdnOptions").length;i++)
                {
                    if(document.getElementById("ctl00_MainContent_hdnOptions").options[i].text==varOptionName)
                    {   
                       Exists=1;
                    }
                }
                if(Exists==0)
                {
                alert(document.getElementById("ctl00_MainContent_hdnOptions").length);
                    newOption =new Option(varOptionName,GroupName)
                    document.getElementById("ctl00_MainContent_hdnOptions").options[i-1] = newOption;
                }
            }
         
            //////////////////////
           
    
            //status.innerText = "Loading.....";//Set the status to "Loading....."
           InitializeRequest();//Call InitializeRequest to set request object
           varGroupName=GroupName ;
            
           
	        //Create the url to send the request to
	        var url="AjaxRequest.aspx?ProductID=" + VariationID+"&OptionID=" +varOptionID;

	        //Delegate ProcessRequest to onreadystatechange property so 
	        //it gets called for every change in readyState value
	      
	        request.onreadystatechange = ProcessRequest;
        	
	        request.open("GET", url, true);//Open a GET request to the URL	

	        request.send(null);//Send the request with a null body.
    }
} 
function ProcessRequest()
{
	
		if (request.readyState == 4 || request.readyState == 'complete')
		{
	
	    //If the readyState is in the "Ready" state or If the returned status code was 200. 
		//Everything was OK.
	     var doc = request.responseXML;   // Assign the XML file to a var
       //  var element = doc.getElementsByTagName('OPTION_ID').item(1);   // Read the first element
         ClearAndSetVariations(request.responseXML.documentElement);

		}
		return true;//return
}

function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function ClearAndSetVariations(countryNode)
{
            
	for (LIntCtr=0; LIntCtr < document.forms[0].elements.length; LIntCtr++) 
	{
	    // Check if the control is a drop down variation.
		if ((document.forms[0].elements[LIntCtr].type == 'select-one'))
		{
		    /*********Convert the Ctrl Unique ID to Normal ID e.g if Ctrl Name is ctl00_Main_Content_Size
            ********* this will convert it into "Size" only. This helps us to make comparison***********/                        				        
		    var strCtrlId=document.forms[0].elements[LIntCtr].id;
		    var strCtrlId=strCtrlId.substring(strCtrlId.lastIndexOf("_")+1);
		    /*******************************************************************************************/
		    
		    // Here a comparison is made, so that the selected dropdown dont get refreshed
		   
		    if(strCtrlId != varGroupName)
		    {
    		    var selectedText;
    		    var selectedValue;
    		    selectedText = document.forms[0].elements[LIntCtr].options[document.forms[0].elements[LIntCtr].selectedIndex].text;
    		    selectedValue = document.forms[0].elements[LIntCtr].options[document.forms[0].elements[LIntCtr].selectedIndex].value;
		     	for (var count = document.forms[0].elements[LIntCtr].length-1; count >-1; count--)
            	{
            	    //Clearing all the values of all drop downs except the first option in all drop downs.
            	    if(count>0)
            	    {
            	        document.forms[0].elements[LIntCtr].options[count] = null;
            		}
            		
            	}
		     	//Get The Option Name and to which Group they belong.			    
				var VariationNodes = countryNode.getElementsByTagName('OPTION_NAME');
				var VariationOptionID=countryNode.getElementsByTagName('OPTION_ID');
				var VariationGroupName = countryNode.getElementsByTagName('OPTION_GROUP_NAME');
            	var textName;
            	var textValue; 
            	var optionItem;
            	//Add new options list to the group combo box.
            	for (var count = 0; count < VariationNodes.length; count++)
            	{
            	    textValue = GetInnerText(VariationNodes[count]);
            	    textName= GetInnerText(VariationOptionID[count]);
            		optionItem = new Option( textValue, textName,  false, false);
                  
                   //Only options related to a particular dropdown are attached. that's why a comparison is made.
                    
                        var GName= document.getElementById('ctl00_MainContent_'+GetInnerText(VariationGroupName[count]))
                  
                        if(strCtrlId==GetInnerText(VariationGroupName[count]))
                        {
                            document.forms[0].elements[LIntCtr].options[document.forms[0].elements[LIntCtr].length] = optionItem;
                        }
            		          
    		            for(var i=0;i<document.getElementById("ctl00_MainContent_hdnOptions").length;i++)
                        {
                         /*Eg Texture*/var OptionValue= document.getElementById("ctl00_MainContent_hdnOptions").options[i].value;
                        /*Eg Smooth*/var OptionText = document.getElementById("ctl00_MainContent_hdnOptions").options[i].text;
                            
                            if(OptionValue==GetInnerText(VariationGroupName[count]))
                            {
                                if(OptionText==textValue) 
                                {
                                    newOption =new Option("Select" +GetInnerText(VariationGroupName[count])  ,"0")
                                    GName.options[0] =newOption;
                                   //  document.getElementById(GName).options[0].add(newOption);
                                        if(GName.options[GName.selectedIndex].text != OptionText)
                                        {
                                           //     GName.options[GName.selectedIndex].text = OptionText;//varOptionName; 
                                            GName.options[GName.length-1].selected=true
                                        }
                                }
                            }
                        }
                  }
//                        var isSet = 0;
//		     	        for (var j=0; j<document.forms[0].elements[LIntCtr].length-1; j++)
//            	        {
//            	            if(isSet != 1)
//            	            {
//            	                if (document.forms[0].elements[LIntCtr].options[j].text == selectedtext)
//            	                {
//            	                document.forms[0].elements[LIntCtr].options[j].selected = true;
//            	                isSet = 1;
//            	                }
//            		        }
//                    		
//            	        }
//                        
             }
		}
	}
	



}
//End of the function.