﻿// JScript File

//Methods for populating Master Data
    /*
    */
	function clearCookie(c_name)
    {
        var exdate=new Date();
        var expireInDays = -2;
        exdate.setDate(exdate.getDate() + expireInDays);
        document.cookie=c_name+ "=" +escape(value)+
        ((expireInDays==null) ? "" : ";expires="+exdate.toGMTString()) + (";path=" + "/");
    }
    function setCookie(c_name,value,expireInHours)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate());
        exdate.setHours(exdate.getHours() + expireInHours, exdate.getMinutes(), exdate.getSeconds(), exdate.getMilliseconds());
        //exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+
        ((expireInHours==null) ? "" : ";expires="+exdate.toGMTString()) + (";path=" + "/");
    }
    
    function Get_Cookie( check_name ) 
    {
	    // first we'll split this cookie up into name/value pairs
	    // note: document.cookie only returns name=value, not the other components
	    var a_all_cookies = document.cookie.split( ';' );
	    var a_temp_cookie = '';
	    var cookie_name = '';
	    var cookie_value = '';
	    var b_cookie_found = false; // set boolean t/f default f
    	
	    for ( i = 0; i < a_all_cookies.length; i++ )
	    {
		    // now we'll split apart each name=value pair
		    a_temp_cookie = a_all_cookies[i].split( '=' );
    		
    		
		    // and trim left/right whitespace while we're at it
		    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    	
		    // if the extracted name matches passed check_name
		    if ( cookie_name == check_name )
		    {
			    b_cookie_found = true;
			    // we need to handle case where cookie has no value but exists (no = sign, that is):
			    if ( a_temp_cookie.length > 1 )
			    {
				    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			    }
			    // note that in cases where cookie is initialized but no value, null is returned
			    return cookie_value;
			    break;
		    }
		    a_temp_cookie = null;
		    cookie_name = '';
	    }
	    if ( !b_cookie_found )
	    {
		    return null;
	    }
    }	
    /*
        Name: GetCountries_Callback
        Parameter: Regions collection
        Description: It populates country combo
    */
    function GetCountries(objCombobox, clsName)
    {
        var regions = QuickSearch.GetCountries();
        
        if (regions.error != null)
        {
            return;
        }
        
        try { masterCountries = regions; } catch(err) { } // This line of code only corresponds to l-search
        
        var combobox = document.getElementById(objCombobox);
        RemoveAll(objCombobox);
        
        //Special Case for ISango Affiliate
        if(Get_Cookie("isangoaffiliate") == null)
        {
                        if(Get_Cookie("language") != null && Get_Cookie("language").toLowerCase() == "en-us") //For US Customization--USA/Canada/Mexico are on top
            {
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7166), 7166)); //USA
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7228), 7228)); //Canada
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 6259), 6259)); //Mexico
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7028), 7028)); //France
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7030), 7030)); //Italy
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7157), 7157)); //Spain
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7156), 7156)); //United Kingdom
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 6169), 6169)); //Australia
            }
            else
            {
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 6169), 6169)); //Australia
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7028), 7028)); //France
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7663), 7663)); //Germany
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7030), 7030)); //Italy
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7157), 7157)); //Spain
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7156), 7156)); //United Kingdom
                combobox.options.add(new Option(getFrequentlyVisitedRegion(regions, 7166), 7166)); //USA
            }
            combobox.options.add(new Option("", -1));
        }
        
        for(var itemContinents = 0; itemContinents < regions.value.length; itemContinents++)
        {
            if(regions.value[itemContinents].RegionLevel == 1) // 1 for Continents
            { 
                combobox.options.add(new Option("-------------------", -1));                    
                combobox.options.add(new Option(regions.value[itemContinents].RegionName, -1));
                combobox.options[combobox.options.length - 1].className = clsName;
                
                for(var itemCountries = 0; itemCountries < regions.value.length; itemCountries++)
                {
                    if((regions.value[itemCountries].ParentRegionID == regions.value[itemContinents].RegionID) 
                        && (regions.value[itemCountries].RegionLevel == 2)) //2 for Countries
                       {
                            combobox.options.add(new Option(regions.value[itemCountries].RegionName, regions.value[itemCountries].RegionID));
                       }
                }
            }
        }
    }
    
    function getFrequentlyVisitedRegion(arr, regionID)
    {
        var _tmpRegionName = "";
        var regions = arr;
        if(regions != null)
        {
            for(var item = 0; item < regions.value.length; item++)
            {
                if(regions.value[item].RegionID == regionID)
                {
                    _tmpRegionName = regions.value[item].RegionName;
                    break;
                }
            }
        }
        return _tmpRegionName;
    }
    
    function GetRegions(regionId, objCombobox, clsName)
    { 
        var regions = QuickSearch.GetRegions(regionId);
        var hasChild = false;
        var index;
        if (regions.error != null)
        {
            return;
        }
        
        try { masterRegions = regions; } catch(err) { } // This line of code only corresponds to l-search
        
        var combobox = document.getElementById(objCombobox);
        RemoveAll(objCombobox);
        for(var itemRegions = 0; itemRegions < regions.value.length; itemRegions++)
        {
            hasChild = false;
            if(regions.value[itemRegions].RegionLevel == 3) // 3 for Cities/Regions
            { 
                combobox.options.add(new Option("-------------------", -1));                    
                combobox.options.add(new Option(regions.value[itemRegions].RegionName));
                combobox.options[combobox.options.length - 1].className = clsName;
                
                index = parseInt(combobox.options.length - 1);
                
                for(var itemSubRegions = 0; itemSubRegions < regions.value.length; itemSubRegions++)
                {
                    if((regions.value[itemSubRegions].ParentRegionID == regions.value[itemRegions].RegionID) 
                        && (regions.value[itemSubRegions].RegionLevel == 4)) //4 for Sub-Regions
                       {
                            hasChild = true;
                            combobox.options.add(new Option(regions.value[itemSubRegions].RegionName, regions.value[itemSubRegions].RegionID));
                       }
                }
//                if(hasChild)
//                {
//                    combobox.options[index].value = regions.value[itemRegions].RegionID;
//                }
//                else
//                {
                    combobox.options[index].value = regions.value[itemRegions].RegionID;
//                }
            }
        }
    }
    
   /*
        Name: GetActivityCategories
        Parameter: Activity Categories collection
        Description: It populates activity categories combo
    */
    function GetActivityCategories(objCombobox, clsName)
    {
        var activityCategories = QuickSearch.GetActivityCategories(); 
        
        if (activityCategories.error != null)
        {
            return;
        }
        
        try { masterActivityCategories = activityCategories; } catch(err) { } // This line of code only corresponds to l-search
        
        var combobox = document.getElementById(objCombobox);
        RemoveAll(objCombobox);
        for(var item = 0; item < activityCategories.value.length; item++)
        {
            combobox.options.add(new Option("-------------------", -1));                    
            combobox.options.add(new Option(activityCategories.value[item].Name, activityCategories.value[item].ID));
            combobox.options[combobox.options.length - 1].className = clsName;
            
            for(var subItems = 0; subItems < activityCategories.value[item].ActivityCategoryTypes.length; subItems++)
            { 
                combobox.options.add(new Option(activityCategories.value[item].ActivityCategoryTypes[subItems].Name, activityCategories.value[item].ActivityCategoryTypes[subItems].ID));
            }
        }
    }	
    
    function GetRegionWiseCategories(regionId, objCombobox, clsName)
    { 
        var activityCategories = QuickSearch.GetRegionWiseCategories(regionId, null);
       
        if (activityCategories.error != null)
        {
            return;
        }
        
        var combobox = document.getElementById(objCombobox);
        RemoveAll(objCombobox);
        for(var item = 0; item < activityCategories.value.length; item++)
        {
            combobox.options.add(new Option("-------------------", -1));                    
            combobox.options.add(new Option(activityCategories.value[item].Name, activityCategories.value[item].ID));
            combobox.options[combobox.options.length - 1].className = clsName;
            
            for(var subItems = 0; subItems < activityCategories.value[item].ActivityCategoryTypes.length; subItems++)
            { 
                combobox.options.add(new Option(activityCategories.value[item].ActivityCategoryTypes[subItems].Name, activityCategories.value[item].ActivityCategoryTypes[subItems].ID));
            }
        }
    }
    
    function GetRecommendations(regionId, objCombobox)
    { 
        var recommendations = LSearch.GetRecommendations(regionId);
       
        if (recommendations.error != null)
        {
            return;
        }
        
        var combobox = document.getElementById(objCombobox);
        RemoveAll(objCombobox);
        
        for(var item = 0; item < recommendations.value.length; item++)
        {
           combobox.options.add(new Option(recommendations.value[item].RecommendationName, recommendations.value[item].RecommendationID));
        }
    }
    
    function RemoveAll(objCombobox)
    {
        //Assuming selection text e.g. Select a Country, All Cities/Regions etc. is the first entry in the Combo box at index 0
        var combobox = document.getElementById(objCombobox);
        if(combobox != null)
        {
            var itemCount = parseInt(combobox.options.length);
            for(var items = 1;items < itemCount; items++)
            { 
                combobox.remove(1);
            }
        }
    }
    
    
