Event.observe(window, 'load', function(){
	setupLinks('lbOn')
});
var valid;
	function setupLinks(className){
		new Popup('popupAjax', null, {modal:true});
		new Popup('doublePopupAjax', null, {modal:true, closebox: 'popup_close'});
		var links = $$('.' + className);
		links.each(function(link){
			link.observe("click", popThis);
		})
	}
	function popThis(event){
		Event.stop(event);
		element = event.element();
		while(!element.hasAttribute('href')){
			element = Element.extend(element.parentNode);
		}
		var myAjax = new Ajax.Updater(
		{success: 'lightboxcontent'}, 
		element.readAttribute('href'), 
		{
			method: 'get', 
			parameters: "", 
			onFailure: reportError,
			onComplete: function(){
				popped("popupAjax");
			}
		});
		return false;
	}
	function reportError(error){
		alert(error);
	}
	function doublePop(event){
		Event.stop(event);
		element = event.element();
		while(!element.hasAttribute('href')){
			element = Element.extend(element.parentNode);
		}
		var myAjax = new Ajax.Updater(
		{success: 'doublePopup'}, 
		element.readAttribute('href'), 
		{
			method: 'get', 
			parameters: "", 
			onFailure: reportError,
			onComplete: function(){
				popped("doublePopupAjax");
			}
		});
		return false;
	}
	function popped(container){
		temp = $$("#" + container + " .popupcontent")[0].readAttribute('id')
		temp = '#' + temp
		$$(temp + ' .ajaxFlag').each(function(flag){
						flag.observe("click",ajaxFlag);
		});
		$$('#agent_profile form').each(function(item){
						item.observe("submit",ajaxReply);
		});
		$$('#edit_profile form').each(function(item){
						item.observe("submit",profileAjaxReply);
		});
		$$(temp + ' .glidebutton').each(function(link){
			link.observe("click", overArchingClick)
		});
		$$(temp + ' .lbOn').each(function(link){
			link.stopObserving("click", overArchingClick)
			link.observe("click", doublePop)
		});
		if($(container).select('#registration').length > 0){
			registration_validation();
		}
		$(container).popup.show();
	}
	function registration_validation(){
		valid = new Validation('registration', {immediate : true, onFormValidate : formCallback});
		// Comment 20090414
		// the callouts for id's to check against cannot have _ in them
		Validation.addAllThese([
			['validate-password', 'at least 8 characters with 1 number',function(v) {
					 return Validation.get('IsEmpty').test(v) || /^\w*(?=\w*\d)(?=\w*[a-zA-Z])\w*$/.test(v); 
			}, {
				minLength : 8,
				notOneOf : ['password','PASSWORD','1234567','0123456'],
				notEqualToField : 'email'
			}],
			['validate-password-confirm', 'Passwords do not match.', {
				equalToField : 'password'
			}],
			['validate-phone-number', 'Please enter a valid phone number.',  function(v) {
					 return Validation.get('IsEmpty').test(v) ||  /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(v);
			}],
			['validate-license-number', 'Please enter a valid license number.',  function(v) {
					 return Validation.get('IsEmpty').test(v) ||  /^\w*/.test(v);
					
			}], //{ maxLength:7}],
			['validate-first-name', 'Please enter a first name.',  function(v) {
					 return Validation.get('IsEmpty').test(v) ||  /^\w*/.test(v) || /First Name/.test(v);
			},{
				notOneOf : ['First Name']
			}],
			['validate-last-name', 'Please enter a last name.',  function(v) {
					 return Validation.get('IsEmpty').test(v) ||  /^\w*/.test(v)
			},{
				notOneOf : ['Last Name']	 	
			}],
			['validate-email-confirm', 'Your emails do not match, please try again.', {
				equalToField : 'email'
			}]

		]);
	}
	function formCallback(result, form){
		window.status = "valiation callback for form '" + form.id + "': result = " + result;
	}