$(document).ready(function() {
	
	$("#webmail-login").hide();
	
	$("#do-login-painel").validationEngine({
		promptPosition: "topRight",
		validationEventTriggers:"submit",
		scroll:false,
		boxtype: "alert"
	});
	
	$("#do-login-webmail").validationEngine({
		promptPosition: "topRight",
		validationEventTriggers:"submit",
		scroll:false,
		boxtype: "alert"
	});
	
	$("#btn-painel-change").click(function() {
		if( $('#webmail-login').is(':visible') ) {
			$('#webmail-login').hide();
			$('#btn-painel-change').removeClass('off-link');
			$('#btn-webmail-change').addClass('off-link');
			$('#btn-header').addClass('btn-header-2');
			$('#painel-login').show();
		} 
	});
	
	$("#btn-webmail-change").click(function() {
		if( $('#painel-login').is(':visible') ) {
			$('#painel-login').hide();
			$('#btn-webmail-change').removeClass('off-link');
			$('#btn-header').removeClass('btn-header-2');
			$('#btn-painel-change').addClass('off-link');
			$('#webmail-login').show();
		}
	});
});


function baloon(caller,promptText,posicao,setwidth) {
	
	deleteItself = "." + $(caller).attr("id") + "divBaloon"
	
	if($(deleteItself)[0]){
		$(deleteItself).stop();
		$(deleteItself).remove();
	}
	
	var divBaloon = document.createElement('div');
	var baloonContent = document.createElement('div');
	var linkTofield = $(caller).attr("id") + "divBaloon";
	linkTofield = linkTofield.replace(/\[/g,""); 
	linkTofield = linkTofield.replace(/\]/g,"");
	$(divBaloon).addClass("baloon");
	$(divBaloon).addClass(linkTofield);
	
	$(baloonContent).addClass("content");
	
	$("body").append(divBaloon);
	$(divBaloon).append(baloonContent);
	
	var arrow = document.createElement('div');
	
	$(arrow).addClass("Arrow");
	$(divBaloon).append(arrow);
		
	if( posicao == "bottomLeft" || posicao == "bottomRight" ) {
		$(arrow).addClass("ArrowBottom");
		$(arrow).html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');
	}
	
	if( posicao == "topLeft" || posicao == "topRight" ) {
		$(divBaloon).append(arrow);
		$(arrow).html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');
	}
	
	$(baloonContent).html(promptText);
	
	callerTopPosition = $(caller).offset().top;
	callerleftPosition = $(caller).offset().left;
	callerWidth =  $(caller).width();
	inputHeight = $(divBaloon).height();
	
	/* POSITIONNING */
	if(posicao == "topRight"){callerleftPosition +=  callerWidth -30; callerTopPosition += -inputHeight -10; }
	if(posicao == "topLeft"){ callerTopPosition += -inputHeight -10; }
	
	if(posicao == "centerRight"){ callerleftPosition +=  callerWidth +13; }
	
	if(posicao == "bottomLeft"){
		callerHeight =  $(caller).height();
		callerleftPosition = callerleftPosition;
		callerTopPosition = callerTopPosition + callerHeight + 15;
	}
	if(posicao == "bottomRight"){
		callerHeight =  $(caller).height();
		callerleftPosition +=  callerWidth -30;
		callerTopPosition +=  callerHeight + 15;
	}
	$(divBaloon).css({
		top:callerTopPosition,
		left:callerleftPosition,
		opacity:0
	})
		
	$(divBaloon).animate({"opacity":0.90},function(){return true;});
}

function baloonkill(caller) {
	var deleteItself = "." + $(caller).attr("id") + "divBaloon";
	
	$($(deleteItself)[0]).fadeOut(150,function(){
			$($(deleteItself)[0]).remove();
	}); 
}

/*
 * OLD FUNCTIONS
 */
function abrir( u, n, w, h ) { 
	window.open( u, n, 'status=yes,resizable=yes,scrollbars=yes,width='+w+',height='+h+'' ) ; 
}

function form_compare_field( f1 , f2, dv ) {
	return form_e_control( dv, !compare( f1, f2 ) ) ;
}	

function form_is_email( v1, dv ) {
	return form_e_control( dv, !is_email( v1 ) ) ;
}

function do_register( oForm ) { 
	return true ; 
}


/* lib */	

function show_layer( f, d ) { 
	document.getElementById(f).style.display = d ? "block" : "none" ;
}
//function sl( f, d ) { show_layer( f, d ) ; } 

function toggle(id) {
    var objDiv = document.getElementById(id);
	objDiv.style.display = (objDiv.style.display == "block") ? "none" : "block" ;
}

