/*
	(c) eclipse-creative.com
	tomc 20090114
	
	ENQUIRY FORM PAGE
*/
badField = null;
tabs = [];

titles=[
	{'short':'Step 1', 'full':'Step 1: Your Details'},
	{'short':'Step 2', 'full':'Step 2: Contact Details'},
	{'short':'Step 3', 'full':'Step 3: Lease Information'},
	{'short':'Step 4', 'full':'Step 4: Empty Property Details'} ];

/*
	GET ATIVE TAB
*/
getActiveTab=function(){
	activeTabs = $$('.activeTab');
	if( activeTabs.length > 1 )
		for( var ati = 1; ati < activeTabs.length; ++ati ){
			activeTabs[ati].deactivate();
		}

	if( activeTabs.length )
		return activeTabs[0];
	else
		return null;
}


/*
	BUTTON ACTIONS
*/
submitEnq=function(){
	/* check all steps are valid */
	for( var i = 0; i != tabs.length; ++i ){
		
		if( !tabs[i].stepValid() ){
			tabs[i].activate();
			return;
		}
	}
	

	deactivateForm();
	$('enqResult').setProperty('html', '<div style="text-align:center;"><img src="grx/spinner.gif" /> Sending Enquiry...</div>');
	
	var myRequest = new Request({
		method: 'post',
		url: "enquiries.php?action=json-submit&timecache="+new Date().getTime(),
		onComplete: function(response){
				
				var r = JSON.decode( response );
				
				if( r.status && r.status == 'ok' ){
					$('enqResult').setProperty('html', r.html);
				}
				
				if( r.status && r.status == 'fail' ){
					$('enqResult').setProperty('html', r.html);
					if( r.badStep >= 0 && r.badStep < tabs.length ){
						if( r.badField >= 0 ){
							tabs[r.badStep].hasBadField = true;
							tabs[r.badStep].badField = r.badField;
						}
						reactivateForm();
						tabs[r.badStep].activate();
					}
				}

				//$('result').setProperty('html', response );
			}
		});
	
	myRequest.send( $('enqForm') );
	
}

nextStep_ck=function(){
	if( getActiveTab().tIndex < tabs.length - 1 ){
		if( getActiveTab().stepValid() )
			tabs[getActiveTab().tIndex+1].activate();
		else
			getActiveTab().tipBadField();
	}
}
lastStep_ck=function(){
	if( getActiveTab().tIndex > 0 ){
		if( getActiveTab().stepValid() )
			tabs[getActiveTab().tIndex-1].activate();
		else
			getActiveTab().tipBadField();
	}
}
lastStep=function(){
	if( getActiveTab().tIndex > 0 )
		tabs[getActiveTab().tIndex-1].activate();
}

/*
	FORM FX
*/
reactivateForm=function(){
	$('enqFormTabs').set('tween',{duration:1000,transition:Fx.Transitions.Pow.easeOut, onComplete:function(){}  }).tween('opacity', 1 );
	$('enqFormLeftC').set('tween',{duration:1000,transition:Fx.Transitions.Pow.easeIn, onComplete:function(){}  }).tween('opacity', 0 );
}

deactivateForm=function(){

	// fade tabs //
	$('enqFormTabs').set('tween',{duration:1000,transition:Fx.Transitions.Pow.easeIn, onComplete:function(){}  }).tween('opacity', 0 );
	// rounded corner //
	$('enqFormLeftC').setStyle('opacity',0);
	$('enqFormLeftC').setStyle('display','block');
	$('enqFormLeftC').set('tween',{duration:1000,transition:Fx.Transitions.Pow.easeIn, onComplete:function(){}  }).tween('opacity', 1 );
	
	// scroll off inputs //	
	$('fCont').set('tween', {duration:2000, transition:Fx.Transitions.Quad.easeOut});
	$('fCont').tween('margin-top', 0 - $('enqResult').getPosition( $('fCont') ).y );
	$('fContHolder').set('tween', {duration:2000, transition:Fx.Transitions.Quad.easeOut});
	$('fContHolder').tween('height', $('enqResult').getSize( ).y );	
	
	hideBtn( $('fNavBack') );
	hideBtn( $('fNavSubmit') );
	hideBtn( $('fNavNext') );			

}

/*
	BUTTON FX FUNCTIONS
*/
hideBtn=function(_e){
	_e.set('tween',{
		duration:1000,
		transition:Fx.Transitions.Pow.easeOut, 
		onComplete:function(e){
			e.setStyle('display','none');
		}
	}).tween('opacity', '0' );
}

fadeBtn=function(_e){
	_e.set('tween',{duration:1000,transition:Fx.Transitions.Pow.easeOut}).tween('opacity', 0.25 );
}

