﻿/*
*****************************************************************************
*javascript for the search pages                                            *
*****************************************************************************
*
*****************************************************************************
*                          Comments Section                                 *
*****************************************************************************
*Name               Date        Job Code    Details                         *
*----               ----        ---------   -------                         *
*Paul Davidson      26/03/2009  T-AME001    creation date                   *
*                                                                           *
*                                                                           *
*                                                                           *
*                                                                           *
*                                                                           *
*                                                                           *
*                                                                           *
*                                                                           *
*****************************************************************************
*/

    //function used to select all days in a check box list or none
    function search2(ddlSchoolId, ddlActivityId, txtDateStartId, txtDateEndId)
    {
          var ddlSchoolTemp    = document.getElementById(ddlSchoolId);
          var ddlActivityTemp  = document.getElementById(ddlActivityId);
          var txtDateStartTemp = document.getElementById(txtDateStartId);
          var txtDateEndTemp   = document.getElementById(txtDateEndId);
          
          if ((ddlSchoolTemp.value != "00000000-0000-0000-0000-000000000000") && (ddlActivityTemp.value == "00000000-0000-0000-0000-000000000000"))
          {
            //go to school page
            var strText     = ddlSchoolTemp.options[ddlSchoolTemp.selectedIndex].text;
            var strTextNew  = strText.replace(" ", "-");
            
            window.location = "/" + strTextNew + ".aspx";
          }
          else
          {
            //go to search results page
            window.location =   "/booking-public/search-results.aspx?school=" + ddlSchoolTemp.value +
                                "&activity=" + ddlActivityTemp.value +
                                "&startdate=" + escape(txtDateStartTemp.value) +
                                "&enddate=" + escape(txtDateEndTemp.value);
          }
          
    }
    
    //function used to filter the activity list depending on the school
    function populateActivity(ddlSchoolId, ddlActivityId, txtDateStartId, txtDateEndId)
    {
        //now changed to just post back to same page
        var ddlSchoolTemp    = document.getElementById(ddlSchoolId);
        var ddlActivityTemp  = document.getElementById(ddlActivityId);
        var txtDateStartTemp = document.getElementById(txtDateStartId);
        var txtDateEndTemp   = document.getElementById(txtDateEndId);
          
        //parse current pages query string
        var qs = new Querystring();
        
        if ((qs.contains("page")) || (qs.contains("Page")))
        {
            var pageNo; 
            
            if (qs.contains("page"))
                pageNo = qs.get("page");
            else
                pageNo = qs.get("Page");
            
            window.location = window.location.protocol + "//" +
                              window.location.host +
                              window.location.pathname + 
                              "?Page=" + pageNo +
                              "&school=" + ddlSchoolTemp.value +
                              "&activity=" + ddlActivityTemp.value +
                              "&startdate=" + escape(txtDateStartTemp.value) +
                              "&enddate=" + escape(txtDateEndTemp.value);
        }
        else
        {
            window.location = window.location.protocol + "//" +
                              window.location.host +
                              window.location.pathname + 
                              "?school=" + ddlSchoolTemp.value +
                              "&activity=" + ddlActivityTemp.value +
                              "&startdate=" + escape(txtDateStartTemp.value) +
                              "&enddate=" + escape(txtDateEndTemp.value);
        }
                       
    }
    
    /* Client-side access to querystring name=value pairs
	*/
    function Querystring(qs) { // optionally pass a querystring to parse
	    this.params = {};
    	
	    if (qs == null) qs = location.search.substring(1, location.search.length);
	    if (qs.length == 0) return;

    // Turn <plus> back to <space>
	    qs = qs.replace(/\+/g, ' ');
	    var args = qs.split('&'); // parse out name/value pairs separated via &
    	
    // split out each name=value pair
	    for (var i = 0; i < args.length; i++) {
		    var pair = args[i].split('=');
		    var name = decodeURIComponent(pair[0]);
    		
		    var value = (pair.length==2)
			    ? decodeURIComponent(pair[1])
			    : name;
    		
		    this.params[name] = value;
	    }
    }

    Querystring.prototype.get = function(key, default_) {
	    var value = this.params[key];
	    return (value != null) ? value : default_;
    }

    Querystring.prototype.contains = function(key) {
	    var value = this.params[key];
	    return (value != null);
    }

