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 makeRequest(ID, url, centralizar) {
	DoLoading(ID, centralizar);
	
	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.XMLHTTP");
		}
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}

	if (!httpRequest) {
		SetDiv(ID, 'Cannot use Ajax call, try update/enable it.');
		return false;
	}
	httpRequest.onreadystatechange = function() { UpdateDiv(ID, httpRequest, centralizar); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');

}

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><BR><img src=ajax-loader.gif><BR>carregando...<BR><BR></center></DIV>');
	if (centralizar==true) {
		centralizarDiv(document.getElementById(ID));
	}
}

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 = getPageSize() + 'px';
		if (centralizar) {
			centralizarDiv(gE('Display'));
		}
		makeRequest('DisplayBody', url, false );
		$('Display').absolutize()
}

function getPageSize(){
	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 = getPageSize() + '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)+scrollXY[1];
	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';
}

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);
}
