// JS COC - Funções Gerais
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

// FUNÇÃO NEWIMAGE
function newImage(strSrc) {
	i = new Image();
	i.src = strSrc;
	return i;
}

// FUNÇÃO LOADIMAGE
function loadImage(obj, img){
	obj.src = img.src;
}

// FUNÇÃO GETOBJ
function getObj(id) {
	if(document.all){
		return document.all(id);
	} else if (document.getElementById) {
		return document.getElementById(id);
	}
}

function setInnerHTML(id, str) {
	if (document.getElementById(id)) {
		document.getElementById(id).innerHTML = str;
	} else if (document.all[id]) {
		document.all[id].innerHTML = str;
	}
}

function getInnerHTML(id) {
	if (document.getElementById(id)) {
		return document.getElementById(id).innerHTML;
	} else if (document.all[id]) {
		return document.all[id].innerHTML;
	}
}

// FUNÇÃO OPENHIDEOBJ
function OpenHideObj(id)
{
	var elemento = getObj(id);
	
	if(elemento.style.display == 'none')
	{
		elemento.style.display = 'block';
	}
	else
	{
		elemento.style.display = 'none';
	}
}

// FUNÇÃO LOADFLASH
function loadFlash(arquivo, w, h) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + w + '" height="' + h + '" align="middle">\n');
	document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
	document.write('<param name="movie" value="' + arquivo + '" />\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="wmode" value="transparent" />\n');
	document.write('<param name="bgcolor" value="#f1f1f1" />\n');
	document.write('<embed src="' + arquivo + '" quality="high" wmode="transparent" bgcolor="#f1f1f1" width="' + w + '" height="' + h + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
	document.write('</object>\n');
}

// FUNÇÃO VALIDAEXTENSAO
function validaExtensao(campo){
	ext = getExtensao(campo);
	if(ext != ".doc"){ 
		alert('O arquivo deve ser do tipo DOC.');
		return false;
	}
}

// FUNÇÃO GETEXTENSAO
function getExtensao(s){
	var ext="";
	var pos;

	pos = s.lastIndexOf(".");
	if(pos > 0){
		ext = s.substr(pos);
		ext = ext.toLowerCase();
	}
	
	return ext;
}

// FUNÇÃO PARA VERIFICAR A FORÇA DA SENHA
function passwordStrength(password,username) {
		var score = 0
		var password;
		//password < 4
		if (password.length < 4 ) { resultado = 0 }
	
		//password == username
		if (password.toLowerCase()==username.toLowerCase()){ resultado = 1 }
	
		//password length
		score += password.length * 4
		score += ( checkRepetition(1,password).length - password.length ) * 1
		score += ( checkRepetition(2,password).length - password.length ) * 1
		score += ( checkRepetition(3,password).length - password.length ) * 1
		score += ( checkRepetition(4,password).length - password.length ) * 1
	
		//password has 3 numbers
		if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5
	
		//password has 2 sybols
		if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5
	
		//password has Upper and Lower chars
		if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10
	
		//password has number and chars
		if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15
		//
		//password has number and symbol
		if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15
	
		//password has char and symbol
		if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15
	
		//password is just a nubers or chars
		if (password.match(/^w+$/) || password.match(/^d+$/) )  score -= 10
	
		//verifing 0 < score < 100
		if ( score < 0 )  score = 0
		if ( score > 100 )  score = 100
	
		if (score > 4 && score < 34 ) { resultado = 2 }
		if (score < 68 && score > 34 ) { resultado = 3 }
		if (score > 68 ) { resultado = 4 }

		if(resultado == 0){
			saida = 'A Segurança de sua senha é: <font color=\'#A04040\'>CURTA</font>';
		} else if(resultado == 1){ 
                saida = 'A Segurança de sua senha é: <font color=\'#A04040\'>MUITO BAIXA</font>';
        } else if(resultado == 2){
                saida = 'A Segurança de sua senha é: <font color=\'#FF5F55\'>BAIXA</font>';
        } else if(resultado == 3){
                saida = 'A Segurança de sua senha é: <font color=\'#7F7FFF\'>BOM</font>';
        } else if(resultado == 4){
                saida = 'A Segurança de sua senha é: <font color=\'#99C55D\'>EXCELENTE</font>';
        }
        
        document.getElementById('Relevancia').innerHTML = saida;
        resultado = null;
		
		return;
}

function checkRepetition(pLen,str) {
	res = ""
	for ( i=0; i<str.length; i++ ) {
		repeated=true
		for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
			repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
		if (j<pLen) repeated=false
		if (repeated) {
			i+=pLen-1
			repeated=false
		}
		else {
			res+=str.charAt(i)
		}
	}
	return res
}

$ = function(elem){
	if(document.getElementById) return document.getElementById(elem);
	else alert("Erro ao capturar elemento via id!");
}

function setCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}

function delCookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function setPreview()
{
	setCookie( 'DataPreview', $('Editor').value , '', '/', '', '' );
	window.open('Preview.php', 'Preview', "status=no, scrollbars=yes, width=860, height=600")
}
