/*
	[FNB] (C) 2009-2010 FNB Inc.
	$Id: script_common.js 8541 2009-08-21 02:34:18Z rubio $
*/

var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

//创建一个对象
function addSort(obj) {
	if (obj.value == 'addoption') {
 	var newOptDiv = document.createElement('div')
 	newOptDiv.id = obj.id+'_menu';
 	newOptDiv.innerHTML = '<h1>添加</h1><a href="javascript:;" onclick="addOption(\'newsort\', \''+obj.id+'\')" class="float_del">删除</a><div class="popupmenu_inner" style="text-align: center;">名称：<input type="text" name="newsort" size="10" id="newsort" class="t_input" /><input type="button" name="addSubmit" value="创建" onclick="addOption(\'newsort\', \''+obj.id+'\')" class="button" /></div>';
 	newOptDiv.className = 'popupmenu_centerbox';
 	newOptDiv.style.cssText = 'position: absolute; left: 50%; top: 200px; width: 400px; margin-left: -200px;';
 	document.body.appendChild(newOptDiv);
 	$('newsort').focus();
 	}
}

//添加选项
function addOption(sid, aid) {
	var obj = $(aid);
	var newOption = $(sid).value;
	$(sid).value = "";
	if (newOption!=null && newOption!='') {
		var newOptionTag=document.createElement('option');
		newOptionTag.text=newOption;
		newOptionTag.value="new:" + newOption;
		try {
			obj.add(newOptionTag, obj.options[0]); // doesn't work in IE
		} catch(ex) {
			obj.add(newOptionTag, obj.selecedIndex); // IE only
		}
		obj.value="new:" + newOption;
	} else {
		obj.value=obj.options[0].value;
	}
	// Remove newOptDiv
	var newOptDiv = document.getElementById(aid+'_menu');
	var parent = newOptDiv.parentNode;
	var removedChild = parent.removeChild(newOptDiv);
}

//检查选项
function checkAll(form, name) {
	for(var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];
		if(e.name.match(name)) {
			e.checked = form.elements['chkall'].checked;
		}
	}
}

//encodeURI
function cnCode(str) {
	return is_ie && document.charset == 'utf-8' ? encodeURIComponent(str) : str;
}

//是否找到对象
function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

//字符串是否存在指定数组
function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

//验证码
function seccode() {
	var img = 'do.php?ac=seccode&rand='+Math.random();
	document.writeln('<img id="img_seccode" src="'+img+'">');
}
function updateseccode() {
	var img = 'do.php?ac=seccode&rand='+Math.random();
	if($('img_seccode')) {
		$('img_seccode').src = img;
	}
}

//缩小图片并添加链接
function resizeImg(id,size) {
	var theImages = $(id).getElementsByTagName('img');
	for (i=0; i<theImages.length; i++) {
		theImages[i].onload = function() {
			if (this.width > size) {
				this.style.width = size + 'px';
				if (this.parentNode.tagName.toLowerCase() != 'a') {
					var zoomDiv = document.createElement('div');
					this.parentNode.insertBefore(zoomDiv,this);
					zoomDiv.appendChild(this);
					zoomDiv.style.position = 'relative';
					zoomDiv.style.cursor = 'pointer';
					
					this.title = '点击图片，在新窗口显示原始尺寸';
					
					var zoom = document.createElement('img');
					zoom.src = 'image/zoom.gif';
					zoom.style.position = 'absolute';
					zoom.style.marginLeft = size -28 + 'px';
					zoom.style.marginTop = '5px';
					this.parentNode.insertBefore(zoom,this);
					
					zoomDiv.onmouseover = function() {
						zoom.src = 'image/zoom_h.gif';
					}
					zoomDiv.onmouseout = function() {
						zoom.src = 'image/zoom.gif';
					}
					zoomDiv.onclick = function() {
						window.open(this.childNodes[1].src);
					}
				}
			}
		}
	}
}

//Ctrl+Enter 发布
function ctrlEnter(event, btnId, onlyEnter) {
	if(isUndefined(onlyEnter)) onlyEnter = 0;
	if((event.ctrlKey || onlyEnter) && event.keyCode == 13) {
		$(btnId).click();
	}
}
//缩放Textarea
function zoomTextarea(id, zoom) {
	zoomSize = zoom ? 10 : -10;
	obj = $(id);
	if(obj.rows + zoomSize > 0 && obj.cols + zoomSize * 3 > 0) {
		obj.rows += zoomSize;
		obj.cols += zoomSize * 3;
	}
}

//复制URL地址
function setCopy(_sTxt){
	if(is_ie) {
		clipboardData.setData('Text',_sTxt);
		alert ("网址“"+_sTxt+"”\n已经复制到您的剪贴板中\n您可以使用Ctrl+V快捷键粘贴到需要的地方");
	} else {
		prompt("请复制网站地址:",_sTxt); 
	}
}

//检查图像URL
function checkImage(url) {
	var re = /^http\:\/\/.{5,200}\.(jpg|gif|png)$/i
	return url.match(re);
}

function quick_op_close() {
	$('quick_box').style.display = 'none';
}

//填写生日
function showbirthday(thday,thmon,thyear){
	var birthday = document.getElementById(thday);
	var birthmonth = document.getElementById(thmon);
	var birthyear = document.getElementById(thyear);
	birthday.length=0;
	birthday.options.add(new Option('請選擇', 0));
	for(var i=0;i<28;i++){
		birthday.options.add(new Option(i+1, i+1));
	}
	if(birthmonth.value!="2"){
		birthday.options.add(new Option(29, 29));
		birthday.options.add(new Option(30, 30));
		switch(birthmonth.value){
			case "1":
			case "3":
			case "5":
			case "7":
			case "8":
			case "10":
			case "12":{
				birthday.options.add(new Option(31, 31));
			}
		}
	} else if(birthyear.value!="") {
		var nbirthyear=birthyear.value;
		if(nbirthyear%400==0 || nbirthyear%4==0 && nbirthyear%100!=0) birthday.options.add(new Option(29, 29));
	}
}

//字符串长度
function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}

//去除前后空格
function trim(s){
	return s.replace(/(^\s*)|(\s*$)/g, "");  
}

//正则表达式匹配
function regex(str,re) {
	return str.match(re);
}

//获取验证码
function imgCodev() {
	var source = "/imgcode/Img/code/" + Math.random();
	var pic = document.getElementById("verifyImg");
	pic.setAttribute("src", source);
}

//转换成大写
function toupper(id){
	var oo = $("#"+id).val().toUpperCase();
	$("#"+id).val(oo);
}

//判断出生日期是否正确
function isValidDate(strDateToChk){   
	var dtToChk = new Date(strDateToChk);   
	var strYear = dtToChk.getFullYear();   
	var strMonth = dtToChk.getMonth() + 1 + "";  
	var strDate = dtToChk.getDate(); 
	var strTheDate = strYear + "/" + strMonth + "/" + strDate;   
	
	if (strTheDate!=strDateToChk){   
		return   false; 
    }   
	return   true;     
}