//Definições de ambiente
document.onkeydown = function() {
    if ((window.event.srcElement.tagName.toUpperCase() != 'INPUT') && (window.event.srcElement.tagName.toUpperCase() != 'TEXTAREA') && (window.event.keyCode == 8)) return false;
}


//Funções da aplicação
function $(obj){
    if(typeof obj=='string') return document.getElementById(obj);
};


function fnLink(pagina,target,op)	{
	var frm = document.frm;

	frm.op.value = op;
	frm.pagina.value = pagina;
	frm.cod_produto.value = "";
	frm.cod_produtocategoria.value = "";
	frm.cod_produtosubcategoria.value = "";
	frm.cod_produto.value = "";
	frm.action = "";
	frm.target = (target=="")?"_self":target;
	frm.submit();
}

function fnProdutoCategoria(cod_produtocategoria,cod_produtosubcategoria)	{
	var frm = document.frm;

	frm.op.value = "produto";
	frm.pagina.value = "p_produto.php";
	frm.cod_produto.value = "";
	frm.cod_produtocategoria.value = cod_produtocategoria;
	frm.cod_produtosubcategoria.value = cod_produtosubcategoria;
	frm.cod_produto.value = "";
	frm.submit();
}

function fnProdutoSubCategoria(cod_produtocategoria)	{
	var frm = document.frm;

	frm.op.value = "produto";
	frm.pagina.value = "p_subcategoria.php";
	frm.cod_produto.value = "";
	frm.cod_produtocategoria.value = cod_produtocategoria;
	frm.cod_produtosubcategoria.value = "";
	frm.cod_produto.value = "";
	frm.submit();
}

function fnContato() {
	var frm = document.frm;
	var reEmail = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

	if (frm.contato_nome.value==""){
		alert("Informe o Nome")
		frm.contato_nome.focus();
		return false;
	}
	if (frm.contato_ender.value==""){
		alert("Informe o Endereço")
		frm.contato_ender.focus();
		return false;
	}

	if (frm.contato_telefone.value==""){
		alert("Informe o Telefone")
		frm.contato_telefone.focus();
		return false;
	}

	if (frm.contato_email.value==""){
		alert("Informe o E-mail")
		frm.contato_email.focus();
		return false;
	}

	if (!reEmail.test(frm.contato_email.value)) {
		alert("E-mail inválido : " + frm.contato_email.value)
		frm.contato_email.focus();
		return false;
	}

	frm.op.value = "contato";
	frm.pagina.value = "p_contato.php";
	frm.target = "";
	frm.submit();
}

function fnSearch(sql_p_){
	var frm = document.frm;
	
	frm.op.value = "search";
	frm.sql_p_.value = (sql_p_)?sql_p_:1;
	frm.submit();
}

function fnImagePreview(src) {
	var html = (fnImagePreview.arguments[1])?fnImagePreview.arguments[1]:"";
	var div = document.createElement("div");
	
	div.id = "image-preview";
	div.style.width = fnSize('')[0];
	div.style.height = document.getElementById('corpo').offsetHeight;
	div.innerHTML = "<div id='image-previewcenter'><img src='"+src+"'>"+html+"</div>"
	div.onclick = function() { fnRemove() };
	function fnRemove() { $("image-preview").parentNode.removeChild($("image-preview")); }
	document.body.appendChild(div);
	$('image-previewcenter').style.left = "100px";
	$('image-previewcenter').style.top = "50px";
}

