

/**
 * 绑定AJAX登录及验证事件
 */
function bindValidate(){
	$(loginForm).validate({
		//验证规则
		rules:{
			userName:{
				required:true,//必须字段
				minlength:4	 //至少4字节
			},
			password:{
				required:true,
				minlength:6	 //至少6字节
			}
		},
		//错误消息
		messages:{
			userName: {
				required: "用户名必须填写",
				minlength: jQuery.format("名称至少{0}位")
			},
			password: {
				required: '密码必须填写',
				minlength: jQuery.format("密码至少{0}位")
			}
		},		
		submitHandler: function(form) {
			form.submit();
			//ajax提交表单
//			var userName = $("#userName").val();
//			var password = $("#password").val();
//			var params={"userName":userName,"password":password};
//			var method=$(form).attr("method")||"POST";
//			var action=$(form).attr("action")||"login!login";
//			$.ajax({
//		   		type: method,
//		   		url: action,
//		   		data: params,
//		   		success: function(msg){
//		     			$("#loginPanel").replaceWith(msg);
//		     			if($("#loginButton").size()>0){
//		     				alert("登录失败,用户名或密码错误！");
//		     				bindValidate();
//		     				//$("#loginButton").bind("click",login);
//		     			}
//		   		}
//			});
//			return false;
		},
		errorPlacement:function(error, element) {
			//放置错误提示消息的Element
			error.appendTo(element.parent());
		},
		errorClass:'fail',//错误消息样式
		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		}
	});	
};

function bindRegist(){
	// 输入框获得焦点时，样式设置 
	$('input').focus(function(){ 
	  if($(this).is(":text") || $(this).is(":password")) 
	   $(this).addClass('focus');
	}); 

	// 输入框失去焦点时，样式设置 
	$('input').blur(function() { 
	  $(this).removeClass('focus'); 
	});
	
$("#userMsg").validate({    
/* 设置验证规则 */    
  rules: {    
   userName: {    
    required: true,        
    byteRangeLength: [4,20],
    hasUser:"#userName"
   },    
   password: {    
    required: true,
    passwordval: [6,16]   
   },    
   password1: {    
    required: true, 
    passwordval: [6,16],
    equalTo: "#password"    
   },      
   email: {    
    required: true,    
    email: true,
    hasEmail: "#email"    
   },
   email1:{
   	required: true,
   	email: true,    
   	equalTo:"#email"    
   },
   code:{
   	required:true,
   	veryCode:"#code"
   } 
  },    
/* 设置错误信息 */    
  messages: {    
   userName: {    
    required: "请填写用户名",    
    byteRangeLength: "( 4-20个字符(汉字、字母、数字),1个汉字为2个字符。 )",
    hasUser:"该用户已经被占用"    
   },    
   password: {    
    required: "请填写密码",    
    byteRangeLength:"( 密码由6-16个字符组成。 )"
   },    
   password1: {    
    required: "请填写确认密码",    
    equalTo: "两次密码输入不相同"    
   },   
   email: {    
    required: "请输入一个Email地址",    
    email: "请输入一个有效的Email地址",
    hasEmail:"该电子邮件已经被注册过"    
   },
   email1:{
   	required:"请再次输入电子邮件",
   	email: "请输入一个有效的email地址",
   	equalTo:"两次电子邮件输入不相同"
   },
   code:{
   	required:"请输入验证码",
    veryCode:"验证码错误"
   }    
  },  
   /* 错误信息的显示位置 */    
	  errorPlacement: function(error, element) {
	   element.next().next().replaceWith(error);    
	  },    
	/* 验证通过时的处理 */    
	  success: function(label) {    
	   // set   as text for IE    
	   label.html("验证成功").addClass("checked");    
	  }, 
	  errorClass:'errorCss',//错误消息样式
		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
	  },   
	/* 获得焦点时不验证 */    
	  focusInvalid: false,    
	  onkeyup: false    
	}); 
}

/**
 * 高级注册
 */
