// JavaScript Document
window.onstopscroll = function(){
	orcamento.alinha();	
	orcamentoP2.alinha();
	orcamentoP3.alinha();
	orcamentoP4.alinha();
	envio.alinha();
	galeria.alinha();
	
}

var orcamento = {
	'opened': false,
	'show': function(foto){
		this.opened = true;
		mensure.load();
		
		var DS = {x:mensure.widthVisible(), y:mensure.heightTotal()};
		$('blockbg').style.height = DS.y + 'px';
		if (!is_ie) {
			$('blockbg').style.width = (DS.x - 17) + 'px';
		} else {
			$('blockbg').style.width = DS.x + 'px';
		}
		setAlpha('blockbg', 80);
		$('blockbg').style.display = 'block';
		
		this.alinha();
		
	},
	'alinha' : function(){
		if(!this.opened) return;
		
		$('orcamento').style.display = 'block';
		//$('orcamentoP2').style.display = 'none';	
		
		var SC = getScroll();
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:$('orcamento').offsetWidth, y:$('orcamento').offsetHeight};
		
		newX = (VS.x-SZ.x)/2+SC.x;
		newY = (VS.y-SZ.y)/2+SC.y;
		
		$('orcamento').style.top = (newY)+'px';
		$('orcamento').style.left = (newX)+'px';

	},
	'hide': function(){
		this.opened = false;
		$('blockbg').style.display = 'none';
		$('orcamento').style.display = 'none';		
	},
	'mostraprods': function(idlinha){
		//alert(idlinha);
		listaOrc(idlinha);
	}	
} 

var orcamentoP2 = {
	'opened': false,
	'show': function(foto){
		this.opened = true;
		mensure.load();
		
		var DS = {x:mensure.widthVisible(), y:mensure.heightTotal()};
		$('blockbg').style.height = DS.y + 'px';
		if (!is_ie) {
			$('blockbg').style.width = (DS.x - 17) + 'px';
		} else {
			$('blockbg').style.width = DS.x + 'px';
		}
		setAlpha('blockbg', 80);
		$('blockbg').style.display = 'block';
		this.alinha();
		
	},
	'alinha' : function(){
		if(!this.opened) return;
		
		$('orcamento').style.display = 'none';		
		$('orcamentoP2').style.display = 'block';
		
		var SC = getScroll();
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:$('orcamentoP2').offsetWidth, y:$('orcamentoP2').offsetHeight};
		
		newX = (VS.x-SZ.x)/2+SC.x;
		newY = (VS.y-SZ.y)/2+SC.y;
		
		$('orcamentoP2').style.top = (newY)+'px';
		$('orcamentoP2').style.left = (newX)+'px';

	},
	'hide': function(){
		this.opened = false;
		$('blockbg').style.display = 'none';
		$('orcamentoP2').style.display = 'none';		
	},
	'mostraprods': function(){
		$('orcamento').style.display = 'none';	
		listaOrcConf();
	}	
} 

var orcamentoP3 = {
	'opened': false,
	'show': function(foto){
		this.opened = true;
		mensure.load();
		
		var DS = {x:mensure.widthVisible(), y:mensure.heightTotal()};
		$('blockbg').style.height = DS.y + 'px';
		if (!is_ie) {
			$('blockbg').style.width = (DS.x - 17) + 'px';
		} else {
			$('blockbg').style.width = DS.x + 'px';
		}
		setAlpha('blockbg', 80);
		$('blockbg').style.display = 'block';
		this.alinha();
		
	},
	'alinha' : function(){
		if(!this.opened) return;
		
		$('orcamento').style.display = 'none';		
		$('orcamentoP2').style.display = 'none';
		$('orcamentoP3').style.display = 'block';
		$('orcamentoP4').style.display = 'none';
		
		
		var SC = getScroll();
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:$('orcamentoP3').offsetWidth, y:$('orcamentoP3').offsetHeight};
		
		newX = (VS.x-SZ.x)/2+SC.x;
		newY = (VS.y-SZ.y)/2+SC.y;
		
		$('orcamentoP3').style.top = (newY)+'px';
		$('orcamentoP3').style.left = (newX)+'px';

	},
	'hide': function(){
		this.opened = false;
		$('blockbg').style.display = 'none';
		$('orcamentoP3').style.display = 'none';		
	}
} 


