/**
 * DOTCUSTOMS.COM SOLUÇÕES WEB LTDA.
 * CNPJ: 08.678.712/0001-76
 * www.dotcustoms.com - Tel: +55 11 2122-4213
 * SOLUÇÕES CUSTOMIZADAS
 * 
 * CRIADO EM: Thu Aug 23 11:46:42 BRT 2007
 * 
 * @author Fábio Matavelli <fabio@dotcustoms.com>
 * @copyright Todos os Direitos Reservados (c) 2008, dotCustoms.com
 */

document.write('<scr'+'ipt type="text/javascript" src="/js/yahoo_2.0.0-b3.js"></scr'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="/js/event_2.0.0-b3.js"></scr'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="/js/dom_2.0.2-b3.js"></scr'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="/js/ie-select-width-fix.js"></scr'+'ipt>');

function launchJavascript(responseText) {
  // RegExp from prototype.sonio.net
  var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
           
  var match    = new RegExp(ScriptFragment, 'img');
  var scripts  = responseText.match(match);

    if(scripts) {
        var js = '';
        for(var s = 0; s < scripts.length; s++) {
            var match = new RegExp(ScriptFragment, 'im');
            js += scripts[s].match(match)[1];
        }
        eval(js);
    }
}

function appendAndLaunchJs(div) {
    //if(!isRequest()) return;
   
    var inner = document.getElementById(div);

    var node = document.createElement('div'); // create a new div element
    node.innerHTML = xmlRequest.responseText; // append the text to node
    inner.innerHTML = '';                     // clear the destination node
    inner.appendChild(node);                  // append the node as a child and ...
                                             // ... at this point the JS is executed
}

function ajax() {
	this.Updater = carrega;

	function carrega(url,idDiv,mth,msg,param) {
		var content = document.getElementById(idDiv);
		if(msg != '') {
			content.innerHTML = msg;
		}

		var xmlHttp = getXmlHttp();
		xmlHttp.open(mth.toUpperCase(),url,true);
		if(mth.toLowerCase() == 'post') {
			xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length",param.length);
			xmlHttp.setRequestHeader("Connection","close");
			xmlHttp.send(param);
		} else if(mth.toLowerCase() == 'file') {
			
		}
		xmlHttp.onreadystatechange = function() {
			var content = document.getElementById(idDiv);
			if(xmlHttp.readyState == 4) {
				var txt = xmlHttp.responseText;
				//txt = txt.replace(/\+/g,'');
				//txt = unescape(txt);
				content.innerHTML = txt;
				if(navigator.appName != 'Netscape') {
					launchJavascript(txt);
				} else {
					//if(!isRequest()) return;
					
					var inner = document.getElementById(idDiv);
					var node = document.createElement('div');
					
					node.innerHTML = xmlHttp.responseText;
					inner.innerHTML = '';
					inner.appendChild(node);
				}
			}
		}
		if(mth.toLowerCase() != 'post') {
			xmlHttp.send(null);
		}
	}
}

function getXmlHttp() {
	var xmlHttp;
	try	{
		xmlHttp = new XMLHttpRequest();
	} catch (ee) {
		try	{
			xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try	{
				xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (E) {
				xmlHttp = false;
			}
		}
	}
	return xmlHttp;
}

function ajaxGET(url,div,msg) {
    var aj = new ajax();
    aj.Updater(url,div,'GET',msg,'');
}

function ajaxPOST(url,param,div,msg) {
	var aj = new ajax();
	aj.Updater(url,div,'POST',msg,param);
}

function resize() {
	var nav = navigator.appName;
	var w;
	var h;
	if(nav == 'Netscape') {
		//w = window.innerWidth - 193;
		w = document.documentElement.offsetWidth - 177;
		h = (document.documentElement.scrollHeight - document.documentElement.clientHeight) - 427;
		if(document.getElementById('tophome')) {
			h = (h - 133);
		}
	} else {
		w = document.body.offsetWidth - 177;
		//h = document.body.offsetHeight;
		h = 1619;
	}
	
	if(document.getElementById('tophome')) {
		if(nav == 'Netscape') {
			document.getElementById('tophome').setAttribute('style','width: '+w+'px;');
		} else {
			document.getElementById('tophome').style.width = w+'px';
		}
	}
	
	if(document.getElementById('carrinho')) {
		var carrinho = document.getElementById('carrinho');
		if(nav == 'Netscape') {
			carrinho.setAttribute('style','width: '+w+'px;');
		} else {
			carrinho.style.width = w+'px';
		}
	}

	if(document.getElementById('conteudo')) {
		if(nav == 'Netscape') {
			if(!document.getElementById('conteudo').getAttribute('style','height')) {
				document.getElementById('conteudo').setAttribute('style','width: '+(w-60)+'px;');
			} else {
				h = document.getElementById('conteudo').getAttribute('style','height');
				document.getElementById('conteudo').setAttribute('style','width: '+(w-60)+'px;');
			}
		} else {
			document.getElementById('conteudo').style.width = (w - 60)+'px';
			//document.getElementById('conteudo').style.height = h+'px';
		}
	}
	
	if(document.getElementById('dirprod')) {
		var dirprod = document.getElementById('dirprod');
		if(nav == 'Netscape') {
			dirprod.setAttribute('style','width: '+(w-260)+'px; height: '+h+'px; max-width: 100%;');
		} else {
			dirprod.style.width = (w - 262) + 'px';
			//dirprod.style.maxWidth = '100%';
		}
	}
}

function setOnLoad() {
	var nav = navigator.appName;
	
	if(nav == 'Netscape') {
		document.body.setAttribute('onresize','resize();');
	} else {
		window.onresize = resize;
	}
	
	resize();
}

function addCart(form) {
	var param = 'pID=' + form.produto.value +
		'&do=add' +
		'&precoHaste=' + form.precoHaste.value +
		'&precoAcessorio=' + form.precoAcessorio.value;
	if(form.medida_adaptador) {
		param += '&adaptador=' + form.medida_adaptador[form.medida_adaptador.selectedIndex].value;
	}
	
	if(form.medida_conexao) {
		/*
		for(var i=0;i<form.medida_conexao.length;i++) {
			if(form.medida_conexao[i].checked == true) {
				param += '&conexoes=' + form.medida_conexao[i].value;
			}
		}
		*/
		param += '&conexoes=' + form.medida_conexao[form.medida_conexao.selectedIndex].value;
	}
	
	if(form.medida_diametro_mostrador) {
		param += '&diametro_mostrador=' + form.medida_diametro_mostrador[form.medida_diametro_mostrador.selectedIndex].value;
	}
	
	if(form.medida_local_conexao) {
		for(var i=0;i<form.medida_local_conexao.length;i++) {
			if(form.medida_local_conexao[i].checked == true) {
				param += '&local_conexao=' + form.medida_local_conexao[i].value;
			}
		}
	}
	
	if(form.medida_ranges) {
		param += '&ranges=' + form.medida_ranges[form.medida_ranges.selectedIndex].value;
	}
	
	if(form.medida_acessorios) {
		param += '&acessorios=' + form.medida_acessorios[form.medida_acessorios.selectedIndex].value;
	}
	
	if(form.medida_hastes) {
		param += '&hastes=' + form.medida_hastes[form.medida_hastes.selectedIndex].value;
	}
	
	if(form.medidas) {
		param += '&medidas=' + form.medidas.value;
	}

	ajaxPOST('/doCart.php',param,'addCart','Aguarde...');
}

function mudaBG(div,cor) {
	if(navigator.appName == 'Netscape') {
		div.setAttribute('style','background-color: '+cor+';');
	} else {
		div.style.backgroundColor=cor;
	}
}

function doLogon(form,back) {
	var param = 'do=login' +
				'&email=' + form.email.value +
				'&senha=' + form.senha.value;
	if(back.length > 0) {
		param += '&back=' + back;
	}
	ajaxPOST('/shop/checkLogin.php',param,'logon','Aguarde...');
}

function resizeCampo(campo) {
	/*
	campo.onfocus = function() {
		this.style.width = 'auto';
	}
	
	campo.onblur = function () {
		this.style.width = '150px';
	}
	*/
	var fix = new YAHOO.Hack.FixIESelectWidth(campo.id);
}
