/*
'=====================================================================
' Module Name:
' File name: common.js
' Author: william.lu
' Project : 
' Copyright (c) all rights reserved by jiajun_lu@163.com
' Create on 2008/12/01
' Version: 1.0
'
'=====================================================================
'*********************************************************************
' Usage: 公共JS函数集
'
'*********************************************************************
*/

/**  定义JS的域  **/
document.domain = "saywo.com";

/**  取得元素对象  **/
function $obj(id){
	return document.getElementById(id);
}

/**  取得母页面元素对象  **/
function $objParent(id){
	return parent.document.getElementById(id);
}

/**  定义juuyou命名空间  **/
var juuyou = {};

/**  定义juuyou.common命名空间  **/
juuyou.common = {};

/**  定义juuyou.common.prototype命名空间  **/
juuyou.common.prototype = {};

/**  定义juuyou.common.json命名空间  **/
juuyou.common.json = {};
juuyou.common.json.success = "success";
juuyou.common.json.failure = "failure";
juuyou.common.json.already = "already";
/**  定义juuyou.common.obj命名空间  **/
juuyou.common.obj = {};

/**  获得字节数  **/
juuyou.common.calculate_msglen = function(message){
	var nbytes = 0;
	for (i=0; i<message.length; i++) {
		var ch = message.charAt(i);
		if(escape(ch).length > 4) {
			nbytes += 3;
		}else if(ch == '\n') {
			if(message.charAt(i-1) != '\r') {
				nbytes += 1;
			}
		} else if (ch == '<' || ch == '>') {
			nbytes += 4;
		} else {
			nbytes += 1;
		}
	}
	return nbytes;
}

/**  有效字符数控制  **/
juuyou.common.validWordLength = function(wordId, cntId, validWordCnt){
	var intCnt = 0;
	var strVal = $obj(wordId).value;
	var remainCnt = 0;
	intCnt = strVal.length;
	remainCnt = validWordCnt - intCnt;
	
	if(remainCnt < 0){
		//alert("字符数最大不得超过" + validWordCnt);
		if(cntId == "strong_word_cnt"){
			$("#strong_word_cnt").html("0");
		}else if(cntId == "strong_know_word_cnt"){
			$("#strong_know_word_cnt").html("0");
		}else if(cntId == "strong_focus_word_cnt"){
			$("#strong_focus_word_cnt").html("0");
		}else if(cntId == "strong_photo_word_cnt"){
			$("#strong_photo_word_cnt").html("0");
		}else{
			$obj(cntId).value="0";
		}
		$obj(wordId).value = $.trim($obj(wordId).value);
		return false;
	}else{
		if(cntId == "strong_word_cnt"){
			$("#strong_word_cnt").html(remainCnt);
		}else if(cntId == "strong_know_word_cnt"){
			$("#strong_know_word_cnt").html(remainCnt);
		}else if(cntId == "strong_focus_word_cnt"){
			$("#strong_focus_word_cnt").html(remainCnt);
		}else if(cntId == "strong_photo_word_cnt"){
			$("#strong_photo_word_cnt").html(remainCnt);
		}else{
			$obj(cntId).value=remainCnt;
		}
		
	}
	
	return true;
	
}
juuyou.common.validWordLength2 = function(wordId, cntId){
	var intCnt = 0;
	var strVal = $obj(wordId).value;
	intCnt = strVal.length;
	$("#" + cntId).html(intCnt);
}

/**  获得对应id下拉框选中的值  **/
juuyou.common.getSelectVal = function(id){
	var objSelect = $obj(id);
	var strTemp = objSelect.options[objSelect.selectedIndex].value;
	return strTemp;
}

/*
	获得对应name单选按钮选中的值
*/
juuyou.common.getRadioBtnValue = function(name){
	var obj = document.getElementsByName(name);
	var intLength = obj.length;
	var strTemp;
	
	for(var i=0; i<intLength; i++){
		if(obj[i].checked) {
			strTemp = obj[i].value;
			break;
		}
	}
	return strTemp;
}

