﻿//内容检索
function RZ_Search(){
	var form = $(this).parents("form");
	var keyword = form.find("input[name='keyword']");
	if($.trim(keyword.val())!="")
		form.submit();
}

/*添加点击数
el_hits:显示点击数的元素
*/
function RZ_LoadHits(el_hits){
	var recordid=el_hits.attr("recordid");
	var proc=el_hits.attr("type");
	if (proc!=undefined && proc!="")
	{
		var cookieid=$.cookie(RZ_APP_PRE+proc+"hits"+recordid);
		if (cookieid==null||cookieid=="")
		{
			$.ajax({
			  url: RZ_ROOT+"asp/hits_add.asp?proc="+proc+"&id="+recordid,
			  cache: false,
			  success: function(data){
				if (data!="")
				  {
					el_hits.text(data);
					$.cookie(RZ_APP_PRE+proc+"hits"+recordid,data);
				  }
			  }
			}); 
		}else{
			$.ajax({
			  url: RZ_ROOT+"asp/hits_count.asp?proc="+proc+"&id="+recordid,
			  cache: false,
			  success: function(data){
				if (data!="")
				  {
					el_hits.text(data);
				  }
			  }
			}); 
		}
	}
}

function RZ_TotalRecord_Minus(){
	if (isInteger($("#RZ_TotalRecord").text()))
	{
		$("#RZ_TotalRecord").text(parseInt($("#RZ_TotalRecord").text())-1);
	}
}

//商品被收藏次数
function RZ_LoadFavor(){
	var el = $("#RZ_FavorCount");
	if($("#RZ_FavorCount").attr("id")){
		var id = el.attr("productid");
		ajaxGet2Text(RZ_ROOT+"asp/product_countfavor.asp?id="+id,el);
	}
}



function RZ_ShowDialog(){
	var params=$(this).attr("params");
	var atitle=$(this).attr("title");
	var awidth=$(this).attr("width");
	var aheight=$(this).attr("height");
	var dialog=$(this).attr("dialog");
	var aurl=$(this).attr("url");
	var aid=$(this).attr("id");
	var receiver=$(this).attr("receiver");
	if(atitle==undefined)
		atitle='';
	if (awidth==undefined)
		awidth=500;
	if (aheight==undefined)
		aheight=300;
	if (aurl==undefined){
		aurl=RZ_ROOT+"asp/ajaxLoadDialog.asp";
	}
	$("#RZ_WindowLayer").empty();
	$("#RZ_WindowLayer").html("<div class='loading'></div>");
	$.ajax({
		type: "POST",
		url: aurl,
		data: "dialog="+dialog+"&"+params,
		cache:false,
		success: function(response){
			$("#RZ_WindowLayer").html(response);
			$('#RZ_Window').window({
				title: ''+atitle,
				width: awidth,
				height: aheight,
				modal: true,
				collapsible:false
			});
			$('#RZ_Window').window('open');
			$(".RZ_Readed[smsid='"+aid+"']").text(getText("READED"));
			$("#receiver").val(receiver);
			$("#RZ_Checkcode_Img").attr("src",RZ_ROOT+"asp/code.asp?t="+(new Date()).getTime());
			$("#RZ_Checkcode_Img").bind("click",function(){$(this).attr("src",RZ_ROOT+"asp/code.asp");});
		}
	});
}

//发送邮件
function RZ_SendMail(title,rand,content,fun){
	$.ajax({
		type: "POST",
		url: RZ_ROOT+"asp/sendmail.asp",
		data: "title="+title+"&rand="+rand+"&content="+content,
		cache:false,
		success: fun
	});
}
//待询价记录数
function RZ_Load_RequestPrice_Count(flag){
	ajaxGet2Text(RZ_ROOT+"asp/requestprice_count.asp?update="+flag,$(".RZ_RequestPrice_Count"));
}
//待订单记录数
function RZ_Load_Orderform_Count(flag){
	ajaxGet2Text(RZ_ROOT+"asp/orderform_count.asp?update="+flag,$(".RZ_Orderform_Count"));
}