function bindRegist2(){
	$("#regist2").validate({    
	/* 设置验证规则 */    
  rules: {    
    firstName: {    
    required: true,     
    byteRangeLength: [1,16]
   },    
   lastName: {    
    required: true,    
    byteRangeLength: [1,16]    
   },    
   postalCode: {    
    required: true,    
    isZipCode:true
   },
   phoneNumber:{
   	isPhone:true
   },
   mobileNumber:{
   	required:true,
   	isMobile:true
   },
   address:{
   	required:true,
   	byteRangeLength:[1,64]
   },
   idNumber:{
	required:true,
	isIdCard:true
   }
  },    
/* 设置错误信息 */    
  messages: {    
   firstName: {    
    required: "请填姓",    
    byteRangeLength:"( 最多不能超过16个字符,1个汉字为2个字符。 )"  
   },    
   lastName: {    
    required: "请填写名称",    
    byteRangeLength:"( 最多不能超过16个字符,1个汉字为2个字符。 )"
   },    
   postalCode: {    
    required: "请填写邮政编码",    
    isZipCode: "请填写正确格式的邮政编码"    
   },   
   phoneNumber: {
    isPhone:"请填写正确格式的电话.例如: 0571-87770333"
   },
   mobileNumber:{
   	required:"请填写手机号码",
   	isMobile:"请填写正确的手机号码"
   },
   address:{		
   		required:"请填写联系地址",
   		byteRangeLength:"地址最多不可超过64个字符"
   },
   idNumber:{
		required:"请填写你的身份证号码",
		isIdCard:"请填写正确的身份证号码  15位或者18位"
	   }
  },  
   /* 错误信息的显示位置 */    
	  errorPlacement: function(error, element) {
	  // error.appendTo( element.parent().next());
	   $('a',element.parent().next()).replaceWith(error);
	   
	  },    
	/* 验证通过时的处理 */    
	  success: function(label) {    
	   // set   as text for IE    
	   label.html("验证成功").addClass("checked");    
	  }, 
	  errorClass:'errorCss',//错误消息样式
		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
	  },   
	/* 获得焦点时不验证 */    
	  focusInvalid: false,    
	  onkeyup: false    
	}); 	
		// 输入框获得焦点时，样式设置 
	$('input').focus(function(){ 
	  if($(this).is(":text") || $(this).is(":password")) 
	   $(this).addClass('focus'); 
	}); 

	// 输入框失去焦点时，样式设置 
	$('input').blur(function() { 
	  $(this).removeClass('focus'); 
	});
	
	$('#province').change(function(){
		$('#cityOpt').empty();
		var opt = $.ajax({
 			 url: "login!changeCity",
 			 data:{"province":$('#province').val()},
  			 async: false
 			}).responseText; 
 		$(opt).appendTo($('#cityOpt'));
	});
}

/**
 * 拍品收藏
 * @param uname
 * @param goodId
 * @param companyId
 * @param sn
 * @return
 */
function track(goodId,companyId,sn){
	var error = '对不起，网站正在维护，请稍后收藏!';
	$.ajax({type:"GET",
			url:"good!track",
			dataType:"text",
			async:false,
			cache:false,
			data:{goodId:goodId,companyId:companyId},
			success:function(msg){
				msg=parseInt(msg);
				if(msg==1)
					alert('第'+sn+'号拍品收藏成功');
				else if(msg==-1)
					alert('第'+sn+'号拍品已经收藏');
				else alert('请您先登陆后，再做此操作');
				},
			error:function(){alert(error);}});
}

/**
 * 参拍登记
 * @return
 */