showBtn=function(_e){
	_e.setStyles({
		display:'block'/*,
		opacity:0*/
	});
	_e.set('tween',{duration:1000,transition:Fx.Transitions.Pow.easeOut}).tween('opacity', 1 );
}


/*
	BUTTON FX
*/
reconfigButtons=function(){
	t = getActiveTab();
	if( t ){
		switch( t.tIndex ){
		case 0:
			fadeBtn( $('fNavBack') );
			hideBtn( $('fNavSubmit') );
			showBtn( $('fNavNext') );			
		break
		
		case tabs.length -1:
			showBtn( $('fNavBack') );
			hideBtn( $('fNavNext') );
			showBtn( $('fNavSubmit') );
		break;
		
		default:
			showBtn( $('fNavBack') );
			showBtn( $('fNavNext') );
			hideBtn( $('fNavSubmit') );
		break;
		
		}
	}
}


/*
	.:: TOOLTIPS ::
*//*
	void TOOLTIP SHOW ( anchor-element )
*/
toolTipA=function( _a ){
	if( !_a || !_a.tipText ) return;
	
	$('formTip').setStyle( 'display', 'block' );
	$('formTip').setStyle( 'width', 'auto' );
	$('formTip').setStyle( 'visibility', 'visible' );
	$('tipText').setProperty( 'text', _a.tipText );
	$('formTip').setStyle( 'width', $('tipText').getSize().x + 12 );
	
	x = _a.getPosition().x; //- $('formTip').getSize().x;
	y = _a.getPosition().y - $('formTip').getSize().y;
	
	if( _a.tipOff ){
		x+= _a.tipOff.x;
		y+= _a.tipOff.y;
	}
	
	$('formTip').setStyles( {left:x, top:y} );
}

/*
	void BINDTIPS( selector )
	adds hover over to anchor tags
*/
bindTips = function( _els, _offset, _autoSize ){
	
	for( var i = 0; i != _els.length; ++i ){
	
		if( _offset ) _els[i].tipOff = _offset;
		_els[i].tipText = new String(_els[i].getProperty('title'));
		_els[i].setProperty('title', null );
		_els[i].addEvent('mouseenter', function(){
			toolTipA(this);
		});
		
		_els[i].addEvent('mouseleave', function(){
			$('formTip').setStyles({ display:'none', visibility:'hidden' });
		});
	}
	
	return _els;
	
}

/*
	HIDE TIPS
*/
hideTips = function(){
	$('formTip').setStyles({ display:'none', visibility:'hidden' });
}



/*
	AUTOFILL STEP 3
*/
autoFillS3 = function(){
	$('s3name').setProperty( 'value', $('s2name').getProperty('value') );
	$('s3addr1').setProperty( 'value', $('s2addr1').getProperty('value') );
	$('s3addr2').setProperty( 'value', $('s2addr2').getProperty('value') );
	$('s3addr3').setProperty( 'value', $('s2addr3').getProperty('value') );
	$('s3addr4').setProperty( 'value', $('s2addr4').getProperty('value') );
	$('s3addr5').setProperty( 'value', $('s2addr5').getProperty('value') );
}


/*
	transfer values from 
	step 2 into step 3,
	only happens once
*/
step3hook = function(){
	if( !this.done ){
		/* ONLY IF HQ AND COMPANY NAME ARE FILLED */
		if( $('s2hq').getProperty('checked') > "" /*  && $('s2name').getProperty('value') > ""  */ ){
			autoFillS3();
		}
		this.done = true;
	}
};
step3hook.done = false;


/*
	INPUT FILED ACCEPT NUMBERS ONLY
*/
inputStripChars = function( _el ){

	var s  = $(_el).getProperty('value');
	var newStr = new String();
	for( var i = 0; i != s.length; ++i )
	{
		if( s.charAt(i) >= 0 && s.charAt(i) <= 9 )
			newStr += s.charAt(i);
	}
	$(_el).setProperty( 'value', newStr );	
			
}


