// JavaScript Document
isInteger = function(s) {
    if (s == "")
        return;
    for (i = 0; i < s.length; i++) {
        if ((s.charAt(i) < '0') || (s.charAt(i) > '9'))
            return false
    }
    return true;

}
trim = function(str) {
    var trimmed = str.replace(/^\s+|\s+$/g, '');
    return trimmed;
}


function get_url_arg(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


/*
Funciones para Formatear numeros
Implementada en Enero del 2004
*/
function format_number(pnumber, decimals) {
    if (isNaN(pnumber)) {
        return 0
    };
    if (pnumber == '') {
        return 0
    };

    var IsNegative = (parseInt(pnumber) < 0);
    if (IsNegative)
        pnumber = -pnumber;

    var snum = new String(pnumber);
    var sec = snum.split('.');
    var whole = parseInt(sec[0]);
    var result = '';
    if (sec.length > 1) {
        var dec = new String(sec[1]);
        dec = parseInt(dec) / Math.pow(10, parseInt(dec.length - decimals - 1));
        Math.round(dec);
        dec = parseInt(dec) / 10;

        if (IsNegative) {
            var x = 0 - dec;
            x = Math.round(x);
            dec = -x;
        } else {
            dec = Math.round(dec);
        }

        /* 
 * If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
 * then we need to add 1 to the 'whole' and set the dec to 0. 
 */
        if (dec == Math.pow(10, parseInt(decimals))) {
            whole += 1;
            dec = "0";
        }

        dec = String(whole) + "." + String(dec);
        var dot = dec.indexOf('.');
        if (dot == -1) {
            dec += '.';
            dot = dec.indexOf('.');
        }
        var l = parseInt(dot) + parseInt(decimals);
        while (dec.length <= l) {
            dec += '0';
        }
        result = dec;
    } else {
        var dot;
        var dec = new String(whole);
        dec += '.';
        dot = dec.indexOf('.');
        var l = parseInt(dot) + parseInt(decimals);
        while (dec.length <= l) {
            dec += '0';
        }
        result = dec;
    }
    if (IsNegative)
        result = "-" + result;
    return result;
}



function get_hash(string_length) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	if(!string_length){var string_length = 8;}
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

  function AddItem(Text,Value, List)
    {
        // Create an Option object        

        var opt = document.createElement("option");

        // Add an Option object to Drop Down/List Box
        $(List).options.add(opt);

        // Assign text and value to Option object
        opt.text = Text;
        opt.value = Value;
		opt.id = randomString();
		
    }
	

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function json_populate(data, select_id, val, opt){
	                var json = eval('(' + data + ')');
                    var optionsValues = '';
                    for (i in json) {
                  optionsValues += '<option value="' + json[i][val] + '">Silo #' + json[i][opt] + ' ' + json[i]['ArticleNo'] + '</option>';
                        //alert(json[i]['IDrawsilo'] );
                    }

                    //optionsValues += '</select>';
                    optionsValues.replace(/null/i, ' ');
                    //var options = $('#'+select_id);
					$('#'+select_id).html(optionsValues);
                    //options.replaceWith(optionsValues);
}

function get_highestZindex(obj){
   var highestIndex = 0;
   var currentIndex = 0;
   var elArray = Array();
   if(obj){ elArray = obj.getElementsByTagName('*'); }else{ elArray = document.getElementsByTagName('*'); }
   for(var i=0; i < elArray.length; i++){
      if (elArray[i].currentStyle){
         currentIndex = parseFloat(elArray[i].currentStyle['zIndex']);
      }else if(window.getComputedStyle){
         currentIndex = parseFloat(document.defaultView.getComputedStyle(elArray[i],null).getPropertyValue('z-index'));
      }
      if(!isNaN(currentIndex) && currentIndex > highestIndex){ highestIndex = currentIndex; }
   }
   return(highestIndex+1);
}
