

// Password strength meter v1.0
// Matthew R. Miller - 2007
// www.codeandcoffee.com
// Based off of code from  http://www.intelligent-web.co.uk

// Settings
// -- Toggle to true or false, if you want to change what is checked in the password
var bCheckNumbers = true;
var bCheckUpperCase = true;
var bCheckLowerCase = true;
var bCheckPunctuation = true;
var nPasswordLifetime = 365;

// Check password
function checkPassword(strPassword)
{
	// Reset combination count
	nCombinations = 0;
	
	// Check numbers
	if (bCheckNumbers)
	{
		strCheck = "0123456789";
		if (doesContain(strPassword, strCheck) > 0) 
		{ 
        		nCombinations += strCheck.length; 
    		}
	}
	
	// Check upper case
	if (bCheckUpperCase)
	{
		strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		if (doesContain(strPassword, strCheck) > 0) 
		{ 
        		nCombinations += strCheck.length; 
    		}
	}
	
	// Check lower case
	if (bCheckLowerCase)
	{
		strCheck = "abcdefghijklmnopqrstuvwxyz";
		if (doesContain(strPassword, strCheck) > 0) 
		{ 
        		nCombinations += strCheck.length; 
    		}
	}
	
	// Check punctuation
	if (bCheckPunctuation)
	{
		strCheck = ";:-_=+\|//?^&!.@$£#*()%~<>{}[]";
		if (doesContain(strPassword, strCheck) > 0) 
		{ 
        		nCombinations += strCheck.length; 
    		}
	}
	
	// Calculate
	// -- 500 tries per second => minutes 
    	var nDays = ((Math.pow(nCombinations, strPassword.length) / 500) / 2) / 86400;
 
	// Number of days out of password lifetime setting
	var nPerc = nDays / nPasswordLifetime;
	
	return nPerc;
}
 
// Runs password through check and then updates GUI 
function runPassword(strPassword, strFieldID) 
{
	// Check password
	nPerc = checkPassword(strPassword);
	
	 // Get controls
    	var ctlBar = document.getElementById(strFieldID + "_bar"); 
    	var ctlText = document.getElementById(strFieldID + "_text");
    	if (!ctlBar || !ctlText)
    		return;
    	
    	// Set new width
    	var nRound = Math.round(nPerc * 100);
	if (nRound < (strPassword.length * 5)) 
	{ 
		nRound += strPassword.length * 5; 
	}
	if (nRound > 100)
		nRound = 100;
    	ctlBar.style.width = nRound + "%";
 
 	// Color and text
 	if (nRound > 95)
 	{
 		strText = "Nível Excelente de Segurança.";
 		strColor = "#3bce08";
 	}
 	else if (nRound > 75)
 	{
 		strText = "Nível Alto de Segurança.";
 		strColor = "orange";
	}
 	else if (nRound > 50)
 	{
 		strText = "Nível Médio de Segurança.";
 		strColor = "#ffd801";
 	}
 	else
 	{
		strText = "Nível Baixo de Segurança.";
 		strColor = "red";
 	}
	ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
}
 
// Checks a string for a list of characters
function doesContain(strPassword, strCheck)
 {
    	nCount = 0; 
 
	for (i = 0; i < strPassword.length; i++) 
	{
		if (strCheck.indexOf(strPassword.charAt(i)) > -1) 
		{ 
	        	nCount++; 
		} 
	} 
 
	return nCount; 
}
/* Fim password */

/* Formata Valor Novo */
function Limpar(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}


function formatarGenerico(src, mask, teclapres){
	
	var i     = src.value.length;
  	var saida = mask.substring(0,1);
  	var texto = mask.substring(i)
  	
  	/* Se a tecla não for a de apagar */
  	if(teclapres.keyCode!=8){
		if (texto.substring(0,1) != saida)
  		{
    		src.value += texto.substring(0,1);
	  	}
	}
  	
}

