var gObjActive = null;

var gStrError = '';
var gObjFirstError = null;

var gStrMessage_Error_HasErros = '';
var gStrMessage_Error_CorrectErrors = '';
var gStrMessage_Permission_DeletePermission = '';

function Tratar_Enter(btn) 
{
	btn = document.getElementById(btn)
	
	if(event.keyCode == 13)
	{
		// Cancela o evento default
		event.returnValue = false; 
		event.cancel = true; 
		
		btn.click(); 
	}
}

function Bloquear_Enter()
{
	var objEvent = event.srcElement;
	
	if(event.keyCode == 13 && objEvent.id == "")
	{ 
		// Cancela o evento default
		event.returnValue = false; 
		event.cancel = true; 
		
		//btn.click(); 
	}
}

function EventRaisedBy(pStrObjectName)
{	
	if(gObjActive != null)
	{
		ErrorHandler();
		var strActive = gObjActive.name.toString().toUpperCase();
		if(strActive.indexOf("_DISAB") == -1)
			return(strActive.indexOf(pStrObjectName.toString().toUpperCase()) != -1);
		else
			throw (new String());
	}

	return false;	
}

function EfetuarPagamento()
{
	alert('Agora é soh pagar!!!');
	document.pagamento.submit();
}

function VerificaSelecaoTipoPagto()
{
	var ret         = false;
	
	var boleto     = document.getElementById("WUCCliente_FormaPagamento_rdbBoleto");
	var amex       = document.getElementById("WUCCliente_FormaPagamento_rdbAmex");
	var visa       = document.getElementById("WUCCliente_FormaPagamento_rdbVisa");
	var mastercard = document.getElementById("WUCCliente_FormaPagamento_rdbMasterCard");
	
	if(boleto != null)
	{
		if(boleto.checked)
		{
			ret = true;
		}
	}
	
	if(amex != null)
	{
		if(amex.checked)
		{
			ret = true;
		}
	}
	
	if(visa != null)
	{
		if(visa.checked)
		{
			ret = true;
		}
	}
	
	if(mastercard != null)
	{
		if(mastercard.checked)
		{
			ret = true;
		}
	}
	
	if(!ret)
	{
		alert('Selecione uma forma de pagamento');
	}
	
	return ret;
}

function SelecionarItemCombo(pObjCombo, valor)
{
	var objCombo = document.getElementById(pObjCombo);
	
	if(objCombo != null)
	{
		for(i = 0; i < objCombo.options.length; i++)
		{
			if(objCombo.options[i].value == valor)
			{
				objCombo.selectedIndex = i;
			}
		}
	}
}

function SendTabWhenFull(obj)
{
	if(obj != null)
	{
		if(obj.getAttribute("maxlength") != null)
		{
			if(obj.getAttribute("maxlength") == obj.value.length)
			{
				for(i = 0; i < obj.form.elements.length; i++)
				{
					if(obj.form.elements[i].name.replace(":", "_") == obj.id)
					{
						obj.form.elements[i + 1].focus();
					}
				}
			}
		}
	}
}