//删除SMS
function RZ_Delete_SMS(id,func){
	$.messager.confirm(getText("INFORMATION"), getText("CONFIRM_DELETE"), function(r){
		if(r){
			$.ajax({
			  url: "asp/sms_delete.asp?id="+id,
			  cache: false,
			  success: function(data){
				if (data=="SUCCESS")
				{
					RZ_TotalRecord_Minus();
					func();
				}
			  }
			}); 
		}
	});
}

//删除Favor
function RZ_Delete_Favor(id,func){
	$.messager.confirm(getText("INFORMATION"), getText("CONFIRM_DELETE"), function(r){
		if(r){
			$.ajax({
			  url: "asp/favor_delete.asp?id="+id,
			  cache: false,
			  success: function(data){
				if (data=="SUCCESS"){
					RZ_TotalRecord_Minus();
					func();
				}
			  }
			}); 
		}
	});
}

//添加商品到询价车
function RZ_Add2RequestPrice(){
	var productid=$(this).attr("productid");
	var title=$(this).attr("title");
	var unit=$(this).attr("unit");
	$.ajax({
	  type: "POST",
	  url: RZ_ROOT+"asp/requestprice_addproduct.asp",
	  cache: false,
	  data:"productid="+productid+"&title="+title+"&unit="+unit,
	  success: function(data){
		if (data.indexOf("SUCCESS")!=-1)
		{
			$.messager.alert(getText("INFORMATION"),data.substring(7),'info');
			RZ_Load_RequestPrice_Count(true);
		}else{
			if (data.indexOf("DUPLICATE")!=-1){
				$.messager.alert(getText("INFORMATION"),data.substring(9),'info');
			}
			if (data=="LOGIN")
			{
				location.href=RZ_ROOT+"index.asp?member/login.html---"+escape(location);
			}
		}
	  }
	});	
}


//添加到待订购单
function RZ_Add2Orderform(){
	var productid = $(this).attr("productid");
	var title=$(this).attr("title");
	var unit=$(this).attr("unit");
	var price=$(this).attr("price");
	$.ajax({
	  type: "POST",
	  url: RZ_ROOT+"asp/orderform_addproduct.asp",
	  cache: false,
	  data:"productid="+productid+"&title="+title+"&unit="+unit+"&price="+price,
	  success: function(data){
		if (data.indexOf("SUCCESS")!=-1)
		{
			$.messager.alert(getText("INFORMATION"),data.substring(7),'info');
			RZ_Load_Orderform_Count(true);

		}else{
			if (data.indexOf("DUPLICATE")!=-1){
				$.messager.alert(getText("INFORMATION"),data.substring(9),'info');
			}
			if (data=="LOGIN")
			{
				location.href="index.asp?member/login.html---"+escape(location);
			}
		}
	  }
	});	
}


//添加商品到收藏夹
function RZ_Add2Favor(){
	var productid=$(this).attr("productid");
	var title=$(this).attr("title");
	$.ajax({
	  type: "POST",
	  url: RZ_ROOT+"asp/favor_add.asp",
	  cache: false,
	  data:"productid="+productid+"&title="+title,
	  success: function(data){
		if (data.indexOf("SUCCESS")!=-1)
		{
			$.messager.alert(getText("INFORMATION"),data.substring(7),'info');
			RZ_LoadFavor();
		}else{
			if (data.indexOf("DUPLICATE")!=-1){
				$.messager.alert(getText("INFORMATION"),data.substring(9),'info');
			}
			if (data=="LOGIN")
			{
				location.href=RZ_ROOT+"index.asp?member/login.html---"+escape(location);
			}
		}
	  }
	});	
}


//更新待询价商品的值
function RZ_Update_PendingRequestPrice_Product(id,field,val){
	$.ajax({
		url: RZ_ROOT+"asp/requestprice_pending_update.asp?id="+id+"&field="+field+"&val="+val,
		cache: false
	});
}

