﻿function IsEmail(strInput)
{
	//generate the email' regular expression
	var reg = "[a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+(([.][a-zA-Z0-9_-]+){1,3})", mode = "gi";	
	return checkRegExp(reg,mode,strInput);
}

function IsUserName(strInput)
{
	//generate the username' regular expression
	var reg = "[0-9A-Za-z_]+", mode = "gi";	
	return checkRegExp(reg,mode,strInput);
}

function GetBytesCount(str)
{
	var len = 0;
	if (str==null||str=="undefined"||str=="") return len;
	for (var i = 0; i < str.length ;i++ )
	{
		ch = str.charCodeAt(i);
		if (ch<0) ch=ch+65536;
		if (ch>255) len++;
		len++;
	}
	return len;
}

function SubStringB(str,maxlen)
{
	var strtemp = str;
	if(maxlen <= 0)
		return '.';
	var len = 0;
	for (var i = 0; i < str.length ;i++ )
	{
		var ch = str.charCodeAt(i);
		if (ch<0) ch=ch+65536;
		if (ch>255)
			len+=2;
		else
			len+=1;
		if(len >= maxlen)
		{
			if(len == maxlen)
				len = i;
			else
				len = i-1;
			break;
		}
	}
	if(len < str.length - 1)
	{
		if(len < 0)
			return ".";
		if(str.length <= 1)
			strtemp = ".";
		else if(str.length<=2)
			strtemp = '..';
		else
		{
			strtemp = str.substring(0,len + 1) + "..";
			if(strtemp.length > maxlen)
				strtemp = strtemp.substring(0,strtemp.length-4)+"..";
		}
	}
	else
	{
		strtemp = str
	}
	return strtemp;
}

function StrCheckByte(str,minSize,maxSize){
	
	if (str=="") return true;

	maxSize = maxSize + 1;
	minSize = minSize - 1;
	var i;
	for (i = 0; i < str.length ;i++ )
	{
		ch = str.charCodeAt(i);
		if (ch != 32)
		{
			str = str.substring(i,str.length);
			break;
		}
	}

	for ( i = str.length -1 ; i >= 0 ; i--)
	{
		ch = str.charCodeAt(i);
		if (ch != 32)
		{
			str = str.substring(0,i+1);
			break;
		}

	}
	  var chinese="中";
	  var chinesecheck,i;
	  if (chinese.length==1) chinesecheck=true;
	  else chinesecheck=false;
	  
	  if (chinesecheck){
		  slen=0;
		  for (i=0;i<str.length;i++){
			ch=str.charCodeAt(i);
			if (ch<0) ch=ch+65536;
			if (ch>255) slen+=1;
			slen++;
			}
	  len=slen;
	  }
	  else len=str.length;
		
		if (len<maxSize && len>minSize)
			 return(true);
		else
			return(false);
}

//check the string whether is pure number or not
function StrCheckNum(num){

	if (num == "")
		return false;
	for(i = 0;i < num.length; i++)
	{
		ch = num.charCodeAt(i);
		if ( (ch < 48) || (ch > 57) )
			return false;	
	}

	return true;
}

function checkRegExp(reg,mode,strInput)
{
	var re = new RegExp(reg,mode);
	
	var ii= strInput.search(re);
	
	if (0 == strInput.search(re))
	{
		strInput = strInput.replace(re,"");
				
		if("" == strInput)
			return true;
		else
			return false;
	}
	else
		return false;
}

//add by 王建财 2005－04-12
function CheckMobile(strInput)
{
	var reg = "[0-9]{11}",mode = "gi";
	return checkRegExp(reg,mode,strInput);
}

//add by 王建财 2005－04-12
String.prototype.trim = function() 
{//压缩字符串两端的空格
   // Use a regular expression to replace leading and trailing 
   // spaces with the empty string
   return this.replace(/(^\s*)|(\s*$)/g, "");
}
//add by 王建财 2005－04-12
function DisUnchecked(obj)
{//将某组里面未选中的单选框置灰，以便不进行提交
	try{
		var i = 0;
		//将未选中的项置灰
		for(i=0;i<obj.length;i++)
		{
			if(!obj[i].checked)
			{
				obj[i].disabled = true;
			}
		}
		
	}catch(e){
	}
	
}
function SetCookie(name,value,expdate,path,domain)//设定Cookie值
{
	document.cookie = name + "=" + escape (value)
	+ ((expdate == null) ? "": ("; expires="+ expdate.toGMTString()))
	+ ((path == null) ? "": ("; path="+ path)) 
	+ ((domain == null) ? "": ("; domain="+ domain)); 
}
function DelCookie(name,path,domain)//删除Cookie
{
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	SetCookie(name,cval,exp,path,domain);
}
function GetCookieVal(offset)//获得Cookie解码后的值
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name)//获得Cookie的原始值
{
	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 GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return null;
}

