/*
 * javascript 下的基础函数
 * 说明：不用任何框架，纯javascript语法
 * 作者：DangFM 2010/06/15 DangFM@qq.com
 */


// 功能：加载文件
// 说明：filename文件路径，支持的类型filetype=js/css
// 事例：LoadFile("myscript.js", "js"); 动态载入.js 文件
function LoadFile(filename, filetype){
	if (filetype=="js"){ //如果是.js文件
		document.write('<script type="text/javascript" src="'+filename+'" language="javascript"></script>')
	}
	else if (filetype=="css"){ //如果是.css文件
		document.write('<link type="text/css" rel="stylesheet"  href="'+filename+'"  />')	
		}	
}

// 功能：检查文件是否已经加载
// 说明：filename 文件路径，filetype 支持js/css文件类型
// 返回：true/false
// 事例：CheckLoadFile("myscript.js", "js") //加载成功
function CheckLoadFile(filename, filetype){
	if (filesadded.indexOf("["+filename+"]")==-1){
		LoadJsCssFile(filename, filetype);
		FilesAdded += "["+filename+"]"; //从表单中加载的文件列	
		return true;
	}
	else
		return false;
}

// 功能：判断ie6浏览器函数
function IsIE6(){
	if (window.XMLHttpRequest) { // Mozilla, Safari,IE7
		return 'Mozilla, Safari,IE7 ';
	if (!window.ActiveXObject) { // Mozilla, Safari,
		return 'Mozilla, Safari';
	} else {
		return 'IE7';
	}
	} else {
		return 'IE6';
	}
	
}

// 功能：去左空格;
function ltrim(s){
	return s.replace( /^\s*/, "");
}
// 功能：去右空格;
function rtrim(s){
	return s.replace( /\s*$/, "");
}
// 功能：左右空格;
function trim(s){
	return rtrim(ltrim(s));
}

// 功能：鼠标坐标
// 说明：需设置全局变量保存鼠标xy轴坐标  mouseX,mouseY
function mouseMove(ev) 
{ 
	ev= ev || window.event; 
	var mousePos = mouseCoords(ev); 
	//alert(ev.pageX); 
	mouseX = mousePos.x;
 	mouseY = mousePos.y;
}

function mouseCoords(ev) 
{ 
	if(ev.pageX || ev.pageY){ 
	return {x:ev.pageX, y:ev.pageY}; 
	} 
	return { 
//	x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
//	y:ev.clientY + document.body.scrollTop - document.body.clientTop 
	x:ev.clientX + document.body.clientLeft,
	y:ev.clientY + Math.max(document.body.scrollTop, document.documentElement.scrollTop)
	}; 
} 

// 功能：获取参数
function request(paras){ 
	var url = location.href;   
	var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");   
	var paraObj = {}   
	for (i=0; j=paraString[i]; i++){   
	paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf 
	("=")+1,j.length);   
	}   
	var returnValue = paraObj[paras.toLowerCase()];   
	if(typeof(returnValue)=="undefined"){   
	return "";   
	}else{   
	return returnValue;   
	}   
}

// 功能：日期减法函数
// 说明：日期格式 dd=12/23/2009,dadd=减的天数
function subdate(dd,dadd) 
{ 
	//file＆#58//可以加上错误处理 
	var a = new Date(dd) 
	a = a.valueOf() 
	a = a - dadd * 24 * 60 * 60 * 1000 
	a = new Date(a) 
	var b =  (a.getMonth() + 1) + "/" + a.getDate() + "/" + a.getFullYear()
	return b
}

// 功能：替换函数
function ReplaceAll(str, sptr, sptr1){
	var newValue = str.replace(new RegExp(sptr,"gm"),sptr1);
	return newValue
}

// 功能：二级菜单导航函数
// 说明：ulId 为ul结构的ID名称,classname用来显示二级菜单的类名
function menuFix(ulId,classname) {
	var sfEls = document.getElementById(ulId).getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=(this.className.length>0? " ": "") + classname;
		}
		sfEls[i].onMouseDown=function() {
			this.className+=(this.className.length>0? " ": "") + classname;
		}
		sfEls[i].onMouseUp=function() {
			this.className+=(this.className.length>0? " ": "") + classname;
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp("( ?|^)"+classname+"\\b"), "");
		}
	}
}