function inComeAuctionUser(companyId,auctionId){
	var error = '对不起，网站正在维护，请稍后登记!';
	var loginMsg = '请您先登陆,在做此操作！';
	if(companyId=='-') {alert(error);return;}
	if(auctionId=='-') {alert(error);return;}
	$.ajax({type:"GET",
			url:"auction!registAuctionUser",
			dataType:"text",
			async:false,
			cache:false,
			data:{companyId:companyId,aId:auctionId},
			success:function(msg){
				msg = parseInt(msg);
				if(msg==1)alert('参拍登记成功！');
				else if(msg == 2) alert(loginMsg);
				else if(msg == -1) alert('您已经参拍登记过，请耐心等待审核消息');
				else if(msg == 0) alert('参拍登记失败，请重新登记');
				else if(msg == 3) alert('恭喜你,你的申请已经通过审核,不需要再登记了！');
			},
			error:function(){alert(error);}
			});
}

 var tt=false;
//身份证号码验证 15或18支持末尾为X身份证号码 
jQuery.validator.addMethod("isIdCard", function(value, element) {  
  return this.optional(element) || (/(^\d{15}$)|(^\d{17}([0-9]|x|X)$)/.test(value));    
}, "请正确填写您的身份证号码 15位或者18位");  		
	//手机号码验证    
jQuery.validator.addMethod("isMobile", function(value, element) {    
  var length = value.length;
  return this.optional(element) || (/^(((1[358][0-9]{1}))+\d{6,9})$/.test(value));    
}, "请正确填写您的手机号码");
  
// 电话号码验证    
jQuery.validator.addMethod("isPhone", function(value, element) {    
  var tel = /^(\d{3,4}-?)?\d{7,9}$/;    
  return this.optional(element) || (tel.test(value));    
}, "请正确填写您的电话号码");    

// 邮政编码验证    
jQuery.validator.addMethod("isZipCode", function(value, element) {    
  var tel = /^(\d{5,6})|(\d{3}-\d{4})$/;    
  return this.optional(element) || (tel.test(value));    
}, "请正确填写您的邮政编码");   

jQuery.validator.addMethod("hasUser", function(value, element, userName){
   $.ajax({
   type: "POST",
   url: "login!hasUser",
   async: false,
   data:{"userName":$(userName).val()},
   success: function(state){
     if(state==0){//
     	tt= false;
     }else if(state==1){
     	tt= true;
     }else {tt= false}
   }
   
}); return tt;
},"用户是否存在");

jQuery.validator.addMethod("hasEmail", function(value, element, email){
   $.ajax({
   type: "POST",
   url: "login!hasEmail",
   async: false,
   data: {"email":$(email).val()},
   success: function(state){
     if(state==0){//
     	tt= false;
     }else if(state==1){
     	tt= true;
     }else {tt = false}
   }
}); return tt;
},"电子邮件是否可用");

jQuery.validator.addMethod("veryCode", function(value, element, code){
   $.ajax({
   type: "POST",
   url: "login!veryCode",
   async: false,
   data: {"code":$(code).val()},
   success: function(state){
     if(state==0){//
     	tt= false;
     }else if(state==1){
     	tt= true;
     }else {tt = false}
     
     if(!tt){
     	changeImg('codeImg');
     }
   }
}); return tt;
},"电子邮件是否可用");

// 中文字两个字节    
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {    
  var length = value.length;    
  for(var i = 0; i < value.length; i++){    
   if(value.charCodeAt(i) > 127){    
    length++;    
   }    
  }
  return this.optional(element) || ( length >= param[0] && length <= param[1] );    
}, "请确保输入的值在6-20个字节之间(一个中文字算2个字节)");    
 