function EnviarParaAmigoFoto(id)
{
	largura = 300;
	altura  = 303;
	
	topo     = (screen.height/2)-(altura/2);
	esquerda = (screen.width/2) - (largura/2);
	
	window.open("email_envia.aspx?id=" + id + "&tipo=f", "fotos",'width=' + largura + ',height=' + altura + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=' + topo + ',left=' + esquerda);
}

function ProcurarCepCorreios()
{
	largura = 800;
	altura  = 500;
	
	topo     = (screen.height/2)-(altura/2);
	esquerda = (screen.width/2) - (largura/2);
	
	window.open("http://www.correios.com.br/servicos/cep/cep_default.cfm", "cep",'width=' + largura + ',height=' + altura + ',toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=' + topo + ',left=' + esquerda);
}

function AlterarSenha()
{
	largura = 400;
	altura  = 170;
	
	topo     = (screen.height/2)-(altura/2);
	esquerda = (screen.width/2) - (largura/2);
	
	window.open("cliente_alterar_senha.aspx", "senha",'width=' + largura + ',height=' + altura + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=' + topo + ',left=' + esquerda);
}

function EnviarParaAmigoAniver(id)
{
	largura = 300;
	altura  = 303;
	
	topo     = (screen.height/2)-(altura/2);
	esquerda = (screen.width/2) - (largura/2);
	
	window.open("email_envia.aspx?id=" + id + "&tipo=a", "fotos",'width=' + largura + ',height=' + altura + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=' + topo + ',left=' + esquerda);
}

function AbrirPopupFoto(id_foto)
{
	largura = 650;
	altura  = 490;
	
	topo     = (screen.height/2)-(altura/2);
	esquerda = (screen.width/2) - (largura/2);
	
	window.open("visualizar_foto.aspx?id=" + id_foto, "fotos",'width=' + largura + ',height=' + altura + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=' + topo + ',left=' + esquerda);
}

function NomearLinksMenos()
{
	var objLinks = document.getElementsByName("lnkMenos");
	
	for(var i=0; i < objLinks.length; i++)
	{
		objLinks[i].setAttribute("idEnum", i)
	}
}

function FormataData(objDataField)
{	
	var keyCode = event.keyCode;
	
	if(keyCode == 8)
	{
		event.keyCode = 0;
		objDataField.Value = "";
	}
	
	if ((objDataField.value.length == 2) || (objDataField.value.length == 5))
	{
		objDataField.value = objDataField.value + '/';
	}
}

function confirmDelete(item)
{
	return confirm("Deseja realmente excluir esse(a) " + item + "?");
}

function confirmAlteracaoPedido()
{
	return confirm("Deseja realmente gravar as alteracoes feitas neste pedido?");
}

function Buscar(id)
{	
	largura = 600;
	altura = 300;
	
	topo = (screen.height/2)-(altura/2);
	esquerda = (screen.width/2) - (largura/2);
	
	jan=window.open("adm_procurar.aspx?id=" + id, "busca",'width=' + largura + ',height=' + altura + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=' + topo + ',left=' + esquerda);
	
	jan.creator=self;
}

function TrataEnter(ke){
	if(ke == 13){
		cmdProcurar.Click();
	}
}

function validarImagem(source, args){
	if((args.Value.toLowerCase().indexOf(".jpg") > 0) || (args.Value.toLowerCase().indexOf(".gif") > 0))
	{
		args.IsValid = true;
	}
	else
	{
		args.IsValid = false;
	}
}

function validarPdf(source, args){
	if(args.Value.toLowerCase().indexOf(".pdf") > 0)
	{
		args.IsValid = true;
	}
	else
	{
		args.IsValid = false;
	}
}

function IsValidDate(source, args) 
{	
	var intDay;
	var intMonth;
	var intYear;
	
	if(args != null)
	{		
		if (args.Value.toString() != "")
		{
			var objArrField = args.Value.toString().split("/");
			
			intDay   = objArrField[0];
			intMonth = objArrField[1];
			intYear  = objArrField[2];		
			
			if ((intDay < 0) || (intDay > 31) || (intMonth < 1) || (intMonth > 12) || (intYear < 1800))
			{
				args.IsValid = false;
				return false;
			}

			if (intDay > 31 && (intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12))
			{
				args.IsValid = false;
				return false;
			}
						
			if (intDay > 30 && (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11))
			{
				args.IsValid = false;
				return false;
			}

			var blnLeapYear = false;
			var intTestYear = Math.floor(intYear/4);		

			intTestYear = intTestYear * 4;
			
			if (intTestYear == intYear)
				blnLeapYear = true;			

			if ((blnLeapYear) && (intMonth == 2) && (intDay > 29)) 
			{
				args.IsValid = false;
				return false;
			}
			
			if ((!blnLeapYear) && (intMonth == 2) && (intDay > 28)) 
			{			
				args.IsValid = false;
				return false;
			}			
		}
	}
	
	args.IsValid = true;
}

//Remove the $ sign if you wish the parse number to NOT include it
var prefix=""
var wd

function FormatCurrency(thisone){
	if (thisone.value.charAt(0)=="$")
	{
		return
	}
	
	wd="w"
	var tempnum=thisone.value
	
	for (i=0;i<tempnum.length;i++)
	{
		if (tempnum.charAt(i)==".")
		{
			wd="d"
			break
		}
	}
	
	if (wd=="w")
	{
		thisone.value=prefix+tempnum+".00"
	}
	else
	{
		if (tempnum.charAt(tempnum.length-2)==".")
		{
			thisone.value=prefix+tempnum+"0"
		}
		else
		{
			tempnum=Math.round(tempnum*100)/100
			thisone.value=prefix+tempnum
		}
	}
}

function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13) return true;  // Enter

	key = String.fromCharCode(whichCode);  // Get key value from key code

	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key

	len = fld.value.length;

	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;

	aux = '';

	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;

	len = aux.length;

	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;

	if (len > 2) {
		aux2 = '';
		
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
		aux2 += aux.charAt(i);
		j++;
	}

	fld.value = '';
	len2 = aux2.length;

for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);

fld.value += decSep + aux.substr(len - 2, len);
}
	return false;
}


/////////////Validacoes //////////////////////////////////////////////////////////
function ErrorHandler()
{
	gStrError	   = '';
	gObjFirstError = null;
}

function Is_Valid_CPF(objCpf, pStrMessage)
{
	var wVr, wVrCPF, wVrSEQ, wTam, wSoma, wSoma2, i, j, wDig1, wDig2,aux,
	    wVETOR_CC = new Array(11), 
	    wVETOR_PESO = new Array(11);
	    
 	var Erro = false;
 	
 	objField1 = document.getElementById(objCpf);
	wVrCPF = document.getElementById(objCpf).value;
	aux = document.getElementById(objCpf).value;
	
	wVr= aux;
	wTam = wVr.length + 1;
	
    if (wTam < 11)
    {
	  AddError(objField1, "Número de dígitos do CPF menor que o normal. Redigite !!!");
	  return false;
	}
    
	for (i = 0; i < wVr.length; i++) 
	{
	   if (isNaN(parseInt(wVr.charAt(i))) ) 
	   {
	     AddError(objField1, "O CPF contém dígitos inválidos. Corrija-o !!!");
	     return false;
	   }
	}
    
    wSoma = 0;
	wSoma2 = 0;
    j = 2;
    
    for (i = 0; i < 11; i++) {
	   wVETOR_CC[i] = wVr.charAt(i);
	   wVETOR_PESO[i] = j;
	   j++; 
	} 
    
    i = 0;	  
    
    while (i < 9)  {
	   i++;
	   if (i < 10) {
          wSoma += wVETOR_CC[9 - i] * wVETOR_PESO[i - 1]; }
	   wSoma2 += wVETOR_CC[10 - i] * wVETOR_PESO[i - 1];
	}
	
	wDig1 = (wSoma * 10) % 11;
	wDig2 = (wSoma2 * 10) % 11;
	
	if (wDig1 == 10) { 
	    wDig1 = 0;
	}
	
	if (wDig2 == 10) { 
	    wDig2 = 0;
	}
	
    if (parseInt(wVr.charAt(9)) != wDig1 || parseInt(wVr.charAt(10)) != wDig2) {
	     AddError(objField1, "CPF com Dígito Verificador inválido. Redigite-o !!!");
	     return false;
    }
	
	return true;
}

function IsEmpty(pStrFieldName, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);
	if(objField != null)
	{			
		if(objField.value.toString().trim() == '')
		{
			AddError(objField, pStrMessage);
			return false;
		}
	}
	return true;
}

