var TrendID = "trendID";
var AssessmentID = "assessmentID";
var DemoID = "demoID";
var Year ="year",TrendID="trendID", Mode="mode",Sort = "sort", States="states";
var baseURL = window.location.href.split('?')[0];

var POLICY_ID = "policyID";
var COLOR_ID = "COLOR";
var CONCAT_SYM = "&";
var EQUALS = "=";

function DoPostBack()
{       
    window.location.href = baseURL + buildURL();                
}
function ClearDemographic()
{
     
   window.location.href = updateQueryString(window.location.href,"demoID","").replace(/&demoID=/gi,"");
}
function CustomComparePostBack()
{
    var states="";
    submitURL = baseURL + buildURL() ;
    submitURL += (states = getQueryString(States,window.location.href)) != ""  ? "&states=" + states : "" ;
    window.location.href = submitURL;
}
function CompareStates()
{
    var states="";    
        
        statesCheckboxes = $$('input[type=checkbox]').filter( function(item, index) {return item.checked});         
        statesCheckboxes.each(function(item){
            if (item.checked)
            {
              states += item.parentNode.getAttribute("alt") + ",";
            }
        });           
       states  = states.charAt(states.length-1,1) == "," ? states.substring(0,states.length-1) : states;    
       submitURL = baseURL + buildURL() + "&states=" + states + "&mode=chart" ;   
       window.location.href  = submitURL;
}

function ChangeSelection()
{         
     window.location.href = (window.location.href).replace(/&mode=chart/gi,"");
}
/**
 * For use with the Policy Drawer (onclick event for the policy radio buttons)
 *
 * @param 'policy' : The index # for the given policy
 * @param 'el' : The actual input element for the given policy
 */
function postPolicy(policy,el) {
    var postURL  = baseURL + buildURL();       
    var color = el.parentNode.attributes.getNamedItem('class').value;
    
    postURL = policy > 0 ? postURL +  CONCAT_SYM + POLICY_ID + EQUALS + policy : postURL;
    var newURL = postURL + CONCAT_SYM + COLOR_ID + EQUALS + color;
    
    window.location.href = newURL;
}

function buildURL()
{
    var assessmentLinks = document.getElements('div#assessments ul li a');
	var currTrendID,currAssessmentID,currDemoID,currYear,currMode,currSort,URL,currStates;	
	
	currTrendID = getQueryString(TrendID,window.location.href);
	currAssessmentID =  getQueryString(AssessmentID,window.location.href);
	currAssessmentID = (currAssessmentID == "") ? getQueryString(AssessmentID,assessmentLinks[0]) : currAssessmentID;
	     
    URL = "?" +  TrendID + "="+ currTrendID + "&"+ AssessmentID + "=" + currAssessmentID ;
    
    var radioSelected =  $$('input[name=demo]').filter( function(item, index) {return item.checked} );
    
    currDemoID =  (radioSelected != "")? radioSelected[0].value : "" ;
    
    if (currDemoID != "" )
      URL += "&" + DemoID + "=" + currDemoID ;
      
    var yearSelected = document.getElements('div.map-table-controls select');
    currYear = (yearSelected != "") ? yearSelected[0][yearSelected[0].selectedIndex].value : "";
    if (currYear != "")
        URL += "&" + Year + "=" + currYear ;
    
    currMode = getQueryString(Mode,window.location.href)
    if (currMode != "")
        URL += "&" + Mode + "=" + currMode ;
        
    currSort = getQueryString(Sort,window.location.href);
    
    if (currSort != "")
        URL += "&" + Sort + "=" + currSort ;            
       
    return URL;
}

function getQueryString(param,link)
{

  param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+param+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(link);
   if( results == null )
    return "";
  else
    return results[1];
}

function updateQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re))         
            return url.replace(re,'$1' + param + "=" + value + '$2');        
    else
        return url + '&' + param + "=" + value;
}
