

// Test in IE and Firefox
function getUrl(url){
	document.location.href = url;
}

function getUrlAjax(url, target, post_data){

	var method = 'GET';
	if(typeof(post_data) != 'undefined') {
		method = 'POST';
	}

	if (window.XMLHttpRequest) {
	    xmlhttp = new XMLHttpRequest();
	} else {
    	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			setHtml(target, xmlhttp.responseText);
		}
	}

	try {
		xmlhttp.open(method, url,true);
	}
	catch(e) {
		alert('message:'+e.message+":url:"+url);
	}
	if (method == 'POST') {
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}

	xmlhttp.send(post_data);
}

function setHtml(target, html){
	if (target != ''){
		document.getElementById(target).innerHTML = html;
	}
}

function moveHtml(source, target){
	var html = document.getElementById(source).innerHTML;
	setHtml(target, html);
}

function cloneContent(source, target){
	var html = document.getElementById(source).innerHTML;
	// alert(html);
	document.getElementById(target).innerHTML = document.getElementById(target).innerHTML + html;
	
}

function setStyle(target, attribute, value){
	document.getElementById(target).style[attribute] = value;
}

function setClass(target, clss){
	document.getElementById(target).className = clss;
}

function confirmar(url, mensaje){
	if (confirm(mensaje))
	{
		getUrl(url);
	}
}

function refresh(){
	document.location.href=document.location.href;
}

function testHtmlIn(sourceValue, targetTag){
	var valor = document.getElementById(sourceValue).value;
	document.getElementById(targetTag).innerHTML = valor;
}

function setValueFromValues(source1, source2, target){
	var val1 = document.getElementById(source1).value;
	var val2 = document.getElementById(source2).value;
	// hack medio trucho...
	if (val2.length == 1) val2 = '0' + val2;
	document.getElementById(target).value = val1 + val2;
	// podria implementar una con una cadena separadora.
}

function setValueFrom3Values(source1, source2, source3, target){
	var val1 = document.getElementById(source1).value;
	var val2 = document.getElementById(source2).value;
	var val3 = document.getElementById(source3).value;
	// hack medio trucho...
	if (val2.length == 1) val2 = '0' + val2;
	if (val3.length == 1) val3 = '0' + val3;
	document.getElementById(target).value = val1 + val2 + val3;
	// podria implementar una con una cadena separadora.
}

function setValueFromValue(source, target){
	var val1 = document.getElementById(source).value;
	document.getElementById(target).value = val1;
}

function nextPage(url, variable, value){
	var n = 0;
	if (value != '')
	{
		n = parseInt(value);
	}
	getUrl(url + '&' + variable + '=' + (n + 1));
}

function previousPage(url, variable, value){
	var n = 0;
	if (value != '')
	{
		n = parseInt(value);
	}
	getUrl(url + '&' + variable + '=' + (n - 1));
}

function openPopup(url, width, height){
	window.open(url, 'popup', 'width='+width+',height='+height+',left=100,top=100,directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes');
}

function sortRowsById(tableId){
	
	// Obtengo un array con todos los rows
	var table = document.getElementById(tableId);
	var rws = table.rows;
	var rows = new Array();
	var aux;
	
	for (var i = 0; i < rws.length; i++){
		rows[i] = rws[i];
	}
	
	for (var j = 1; j < rows.length; j++){
	for (var i = 1; i < rows.length; i++){
		// swapeo como loco
		if (rows[i].id != '' && rows[i-1].id != '' && rows[i].id > rows[i-1].id){
			aux = rows[i];
			rows[i] = rows[i-1];
			rows[i-1] = aux;
		}
	}
	}
	
	aux = '';
	for (var i = 0; i < rows.length; i++){
		aux = aux + '<tr>' + rows[i].innerHTML + '</tr>';
	}
	
	table.innerHTML = aux;
	
	
}

function writeSwf(file, id, width, height, color, vars){

  var t = swfCode(file, id, width, height, color, vars);
	document.write(t);

}

function putSwf(file, id, width, height, color, vars, tag_id){
	
	var t = swfCode(file, id, width, height, color, vars);
	setHtml(tag_id, t);
	
}

function swfCode(file, id, width, height, color, vars){
	var t = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="'+id+'" align="middle">';
  t = t + '<param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="'+file+'" />';
  t = t + '<param name="FlashVars" value="'+ vars +'">';
  t = t + '<param name="quality" value="high" /><param name="bgcolor" value="#' + color + '" />';
  t = t + '<embed src="'+file+'" FlashVars="'+ vars +'" quality="high" bgcolor="#' + color + '" width="'+width+'" height="'+height+'" name="'+id+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
  t = t + '</object>';
  return t;
}

// Ideal para poner en los dropdown
function submitValue(URL, tag_id){
	document.location.href = URL + document.getElementById(tag_id).value;
}


// Verificar inputs:
function verify_not_null(tag_id, message, message_id){
	
	var valid = false;
	var value = document.getElementById(tag_id).value;
	
	if (value != ''){
		valid = true;
	}
	
	verify_one(valid, message, message_id);
	
	return valid;
	
}

function verify_email(tag_id, message, message_id){
	
	// Use regular expressions!
	var myRegxp = /^.*@.*$/;
	var value = document.getElementById(tag_id).value;
	var valid = myRegxp.test(value);
  
  verify_one(valid, message, message_id);
  
  return valid;
	
}

function verify_id(tag_id, message, message_id){

	
	// Use regular expressions!
	var myRegxp = /^\w\w*$/;
	var value = document.getElementById(tag_id).value;
	var valid = myRegxp.test(value);

	verify_one(valid, message, message_id);

  return valid;
	
}

function verify_password(tag1_id, tag2_id, message, message_id){
	
	var valid = verify_id(tag1_id, message, message_id);
	
	if (valid){
		
		var value1 = document.getElementById(tag1_id).value;
		var value2 = document.getElementById(tag2_id).value;
		
		valid = (value1 == value2);
		
		verify_one(valid, message, message_id);
		
	}
	
	return valid;
	
}

function verify_one(valid, message, message_id){
	
	var message_tag = document.getElementById(message_id);
	
	if (valid){
		message_tag.innerHTML = '';
	} else {
		message_tag.innerHTML = message;
	}
	
}

function verify_and_submit(message_array, form_id){
	
	var form = document.getElementById(form_id);
	var messages = document.getElementsByName(message_array);
	var valid = true;
	//var messages = document.getElementById(message_array);
	
	// Todos deberían estar vacíos
	// además los hago visibles
	for ( var i = 0; i < messages.length; i++){
		
		messages[i].style.display = "inline";
		
		if (messages[i].innerHTML != ''){
			valid = false;
		}
	}
	
	if (valid) form.submit();
	
}

function verIP(ip){
	
	var URL = 'http://www.infosniper.net/index.php?ip_address=' + ip + '&map_source=1&overview_map=1&lang=3&map_type=1&zoom_level=8';
	openPopup(URL, 900, 500);
	
}

function writeTextarea(name, content, style){
	document.write('<textarea name="' + name + '" style="'+ style +'">' + content + '</textarea>');
}