var abertos = 1;
var enviando = false;

function clickTAG( url ){
	window.open( url );
	return( false );
}

function abre_construtor(){
	window
		.open( 'http://construtor.com4.com.br' , '_blank' , 'width=800,height=600,left=' + ( screen.availWidth / 2 - 400 ) + ',top=' + ( screen.availHeight / 2 - 300 ) + '' );
}
/*
 * FUNCAO DO jquery.form PARA POST DE UPLOAD DE IMAGENS
 */
function fPost(){
	var options = {
		target			: '#resposta',
		beforeSubmit	: postRequest,
		success			: postResponse,
		clearForm		: true
	};
		
	//if ( $( '#fContato' ).ajaxForm != null )
	//	$( '#fContato' ).ajaxForm( options );
	
	if ( $( '#fConsulta' ).ajaxForm != null )
		$( '#fConsulta' ).ajaxForm( options );
}

function postRequest( formData , jqForm , options ){
	ret = true;
	
	if ( !enviando ) {
		enviando = true;
		$( "#resposta" ).html( "Enviando mensagem..." );
	} else {
		ret = false;
	}
	
	return( ret );
}

function postResponse( responseText , statusText ){
	enviando = false;
	$( "#resposta" ).html( "" );
	alert( "Obrigado pelo seu interesse em falar conosco, sua mensagem foi enviada com sucesso e em breve lhe retornaremos." );
}

function isEmail( email , com4 ) {
	var regex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	var parts = email.split( "@" );
	
	if ( com4 == undefined ) com4 = true;
	
	if ( ( parts.length == 1 ) && com4 ) {
		email = [ email , "@com4.com.br" ].join( "" );
	}
	
	return( regex.test( email ) );
}

function faleconosco() {
	var ret				= false;
	var departamento	= $( "form#fContato select#departamento" ).val();
	var nome			= $( "form#fContato input#nome" ).val();
	var email			= $( "form#fContato input#email" ).val();
	var ddd				= $( "form#fContato input#ddd" ).val();
	var fone			= $( "form#fContato input#fone" ).val();
	var cidade			= $( "form#fContato input#cidade" ).val();
	var estado			= $( "form#fContato input#estado" ).val();
	var assunto			= $( "form#fContato input#assunto" ).val();
	var mensagem		= $( "form#fContato textarea#mensagem" ).val();
	
	if ( enviando ) {
		alert( "Já estamos enviando sua mensagem, por favor aguarde..." );
	} else {
				if ( ( !departamento.length ) || ( departamento == "Selecione" ) ) alert( "Por favor informe o departamento que deseja falar." );
		else	if ( !nome.length ) alert( "Por favor, informe seu nome." );
		else	if ( !isEmail( email , false ) ) alert( [ nome , " por favor informe seu email." ] );
		else	if ( !fone.length ) alert( [ nome , " por favor informe seu telefone." ]  );
		else	if ( !assunto.length ) alert( [ nome , " por favor  informe o assunto da sua mensagem." ] );
		else	if ( !mensagem.length ) alert( [ nome , "por favor  informe sua mensagem." ] );
		else {
			$( "#resposta" ).html( "<b>Enviando mensagem...</b>" );
			enviando = true;
			ret = true;
		}
				
		if ( ret ) {
			ret = false;
			
			$.ajax( {
				type		: "POST" ,
				url			: "?session=contato.php&ajax" ,
				data		: {
					departamento	: departamento,
					nome			: nome,
					email			: email,
					ddd				: ddd,
					fone			: fone,
					cidade			: cidade,
					estado			: estado,
					assunto			: assunto,
					mensagem		: mensagem
				},
				dataType	: "json" ,
				success		: function( msg ){
					if ( msg.Sucesso !== true ) {
						alert( "Não foi possível enviar sua mensagem, por favor tente novamente." );
					} else {
						$( "#resposta" ).html( "" );
						$( "form#fContato input#nome" ).val( "" );
						$( "form#fContato input#email" ).val( "" );
						$( "form#fContato input#ddd" ).val( "" );
						$( "form#fContato input#fone" ).val( "" );
						$( "form#fContato input#cidade" ).val( "" );
						$( "form#fContato input#estado" ).val( "" );
						$( "form#fContato input#assunto" ).val( "" );
						$( "form#fContato textarea#mensagem" ).val( "" );
						alert( "Obrigado pelo seu interesse em falar conosco, sua mensagem foi enviada com sucesso e em breve lhe retornaremos." );
					}
				},
				complete	: function( msg ){
					enviando = false;
				}
			} );
		}
	}
	
	return( ret );
}