var orcamentoP4 = {
	'opened': false,
	'show': function(foto){
		this.opened = true;
		mensure.load();
		
		var DS = {x:mensure.widthVisible(), y:mensure.heightTotal()};
		$('blockbg').style.height = DS.y + 'px';
		if (!is_ie) {
			$('blockbg').style.width = (DS.x - 17) + 'px';
		} else {
			$('blockbg').style.width = DS.x + 'px';
		}
		setAlpha('blockbg', 80);
		$('blockbg').style.display = 'block';
		this.alinha();
		
	},
	'alinha' : function(){
		if(!this.opened) return;
		
		$('orcamento').style.display = 'none';		
		$('orcamentoP2').style.display = 'none';
		$('orcamentoP3').style.display = 'none';
		$('orcamentoP4').style.display = 'block';
		
		var SC = getScroll();
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:$('orcamentoP4').offsetWidth, y:$('orcamentoP4').offsetHeight};
		
		newX = (VS.x-SZ.x)/2+SC.x;
		newY = (VS.y-SZ.y)/2+SC.y;
		
		$('orcamentoP4').style.top = (newY)+'px';
		$('orcamentoP4').style.left = (newX)+'px';

	},
	'hide': function(){
		this.opened = false;
		$('blockbg').style.display = 'none';
		$('orcamentoP4').style.display = 'none';	
		$('msgaguarde').innerHTML = '';
	}
} 

function listaOrc(idlinha){
	$('idlinhaorc').value = idlinha;
	var xml = new xmlConnection();
	if (xml.create()){
		xml.setURL('/orcamento/lista/');
		xml.setMethod('POST');
		xml.addParameters('nocache',nocache());
		xml.addParameters('linha',idlinha);
	}
	xml.onComplete = function() {
		SAWait.close();
		this.setChildName('prod');
		var qtde = this.getCountItens();
		var html = '';
		for(i=0;i<qtde;i++){
				html += '<table width="97%" cellpadding="0" cellspacing="3" border="0">';
				html += '			<tr>';
				html += '				<td height="22" width="58" align="left" style="border:solid 1px #cacaca; padding-left:1px;">';
				html += '					<table width="100%" cellspacing="0" cellpadding="0" border="0">';
				html += '						<tr>';
				html += '							<td rowspan="2"><input readonly="readonly" type="text" class="formQtdLista" maxlength="3" id="qtde'+i+'" value="'+this.getAttByName('qtde',i)+'" /><input type="hidden" id="id'+i+'" value="'+this.getAttByName('id',0)+'" /></td>';
				html += '							<td align="right"><a href="javascript:maisQtde('+i+','+this.getAttByName('idprod',i)+');void(0);"><img src="/media/img/setaMaisProd.gif" width="17" height="12" border="0" /></a></td>';
				html += '						</tr>';
				html += '						<tr>';
				html += '							<td align="right"><a href="javascript:menosQtde('+i+','+this.getAttByName('idprod',i)+');void(0);"><img src="/media/img/setaMenosProd.gif" width="17" height="12" border="0" /></a></td>';
				html += '						</tr>';
				html += '					</table>';
				html += '				</td>';
				html += '				<td height="22" style="border:solid 1px #cacaca; padding-left:5px;">'+this.getAttByName('produto',i)+'</td>';
				html += '			</tr>';
				html += '		</table>';	
		}
		
		$('orcprod').innerHTML = html;		
		
		orcamento.show();
		
	}
	xml.execute();
}



function maisQtde(idhtml,idprod){
	if($('orcamento').style.display=="block"){
		var qtde = parseInt($('qtde'+idhtml).value);
	}else{
		var qtde = parseInt($('qtdeP2'+idhtml).value);
	}
	mudaQtde(idhtml,idprod,qtde+1);
}

function menosQtde(idhtml,idprod){
	//mudaQtde(idhtml,idprod,parseInt($('qtde'+idhtml).value)-1);
	if($('orcamento').style.display=="block"){
		var qtde = parseInt($('qtde'+idhtml).value);
	}else{
		var qtde = parseInt($('qtdeP2'+idhtml).value);
	}
	mudaQtde(idhtml,idprod,qtde-1);
}


function excluirItem(idhtml,idprod){
	var xml = new xmlConnection();
	if (xml.create()){
		xml.setURL('/orcamento/excluiritem/');
		xml.setMethod('GET');
		xml.addParameters('nocache',nocache());
		xml.addParameters('idprod',idprod);
	}
	xml.onComplete = function() {
		$('qtde'+idhtml).value = 0;
		listaOrcConf();
	}
	xml.execute();
}