function OpenWindow(url,target,channelmode,directories,fullscreen,location,menubar,resizable,scrollbars,status,titlebar,toolbar,width,height,replace)
{
	var top = window.screen.height/2-height/2;
	var left = window.screen.width/2-width/2;
	
	window.open(url,target,'channelmode='+channelmode+',directories='+directories+',fullscreen='+fullscreen+',location='+location+',menubar='+menubar+',resizable='+resizable+',scrollbars='+scrollbars+',status='+status+',titlebar='+titlebar+',toolbar='+toolbar+',top='+top+',left='+left+',width='+width+',height='+height,replace);
}

String.prototype.left = function(num,mode){
        if(!/\d+/.test(num))return(this);
        var str = this.substr(0,num);
        if(!mode) return str;
        var n = str.Tlength() - str.length;
        num = num - parseInt(n/2);
        return this.substr(0,num);
    }
    
function DrawPlayer(id,width,height,playurl)
{
	var code = "";
	code+="<object id=\""+id+"\" width=\""+width+"\" height=\""+height+"\" classid=\"clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6\" VIEWASTEXT>";
	code+="<param name=\"URL\" value=\""+playurl+"\">";
    code+="<param name=\"stretchToFit\" value=\"1\">";
	code+="<param name=\"rate\" value=\"1\">";
	code+="<param name=\"currentPosition\" value=\"0\">";
	code+="<param name=\"playCount\" value=\"1\">";
	code+="<param name=\"autoStart\" value=\"-1\">";
	code+="<param name=\"volume\" value=\"60\">";
	code+="<param name=\"mute\" value=\"0\">";
	code+="<param name=\"uiMode\" value=\"full\">";
	code+="<param name=\"stretchToFit\" value=\"false\">";
	code+="<param name=\"windowlessVideo\" value=\"1\">";
	code+="<param name=\"enableContextMenu\" value=\"1\">";
	code+="</object>";
	
	document.write(code);
}

function DrawFlash(filePath,objectId,width,height,value) 
{
	var code = "";
	code+="<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" WIDTH=\""+width+"\" HEIGHT=\""+height+"\" CODEBASE=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab\" ID=\""+objectId+"\" VIEWASTEXT>";
	code+="	<PARAM NAME=\"allowScriptAccess\" VALUE=\"true\">";
	code+="	<PARAM NAME=\"MOVIE\" VALUE=\""+filePath+"\">";
	code+="	<PARAM NAME=\"QUALITY\" VALUE=\"high\">";
	code+="	<PARAM NAME=\"SCALE\" VALUE=\"SHOWALL\">";
	code+="	<PARAM NAME=\"MENU\" VALUE=\"false\">";
	code+="	<PARAM NAME=\"wmode\" VALUE=\"opaque\">";
	if(value != "undefined" && value != null && value != "")
	    code+="	<PARAM NAME=\"FlashVars\" VALUE=\""+value+"\">";
	code+="	<EMBED";
	code+="		NAME=\""+objectId+"\"";
	code+="		width=\""+width+"\"";
	code+="		height=\""+height+"\"";
	code+="		SRC=\""+filePath+"\"";
	if(value != "undefined" && value != null && value != "")
	    code+="		FlashVars=\""+value+"\"";
	code+="		QUALITY=\"high\"";
	code+="		wmode=\"opaque\"";
	code+="		menu=\"false\"";
	//code+="		swLiveConnect=\"true\"";
	code+="     allowScriptAccess=\"sameDomain\"";
	code+="     type=\"application/x-shockwave-flash\"";
	code+="		PLUGINSPAGE=\"http://www.macromedia.com/go/flashplayer/\"> </EMBED>";
	code+="</OBJECT>";
	
	document.write(code);
}