/*Formata número tipo moeda usando o evento onKeyDown*/
function Formata(campo,tammax,teclapres,decimal) {

	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal

	if(tam < tammax && tecla != 8){ 
		tam = vr.length + 1 ; 
	}

	if (tecla == 8 ){ 
		tam = tam - 1 ; 
	}
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{

		if ( tam <= dec ){ 
			campo.value = vr; 
		}

		if ( (tam > dec) && (tam <= 5) ){
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam );
		}
	
		if ( (tam >= 6) && (tam <= 8) ){
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ); 
		}
	
		if ( (tam >= 9) && (tam <= 11) ){
			campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
		}
		
		if ( (tam >= 12) && (tam <= 14) ){
			campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ); 
		}
		
		if ( (tam >= 15) && (tam <= 17) ){
			campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam );
		}
	} 

}
/* fim */

var dir = 'sys/images/'
	
function imageOn(obj,name) {
	obj.src = dir+name+'_f2.png';
}	
		
function imageOut(obj,name) {
	obj.src = dir+name+'.png';
}
		


function M_Over(obj,iclass) {
	obj.className= iclass+'s';
}


function M_Out(obj,iclass) { 

	var oChildren = obj.childNodes;
	var iChildren = oChildren.length;
	
	for (var i = 0; i < iChildren; i++)
    {
     	var oChild = oChildren.item(i);
     	var oChildi = oChild.childNodes;
    	var iChildi = oChildi.length;
    		for (var x = 0; x < iChildi; x++) {	
    				var oItem= oChildi.item(x);
  					if(oItem.type=='checkbox') {
    						obj.className = iclass + ( (oItem.checked)?'c':'' ); 
  					}	
    		}
    }
}

function M_Out2(obj,iclass) { 
	obj.className= iclass;
}

function CheckAll(form) {
	
	form = (form)?form:'form';
	
	objs = document.getElementsByTagName('INPUT');
	eval("var ckall = document."+form+".ckall;");
	
	if(ckall.value==1) {
		action = 'false';
		ckall.value=0; 	
	} else {
		action = 'true';
		ckall.value=1; 	
	}
		
	eval("var len = document."+form+".elements.length;");

	for(i=0;  i < len ; i++) { 
		eval("var e = document."+form+".elements[i];");
		
		if (e.type == 'checkbox' && !e.disabled) {
			var td = e.parentNode;
        	var tr = td.parentNode;
        	var classn = tr.className;
     		 		if (ckall.value==0) {
        				e.checked = '';
        				tr.className = classn.substr(0,4);
        			} else if(ckall.value==1 && !e.checked) {
          				e.checked = 'true';
						tr.className = classn+'c';
        			}
			}
      	}
	}
	