// 功能：通用表单验证函数
// 说明：通用验证 姓名username，邮件email，电话phone
function form_check(formID,Clname){
		var username = $(formID).find("#username");
		var email = $(formID).find("#email");
		var phone = $(formID).find("#phone");
		var city = $(formID).find("#city");
		var nationality = $(formID).find("#nationality");
		
		username.removeClass(Clname);
		email.removeClass(Clname);
		phone.removeClass(Clname);
		if(trim(username.val())=="" || rName.test(username.val())==false){
			username.addClass(Clname);
			username.focus();
			return false;
			}
		if(trim(email.val())=="" || rEmail.test(email.val())==false){
			email.addClass(Clname);
			email.focus();
			return false;
			}
		if(trim(phone.val())=="" || rPhone.test(phone.val())==false){
			phone.addClass(Clname);
			phone.focus();
			return false;
			}
		if(trim(city.val())==""){
			city.addClass(Clname);
			city.focus();
			return false;
			}
		if(trim(nationality.val())==""){
			nationality.addClass(Clname);
			nationality.focus();
			return false;
			}
		
		return true;
}

//功能：面板切换函数 ShowTablePanel
//参数：hClass 所在集合,thisid当前元素ID,sClass,当前类,contentclass对于内容类
//作用：实现tablepanel功能
//说明：此函数只能用在按照顺序对应的集合块中
function ShowTablePanel(hClass,thisid,sClass,contentclass){
	//$(hClass).hide();
	var cid=0;
	$(hClass).each(function(i){
		if($(this).text() == $(thisid).text()){
			cid = i; //循环找到当前活动菜单的集合ID值
		}
			
	})

	$(hClass).removeClass(sClass)
	$(thisid).addClass(sClass)
	$(thisid).show(); //显示菜单
	
	//寻找菜单集合ID对应的内容集合
	$(contentclass).hide();
	$(contentclass).eq(cid).show();
	
}

// 功能：锁定屏幕
// 说明：str,所要显示的字符串或标签信息
function sAlert(str){
	var msgw,msgh,bordercolor;
	msgw=0;//提示窗口的宽度
	msgh=0;//提示窗口的高度
	titleheight=0 //提示窗口标题高度
	bordercolor="#cccccc";//提示窗口的边框颜色
	titlecolor="#efefef";//提示窗口的标题颜色
	
	var sWidth,sHeight;
	sWidth=screen.width;
	sHeight=screen.height;
	
	var bgObj=document.createElement("div");
	bgObj.setAttribute('id','bgDiv');
	bgObj.style.position="absolute";
	bgObj.style.top="0";
	bgObj.style.background="#cccccc";
	bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
	bgObj.style.opacity="0.6";
	bgObj.style.left="0";
	bgObj.style.width=sWidth + "px";
	bgObj.style.height=sHeight + "px";
	bgObj.style.zIndex = "10000";
	document.body.appendChild(bgObj);
	
	var msgObj=document.createElement("div")
	msgObj.setAttribute("id","msgDiv");
	msgObj.setAttribute("align","center");
	msgObj.style.background="white";
	msgObj.style.border="1px solid " + bordercolor;
	msgObj.style.position = "absolute";
	msgObj.style.left = "50%";
	msgObj.style.top = "50%";
	msgObj.style.font="11px Arial, Tahoma, Verdana, sans-serif";
	msgObj.style.marginLeft = "-225px" ;
	msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
	msgObj.style.width = msgw + "px";
	msgObj.style.height =msgh + "px";
	msgObj.style.textAlign = "center";
	msgObj.style.lineHeight ="25px";
	msgObj.style.zIndex = "10001";
	
	var title=document.createElement("h4");
	title.setAttribute("id","msgTitle");
	title.setAttribute("align","right");
	title.style.margin="0";
	title.style.padding="3px";
	title.style.background=bordercolor;
	title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
	title.style.opacity="0.75";
	title.style.border="1px solid " + bordercolor;
	title.style.height="18px";
	title.style.font="11px Arial, Tahoma, Verdana, sans-serif";
	title.style.color="white";
	title.style.cursor="pointer";
	title.innerHTML="";
	title.onclick=function(){
	document.body.removeChild(bgObj);
	document.getElementById("msgDiv").removeChild(title);
	document.body.removeChild(msgObj);
	}
	document.body.appendChild(msgObj);
	document.getElementById("msgDiv").appendChild(title);
	var txt=document.createElement("p");
	txt.style.margin="1em 0"
	txt.setAttribute("id","msgTxt");
	txt.innerHTML=str;
	document.getElementById("msgDiv").appendChild(txt);
	return bgObj
}
function CloseS(bgObj){
	$("#bgDiv").remove()
	$("#msgDiv").remove()
}


