// JavaScript Document
function updateInterface(){
	updateBackground();
	updateBackground();	
}

function updateBackground(){
	/* Copiando */ 
	if (getStyle('leftbg', 'visibility') == 'hidden'){
		copyHeight('main', 'leftbg');
		copyWidth('left', 'leftbg');
	}

//	var height = pixels2Int(getStyle('main', 'height'));
//	var sectionHeight = calculateHeight('section');
	var teste = document.getElementById('main');
	var height = teste.offsetHeight;
	teste = document.getElementById('section');
	var sectionHeight = teste.offsetHeight;

	/* Posicionando */
	var target = document.getElementById('left');
	applyStyleProperty('leftbg', 'left', int2Pixels(findPosX(target)));
	applyStyleProperty('leftbg', 'top', int2Pixels(findPosY(target)));

	/* Corrigindo altura do main */
	if (getStyle('leftbg', 'visibility') == 'hidden'){
		applyStyleProperty('main', 'height', int2Pixels(height+sectionHeight));
	}
	
	applyStyleProperty('leftbg', 'visibility', 'visible');
	
	/* Background da seção */
	height = pixels2Int(copyHeight('section', 'sectionbg'));
	copyWidth('section', 'sectionbg');
	var newWidth = pixels2Int(getStyle('sectionbg', 'width')) + 10;
	
	applyStyleProperty('sectionbg', 'width', int2Pixels(newWidth));
	copyStyleProperty('section', 'sectionbg', 'padding');
	copyStyleProperty('section', 'sectionbg', 'margin');

	/* Posicionando */
	target = document.getElementById('section');
	var correction = 0;
	if (isIE()){
		correction = 2;
	}
	applyStyleProperty('sectionbg', 'left', int2Pixels(findPosX(target)+correction));
	applyStyleProperty('sectionbg', 'top', int2Pixels(findPosY(target)+correction));

	applyStyleProperty('sectionbg', 'visibility', 'visible');
	
	/* Posiciona L2 Logo */
	target = document.getElementById('sitebanner');
	applyStyleProperty('l2logo', 'left', int2Pixels(findPosX(target) + 15));

	var yPos = findPosY(target);
	var yDiff = (pixels2Int(getStyle('l2logo', 'height')) - pixels2Int(getStyle('sitebanner', 'height'))) / 2;
	yPos -= yDiff;

	if (isIE())
	{
		yPos += 3;
	}	

	applyStyleProperty('l2logo', 'top', int2Pixels(yPos));
	applyStyleProperty('l2logo', 'visibility', 'visible');
}

function clonePosition(strOriginal, strClone){
	var source = document.getElementById(strOriginal);
	var target = document.getElementById(strClone);

	applyStyleProperty(strClone, 'top', source.offsetTop + 'px');
//	target.offsetBottom = source.offsetBottom;
	applyStyleProperty(strClone, 'left', source.offsetLeft + 'px');
//	target.offsetRight = source.offsetRight;
}

function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function pixels2Int(strPixels){
	return parseInt(strPixels);
}

function int2Pixels(intValue){
	return intValue + "px";
}

function copyHeight(strFrom, strTo){
	var found = 0;
	
	if (!isIE()){
		found = copyStyleProperty(strFrom, strTo, 'height');
	} else {
		var source = document.getElementById(strFrom);
		applyStyleProperty(strTo, 'height', source.offsetHeight);
		found = source.offsetHeight;
	}
	
	return found;
}

function copyWidth(strFrom, strTo){
	var found = 0;

	if (!isIE()){
		found = copyStyleProperty(strFrom, strTo, 'width');
	} else {
		var source = document.getElementById(strFrom);
		applyStyleProperty(strTo, 'width', source.offsetWidth);
		found = source.offsetWidth;
	}
	
	return found;
}

function copyStyleProperty(strFrom, strTo, propertyName){
	var to = document.getElementById(strTo);
	if (propertyName != 'padding' && propertyName != 'margin'){
		var valueFrom = getStyle(strFrom, propertyName);
		applyStyleProperty(strTo, propertyName, valueFrom);
		return valueFrom;
	} else {
		var positions = new Array('top', 'bottom', 'left', 'right');
		for(var position in positions){
			var property = propertyName+"-"+positions[position];
			var valueFrom = getStyle(strFrom, property);
			applyStyleProperty(strTo, property, valueFrom);
		}
	}
}

