var xmlhttp=false;
var xmlhttpdos=false;
var xmlhttptres=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttpdos=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttpdos=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttpdos=false
  }
 }
@else
 xmlhttpdos=false
@end @*/

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttptres=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttptres=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttptres=false
  }
 }
@else
 xmlhttptres=false
@end @*/


if (!xmlhttpdos && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttpdos = new XMLHttpRequest();
 } catch (e) {
  xmlhttpdos=false
 }
}

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
if (!xmlhttptres && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttptres = new XMLHttpRequest();
 } catch (e) {
  xmlhttptres=false
 }
}
function markAllForRemove() {
	for (var i=0; i< document.comentarios.length; i++) {
		if (document.comentarios[i].name == "borrar_comentario[]") {
			document.comentarios[i].checked = true;
		}
	}
}
function markAllForApprove() {
	for (var i=0; i< document.comentarios.length; i++) {
		if (document.comentarios[i].name == "publicar_comentario[]") {
			document.comentarios[i].checked = true;
		}
	}
}

function actualizarCampo(id, texto) {
	if (document.getElementById) {
		obj = document.getElementById(id);
		obj.firstChild.data = texto;
	}
}


function toggle(id) {
	if (document.getElementById(id).style.display == 'block') {
		document.getElementById(id).style.display = 'none'
	} else {
		document.getElementById(id).style.display = 'block'
	}
}
function toggletags(id) {
	var d = document.getElementById(id);
		for (var i = 1; i<=10; i++) {
			if (document.getElementById('divtag'+i)) { document.getElementById('divtag'+i).style.display='none'; }
		}
	if (d) {d.style.display='block';}
}

function mostrar(id) {
	document.getElementById(id).style.display = 'block'
}
function borrarimg(nombre) {
	document.getElementById(nombre+'imgbg').value = '';
	document.getElementById('imgactual'+nombre).style.display = 'none'
}
function previsualizar(id1, id2){
 		var NewText = document.getElementById(id1).value;
 		splitText = NewText.split(/\n/).join("<br />");
 		var DivElement = document.getElementById(id2);
 		DivElement.innerHTML = splitText;
}
function addcat(lang) {
	  frm=document.getElementById('newpostform')
	  url="/"+lang+"/admin/nuevo/"+frm.elements['newcategory'].value+"/"+frm.elements['id_blog'].value
	  brandnew=frm.elements['newcategory'].value
	  frm.elements['newcategory'].value=''
	  xmlhttp.open("GET",url,true)
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText==0) {
		chkbx = '<div id="prohibido"><p>Category already exits!</p></div>'
		} else {
		chkbx = '<input type="checkbox" name="categ[]" id="b" value="'+xmlhttp.responseText+'" checked ><label for="b">'
		chkbx += brandnew + '</label> <br />'
		}
		document.getElementById('addcat').innerHTML+= chkbx
	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}


function uploadDelete(lang, id) {
//	  frm=document.getElementById('newpostform')
	  url="/"+lang+"/admin/upload/delete/"+id
	  xmlhttp.open("GET",url,true)
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText==1) {
	  		document.newpostform["imagename"].value= '';
			document.forms['asyncupload'].file_1.value = '';
	  		var form_upload = document.getElementById('form_upload');
	  		form_upload.style.display = 'block';
	  		form_upload.style.visibility = 'visible';
			var spanimage = document.getElementById('spanimage');
			var spanimageimg = spanimage.getElementsByTagName('img').item(0);
			spanimage.removeChild(spanimageimg);
			var deleteimage = document.getElementById('deleteimage');
			spanimage.removeChild(deleteimage);
		}

	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}


function cargando(id) {
	document.getElementById('compdis'+id).innerHTML = '';
}