var temItens = false;
function listaOrcConf(){
	var xml = new xmlConnection();
	if (xml.create()){
		xml.setURL('/orcamento/conferencia/');
		xml.setMethod('GET');
		xml.addParameters('nocache',nocache());
	}
	xml.onComplete = function() {
		SAWait.close();
		this.setChildName('produto');
		
		var qtde = this.getCountItens();
		var html = '';
		//alert(qtde);
		$('qtdetotal').value = qtde;
		//alert($('qtdetotal').value);
		for(i=0;i<qtde;i++){
				html += '<table width="97%" cellpadding="0" cellspacing="3" border=0">';
				html += '			<tr>';
				html += '				<td height="22" width="58" align="left" style="border:solid 1px #cacaca; padding-left:1px;">';
				html += '					<table width="100%" cellspacing="0" cellpadding="0" border="0">';
				html += '						<tr>';
				html += '							<td rowspan="2"><input readonly="readonly" type="text" class="formQtdLista" maxlength="3" id="qtdeP2'+i+'" value="'+this.getAttByName('qtde',i)+'" /><input type="hidden" id="idprod'+i+'" value="'+this.getAttByName('idprod',i)+'" /></td>';
				html += '							<td align="right"><a href="javascript:maisQtde('+i+','+this.getAttByName('idprod',i)+');void(0);"><img src="/media/img/setaMaisProd.gif" width="17" height="12" border="0" /></a></td>';
				html += '						</tr>';
				html += '						<tr>';
				html += '							<td align="right"><a href="javascript:menosQtde('+i+','+this.getAttByName('idprod',i)+');void(0);"><img src="/media/img/setaMenosProd.gif" width="17" height="12" border="0" /></a></td>';
				html += '						</tr>';
				html += '					</table>';
				html += '				</td>';
				html += '				<td height="22" width="490" style="border:solid 1px #cacaca; padding-left:5px;">'+this.getAttByName('nome',i)+'</td>';
				html += '				<td align="center" height="22" width="10"><a href="javascript:excluirItem('+i+','+this.getAttByName('idprod',i)+');void(0);"><img src="/media/img/excluir.gif" width="23" height="15" border="0" /></a></td>';
				html += '			</tr>';
				html += '			<tr>';
				html += '				<td height="22" colspan="3">';
				html += '					<table width="100%" height="18" cellpadding="0" cellspacing="0">';
				html += '						<tr>';
				html += '							<td align="center" width="61" style="background-color:#999;color:#fff;"><b>OBS:</b></td>';
				html += '							<td width="3"></td>';
				html += '							<td><input type="text" name="obs'+i+'" id="obs'+i+'" class="imputOrcamento"/></td>';
				html += '						</tr>';
				html += '					</table>';
				html += '				</td>';
				html += '			</tr>';
				html += '</table>';	
		}
		if(qtde==0){
			html = '<div align="center">Você não possui itens a serem orçados.</div>';
			temItens =	false
		}else{
			temItens =	true
		}
		
		$('conforcprod').innerHTML = html;
		orcamentoP2.show();
	}
	xml.onError = function(e,t){
		alert(t);
	}
	xml.execute();
}


function mudaQtde(idhtml,idprod,qtde){		
	var xml = new xmlConnection();
	if (xml.create()){
		xml.setURL('/orcamento/mudaqtde/');
		xml.setMethod('POST');
		xml.addParameters('nocache',nocache());
		xml.addParameters('idprod',idprod);
		xml.addParameters('qtde',qtde);
	}
	xml.onComplete = function() {
		//SAWait.close();
		this.setChildName('prod');
		if(this.getAttByName('retorno',0)==1){
			if($('orcamento').style.display=="block"){
				$('qtde'+idhtml).value = this.getAttByName('qtde',0);			
			}else{
				$('qtdeP2'+idhtml).value = this.getAttByName('qtde',0);			
			}
		}else{
			SA.open('Erro!', 'Não foi possível alterar sue orçamento.', null, 1, 1);
		}
	}
	xml.execute();
}

passo1 = function(){	
	orcamentoP2.hide();
	orcamentoP3.hide();
	listaOrc(parseInt($('idlinhaorc').value));
	//orcamento.show();
	$('blockbg').style.display = 'block';
	
}
passo2 = function(){	
	orcamento.hide();
	orcamentoP3.hide();	
	orcamentoP2.mostraprods();
	$('blockbg').style.display = 'block';
}