function loginWebMail( clique ) {
	var ret		= false;
	var user	= $( "form#webmail input#log1" ).val();
	var pswd	= $( "form#webmail input#log2" ).val();
	
	if ( clique == undefined ) clique = false;
		
	if ( user.length && pswd.length ) {
		if ( isEmail( user ) ) {
		
			var prefix	= "http://mail";
			var posfix	= "login.html";
			var domain	= user.split( "@" );
			var action	= "com4.com.br";
			
			if ( domain.length == 2 ) {
				action = domain[ 1 ];
				
				if ( domain[ 1 ] == "com4.com.br" ) {
					posfix	= "login.php"; //o merak com4 tem um endereço
					$( "form#webmail input#log1" ).val( domain[ 0 ] );
					
					$( "form#webmail" ).attr( "action" , [ [ prefix , action ].join( "." ) , posfix ].join( "/" ) );
				}
				else if(domain[ 1 ] == "netzap.com.br" || domain[ 1 ] == "meganet.com.br" || domain[ 1 ] == "megasinal.com.br"){
					
					posfix	= "login.html"; //o merak com4 corp para este dominio tem um endereço
					$( "form#webmail" ).attr( "action" , [ [ prefix , action ].join( "." ) , posfix ].join( "/" ) );
				}
				else{
				
					posfix	= "login.php"; //o merak com4 corp para este dominio tem um endereço
					$( "form#webmail" ).attr( "action" , [ [ prefix , action ].join( "." ) , posfix ].join( "/" ) );
				}
			}
			
			
			
			if ( sha1sumbit() ) {
				if ( clique ) {
					$( "form#webmail" ).submit();
					$( "form#webmail input" ).val( "" );
				}
				
				ret = true;
			}	
		} else {
			alert( "O nome de usuário é inválido" );
		}
	} else {
		if ( !user.length ) {
			alert( "Você precisa informar o nome de usuário da sua conta de email." );
		} else {
			alert( "Você precisa informar a senha da sua conta de email." );
		}
	}
	
	return( ret );
}

function homeWebMail(){
	var assinante_class = $( ".assinante" ).attr( "class" );

	/*
	 * home - webmail e central do assinante assinante esconde os titulos e
	 * exibe lista para trocar entre paineis
	 */
	$( ".webmail h3" ).remove();
	$( ".assinante h3" ).remove();

	lista = "<div>";
	lista += "    <ul>";
	lista += "        <li class=\"l00\"><a href=\"#\">WEB Mail</a></li>";
	lista += "        <li class=\"l01\"><a href=\"#\">Central do Assinant</a></li>";
	lista += "    </ul>";
	lista += "</div>";

	$( ".webmail" ).prepend( lista );
	$( ".assinante" ).prepend( lista );

	if ( assinante_class != undefined ) {
		if ( assinante_class.indexOf( "escondido" ) >= 0 ) {
			$( ".webmail ul li.l01" ).remove();
			$( ".webmail" ).show();
			$( ".webmail" ).addClass( "webmail_aoff" );
			$( ".assinante" ).hide();
		}
	}

	$( ".webmail li.l01 a" ).click( function(){
		$( ".webmail" ).hide();
		$( ".assinante" ).animate( {
			opacity : 'show'
		} , 300 );
		return false;
	} );

	$( ".assinante li.l00 a" ).click( function(){
		$( ".webmail" ).animate( {
			opacity : 'show'
		} , 300 );
		$( ".assinante" ).hide();
		return false;
	} );
}