function Check(id) { 
		
	obj  = document.getElementById("ck_"+id);
	obj2 = document.getElementById('tr_'+id);
	
	var classn = obj2.className;
	
	if(obj.checked=='') {
		obj.checked = 'true'
		obj2.className = classn.substr(0,4);
	} else {
		obj.checked = ''
		obj2.className = classn+'c';
	}
	
}

	
    function error() {
      document.getElementById('loader').style.display = 'none';
      document.getElementById('erro').style.display = 'block';
    }
    
    
    
    function excl(form,msg) {
    	form = (form)?form:'form';
    	
    	msg = (msg)?msg:"Deseja excluir esse(s) registro(s) ?";
    	
    	if(confirm(msg)) {
    		eval("document."+form+".action.value = 'excl'");
    		eval("document."+form+".submit()");
    	}
    }
    
    
    function excl2(form,msg) {
    	form = (form)?form:'form';
    	
    	msg = (msg)?msg:"Deseja excluir esse(s) registro(s) ?";
    	
    	if(confirm(msg)) {
    		eval("document."+form+".pressed.value = ''");
			eval("document."+form+".target = '_self'");
			eval("document."+form+".action = ''");
    		eval("document."+form+".act.value = 'excl'");
    		eval("document."+form+".submit()");
    	}
    } 
    
    function morto(form)
	{
		form = (form)?form:'form';	
		if(confirm("Deseja cancelar este(s) pedidos(s)?"))
		{
			eval("document."+form+".pressed.value = ''");
			eval("document."+form+".target = '_self'");
			eval("document."+form+".action = ''");
			eval("document."+form+".act.value = 'morto'");
	    	eval("document."+form+".submit()");
		}
	}

    
    function help(page) { 
    	window.open('sys/help/'+page+'.php','help','width=650,height=400,scrollbars=yes,resizable=yes');
    }
    
    function win(page) { 
    	window.open(page,'win','width=750,height=500,scrollbars=yes,resizable=yes');
    }
    
    function winPrint(page) { 
    	window.open(page,'win','width=750,height=500,scrollbars=yes,resizable=yes,menubar=yes');
    }
    
        
    function pop(vars) { 
    
    	wname = 'pop';
    		
    	if(window.name = pop)
    	{ 	
    		wname = 'pop1'			
    	}    	
    	
   		window.open('pop.php'+vars,wname,'top=0,left=0,width='+screen.width+',height='+(screen.height*0.5)+',scrollbars=yes,resizable=yes');
   		 
    }
    
    
    
     function add(vars) { 
    
    	wname = 'pop';
    		
    	if(window.name = pop)
    	{ 	
    		wname = 'pop1'			
    	}    	
    	
   		window.open('add.php'+vars,wname,'top=0,left=0,width='+screen.width+',height='+(screen.height*0.5)+',scrollbars=yes,resizable=yes');
   		 
    }
    
    
    function pop2(vars) { 
    	window.open('pop.php'+vars,'pop2','width=850,height=600,scrollbars=yes');
    }
    
    function list(vars) { 
    	window.open('list.php'+vars,'list','width=750,height=400,scrollbars=yes');
    }
    
    function addcadastro(page) { 
    	window.open(page,'addcadastro','width=400,height=400,scrollbars=yes');
    }
    
    function FinanceiroBaixa(id) { 
		window.open('sys/fluxo/fluxo_baixa.php?id='+id,'FinanceiroBaixa','width=450,height=450,scrollbars=yes');
    }
    
    
function str_replace(busca,subs,valor){
	var ret=valor;
	var pos=ret.indexOf(busca);
	while(pos!=-1){
		ret=ret.substring(0,pos)+subs+ret.substring(pos+busca.length,ret.length);
		pos=ret.indexOf(busca);
	}
	return ret;
}

function mascara(valor,masc){
	var res=valor,mas=str_replace("?","",str_replace("9","",masc));
	for(var i=0;i<mas.length;i++){
		res=str_replace(mas.charAt(i),"",res);
		mas=str_replace(mas.charAt(i),"",mas);
	}
	var ret="";
	for(var i=0;i<masc.length&&res!="";i++){
		switch(masc.charAt(i)){
			case"?":
				ret+=res.charAt(0);
				res=res.substring(1,res.length);
				break;
			case"9":
				while(res!=""&&(res.charCodeAt(0)>57||res.charCodeAt(0)<48))res=res.substring(1,res.length);
				if(res!=""){
					ret+=res.charAt(0);
					res=res.substring(1,res.length);
				}
				break;
			default:
				ret+=masc.charAt(i);
		}
	}
	return ret;
}



/**************************************
 * 
 *
 *
 *
 */


function AdvanceSearch(id) {
	
	obj 	= document.getElementById('advance');
	obj_img = document.getElementById('advanceImg');
	
	if(obj.style.display=='none') {
		obj.style.display = '';
		obj_img.src = 'sys/images/uparrow0.png';
		setCookie("AdvanceSearch"+id, "1");
	} else {
		obj.style.display = 'none';
		obj_img.src = 'sys/images/downarrow0.png';
		setCookie("AdvanceSearch"+id, "0");
	}
	
	
}
 