passo3 = function(){
	if(temItens==true){
		orcamento.hide();
		orcamentoP2.hide();
		gravaObs($('qtdetotal').value);
		$('blockbg').style.display = 'block';
	}else{
		var cb = new Object();
		cb.onOk = function(){
			//limparContato();
			$('blockbg').style.display = 'block';
			SA.close();
			
		}
		SA.open('Alerta!', 'Não é possível finalizar um orçamento sem itens.', cb, 1, 1);	
		
	}
}

passo4 = function(){	
	orcamento.hide();
	orcamentoP3.hide();	
	orcamentoP2.hide();
	var ld = new selectLoader();
	ld.load('segmento','/segmentoAtividadeOrcXML/?atividade=0&');
	orcamentoP4.show();
	$('blockbg').style.display = 'block';
}


gravaObs = function(qtde){
	var xml = new xmlConnection();
	if (xml.create()){
		xml.setURL('/orcamento/gravarobs/');	
		xml.setMethod('POST');
		xml.addParameters('nocache',nocache());
		xml.addParameters('qtde',qtde);
		
		for(i=0;i<qtde;i++){
			//alert($('id'+i).value);
			xml.addParameters('id'+i,$('idprod'+i).value);
			xml.addParameters('obs'+i,$('obs'+i).value);
		}
	}
	xml.onComplete = function() {
		orcamentoP3.show();	
	}
	xml.execute();
}



finaliza = function(){
	var err = '';
	var form = $('formorc')
	
	if(form.orcnome.value==''){
		err += 'Preencha o nome<br>';
	}
	if(form.orcempresa.value==''){
		err += 'Preencha a empresa<br>';
	}
	if(form.orcdpto.value==''){
		err += 'Preencha o departamento<br>';
	}
	/*if(form.orcperfil.value==''){
		err += 'Preencha o perfil\n';
	}*/
	
	if(form.orcemail.value=='' && !form.orcemail.value.isMail()){
		err += 'Preencha o email corretamente<br>';
	}
	if(form.orccidade.value==''){
		err += 'Preencha a cidade<br>';
	}
	if(form.orcestado.value==''){
		err += 'Preencha o estado<br>';
	}
	if(form.orctelefone.value==''){
		err += 'Preencha o telefone<br>';
	}
	
	
	if(err==''){
		var xml = new xmlConnection();
		if (xml.create()){
			$('btFinalizaOrc').style.display = 'none';
			$('msgaguarde').innerHTML = 'Enviando orçamento...Aguarde.'
			xml.setURL('/orcamento/gravar/');
			xml.setMethod('POST');
			xml.addParameters('nocache',nocache());
			xml.addParameters('nome',form.orcnome.value);
			xml.addParameters('empresa',form.orcempresa.value);
			xml.addParameters('departamento',form.orcdpto.value);
			//xml.addParameters('perfil',form.orcperfil.value);
			
			xml.addParameters('atividade',form.atividade.value);
			xml.addParameters('segmento',form.segmento.value);
			
			xml.addParameters('email',form.orcemail.value);
			xml.addParameters('cidade',form.orccidade.value);
			xml.addParameters('estado',form.orcestado.value);
			xml.addParameters('telefone',form.orctelefone.value);
			xml.addParameters('obs',form.orcobs.value);	
		}
		xml.onComplete = function() {
			$('btFinalizaOrc').style.display = 'block';
			SAWait.close();
			this.setChildName('prod');
			if(this.getAttByName('retorno',0)==1){			
				orcamentoP3.hide();
				
				/*var html = 'Obrigado por consultar a Contemp.<br>';
				html += 'Sua solicitação de orçamento foi enviada ao nosso departamento comercial.<br>';		
				html += 'Responderemos dentro de algumas horas!<br>';
				html += 'Caso deseja falar com um de nossos consultores, os contatos são:<br>';
				html += '(11) 4223-5100  | vendas@contemp.com.br <br>';
				
				SA.open('Sucesso!', html, null, 1, 1);*/
				
				passo4();
			}else{
				SA.open('Erro!', 'Não foi possível alterar sue orçamento.', null, 1, 1);
			}			
		}
		xml.execute();
	}else{
		SA.open('Erro!', err, null, 1, 1);	
	}
}

limparTudo = function(idlinha){
	var xml = new xmlConnection();
	if (xml.create()){
		xml.setURL('/orcamento/limpar/');
		xml.setMethod('POST');
		xml.addParameters('nocache',nocache());
	}
	xml.onComplete = function() {
		listaOrc(parseInt($('idlinhaorc').value));
	}
	xml.execute();
}