var $uploaderlock = 0;
var $scs;

function trim($str){
	return $str.replace(/(^\s*)|(\s*$)/g, "");
}

function isabc($val) {
	var pattern = /^[a-z]*$/i;
	if(pattern.exec($val)) {
		return true;
    } else {
		return false;
	}
}

function isnum($val) {
	var pattern = /^([\d|\+|\-|\*|\/|=])*$/;
	if(pattern.exec($val)) {
		return true;
    } else {
		return false;
	}
}

function istel($val) {
	var pattern =/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
    if(!pattern.exec($val)) {
		return false;
    } else {
		return true;
	}
}

function isemail($val) {
	var pattern =/^[a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,}$/;
    if(!pattern.exec($val)) {
		return false;
    } else {
		return true;
	}
}

function ismobile($val) {
	var pass = false;
	var reg0 = /^(([0\+]\d{2,3})?(13\d{9}))$/;
	var reg1 = /^(([0\+]\d{2,3})?(15\d{9}))$/;
	if(reg0.exec($val)) pass = true;
	if(reg1.exec($val)) pass = true;
	if (pass == true) {
		return true;
	} else {
		return false;
	}
}

function checkform($eform) {
	if (!document.getElementById($eform)) {
		alert($error['formerror']);
		return false;
	}
	var $form_obj = document.getElementById($eform);
	$objs = $form_obj.getElementsByTagName("input");
	for ($i=0;$i<$objs.length;$i++) {
		if ($objs[$i].getAttribute("need") == 1) {
			if (($objs[$i].type == "text" || $objs[$i].type == "file" || $objs[$i].type == "password") && trim($objs[$i].value) == "") {
				$objs[$i].value = "";
				alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['isrequire']);
				$objs[$i].style.borderColor = "#ff0000";
				return false;
			}
			if ($objs[$i].type == "radio" || $objs[$i].type == "checkbox") {
				$check = 0;
				for ($j=0;$j<$objs.length;$j++) {
					if ($objs[$j].name == $objs[$i].name && $objs[$j].checked) $check = 1;
				}
				if ($check == 0) {
					alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['isrequire']);
					return false;
				}
			}
		}
		$ctype = $objs[$i].getAttribute("ctype");
		if ($ctype == "num" && isnum($objs[$i].value) == false && trim($objs[$i].value) != "") {
			alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['onlynum']);
			$objs[$i].style.borderColor = "#ff0000";
			return false;
		}
		if ($ctype == "abc" && isabc($objs[$i].value) == false && trim($objs[$i].value) != "") {
			alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['onlyabc']);
			$objs[$i].style.borderColor = "#ff0000";
			return false;
		}
		if ($ctype == "tel" && istel($objs[$i].value) == false && trim($objs[$i].value) != "") {
			alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['onlytel']);
			$objs[$i].style.borderColor = "#ff0000";
			return false;
		}
		if ($ctype == "mobile" && ismobile($objs[$i].value) == false && trim($objs[$i].value) != "") {
			alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['onlymobile']);
			$objs[$i].style.borderColor = "#ff0000";
			return false;
		}
		if ($ctype == "email" && isemail($objs[$i].value) == false && trim($objs[$i].value) != "") {
			alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['onlyemail']);
			$objs[$i].style.borderColor = "#ff0000";
			return false;
		}
	}
	$objs = $form_obj.getElementsByTagName("textarea");
	for ($i=0;$i<$objs.length;$i++) {
		if ($objs[$i].getAttribute("need") == 1 && trim($objs[$i].value) == "") {
			$objs[$i].value = "";
			alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['isrequire']);
			$objs[$i].style.borderColor = "#ff0000";
			return false;
		}
		if ($objs[$i].getAttribute("maxlen")) {
			$maxlen = $objs[$i].getAttribute("maxlen");
			if ($objs[$i].value.length > $maxlen) {
				alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['overlength']+$maxlen);
				$objs[$i].style.borderColor = "#ff0000";
				return false;
			}
		}
	}
	$objs = $form_obj.getElementsByTagName("select");
	for ($i=0;$i<$objs.length;$i++) {
		if ($objs[$i].getAttribute("need") == 1 && trim($objs[$i].value) == $objs[$i].getAttribute("init")) {
			alert("“"+$objs[$i].getAttribute("notify")+"”"+$error['isrequire']);
			$objs[$i].options[0].style.backgroundColor = "#ff0000";
			$objs[$i].options[0].style.color = "#ffffff";
			return false;
		}
	}
}

