/**
* @ ASP Javascript
* @ athor : wangchuyun@hotmail.com
* @ createdate		: 2008-03-24
* @ changehistory	: 2008-03-24
**/
 
function checkBoxAll(obj){
    var myform;
	myform = obj.form;
	for (var i=0;i<myform.elements.length;i++){
	   var e = myform.elements[i];
	   if (e != obj){
	       e.checked = obj.checked;
	   }
    }
}

//复制文本到剪贴板
function setCopy(_sTxt){
	if($.browser.msie) {
		clipboardData.setData('Text',_sTxt);
		alert ("网址“"+_sTxt+"”,已经复制到您的剪贴板中,您可以使用Ctrl+V快捷键粘贴到需要的地方");
	} else {
		prompt("请复制网站地址:",_sTxt); 
	}
}


//添加到收藏夹，支持IE和FIREFOX
function bookmarkit(url,title){
		if( document.all ) {
			window.external.AddFavorite( url, title);
		} else if (window.sidebar) {
			window.sidebar.addPanel(title, url,"");
		} else if( window.opera && window.print ) {
			return true;
		}
}


//弹出小窗口
function openWinUpload(url,w,h){
  	var targetName = "mhPop";
	var str_domain = document.location.href;		
	var arr_domain = str_domain.split("/");			
	url += "&base_domain="+arr_domain[2];		
	winOpen(url, targetName, w, h);
}

function winOpen(strUrl,strName,winWidth,winHeight,scrollBar,resize,winGubun,strProperty){
	if(!scrollBar) {scrollBar = "no";}
	if(!resize) {resize = "no";}
	if(!winGubun) {winGubun = "Normal";}
	if(!strProperty) {strProperty = "";}

	var x=screen.width/2 - winWidth/2;
	var y=screen.height/2 - winHeight/2;
	var opBrWin;

	if (winGubun == "" || winGubun == "Normal"){
		opBrWin = window.open(''+strUrl+'',''+strName+'','scrollbars='+scrollBar+',location=no'+',resizable='+resize+',width='+winWidth+',height='+winHeight+','+strProperty); }
		// (winGubun : Center)
		else{
			opBrWin = window.open(''+strUrl+'',''+strName+'','left='+x+', top='+y+', width='+winWidth+', height='+winHeight+',scrollbars='+scrollBar+',location=no'+',resizable='+resize+','+strProperty);
		}

		if(opBrWin){
			opBrWin.focus();
		}
		return(opBrWin);
}
 
function showFlashCode(id,file,width,height){
	var str='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '" align="middle">';
	str+='<param name="allowScriptAccess" value="sameDomain" />';
	str+='<param name="movie" value="'+file+'" />';
	str+='<param name="quality" value="high" />';
	str+='<param name="bgcolor" value="#ffffff" />';
	str+='<param name="wmode" value="Opaque">';
	str+='<embed src="'+file+'" swLiveConnect="true" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="'+id+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="opaque"></embed>';
	str+='</object>';
	return str;
}

/**
 * 初始化html form表单
 * formname		form表单 name的名称
 * inputname	form表单中 控件的name
 * initvalue	默认值
 */

function initForm(formname,inputname,initvalue){
	var objform = $("form[name='"+formname+"']");

	if(!objform){
		return;
	}
	
	var objinput_temp = $("[name="+inputname+"]");
	
	if(!objinput_temp || objinput_temp.length==0){		
		return;
	}
	var objinput = null;
	if(objinput_temp.length>1){
		objinput = new Array();
		objinput_temp.each(function(i){
			if($(this)[0].form.name==formname){
				objinput=$(this);
				initFormObject(objinput,initvalue);
			}			
		});
		
	}else{		
		objinput = objinput_temp;
		initFormObject(objinput,initvalue);
	}
	
}
/**
 * 初始化html form表单
 * objinput		input对象
 * initvalue	默认值
 */
