﻿//*******************************************************
//														*
//		Javascript Framework for GR2M ver 3.7.0			*
//		Modify Date: 2008-04-03							*
//														*
//		(c) 2007 Jim Yuan <jim.yuan@g2.com>				*
//														*
//		GRMscript.js is freely for G2RM's project.		*
//		For details, contact me by my email.			*
//														*
//		Now at this version: 							*
//		It's based on prototype.js ver 1.6.0			*
//														*
//*******************************************************


//*******************Element extend method*******************//

Element.addMethods({
	absPosition: function(element, direction){
		element=$(element);
		element.setStyle('position:absolute;');
		var sh=element.getHeight();
		var sw=element.getWidth();

		var chgPositon=function(){
			var vh=document.viewport.getHeight();
			var vw=document.viewport.getWidth();
			var oh=document.viewport.getScrollOffsets().top;
			var ow=document.viewport.getScrollOffsets().left;

			var corners={
				"top-left"		:	"left:"+ow+"px; top:"+oh+"px",
				"bottom-left"	:	"left:"+ow+"px; top:"+(vh+oh-sh)+"px",
				"top-right"		:	"left:"+(vw+ow-sw)+"px; top:"+oh+"px",
				"bottom-right"	:	"left:"+(vw+ow-sw)+"px; top:"+(vh+oh-sh)+"px",
				"center"		:	"left:"+(vw/2+ow-sw/2)+"px; top:"+(vh/2+oh-sh/2)+"px"
			}
			if(!corners[direction]) direction="center";
			element.setStyle(corners[direction]);
		}
		chgPositon();
		Event.observe(window, 'scroll', chgPositon);
		Event.observe(window, 'resize', chgPositon);
		return Element.extend(element);
	}
})

//*******************GRM object class*******************//

