// JavaScript Document


function frmSetDefaultValue(id) {
	
	$("#"+id).val($("#"+id).attr("title"));
	
	$("#"+id).blur(function() {
		
		if(jQuery.trim(this.value) == "") {
			this.value = this.title;	
		}
	});
	
	$("#"+id).focus(function() {
		
		if(jQuery.trim(this.value) == this.title) {
			this.value = "";	
		}
	});
	
}


function retiraAcentuacao(texto) {
	
	var chrEspeciais = new Array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë",
				     "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö",
				     "ú", "ù", "û", "ü", "ç",
				     "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë",
				     "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö",
				     "Ú", "Ù", "Û", "Ü", "Ç");
	var chrNormais = new Array("a", "a", "a", "a", "a", "e", "e", "e", "e",
				   "i", "i", "i", "i", "o", "o", "o", "o", "o",
				   "u", "u", "u", "u", "c",
				   "A", "A", "A", "A", "A", "E", "E", "E", "E",
				   "I", "I", "I", "I", "O", "O", "O", "O", "O",
				   "U", "U", "U", "U", "C");
	
	for (index in chrEspeciais) {
		texto = texto.replace(chrEspeciais[index], chrNormais[index]);
	}

	return texto;
}

function onlyNumber(evt)
{
	 var charCode = (evt.which) ? evt.which : event.keyCode;
	 if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	 return true;
}

function AbreJanela(szJanela, szUrl, intWidth, intHeight ) 
{
    var posx = (screen.width-intWidth)/2;
    var posy = (screen.height-intHeight)/2;

    win = window.open(szUrl,szJanela,'toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,menubar=no,width='+intWidth+',height='+intHeight+',screenX='+posx+',screenY='+posy);
    win.moveTo(posx,posy);
    win.focus();
}

function hideMessage(id, timeOut) {

	try {
		setTimeout("$('#"+id+"').fadeOut(2000)", timeOut);
	} catch (e) {}
}

function gerSelect(form, campoout, campoin){
	
	if(document.forms[form].elements[campoout].options.selectedIndex==-1) {
		
		alert('Selecione um item para mover!')
		return;
		
	} else {
		
		var list = document.forms[form].elements[campoout];
		
		for(i = 0 ; i < list.options.length ; i++ ) {
								
			if(list.options[i].selected == true){
		
				insereOption(list.options[i].value, list.options[i].text, form,campoin);
				
			} 
		}
			
		deletaOption(document.forms[form].elements[campoout]);
	}
}

function insereOption(valor,nome,form,campo) {
	
	var ob;
	var i;
	
	ob = document.forms[form].elements[campo];
	
	if(ob.options.length) {
		
		for(i=0; i < ob.options.length ; i++) {
			
			if(ob.options[i].value == valor){
				return;
			}
		}
	}
	
	ob.options[ob.options.length] = new Option(nome,valor);
}

function deletaOption(list) {
	
	for(i = 0 ; i < list.options.length ; ) {
		
		if(list.options[i].selected == true){
			list.options[i] = null;
			
		} else {
			i++;
		}
	}
}

function selectAllOptions(obj) {
	
	if(obj.options.length) {
		
		for(i = 0; i < obj.options.length ; i++) {
			
			obj.options[i].selected = true;
		}
	}
}

function joinOptions(sel) {
	
	var t = '';
	
	for( var i=0; i < sel.length; i++ ) {
		
		if((i+1)==sel.length){
			t+=sel.options[i].value; 
		}else{
			t+=sel.options[i].value+','; 
		}
	}
	return t;
}

function getRandomNumber(range) {
	
	return Math.floor(Math.random() * range);
}

function getRandomChar() {
	
	var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
	return chars.substr( getRandomNumber(62), 1 );
}

function randomID(size) {
	
	var str = "";
	for(var i = 0; i < size; i++)
	{
		str += getRandomChar();
	}
	return str;
}
 