//密码验证
jQuery.validator.addMethod("passwordval", function(value, element) {    
  return this.optional(element) || /^([a-zA-Z0-9]|[!@#$%^&*_.]){6,16}$/.test(value);    
}, "请输入6-16位英文字母、数字和符号  符号只能包括:(!@#$%^&*_.)");

// 字符验证    
jQuery.validator.addMethod("userName", function(value, element) {    
  return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);    
}, "用户名只能包括中文字、英文字母、数字和下划线");



/**
 * Tab切换及事件绑定，在document装入时运行。
 * _divClass:包含ul li的div的css样式
 * nav_on_class:Tab选中时的css样式
 * nav_on_class:Tab未选中时的css样式
 */
function bindTabChange(_divClass,nav_on_class,nav_off_class){
	$("."+_divClass+" > ul > li").each(function(){
		$(this).bind("mouseover",function(){
			$(this).siblings().each(function(){
				$(this).removeClass(nav_on_class).addClass(nav_off_class);
				$("#"+$(this).attr("ref")).css("display","none");
			});
			$(this).removeClass(nav_off_class).addClass(nav_on_class);
			$("#"+$(this).attr("ref")).css("display","block");
		});
	});
}


function search(_id){
	var _value=encodeURIComponent(encodeURIComponent($('#keyword').val()));	
	var message="";
	var url="";	
	if(_id.toLowerCase().indexOf("auctions")!=-1){
		url="fullSearch!searchAuctions";
		message="拍卖会";
	}
	if(_id.toLowerCase().indexOf("goods")!=-1){
		url="fullSearch!searchGoods";
		message="拍品";
	}
	if(_id.toLowerCase().indexOf("news")!=-1){
		url="fullSearch!searchNews";
		message="资讯";
	}	
	
	if(_value!="")
	{
		window.open(url+"?keyword="+_value,'window','');		
	}else{
		alert("请输入"+message+"搜索条件!");
		$('#keyword').focus();
	}
}

function searchKeyWord(keyWord){	
	var value=encodeURIComponent(encodeURIComponent(keyWord));
	window.open("news!search?keyword="+value,'','');
}

function searchChannelGoods(keyWord){
	var value=encodeURIComponent(encodeURIComponent(keyWord));
	window.open("fullSearch!searchGoods?keyword="+value,'','');
}

function seachNews(){
	var key = jQuery.trim($('#key').val());
	if(key == '')
	{
		alert('请输入资讯搜索条件');
		return false;
	}
	return true;
}

function seachGoods(){
	var key = jQuery.trim($('#g_name').val());
	if(key == ''){
		alert('请输入拍品搜索条件');
		return false;
	}
	return true;
}

function seach(arg){
	if(arg==null || arg == undefined) return false;
	if(typeof(arg)=='string' && arg=='good'){
		var gname = document.getElementById('gname').value;
		if($.trim(gname)==null || $.trim(gname)==''){
			alert('请输入拍品关键字！');
			document.getElementById('gname').focus();
			return false;
		}
		return true;
	}
	return false;
}

/**
 * 加入收藏
 */
function addBookMark(url,name){
	if (document.all){//IE
       window.external.addFavorite(url,name);
    }else if (window.sidebar){//ff
       window.sidebar.addPanel(name,url,"");
 	}else{
 		alert("你的浏览器不支持此事件！");
 	}
}

/**
 * 设为首页
 */
function setHomepage(url){
	if (document.all){
		document.body.style.behavior='url(#default#homepage)';
  		document.body.setHomePage(url);
	}else if (window.sidebar){
    	if(window.netscape){
			try{  
            	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
         	}catch (e){  
    			alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );  
         	}
    	}else{
    		alert("你的浏览器不支持此事件！");
    	}
	    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
	    prefs.setCharPref('browser.startup.homepage',url);
	}else{
		alert("你的浏览器不支持此事件！");
	}
}

/**
 * 复制Url
 * @return
 */
function copy_code() 
{
 	var copyText=window.document.location.href;  
     if (window.clipboardData) 
     {
        window.clipboardData.setData("Text", copyText);
     } 
     else
     {
         var flashcopier = 'flashcopier';
       
         if(!document.getElementById(flashcopier)) 
         {              
           var divholder = document.createElement('div');
           divholder.id = flashcopier;
           document.body.appendChild(divholder);
         }
         document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="images/_clipboard.swf" FlashVars="clipboard='+copyText+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;          
     }
     alert("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友!");
 }

/**
 * 获取焦点 预览拍品图片效果
 * @param event
 * @param picid
 * @return
 */