juuyou.common.setRadioBtnValue = function(name, val){
	var obj = document.getElementsByName(name);
	var intLength = obj.length;
	
	for(var i=0; i<intLength; i++){
		if(obj[i].value == val) {
			obj[i].checked = true;
			break;
		}
	}
}

/**  去除字符窜左右空白  **/
juuyou.common.trim = function(str){
	return str.replace(/^\s+|\s+$/g,"");
}


/**  在给定对象集合中显示和隐藏对象  **/
juuyou.common.switchObjDisplayInList = function(showId, objList){
	for(var cssLiId in objList){
		var temp = objList[cssLiId];
		if(temp == showId){
			$obj(temp).style.display = "";
		}else{
			$obj(temp).style.display = "none";
		}
	}
}

/**  显示隐藏状态切换  **/
juuyou.common.switchDisplay = function(objId){
	if($obj(objId).style.display == "none"){
		$obj(objId).style.display = "";
	}else{
		$obj(objId).style.display = "none";
	}
}

/**  母页面中元素显示隐藏状态切换  **/
juuyou.common.parentSwitchDisplay = function(objId){
	if($objParent(objId).style.display == "none"){
		$objParent(objId).style.display = "";
	}else{
		$objParent(objId).style.display = "none";
	}
}

/**  隐藏display状态,传递对象id的数组或单个id字符窜  **/
juuyou.common.switchDisplayNone = function(ids){
	if(ids.constructor == Array){
		for(var id in ids){
			$obj(ids[id]).style.display = "none";
		}
	}else if(typeof ids == "string"){
		$obj(ids).style.display = "none";
	}
}

/**  母页面隐藏display状态,传递对象id的数组或单个id字符窜  **/
juuyou.common.parentSwitchDisplayNone = function(ids){
	if(ids.constructor == Array){
		for(var id in ids){
			$objParent(ids[id]).style.display = "none";
		}
	}else if(typeof ids == "string"){
		$objParent(ids).style.display = "none";
	}
}

/**  显示display状态,传递对象id的数组或单个id字符窜  **/
juuyou.common.switchDisplayShow = function(ids){
	if(ids.constructor == Array){
		for(var id in ids){
			$obj(ids[id]).style.display = "";
		}
	}else if(typeof ids == "string"){
		$obj(ids).style.display = "";
	}
}

/**  替换对象的className属性  **/
juuyou.common.changeClass = function(obj, className){
	obj.className = className;
}

/**  验证是否为英文字母和数字  **/
juuyou.common.isAlphaNumeric = function(inputText) {
	var reg = /^[a-zA-Z0-9_]+$/;
	
	if (reg.test(inputText)) {
		return true;
	}
	
	return false;
}

/**  验证是否为英文字母  **/
juuyou.common.isAlpha = function(inputText) {
	var reg = /^[a-zA-Z]+$/;
	
	if (reg.test(inputText)) {
		return true;
	}
	
	return false;
}

/**  验证是否为英文字母和数字,特殊字符  **/
juuyou.common.isPassword = function(inputText) {
	var reg = /^[\w\W]+$/;
	
	if (reg.test(inputText)) {
		return true;
	}
	
	return false;
}

/**  验证是否包含空格  **/
juuyou.common.isIncludeSpace = function(inputText){
	var invalid = " ";
	if (inputText.indexOf(invalid) > -1) {
		return true;
	}
	return false;
}

/**  计算字符窜字节长度  **/
juuyou.common.calculate_msglen = function(message){
	var nbytes = 0;
	for (i=0; i<message.length; i++) {
		var ch = message.charAt(i);
		if(escape(ch).length > 4) {
			nbytes += 3;
		}else if(ch == '\n') {
			if(message.charAt(i-1) != '\r') {
				nbytes += 1;
			}
		} else if (ch == '<' || ch == '>') {
			nbytes += 4;
		} else {
			nbytes += 1;
		}
	}
	return nbytes;
}