function applyStyleProperty(strObj, propertyName, propertyValue){
	var target = document.getElementById(strObj);
	
	if (target.style.setProperty){
		target.style.setProperty(propertyName, propertyValue, null);
	} else {
		target.style[propertyName] = propertyValue;
	}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function calculateHeight(strObj){
	return pixels2Int(getStyle(strObj, 'height')) + pixels2Int(getStyle(strObj, 'padding-top')) + pixels2Int(getStyle(strObj, 'padding-bottom'));
}

function calculateWidth(strObj){
	return pixels2Int(getStyle(strObj, 'width')) + pixels2Int(getStyle(strObj, 'padding-left')) + pixels2Int(getStyle(strObj, 'padding-right'));
}

function isIE(){
	return (navigator.appName == 'Microsoft Internet Explorer');
}
 	
function isEmail(campo) 
{ 
	  return (campo.indexOf(".") > 2) && (campo.indexOf("@") > 0);
}

   
function isPassword(senha,confirmacao)
{
	if(senha.length < 4 || senha.length > 15) {
		alert("A senha deve ter entre 5 e 15 caracteres.");	
		return false;		
	} 
	else if (senha.indexOf("7")!=-1) {
		alert("A senha não pode conter o número 7!");
		return false;
	} 		
	else if (senha != confirmacao) {
		alert("Erro na confirmacao da senha, senha e confirmacao devem ser iguais!");
		return false;
	}
	return true;
}
    
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}
	
/*Em JavaScript, uma expresso regular  delimitada por uma barra / inicial 
e outra final. Cada expresso de validao sempre comea com ^ (incio de linha) 
e termina com $ (fim de linha), para garantir que abrange o contedo inteiro do 
texto (parmetro) a ser validado.
Existe uma seqncia de expresso regular que significa um dgito (0 a 9): \d.
Adicionando um + aps essa seqncia, significa "uma ou mais ocorrncias da seqncia precedente". 
Isto , um ou mais dgitos. Note que isso implica o preenchimento obrigatrio; 
 melhor que o caso do campo no preenchido (vazio) seja tratado  parte. 
 Assim, temos a expresso regular completa para validar um nmero apenas composto por dgitos:
*/
function isInteger(pVal)
{
	var reTipo = /^\d+$/;
	return reTipo.test(pVal);
}


function replaceAll(str,find,replace)
{
	var ret=str.replace(find,replace);
	while(ret!=str)
	{
		str=ret;
		ret=str.replace(find,replace)
	}
	return ret;
}

/*
 Padawan's JavaScript-Mega-Validator 3000+
 Todos os direitos reservados para Diego Pires Plentz
 Voc pode usar esse cdigo nas suas pginas desde que mantenha os crditos ;-)
 */
 function isDate(data,ano,mes,dia, opcional)
 {
	//Se o parmetro obrigatrio for igual  zero, significa que elepode estar vazio, caso contrrio, no
	if(dia+mes+ano != "")
	{
		//Verifica o ms
		if (!isInteger(mes) || mes > 12 || mes < 1)
		{
			if(isInteger(mes)  && opcional==1 && mes==0)
			{
				ano=0;
				dia=0;
				return true;
			}
			
			alert("Formato do mes invalido.");
			data.focus();
			return false
		}
		
		//converte para numero
		mes=parseInt(mes);
		
							
		//Verifica o dia
		if (!isInteger(dia) || dia > 31 || dia < 1)
		{
			if(isInteger(dia)  && opcional==1 && dia==0)
			{
				ano=0;
				mes=0;
				return true;
			}
			alert("Formato do dia nao valido.");
			data.focus();
			return false
		}
		
		//converte para numero
		dia=parseInt(dia);
		
		if (mes == 4 || mes == 6 || mes == 9 || mes == 11)
		{
			if (dia == 31)
			{
				alert("O mes informado nao possui 31 dias.");
				data.focus();
				return false
			}
		}
		if (mes == 2)
		{
			bissexto = ano % 4;
			if (bissexto == 0)
			{
				if (dia > 29)
				{
					alert("O mes informado possui somente 29 dias.");
					data.focus();
					return false
				}
			}
			else
			{
				if (dia > 28)
				{
					alert("O mes informado possui somente 28 dias.");
					data.focus();
					return false
				}
			}
		}

		//Verifica o ano
		if (!isInteger(ano))
		{
			alert("Formato do ano no  invalido.");
			data.focus();
			return false
		}
		else if(ano==0 && opcional==1)
		{
				dia=0;
				mes=0;
				return true;
		}
	}
	
	return true;
 }
 
function getElement(elementID)
{
	return  window.document.getElementById(elementID);
}

function getElementById(elementID)
{
	return getElement(elementID);
}

function getElementByID(elementID)
{
	return getElement(elementID);
}

function hide(elementID)
{
	var element  = getElementByID(elementID);
	element.style.display = "none";
}