function showDiv(event,picid,catName) { 
	var url = '';
	if(picid!=null && picid!=''){
		url = "http://artimg.pmjj.com/s_AuctionSceneGoods/"+picid;
	 }else if(catName !=null && catName!=''){
		url = "/images/dfImg/"+catName+".gif"; 
	 }else {return ;}
	 var newDiv = document.getElementById("auctionShow");
    	 newDiv.style.display="block"; 
    	 newDiv.style.left=event.clientX+20+document.body.parentNode.scrollLeft+"px";
    	 newDiv.style.top=event.clientY-100+document.body.parentNode.scrollTop+"px"; 
    	 newDiv.style.position="absolute"; 
    	 newDiv.innerHTML="<img src='"+url+"' scrolling='no' height='80' width='120' />" ;
 }

/**
 * 获取焦点 预览拍品图片效果
 * @param event
 * @param picid
 * @return
 */
function showMapDiv(event,picid) {
	var newDiv = document.getElementById("auctionShow");
    newDiv.style.display="block"; 
	newDiv.style.left=event.clientX+20+document.body.parentNode.scrollLeft+"px";
    newDiv.style.top=event.clientY-100+document.body.parentNode.scrollTop+"px"; 
    newDiv.style.position="absolute";  
    // document.getElementById("iframeShow").src="auctionlist_bf.php?picid="+picid+"&w=150&h=150";
	newDiv.innerHTML="<iframe frameborder='0' src='"+picid+"' scrolling='no' height='400' width='450' ></iframe>";	
    }
/**
 * 失去焦点 图片关闭
 * @return
 */
 function closeDiv(){
	   var newDiv = document.getElementById("auctionShow");
    newDiv.style.display="none"; 
  }
  
 /**
  * 刷新验证码
  */
function changeImg(_id)
{
	var obj=document.getElementById(_id);
	obj.src='common/image.jsp?'+(new Date().getTime().toString(36));	
	return false;
}

function showPrew(){
	 var prev = new Array('prewTimeTab','prewAreaTab','prewSceneTab');
	 var vewTable = new Array('date','area','scene');
	 var oUl = $('#prewUL0').children();
	 oUl.each(function(){
		 $(this).mouseover(function(){
			 oUl.removeClass('p_y_l1');
			 $(this).addClass('p_y_l1');
			 for(var i=0;i<prev.length;i++){
				 $('#'+prev[i]).hide();
				 $('#'+vewTable[i]).hide();
			 }
			$('#'+prev[oUl.index($(this))]).show();
			$('#'+vewTable[oUl.index($(this))]).show();
			if(oUl.index($(this))==2){
				$('#prewSceneTitle').show();
				$('#prewAuctionTitle').hide();
			}else{$('#prewAuctionTitle').show();$('#prewSceneTitle').hide();}
		 });
	 }); 	 
}

function showResult(){
	 var prev = new Array('resultTimeTab','resultAreaTab','resultSceneTab');
	 var vewTable = new Array('resultDate','resultArea','resultScene');
	 var oUl = $('#resultUL0').children();
	 oUl.each(function(){
		 $(this).mouseover(function(){
			 oUl.removeClass('p_y_l1');
			 $(this).addClass('p_y_l1');
			 for(var i=0;i<prev.length;i++){
				 $('#'+prev[i]).hide();
				 $('#'+vewTable[i]).hide();
			 }
			$('#'+prev[oUl.index($(this))]).show();
			$('#'+vewTable[oUl.index($(this))]).show();
			if(oUl.index($(this))==2){
				$('#resultSceneTitle').show();
				$('#resultAuctionTitle').hide();
			}else{$('#resultAuctionTitle').show();$('#resultSceneTitle').hide();}
		 });
	 }); 	 
}

function showContent(){
	 var links = $('#prewAreaTab').children();
	 var conts = new Array('jjt','csj','zsj','gat','others');
	 links.each(function(){$(this).click(function(){$('#area').children().hide();
					 links.removeClass('p_y_a1').addClass('p_y_a2');
					 $(this).removeClass('p_y_a2').addClass('p_y_a1');
					 $('#'+conts[links.index($(this))]).show();
				 })
			 }
	 );
}

