	var tpEnd = ['Comercial','Entrega','Cobrança','Residencial'];
	var tpTel = ['Comercial','Celular','Residencial','Fax','Recado'];
	var uf = ['AC','AL','AM','AP','BA','CE','DF','ES','GO','MA','MG','MT','MS','PA'
	  			,'PB','PE','PI','PR','RJ','RN','RO','RR','RS','SC','SE','SP','TO'];


function loadPage(page)
{
 window.top.location=page;
}
/**
 * cria uma float estilo lightBox com uma div branca ao centro
 * @return
 */
function createFloatBox(){
	$.create('div',{attributes: {id:'editFloat',style:'position:absolute;top:0;left:0;width:100%;height:'+$(document).height()
														+'px;background-color:#000;opacity: 0.8; filter: alpha(opacity=80);'}}).appendTo('body');
	$.create('div',{attributes: {id:'containerFloat',style:'width:100%;position:absolute;top:30%;background-color:transparent;' }}).appendTo('body');
	$.create('div',{attributes: {id:'edit_float'}}).addClass('div_flutuante').appendTo('#containerFloat');
	
}
/**
 * destroi a div float lightBox
 * @return
 */
function removeFloat(){
	$('#editFloat').remove();$('#containerFloat').remove();
}


function createSelect(opt,select){
	var returnOtions = Array();
	if(select){
	returnOtions[0] = $.create('option',{attributes: {value: '',selected:'selected'}});			
	 $.each(opt, function (i, val) { returnOtions[i+1] = $.create('option',{attributes: {value: val.toLowerCase().replace('ç','c')}}).html(val);});
	}else
		 $.each(opt, function (i, val) { returnOtions[i] = $.create('option',{attributes: {value: val.toLowerCase().replace('ç','c')}}).html(val);});			
	return returnOtions;
}
function rmForm(id,container){
	//se existirem mais tabelas de formularios excluir a selecionada// se não apenas limpar os campos
	if($('#tabela_'+container).find('table').size()>1){
		$('#'+container+'_'+id).remove();
		$('#hr_'+container+'_'+id).remove();
		showButton('#tabela_'+container);
	}else{
		$('#'+container+'_'+id).find("input").each(function() {$(this).attr('value','');});
		$('#'+container+'_'+id).find("select").each(function() {$(this).find("option").each(function(i) {if(i==0)$(this).attr('selected','selected');});});
	}
}
function showButton(id){
	$(id).find("img").each(function(i) {
		if($(this).attr('name')=='mais')
        $(this).attr('style','display:none');
	});
	$(id+' img[name=mais]:last' ).attr('style','display:inline');				
}
var contEnd = 0;	
function addFormEnd(){
	var tabela = contEnd;
	var imgs = [$.create('div',{attributes: {style: 'width:38pt;float:left' },
								children: [$.create('img', {attributes: {id:'menos_'+contEnd, name:'menos' ,src: 'images/icon_menos.png'}}).addClass('pointer').click(function() {rmForm(tabela,'endereco');}),
											$.create('img', {attributes: {id:'mais_'+contEnd, name:'mais' ,src: 'images/icon_mais.png'}, events: {click: addFormEnd}})]}).addClass('pointer'),
				$.create('select',{attributes: {id:'tipo_endereco_'+contEnd, name:'endereco['+tabela+'][tipo]'}, children:createSelect(tpEnd,false)})];
	var tr1 = [ $.create('td',{attributes: {width: '130' }, children: 'Endereço:'}),
				$.create('td',{attributes: {colspan: 6}, children: imgs })];
	var tr2 = [ $.create('td',{children: 'Logradouro:'}),
				$.create('td',{attributes: {width: '238' },children: $.create('input',{attributes: {id:'logradouro_'+contEnd,name:'endereco['+tabela+'][logradouro]',type:'text',style:'width:200px'}})}),
				$.create('td',{children: 'Nº.:'}),
				$.create('td',{children: $.create('input',{attributes: {id:'numero_'+contEnd,name:'endereco['+tabela+'][numero]',type:'text',style:'width:40px'}})}),
				$.create('td',{children: 'Compl.:'}),
				$.create('td',{children: $.create('input',{attributes: {id:'complemento_'+contEnd,name:'endereco['+tabela+'][complemento]',type:'text',style:'width:70px'}})})];
	var tr3 = [ $.create('td',{children: 'Cidade:'}),
				$.create('td',{children: $.create('input',{attributes: {id:'cidade_'+contEnd,name:'endereco['+tabela+'][cidade]',type:'text',style:'width:200px'}})}),
				$.create('td',{children: 'Bairro:'}),
				$.create('td',{attributes: {colspan: 3},children: $.create('input',{attributes: {id:'bairro_'+contEnd,name:'endereco['+tabela+'][bairro]',type:'text',style:'width:150px'}})})];
	var tr4 = [ $.create('td',{children: 'CEP:'}),
				$.create('td',{children: $.create('input',{attributes: {id:'cep_'+contEnd,name:'endereco['+tabela+'][cep]',type:'text',style:'width:100px'}}).addClass('cep')}),
				$.create('td',{children: 'Estado:'}),
				$.create('td',{attributes: {colspan: 3},children: $.create('select',{attributes: {id:'estado_'+contEnd,name:'endereco['+tabela+'][estado]'},children:createSelect(uf,true)})})];
	//cria tabela com seus elementos
	$.create('hr',{attributes: {id: 'hr_endereco_'+contEnd}}).appendTo('#tabela_endereco');
	$.create('table', {attributes: {id: 'endereco_'+contEnd,cellpadding: 0,cellspacing: 0,border: 0,width: '100%'}}).appendTo('#tabela_endereco');
	$.create('tr',{children: tr1}).appendTo('#endereco_'+contEnd);
	$.create('tr',{children: tr2}).appendTo('#endereco_'+contEnd);
	$.create('tr',{children: tr3}).appendTo('#endereco_'+contEnd);
	$.create('tr',{children: tr4}).appendTo('#endereco_'+contEnd);
	$(function($){$(".cep").setMask("cep",{autoTab:'false'});}); 

	showButton('#tabela_endereco');
		
	contEnd++;

			
}
var contTel = 0;
function addFormTel(){
	var tabela = contTel;
	var imgs = [$.create('div',{attributes: {style: 'width:38pt;float:left' },
		children: [$.create('img', {attributes: {id:'menos_'+contTel, name:'menos' ,src: 'images/icon_menos.png'}}).addClass('pointer').click(function() {rmForm(tabela,'telefone');}),
					$.create('img', {attributes: {id:'mais_'+contTel, name:'mais' ,src: 'images/icon_mais.png'}, events: {click: addFormTel}})]}).addClass('pointer'),
					$.create('select',{attributes: {id:'tipo_telefone_'+contTel, name:'telefone['+tabela+'][tipo]'}, children:createSelect(tpTel,false)}),'&nbsp;',
					$.create('input',{attributes: {id:'telefone_'+contTel,name:'telefone['+tabela+'][numero]',type:'text',style:'width:220px'}}).addClass('fone')];
	var tr1 = [ $.create('td',{attributes: {width: '130' }, children: 'Telefone:'}),
	            $.create('td',{attributes: {colspan: 6}, children: imgs })];

	$.create('hr',{attributes: {id: 'hr_telefone_'+contTel}}).appendTo('#tabela_telefone');
	$.create('table', {attributes: {id: 'telefone_'+contTel,cellpadding: 0,cellspacing: 0,border: 0,width: '100%'}}).appendTo('#tabela_telefone');
	$.create('tr',{children: tr1}).appendTo('#telefone_'+contTel);
	$(function($){$(".fone").setMask("phone-comp",{autoTab:'false'});});

	showButton('#tabela_telefone');

	contTel++;
	
}