function initFormObject(objinput,initvalue){
	
	if(!objinput){
		return;
	}
	
	var nodeName = objinput[0].nodeName;

	if( !(nodeName=="SELECT" || nodeName=="INPUT" ) ){
		return;
	}
	
	var isArrayInitValue = false;
	var initvalue_obj;
	if(typeof(initvalue)=='object' && initvalue.length){
		isArrayInitValue = true;
		initvalue_obj = initvalue;
	}
	try{
		initvalue_obj = eval(initvalue);
		if(typeof(initvalue_obj)=='object' && initvalue_obj.length){
			isArrayInitValue = true;
		}
	}catch(e){
	}


	objinput.each(function(i){
		var value = $(this).val();
		if($(this).attr("type")=="text" || 
			$(this).attr("type")=="password" ||
			$(this).attr("type")=="hidden"
			){
			$(this).val(initvalue);
			
		}else{

			if(isArrayInitValue){
				
				for(var j=0;j<initvalue_obj.length;j++){
					if( initvalue_obj[j] == value ){
						if($(this).attr("type")=="select"){
							$(this).attr("selected","true");		
						}else{
							$(this).attr("checked","true");								
						}						
					}
				} 
			}else{
				if( initvalue == value ){
					if($(this).attr("type")=="select"){
						$(this).attr("selected","true");		
					}else{
						$(this).attr("checked","true");								
					}
				}			
			}

		}
	}); 

	if(nodeName=="SELECT"){			
		for(var i=0;i<objinput[0].options.length;i++){
			var value = objinput[0].options[i].value;
			if(isArrayInitValue){				
				for(var j=0;j<initvalue_obj.length;j++){
					if( initvalue_obj[j] == value ){
						objinput[0].options[i].selected = true;															
					}
				} 
			}else{
				if( initvalue == value ){
					objinput[0].options[i].selected = true;
				}			
			}
		}
	}	 
}



/**
 * 消息框模块
 * @param options	
 * @return
 */
function jmessagebox(options){
	var _options = {
		msg      : '',
		errorid  : 0,
		callback : function(){},	//return true/false 来决定是否关闭弹出层，默认true		
		title    :'',
		width    : '520px',
		btn_yes  : '确定',
		btn_no   : '取消',
		btn_yes_show: true,
		btn_no_show : true			
	}
	var op = $.extend({}, _options, options);
	
	var id = Date.parse(new Date())+(new Date().getUTCMilliseconds());	
	var dhtml=
	'<div class="popup_box">'+
	'<table style="width:'+op.width+';">'+
	'<tr>'+
	'	<td class="body">'+
	'		<div class="header">'+
	'			<div class="title"><span class="b">'+op.title+'</span></div>'+
	'			<div class="link"><a href="javascript:void(0)" onclick="javascript:$.unblockUI();">关闭窗口</a></div>'+
	'			<div class="clear"></div>'+
	'		</div>'+
	'       <ul class="ladder"><li>' +
	'		<div class="caution_box">';
	if(op.errorid>0){
		dhtml += '<h1>' + op.msg + '</h1>';
	}else{
		dhtml += op.msg;
	}					
	dhtml+='</div><div class="clear"></div><div class="clear"></div><div>';
	if(op.btn_yes_show){
		dhtml+='<input type="button" class="submit" value="'+op.btn_yes+'" id=\"btn_yes_'+id+'\"/>';
	}
	if(op.btn_no_show){
		dhtml+='  <input type="button" class="submit" value="'+op.btn_no+'" onclick="javascript:$.unblockUI();" id=\"btn_no_'+id+'\"/>';
	}
	
	dhtml+='</div>'+
	'		</li></ul>'+
	'	</td>'+
	'</tr>'+
	'</table>'+
	'</div>';
	
	$.blockUI({ message: dhtml,css:{width:op.width} });	     
	
	$("#btn_yes_"+id).focus();

    $("#btn_no_"+id).click(function(){ 
		$.unblockUI();
	});
	
	$("#btn_yes_"+id).click(function(){        
		var close = true;//是否关闭弹出窗口
		if(typeof(op.callback)=='function'){
			if(op.callback() == false){
				close = false;				
			}
		}else{
			var func = ""; 
			if(op.callback){
				if(op.callback.indexOf('(')>0){
					func = (op.callback);
				}else{
					func = (op.callback+"()");
				}
				try{
					if(eval(func)==false){
						close = false;
					}
				}catch(e){					
				}				
			}
		}
		if(close){
			$.unblockUI();
		}
	});

	return false;
}

