function regLoader(content,where){
	if($defined($('reg_loader'))){
		$('reg_loader').destroy();
	}
	var loader = new Element('div').setProperties({
		'id': 'reg_loader'
	})
	loader.set('html',content);
	where.set('html','');
	loader.inject(where,'bottom');
}
function intRegForm(){
	if($defined($('reg_form'))){
		formCheck = new FormCheck('reg_form', {
			submit: false,
			trimValue: true,
			onValidateSuccess: (function(){ processRegForm(); }),
			display : {
				showErrors: 1,
				indicateErrors: 2,
				checkValueIfEmpty: 0
			}
		});
		FancyForm.add($$('div.f_checkbox'));
	}
	if($defined($('loc_form'))){
		formCheck = new FormCheck('loc_form', {
			submit: false,
			trimValue: true,
			onValidateSuccess: (function(){
				if($('submit').hasClass('add')){
					addLoc();
				}
				if($('submit').hasClass('edit')){
					editLoc();
				}
			}),
			display : {
				showErrors: 1,
				indicateErrors: 2,
				checkValueIfEmpty: 0
			}
		});
	}
}
function addLoc(){
	var locForm = new Request.HTML({
		method: 'post',
		url: '/ajax/registration/process.php',
		evalScripts:false,
		evalResponse:false,
		onRequest: function(){
			if($defined($$('.fc-tbx'))){
				$$('.fc-tbx').each(function(el){
					el.dispose();
				});
			}
			mooSpinner($('loc_holder'),'Adding...')
			new Fx.Scroll(window).set(0,0);
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			(function(){
				$('loc_holder').set('html', responseHTML);
				new Fx.Scroll(window).set(0,0);
				$$("input").each(function(element){
					if(element.getProperty('type') == 'text'){
						element.set('value','');
					}
				});
				$exec(responseJavaScript);
			}).delay(750);
		},
		onFailure: function() {
			(function(){
				$('loc_holder').set('html','<h1>A transport error occured.</h1>Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.');
			}).delay(750);
		}
	});
	locForm.post($('loc_form'));
}
function editLoc(){
	var locForm = new Request.HTML({
		method: 'post',
		url: '/ajax/registration/process.php',
		evalScripts:false,
		evalResponse:false,
		onRequest: function(){
			if($defined($$('.fc-tbx'))){
				$$('.fc-tbx').each(function(el){
					el.dispose();
				});
			}
			mooSpinner($('loc_holder'),'Editing...');
			new Fx.Scroll(window).set(0,0);
			$('submit').removeClass('edit');
			$('submit').addClass('add');
			$('step').set('value','add-location');
			$('editholder').set('value','');
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			(function(){
				$('loc_holder').set('html', responseHTML);
				new Fx.Scroll(window).set(0,0);
				$$("input").each(function(element){
					if(element.getProperty('type') == 'text'){
						element.set('value','');
					}
				});
				$exec(responseJavaScript);
			}).delay(750);
		},
		onFailure: function() {
			(function(){
				$('loc_holder').set('html','<h1>A transport error occured.</h1>Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.');
			}).delay(750);
		}
	});
	locForm.post($('loc_form'));
}
function clearRegForm(){
	$$("input").each(function(element){
		if(element.getProperty('type') == 'text'){
			element.set('value','');
		}
	});
	$('submit').removeClass('edit');
	$('submit').addClass('add');
}
function processRegForm(){
	if($defined($('override_to'))){
		var step = $('override_to').get('value').toInt();
	}else{
		var step = $('step').get('value').toInt() + 1;
	}
	var processForm = new Request.HTML({
		method: 'post',
		url: '/ajax/registration/process.php',
		evalScripts:false,
		evalResponse:false,
		onRequest: function(){
			if($defined($$('.fc-tbx'))){
				$$('.fc-tbx').each(function(el){
					el.dispose();
				});
			}
			if(step==6){
				regLoader('Creating Your New RENPS Account', $('reg_form_wrap'));
			}else{
				regLoader('Processing Registration', $('reg_form_wrap'));
			}
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			(function(){
				if(step==6){
					if(responseHTML=='S'){
						window.location = "thank-you.php";
					}else{
						$('reg_form_wrap').set('html','');
						$('reg_form_wrap').set('html', responseHTML);
						new Fx.Scroll(window).set(0,0);
						intNavLinks();
					}
				}else{
					$('reg_form_wrap').set('html','');
					$('reg_form_wrap').set('html', responseHTML);
					new Fx.Scroll(window).set(0,0);
					$exec(responseJavaScript);
					progressNav($('nav_'+((step)>4?4:step)));
					intRegForm();
					intNavLinks();
					buildAllTipz();
				}
			}).delay(750);
		},
		onFailure: function() {
			(function(){
				$('reg_form_wrap').set('html','<h1>A transport error occured.</h1>Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.');
			}).delay(750);
		}
	});
	if($defined($('reg_form'))){
		processForm.post($('reg_form'));
	}else{
		processForm.send('step='+step);
	}
}
function initRegScripts(){
	if(!$defined($('reg_form'))){
		var intRegistration = new Request.HTML({
			method: 'post',
			url: '/ajax/registration/process.php',
			data: 'step=new',
			evalScripts:false,
			evalResponse:false,
			onRequest: function(){
				regLoader('Initializing Registration', $('reg_form_wrap'));
			},
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				(function(){
					$('reg_form_wrap').set('html','');
					$('reg_form_wrap').set('html', responseHTML);
					new Fx.Scroll(window).set(0,0);
					$exec(responseJavaScript);
					progressNav($('nav_1'));
					intRegForm();
					intNavLinks();
					buildAllTipz();
				}).delay(750);
			},
			onFailure: function() {
				(function(){
					$('reg_form_wrap').set('html','<h1>A transport error occured.</h1>Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.');
				}).delay(750);
			}
		});
		intRegistration.send();
	}
	defaultReBox = new reBox();
}
function intNavLinks(){
	$$("form input, form a").each(function(element){
		element.removeEvents('focus');
		element.addEvent('focus', function(e){
			if(element.getProperty('type') != 'hidden'){
				var forceScrollOffset = 20;
				var bottomToTop = ((element.getTop(window).toInt())+(element.getSize().y.toInt()));
				var windowHeight  = window.getSize().y.toInt();
				var windowScrollHeight = window.getScrollSize().y.toInt();
				if(bottomToTop+forceScrollOffset>windowHeight){
					var dest = Math.abs(windowHeight-(bottomToTop+forceScrollOffset));
					if(window.getScroll().y <= dest+forceScrollOffset){
						var moveRegWin = new Fx.Scroll(window).set(0,dest+forceScrollOffset);
					}
				}
			}
		});
	});
	$$("div#form_nav div").each(function(element){
		element.removeEvents();
		element.addEvent('click', function(e){
			e.stop();
			if(element.hasClass('blue')){
				if($defined($('step'))){
					var step = $('step').get('value');
					var override = element.get('rel').toInt();
					if((step==override) || (step=='add-location'&&override==3) || (step=='edit-location'&&override==3) || (step==5 && override==4)){
						quickAlert('Current Page', 'You are currently on this page. Please scroll to the bottom to submit or click on another tab.', 300, 100);
					}else if(step==1 || step==2){
						if(formCheck.isFormValid()){
							$('step').set('value','override');
							var overRideVar = new Element('input').setProperties({
								'type': 'hidden',
								'name': 'override_to',
								'id': 'override_to',
								'value': override
							})
							overRideVar.inject($('reg_form'),'bottom');
							processRegForm();
						}else{
							$('reg_form').fireEvent('submit');
						}
					}else if(step==4){
						$('step').set('value','override');
						var overRideVar = new Element('input').setProperties({
							'type': 'hidden',
							'name': 'override_to',
							'id': 'override_to',
							'value': override
						})
						overRideVar.inject($('reg_form'),'bottom');
						processRegForm();
					}else if(step==5){
						$('step').set('value','override');
						var overRideVar = new Element('input').setProperties({
							'type': 'hidden',
							'name': 'override_to',
							'id': 'override_to',
							'value': override
						})
						overRideVar.inject($('reg_form'),'bottom');
						processRegForm();
					}else if(override<5&&override>0){
						if($('step').get('value')=='add-location'){
							$('step').set('value','location')
						}
						var intRegistration = new Request.HTML({
							method: 'post',
							url: '/ajax/registration/process.php',
							data: 'step='+$('step').get('value')+'&override_to='+override,
							evalScripts:false,
							evalResponse:false,
							onRequest: function(){
								if($defined($$('.fc-tbx'))){
									$$('.fc-tbx').each(function(el){
										el.dispose();
									});
								}
								regLoader('Initializing Registration', $('reg_form_wrap'));
							},
							onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
								(function(){
									$('reg_form_wrap').set('html','');
									$('reg_form_wrap').set('html', responseHTML);
									var moveRegWinToTop = new Fx.Scroll(window).set(0,0);
									$exec(responseJavaScript);
									progressNav($('nav_'+override));
									intRegForm();
									intNavLinks();
									buildAllTipz();
								}).delay(750);
							},
							onFailure: function() {
								(function(){
									$('reg_form_wrap').set('html','<h1>A transport error occured.</h1>Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.');
								}).delay(750);
							}
						});
						intRegistration.send();
					}
				}
			}
		});
	});	
}
function progressNav(el){
	el.setStyle('opacity','0');
	el.removeClass('gray');
	el.addClass('blue');
	el.fade('1');	
}
function readyLocOptions(){
	$$('div.ind_loc').each(function(element){
		var optLoc = element.getFirst('div.loc_opt');
		var editLoc = optLoc.getFirst('div.edit-loc');
		var delLoc = optLoc.getFirst('div.del-loc');
		var copyLoc = optLoc.getFirst('div.copy-loc');
		editLoc.removeEvents();
		delLoc.removeEvents();
		copyLoc.removeEvents();
		editLoc.addEvent('click', function(){
    		editLocation(editLoc.get('rel'));
		});
		delLoc.addEvent('click', function(){
    		delLocation(delLoc.get('rel'));
		});
		copyLoc.addEvent('click', function(){
    		copyLocation(copyLoc.get('rel'));
		});
		optLoc.set('opacity',0);
		element.removeEvents();
		element.addEvents({
			'mouseover': function(){
				element.setStyle('background-position', '50% 100%');
				optLoc.fade(1);
			},
			'mouseout': function(){
				element.setStyle('background-position', '50% 200%');
				optLoc.fade(0);
			}
		});
	});	
}
function editLocation(v){
	var editloc = new Request.JSON({
		method: 'post',
		url: '/ajax/registration/get-loc-info.php',
		data: 'method=copy&loc='+v,
		onSuccess: function(company) {
			if(company){
				$('submit').removeClass('add');
				$('submit').addClass('edit');
				$('step').set('value','edit-location');
				$('editholder').set('value',v);
				if($defined(company.name)){ $('location_name').set('value',unescape(company.name).trim()).getParent().highlight('#ddf'); }
				if($defined(company.address_1)){ $('location_address_1').set('value',unescape(company.address_1).trim()).getParent().highlight('#ddf'); }
				if($defined(company.address_2)){ $('location_address_2').set('value',unescape(company.address_2).trim()).getParent().highlight('#ddf'); }
				if($defined(company.zip)){ $('location_zip').set('value',unescape(company.zip).trim()).getParent().highlight('#ddf'); }
				if($defined(company.city)){ $('location_city').set('value',unescape(company.city).trim()).getParent().highlight('#ddf'); }
				if($defined(company.state)){ $('location_state').set('value',unescape(company.state).trim()).getParent().highlight('#ddf'); }
				if($defined(company.phone)){ $('location_phone').set('value',unescape(company.phone).trim()).getParent().highlight('#ddf'); }
				if($defined(company.fax)){ $('location_fax').set('value',unescape(company.fax).trim()).getParent().highlight('#ddf'); }
				if($defined(company.website)){ $('location_website').set('value',unescape(company.website).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_name)){ $('location_name').set('value',unescape(company.location_name).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_address_1)){ $('location_address_1').set('value',unescape(company.location_address_1).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_address_2)){ $('location_address_2').set('value',unescape(company.location_address_2).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_zip)){ $('location_zip').set('value',unescape(company.location_zip).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_city)){ $('location_city').set('value',unescape(company.location_city).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_state)){ $('location_state').set('value',unescape(company.location_state).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_phone)){ $('location_phone').set('value',unescape(company.location_phone).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_fax)){ $('location_fax').set('value',unescape(company.location_fax).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_website)){ $('location_website').set('value',unescape(company.location_website).trim()).getParent().highlight('#ddf'); }
				if($defined(company.location_zips)){ $('location_zips').set('value',unescape(company.location_zips).trim()).getParent().highlight('#ddf'); }
				if($defined(company.stats.location_no_agents)){ $('location_no_agents').set('value',unescape(company.stats.location_no_agents).trim()).getParent().highlight('#ddf'); }
				if($defined(company.stats.location_yr_opened)){ $('location_yr_opened').set('value',unescape(company.stats.location_yr_opened).trim()).getParent().highlight('#ddf'); }
				if($defined(company.stats.location_cur_yr_sv)){ $('location_cur_yr_sv').set('value',unescape(company.stats.location_cur_yr_sv).trim()).getParent().highlight('#ddf'); }
				if($defined(company.stats.location_cur_yr_sides)){ $('location_cur_yr_sides').set('value',unescape(company.stats.location_cur_yr_sides).trim()).getParent().highlight('#ddf'); }
				if($defined(company.stats.location_last_yr_sv)){ $('location_last_yr_sv').set('value',unescape(company.stats.location_last_yr_sv).trim()).getParent().highlight('#ddf'); }
				if($defined(company.stats.location_last_yr_sides)){ $('location_last_yr_sides').set('value',unescape(company.stats.location_last_yr_sides).trim()).getParent().highlight('#ddf'); }
				formCheck.reinitialize('forced');
			}
		},
		onFailure: function() {
			quickAlert('A transport error occured.', 'Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.', 300, 100);
		}
	});
	editloc.send();
}
function delLocation(v){
	var msg = 'Are you sure you would like to delete this location? This cannot be undone.';
	if(confirm(msg)){
		var delloc = new Request.HTML({
			method: 'post',
			url: '/ajax/registration/get-loc-info.php',
			data: 'method=del&loc='+v,
			onRequest: function(){
				mooSpinner($('loc_holder'),'Deleting...')
			},
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				(function(){
					$('loc_holder').set('html', responseHTML);
					var moveRegWinToTop = new Fx.Scroll($('loc_holder')).set(0,0);
					$exec(responseJavaScript);
				}).delay(750);
			},
			onFailure: function() {
				quickAlert('A transport error occured.', 'Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.', 300, 100);
			}
		});
		delloc.send();
	}
}
function copyLocation(v){
	switch(v){
		case 'name':
		case 'address_1':
		case 'address_2':
		case 'zip':
		case 'city':
		case 'state':
		case 'phone':
		case 'fax':
		case 'website':
			var ind = true;
			var cont = true;
			var profile = false;
		break;
		case 'profile':
			var profile = true;
			var ind = false;
			var cont = true;
		break;
		default:
			if(v>0){
				var cont = true;
			}else{
				var cont = false;
				quickAlert('A System Error Occured', 'We have been notified, Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.', 300, 100);
			}
			var ind = false;
			var profile = false;
		break;
	}
	if(cont){
		var m = 'post';
		var u = '/ajax/registration/get-loc-info.php';
		var d = 'method=copy&loc='+v;
		if(ind){
			var copyloc = new Request.HTML({
				method: m,
				url: u,
				data: d,
				onRequest: function(){
					if(!ind && !profile){
						mooSpinner($('loc_holder'),'Adding...')
					}
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					if(responseHTML){
						$('location_'+v).set('value',unescape(responseHTML).trim()).blur();
						$('location_'+v).getParent().highlight('#ddf');
						formCheck.reinitialize('forced');
					}
				},
				onFailure: function() {
					quickAlert('A transport error occured.', 'Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.', 300, 100);
				}
			});
		}else{
			var copyloc = new Request.JSON({
				method: m,
				url: u,
				data: d,
				onSuccess: function(company) {
					if(company){
						if($defined(company.name)){ $('location_name').set('value',unescape(company.name).trim()).getParent().highlight('#ddf'); }
						if($defined(company.address_1)){ $('location_address_1').set('value',unescape(company.address_1).trim()).getParent().highlight('#ddf'); }
						if($defined(company.address_2)){ $('location_address_2').set('value',unescape(company.address_2).trim()).getParent().highlight('#ddf'); }
						if($defined(company.zip)){ $('location_zip').set('value',unescape(company.zip).trim()).getParent().highlight('#ddf'); }
						if($defined(company.city)){ $('location_city').set('value',unescape(company.city).trim()).getParent().highlight('#ddf'); }
						if($defined(company.state)){ $('location_state').set('value',unescape(company.state).trim()).getParent().highlight('#ddf'); }
						if($defined(company.phone)){ $('location_phone').set('value',unescape(company.phone).trim()).getParent().highlight('#ddf'); }
						if($defined(company.fax)){ $('location_fax').set('value',unescape(company.fax).trim()).getParent().highlight('#ddf'); }
						if($defined(company.website)){ $('location_website').set('value',unescape(company.website).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_name)){ $('location_name').set('value',unescape(company.location_name).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_address_1)){ $('location_address_1').set('value',unescape(company.location_address_1).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_address_2)){ $('location_address_2').set('value',unescape(company.location_address_2).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_zip)){ $('location_zip').set('value',unescape(company.location_zip).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_city)){ $('location_city').set('value',unescape(company.location_city).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_state)){ $('location_state').set('value',unescape(company.location_state).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_phone)){ $('location_phone').set('value',unescape(company.location_phone).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_fax)){ $('location_fax').set('value',unescape(company.location_fax).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_website)){ $('location_website').set('value',unescape(company.location_website).trim()).getParent().highlight('#ddf'); }
						if($defined(company.location_zips)){ $('location_zips').set('value',unescape(company.location_zips).trim()).getParent().highlight('#ddf'); }
						if(!profile){
							if($defined(company.stats.location_no_agents)){ $('location_no_agents').set('value',unescape(company.stats.location_no_agents).trim()).getParent().highlight('#ddf'); }
							if($defined(company.stats.location_yr_opened)){ $('location_yr_opened').set('value',unescape(company.stats.location_yr_opened).trim()).getParent().highlight('#ddf'); }
							if($defined(company.stats.location_cur_yr_sv)){ $('location_cur_yr_sv').set('value',unescape(company.stats.location_cur_yr_sv).trim()).getParent().highlight('#ddf'); }
							if($defined(company.stats.location_cur_yr_sides)){ $('location_cur_yr_sides').set('value',unescape(company.stats.location_cur_yr_sides).trim()).getParent().highlight('#ddf'); }
							if($defined(company.stats.location_last_yr_sv)){ $('location_last_yr_sv').set('value',unescape(company.stats.location_last_yr_sv).trim()).getParent().highlight('#ddf'); }
							if($defined(company.stats.location_last_yr_sides)){ $('location_last_yr_sides').set('value',unescape(company.stats.location_last_yr_sides).trim()).getParent().highlight('#ddf'); }
						}
						formCheck.reinitialize('forced');
					}
				},
				onFailure: function() {
					quickAlert('A transport error occured.', 'Please try your request again later (by using your browser refresh button / revisiting the link in the URL) or check your internet connection.', 300, 100);
				}
			});	
		}
		copyloc.send();	
	}
}
function stateCheck(el){
	var states = $('state_error_pointer').getElements('input:checked');
	if(states.length>0){
		formCheck.dispose($('check_states'));
		$('check_states').dispose();
		return true;
	}else{
		el.errors.push("Please select at least one state from below.");
		return false;
	}
}
var formCheck = '';
window.addEvent('domready', function() {
	initRegScripts();
});