function getLen(str) {
     var len = 0;
     for (var i=0; i<str.length; i++) {
      if (str.charCodeAt(i) > 127)
       len += 2; //utf8格式下中文占3位，gb2312请修改位2位
      else 
       len++;
     }
     return len;
}

//function for roundedRectClass
function hex2num(hex){
	if(hex.charAt(0) == "#") hex = hex.slice(1);
	hex = hex.toUpperCase();
	var value = new Array(3);
	var k = 0;
	for(var i=0;i<6;i+=2){
		value[k]=parseInt("0x"+hex.substr(i,2),16);
		k++;
	};
	return value;
}
//function for roundedRectClass
function num2hex(triplet){
	var hex="#";
	var hR=triplet[0].toString(16);
	var hG=triplet[1].toString(16);
	var hB=triplet[2].toString(16);
	var value="#"+(triplet[0]<16?("0"+hR):hR)+(triplet[1]<16?("0"+hG):hG)+(triplet[2]<16?("0"+hB):hB)
	return value.toUpperCase();  
}
//class for rounded rectangle background
var roundedRectClass = new Class({
	Implements: Options,
	options:{
		canvas: 'cav',
		fillStyle: "#f4ffef",
		strokeStyle: "#43dd00",
		lineWidth: 4,
		radius: 5,
		useShadow: true,
		shadowColor: "#43dd00",
		shadowBlur: 5,
		zIndex: 10,
		contentPadding: 20
	},
	initialize: function(target, content, options){
		this.options = $extend(this.options, options);
		var offset = 2;
		target.setStyles({"z-index":this.options.zIndex, "position":"relative"});
		content.setStyles({"padding":this.options.contentPadding+"px"});
		var w = target.getStyle("width").toInt()-offset;
		var h = target.getStyle("height").toInt()-offset;
		var canvas = new Element("canvas", {
			"id" : "csv-" + canvas,
			"width": w,
			"height": h
		}).setStyles({"position":"absolute","top":"0px", "left":"0px", "z-index":-1}).inject(content, "before");
		if(Browser.Engine.trident)
			G_vmlCanvasManager.initElement(canvas);
		var ctx = canvas.getContext("2d");
		if(this.options.useShadow == true){
			for (var x = 0; x <= this.options.shadowBlur; x++){
				this.roundedRect(
					ctx,
					offset + x,
					offset + x,
					w - (x * 2) - offset,
					h - (x * 2) - offset,
					this.options.radius + (this.options.shadowBlur - x),
					hex2num(this.options.shadowColor),
					x == this.options.shadowBlur ? 1 : 0.06 + (x * 0.01)
				);
			}
		}else this.roundedRect(ctx, offset+this.options.lineWidth, offset+this.options.lineWidth, w-offset-this.options.lineWidth*2, h-offset-this.options.lineWidth*2, this.options.radius, hex2num(this.options.strokeStyle), 1000);
	},
	roundedRect: function(ctx, x, y, width, height, radius, rgb, a){
		if(a<1)ctx.fillStyle = 'rgba(' + rgb.join(',') + ',' + a + ')';
		else{
			ctx.fillStyle = this.options.fillStyle; 
			ctx.strokeStyle = this.options.strokeStyle;
			ctx.lineWidth = this.options.lineWidth;
			if(a==1000)ctx.strokeStyle = num2hex(rgb);
		}
		ctx.beginPath();
		ctx.moveTo(x, y + radius);
		ctx.lineTo(x, y + height - radius);
		ctx.quadraticCurveTo(x, y + height, x + radius, y + height);
		ctx.lineTo(x + width - radius, y + height);
		ctx.quadraticCurveTo(x + width, y + height, x + width, y + height - radius);
		ctx.lineTo(x + width, y + radius);
		ctx.quadraticCurveTo(x + width, y, x + width - radius, y);
		ctx.lineTo(x + radius, y);
		ctx.quadraticCurveTo(x, y, x, y + radius);
		ctx.fill(); 
		if(a==1||a==1000)ctx.stroke();
	}
});