function showconfirm(msg,url) {
	if (confirm(msg)) {
		location.href = url;
	}
}

//$pos有两个参数,一个就是在$btn下面;一个是自动居中.$type有三个可选属性,ajax,iframe,msg默认为ajax,如果为ajax时$method有两个可选属性get跟post
function showwin($url,$target,$btn,$title,$w,$h,$pos,$type,$method) {
	if (!$w || $w == '') $w = 500;
	if (!$h || $h == '') $h = 300;
	$w = parseInt($w,10);
	$h = parseInt($h,10);
	if (!$pos || $pos == '') $pos = 'fixed';
	if (!$type || $type == '') $type = 'ajax';
	if (!$method || $method == '') $method = 'get';
	$("body").append('<div id="mask" style="position:absolute;left:0px;top:0px;width:'+$(window).width()+'px;height:'+$("body").height()+'px;background:#ffffff;z-index:999;opacity:0.6;filter:alpha(opacity:60);"></div>');
	$("body").append('<div id="popwin" style="position:absolute;width:'+$w+'px;height:'+$h+'px;border:#cccccc 3px solid;background:#ffffff;z-index:1000;overflow:hidden;"><div id="wintitle" style="clear:both;padding:0px 5px;background:#ebebeb;height:30px;line-height:30px;font-size:14px;font-weight:bold;" onselectstart="return false;" oncontextmenu="return false;"><span style="float:left">'+$title+'</span><span style="float:right"><a href="javascript:void(0);" onclick="hidewin()">关闭</a></span></div><div id="wincontent" style="clear:both;line-height:16px;text-align:left;"></div></div>');
	if ($pos == 'att') {
		$pos_x = $("#"+$btn).position().left;
		$pos_y = $("#"+$btn).position().top;
		$pos_h = $("#"+$btn).height();
		if ($pos_x+$w > $(window).width()) {
			$("#popwin").css({left:($(window).width()-$w-10)+"px",top:($pos_y+$pos_h+6)+"px"});
		} else {
			$("#popwin").css({left:$pos_x+"px",top:($pos_y+$pos_h+6)+"px"});
		}
	} else if ($pos = 'fixed') {
		$st = $(window).scrollTop();
		$mt = parseInt(($(window).height()-$h)/2,10);
		$ml = parseInt(($(window).width()-$w)/2,10);
		$("#popwin").css({left:$ml+"px",top:($mt+$st)+"px"});
	}
	if ($type == 'msg') {
		$("#wincontent").html(unescape($url));
	} else if ($type == 'ajax') {
		$("#wincontent").html("载入中...");
		$.get($url,{target:$target,sid:Math.random()},function(data){$("#wincontent").html(data);});
	} else if ($type == 'iframe') {
		$("#wincontent").html('<iframe frameborder="0" width='+$w+'px height='+($h-30)+'px scrolling="auto" src="'+$url+'"></iframe>');
	}
}

function hidewin() {
	$("#mask").remove();
	$("#popwin").remove();
}

function showvote($id) {
    showwin(cfg_root+'plus/vote.php?act=view&voteid='+$id,'','','','600','400','fixed','iframe','');
}

function stepoption($obj,$opttype,$label,$pid,$method){
	if (trim($obj) != "") {
		if (!isnum($pid)) $pid = 0;
		var $sval = 0;
		var $i = 0;
		$.each($("._"+$label),function($k,$v){
			if ($v.parentNode != null) {
				if ($v.parentNode.getAttribute("id") == $obj) $sval = 1;
				if ($sval == 1) $v.parentNode.innerHTML = "";
			}
		});
		var $val_up = "";
		$.each($("._"+$label),function($k,$v){
			$val_new = $v.getAttribute("value");
			if ($val_new == 0 && $i != 0) {
				$("#_"+$label).attr("value",$val_up);
				return;
			}
			$val_up = $val_new;
			$("#_"+$label).attr("value",$val_new);
			$i++;
		});
		if ($sval == 1 && $obj != $label && $pid == 0) return;
		$("#"+$obj).html("载入中...");
		if ($method == 'view') {
			$.get(cfg_root+'include/stepoption.php',{method:'view',opttype:$opttype,label:$label,pid:$pid},function(data){$("#"+$obj).html(data);});
		} else {
			$.get(cfg_root+'../include/stepoption.php',{opttype:$opttype,label:$label,pid:$pid},function(data){$("#"+$obj).html(data);});
		}
	} else {
		$("#_"+$label).attr("value",$pid);
	}
}

