var ns4 = (document.layers)? true:false;         //NS 4
var ie4 = (document.all)? true:false;         //IE 4
var dom = (document.getElementById)? true:false;   //NS 6 ou IE 5

function reload_page(pUrl,pParent) {
	var doc;
	if (!pParent) pParent=false;
	doc=document;
	if (pParent) doc=parent.document;
	if (!pUrl) pUrl='';
	if (pUrl=='') {
		doc.location.replace(window.location.href);
	} else {
		doc.location.replace(pUrl);
	}
}
function verificar_logado(url) {
	if (!url) {
		url=document.location.href;
	}
	processa.location.replace('verificar_logado.asp?_url=' + url);
}

function makeRequest(ID, url, centralizar) {
	DoLoading(ID, centralizar);
	var myAjax=new Ajax.Updater(ID, url, {
										onFailure: function() {
																										ID.innerHTML='<DIV><CENTER><br>A busca não obteve resposta, tente novamente mais tarde.<br><br></CENTER></DIV>';
																										}
										});
}

function makeRequestEditor(url,oEditor) {
	
	var httpRequest;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.ServerXMLHTTP.6.0");
		}
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Msxml2.ServerXMLHTTP.6.0");
			}
			catch (e) {}
		}
	}

	if (!httpRequest) {
		return 'Cannot use Ajax call, try update/enable it.';
	}
	httpRequest.onreadystatechange = function() {
														if (httpRequest.readyState == 4) {
															if (httpRequest.status == 200) {
																oEditor.SetHTML(httpRequest.responseText);
															} else {
																oEditor.SetHTML('<DIV><CENTER><br>A busca não obteve resposta, tente novamente mais tarde.<br><br></CENTER></DIV>');
															}
														}
										};
	httpRequest.open('GET', url, true);
	httpRequest.send('');

}

function UpdateDiv(ID, httpRequest, centralizar) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			SetDiv(ID, httpRequest.responseText);
		} else {
			SetDiv(ID, '<DIV><CENTER><br>A busca não obteve resposta, tente novamente mais tarde.<br><br></CENTER></DIV>');
		}
		if (centralizar==true) {
			centralizarDiv(document.getElementById(ID));
		}
	}
}

function DoLoading(ID, centralizar) {
	SetDiv(ID,'<DIV style=width=100%;><CENTER><img src=\'ajax-loader.gif\'></CENTER></DIV>');
	if (centralizar==true) {
		centralizarDiv(document.getElementById(ID));
	}
	return true;
}

function SetDiv(ID,Content) {
  if (dom) {
  document.getElementById(ID).innerHTML = Content;
	 return;
  }
  if (ie4) {
	  document.all[ID].innerHTML = Content;
	 return;
  }
  if (ns4) {
	  with (eval('document.'+ID+'.document')) {
		 open();
		 write(Content);
		 close();
	 }
	 return;
  }
}

function validate() {
	var searchform = document.searchform;
	request = 'do=search&';
	request += 'search=' + searchform.search.value + '&';
	request += 'cat=' + searchform.cat.options[searchform.cat.selectedIndex].value + '&';
	request += 'incldead=' + searchform.incldead.options[searchform.incldead.selectedIndex].value + '&';
	request += 'free=' + searchform.free.options[searchform.free.selectedIndex].value + '&';
	
	request += 'lang=' + searchform.lang.options[searchform.lang.selectedIndex].value ;
	makeRequest('torrents-ajax.php?'+request);
	return false;
}    


function mostrarDisplay(url, centralizar) {
		document.getElementById("lockPagina").style.display = 'block';
		document.getElementById("Display").style.display = 'block';
		document.getElementById("lockPagina").style.height = PageSize() + 'px';
		if (centralizar) {
			centralizarDiv(gE('Display'));
		}
		if (! url=='') {
			makeRequest('DisplayBody', url, false );
		}
}