//删除待询价商品
function RZ_Delete_PendingRequestPrice_Product(id,func){
	$.messager.confirm(getText("INFORMATION"), getText("CONFIRM_DELETE"), function(r){
		if(r){
			$.ajax({
				type: "GET",
				url: RZ_ROOT+"asp/requestprice_pending_delete.asp?id="+id,
				cache: false,
				success: function(data){
					if (data=="SUCCESS")
					{
						RZ_Load_RequestPrice_Count(true);
						func();
					}
				}
			});
		}
	});
}

function RZ_Delete_PendingOrderform_Product(id,func){
	$.messager.confirm(getText("INFORMATION"), getText("CONFIRM_DELETE"), function(r){
		if(r){
			$.ajax({
				type: "GET",
				url: RZ_ROOT+"asp/orderform_pending_delete.asp?id="+id,
				cache: false,
				success: function(data){
					if (data=="SUCCESS")
					{
						RZ_Load_Orderform_Count(true);
						func();
					}
				}
			});
		}
	});
}

//提交询价单
function RZ_SendRequestPrice(ids, productids, productnums, productunits, productnames,memberremark){
	$.ajax({
		type: "POST",
		url: RZ_ROOT+"asp/requestprice_save.asp",
		cache: false,
		data:"ids="+ids+"&productids="+productids+"&productnums="+productnums+"&productunits="+productunits+"&productnames="+productnames+"&memberremark="+memberremark,
		success: function(data){
			if (data.indexOf("SUCCESS")!=-1)
			{
				RZ_Load_RequestPrice_Count(true);
				alert(data.substring(7));
				location.href="?member/myRequestPrice.html";
			}
		}
	});
}

//提交订单
function RZ_SendOrderform(ids, productids, productnums, productunits, productnames, productprices,memberremark){
	$.ajax({
		type: "POST",
		url: RZ_ROOT+"asp/orderform_save.asp",
		cache: false,
		data:"ids="+ids+"&productids="+productids+"&productnums="+productnums+"&productunits="+productunits+"&productnames="+productnames+"&productprices="+productprices+"&memberremark="+memberremark,
		success: function(data){
			if (data.indexOf("SUCCESS")!=-1)
			{
				RZ_Load_Orderform_Count(true);
				alert(data.substring(7));
				location.href="?member/myOrderform.html";
			}
		}
	});
}

//修改订单中商品的数量
function RZ_Change_Orderform_Number(id,num){
	$.ajax({
		url: RZ_ROOT+"asp/orderform_pending_update.asp?id="+id+"&number="+num,
		cache: false
	});
}

//确认、取消订单
function RZ_Change_Orderform_State(id,state){
	$.ajax({
	   url: RZ_ROOT+"asp/orderform_state.asp?id="+id+"&state="+state,
	   cache: false,
	   success: function(data){
		  if (data!="")
		  {
			alert(data);
			location.href="?member/myOrderform.html";
		  }
	   }
	});
}

function RZ_SaveVote(itemids, voteid){
	$.ajax({
		type: "POST",
		url: RZ_ROOT+"asp/vote_save.asp",
		cache:false,
		data:"itemids="+itemids+"&voteid="+voteid,
		success: function(response){
			if (response=="SUCCESS0"||response=="SUCCESS1")
			{
				$.messager.alert(getText("INFORMATION"),getText("VOTE_SUCCESS"),'info');
			}else{
				$.messager.alert(getText("INFORMATION"),response,'info');
			}
		}
	});
}

$(document).ready(function(){
	//关闭对话框
	$('#RZ_Window').window('close');
	RZ_Load_RequestPrice_Count(false);
	RZ_Load_Orderform_Count(false);
	RZ_LoadFavor();
	
	$(".RZ_ShowDialog").bind("click",RZ_ShowDialog);
	$(".RZ_Add2RequestPrice").bind("click",RZ_Add2RequestPrice);
	$(".RZ_Add2Orderform").bind("click",RZ_Add2Orderform);
	$(".RZ_Add2Favor").bind("click",RZ_Add2Favor);
	$("*[receiver='']").hide();
	ajaxGet(RZ_ROOT+"asp/loginStatus.asp",$("#RZ_LoginStatus"));
});