var GRMobj = Class.create({
	initialize: function(){
		this.version="3.7.0";
		this.author	="Jim Yuan";
		this.maskID	="GRMask";
	},

	request: function(argu){ //get parameters value
		var parseJSON=location.href.toQueryParams();
		return (parseJSON[argu]) ? parseJSON[argu] : ""
	},
	
	_getCookieVal: function(offset){
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1) endstr = document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
	},

	setCookie: function(name,value){
		document.cookie=name+"="+escape(value);
	},
	
	getCookie: function(name){
		if(name == "path" || name == "expires" || name == "domain" || name == "version") name = "badCookieName";

		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen){
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg) return this._getCookieVal (j);
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break; 
		}
		return null;
	},

	newWin: function(){
		var ancs=$$('a[href][rel=external]');
		ancs.map(function(anc){anc.target="_blank"});
	},

	colorInvert: function(color){
		color=color.toUpperCase();
		var dec2hex="01234567##89ABCDEF".toArray();
		var ptn3=/^#[0-9A-F]{3}$/;
		var ptn6=/^#[0-9A-F]{6}$/;
		var c=""; var newColor=[];

		if(ptn6.test(color)) c=color;
		else if(ptn3.test(color)) c=color.gsub(/[0-9A-F]/, function(match){return match[0].times(2)});
		
		if(!c.empty()){
			c.toArray().each(function(item){
				newColor.push(dec2hex.reverse(false)[dec2hex.indexOf(item)]);
			});
			return newColor.join("");
		}
		else return null
	},

	getPageSize: function(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} 
		else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} 
		else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) { // all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} 
		else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} 
		else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight) pageHeight = windowHeight;
		else  pageHeight = yScroll;

		// for small pages with total width less then width of the viewport
		pageWidth = xScroll;

		//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
		pageSize={cX:pageWidth,cY:pageHeight}
		return pageSize;
	},

	setWinInMask: function(frameURL){
		var winID="GRMaskWin";
		var option=Object.extend(
			{maskColor:"#F90", alpha:0.7, winWidth:400, winHeight:300},//default option
			arguments[1]||{}
		);
		var pageW=this.getPageSize().cX;
		var pageH=this.getPageSize().cY;

		$(document.body).insert(
			new Element("div", {id: this.maskID}).setStyle(
				'position:absolute; background:'+option.maskColor+'; top:0; left:0; width:100%; height:'+pageH+'px;'
			)
		);
		$(this.maskID).setOpacity(option.alpha);
		
		var inHTML='<iframe frameborder="0" id="GRMdivFrame"></iframe>'

		$(document.body).insert(
			new Element("div", {id:winID}).setStyle(
				'width:'+option.winWidth+'px; height:'+option.winHeight+'px; background:white; z-index:1000;'
			).update(inHTML)
		);
		$(winID).absPosition("center");

		$("GRMdivFrame").setStyle('width:'+option.winWidth+'px; height:'+option.winHeight+'px');
		$("GRMdivFrame").src=frameURL;
	},

	clearMask: function(){
		if(window.parent.location.href!=window.location.href){
			window.parent.$(this.maskID).remove();
			window.parent.$("GRMaskWin").remove();
		}
		else if($(this.maskID)) {
			$(this.maskID).remove(); 
			$("GRMaskWin").remove();
		}
	},

	copyText: function(text){
		var tipMsg=Object.extend(
			{success:"Copy Successfully!", failure:"Your Browser does not support the method!"},
			arguments[1] || {}
		);
		if(window.clipboardData){
			window.clipboardData.setData("text",text);
			alert(tipMsg.success);
		}
		else alert(tipMsg.failure);
	},
	
	mixArray: function(source){
		//source=[1,2,3,4,5]
		var goal=[];
		var len=source.length;
		source.each(function(item,i){
			var pos=Math.floor(Math.random()*(len-i));
			goal[i]=source[pos];
			source[pos]=source[len-1-i];
		});
		return goal;
	},

	fadeView: function(obj){
		obj=$(obj)
		var option=Object.extend(
			{interval:0.05, frequency:0.1, display:true, range:[0,1], block:true, complete:Prototype.emptyFunction},//default value
			arguments[1] || {}
		);
		var start;

		with(option){
			if(display){
				start=range.first();
				obj.descendants().invoke("show");
				obj.show();
				obj.setStyle({opacity:start,visibility:"visible"});

				new PeriodicalExecuter(function(pe){
					obj.setStyle({opacity:start});
					start+=frequency;
					if(start >= range.last()) {
						pe.stop(); complete();
					}
				},interval);
			}
			else{
				start=range.last();

				new PeriodicalExecuter(function(pe){
					obj.setStyle({opacity:start});
					start-=frequency;
					if(start <= range.first()) {
						pe.stop();
						if(block && range.first()==0) obj.setStyle({visibility:"hidden"});
						else if(!block && range.first()==0){
							obj.descendants().invoke("hide");
							obj.hide(); 
						}
						complete();
					}
				},interval);
			}
		}
	},

	slideOn: function(obj){
		obj=$(obj);
		var option=Object.extend(
			{interval:0.05, frequency:0.1, direction:"vertical", block:true, complete: Prototype.emptyFunction},
			arguments[1] || {}
		);
		var w=obj.getWidth(); var h=obj.getHeight();
		var nw,nh;

		//Container Initialization
		obj.descendants().invoke("hide");
		obj.show();
		if(option.block){
			if(option.direction=="vertical"){ nw=w+"px"; nh=0;}
			if(option.direction=="horizon"){ nw=0; nh=h+"px";}
		}
		else {nw=0; nh=0;}

		obj.setStyle({width:nw, height:nh, visibility:"visible"});
		
		with(option){
			switch(direction){
				case "vertical":
					new PeriodicalExecuter(function(pe){
						nh+=frequency*h
						obj.setStyle({width:w+"px", height:nh+"px"});
						if(obj.getHeight()>=h){
							pe.stop();
							obj.setStyle({width:w+"px", height:h+"px"})
							obj.descendants().invoke("show");
							complete();
						}
					},interval);
				break;

				case "horizon":
					new PeriodicalExecuter(function(pe){
						nw+=frequency*w;
						obj.setStyle({width:nw+"px",height:h+"px"});
						if(obj.getWidth()>=w){
							pe.stop();
							obj.setStyle({width:w+"px", height:h+"px"})
							obj.descendants().invoke("show");
							complete();
						}
					},interval);
				break;

				default: return;
			}
		}
	},

	slideOff: function(obj){
		obj=$(obj);
		var option=Object.extend(
			{interval:0.05, frequency:0.1, direction:"vertical", block:true, complete: Prototype.emptyFunction},
			arguments[1] || {}
		);
		var w=obj.getWidth(); var h=obj.getHeight();
		
		//Container Initialization
		obj.setStyle({width:w+"px", height:h+"px", visibility:"visible"});
		obj.descendants().invoke("hide");
		obj.show();

		with(option){
			switch(direction){
				case "horizon":
					new PeriodicalExecuter(function(pe){
						obj.setStyle({
							width:(obj.getWidth()-frequency*w)+"px", height:h+"px"
						});
						if(obj.getWidth()<= 18){
							pe.stop();
							if(block) obj.setStyle({visibility:"hidden"})
							else obj.hide();
							obj.setStyle({width:w+"px", height:h+"px"});
							complete();
						}
					},interval);
				break;

				case "vertical":
					new PeriodicalExecuter(function(pe){
						obj.setStyle({
							width:w+"px", height:(obj.getHeight()-frequency*h)+"px",
							lineHeight:0, overflow:"hidden"
						});
						if(obj.getHeight() <= 18){
							pe.stop();
							obj.hide();
							obj.setStyle({width:w+"px", height:h+"px"});
							complete();
						}
					},interval);
				break;

				default: return;
			}
		}
	}
});

var GRM=new GRMobj();

document.observe("dom:loaded", GRM.newWin);