function diydata($tag,$page) {
    $pagesize = $("#pagesize_"+$tag).attr("value");
    $index = $("#index_"+$tag).attr("value");
    $innerhtml = $("#innerhtml_"+$tag).attr("value");
    $cfg_root = $("#cfg_root").attr("value");
    $lang = $("#lang").attr("value");
    $.post($cfg_root+'plus/diydata.php',{lang:$lang,tag:$tag,index:$index,page:$page,pagesize:$pagesize,innerhtml:$innerhtml},function(data){$('#ajax_'+$tag).html(data)});
}

function comment_good($comment_id){
    if (GetCookie("comment_goodbad").indexOf($comment_id) != -1) {
        alert("您已经发表过了，先休息会吧！");
        return;
    }
    $num = parseInt($("#comment_good_"+$comment_id+">span").html(),10);
    $.post(
        cfg_root+'plus/comment.php',
        {
            act:'good',
            comment_id:$comment_id
        },
        function(data){
            if (data.match(/^msg/i)) {
                alert(data.replace('msg:',''));
            } else {
                $("#comment_good_"+$comment_id+">span").html($num+1);
                $("#comment_good_"+$comment_id).removeAttr("href");//这里是发表以后就不可再点击,但刷新页面可再点击
                $("#comment_bad_"+$comment_id).removeAttr("href");
                if (GetCookie("comment_goodbad") == "") SetCookie("comment_goodbad",",");
                SetCookie("comment_goodbad",GetCookie("comment_goodbad")+$comment_id+",");
            }
        });
}

function comment_bad($comment_id){
    if (GetCookie("comment_goodbad").indexOf($comment_id) != -1) {
        alert("您已经发表过了，先休息会吧！");
        return;
    }
    $num = parseInt($("#comment_bad_"+$comment_id+">span").html(),10);
    $.post(
        cfg_root+'plus/comment.php',
        {
            act:'bad',
            comment_id:$comment_id
        },
        function(data){
            if (data.match(/^msg/i)) {
                alert(data.replace('msg:',''));
            } else {
                $("#comment_bad_"+$comment_id+">span").html($num+1);
                $("#comment_bad_"+$comment_id).removeAttr("href");//这里是发表以后就不可再点击,但刷新页面可再点击
                $("#comment_good_"+$comment_id).removeAttr("href");
                if (GetCookie("comment_goodbad") == "") SetCookie("comment_goodbad",",");
                SetCookie("comment_goodbad",GetCookie("comment_goodbad")+$comment_id+",");
            }
        });
}

function SetCookie(name,value) {
   var argv=SetCookie.arguments;
   var argc=SetCookie.arguments.length;
   var expires=(2<argc)?argv[2]:null;
   var path=(3<argc)?argv[3]:null;
   var domain=(4<argc)?argv[4]:null;
   var secure=(5<argc)?argv[5]:false;
   document.cookie=name+"="+escape(value)+((expires==null)?"":("; expires="+expires.toGMTString()))+((path==null)?"":("; path="+path))+((domain==null)?"":("; domain="+domain))+((secure==true)?"; secure":"");
}

function GetCookie(Name) {
   var search = Name + "=";
   var returnvalue = "";
   if (document.cookie.length > 0)
   {
      offset = document.cookie.indexOf(search);
   if (offset != -1)
   {
   offset += search.length;
         end = document.cookie.indexOf(";", offset);
   if (end == -1)
            end = document.cookie.length;
         returnvalue=unescape(document.cookie.substring(offset,end));
       }
    }
    return returnvalue;
}

window.onload=function () {
	window.status = "技术支持：广州木棉科技 www.wsing.net";
}