/**  验证Email格式  **/
juuyou.common.isEmail = function(strEmail) {
	return strEmail.search(/^\s*[\w\~\-\.]+\@[\w\~\-]+(\.[\w\~\-]+)+\s*$/g) >= 0;
}

/**  生日选择生成  **/
juuyou.common.createSelBirth = function(id){
	var strTemp = "<select size=\"1\" id=\"sel_year\" name=\"sel_year\" class=\"select_60\">";
	for(var i=2008; i>1950; i--){
		strTemp += "<option value=" + i + ">" + i + "</option>";
	}
	strTemp += "</select>&nbsp;年&nbsp;<select size=\"1\" id=\"sel_month\" name=\"sel_month\" class=\"select_60\">";
	for(var i=1; i<13; i++){
		var j = i;
		if(i > 0 && i < 10){
			j = "0"+i
		}
		strTemp += "<option value=" + j + ">" + j + "</option>";
	}
	strTemp += "</select>&nbsp;月&nbsp;<select id=\"sel_day\" size=\"1\" name=\"sel_day\" class=\"select_60\">";
	for(var i=1; i<32; i++){
		var j = i;
		if(i > 0 && i < 10){
			j = "0"+i
		}
		strTemp += "<option value=" + j + ">" + j + "</option>";
	}
	strTemp += "</select>&nbsp;日";
	$obj(id).innerHTML = strTemp;
}

/**  出版日期选择生成  **/
juuyou.common.createSelPublish = function(id){
	var strTemp = "<select size=\"1\" id=\"sel_year\" name=\"sel_year\" class=\"select_60\">";
	for(var i=2011; i>1950; i--){
		strTemp += "<option value=" + i + ">" + i + "</option>";
	}
	strTemp += "</select>&nbsp;年&nbsp;<select size=\"1\" id=\"sel_month\" name=\"sel_month\" class=\"select_60\">";
	for(var i=1; i<13; i++){
		var j = i;
		if(i > 0 && i < 10){
			j = "0"+i
		}
		strTemp += "<option value=" + j + ">" + j + "</option>";
	}
	strTemp += "</select>&nbsp;月";
	$obj(id).innerHTML = strTemp;
}


/**  验证是否为数字  **/
juuyou.common.IsNumeric = function(inputText) {
	var reg = /^[0-9]+$/;
	
	if (reg.test(inputText)) {
		return true;
	}
	
	return false;
}

/**  复制文本内容到剪贴板  **/
juuyou.common.copyToClipboard = function(txt) {        
     if(window.clipboardData) {        
             window.clipboardData.clearData();        
             window.clipboardData.setData("Text", txt);        
     } else if(navigator.userAgent.indexOf("Opera") != -1) {        
          window.location = txt;        
     } else if (window.netscape) {        
          try {        
               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");        
          } catch (e) {        
               alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");  
			   return false;
          }        
          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 = txt;        
          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);
     }  
	 return true;
}   