function selreg(field, id, value1, VarEval) { 
	
	if(opener && typeof opener.document != 'undefined') {	
		obj1 = opener.document.getElementById(field).value = id;			
		obj2 = opener.document.getElementById(field+'Value').value = value1;
		
		//alert(eval);
		
		if(VarEval)
			eval(VarEval);
			
		window.close();
	}

	
}



/*
*	LibSug
*/


function addAlert(type, name,subtitle, description,time, redirect) {
	var addIndex = alertList.length;
	alertList[addIndex]= new Array();
	alertList[addIndex]['name'] = name;
	alertList[addIndex]['type'] = type;
	alertList[addIndex]['subtitle'] = subtitle;
	alertList[addIndex]['description'] = description.replace(/<br>/gi, "\n").replace(/&amp;/gi,'&').replace(/&lt;/gi,'<').replace(/&gt;/gi,'>').replace(/&#039;/gi,'\'').replace(/&quot;/gi,'"');
	alertList[addIndex]['time'] = time;
	alertList[addIndex]['done'] = 0;
	alertList[addIndex]['redirect'] = redirect;
} 

function checkAlerts() {
	secondsSinceLoad += 1;
	var mj = 0;
	var alertmsg = '';
	for(mj = 0 ; mj < alertList.length; mj++) {
		if(alertList[mj]['done'] == 0) {
			if(alertList[mj]['time'] < secondsSinceLoad && alertList[mj]['time'] > -1 ) {
				alertmsg = alertList[mj]['type'] + ":" + alertList[mj]['name'] + "\n" +alertList[mj]['subtitle']+ "\n"+ alertList[mj]['description'] + "\n\n";
				alertList[mj]['done'] = 1;
				if(alertList[mj]['redirect'] == '') {
					alert(alertmsg);
				} 
				else {
					window.focus();
					if(confirm(alertmsg)) {
						window.location = alertList[mj]['redirect'];
					}
				}
			}
		}
	}
	
	setTimeout("checkAlerts()", 1000);
}



function FormReset() 
{
	document.form.reset();
	document.form.mod.value='edit';
}

/* 
  Ori 	


var count1 = 0;
var count2 = 0;

function insertOptionBefore(num)
{
  var elSel = document.getElementById('selectX');
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = 'Insert' + num;
    elOptNew.value = 'insert' + num;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
}

function removeOptionSelected()
{
  var elSel = document.getElementById('selectX');
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

function appendOptionLast(num)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = 'Append' + num;
  elOptNew.value = 'append' + num;
  var elSel = document.getElementById('selectX');

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function removeOptionLast()
{
  var elSel = document.getElementById('selectX');
  if (elSel.length > 0)
  {
    elSel.remove(elSel.length - 1);
  }
}

*/ 

function appendOptionLast(field, id, value, selected)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = value;
  elOptNew.value = id;
  
  if(selected)
  	elOptNew.selected = 'selected';
  
  var elSel = document.getElementById(field);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}


function insertOptionBefore(field, id, value, selected, index)
{
  var elSel = document.getElementById(field);
  var Existe = 0;
  
  
 
  //alert(elSel.selectedIndex);
	
		  if (elSel.selectedIndex >= 0) {
		  	
					//alert('a');	
		  	
		  			 for (i = elSel.length - 1; i>=0; i--) {
					    	if (elSel.options[i].value==id) {
					      		Existe=1;
					    	}
					  }
		  	
						  	
					if(!Existe) 
				  	{
			  	
						    var elOptNew = document.createElement('option');
						    elOptNew.text = value;
						    elOptNew.value = id;
						   // alert(index);
						     if(selected)
							    elOptNew.selected = 'selected';
						    
							    if(index) 
							    {
							    	//alert(1);	
							    	var elOptOld = index;
							    }
							    else	
							    {
							    	//alert(2);	
						    		var elOptOld = elSel.options[elSel.selectedIndex];  
							    }
						    
						    try {
						      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
						    }
						    catch(ex) {
						      elSel.add(elOptNew, elSel.selectedIndex); // IE only
						    }
			  	
				  	}
		    
		  } else {
		  	

	  			 for (i = elSel.length - 1; i>=0; i--) {
				    	if (elSel.options[i].value==id) {
				      		Existe=1;
				    	}
				 }
					  	
				if(!Existe) 
			  	{
			  		
		  			appendOptionLast(field, id, value, selected)
		  	
			  	}
		  }
	
  
}

