function byteSplit(str){
	var buf = [];
	for(var pos = 0;pos < str.length;pos++){
		var c = str.charCodeAt(pos);
		if(c > 255){
			var bytes = encodeURI(str).replace(/%/g,"");
			for(var i=0;i < bytes.length;i+=2){
				buf.push(String.fromCharCode(
					parseInt(bytes.charAt(i)+bytes.charAt(i+1),16)
				))
			}
		}else{
			buf.push(str.charAt(pos))
		}
	}
	return buf.join("")
}
String.prototype.element = function(){
	return document.getElementById(this)
}
String.prototype.x = function (l) {
    for(var i=0,tmp=[];i<l;tmp[i++]=this);
    return tmp.join("")
}
String.prototype.chars = function(){
	return this.split("")
}
Function.prototype.ruby_slice_support = function(){
	var body = this.getbody();
	var m = body.replace(/([\w\]])\[\(*([^\[]*?),([^\[]*?)\)*\]/g,
		function($0,$1,$2,$3){
			return [$1,".ruby_slice(",$2,",",$3,")"].join("")
		}
	).replace(/(\w+)\.ruby_slice\((.*?)\)\s*=\s*(.*?)[;\n]/g,
		function($0,$1,$2,$3){
			return [$1,"=",$1,".ruby_slice(",$2,").eq(",$3,");\n"].join("")
		}
	);
	new_func = new Function(this.getargs(),m);
	return new_func;
}
Function.prototype.forcePrivate = function(){
	var body = this.getbody();
	var list = [];
	var m = body.replace(/[^\.\w](\w+)(?:[ ]*=)/g,function(){
		list.push(arguments[1])
	});
	if(list.length == 0){return this}
	var priv = "var " + list.uniq() + ";\n";
	new_func = new Function(
		this.getargs(), priv + body
	);
	return new_func;
}
Object.prototype.to_s = function(){
	return this.toString()
}
chr = String.fromCharCode;
Number.prototype.chr = function(){
	return String.fromCharCode(this)
}
Number.prototype.to_int = Number.prototype.to_i = function(){
	return Math.floor(this)
}
Number.prototype.to_s = function(){
	return this.toString()
}

Number.prototype.abs = function(){
	return Math.abs(this)
}
String.prototype.to_i = function(){
	return parseInt(this)
}
String.prototype.to_int = String.prototype.to_i;


String.prototype.ruby_pos = function(){
	for(var i=0;i<this.length;i++){this[i] = this.charCodeAt(i)};
	alert(this[0]);
	return this;
}
String.prototype.ruby_slice = function(offset,length){
	var self = this;
	var str = new String(this.substr(offset,length));
	str.eq  = function(rval){
		return self.substr(0,offset) + rval + self.substr(offset+length);
	}
	return str;
}
String.prototype.pack = function(){
	var tmp = []
	for(var i=0;i<this.length;i+=2){
		tmp.push(
			String.fromCharCode(
				parseInt(this.charAt(i)+this.charAt(i+1),16)
			)
		)
	}
	return tmp.join("");
}
String.prototype.unpack = function(format){
	var str = this;
	if(format == "C"){
		var res = [];
		for(var i=0;i<str.length;i++) res[i] = str.charCodeAt(i);
		return res;
	}
	if(format == "C*"){
		var res = [];
		for(var i=0;i<str.length;i++) res[i] = str.charCodeAt(i);
		return res;
	}
}
String.prototype.each_byte = function(func){
	var bytes = this.unpack("C*");
	for(var i=0;i<bytes.length;i++) func(bytes[i])
}

function Range(start,end,exclude_end){
	this.start = start;
	this.end   = end;
	this.ex_end = exclude_end ? 1 : 0;
}
Range.prototype.collect = function(func){
	var res = [];
	for(var i=this.start;i<=this.end-this.ex_end;i++){
		res.push(func())
	}
	return res;
}
Range.prototype.map = Range.prototype.collect;
Array.prototype.fill = function(v){
	for(var i=0;i<this.length;this[i++]=v);
	return this
}
Regexp = RegExp;
Regexp.compile = function(){
	return RegExp.call(null,arguments);
}
RegExp.prototype.match = function(str) {
  var matches
  if (matches = this.exec(str)) {
    var pos = str.search(this)
    return(new MatchData(matches, str, pos))
  }
}

function MatchData(matches, str, pos) {
  this.matches = matches, this.string = str
  this.begin = this.position = pos
  this.match = matches[0]
  this.captures = matches.slice(1)
  this.end = pos + this.match.length
  this.length = matches.length
  this.preMatch = str.substr(0, pos)
  this.postMatch = str.substr(this.end)
}
MatchData.prototype.toString = function() { return(this.match) }
MatchData.prototype.toArray = function() { return(this.matches) }
String.prototype.scan = function(pattern) {
  var str = this, result = [], oldPos = -1, match, offset = 0
  while (match = pattern.match(str)) {
    if (match.end == match.begin)
      throw("Can't have null length matches with scan()")
    var newMatch = new MatchData(match.matches, match.string, match.position + offset)
    result.push(newMatch)
    str = match.postMatch
    offset += match.toString().length
  }
  return(result)
}
String.prototype.count = function(s){
	var c = 0;
	var offset = 0;
	while(1){
		offset = this.indexOf(s,offset);
		if(offset != -1){c++;offset++}else{break}
	}
	return c
}
function makeqr(src){
	setTimeout("_makeqr(\"" + src + "\")",100);
/*
try{
	var qr = new Qrcode;
	var res = qr.make_qrcode(byteSplit(src));
	return res.split(/\r\n|\r|\n/).join(",");
}catch(e){
	return "";
}*/
}
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}
function _makeqr(src){
var result = "";
try{
	var qr = new Qrcode;
	var res = qr.make_qrcode(byteSplit(src));
	result= res.split(/\r\n|\r|\n/).join(",");
}catch(e){
	result= "";
}

 thisMovie("qrbots").QRCodeGenerated(result);
}


Qrcode.prototype.string_bit_cal = Qrcode.prototype.string_bit_cal.ruby_slice_support();
"string_bit_cal,string_bit_not,set_qrcode_version,set_qrcode_error_correct,get_qrcode_version,set_structureappend,cal_structureappend_parity".split(",").forEach(function(v){
	Qrcode.prototype[v] = Qrcode.prototype[v].forcePrivate()
})
