glMashFormBackColor = null;
glMashFormMode = 1;

function tabstyle(xy)
{
	xy.setStyle({borderTop: 'solid 1px #a47979'});
	xy.setStyle({borderLeft: 'solid 1px #a47979'});
	xy.setStyle({borderRight: 'solid 1px #a47979'});
	xy.setStyle({background: '#e0c8c8'});
}

function nostyle(xy)
{
	xy.setStyle({border: 'none'});
	xy.setStyle({background: 'transparent'});
}


function toggleLoginSignUpForms(how) {
	if(how==1) {
		$('signup-tab-content').show();
		$('signin-tab-content').hide();
		$('guest-tab-content').hide();
		$('form_type').value = 1;
	}

	if(how==2) {
		$('signup-tab-content').hide();
		$('signin-tab-content').show();
		$('guest-tab-content').hide();
		$('form_type').value = 2;
	}

	if(how==3) {
		$('signup-tab-content').hide();
		$('signin-tab-content').hide();
		$('guest-tab-content').show();
		$('form_type').value = 3;
	}

	glMashFormMode = parseInt(how);

	var b1 = $('signup-tab').getStyle('background'),
		b2 = $('signin-tab').getStyle('background'),
		b3 = $('guest-tab').getStyle('background');
	if(typeof b1!="undefined" && b1!="" && glMashFormBackColor==null) {glMashFormBackColor = b1;}
	if(typeof b2!="undefined" && b2!="" && glMashFormBackColor==null) {glMashFormBackColor = b2;}
	if(typeof b3!="undefined" && b3!="" && glMashFormBackColor==null) {glMashFormBackColor = b3;}

	var str = new String(document.location), 
		x = $('signup-tab'), y = $('signin-tab'), z = $('guest-tab');
	if(str.match(/xyztest/i))
		alert(glMashFormBackColor);

	if(how==1) {
		tabstyle(x);
		nostyle(y);
		nostyle(z);	
	}

	if(how==2) {
		nostyle(x);
		tabstyle(y);
		nostyle(z);
	}

	if(how==3) {
		nostyle(x);
		nostyle(y);
		tabstyle(z);
	}
}

function changeCommentSize(d){
	var el = document.getElementById("comment");
	var height = parseInt(el.style.height);
	if(!height && el.offsetHeight)
		height = el.offsetHeight;
	height += d;
	if(height < 20) 
		height = 20;
	el.style.height = height+"px";
}		

function j_logout(){
	var url = 'logout.php'
    new Ajax.Request(url, {
        onComplete: completeLogout
    });
}

function completeLogout(response, data){
	location.reload(true);
} 

function j_login(){
	var params = 'pw='+$('signin_email_password').value+'&email='+$('signin_email_text').value; 
	var url = 'jlogin.php'
    new Ajax.Request(url, {
    	postBody: params, 
        onComplete: completeJLogin
    });
}

function completeJLogin(response, data){
	if (response.responseText == 'true'){
		location.reload(true);
	}else{
		alert('Check your email and password');
	}
} 

function jlogin_header(){
	var params = 'pw='+$('lpassword').value+'&email='+$('lemail').value; 
	var url = 'jlogin.php'
    new Ajax.Request(url, {
    	postBody: params, 
        onComplete: completeJLogin_Header
    });
}

function completeJLogin_Header(response, data){
	if (response.responseText == 'true'){
		location.href = 'logincomplete.php';
	}else{
		alert('Check your email and password');
	}
} 

function check_adding_comment(){
	
	if($F('comment').strip()=='') { 
		alert('Enter your comment please.'); 
		$('comment').focus();
		return false;
	}
	
	if ($('form_type').value == 4){
		return true;
	}
	
	if ($('form_type').value == 1){
		var params = 'email='+$('email').value; 
		var url = 'check_email.php'
	    new Ajax.Request(url, {
	    	postBody: params, 
	        onComplete: completeEmailCheck
	    });
	    return false;
	}
	if ($('form_type').value == 3){
		if ($('guest_author').value == ''){
			alert('please enter your name\nall fields must be filled');
			return false;
		}
		if ($('guest_email').value == ''){
			alert('please enter your email\nall fields must be filled');
			return false;
		}
		return true;
	}
	
	if ($('form_type').value == 2){
		alert('please login before you can post comment');
		return false;
	}
	return false;
}

function completeEmailCheck(response, data){
	if (response.responseText == 'true'){
		alert('entered email already exist!\nplease login before you can post comment');
		toggleLoginSignUpForms(2);
		$('signin_email_text').value = $('email').value;
	}else{
		window.location = root + '/join_us.php';
	}
	
}