/**  cookie类  **/
juuyou.common.obj.Cookie = function(){
	this.SetValue = function(name, value, hours, path, domain, secure){
		var str = new String();
    	var nextTime = new Date();
    	nextTime.setHours(nextTime.getHours() + hours);
    	str = name + "=" + escape(value);
    	if(hours)
     		str += ";expires=" + nextTime.toGMTString();
    	if(path)
			str += ";path=" + path;
		if(domain)
			str += ";domain=" + domain;
		if(secure)
			str += ";secure";
		document.cookie = str;
	}
	this.GetValue = function(name){
		var rs = new RegExp("(^|)" + name + "=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
    	if(tmp=rs)
			return unescape(tmp[2]);
    	return null;
	}
}

/**  刷新验证码图片  **/
juuyou.common.refreshValidImg = function(id){
	$obj(id).src = "http://www.saywo.com/includes/common/validCode.php?i=" + Math.random();
}

/**  赋值到对象  **/
juuyou.common.setDefaultVal = function(id, val){
	$obj(id).value = val;
}

/** 删除好友确认 **/
juuyou.common.deleteConfirm = function(message, url){
	if(confirm(message)){
		window.location = url;
		return true;
	}
	return false;
}

juuyou.common.isURL = function(str_url){
	var str_url=str_url.toLowerCase();
	var reghttp = /^http:\/\/saywo\.net/;
	if (reghttp.test(str_url)){
		return true; 
	}
	
	var reghttp=/^http\:\/\/\w+(\.\w+)*(\/\w+)*(\?\w+=\w*(&\w+=\w*)*)?$/g;	
	if (reghttp.test(str_url)){
		return true; 
	}else{ 
		return false; 
	}
}

/** 日期格式验证 **/
juuyou.common.isValidateDate = function(strValue) { 
	var objRegExp = /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
	
	if(!objRegExp.test(strValue)){
		return false; 
	}
	return true;
	
}

/** isIE **/
juuyou.common.isIE = function(){
	var d = document, w = window, na = navigator, ua = na.userAgent, i, nl, n, base, p, v;
	var isOpera = w.opera && opera.buildNumber;
	var isWebKit = /WebKit/.test(ua);

	return !isWebKit && !isOpera && (/MSIE/gi).test(ua) && (/Explorer/gi).test(na.appName);
}

/** isFirefox **/
juuyou.common.isFirefox = function(){
	if(navigator.userAgent.indexOf('Firefox') >= 0){
		return true;
	}else{
		return false;
	}
}

//选中下拉框值
juuyou.common.setSel = function(objId, strVal){  
	var objSel = $obj(objId); 
	for(var i=0; i<objSel.options.length; i++){                
		if(objSel.options[i].value == strVal){
			objSel.selectedIndex=i;
			break;
		}
	}
}

//添加声音提示
var blIsIE = juuyou.common.isIE();
var blNewSound = true;

juuyou.common.soundNewSay = function(){
	if(blIsIE){
	    var strVal = "<object classid=\"clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95\" class=\"j_sound_new_say\" width=\"0\" height=\"0\">";
	    strVal +="<param name=\"Filename\" value=\"http://img.saywo.com/images/sound/newSay.wav\"><param name=\"autostart\" value=\"true\"></object>";
	}else{
    	var strVal = "<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" WIDTH=\"0\" HEIGHT=\"0\" class=\"j_sound_new_say\"> <PARAM NAME=movie VALUE=\"http://img.saywo.com/images/sound/newSay.swf\"> <PARAM NAME=play VALUE=true> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src=\"http://img.saywo.com/images/sound/newSay.swf\" quality=high bgcolor=#FFFFFF WIDTH=\"0\" HEIGHT=\"0\"  ALIGN=\"\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\"> </EMBED> </OBJECT>";
	}

    $("body").append(strVal);

    setTimeout(function(){$(".j_sound_new_say").remove();}, 2000)
}

juuyou.common.newSoundControl = function(){
	blNewSound = !blNewSound;
}

//js生成Flash Html
juuyou.common.getFlashHtml = function(movieName, flashUrl, width, height, mode, settings){
	return ['<object id="', movieName, '" type="application/x-shockwave-flash" data="', flashUrl, '" width="', width, '" height="', height, '">',
	'<param name="wmode" value="', mode, '" />',
	'<param name="movie" value="', flashUrl, '" />',
	'<param name="quality" value="high" />',
	'<param name="menu" value="false" />',
	'<param name="allowScriptAccess" value="always" />',
	'<param name="flashvars" value="' + settings + '" />',
	'</object>'].join("");
}

//获取当前时间
juuyou.common.getNow = function(){
	var d=new Date()
	var day=d.getDate()
	var month=d.getMonth() + 1
	var year=d.getFullYear()
	var hour = d.getHours();
	var minute = d.getMinutes();
	var second = d.getSeconds();

	return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}











