var state_list = new Array();
state_list['al'] = 0;
state_list['ak'] = 1;  state_list['az'] = 2;  state_list['ar'] = 3;  state_list['ca'] = 4;  state_list['co'] = 5;
state_list['ct'] = 6;  state_list['de'] = 7;  state_list['dc'] = 8;  state_list['fl'] = 9;  state_list['ga'] = 10;
state_list['hi'] = 11; state_list['id'] = 12; state_list['il'] = 13; state_list['in'] = 14; state_list['ia'] = 15;
state_list['ks'] = 16; state_list['ky'] = 17; state_list['la'] = 18; state_list['me'] = 19; state_list['md'] = 20;
state_list['ma'] = 21; state_list['mi'] = 22; state_list['mn'] = 23; state_list['ms'] = 24; state_list['mo'] = 25;
state_list['mt'] = 26; state_list['ne'] = 27; state_list['nv'] = 28; state_list['nh'] = 29; state_list['nj'] = 30;
state_list['nm'] = 31; state_list['ny'] = 32; state_list['nc'] = 33; state_list['nd'] = 34; state_list['oh'] = 35;
state_list['ok'] = 36; state_list['or'] = 37; state_list['pa'] = 38; state_list['ri'] = 39; state_list['sc'] = 40;
state_list['sd'] = 41; state_list['tn'] = 42; state_list['tx'] = 43; state_list['ut'] = 44; state_list['vt'] = 45;
state_list['va'] = 46; state_list['wa'] = 47; state_list['wv'] = 48; state_list['wi'] = 49; state_list['wy'] = 50;


Event.observe(window, "load", function() {
   
   new Ajax.Autocompleter('degree_name', 'degree_auto_complete', '/axn/degree-list.axn.php',  {})
   
   
   sel_degree = getURLpound_param('degree');
   sel_state = getURLpound_param('state');
   
   if(sel_state && sel_degree) {
   $('loading').show();
   $('state').options[state_list[sel_state]].selected = 'true'
   $('degree_name').value = sel_degree;
   ajax_query_school(sel_state.toUpperCase(), sel_degree);
   }
});

getURLpound_param = function(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("#") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("#")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

function ajax_query_school(sel_state, sel_degree)
{
   
   location.href = '#state='+sel_state+'&degree='+sel_degree;
   new Ajax.Request('/axn/query-school.axn.php', {
		method:'post',
		parameters: 'degree%5Bname%5D='+sel_degree+'&state='+sel_state,
		onSuccess: function(requestResponse) {
         schools = requestResponse.responseText.evalJSON();  
         $('school_div').style.display = 'block';
         if(schools) {
            schools.each(function(s) {
               
               school = '<div class="archive_sections" style="clear: both; margin: 10px 0 15px 0; text-align: left; overflow: hidden">';
               school += '<div style="float: left;">';
               
               link_school_name = s[0].ATCO_name.replace("\s","-");
               school += '<h3 style="margin: 0; font-size: 15px"><a href="/higher-education/grad-school-profiles/'+s[0].mail_state+'/'+s[0].link_name+'">'+s[0].ATCO_name+'</a></h3>';
               school += '<p style="margin: 3px 0 0 15px">'+s[0].mail_city+', '+s[0].mail_state+'</p>';
               school += '</div>';
               school += '</div>';
   
               Insertion.Bottom('query_results', school);
               
            });
         } else {
            Insertion.Bottom('query_results', "<strong>No Schools Found.</strong>");
         }
         Insertion.Bottom('query_results', "</div>");
         $('loading').hide();
		}
	});
   return false;
}
   
function query_school(form) {
   $('query_results').update('');
   $('loading').show();
   sel_degree = $('degree_name').value;
   sel_state =  $('state').value;
   ajax_query_school(sel_state, sel_degree);
   return false;
}   
   