document.observe('dom:loaded',init,false);

function init(){
	advanced_options_init();
	registration_form_init();
}
function advanced_options_init(){
	var mo = $('fdc_more_options');
	if(mo){
		Event.observe($('fdc_more_options'), 'click', show_advanced_options, false);
		Event.observe($('fdc_state_selector'), 'change', toggle_zipcode_field, false);
	}
}

function registration_form_init(){
	var detailsLinks = $$('.fdc_details_link');
	detailsLinks.each(function(el){
		//el.observe('click', fdc_get_details, true);
		el.show();
	});
	
	// set up the registration form cancel and submit buttons
	/*
	if($('fdc_registration_div')){
		$('fdc_reg_reset').observe('click',fdc_registration_cancel);
		$('fdc_reg_submit').observe('click',fdc_submit_registration);
	}
	*/
}

function show_advanced_options(e){	
	Effect.BlindDown($('fdc_advanced_options'), {duration: 0.4, afterFinish: function(){$('fdc_more_options').innerHTML = 'Simple Search';}});
	Event.observe($('fdc_more_options'), 'click', hide_advanced_options, false);
	Event.stopObserving($('fdc_more_options'), 'click', show_advanced_options, false);
	e.stop();
}

function hide_advanced_options(){
	Effect.BlindUp($('fdc_advanced_options'), {duration: 0.4, afterFinish: function(){$('fdc_more_options').innerHTML = 'Advanced Search';}});
	Event.observe($('fdc_more_options'), 'click', show_advanced_options, false);
	Event.stopObserving($('fdc_more_options'), 'click', hide_advanced_options, false);
}

function toggle_zipcode_field(){
	if($F('fdc_state_selector') == '-1'){
		Effect.BlindUp($('state_required_options'), {duration:0.3});
		$('fdc_zip_input').disable();
		$('fdc_city_input').disable();
		$('fdc_county_selector').disable();
	} else {
		var stateReqOpts = $('state_required_options');
		if(stateReqOpts.style.display == 'none'){
			Effect.BlindDown(stateReqOpts, {duration:0.3});
		}
		$('fdc_zip_input').enable();
		$('fdc_city_input').enable();
		$('fdc_county_selector').enable();
	}
}

function resetCounties(elem){
	while((op = (elem.length - 1)) > 0){
		elem.remove(op);
	} 
}

function getCounties(baseurl){
	state = $F('fdc_state_selector');
	countyElem = $('fdc_county_selector');
	
	resetCounties(countyElem);
	
	if(state == '-1'){
		countyElem.options[0] = new Option('', '');
		return;
	} else {
		countyElem.options[0] = new Option('Getting Counties...', '');
		countyElem.setAttribute('disabled', true);
	}
	
	var url = baseurl + '/ajax/get_counties.php';
	var parms = 'state=' + state;
	var ajax = new Ajax.Request(url,{ method:'get', parameters: parms, onComplete: handleCountyResponse});
}

function handleCountyResponse(req){
	countyXml = req.responseXML;
	countyList = countyXml.getElementsByTagName('county');
	var countyElem = $('fdc_county_selector');
	resetCounties(countyElem);
	countyElem.removeAttribute('disabled');
	
	countyElem.options[0] = new Option('Select a County', '000');
	for(var i=0; i < countyList.length; i++){
		fips = countyList[i].getAttribute('fips');
		county = countyList[i].firstChild.nodeValue;
		countyElem.options[i+1] = new Option(county, fips);
	}
}

function fdc_search_validate(formElem){
	var countyStr = $F('fdc_county_selector');
	var stateStr = $F('fdc_state_selector');
	var cityStr = $F('fdc_city_input');
	var zipStr = $F('fdc_zip_input');
	
	var hasError = false;
	if(countyStr == '000'){
		if((zipStr.strip() == '') && ( cityStr.strip() == '') ){
			$('fdc_search_error').innerHTML = 'Please enter a county, city or zipcode.';
			Effect.Appear($('fdc_search_error'));
			hasError = true;
		}	
	}
	if(stateStr == '-1'){
		$('fdc_search_error').innerHTML = 'Please select a state.';
		Effect.Appear($('fdc_search_error'));
		hasError = true;	
	}
	return !hasError;
}

function grayOut(vis) {
  var options = options || {}; 
  var zindex = 50;
  var opacity = 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');
        tnode.style.position='absolute';
        tnode.style.top='0px';
        tnode.style.left='0px';
        tnode.style.overflow='hidden';           
        tnode.style.display='none';
        tnode.id='darkenScreenObject';
    tbody.appendChild(tnode);
    dark=document.getElementById('darkenScreenObject');
  }
  if (vis) {
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.zIndex = 0;
     dark.style.display='none';
  }
}

function fdc_get_details(e){
	var element = Event.element(e);
	if(Get_Cookie('fdc_reg') == null){
		fdc_show_registration_form(element);
		e.stop();
	}
}

function fdc_registration_cancel(){
	if($('xlisting_search')){
		$('xlisting_search').style.visibility = 'visible';
	}
	grayOut(false);
	Effect.Fade($('fdc_registration_div'),{duration: 0.2, afterFinish: function(){$('fdc_registration_error').hide();}});
	
}

function fdc_show_registration_form(element){
	var reg_form = $('fdc_registration_div');
	if($('xlisting_search')){
		$('xlisting_search').style.visibility = 'hidden';
	}
	grayOut(true,null); 
	reg_form.style.top = (document.viewport.getScrollOffsets().top + 100) + 'px';
	//reg_form.style.left =  (document.viewport.getWidth()/2) - (reg_form.getWidth() /2) + 'px';
	reg_form.style.zIndex = 100;
	$('caller_link_destination').value = element.href;
	Effect.Appear(reg_form, {duration: 0.2});
}

function fdc_submit_registration(e){
	if( !fdc_validate_registration($('fdc_registration_form'))){
		e.stop();
		return false;
	}
	$('fdc_registration_form').request();
	if($('xlisting_search')){
		$('xlisting_search').style.visibility = 'visible';
	}
	Effect.Fade($('fdc_registration_div'),{duration: 0.2});
	e.stop();
	return false;
}

function fdc_validate_registration(formElem){
	var name = formElem.name.value;
	var email = formElem.email.value;
	var phone = formElem.phone.value;
	
	var error = '';
	if(name.strip() == ''){
		error += "<li>Name cannot be empty.</li>";
	}
	if(email.strip() == ''){
		error += "<li>Email address cannot be empty.</li>";
	}
	if(phone.strip() == ''){
		error += "<li>Phone cannot be empty.</li>";
	}
	
	if(error){
		$('fdc_registration_error').update('<ul>' + error + '</ul>');
		Effect.Appear($('fdc_registration_error'), {duration: 0.3});
		return false;
	}
	
	return true;
}