function verifyMobile(mobile){
	var reg=/(^0{0,1}1[3|5|8][0-9]{9}$)/; 
	return reg.test(mobile);
}

var Base64j = {
 	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 		input = Base64j._utf8_encode(input);
 		while (i < input.length) {
 			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 			output = output+this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2)+this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 		}
 		return output;
	},
 	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 		while (i < input.length) {
 			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 		}
 		output = Base64j._utf8_decode(output);
 		return output;
 	},
 
	_utf8_encode : function (string) { 
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 		for (var n = 0; n < string.length; n++) {
 			var c = string.charCodeAt(n);
 			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 		while ( i < utftext.length ) {
 			c = utftext.charCodeAt(i);
 			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 		return string;
	}
}
function setClipboard(maintext, message) 
{
	if(!message||message==""){message="复制成功";}
    if (window.clipboardData) 
    {
    	alert(message);
        return (window.clipboardData.setData("Text", maintext));
    } 
    else 
    {
        if (window.netscape) 
        {
            try{
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
            if (!clip) 
            {
                return;
            }
            var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
            if (!trans) 
            {
                return;
            }
            trans.addDataFlavor("text/unicode");
            var str = new Object();
            var len = new Object();
            var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
            var copytext = maintext;
            str.data = copytext;
            trans.setTransferData("text/unicode", str, copytext.length * 2);
            var clipid = Components.interfaces.nsIClipboard;
            if (!clip) 
            {
                return false;
            }
            clip.setData(trans, null, clipid.kGlobalClipboard);
            alert(message);
            return true;
            }
            catch(e)
            {
                alert("您的firefox安全限制限制您进行剪贴板操作，请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试，相对路径为firefox根目录/greprefs/all.js");
                return false;
            }
        }
    }
    return false;
}
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9-_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString.substr(x, 1) == ' ') {
        //原文在此用 clearString[x] == ' ' 做判断, 但ie不支持把字符串当作数组来访问, 
        //修改后两种浏览器都可兼容 
        output += '+';
      }
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

var LazyLoad = new Class({
	Implements: [Options,Events],
	/* additional options */
	options: {
		range: 0,
		image: 'http://bangimg.oobang.com/images/common/hidden.gif',
		resetDimensions: true,
		elements: 'img[osrc]',
		container: window,
		fireScroll: true, /* keeping for legacy */
		mode: 'vertical',
		startPosition: 0,
		noImg:'no-image.jpg'
	},

	/* initialize */
	initialize: function(options) {
		/* vars */
		this.setOptions(options);
		this.container = document.id(this.options.container);
		this.elements = $$(this.options.elements);
		var axis = (this.options.mode == 'vertical' ? 'y': 'x');
		this.containerDimension = this.container.getSize()[axis];
		this.startPosition = 0;

		var offset = (this.container != window && this.container != document.body ? this.container : "");
 
		this.elements.set('src', this.options.image);  
		var action = function() {
			var cpos = this.container.getScroll()[axis];
			 
			if(cpos >= this.startPosition) {
				this.elements.each(function(item, index){
					if((cpos + this.options.range + this.containerDimension) >= item.getPosition(offset)[axis]) {
						var xa = item.getProperty("src");
						var xb = item.getProperty("osrc");
						if(!xa||xa==''||xa==this.options.image){
							item.addEvent('load', function(){  
								item.fade("in");
							});
							item.set('src', item.getProperty('osrc'));
							item.setStyle("opacity", 0); 	
							//this.fireEvent('load',[item]);
							return false;
						}						
					}
					return true;
				}, this);
				this.startPosition = cpos;
			}
			this.fireEvent('scroll'); 
			if(!this.elements.length) {
				this.container.removeEvent('scroll',action);
				this.fireEvent('complete');
			}
		}.bind(this);
 
		window.addEvent('scroll',action);
		if(this.options.fireScroll) { action(); }
	}
});