var contTelRes = 0;
function addFormTelResp(){
	var tabela = contTelRes;
	var imgs = [$.create('div',{attributes: {style: 'width:38pt;float:left' },
		children: [$.create('img', {attributes: {id:'menos_'+contTelRes, name:'menos' ,src: 'images/icon_menos.png'}}).addClass('pointer').click(function() {rmForm(tabela,'tele_resp');}),
					$.create('img', {attributes: {id:'mais_'+contTelRes, name:'mais' ,src: 'images/icon_mais.png'}, events: {click: addFormTelResp}})]}).addClass('pointer'),
					$.create('select',{attributes: {id:'tipo_tele_resp_'+contTelRes, name:'tele_resp['+tabela+'][tipo]'}, children:createSelect(tpTel,false)}),'&nbsp;',
					$.create('input',{attributes: {id:'tele_resp_'+contTelRes,name:'tele_resp['+tabela+'][numero]',type:'text',style:'width:220px'}}).addClass('fone')];
	var tr1 = [ $.create('td',{attributes: {width: '130' }, children: 'Telefone:'}),
	            $.create('td',{attributes: {colspan: 6}, children: imgs })];

	$.create('hr',{attributes: {id: 'hr_tele_resp_'+contTelRes}}).appendTo('#tabela_tele_resp');
	$.create('table', {attributes: {id: 'tele_resp_'+contTelRes,cellpadding: 0,cellspacing: 0,border: 0,width: '100%'}}).appendTo('#tabela_tele_resp');
	$.create('tr',{children: tr1}).appendTo('#tele_resp_'+contTelRes);
	$(function($){$(".fone").setMask("phone-comp",{autoTab:'false'});});

	showButton('#tabela_tele_resp');

	contTelRes++;
	
}