function testPassword(passwd) {
	
	var description = new Array();
	description[0] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=22 width=30 bgcolor=#ff0000></td><td height=15 width=120 bgcolor=#dddddd></td></tr></table></td><td class=bold>Muito Fraco</td></tr></table>";
	description[1] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=22 width=60 bgcolor=#bb0000></td><td height=15 width=90 bgcolor=#dddddd></td></tr></table></td><td class=bold>Fraco</td></tr></table>";
	description[2] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=22 width=90 bgcolor=#ff9900></td><td height=15 width=60 bgcolor=#dddddd></td></tr></table></td><td class=bold>Médio</td></tr></table>";
	description[3] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=22 width=120 bgcolor=#00bb00></td><td height=15 width=30 bgcolor=#dddddd></td></tr></table></td><td class=bold>Forte</td></tr></table>";
	description[4] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=22 width=150 bgcolor=#00ee00></td></tr></table></td><td class=bold>Muito Forte</td></tr></table>";
	description[5] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=22 width=150 bgcolor=#dddddd></td></tr></table></td><td class=bold>&nbsp;</td></tr></table>";
 
	var intScore   = 0
	var strVerdict = 0
	
	// PASSWORD LENGTH
	if (passwd.length==0 || !passwd.length)                         // length 0
	{
		intScore = -1
	}
	else if (passwd.length>0 && passwd.length<5) // length between 1 and 4
	{
		intScore = (intScore+3)
	}
	else if (passwd.length>4 && passwd.length<8) // length between 5 and 7
	{
		intScore = (intScore+6)
	}
	else if (passwd.length>7 && passwd.length<12)// length between 8 and 15
	{
		intScore = (intScore+12)
	}
	else if (passwd.length>11)                    // length 16 or more
	{
		intScore = (intScore+18)
	}
	
	
	// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
	if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
	{
		intScore = (intScore+1)
	}
	
	if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
	{
		intScore = (intScore+5)
	}
	
	// NUMBERS
	if (passwd.match(/\d+/))                                 // [verified] at least one number
	{
		intScore = (intScore+5)
	}
	
	if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
	{
		intScore = (intScore+5)
	}
	
	
	// SPECIAL CHAR
	if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
	{
		intScore = (intScore+5)
	}
	
															 // [verified] at least two special characters
	if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
	{
		intScore = (intScore+5)
	}

	
	// COMBOS
	if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
	{
		intScore = (intScore+2)
	}

	if (passwd.match(/(\d.*\D)|(\D.*\d)/))                    // [FAILED] both letters and numbers, almost works because an additional character is required
	{
		intScore = (intScore+2)
	}

															  // [verified] letters, numbers, and special characters
	if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
	{
		intScore = (intScore+2)
	}


	if(intScore == -1)
	{
	   strVerdict = description[5];
	}
	else if(intScore > -1 && intScore < 16)
	{
	   strVerdict = description[0];
	}
	else if (intScore > 15 && intScore < 25)
	{
	   strVerdict = description[1];
	}
	else if (intScore > 24 && intScore < 35)
	{
	   strVerdict = description[2];
	}
	else if (intScore > 34 && intScore < 45)
	{
	   strVerdict = description[3];
	}
	else
	{
	   strVerdict = description[4];
	}

	document.getElementById("passwordMeterWords").innerHTML= (strVerdict);
	
}

function trocaAba(id) {
	
	
	
	try {
		
		
		
		
		$("#galeriaImgList img").removeClass("current_two").addClass("current");
		$(this).children().attr('class', 'current_two');
		
		
		
		var arrayDiv = $(".current");
		
		if(arrayDiv.length > 0) {
			$("#aba_"+ arrayDiv[0].id).css("display", "none");
		}
						
		$("#aba_"+id).fadeIn("fast");
		$(".current").removeAttr("class");
		
		$("#"+id).attr("class", "current");
					
	} catch(e) {
		alert(e.message);	
	}
}

function imprimir(nome,email,departamento,assunto,texto,resposta){
    
    var html ="<style type='text/css'>"+
                    "#box"+
                    "{  width: 100%;"+
                       "font: normal 12px/18px Arial, Helvetica, sans-serif }"+
                    "#box h1 { line-height: 22px; font-family: Georgia, 'Times New Roman', Times, serif; margin: 10px 0; }"+
                    "#box small { font-size: 12px; }"+
                    "#box strong { font-family: Georgia, 'Times New Roman', Times, serif; }"+
                    "#box table { margin: 14px 0; }"+
              "</style>"+
                "<div id='box'> "+
                    "<h1>CONSELHO REGIONAL DE FARM&Aacute;CIA</h1>"+
                    "<hr/>"+
                    "<table width='100%' border='0' cellspacing='2' cellpadding='0'>"+
                        "<tr>"+
                            "<td width='21%'>Nome:</td>"+
                            "<td width='79%'><strong>"+$("#"+nome).val()+"</strong></td>"+
                        "</tr><tr>"+
                            "<td> E-mail: </td>"+
                            "<td><strong>"+$("#"+email).val()+"</strong></td>"+
                        "</tr><tr>"+
                            "<td>Departamento:</td>"+
                            "<td><strong>"+$("#"+departamento).val()+"</strong></td>"+
                        "</tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr>"+
                            "<td>Assunto:</td>"+
                            "<td><strong>"+$("#"+assunto).val()+"</strong></td>"+
                        "</tr><tr>"+
                            "<td valign='top' width='21%'>Mensagem:</td>"+
                            "<td width='79%'><p>"+
                                $("#"+texto).html()+
                            "</p></td>"+
                        "</tr>";

              if($("#"+resposta).html()!=""){
              html +=   "</tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr>"+
                            "<td valign='top' width='21%'>Resposta:</td>"+
                            "<td width='79%'><p>"+
                                $("#"+resposta).html()+
                            "</p></td>"+
                        "</tr>";
              }
             html +="</table><hr />"+
                    "<p><strong>CONSELHO REGIONAL DE FARM&Aacute;CIA</strong><br />Av. Rodolfo Jos&eacute; Pinho, 66 | Campo Grande - MS | 79004-690 | (0xx)67 3325-8090 <br /></p>"+
                "</div>";

    var x = document.createElement('iframe');
    x.setAttribute('style','visibility:hidden');
    x.setAttribute('WIDTH','1');
    x.setAttribute('name','frameImpressao');
    x.setAttribute('id','frameImpressao');
    x.setAttribute('HEIGHT','1');

    document.getElementById('footer').appendChild(x);

    x.contentWindow.document.open();
    x.contentWindow.document.write(html);
    x.contentWindow.document.close();

    frames['frameImpressao'].focus();
    frames['frameImpressao'].print();
}

function showAlert(msg) {
	
	jAlert(msg, 'ATENÇÃO!');
}