function DjTextArea_AddMaxlengthAttr()
{
    var aX = document.getElementsByTagName('textarea');

    for ( var iI = 0; aX.length > iI; iI++ )
    {
        if ( aX[iI].getAttribute('maxlength') )
        {
            aX[iI].onkeyup = aX[iI].onchange = DjTextArea_CheckMaxlength;
            aX[iI].onchange = DjTextArea_CheckMaxlength;
            
        }
    }
} //@ END OF DjTextArea_AddMaxlengthAttr()

function DjTextArea_CheckMaxlength()
{
    var iMaxLen = parseInt(this.getAttribute('maxlength'));
    var iCurLen = this.value.length;

    if ( this.getAttribute && iCurLen > iMaxLen )
    {
        this.value = this.value.substring(0, iMaxLen);
    }
} //@ END OF DjTextArea_CheckMaxlength()

function changeText(obj,count)
{
	var input = obj.value;
	input = input.replace(/(^\s*)|(\s*$)/g, "");
	var Formate = "^[0-9]{1,}$";
	var Reg = new RegExp(Formate);
	if(!Reg.test(input)|(input == ""))
	{
		//alert(requireMessage);
		obj.value = "";
		return false;
	}
	var intPageCount = parseInt(count);
	var intInputPage = parseInt(input);
	
	if((intInputPage > intPageCount)|(intInputPage < 1))
	{
		//alert(regularMessage);
		obj.value = "";
		return false;
	}
	return true;
}

var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

function tobase64(str)
{
    return base64encode(utf16to8(str))
}

function frombase64(str)
{
    return utf8to16(base64decode(str))
}

function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
    c1 = str.charCodeAt(i++) & 0xff;
    if(i == len)
    {
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt((c1 & 0x3) << 4);
        out += "==";
        break;
    }
    c2 = str.charCodeAt(i++);
    if(i == len)
    {
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt((c2 & 0xF) << 2);
        out += "=";
        break;
    }
    c3 = str.charCodeAt(i++);
    out += base64EncodeChars.charAt(c1 >> 2);
    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
    out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
    /* c1 */
    do {
        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c1 == -1);
    if(c1 == -1)
        break;

    /* c2 */
    do {
        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c2 == -1);
    if(c2 == -1)
        break;

    out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

    /* c3 */
    do {
        c3 = str.charCodeAt(i++) & 0xff;
        if(c3 == 61)
        return out;
        c3 = base64DecodeChars[c3];
    } while(i < len && c3 == -1);
    if(c3 == -1)
        break;

    out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

    /* c4 */
    do {
        c4 = str.charCodeAt(i++) & 0xff;
        if(c4 == 61)
        return out;
        c4 = base64DecodeChars[c4];
    } while(i < len && c4 == -1);
    if(c4 == -1)
        break;
    out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}

function utf16to8(str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
        out += str.charAt(i);
    } else if (c > 0x07FF) {
        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
        out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    } else {
        out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    }
    }
    return out;
}

function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;

    out = "";
    len = str.length;
    i = 0;
    while(i < len) {
    c = str.charCodeAt(i++);
    switch(c >> 4)
    { 
      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
        // 0xxxxxxx
        out += str.charAt(i-1);
        break;
      case 12: case 13:
        // 110x xxxx   10xx xxxx
        char2 = str.charCodeAt(i++);
        out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
        break;
      case 14:
        // 1110 xxxx  10xx xxxx  10xx xxxx
        char2 = str.charCodeAt(i++);
        char3 = str.charCodeAt(i++);
        out += String.fromCharCode(((c & 0x0F) << 12) |
                       ((char2 & 0x3F) << 6) |
                       ((char3 & 0x3F) << 0));
        break;
    }
    }

    return out;
}


var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0 

function dyniframesize(iframename) {
    var pTar = null;
    if (document.getElementById){
        pTar = document.getElementById(iframename);
    }
    else{
        eval('pTar = ' + iframename + ';');
    }
    if (pTar && !window.opera){
    //begin resizing iframe
        pTar.style.display="block"

        if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
        //ns6 syntax
            pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight; 
        }
        else if (pTar.Document && pTar.Document.body.scrollHeight){
        //ie5+ syntax
            pTar.height = pTar.Document.body.scrollHeight;
        }
    }
}