function loadEnderCliente(tipo,logradouro,numero,complemento,bairro,cidade,estado,cep,idObj){
	var tabela = contEnd;
	var hiddenId  = $.create('input',{attributes: {id: 'id_hidden_endereco_'+contEnd,value: idObj,type:'hidden' }});
	var imgs = [$.create('div',{attributes: {style: 'width:68pt;float:left' },
								children: [$.create('img', {attributes: {id:'menos_'+contEnd, name:'menos' ,src: 'images/icon_menos.png'}}).addClass('pointer').click(function() {rmFormEdit(tabela,'endereco');}),
										   $.create('img', {attributes: {id:'mais_'+contEnd, name:'mais' ,src: 'images/icon_mais.png'}, events: {click: addFormEnd}}).addClass('pointer'),
										   $.create('img', {attributes: {name:'edit' ,src: 'images/icon_edit.png'}}).addClass('pointer').click(function() {editEndereco('endereco_'+tabela,idObj);}),hiddenId]})];
	var chidEnder = [$.create('span',{attributes: {name: 'sp_tipo' }, children:tipo}),$.create('br'),
					$.create('span',{attributes: {name: 'sp_logradouro' }, children:logradouro}),', ',
					$.create('span',{attributes: {name: 'sp_numero' }, children:numero}),' ',
					$.create('span',{attributes: {name: 'sp_complemento' }, children:complemento}),$.create('br'),
					$.create('span',{attributes: {name: 'sp_bairro' }, children:bairro}),' - ',
					$.create('span',{attributes: {name: 'sp_cidade' }, children:cidade}),'/',
					$.create('span',{attributes: {name: 'sp_estado' }, children:estado.toUpperCase()}),' CEP: ',
					$.create('span',{attributes: {name: 'sp_cep' }, children:cep })];	
	var divEnd = $.create('div',{attributes: {style: 'padding-bottom:10px;'},
						children:chidEnder});	
	var tr1 = [ $.create('td',{attributes: {width: '130' }, children: 'Endereço:'}),
	            $.create('td',{attributes: {width: '50'}, children: imgs }),
		    	$.create('td',{children: divEnd })];			      	
    	$.create('hr',{attributes: {id: 'hr_endereco_'+contEnd}}).appendTo('#tabela_endereco');
    	$.create('table', {attributes: {id: 'endereco_'+contEnd,cellpadding: 0,cellspacing: 0,border: 0,width: '100%'}}).appendTo('#tabela_endereco');
    	$.create('tr',{children: tr1}).appendTo('#endereco_'+contEnd);
	showButton('#tabela_endereco');	
	contEnd++;
		
}

function loadTelCliente(tableId,tipo,numero,idObj){
	var tabela = contTel;
	var hiddenId  = $.create('input',{attributes: {id: 'id_hidden_'+tableId+'_'+contTel,value: idObj,type:'hidden' }});
	var imgs = [$.create('div',{attributes: {style: 'width:68pt;float:left' },
		children: [$.create('img', {attributes: {id:'menos_'+contTel, name:'menos' ,src: 'images/icon_menos.png'}}).addClass('pointer').click(function() {rmFormEdit(tabela,tableId);}),
				   $.create('img', {attributes: {id:'mais_'+contTel, name:'mais' ,src: 'images/icon_mais.png'}, events: {click: addFormTel}}).addClass('pointer'),
				   $.create('img', {attributes: {name:'edit' ,src: 'images/icon_edit.png'}}).addClass('pointer').click(function() {editTelefone(tableId+'_'+tabela,idObj);}),hiddenId]})];
	var chidTel = [$.create('span',{attributes: {name: 'sp_tipo' }, children:tipo}),' - ',
				   $.create('span',{attributes: {name: 'sp_numero' }, children:numero})];	

	var divTel = $.create('div',{attributes: {style: 'padding-bottom:10px;'},
		children:chidTel});	
	var tr1 = [ $.create('td',{attributes: {width: '130' }, children: 'Telefone:'}),
	            $.create('td',{attributes: {colspan: 6}, children: imgs })];


	var tr1 = [ $.create('td',{attributes: {width: '130' }, children: 'Telefones:'}),
	            $.create('td',{attributes: {width: '50'}, children: imgs }),
		    	$.create('td',{children: divTel })];			      	
    	$.create('hr',{attributes: {id: 'hr_'+tableId+'_'+contTel}}).appendTo('#tabela_'+tableId);
    	$.create('table', {attributes: {id: tableId+'_'+contTel,cellpadding: 0,cellspacing: 0,border: 0,width: '100%'}}).appendTo('#tabela_'+tableId);
    	$.create('tr',{children: tr1}).appendTo('#'+tableId+'_'+contTel);
    	
		showButton('#tabela_'+tableId);
    	contTel++;

		
}