function IsMax(pStrFieldName, pValorMaximo, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);
	
	if(objField != null)
	{
		if(objField.value.toString() > pValorMaximo)
		{
			AddError(objField, pStrMessage);
			return false;
		}
	}
	
	return true;
}

function IsLessThan(pStrFieldName, pTamanho, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);
	
	if(objField != null)
	{			
		if(objField.value.toString().trim().length < pTamanho)
		{
			AddError(objField, pStrMessage);
			return false;
		}
	}
	
	return true;
}

function IsDifferent(pStrFieldName1, pStrFieldName2, pStrMessage)
{
	var objField1 = document.getElementById(pStrFieldName1);
	var objField2 = document.getElementById(pStrFieldName2);
	
	if((objField1 != null) && (objField2 != null))
	{		
		if(objField1.value.toString() != objField2.value.toString())
		{
			objField1.value = "";
			objField2.value = "";
			AddError(objField1, pStrMessage);
			return false;
		}
	}
	return true;
}

function IsSelected(pStrFieldName, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);
	if(objField != null)
	{		
		if(objField.options[objField.selectedIndex].value.toString() == '')
		{	
			AddError(objField, pStrMessage);
			return false;
		}
	}
	return true;
}

function IsCheckedList(pStrFieldName, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);	
	if ((objField != null) && (objField.tagName.toString().toUpperCase() == "TABLE"))
	{	
		var blnChecked = false;
		
		var objCheckBoxList = objField.getElementsByTagName("input");
		for (var intI = 0; intI < objCheckBoxList.length; intI++)
		{
			if ((objCheckBoxList[intI].type.toString().toUpperCase() == "CHECKBOX") && (objCheckBoxList[intI].checked))
			{
				blnChecked = true;
				break;
			}
		}
		
		if (!blnChecked)
		{
			AddError(objField, pStrMessage);
			return false;		
		}
	}
	return true;
}

function IsTreeViewSelected(pStrTreeViewName, pStrMessage, pBlnHasRoot)
{	
	var objTreeView = document.getElementById(pStrTreeViewName);	
	if(objTreeView != null)
	{	
		var objTreeNode = objTreeView.getTreeNode(objTreeView.selectedNodeIndex);		
		if(objTreeNode != null)
		{	
			var strCompareID = '';			
			if(pBlnHasRoot)	strCompareID = '_1'; //Root = _1
			
			if(objTreeNode.getAttribute("id").toString() == strCompareID)
			{				
				AddError(objTreeView, pStrMessage);
				return false;
			}
		}
	}
	return true;
}

function IsValidEmail(pStrFieldName, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);
	if(objField != null)
	{
		if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(objField.value.toString())))
		{
			AddError(objField, pStrMessage);
			return false;
		}
	}
	return true;
}

