
function ValidateString(theField, checkOK)
{
  var checkStr = theField.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
	sMsg = "debe poseer caracteres válidos";
    return (false);
  }
  else
  {
    return (true);
  }
}


// valida cadenas vacias 
String.prototype.trim = _String_trim;

function _String_trim(){
 var tstr = this.toString();

 while(tstr.charAt(0) == " ")
 tstr = tstr.substring(1);

 while(tstr.charAt(tstr.length - 1) == " ")
 tstr = tstr.substring(0,tstr.length-2);
 
 return tstr;
}

// valida mail
var sMsg       = ""
var sAlpha     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_ ";
var sAlphaExt  = "ÁÉÍÓÚÑÄËÏÖÜÂÊÎÔÛ áéíóúñäëïöüâêîôû";
var sAlphaMail = "@.-_";
var sNumeric   = "1234567890";
var sDot       = ".";
var sHyphen	   = "-";
var sAlphaOk = "@.-_,:;$!()|?°";

function ValidateEmail(theField){
var checkOK = sAlpha + sNumeric + sAlphaMail;
var checkStr = theField.value;
var allValid = true;

for (i = 0;  i < checkStr.length;  i++)
{
  ch = checkStr.charAt(i);
  for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	  break;
  if (j == checkOK.length)
  {
	allValid = false;
	break;
  }
}

if (!allValid)
{
 sMsg = " contiene caracteres inválidos.";
  return (false);
}
else
{
  return (true);
}
}

function ValidarLogin(datos)
    {
    
	if(datos.name.value=="")
		{
		jAlert("Ingrese su nombre de usuario o su direccion de correo","Aviso");
		datos.name.focus();
		return false;
		}
	if(datos.pass.value=="")
		{
		jAlert("Ingrese su clave de usuario","Aviso");
		datos.pass.focus();
		return false;
		}
	  if (ValidateString(datos.pass, sAlphaExt+sAlpha+sNumeric)== false) {
           datos.pass.focus();
           jAlert("La clave ingresada contiene caracteres incorrectos.","Aviso");
           return false;
      } 
	

	return true;

}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}


function str_replace(search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

/*function TweetThis(url){
  var ret = "";
  $.ajax({
    url: "http://tinyurl.com/api-create.php?url=" + url,
    cache: false,
    success: function(data){
      ret = data;
    }
  });
  return ret;
}*/

function showdiv(url,titulo,hack,nid){
  if(hack=='top'){
    document.getElementById('flotante'+nid+hack).style.display='block';
  } else if(hack=='bottom'){
    document.getElementById('flotante'+nid+hack).style.display='block';
  } else if(hack=='listado'){
    document.getElementById('flotante'+nid+hack).style.display='block';
  } else {
    document.getElementById('flotante'+nid).style.display='block';
  }
  
  document.getElementById('nodo_url'+nid+hack).value=str_replace("'","´",url);
  document.getElementById('nodo_titulo'+nid+hack).value=str_replace("'","´",titulo);
  document.getElementById('delicious'+nid+hack).href="http://del.icio.us/post?url="+url+"&title="+titulo;
  document.getElementById('facebook'+nid+hack).href="http://www.facebook.com/share.php?u="+url;
  document.getElementById('digg'+nid+hack).href="http://www.digg.com/submit?url="+url;
  document.getElementById('reddit'+nid+hack).href="http://reddit.com/submit?url="+url+"&title="+titulo;
  document.getElementById('google'+nid+hack).href="http://www.google.com/bookmarks/mark?op=edit&bkmk="+url+"&title="+titulo;
  document.getElementById('twitter'+nid+hack).href="http://twitter.com/home?status=Leyendo "+url+" en "+window.location.hostname;
  document.getElementById('meneame'+nid+hack).href="http://meneame.net/login.php?url="+url+"&title="+titulo;
  //document.getElementById('wikio'+nid+hack).href="http://www.wikio.es/vote?url="+url;
  document.getElementById('misterWong'+nid+hack).href="http://www.mister-wong.es/index.php?action=addurl&bm_url="+url+"&bm_description="+titulo;
  document.getElementById('linkedIn'+nid+hack).href="http://www.linkedin.com/shareArticle?mini=true&url="+url+"&title="+titulo+"&summary="+titulo;
  document.getElementById('mySpace'+nid+hack).href="http://www.myspace.com/Modules/PostTo/Pages/?u="+url+"&t="+titulo;
  document.getElementById('friendFeed'+nid+hack).href="http://www.friendfeed.com/share?title="+titulo+"&link="+url;
  document.getElementById('stumbleupon'+nid+hack).href="http://www.stumbleupon.com/submit?url="+url+"&title="+titulo;
  document.getElementById('technorati'+nid+hack).href="http://technorati.com/faves?add="+url;
  document.getElementById('googleBookmarks'+nid+hack).href="http://www.google.com/bookmarks/mark?op=edit&bkmk="+url+"&title="+titulo+"&annotation="+titulo;
  document.getElementById('myYahoo'+nid+hack).href="http://buzz.yahoo.com/submit/?submitUrl="+url+"&submitHeadline="+titulo+"&submitSummary="+titulo+"&submitCategory=science&submitAssetType=text";
  document.getElementById('bebo'+nid+hack).href="http://www.bebo.com/c/share?Url="+url+"&Title="+titulo;
  
  return;
}

function hidediv(nid,hack) {
	document.getElementById('flotante'+nid+hack).style.display='none';
}

//div en posicion del mouse
function showdivEnviar(nid,hack) {
	if(hack=='top'){
		document.getElementById('flotante2'+nid+hack).style.display='block';
	} else if(hack=='bottom'){
		document.getElementById('flotante2'+nid+hack).style.display='block';
	} else if(hack=='listado'){
		document.getElementById('flotante2'+nid+hack).style.display='block';
	} else {
		document.getElementById('flotante2'+nid).style.display='block';
	}
	document.getElementById('flotante2'+nid+hack).style.display='block';
	document.getElementById('nodo_id'+nid+hack).value=nid;
	document.getElementById('edit-path'+nid+hack).value="node/"+nid;
    	
	return;
}

function hidedivEnviar(nid,hack) {
	document.getElementById('flotante2'+nid+hack).style.display='none';
}


function urlencode (str) {
    // URL-encodes string  
    // 
    // version: 910.813
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    var hexStr = function (dec) {
        return '%' + (dec < 16 ? '0' : '') + dec.toString(16).toUpperCase();
    };

    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters); https://developer.mozilla.org/index.php?title=en/Core_JavaScript_1.5_Reference/Global_Objects/String/charCodeAt
                ret += ((code - 0xD800) * 0x400) + (str.charCodeAt(i+1) - 0xDC00) + 0x10000;
                i++; // skip the next one as we just retrieved it as a low surrogate
            }
            // We never come across a low surrogate because we skip them, unless invalid
            // Reserved assumed to be in UTF-8, as in PHP
            else if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048) { // 3 bytes (code < 65536)
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

function urldecode (str) {
    // Decodes URL-encoded string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/urldecode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function (search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}

/* para simular un sleep, para poner en espera script */
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

// Read a page's GET URL variables and return them as an associative array.
//http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        if (vars[hash[0]] == undefined) {
            vars[hash[0]] = hash[1];
        }
        else {
            vars.splice(vars.length);
        }
    }
    return vars;
}