/*
	DOMREADY
*/
window.addEvent( 'domready', function(){

	/*
		BIND TOOL TIPS
	*/
	bindTips( $$('a.rqTip'), {x:-32, y:8}, true );
	$('formTip').addEvents({
		mouseenter: function(){$('formTip').setStyles({ display:'block', visibility:'visible' });},
		mouseleave: function(){hideTips();}
	});

	/* anchors that fire another tool tip */
	displayTips = $$('a.rqTipDisplay');
	for( var i = 0; i !=  displayTips.length; ++i ){
		
		displayTips[i].addEvents({
			mouseenter: function(){
				a = this.getParent().getParent().getElements('a.rqTip');
				if(  a.length ){
					a[0].fireEvent('mouseenter');
				}
			},
			mouseleave: function(){hideTips();}
		});
		
	}
	
	
	
	/*
		BIND TAB OBJECTS
	*/
	tabEls = $$('.tab');
	stepEls = $$('.fTabCont');
	
	for( var i = 0; i != tabEls.length && i != stepEls.length && i != titles.length; ++i ){
		tabs[i] = tabEls[i];
		tabs[i].tIndex = i;
		//tabs[i].title = titles[i];
		tabs[i].stepEl = stepEls[i];
		tabs[i].setStyle( 'opacity', 0.75 );		
		
		/*
			ACTIVATE TAB FUNCTION
		*/
		tabs[i].activate = function( ){
			/*
				DEACTIVATE OTHER TAB
			*/
			hideTips();
			getActiveTab().dactivate();

			
			/*	ANIMATION	*/
			/*
			$('fCont').set('tween', {duration:1000, transition:Fx.Transitions.Quad.easeOut});
			$('fCont').tween('margin-top', 0 - this.stepEl.getPosition( $('fCont') ).y );
			
			if( this.hasBadField ){
				$('fContHolder').set('tween', {duration:1000, transition:Fx.Transitions.Quad.easeOut, onComplete:function(){getActiveTab().tipBadField();}});
			}
			else{
				$('fContHolder').set('tween', {duration:1000, transition:Fx.Transitions.Quad.easeOut});
			}
			
			$('fContHolder').tween('height', this.stepEl.getSize( ).y );
			*/
			/* NON-ANIMATED */
			$('fCont').setStyle( 'margin-top', 0 - this.stepEl.getPosition( $('fCont') ).y ); 
			$('fContHolder').setStyle( 'height', this.stepEl.getSize( ).y );
			if( this.hasBadField )
				this.tipBadField();
			
			
			/* CHANGE TITLE */
			this.getElements('.title')[0].setProperty('html', titles[this.tIndex].full );
			this.addClass('activeTab');
			this.setStyle('opacity', 1 );
			
			reconfigButtons();
		}

		/*
			DEACTIVATE TAB FUNCTION
		*/
		tabs[i].dactivate = function(){
			this.getElements('.title')[0].setProperty( 'html', titles[ this.tIndex ].short );
			this.removeClass('activeTab');
			this.setStyle('opacity', 0.75 );
		}


		/*
			VERIFY FIELDS
		*/
		tabs[i].stepValid=function(){
			/*
				CHECK FOR REQUIRED FIELDS
			*/
			hideTips();
			var rqs=this.stepEl.getElements( '.rq' );
			for( var i = 0; i != rqs.length; ++i ){
				if( rqs[i].getElements('input')[0].getProperty('value') <= "" ){
					this.hasBadField = true;
					this.badField = i;
					return false;
				}
				/*
					if this rquired filed has the class email
					check it is a valid email address
					also check if the next filed is an email confirm
				*/
				if( rqs[i].hasClass('email') )
				{
					eml1 = rqs[i].getElements('input')[0];
					if( eml1.getProperty('value').indexOf('@') <= 0 ){
							this.hasBadField = true;
							this.badField = i;
							return false;
					}
					
					/* is the next field an email aswell? */
					if( rqs[i+1]  && rqs[i+1].hasClass('emailConfirm') ){
						
						eml2 = rqs[i+1].getElements('input')[0];
						
						if( eml1.getProperty('value').toUpperCase() != eml2.getProperty('value').toUpperCase() ){
							this.hasBadField = true;
							this.badField = i+1;
							return false;
						}
					}
				}
				/* NUMERICAL */
				if( rqs[i].hasClass('numerical') ){
					var val = rqs[i].getElements('input')[0].getProperty('value');
					for( var valItr = 0; valItr != val.length; ++valItr ){
						if( !(val.charAt(valItr) >= 0 && val.charAt(valItr) <= 9) )
						{
							this.hasBadField = true;
							this.badField = i;
							return false;
						}
					}
				}
			}
		
			this.hasBadField = false;
			return true;
		}
		
		tabs[i].tipBadField=function(){
			hideTips();
			var rqs=this.stepEl.getElements( '.rq' );
			
			if( rqs[this.badField] )
				rqs[this.badField].getElements('.rqTip')[0].fireEvent('mouseenter');			
		}
		
	}
	
	/*
		ADD CLICK EVENTS
	*/
	//	for( var i = 0; i!= tabs.length; ++i ){	tabs[i].addEvent( 'click', function(){ this.activate(); });		}
	
	/* step 3 hook */
	$('next1').addEvent('click', function(){ if( getActiveTab().tIndex == 2 ) step3hook();  } );
	
	/*
		ACTIVATE TAB 0 AT STARTUP
	*/
	tabs[0].activate();
	
});