function IsNumber(pStrFieldName, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);
	if(objField != null)
	{
		if((objField.value.toString() != '') && (isNaN(objField.value.toString())))
		{
			AddError(objField, pStrMessage);
			return false;	
		}				
	}
	return true;
}

function IsFloat(pStrFieldName, pStrMessage)
{
	var objField = document.getElementById(pStrFieldName);
	if(objField != null)
	{
		var strField = objField.value.toString();
		strField = strField.replace(/,/g, '.');
		
		if((strField != '') && (isNaN(strField)))
		{
			AddError(objField, pStrMessage);
			return false;	
		}				
	}
	return true;
}

function IsValidBeginEndDate(pStrFieldNameB, pStrFieldNameE, pStrMessage)
{
	var objFieldB = document.getElementById(pStrFieldNameB);
	var objFieldE = document.getElementById(pStrFieldNameE);
	var dtB;
	var dtE;
	
	if((objFieldB != null) && (objFieldE != null))
	{
		dtB = new Date(objFieldB.value);
		dtE = new Date(objFieldE.value);
		
		if(dtB.getTime() > dtE.getTime())
		{
			AddError(objFieldB, pStrMessage);
			return false;
		}
	}
	
	return true;
}

function IsValidDate(pData, pStrFieldName, pStrMessage) 
{	
	var intDay;
	var intMonth;
	var intYear;
	
	objField = document.getElementById(pStrFieldName);
	
	if(objField != null)
	{
		if (pData != "")
		{
			var objArrField = pData.split("/");
			
			intDay   = objArrField[0];
			intMonth = objArrField[1];
			intYear  = objArrField[2];		
			
			if ((intDay < 0) || (intDay > 31) || (intMonth < 1) || (intMonth > 12) || (intYear < 1800))
			{
				AddError(objField, pStrMessage);
				return false;	
			}

			if (intDay > 31 && (intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12))
			{
				AddError(objField, pStrMessage);
				return false;	
			}
						
			if (intDay > 30 && (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11))
			{
				AddError(objField, pStrMessage);
				return false;	
			}

			var blnLeapYear = false;
			var intTestYear = Math.floor(intYear/4);		

			intTestYear = intTestYear * 4;
			
			if (intTestYear == intYear)
				blnLeapYear = true;			

			if ((blnLeapYear) && (intMonth == 2) && (intDay > 29)) 
			{
				AddError(objField, pStrMessage);
				return false;	
			}
			
			if ((!blnLeapYear) && (intMonth == 2) && (intDay > 28)) 
			{			
				AddError(objField, pStrMessage);
				return false;	
			}			
		}
	}			
	return true;	
}

function IsDateBiggerToday(pStrFieldName, pStrMessage) 
{
	var objField = document.getElementById(pStrFieldName);
	if(objField != null)
	{			
		var datToday = new Date(strDateToday());		
		var datField = new Date(objField.value);

		if(datField.getTime() > datToday.getTime())
		{
			AddError(objField, pStrMessage);
			return false;				
		}
	}
	return true;
}

function strDateToday()
{
	var objDatToday = new Date();
	
	var strDay   = objDatToday.getDate(); 
	var strMonth = objDatToday.getMonth() + 1; 
	var strYear  = objDatToday.getYear();
	
	if(parseInt(strDay) < 10) strDay = ("0" + strDay);
	if(parseInt(strMonth) < 10) strMonth = ("0" + strMonth);
	
	return strDay + "/" + strMonth + "/" + strYear; 
}
	
function SetHiddenField(pStrHiddenFieldName, pStrValue)
{
	var objField = document.getElementById(pStrHiddenFieldName);
	if(objField != null)	
		objField.value = pStrValue.toString();
}
	
function AddError(pObjField, pStrMessage)
{
	gStrError += '--> ' + pStrMessage + '\n';
	if(gObjFirstError == null) gObjFirstError = pObjField;
}

function RaiseError()
{
	var blnOk = (gObjFirstError == null);
	
	if(blnOk == false)
	{
		alert('Verifique os seguintes erros:' + '\n' + gStrError);
		
		try
		{
			gObjFirstError.focus();
		}catch(e){}
	}
	gStrError = "";
	return(blnOk)
}

function Call_Search(pObjButton)
{
	var keyCode = event.keyCode;
	
	if(keyCode == 13)
	{
		var objBtnSearch = document.getElementById("LMHeaderControl:" + pObjButton);
		
		objBtnSearch.click();
	}
}
//////////////////////////////////////////////////////////////////////////////////

function strLTrim()
{
	return this.replace(/^\s+/,'');
}

function strRTrim()
{
	return this.replace(/\s+$/,'');
}

function strTrim()
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.ltrim = strLTrim;
String.prototype.rtrim = strRTrim;
String.prototype.trim  = strTrim;