function pagenext(screenid,pi,type){
//统计离它最近的Ol的项目数
		//设置总页数
		var typecss="top"
		var screenheight = $(screenid).height()
		if(type=="width"){
			 screenheight = $(screenid).width()
			 typecss="left"
			}
		var tolpage //li 的总数,也即page总数
		var liheight = 0
		var tolli = 1
		//统计li的总高度
		$(screenid).find("ol:visible li").each(function(i){
			
				 //循环找到当前活动菜单的集合ID值	
				if(type=="width"){
					liheight += $(this).width()
					}
				else{
					liheight += $(this).height();
					}
				tolli = i
		})
		//alert(liheight/screenheight)
		tolpage = liheight/screenheight
		if(type=="height"){
		tolpage = tolpage * 2
		}
		tolpage = Math.ceil(tolpage)
		
		//当前页码
		
		var page = $(screenid).parent().find("div:visible").next(".next_page").eq(0).attr("title")
		if(page=="" || page==null) page=1
		
		page = parseInt(page) + pi
		if (page<=1) page=1
		if (page>tolpage) page=1
		
		$(screenid).parent().find("div:visible").next(".next_page").eq(0).html(page+" of "+tolpage)
		$(screenid).parent().find("div:visible").next(".next_page").eq(0).attr("title",page)
		if(type=="height"){
			$(screenid).parent().find("div:visible").next(".next_page").eq(0).html((tolli+1))		
			
		}
		
		//移动显示区域\
		//alert(screenheight)
		return (page-1)*screenheight

	//alert(liheight+'/'+screenheight+'='+tolpage)
}
function movepage(screenid,type,space){
		if(type=="width"){
			  $(screenid).find("ol:visible").animate({"left" : -(space)+'px'}, 500);
			}
		else{
			 $(screenid).find("ol:visible").animate({"top" : -(space/2)+'px'}, 500);
			}
}

//读取文件源代码
function GetPageCode(strPath){
	var temphtml='';
	if(strPath!=""){
		//alert(strPath)
		temphtml = $.ajax({
			url:strPath,
			type:"GET",
			dataType:"html",
			async:false
			
		})
	}

	return temphtml.responseText;
}


var delay=function(t,fn){
	 //接收两个参数 t延迟时间秒为单位，fn要执行的函数     
	 var _this=this,//请注意还要个免费的this参数可以让这个delay更完美          
	 d=setInterval(function(){                                       
							fn.apply(_this);                                       
							},t*1000);                
	 _this.onmouseout=function(){                        
	 clearInterval(d);                    
	 };  
	 clearInterval(d); 
}



//写cookies函数 作者：翟振凯
function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
    var Days = 30; //此 cookie 将被保存 30 天
    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 arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function DelCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=GetCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}


//图片马赛克切换效果
//说明：图片需加  style="FILTER: progid:DXImageTransform.Microsoft.Pixelate(MaxSquare=1,Duration=1);"

function aspphp_zoom(img){
	if(img && img.filters && img.filters[0]){
	img.filters[0].apply();
	img.filters[0].play();}
}

function aspphp_over(img){
	if(img && img.filters && img.filters[0]){
	img.filters[0].apply();
	img.filters[0].play();}
}



//复制到剪切板按钮
var clip = new Array();
function copyfunc(id,thisid,thino,IsTrue) {
	
	clip[thino] = new ZeroClipboard.Client();
	clip[thino].setHandCursor( true );
	if(id!=''){
	clip[thino].setText( $('#'+id).val() );
	if(IsTrue!=0){alert("Copy Success!");}
	clip[thino].addEventListener('mouseOver', function (client) {
		clip[thino].setText( $('#'+id).val() );
	});
}
	clip[thino].glue( thisid );
	
	
}