function showResultContent(){
	 var links = $('#resultAreaTab').children();
	 var conts = new Array('resultjjt','resultcsj','resultzsj','resultgat','resultothers');
	 links.each(function(){$(this).click(function(){$('#resultArea').children().hide();
					 links.removeClass('p_y_a1').addClass('p_y_a2');
					 $(this).removeClass('p_y_a2').addClass('p_y_a1');
					 $('#'+conts[links.index($(this))]).show();
				 })
			 }
	 );
}

function showScene(s){
	 var oTabs = $('#prewSceneTab').children();
	 var scene = $('#scene').children();
	 if(parseInt(s)==31){
		 oTabs = $('#prewSceneTab').children();
		 scene = $('#scene').children();
	 }else{
		  oTabs = $('#resultSceneTab').children();
		  scene = $('#resultScene').children();
	 }
	 oTabs.each(function(){
		 $(this).click(function(){
			 oTabs.removeClass('p_y_a1').addClass('p_y_a2');
			 $(this).removeClass('p_y_a2').addClass('p_y_a1');
			 scene.hide(); 
			 scene.get(oTabs.index($(this))).style.display='block';
		 });
	 });
}

function showCompany(px){
	s = "pinav";
	var t = parseInt(px);
	/*
	switch(parseInt(t)){
	case 1:
		s+='1';
		break;
	case 2:
		s+='2';
		break;
	case 3:
		s+='3';
		break;
	case 4:
		s+='4';
		break;
	case 5:
		s+='5';
		break;
    case 6:
		s+='6';
		break;
	case 7:
		s+='7';
		break;
	default:
		break; 	
	}
	*/
	for(var i=1;i<=7;i++){
		if(t==i){
			s+=i;
			break;
		}
	}
    if( t!=undefined &&  t!=0){
		document.getElementById(s).className="p_i_nav_on";
    }
}


/**
 * 配合apache伪静态分页用
 */
jQuery(function($) {
	var staticPageReg = /^(.*)pn\d*pg\d*([\w|\W]*.html)$/g;
	var urlReg = /currentPageNo=([\d|☆]+)&pageSize=([\d|☆]+)/;
	var url = location.href || document.URL;
	//url='test_pn2pg20_ltyp90.html';
	if(staticPageReg.test(url)){//可能属于伪静态化页面的地址
		$('a').each(function(){
			var href = $(this).attr('href');
			if(urlReg.test(href)){//对于伪静态化页面的分页url提取并作修改
				var matchs = urlReg.exec(href);
				url = url.replace(staticPageReg,'$1'+'pn'+matchs[1]+'pg'+matchs[2]+'$2');
				$(this).attr('href',url);
			}
		});
		$('select[onchange]').each(function(){
			var locationurl=$(this).attr('onchange').toString();
			var valueReg = /\+this\.value\+/g;
			locationurl = locationurl.replace(valueReg,'☆').replace(/\'/g,"");
			if(urlReg.test(locationurl)){
				$(this).removeAttr('onchange');
				$(this).change(function(){
					locationurl = locationurl.replace(/☆/g,this.value);
					var matchs = urlReg.exec(locationurl);
					var attmpUrl = url.replace(staticPageReg,'$1');
					var stuffUrl = url.replace(staticPageReg,'$2');
					url = url.replace(staticPageReg,'$1'+'pn'+matchs[1].replace(/'/g,'')+'pg'+matchs[2]+'$2');
					window.location.href=url;
				});
			}
		});
	}
});
				
				
	/*
	 * 首页 手机端下载
	 */			
function tip(q, for_q){
					
					q = $(q);
					
					for_q = $(for_q);
					
					q.onfocus = function(){
						for_q.style.display = 'none';
						q.style.backgroundPosition = "right -17px";
					}
					q.onblur = function(){
						if(!this.value) for_q.style.display = 'block';
						q.style.backgroundPosition = "right 0";
					}
					for_q.onclick = function(){
						this.style.display = 'none';
						q.focus();
					}
				};
			//	tip('pst_keyword','for_pst_keyword');