//替换
/*
返回值:替换后的值
参数说明:str_raw,原值;strFind 要替换的值; strReplace替换成
*/
function f_replace(str_raw,strFind,strReplace)
{
    var tmpval=str_raw.toString();
    return tmpval.split(strFind).join(strReplace);
} 

//检验时间,网上下载
/*返回值:true真,false假
参数说明:valobj,检验的控件对象
*/
function f_validatetime(valobj)
{
	timeStr=valobj.value.replace("：",":")
	if (timeStr=="") return false;
	if (timeStr.length==4)
	timeStr=timeStr.substr(0,2)+":"+timeStr.substr(2,2)

	valobj.value=timeStr
	var timePat = /^(\d{1,2}):(\d{1,2})$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) 
	{
		alert("输入的时间有误，请参照格式:小时:分钟!");
		valobj.value="08:00"
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];

	if (hour < 0  || hour > 23) 
	{
		alert("小时数必须在00--23之间!");
		hour=8;
	}

	if (minute < 0 || minute > 59) 
	{
		alert ("分钟数必须在00--59之间!");
		minute=0;
	}
	valobj.value=('00'+hour).substr(('00'+hour).length-2,2)+":"+('00'+minute).substr(('00'+minute).length-2,2)
	return true;
}

//检验日期,网上下载
/*
返回值:true真,false假
参数说明:valobj,检验的控件对象
*/
function f_validatedate(valobj) 
{
	dar=f_replace(valobj.value,".","-")
	if (dar=="") return false;
	if(dar.split("-")[0].length==2)
	{
		var Current_Date = new Date();
		var Current_year = Current_Date.getYear();
		dar=Current_year.toString().substr(0,2)+dar
	}
	var datePat = /^(\d{4})(-)(\d{1,2})(-)(\d{1,2})$/;

	var matchArray = dar.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		alert("输入的日期有误，请参照格式:年年年年-月月-日日!");
		valobj.value=""
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[5];
	year = matchArray[1];
	if (month < 1 || month > 12) 
	{ // check month range
		alert("月份超界!");
		month=1;
	}
	if (day < 1 || day > 31) 
	{
		alert("日期超界!");
		day=1;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		alert(month+"月没有31日!");
		day=30;
	}
	if (month == 2) { // check for february 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day>29) 
	{
		alert("2月不能超过29日!");
		day=28;
	}
	if (day==29 && !isleap) 
	{
		alert(year + "年不是闰年，2月没有29日!");
		day=28;
	}
	}
	valobj.value=year+"-"+('0'+month).substr(('0'+month).length-2,2)+"-"+('0'+day).substr(('0'+day).length-2,2)
	return true;
}

function showToolTips(i) 
{ 
document.getElementById("tooltips"+i).style.display = 'block';
} 
function hiddenToolTips(i) 
{ 
document.getElementById("tooltips"+i).style.display = 'none';
}
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;
    }
  }
}

function isChinese(str) {
   var lst = /[u00-uFF]/;       
   return !lst.test(str);      
}
function strlen(str) {
   var strlength=0;
   for (i=0;i<str.length;i++) {
     if (isChinese(str.charAt(i))==true)
        strlength=strlength + 2;
     else
        strlength=strlength + 1;
   }
   return strlength;
}

function strip_tags(str, allowed_tags) {
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = ''; 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    }; 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }  
    str += ''; 
    str=CtoH(str);
    // Match tags
    matches = str.match(/(<\/?[^>]+>)/gi); 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        } 
        // Save HTML tag
        html = matches[key].toString(); 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    return str;
}
function CtoH(objStr)
{ 
var str=objStr;
var result="";
for (var i = 0; i < str.length; i++)
{
if (str.charCodeAt(i)==12288)
{
result+= String.fromCharCode(str.charCodeAt(i)-12256);
continue;
}
if (str.charCodeAt(i)>65280 && str.charCodeAt(i)<65375)
result+= String.fromCharCode(str.charCodeAt(i)-65248);
else result+= String.fromCharCode(str.charCodeAt(i));
} 
return result;
}

function getCommonObj() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

// javascript 操作Cookies
function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
    var Days = 7; //此 cookie 将被保存 7 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var err = "";
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return err;
}