function compare( v1, v2 ) { 
	return ( v1 == v2 ) ? true : false ; 
}

function is_email( email ) {
	var er = /[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/i;
	return (er.test(email.toString().toLowerCase())) ; 
}

/* a-z 0-9 */
function nospecialchars(objvalue){
	objcheck = objvalue.replace(/[^a-z0-9]/i, "") ;
	for( var i = 0 ; i < objvalue.length ; i++) {
		objcheck = objcheck.replace(/[^a-z0-9]/i, "") ;
	}
	return objcheck ;
}


function clearForm( oForm ) {
	if ( typeof( oForm ) == 'string' ) oForm = getElem( oForm );
	if ( oForm != null && oForm.elements != null ) {
		for ( var i = 0; i < oForm.elements.length; i++ ) {
			sType = new String(oForm.elements[ i ].type);
			if ( sType.indexOf( 'text' ) > -1 ) { oForm.elements[ i ].value = ''; 
			} else if ( sType.indexOf( 'select' ) > -1 ) { oForm.elements[ i ].selectedIndex = -1;
			} else if ( sType.indexOf( 'check' ) > -1 ) { oForm.elements[ i ].checked = false;
			}
		}
	}
}


function remove_accents(fieldvalue) {
	var noaccept = 'áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';
	var accept = 'aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';
	var correct = '';
	for( i=0 ; i < fieldvalue.length ; i++ ) {
		if ( noaccept.search( fieldvalue.substr(i,1) ) >= 0 ) {
			correct += accept.substr(noaccept.search(fieldvalue.substr(i,1)),1);
		} else {
			correct += fieldvalue.substr(i,1);
		}
	}
	return correct;
}


function String_LetNormal() {
	if ( this.length <= 0 ) return this ;
	s = this.toLowerCase() ;
	var aMask = new Array(
	["a","à"], ["a","ã"], ["a","á"], ["a","ä"], ["a","â"],
	["e","è"], ["e","ë"], ["e","é"], ["e","ê"],
	["i","î"], ["i","ì"], ["i","ï"], ["i","í"],
	["o","ó"], ["o","ö"], ["o","ò"], ["o","õ"], ["o","ô"],
	["u","ú"], ["u","ü"], ["u","ù"], ["u","û"],
	["c","ç"]
	);
	for ( var i = 0 ; i < aMask.length ; i++ ) {
		while ( s.indexOf( aMask[i][1] ) != -1 ) { s = s.replace(aMask[i][1],aMask[i][0]) ; }
	}
	return s ;
}
String.prototype.toNormal = String_LetNormal ;


function CleanUpString() {
	if ( this.length <= 0 ) return this ;
	s = this ;
	var sResult = "" ;
	for ( var i = 0 ; i <= s.length ; i++ ){
		var j = s.charAt( i ) ;
		if ( isNaN( j ) ) {
			if ( ( ( ( j <= "Z" && j >= "A" ) || ( j <= "z" && j >= "a" ) ) || ( j <= "." && j >= "-" ) || ( j == "_" ) ) ) sResult += j ;
		} else sResult += j ;
	}
	return sResult ;
}
String.prototype.CleanUpString = CleanUpString ;


function LetOnlyNumbers(){
	if ( this.length <= 0 ) return this ;
	s = this ; 
	var newnumber = "" ;
	for ( i = 0 ; i <= s.length ; i++ ) {
		if (! isNaN( s.charAt( i )) && s.charAt( i ) != " " ) newnumber = newnumber + s.charAt( i ) ;
	}
	return newnumber ;
}
String.prototype.LetNumbers = LetOnlyNumbers ;


function String_NoWhiteSpace(){
	if ( this.length <= 0 ) return this ;
	s = this ; 
	while( s.indexOf( ' ' ) != -1 ) s = s.replace(' ','') ;
	return s ; 
}
String.prototype.NoWhiteSpace = String_NoWhiteSpace ; 


function isSerialNumber ( s, iLen ) {
	var sResult = "" ; var J = 0 ;
	if ( s.length < iLen ) return false ;
	for ( var i = 0 ; i < s.length ; i++ ) { if ( !isNaN(s.charAt(i)) ) { J++ ; sResult += s.charAt(i) ; } }
	if ( J < iLen ) return false ;
	return true ;
}
   
function ltrim( s ) { var i = 0 ; while( s.substring(0,1) == " " ){ s = s.substring(1,s.length) ; i++ ; } return s ; }

function rtrim( s ) { var i = 0 ; while( s.substring(s.length-1,s.length) == " " ){ s = s.substring(0,s.length-1) ; i++ ; } return s ; }

function trim( s ) { return rtrim(ltrim(s.value)) ; }