/**
 * 确认消息弹出窗口，替代confirm
 * @param message	信息内容
 * @param callback	点击确认以后的回调函数,return true/false觉得是否关闭弹出窗口
 * @return false; 接管一些onclick事件的触发，防止多次提交
 */
function jconfirm(message,callback){
	return jmessagebox({'msg':message,'callback':callback,'title':'是否确认'});
}



/**
 * 消息弹出窗口，替代alert
 * @param data	消息对象，如果是字符串直接输出，如果是对象需要 data.msg和data.errorid判断错误类型
 * @param callback	点击确认以后的回调函数,return true/false觉得是否关闭弹出窗口
 * @return false; 接管一些onclick事件的触发，防止多次提交
 */
function jalert(data,callback){
	var message = "";
	var errorid = 0;
	if(typeof(data)=='string'){
		message = data;
	}else{
		message = data.msg;
		errorid = data.errorid;
	}
	return jmessagebox({'msg':message,"errorid":errorid,'callback':callback,'title':'',btn_no_show:false});
}

/**
 * 获取某年某月都天数
 */ 
function getMonthDay(year,month){
	if(month < 1 || month > 12){
		return 0;
	}
	//日期随月份变化
	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var dayInt = 31;	
	if(2 == month){
		dayInt = ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28; 
	}else{
		dayInt = daysInMonth[month-1]; 
	}			
	return dayInt;
}


/**
 * 跨域提交from表单
 */
function ajaxFormCrossDomain(form, options) {
	//alert(options);
	var fn = document.forms[form] || $("#" + form)[0];
	
	if (! fn) {
		alert('form not exist');
		return;
	}
	
	var frameUpdate = fn.ownerDocument.createElement('iframe');
	var frameName = 'gotoframe' + new Date().getTime() + Math.ceil(999 * Math.random());
	frameUpdate.id = frameName;
	frameUpdate.name = frameName;
	frameUpdate.style.display = 'none';
	fn.ownerDocument.body.appendChild(frameUpdate);
	
	frameUpdate.contentWindow.name = frameUpdate.name;
	
	fn.target = frameUpdate.name;
	
	if (options && options.callback) {
		fn.callback.value = options.callback;
	}
	if (options && options.transfer) {
		fn.transfer.value = options.transfer;
	}
	if (options && options.post_url) {
		fn.action = options.post_url;
	}
	
	fn.submit();
}

/*
 *输入文件预览区域中同时显示
 * */
function showPreview(val, id) {
	var showObj = $("#"+id);
	if(typeof showObj == 'object') {
		//showObj.innerHTML = val.replace(/\n/ig, "<br />");
		showObj.empty().append(val.replace(/\n/ig, "<br />"));
	}
}

/**
 * 自动聚焦文本框以后，光标移动到文字最后。
 * @param obj 对象
 * @param pos 文本长度
 * @return
 */
function setFocusPosition(obj, pos) {
	if (obj.setSelectionRange) {
		setTimeout(function() {
			obj.setSelectionRange(pos, pos);
			obj.focus();
		}, 0);
	} else if (obj.createTextRange) {
		var rng = obj.createTextRange();
		rng.collapse(true);
		rng.moveStart('character', pos);
		rng.moveEnd('character', pos);
		rng.select();
	}
}

/**
 * 计算字符串长度，一个汉字算2个字符
 * @param s 字符串
 * @return
 */
function getStringLen(s) {
	   var m = s.match(/[^\x00-\xff]/ig);
	   return s.length + (m == null ? 0 : m.length);
}

function isEmail(s){
	return s.search(/^\s*[\w\~\-\.]+\@[\w\~\-]+(\.[\w\~\-]+)+\s*$/g) >= 0;
}

function isMobile(s){
	return /^(13\d{9})|(15[8-9]\d{8})$/.test(s);
}

function isNumber(s){
	return /^\d+$/.test(s);
}

function loadJS(url){
	$.ajax({
		  url: url,
		  cache: true,
		  dataType: "script"
	}); 
}

loadJS("http://bpimg.cn/resources/lib/jquery/jquery.bgiframe.js");