//*******************String prototype extend*******************//
var myExp={
	//双字节字符

	pattern_DB	:	/[^\x00-\xff]/g,
	//中文字符
	pattern_CN	:	/[\u4e00-\u9fa5]/,
	//中文字符
	pattern_CN2	:	/^[\u4e00-\u9fa5]+$/,
	//数字
	pattern_NB	:	/^\d+$/,
	//浮点数

	pattern_FN	:	/^\d+\.\d+$/,
	//EMAIL
	pattern_EM	:	/^\w(\w*\.*-*)*@([\w-]+\.)+\w{2,4}$/,
	//URL
	pattern_UL	:	/^(http|https|ftp):\/\/.*/
}

if(!document.all) document.all=document.getElementById;

Object.extend(String.prototype, {
	hasCN: function(){
		return myExp.pattern_CN.test(this);
	},
	isCN: function(){
		return myExp.pattern_CN2.test(this);
	},
	isInt: function(){
		return myExp.pattern_NB.test(this);
	},
	isFloat: function(){
		return myExp.pattern_FN.test(this);
	},
	isEmail: function(){
		return myExp.pattern_EM.test(this);
	},
	isURL: function(){
		return myExp.pattern_UL.test(this);
	},
	isDate: function(){//(yyyy.mm.dd || yyyy-mm-dd || yyyy#mm#dd)
		var str=this.gsub(/\.|-|#/, "");
		if(str.length==6) str="19"+str
		if(str.length==8){
			var tmpY=str.substring(0,4)-0;
			var tmpM=str.substring(4,6)-0;
			var tmpD=str.substring(6)-0;
			var d=new Date(tmpY,tmpM-1,tmpD);
			if((d.getMonth() != tmpM-1) || (d.getDate() != tmpD)) return false;
			else return true;
		}
		else return false;
	},
	isLeapYear: function(){
		if(this.isInt()){
			var curYear=parseInt(this, 10);
			if(curYear % 400 == 0 || (curYear % 100 !=0 && curYear % 4 == 0)) return true;
			else return false;
		}
		else return false;
	},
	lenB: function(){
		len=this.replace(myExp.pattern_DB,"aa").length;
		return len;
	},
	left: function(charNo){
		return this.substring(0,charNo);
	},
	right: function(charNo){
		return this.substring(this.length-charNo);
	},
	cutStr:function(strNo){
		var cutFix	=arguments[1] || "...";
		var strLen	=this.length;
		var tempLen	=0;
		var totle	=strNo;
		var tempStr	="";

		if (strLen<totle/2) tempStr=this;
		else{
			$R(0, strLen, true).each(function(i){
				var tempChr=this.charCodeAt(i);
				tempLen+=1;
				if(tempLen>totle){
					tempStr+=cutFix;
					throw $break;
				}
				if (tempChr<0 || tempChr>255) tempLen++;
				tempStr+=this.charAt(i);
			}, this);
		}
		return tempStr;
	},
	fillChar: function(lens,fix){
		var tmpFix="";

		if(this.length<lens){
			var fixNo=lens-this.length;
			for(var i=0; i<fixNo; i++) tmpFix+=fix;
			return tmpFix+this
		}
		else return this;
	},
	
	isIDCard: function(status){
		/*{areaCode:areaZone}
		{11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
		*/
		
		var area=[11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91]

		var callBack, yearNo;
		var areaCode=parseInt(this.left(2), 10);
		var	idcard_array=this.toArray().map(function(i){return parseInt(i, 10)});

		switch (this.length){
			default: 
				callBack=1;
				break;

			case 15:
				if(area.indexOf(areaCode)==-1) return callBack=4;
				yearNo="19"+this.substr(6,6);
				if(yearNo.toString().isDate()) callBack=0;
				else callBack=2;
				break;

			case 18:
				if(area.indexOf(areaCode)==-1) return callBack=4;
				yearNo=this.substr(6,8);
				if(yearNo.isDate()){
					var S=	(idcard_array[0] + idcard_array[10]) * 7
						+	(idcard_array[1] + idcard_array[11]) * 9
						+	(idcard_array[2] + idcard_array[12]) * 10
						+	(idcard_array[3] + idcard_array[13]) * 5
						+	(idcard_array[4] + idcard_array[14]) * 8
						+	(idcard_array[5] + idcard_array[15]) * 4
						+	(idcard_array[6] + idcard_array[16]) * 2
						+	 idcard_array[7] * 1 
						+	 idcard_array[8] * 6
						+	 idcard_array[9] * 3;

					var M = '10X98765432'.substr(S%11, 1);//判断校验位
					if(M == idcard_array[17]) callBack=0; //检测ID的校验位
					else callBack=3;
				}
				else callBack=2;
				break;
		}
		var showStatus=arguments[0] || false;
		if(showStatus){
			var callCode=[
				"验证通过！", "身份证号码位数不对！", "身份证号码出生日期超出范围或含有非法字符！", "身份证号码校验错误！", "身份证地区代码错误！"
			];
			return callCode[callBack];
		}
		else return (callBack == 0) ? true : false;
		//return callBack;
	}
});

Object.extend(Array.prototype,{
	mixArray: function(){return GRM.mixArray(this)}
});