function fnSize(obj) {
	if (obj=='') {
		width = (document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
		height = (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
	} else {
		width = $(obj).clientWidth;
		height = $(obj).clientHeight;
	}
	fnSize[0] = width;
	fnSize[1] = height;
	return fnSize;
}

function fnLoadImage() { 
	var aImage=fnLoadImage.arguments;
	var aAux=new Array();
	
	for(var i=0; i<aImage.length; i++) {
		aAux=new Image; 
		aAux.src=aImage[i];
	}
}

// FUNÇÕES GERAIS

if(Browser == undefined)
{
    var Browser = {
        isIE: function(){ return (window.ActiveXObject && document.all && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? true : false; }
    };
}

var Flash = function(movie, id, width, height, initParams)
{
    this.html = "";
    
    this.variables = new Array();
    
    this.flashversion = (typeof initParams != "undefined" && typeof initParams.flashversion != "undefined") ? initParams.flashversion : "7,0,0,0";
    
    this.attributes = {
        "classid": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",
        "codebase": "http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version=" + this.flashversion,
        "type": "application/x-shockwave-flash"
    };
    
    this.params = { "pluginurl": "http://www.macromedia.com/go/getflashplayer" };
    
    if(movie)
    {
        this.addAttribute("data", movie);
        this.addParameter("movie", movie);
    }
    
    if(id && id != null && (this.id = id)) 
    {
        this.addAttribute("id", this.id);
        this.addAttribute("name", this.id);
    }
    else
    {
        this.id = null;
    }
    
    if(width) this.addAttribute("width", width);
    if(height) this.addAttribute("height", height);

    this.addAttribute("wmode", "transparent");
    this.addParameter("wmode", "transparent");

    if(initParams != undefined)
    {
        for(var i in initParams) this.addParameter(i.toString(), initParams[i]);
    }
};
Flash.version = "v1.8";

Flash.prototype.getObject = function()
{
    if(this.id == null) return null;
    try
    {
        if(window.document[this.id])
        {
            return window.document[this.id];
        }
        else
        {
            return document.getElementById(window.document[this.id]);
        }
    }
    catch(e) { return null; }
};

Flash.getObjectByExceptions = function(obj, excep)
{
    var tempObj = {};
    for(var i in obj)
    {
        var EOF = false;
        for(var j=0; j<excep.length; j++) if(excep[j] == i.toString()) { EOF = true; break; };
        if(!EOF) tempObj[i] = obj[i];
    }
    return tempObj;
};

Flash.prototype.addAttribute = function(prop, val){ this.attributes[prop] = val; };
Flash.prototype.addParameter = function(prop, val){ this.params[prop] = val; };
Flash.prototype.addVariable = function(prop, val){ this.variables.push([prop, val]); };

Flash.prototype.getFlashVars = function()
{
    for(var i=0, tempString = new Array(); i<this.variables.length; i++) tempString.push(this.variables[i].join("="));
    return tempString.join("&");
};
Flash.prototype.toString = function()
{
    this.params.flashVars = this.getFlashVars();
    if(Browser.isIE())
    {
        //IE
        this.html = "<ob" + "ject";
        var attr = Flash.getObjectByExceptions(this.attributes, ["type", "data"]);
        for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
        this.html += "> ";
        var params = Flash.getObjectByExceptions(this.params, ["pluginurl", "extend"]);
        for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> ";
        this.html += " </obj" + "ect>";
    }
    else
    {
        //non-IE
        this.html = "<!--[if !IE]> <--> <obj" + "ect";
        var attr = Flash.getObjectByExceptions(this.attributes, ["classid", "codebase"]);
        for(var i in attr) if(i.toString() != "extend") this.html += " " + i.toString() + " = \"" + attr[i] + "\"";
        this.html += "> ";
        var params = Flash.getObjectByExceptions(this.params, ["extend"]);
        for(var i in params) if(i.toString() != "extend") this.html += "<param name=\"" + i.toString() + "\" value=\"" + params[i] + "\" /> ";
        this.html += " </obj" + "ect> <!--> <![endif]-->";
    }
    return this.html;
};
Flash.prototype.write = Flash.prototype.writeIn = function(w)
{
    if(typeof w == "string" && (w = document.getElementById(w)));
    if( w != null ) { w.innerHTML = this.toString(); }
    else if( w == undefined ) { document.write( this.toString() ); }
        else { return false; }
};

//automatization functions...
Flash.correctAll = function()
{
    if(!/msie/.test(navigator.userAgent.toLowerCase()) || !document.getElementsByTagName) return false;
    for (var i = 0, objects = document.getElementsByTagName("OBJECT"); i < objects.length;
        (objects[i].outerHTML ? (objects[i].outerHTML = objects[i].outerHTML, objects[i].style.visibility = "visible") : null), i++);
};
Flash.automatic = function(r)
{
    if(r && window.attachEvent)
    {    
        for (var i = 0, objects = document.getElementsByTagName("OBJECT"); i < objects.length; (objects[i].style.visibility = "hidden"), i++);
        window.attachEvent("onload", Flash.correctAll);
        window.attachEvent("onunload", function(){ window.detachEvent("onload", Flash.correctAll); });
    }
    else
    {
        Flash.correctAll();
    }
};


