var div_response 	= "#div_response";
/*
$(document).ready(function()
{ 
    alert($('#form'));
    
    var options = { 
		beforeSubmit:  	showRequest,
        success:       	showResponse
    };
    $('#form').ajaxForm(options);
}); 
*/
function sendForm(myurl)
{
    blockUI();
    
    var post = $('#form').serialize();
    $.ajax({
        type: 'POST',
        url: myurl,
        data: post,
        success: function(msg){ showResponse(msg); }
    });
}

function sendForm2(myurl)
{
    var post = $('#form').serialize();
    $.ajax({
        type: 'POST',
        url: myurl,
        data: post,
        success: function(msg){ showResponse(msg); }
    });
}

function sendFormWithAppend(myurl)
{
    blockUI();
    
	var post = $('#form').serialize();
    $.ajax({
		async: false,
        type: 'POST',
        url: myurl,
        data: post,
        success: function(msg){ showResponseAndAppend(msg); }
    });
}

function showResponseAndAppend(responseText)
{ 
    //REDIRECT THE OUTPUT
    if(responseText == "OK_JOIN"){
		window.location='?plugin=members&act=joinOK';
    }
        
    if(responseText == "OK_BACK_USERS"){
        alert("Item salvo com sucesso.");
		window.location='?plugin=members&act=members_home';
    }
	
	if(responseText == "OK_BACK"){
        alert("Item salvo com sucesso.");
		
		history.go(-1);
    }
    
    if(responseText == "OK_NEWS"){
        alert("Item salvo com sucesso.");
		window.location='?plugin=calendar&act=show#'+$('#date_anchor').val();
    }
    
    if(responseText == "OK_MESSENGER"){
		window.open('pages/messenger_preview.php', 'mmwin', 'width=600,height=600');
    }
    
	unblockUI();
    
	if(responseText == "OK_JOIN" || responseText == "OK_BACK_USERS" || responseText == "OK_BACK" || 
		responseText == "OK_NEWS" || responseText == "OK_MESSENGER")
		return responseText;
	
	//alert(responseText);
}


function showResponse(responseText)
{ 
    unblockUI();

    //REDIRECT THE OUTPUT
    if(responseText == "OK_JOIN"){
        window.location='?plugin=members&act=joinOK';
        return;
    }
        
    if(responseText == "OK_BACK_USERS"){
        alert("Item salvo com sucesso.");
        window.location='?plugin=members&act=members_home';
        return;
    }
	
	if(responseText == "OK_BACK"){
        alert("Item salvo com sucesso.");
        history.go(-1);
        return;
    }
    
    if(responseText == "OK_NEWS"){
        alert("Item salvo com sucesso.");
        window.location='?plugin=calendar&act=show#'+$('#date_anchor').val();
        return;
    }
    
    if(responseText == "OK_MESSENGER"){
        window.open('pages/messenger_preview.php', 'mmwin', 'width=600,height=600');
        return;
    }
    
    alert(responseText);
}

function blockUI()
{
	var loader_img = "<img src='imgs/private/loader.gif'> ";
	
	jQuery.blockUI({
		message: loader_img + "<br><b>Aguarde...</b>",
		css: { 
			border: 'none', 
			padding: '15px', 
			backgroundColor: '#fff', 
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			opacity: '.5', 
			color: '#000'
		}
	}); 		
} 

function unblockUI()
{
    jQuery.unblockUI();
}


function checa_email(){
		//document.getElementById('iframex').src='checa_email.php?email='	+ email;
		var caracteres = document.getElementById('email_indicacao').value;
		var arroba = 0;
   		for(i=0;i< caracteres.length;i++){
			if(caracteres[i] == "@"){
				arroba = arroba + 1;
			}			
		}
		if(arroba > 1){
			alert('Apenas um email pode ser indicado. Verifique por favor.');
			document.getElementById('confirma_indicacao').value = '';
			document.getElementById('email_indicacao').style.border = '2px solid #FF0000';
		} else {
			document.getElementById('iframex').src='checa_email.php?email='	+ document.getElementById('email_indicacao').value;
		}
}