function compdis(formulario, id, lang) {
	  document.getElementById('compdis'+id).innerHTML = '';
	  frm=document.getElementById(formulario)
	  if (id=="login") id2="login";
	  if (id=="nomblog") id2="nomblog";
	  if (id=="nombre") id2="nomblog";
	  url="/ajax/ajax.php?lang="+lang+"&id1=compdis&id2="+id2+"&id3="+frm.elements[id].value
	  xmlhttp.open("GET",url,true)
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		chkbx = xmlhttp.responseText;
		document.getElementById('compdis'+id2).innerHTML+= chkbx;
		document.getElementById('carg'+id2).style.display = 'none';
	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
		obj = document.getElementById(countfield);
        obj.childNodes[0].data = maxlimit - field.value.length;
	}
}
function peview_post(a) {
	var titulo = window.opener.document.newpostform.titulo.value;
	var texto = window.opener.document.newpostform.texto.value;
	document.getElementById('titulo').innerHTML+= titulo;
	document.form_text.text.value = texto;
	if (a) {
	document.form_text.submit();
	}
//	document.getElementById('texto').innerHTML+= texto;
}
function hide(idToHide) {
					document.getElementById(idToHide).style.display='none';
				}
function show(idToShow) {
					document.getElementById(idToShow).style.display='block';
				}


function trim(s) {
  while (s.length>0 && (s[0]==' '||s[0]=='\n')) s=s.substring(1, s.length);
  while (s.length>0 && (s[s.length-1]==' '||s[s.length-1]=='\n')) s=s.substring(0, s.length-1);
  return s; 
}

function cargandoInicio(id, imagen) {
  var elmP = document.getElementById(id);
  var elmIMG = document.createElement('img');
  elmIMG.src = '/imagenes/'+ imagen;
  elmP.appendChild(elmIMG);
}
function cargandoFin(id) {
	var elmP = document.getElementById(id);
	var image2 = elmP.getElementsByTagName('img')[0];
	if (image2) elmP.removeChild(image2);
}

function deleteTempImage(sid, lang) {
	  url="/"+lang+"/admin/upload/tmpthumb/delete/"+sid+"/";
	  xmlhttp.open("GET",url,true)
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText==1) {
			var spanimage = document.getElementById('spanimage');
			var spanimageimg = spanimage.getElementsByTagName('img').item(0);
			spanimage.removeChild(spanimageimg);
			var deleteimage = document.getElementById('deleteimage');
			spanimage.removeChild(deleteimage);
			var form_upload = document.getElementById('form_upload');
			document.forms['asyncupload'].file_1.value = '';
			form_upload.style.display = 'block';
		}
	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}
