/*
Credits: Bit Repository
URL: http://www.bitrepository.com/web-programming/ajax/login-form.html
*/

// Preload Images
img1 = new Image(16, 16);  
img1.src="/common/spinner.gif";

img2 = new Image(220, 19);  
img2.src="/common/loader-bar.gif";

function FormFiring() {	

	// ---------------------------------------------------
	// MINI LOG IN FORM

	if ($('form_login_mini')) {
		$('form_login_mini').addEvent('submit', function(e) {
			e.stop();
			$('ajax_loading').setStyle('display','block');
			$('submitbtn').setStyle('display','none');
			this.set('send', { 
				method:'post',
				evalScripts: true, 
				onComplete: function(response) {
					$('ajax_loading').setStyle('display','none');
					$('mini_login').set('html', response);
					loadResponseForm();
					FormFiring();
				}
			});
			this.send();
		});
	} // end form_login





	function reLoadForm_Register() {
		var req = new Request({  
			method: 'get',  
			url: '/includes/forms/register_form.php', 
			update: $('message-here')
		}).send(); 
	}



	// ---------------------------------------------------
	// LOGOUT MINI

	if ($('logout_form_mini')) {
		$('logout_form_mini').addEvent('submit', function(e) {
			e.stop();
			e.preventDefault();
			evalScripts:true;
			$('ajax_loading_logout').setStyle('display','block');
			$('logoutbtn').setStyle('display','none');
			$('logoutbtn').setProperty('disabled', 'true');
	
			// Set the options of the form's Request handler.
			// ("this" refers to the $('login') element).

			this.set('send', { 
			method:'post',
			evalScripts: true, 
			onComplete: function(response) {
				$('ajax_loading_logout').setStyle('display','none');
				$('mini_login').set('html', response);
				loadResponseForm();
				FormFiring();
				return false;
			}});
			this.send();
		});
	} // end 


	// ---------------------------------------------------
	// BLOG RESPONSE

	if ($('reply_form')) {
		$('reply_form').addEvent('submit', function(e) {
			e.stop();
			e.preventDefault();
			evalScripts:true;
			$('new_comments').set('html', '<center><img src="/common/loader-bar.gif" /></center>');

			this.set('send', { onComplete: function(response) {
				$('new_comments').set('html', response);
//				FormFiring();
				$$('#reply_form textarea').set('value','');
				getPostResponses();
			}});
			this.send();
		});
	} // end 






// ---------------------------------------------------
// PREVENT RETURN KEY SUBMIT
var inputs = $$('.login_forms form');  
$each(inputs,function(el,i) {  
	el.addEvent('keypress',function(e) {  
		if(e.key == 'enter') {   
			e.stop();   
			if(inputs[i+1]) { inputs[i+1].focus(); }  
			//last one?  
			if(i == inputs.length-1) { $('submit').focus(); }  
		}  
	});  
});

}



// ---------------------------------------------------
function getPostResponses() {
	var req = new Request.HTML({  
		method: 'get',  
		url: '/blog/PostResponses.php',  
//		evalScripts: true,
//		headers: {'Pragma': 'no-cache'},
		data: { 'id' : $('postresponseid').value },  
		onRequest: function() {

		},  
		update: $('PostResponses'),  
		onComplete: function(response) { 

		} 
	}).send(); 

}





// ---------------------------------------------------
function loadResponseForm() {
	if ($('ResponseForm')) {
		var req = new Request.HTML({  
			method: 'get',  
			url: '/blog/response_form.php',  
			evalScripts: true,
			headers: {'Pragma': 'no-cache'},
			data: { 'id' : $('postresponseid').value },  
			onRequest: function() {

			},  
			update: $('ResponseForm'),  
			onComplete: function(response) { 
				FormFiring();
			} 
		}).send(); 
	}
}




// ---------------------------------------------------
function checkContributeForm() {
//	alert('dog');
	if ($('post_story_div')) {
//		alert('bark');
		document.location.href = "/post_story.php";
	}
}



// ---------------------------------------------------
function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}



// ---------------------------------------------------
function go_to_private_page()
{
window.location = 'private.php'; // Members Area
}


function showEle(theDiv) {
	if ($(theDiv)) {
		$(theDiv).setStyle('display','block');
	}
}

function hideEle(theDiv) {
	if ($(theDiv)) {
		$(theDiv).setStyle('display','none');
	}
}


function showfadeEle(theDiv) {	
	if ($(theDiv)) {
	$(theDiv).set('opacity',0);
	showEle(theDiv);
	new Fx.Morph(theDiv, {duration:200}).start({opacity:1});	
	}
}

function hidefadeEle(theDiv) {
	if ($(theDiv)) {
	$(theDiv).set('opacity',1);
	showEle(theDiv);
	new Fx.Morph(theDiv, {duration:200}).start({opacity:0});	
	}
}



// ---------------------------------------------------
function fadeLog() {
	var div = $('log_login').setStyles({
		display:'block'
	});

	div = $('log_login');

	new Fx.Morph(div, {duration:1000, onComplete: function() {
		new Fx.Morph(div, {duration:1000}).start({opacity:0});
	}}).start({'opacity':1}); 

}


// ---------------------------------------------------
function confirmContributeForm() {
	document.location.href = "/pg-Post-Story-Success.html";
}