/*
 * ON LOAD DA PAGINA
 */
$( document ).ready( function(){
	homeWebMail();
	fPost();
	
	$( ".assinante li.l00 a" ).click();
	
	$( "form#fContato input#botao_enviar" ).click( function(){
		faleconosco();
	} );
	
	$( "#abreChat" ).click( function(){
		url = "/vcomchat/";
		name = "vcomchat";
		w = "600";
		h = "450";
		xPos = ( screen.width - w ) / 2;
		yPos = ( screen.height - h ) / 2;
		config = 'left=' + xPos + ',screenX=' + xPos + ',top=' + yPos + ',screenY=' + yPos + ',width=' + w + ',height=' + h;
		newWindow = window.open( url , name , config );

		return false;
	} );

	$( "#abreAtendente" ).click( function(){
		url = "/vcomchat/atendente.php";
		name = "vcomchat";
		w = "900";
		h = "600";
		xPos = ( screen.width - w ) / 2;
		yPos = ( screen.height - h ) / 2;
		config = 'left=' + xPos + ',screenX=' + xPos + ',top=' + yPos + ',screenY=' + yPos + ',width=' + w + ',height=' + h;
		newWindow = window.open( url , name , config );

		return false;
	} );

	$( "#imgAcessar" ).click( function(){
		var cidade = $( "#cidade" ).val();
		var partes = [];

		if ( cidade != undefined ){
			partes = cidade.split( ":" );
			cidade = parseInt( partes[ 0 ] );

			if ( partes.length == 2 )
				cidade = partes[ 1 ];
		}

		if ( ( ( partes.length == 2 ) && ( cidade.length == 2 ) ) || ( ( partes.length == 1 ) && ( cidade >= 0 ) ) ){
			window.location.href = [ window.location.href , cidade ].join( "&cidade=" );
		}
	} );


	$( ".menu li" ).hover( function(){
		var obj = $( this ).attr( "class" );

		if ( obj !== undefined && ( obj != "s00" ) ) {
			var obj2 = obj.replace( /l/ , "h" );

			if ( obj2 != "" ){
				$( ".menu li." + obj ).addClass( obj2 );
				$( ".menu li." + obj + " ul" ).animate( {
					opacity : 'show'
				} , 300 );
			}
		}
	} , function(){
		var obj = $( this ).attr( "class" );

		if ( obj !== undefined && ( obj != "s00" ) ) {
			var obj2 = ( obj = obj.substr( 0 , 3 ) ).replace( /l/ , "h" );

			if ( obj2 != "" ){
				$( ".menu li." + obj ).removeClass( obj2 );
				$( ".menu li." + obj + " ul" ).hide();
			}
		}
	} );

	/*
	 * ROLOVER DO SUB MENU
	 */
	$( ".subMenu li" ).hover( function(){
		$( this ).addClass( "hover" );
	} , function(){
		$( this ).removeClass( "hover" );
	} );

	$( "a[ rel=grupo ]" ).each( function(){
		var grupo = parseInt( $( this ).attr( "name" ) );

		if ( grupo != 0 ) {
			$( this ).addClass( "closed" );
			$( "tr[ rel=grupo" + grupo + "]" ).each( function(){
				$( this ).hide();
			} );
		}
	} );

	$( "a[ rel=grupo ]" ).click( function(){
		var atual = parseInt( $( this ).attr( "name" ) );
		var linhas = [];
		
		if ( $(this).attr( "class" ) == "closed" ) {
			$(this).removeClass( "closed" );
			
			$( "tr[ rel=grupo" + atual + "]" ).each( function(){
				$(this).show();
			} );
			
			++abertos;
		} else if ( abertos > 1 ) {
			$(this).addClass( "closed" );
			
			$( "tr[ rel=grupo" + atual + "]" ).each( function(){
				$(this).hide();
			} );
			
			--abertos;
		}
	} );
	if ( $( '.tip' ).corner !== undefined ) {
		$( '.tip' ).corner();
	}
} );