/**
 * Função de alerta personalizado
 * Opções 
 * @var texto - mensagem exibida
 * @var kind - tipo do alerta (ERROR,OK,MAIL,CONFIRM)
 *
 * Caso opção CONFIRM é necessário complementar com:
 *	 	$('#btnConfirm').click(function() {
 *			<< FUNCAO A SER EXECUTADA >>
 *			$('#alertMessageCell').remove();
 *			$('#alertFloat').remove();
 *		});
 */
function showAlert(texto,kind,redirect) {
	
	if (kind == 'WARNING') {
		var arquivo = 'alert_img_y.png';
	} else if(kind == 'ERROR') {
		var arquivo = 'alert_img_r.png';
	} else if(kind == 'OK') {
		var arquivo = 'alert_img_g.png';
	} else if(kind == 'MAIL') {
		var arquivo = 'alert_img_m.png';
	} else if(kind == 'CONFIRM') {
		var arquivo = 'alert_img_y.png';
	}
	
	$.create('div',{attributes: {id:'alertFloat',style:'position:absolute;z-index:50;top:0;left:0;width:100%;height:'+$(document).height()
														+'px;background-color:#000;opacity: 0.7; filter: alpha(opacity=70);'}}).appendTo('body');
	$.create('div',{attributes: {id:'containerAlertFloat',style:'width:100%;z-index:51;position:absolute;top:30%;background-color:transparent;' }}).appendTo('body');
	$.create('div',{attributes: {id:'alertMessageCell'}}).appendTo('#containerAlertFloat');
	$.create('div',{attributes: {id:'alertMessage'}}).appendTo('#alertMessageCell');
	$.create('div',{attributes: {id:'alertMessageTop'}}).css('display','table').appendTo('#alertMessage');
	$.create('div',{attributes: {id:'alertMessageCenter'}}).css('display','table').appendTo('#alertMessage');
	$.create('div',{attributes: {id:'alertMessageBottom'}}).css('display','table').appendTo('#alertMessage');
	$.create('div').addClass('topLeft').appendTo('#alertMessageTop');
	$.create('div').addClass('top').appendTo('#alertMessageTop');
	$.create('div').addClass('topRight').appendTo('#alertMessageTop');
	$.create('div').addClass('bottomLeft').appendTo('#alertMessageBottom');
	$.create('div').addClass('bottom').appendTo('#alertMessageBottom');
	$.create('div').addClass('bottomRight').appendTo('#alertMessageBottom');
	$.create('div').addClass('left').appendTo('#alertMessageCenter');
	$.create('div',{attributes: {id:'alertContent'}}).addClass('center').appendTo('#alertMessageCenter');
	$.create('div').addClass('right').appendTo('#alertMessageCenter');
	$.create('div').addClass('img').css('background','url("../images/'+arquivo+'") no-repeat').appendTo('#alertContent');
	$.create('div',{attributes: {id:'alertTxt'}}).addClass('txt').css('margin','15px').html(texto).appendTo('#alertContent');
	$.create('div',{attributes: {id:'alertBtnClose'}}).addClass('btn').appendTo('#alertContent');
	/* Caso opção CONFIRM é necessário complementar com:
	 	$('#btnConfirm').click(function() {
			<< FUNCAO A SER EXECUTADA >>
			$('#alertMessageCell').remove();
			$('#alertFloat').remove();
		});
	 */ 
	if(kind == 'CONFIRM') {
		
		$.create('div',{attributes: {id:'btnConfirm'}}).addClass('botaoR btn_confirm pointer').css('margin','0px 10px').html('Confirmar').appendTo('#alertBtnClose');
		$.create('div',{attributes: {id:'btnClose'}}).addClass('botaoR btn_del pointer').css('margin','0px').html('Cancelar').appendTo('#alertBtnClose');
	} else {
		$.create('div',{attributes: {id:'btnClose'}}).addClass('botaoR btn_del pointer').css('margin','0px').html('Fechar').appendTo('#alertBtnClose');	
	}
	
	
	var height = $('#alertMessageCenter .center').css('height');
	$('#alertMessageCenter .left').css('height',height);
	$('#alertMessageCenter .right').css('height',height);
	$('#alertMessage').css('z-index','52');
	// Verifica se tem o REDIRECT
	if(redirect != undefined) {
		$('#btnClose').click(function() {loadPage(redirect);} );
	} else {
		$('#btnClose').click(function() {$('#alertMessageCell').remove();$('#alertFloat').remove();} );
	}
	
	
}
function removeFloatAlert(){
	$('#alertMessageCell').remove();$('#alertFloat').remove();
}

function goToByScroll(id){
	      $('html,body').animate({scrollTop: $("#"+id).offset().top},'fast');
	}