function createThumb(sid, lang) {
	  url="/"+lang+"/admin/upload/tmpthumb/addjpg/"+sid+"/";
	  xmlhttp.open("GET",url,true)
	  var spanimage = document.getElementById('spanimage');
	  spanimage.style.display = 'block';
	  cargandoInicio('spanimage', 'bigrotation2.gif');
	  xmlhttp.onreadystatechange=function() {
	   if (xmlhttp.readyState==4) {
		if (xmlhttp.responseText==1) {
			cargandoFin('spanimage');
			var image_new = document.createElement('img');
			image_new.src = '/tmp/'+ sid +'.jpg';
			image_new.className = 'loaded';
			spanimage.appendChild(image_new);
			chkbx = '<span id="deleteimage"><a href="#" onClick="deleteTempImage(\''+sid+'\', \''+lang+'\'); return false;"><img src="/imagenes/cross.png" alt="delete" /></a><span>'
			spanimage.innerHTML+= chkbx
		}
	   }
	  }
	  xmlhttp.setRequestHeader('Accept','message/x-formresult')
	  xmlhttp.send(null)
	  return false
}

    function geonamesQuery(lang) {	
      //ul.form.submit();
	var valor = document.getElementById('q').value;
	getGoogle(valor);
	url="/geturl.php?service=geonames&q="+ encodeURIComponent(valor) +"&maxRows=5&lang="+lang;
	  xmlhttptres.open("GET",url,true)
	  xmlhttptres.onreadystatechange=function() {
	   if (xmlhttptres.readyState==4) {
		if (xmlhttptres.status==200) {
			if (xmlhttptres.responseText=="") {
				var searchloc = document.getElementById('geonamesDiv');
				var geonames_data = xmlhttptres.responseText;
				searchloc.innerHTML = geonames_data;
//				addhtml = '';
//				var datos = eval("(" + geonames_data + ")");
//				for (var i = 0; i < datos['total']; i++) {
//					addhtml = '<a href="#" onClick="loadmap(' + datos['geonames'][i]['lat'] +',' + datos['geonames'][i]['lng'] + '); return false;">'+datos['geonames'][i]['name']+' - '+datos['geonames'][i]['countryName']+'</a><br />';
//					searchloc.innerHTML+= addhtml;
//				}
			}
		}
	   }
	  }
	  xmlhttptres.setRequestHeader('Accept','message/x-formresult')
	  xmlhttptres.send(null)
	  return false
    }

    function getNirudia(valor) {	
	 //url="http://nirudia.com/xml/geoname/"+valor+"/index.xml";
	 url="/xml/geoname/"+valor+"/index.xml";
	  if (xmlhttptres.open("GET",url,true)) {
	  xmlhttptres.onreadystatechange=function() {
	   if (xmlhttptres.readyState==4) {
		if (xmlhttptres.status==200) {
			var searchloc = document.getElementById('searchloc');
			var geonames_data = xmlhttptres.responseText;
			searchloc.innerHTML+= geonames_data;
		}
	   }
	  }
	  xmlhttptres.setRequestHeader('Accept','message/x-formresult')
	  xmlhttptres.send(null)
	  return false
	}
    }


function getGoogle(valor) {
  geocoder.getLatLng(
    valor,
    function(point) {
      if (!point) {
	getNirudia(valor);
      } else {
	loadmap();
        map.setCenter(point, 12, G_HYBRID_MAP);
//        var marker = new GMarker(point);
//        map.addOverlay(marker);
      }
    }
  );
}	

// this function will be called by our JSON callback
// the parameter jData will contain an array with geonames objects
function getLocation(jData) {
  if (jData == null) {
    // There was a problem parsing search results
    html = '<strong>No results<strong>';
	document.getElementById('resultDiv').innerHTML = html;
    return;
  }

  var html = '';
  var geonames = jData.geonames;
  for (i=0;i< geonames.length;i++) {

     var name = geonames[i];
	if (i==0) {
		loadmap(name.lat, name.lng);
	}
     // we create a simple html list with the geonames objects
     // the link will call the center() javascript method with lat/lng as parameter
     html = html + '<a href="#" onClick="loadmap(' + name.lat +',' + name.lng + '); return false;">' + name.name +' - '+ name.countryName + '</a><br>';
  }
  document.getElementById('resultDiv').innerHTML = html;
}


// calls the geonames JSON webservice with the search term
function search(lang) {
  request = 'http://ws.geonames.org/searchJSON?q=' +  encodeURIComponent(document.getElementById('q').value)  + '&maxRows=5&lang=' + lang + '&callback=getLocation';

  // Create a new script object
  // (implementation of this class is in /export/jsr_class.js)
  aObj = new JSONscriptRequest(request);
  // Build the script tag
  aObj.buildScriptTag();
  // Execute (add) the script tag
  aObj.addScriptTag();
}

function getval(id) {
	if (document.getElementById(id)) var resultadovalor = document.getElementById(id).value;
	else var resultadovalor = '';
	return resultadovalor;
}

function ocultar(id) {
	var ocultar = document.getElementById(id);
	ocultar.style.display = 'none';
}
function mostrar(id) {
	var mostrar = document.getElementById(id);
	mostrar.style.display = 'block';
}
function addclassname(id1, id2, clase) {
	document.getElementById(id1).className=clase;
	document.getElementById(id2).className='';
}