function removeOptionSelected(field)
{
  var elSel = document.getElementById(field);
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
  
}



function removeOptionLast()
{
  var elSel = document.getElementById('selectX');
  if (elSel.length > 0)
  {
    elSel.remove(elSel.length - 1);
  }
}

function addreg(field, id, value1, VarEval) { 

	//alert(field + id + value1);
	
	if(opener && typeof opener.document != 'undefined') {	
		//obj1 = opener.document.getElementById(field).value = id;			
		//obj2 = opener.document.getElementById(field+'Value').value = value1;
		opener.insertOptionBefore(field, id, value1, 0);
		
		//alert(eval);
		
		if(VarEval)
			eval(VarEval);
	}
}

function selectClear(id)
{
	document.getElementById(id).value = document.getElementById(id+'Value').value = '';
}

function selectSemRegistro(id)
{
	document.getElementById(id).value = '0'
	document.getElementById(id+'Value').value = 'Sem registro';
}



var lastSubmitTime = 0;	
var notId = 0;


function ykShortCut(id) {

		
	
		var _date = new Date();

		obj1 = document.getElementById(id);
		obj2 = document.getElementById(id + 'Value');
		obj3 = document.getElementById(id + '2');
		obj4 = document.getElementById(id + 'Value2');
		obj5 = document.getElementById(id+'C');
	
		shortcut("Ctrl+S",function() {
				obj1.value = obj3.value = '0';	
		    	obj2.value = obj4.value= 'Sem Registro';	
		    	obj5.value = '1';	
		    	//obj2.select();
		},{
    		'type':'keydown',
    		'propagate':false,
		    'target':document
		});
		
		shortcut("Ctrl+A",function() {
			
			if(_date.getTime() < (lastSubmitTime + 2000)) { 
				return false;
			}
			lastSubmitTime = _date.getTime();
			
			var browserName=navigator.appName; 
			
		    str = document.getElementById(id+'B1').onclick+"";
		    
		    if (browserName=="Microsoft Internet Explorer") {
		    	str = str.substr(23);
		    	str = str.substr(0, str.length - 1 );
		    } else {
		    	str = str.substr(26);
		    	str = str.substr(0, str.length - 1 );
		    }

		    eval(str);
		    
		    //obj1.value = OldId;	
		    //obj2.value = OldValue;	
		    //obj2.select();
		    
		},{
		    'type':'keydown',
		    'propagate':false,
		    'target':document
		});
		
		shortcut("Ctrl+D",function() {
				obj1.value = obj3.value = '';	
		    	obj2.value = obj4.value = '';	
		    	
		},{
		    'type':'keydown',
		    'propagate':false,
		    'target':document
		});
		
		
		/*
		shortcut("escape",function() {
			
			if(_date.getTime() < (lastSubmitTime + 2000)) { 
				return false;
			}
			lastSubmitTime = _date.getTime();
			
			
			//if( id == notId )
			//	return false;
			
			document.getElementById(id+'C').value = 0;
		    obj1.value = obj3.value = document.getElementById(id+'OLDID').value;	
		    obj2.value = obj4.value = document.getElementById(id+'OLDVALUE').value;	
		    
		    vDebug  = document.getElementById('debug');
		    vDebug.value += id+"\n"	;
		    	
		},{
		    'type':'keyup',
		    'propagate':false,
		    'target':document
		});
		
		*/

}