var comment_backurl= document.URL;
var comment_loginfile;
var comment_temp;
var comment_userIsLogin = false;
function comment_get_cookie(varname)
{
	var tmp_ary = new Array();
	if (varname)
	{
		var a = document.cookie.indexOf(varname+'=');
		if (a != -1)
		{
			var b = document.cookie.substring((a+varname.length+1),document.cookie.length);
			var c = b.split(';');
			var d = c[0];
			return d;
		}
	}
}
if( document.cookie.indexOf('NicWellNickName=') > -1)
{
	//if( document.cookie.indexOf('NicWellIsCorp=') > -1 && getCookie('NicWellisLockCorp')=='0')
	//{
		comment_temp = unescape(decodeURI(comment_get_cookie('NicWellNickName')))+' | <a class="recsite" href="http://www.ieche.com/users/main.asp" target=_blank>用户中心</a>'+' | ';
	//}else{
	//	comment_temp = unescape(decodeURI(comment_get_cookie('NicWellNickName')))+' | ';
	//}
}else
	{
		comment_temp ="";
	}
if( document.cookie.indexOf('NicWellNickName=') > -1)
{
	comment_userIsLogin = true;
}
function InitLoginStatus()
{
    if( !comment_userIsLogin ) 
		{
			getCommonObj("divUnLogin").style.display = "block";
        	getCommonObj("divLogined").style.display = "none";
		}
		else
		{
			getCommonObj("divUnLogin").style.display = "none";
        	getCommonObj("divLogined").style.display = "block";
		}
}

function copyToclipboard(ss)
{
	window.clipboardData.setData("text",ss);
	alert("您已经复制了该问题的链接，可以直接粘贴发给您的朋友。");
}
function CheckLoginForm()
{
	if(document.myform.name.value=="")
	{
		alert("请输入您的用户名！");
		document.myform.name.focus();
		return false;
	}
	if(document.myform.password.value == "")
	{
		alert("请输入您的密码！");
		document.myform.password.focus();
		return false;
	}
}

function strlen(str) {
   var strlength=0;
   for (i=0;i<str.length;i++) {
     if (isChinese(str.charAt(i))==true)
        strlength=strlength + 2;
     else
        strlength=strlength + 1;
   }
   return strlength;
}

function strip_tags(str, allowed_tags) {
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = ''; 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    }; 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }  
    str += ''; 
    str=CtoH(str);
    // Match tags
    matches = str.match(/(<\/?[^>]+>)/gi); 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        } 
        // Save HTML tag
        html = matches[key].toString(); 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    return str;
}
function CtoH(objStr)
{ 
var str=objStr;
var result="";
for (var i = 0; i < str.length; i++)
{
if (str.charCodeAt(i)==12288)
{
result+= String.fromCharCode(str.charCodeAt(i)-12256);
continue;
}
if (str.charCodeAt(i)>65280 && str.charCodeAt(i)<65375)
result+= String.fromCharCode(str.charCodeAt(i)-65248);
else result+= String.fromCharCode(str.charCodeAt(i));
} 
return result;
}
function CheckCommentForm(){
	var formobj=document.getElementById("frm_comment");
	var html_content=strip_tags(formobj.CommentText.value,'');
	var mobile = /((\(\d{3}\))|(\d{3}\-))?1[35]\d{9}/;
	var urlstr=/((\w)+[.]){1,}net|com|cn|org|cc|tv|[0-9]{1,3}/;
	if(html_content!=""){
		if(strlen(html_content) < 5) {
			alert("您的评论不能少于5个字符，尚不能提交。");
			return false;
		}
		if(strlen(html_content) > 500) {
			alert("您的总评超过了500字符，尚不能提交。");
			return false;
		}
		if(mobile.exec(html_content)) {
			alert("您好，您发送的评论信息被怀疑是广告，请核实后再发");
			return false;
		}
		if(urlstr.exec(html_content)) {
			alert("您好，您发送的评论信息被怀疑是广告，请核实后再发");
			return false;
		}
	}
	if(html_content==""){
		alert("画龙点睛，就差你神来一笔，您可以任意发表您的评论！！！");
		formobj.CommentText.focus();
		formobj.CommentText.select();
		return false;
	}
}