function CarregarNomeEParceiro()
{
	var qs = new Querystring();
	var ParceiroCod = qs.get("parc", "");

	// FORCA O LOGOFF SE NAO HOUVER PARCEIRO
	if(ParceiroCod == ""
		&& (window.location.href.indexOf("/ch/iprodutos.aspx") > -1
			|| window.location.href.indexOf("/ch/busca/index.aspx") > -1
			|| window.location.href.indexOf("/ch/categoria/index.aspx") > -1
			|| window.location.href.indexOf("/ch/fornecedor/index.aspx") > -1
			|| window.location.href.indexOf("/ch/vitrines/index.aspx") > -1
			|| window.location.href.indexOf("/ch/produtos/detalhes.aspx") > -1
			|| window.location.href.indexOf("/ch/home.aspx") > -1))
	{
		ParceiroCod = -1;
	}

	PageMethods.CarregarNomeEParceiro(ParceiroCod, CarregarNomeEParceiroSuccess, WebMethodFailed, null);
}

function CarregarNomeEParceiroSuccess(value, ctx, methodName)
{
	if(value)
	{
		if(value[0] != "")
		{
			window.location.href = value[0];
		}

		J("#date").html(value[1]);
		J("#page").html(value[2]);
	}
}

function VerificarCotasListagem()
{
	PageMethods.VerificarCotasListagem(VerificarCotasListagemSuccess, WebMethodFailed, null);
}

function VerificarCotasListagemSuccess(value, ctx, methodName)
{
	if(value && value != "")
	{
		CarregarCota(true, value);
	}
}

function WebMethodFailed(exc, ctx, methodName)
{
    window.location = exc.get_message();
}

function Logoff(urlOutLogoff)
{
	PageMethods.Logoff(urlOutLogoff,LogoffSuccess, WebMethodFailed, null);
}

function LogoffSuccess(value, ctx, methodName)
{
	if(value)
	{
		window.location.href = value;
	}
}

function abas()
{
	J("div.conteudo_abas div").hide();
	J("div.conteudo_abas div:first").show();
	J("ul.abas_info a:first").addClass("ativo");
	J("ul.abas_info a").unbind("click"); // IKEDA
	J("ul.abas_info a").click(mostrarAba);
}

function mostrarAba()
{
	J("div.conteudo_abas div").hide();
	J("ul.abas_info a").removeClass("ativo");
	J(this).addClass("ativo");
	var linkHref = J(this).attr("href");
	linkHref = linkHref.substring(linkHref.indexOf("#"), linkHref.length);
	J(linkHref).show();
	return false;
}

function _isinteger(test_string)
{
	var i;
	var non_nums = 0;

	for (i = 1; i < test_string.length; i++)
	{
		if ((test_string.substring(i-1,i) < '0') || (test_string.substring(i-1,i) > '9'))
		{
			non_nums++;
		}
	}

	if (non_nums == 0)
		return true;
	else
		return false;
}

function reopenModal(url)
{
	closeModal();
	window.setTimeout('openModal("' + url + '");', 500);
}

	function SomenteNumero(e)
	{
		var key;

		if (window.event)
		{
			key = event.keyCode;
		}
		else
		{
			key = e.which;
		}

		if(key > 47 && key < 58 || key == 8 || key == 0)
		{
			return;
		}
		else
		{
			if(window.event)
			{
				window.event.returnValue = null;
			}
			else
			{
				e.preventDefault();
			}
		}
	}
	
	function Mascara(src, mask)
	{
		 var i = src.value.length;
		 var saida = mask.substring(0,1);
		 var texto = mask.substring(i)
		 if (texto.substring(0,1) != saida)
		 {
			  src.value += texto.substring (0,1);
		 }
	}	
	
	function Mascara2(e, src, mask)
    {
	    var key;

	    if (window.event)
	    {
		    key = event.keyCode;
	    }
	    else
	    {
		    key = e.which;
	    }

	    if (key != 8)
	    {
		    Mascara(src, mask);
	    }
    }

	function SomenteNumero2(e, src, mask)
    {
	    SomenteNumero(e);

	    var key;

	    if (window.event)
	    {
		    key = event.keyCode;
	    }
	    else
	    {
		    key = e.which;
	    }


	    if(	key != 8)
	    {
		    Mascara(src,mask);
	    }
    }
    
	function SomenteNumeroDecimal(e)
	{
		var key;

		if (window.event)
		{
			key = event.keyCode;
		}
		else
		{
			key = e.which;
		}

		if(key > 47 && key < 58 || key == 8 || key == 0 || key == 44)
		{
			return;
		}
		else
		{
			if(window.event)
			{
				window.event.returnValue = null;
			}
			else
			{
				e.preventDefault();
			}
		}
	}
	
	function ColocaFocus(NomeBtn, e)
	{
		try
		{
			var key;

			if(window.event)
			{
				key = window.event.keyCode;
			}
			else
			{
				key = e.which;
			}
			if(key == 13)
			{
				document.getElementById(NomeBtn).focus();

				if(window.event)
				{
					window.event.keyCode = 0;
				}
			}
		}
		catch(err)
		{
			alert(err.description);
		}

		return false;
	}
	
	function Trim(inp)
	{
		var outp = "";

		for (i = 0; i <= inp.length; i++)
		{
			if (inp.charAt (i) != " ")
			{
				outp += inp.charAt (i) ;
			}
		}

		return outp;
	}

function TRIM(STRING)
{
    STRING = LTrim(STRING);
    
    return RTrim(STRING);
}   

function RTrim(STRING)
{
    while(STRING.charAt((STRING.length -1))=="")
    {
        STRING = STRING.substring(0,STRING.length-1);
    }
    
    return STRING;
}

function LTrim(STRING)
{
    while(STRING.charAt(0) == "")
    {
        STRING = STRING.replace(STRING.charAt(0) ,"");
    }
    
    return STRING;
}
	
    function AbrirPopUP(URL,nmPagina)
    {
	    try
	    {
		    window.open(TRIM(URL),TRIM(nmPagina),'status = 1, height = 700, width = 600, resizable = 1,scrollbars=1' );

	    }
	    catch(err)
	    {
		    alert(err.description);
	    }

	    return false;
    }

function AbrirPopUPTamanho(URL, nmPagina, largura, altura)
{
	try
	{
		void(window.open(URL, nmPagina,'status = 1, height = ' + altura + ', width = ' + largura + ', resizable = 1,scrollbars=1'));
	}
	catch(err)
	{
		alert(err.description);
	}
}

function CarregarCota(bitMostraCota, strMensagem)
{
    try
    {
        if(bitMostraCota)
        {
			document.getElementById("divCota").style.display = "";
			document.getElementById("divCota").innerHTML = '<a href="javascript:CarregarCota(false);" class="bt_fechar_aviso" alt="fechar"><strong>X FECHAR</strong></a><p>'+ strMensagem +'</p>';
        }
        else
        {
			document.getElementById("divCota").style.display = "none";
        }
    }
    catch(err)
    {
	    window.alert(err.description);
    }
}
    
function imposeMaxLength(Object, MaxLen)
{
    return (Object.value.length <= MaxLen);
}

function ismaxlength(obj,max)
{
    var mlength=max;
    if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}

/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