function PageSize(){
	var yScroll;
	if (window.innerHeight && window.scrollMaxY) {
			 yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			 yScroll = document.body.scrollHeight;
	} else {
			 yScroll = document.body.offsetHeight;
	}
	var windowHeight;
	if (self.innerHeight) {
			windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
			windowHeight = document.body.clientHeight;
	}
	if(yScroll < windowHeight){
			pageHeight = windowHeight;
	} else {
			pageHeight = yScroll;
		}
		return pageHeight;
}        

function mostraAlertMessage() {
		document.getElementById("Display").style.display = 'none';
		document.getElementById("lockPagina").style.display = 'block';
		document.getElementById("alertMessage").style.display = 'block';
		document.getElementById("lockPagina").style.height = PageSize() + 'px';        
}

function fecharDisplay() {
		gE('lockPagina').style.display = 'none';
		//new Effect.Puff('Display');
		gE('Display').style.display = 'none';
}

function centralizarDiv(objdiv){
	var scrollXY = getScrollXY();
	var windowXY = getWindowXY();
	
	var posTop = 0;
	var posLeft = 0;
	posTop = parseInt((windowXY[1]-objdiv.offsetHeight)/2);
	posLeft = parseInt((windowXY[0]-objdiv.offsetWidth)/2);
	//posTop = parseInt((windowXY[1]-objdiv.offsetHeight)/2)+scrollXY[1]; somente quando position <> fixed
	//posLeft = parseInt((windowXY[0]-objdiv.offsetWidth)/2)+scrollXY[0];
	if (posTop < 0) posTop=0;
	if (posLeft < 0) posLeft=0;
	objdiv.style.top = posTop + 'px';
	//objdiv.style.left = posLeft + 'px';
	objdiv.style.display='block';
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowXY() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
	return [ myWidth, myHeight ];
}
function preloadImage(file) {
	if (document.images) {
	    img1 = new Image();
  	  img1.src = file;
	}
}
function gE(id) {
	return document.getElementById(id);
}
function fLockPagina() {
		$("lockPagina").style.display = 'block';
		$("lockPagina").style.height = PageSize() + 'px';
}

function confirm_action(obj, mensagem, exibirLoading) {
	var html=new Element('div', { 'id' : 'lockP', 'class' : 'lockPagina' } )
	html.setStyle({ 'display' : 'block', 'height' : document.viewport.getHeight() + 'px' });
	
	var target=obj.target;
	var _onclick='';
	//if (exibirLoading) {
		_onclick=' onClick="$(\'div_conf\').insert(\'<DIV id=div_loading></DIV>\'); return DoLoading(\'div_loading\',true);"'
	//}
	mensagem+=' <BR>?<BR><BR><a href="' + obj.href + '" target="' +target + '"' + _onclick + '><img src="/images/bt_confirmar.png"></a>&nbsp;&nbsp;&nbsp;&nbsp;'
	mensagem+='<a href="" onClick="cancel_confirm(); return false;"><img src="/images/bt_cancelar.png"></a>'
	
	var div_conf=new Element('div', { id: 'div_conf', 'class' : 'div-confirma' }).insert(mensagem);
	document.body.appendChild(html);
	document.body.appendChild(div_conf);

	// retrieve required dimensions
	var eltDims     = div_conf.getDimensions();
	var browserDims = document.viewport.getDimensions();
	 
	// calculate the center of the page using the browser and element dimensions
	var y  = (browserDims.height - eltDims.height) / 2;
	var x = (browserDims.width - eltDims.width) / 2;
	y+=document.viewport.getScrollOffsets()[1]
	 
	// set the style of the element so it is centered
	var styles = { position : 'absolute',
		top      : y + 'px',
		left     : x + 'px' };
	 
	div_conf.setStyle(styles);
	
	return false;

}

function cancel_confirm() {
	$('div_conf').remove();
	$('lockP').remove();
}


