Event.observe(document, 'dom:loaded', fdc_init, false);

function fdc_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);
	});
	
	// 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_registration_submit);
	}
}

function show_advanced_options(){	
	$('fdc_more_options').blur();
	Effect.BlindDown($('fdc_advanced_options'), {duration: 0.2, afterFinish: function(){$('fdc_more_options').className = 'optbox_expanded';}});
	Event.observe($('fdc_more_options'), 'click', hide_advanced_options, false);
	Event.stopObserving($('fdc_more_options'), 'click', show_advanced_options, false);
}

function hide_advanced_options(){
	$('fdc_more_options').blur();
	Effect.BlindUp($('fdc_advanced_options'), {duration: 0.2, afterFinish: function(){$('fdc_more_options').className = 'optbox_hidden';}});
	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.2, afterFinish: function(){
			$('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.2});
		}
		$('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 + '/wp-content/plugins/foreclosure/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;
}

/* registration capturing functions */
function fdc_get_details(e){
	var element = e.element();
	if(Get_Cookie('fdc_reg') == null){
		fdc_show_registration_form(element);
		e.stop();
	}
}

function fdc_registration_cancel(){
	tb_remove();
	$('fdc_registration_div').hide();
	$('fdc_registration_error').hide();
}

function fdc_show_registration_form(element){
	$('caller_link_destination').value = element.href;
	tb_show("Register", "#TB_inline?height=170&width=300&inlineId=fdc_registration_div&modal=true", false);
	return false;
}

function fdc_registration_submit(e){
	var regForm =$('fdc_registration_form');
	if( !fdc_registration_validate(regForm)){
		e.stop();
		return false;
	}
	$('fdc_registration_form').hide();
	regForm.request({
		method: 'post',
		asynchronous: false,
		onSuccess: function(transport){
			var result = transport.responseText.evalJSON().result;
			if(result){
				Set_Cookie('fdc_reg', "true", 30, '/');
			}
		}
	});
	var cobrandSubdomain = $('cobrand_subdomain').value;
	var registrationPage = 'https://' + cobrandSubdomain + '.foreclosure.com/registration.html';
	regForm.action = registrationPage;
}

function fdc_registration_validate(formElem){
	var fname = formElem.firstName.value;
	var lname = formElem.lastName.value;
	var email = formElem.email.value;
	var phone = formElem.phone.value;
	
	var error = false;
	if(fname.strip() == ''){
		error = true;
		$(formElem.firstName).pulsate({pulses:5, duration:1.5});
	}
	if(lname.strip() == ''){
		error = true;
		$(formElem.lastName).pulsate({pulses:5, duration:1.5});
	}
	if(email.strip() == ''){
		error = true;
		$(formElem.email).pulsate({pulses:5, duration:1.5});
	}
	if(phone.strip() == ''){
		error = true;
		$(formElem.phone).pulsate({pulses:5, duration:1.5});
	}
	
	if(error){
		return false;
	}
	
	return true;
}
