/*该js文件是基于jquery框架之下运行 所以引入该文件之前必须先引入jquery文件*/

// 判断fck编辑器是否填写内容 
function GetMessageLength(cId)
{
    var oEditor = FCKeditorAPI.GetInstance(cId);
	return oEditor.GetXHTML( true ).length;
}
//更新fck 为提交表单做准备
function UpdateFckValue(){
	if (FCKeditorAPI){
		for (var i in FCKeditorAPI.__Instances)
		{
			FCKeditorAPI.__Instances[i].UpdateLinkedField();
		}
	}
}
//清除fck 编辑器的html
function ClearFckValue(){
	if (FCKeditorAPI){
		for (var i in FCKeditorAPI.__Instances)
		{
			FCKeditorAPI.__Instances[i].EditorDocument.body.innerHTML="";
		}
	}
}
//全选或者取消全选
var checkflag = "false";
function check_box(field) {
	if (checkflag == "false") {
		if(isNaN(field.length)){
			field.checked=true;
		}else{
			for (var i = 0; i < field.length; i++) { 
				field[i].checked = true;
			}
		}
		checkflag = "true"; 
		return "取消全选"; 
	} else {
		if(isNaN(field.length)){
			field.checked=false;
		}else{
			for (var i = 0; i < field.length; i++) { 
				field[i].checked = false; 
			}
		}
		checkflag = "false"; 
		return "选中全部"; 
	}
}

/**
 * 获取验证码
 * @param  string
 * @return  void
 */
function get_validate_code(tid) { 
	var code_url =js_root_path+'common/get_validate_code';  
	if (document.getElementById(tid))
	{
		document.getElementById(tid).innerHTML = '<img src="'+code_url+'" alt="点击刷新验证码" onclick="this.src=\''+code_url+'?t=\'+Math.random()" />';
	}
}
 
/**********************
图片loading效果+自动按比例缩放+支持单图和图片列表
@imgid:jquery 对象
@w:图片缩放的最大尺寸
***********************/
function get_pic_size(ob,w)
{
	var sizes=new Array(0,0);
	if(ob.height>0&&ob.width>0)
	{
		if(ob.width/ob.height>= 1){ 
			if(ob.width>w)
			{
				sizes[0]=w; 
				sizes[1]=(ob.height*w)/ob.width; 
			}
			else
			{ 
				sizes[0]=ob.width;
				sizes[1]=ob.height; 
			} 
		} 
		else
		{ 
			if(ob.height>w)
			{
				sizes[1]=w; 
				sizes[0]=(ob.width*w)/ob.height; 
			}
			else
			{ 
				sizes[0]=ob.width;
				sizes[1]=ob.height; 
			} 
		}
	}
	return sizes;
}
function LoadImage(imgid,val,w){ 
	if(val==""||val=="\/")
	{
		imgid.attr({src: onerror_pic_path, width: 50, height: 38});
		return ;
	}
	
	var img=new Image();
	if(img.complete)
	{
		var img_size=get_pic_size(img,w);
		imgid.attr({src: val, width: img_size[0], height: img_size[1]});
	}
	img.onload=function(){
		var img_size=get_pic_size(img,w);
		imgid.attr({src: val, width: img_size[0], height: img_size[1]});
	}
	img.onerror=function(){imgid.attr({src: onerror_pic_path, width: 50, height: 38});}
	img.src=val;
}

//提交表单
function submit_list_form(f,e){
	var action_ob=document.getElementById("action_url");
	var selected_text=$("#action_url option:selected").text();
	if ($("#check_box_id input:checked").length==0)
	{
		alert("请勾选要操作的项,然后再提交");
		return false;
	}
	if (!Validator.Validate(f,2)) return false;
	if (action_ob.value=="") 
	{
		alert("请选择要执行的操作,然后再提交");
		return false;
	}
	if(selected_text=='转移到分类')
	{
		if($("#to_catalog_id").val()=='')
		{
			alert("请选择要转到的分类,然后再提交");
			return false;
		}
	}
	if(selected_text=='复制到'||selected_text=='剪切到')
	{
		if($("#to_dir").val()=='')
		{
			alert("请选择要复制/剪切到的路径");
			return false;
		}
	}
	if (confirm('确定要执行该操作吗？')){
		f.action=action_ob.value;
		e.value = '数据处理中..';
		e.disabled = true;
		f.submit();
	}
}

function remove_ob(ob)
{
	$(ob).parent().remove();
}


//转移分类
function to_catalog_onChange()
{
	var selected_text=$("#action_url option:selected").text();
	if(selected_text=='转移到分类'||selected_text=='复制到'||selected_text=='剪切到')
	{
		$("#to_catalog_span").show();
	}
	else
	{
		$("#to_catalog_span").hide();
	}
}
//拷贝文本
function js_copy(txt)
{
	if(window.clipboardData) 
	{    
		window.clipboardData.clearData();    
		window.clipboardData.setData("Text", txt);    
	}
	else if(navigator.userAgent.indexOf("Opera") != -1)
	{    
		window.location = txt;   
	} 
	else if (window.netscape) 
	{    
		try {    
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");    
		} catch (e) {    
			alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");  
		}    
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);    
		if (!clip)    
		return;   
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);    
		if (!trans)    
		return;    
		trans.addDataFlavor('text/unicode');    
		var str = new Object();    
		var len = new Object();    
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);    
		var copytext = txt;    
		str.data = copytext;    
		trans.setTransferData("text/unicode",str,copytext.length*2);    
		var clipid = Components.interfaces.nsIClipboard;    
		if (!clip)    
		return false;    
		clip.setData(trans,null,clipid.kGlobalClipboard);    
	} 
	alert("链接已经成功拷贝，您可以粘贴到旺旺,QQ,MSN或者邮件发给好友了");    
}

//切换状态
function ajax_toggle(ob,id,url)
{
	hiOverAlert("<span id='showMsg' style='font-size:12px;'><img src='"+pic_loading_path+"' border='0' /> 数据处理中，请稍等...</span>",0);
		$.ajax({
		 type: "POST",
		 url: url,
		 data:   "rd_id="+id,
		 success: function(msg){ 
		 	if(msg==1) {
				var img_src=$(ob).attr("src");
				if(img_src.indexOf("no.gif")>0)
				{
					$(ob).attr("src",img_src.replace("no.gif","yes.gif"));
				}
				else
				{
					$(ob).attr("src",img_src.replace("yes.gif","no.gif"));
				}
			}
			unhiOverAlert();
		 },
		 error: function(data){ 
			$("#showMsg").html("数据提交失败");
			setTimeout(function(){unhiOverAlert(); }, 2000); 
		 }
	}); 
}

/*弹出选择文件窗口*/
var input_field_id;
function GetFileDialog(ob)
{
	input_field_id=ob;
	hiOverAlert("<span id='showMsg' style='font-size:12px;'><img src='"+pic_loading_path+"' border='0' /> 正在获取数据...</span>",0);
	if(!document.getElementById("file_dialog_id"))
	{
		$("<div id=\"file_dialog_id\" style=\"display:none;\"></div>").prependTo("body");
	}
	$.ajax({
		 type: "POST",
		 url: js_root_path+"shop_admin/file_manage/ajax_get_file",
		 data: "action=getdata",
		 success: function(msg){
			$("#file_dialog_id").html(msg);
			unhiOverAlert();
			$('#file_dialog_id').dialog({  
				hide:'',    
				autoOpen:false,
				width:750,
				height:480,  
				modal:false, //蒙层  
				title:'选择文件', 
				close: function(){ 
					$(this).dialog('destroy');
					$("#file_dialog_id").remove();
					$('select').show();
				},  
				overlay: {  
					opacity: 0.5, 
					background: "black"
				},
				buttons:{  
					'取消':function(){$(this).dialog("close");}
				}
			});
			$('#file_dialog_id').dialog('open');
			ReloadImage();
			$('select').hide();
		 },
		 error:function(msg){
			$("#showMsg").html("请求没有完成...");
			setTimeout(function(){unhiOverAlert();}, 2000); 
		}
	});
}
function ReloadFileDialog(fp)
{
	$("<img src='"+pic_loading_path+"' /> 正在获取数据...").prependTo("#file_dialog_id");
	$.ajax({
		 type: "POST",
		 url: js_root_path+"shop_admin/file_manage/ajax_get_file",
		 data: "folder_path="+fp,
		 success: function(msg){
			$("#file_dialog_id").html(msg);
			ReloadImage();
		 },
		 error:function(msg){
			$("#file_dialog_id").html("请求没能完成");
		}
	});
	return ;
}

function GetFilePath(ob)
{
	$("#"+input_field_id).val($(ob).attr("rel"));
	$('#file_dialog_id').dialog('close');
	//return ;
}
function ReloadImage()
{
	$(".ajax_jq_pic_loading").each(function(){
		LoadImage($(this),$(this).attr("rel"),60);
	});
}

function subForm(f,e)
{
	if( ! Validator.Validate(f,3)) return;
	e.value = '数据处理中..';
	e.disabled = true;
	f.submit();
}

/*---获取推广链接----------*/
function get_tb_dialog(ob)
{
	input_field_id=ob;
	if(!document.getElementById("tb_dialog_id"))
	{
		$("<div id=\"tb_dialog_id\" style=\"display:none;\"></div>").prependTo("body");
	}
	if(!document.getElementById("tb_content"))
	{
		hiOverAlert("<span id='showMsg' style='font-size:12px;'><img src='"+pic_loading_path+"' border='0' /> 正在获取数据...</span>",0);
		$.ajax({
			 type: "POST",
			 url: js_root_path+"shop_admin/tb_search/get_search_form",
			 data: "action=getdata",
			 success: function(msg){
				$("#tb_dialog_id").html(msg);
				unhiOverAlert();
				$('#tb_dialog_id').dialog({  
					hide:'',    
					autoOpen:false,
					width:770,
					height:480,  
					modal:false, //蒙层  
					title:'获取推广链接', 
					close: function(){ 
					},  
					overlay: {  
						opacity: 0.5, 
						background: "black"
					},
					buttons:{  
						'取消':function(){$(this).dialog("close");}
					}
				});
				$('#tb_dialog_id').dialog('open');
			 },
			 error:function(msg){
				$("#showMsg").html("请求没有完成...");
				setTimeout(function(){unhiOverAlert();}, 2000); 
			}
		});
	}
	else
	{
		$('#tb_dialog_id').dialog('open');
	}
}

function get_tb_catalog(s)
{
	if( ! s) return;
	$.ajax({
		 type: "POST",
		 url: js_root_path+"shop_admin/tb_search/get_catalog",
		 data: "stype="+s,
		 dataType:'json',
		 success: function(msg){
			$('#cid').empty();
			$('<option value="0">选择行业类目</option>').appendTo('#cid');
			$.each(msg,function(k,v){
				$('<option value="'+k+'">'+v+'</option>').appendTo('#cid');			
			});
		 }
	});
}

function get_tb_data(p)
{
	var theform = document.tb_search_form;
	if( ! Validator.Validate(theform,1)) return; 
	var stype = theform.stype.value;
	var keyword = theform.keyword.value;
	var cid = theform.cid.value;
	var start_commission = theform.start_commission.value;
	var end_commission = theform.end_commission.value;
	var start_credit = theform.start_credit.value;
	var end_credit = theform.end_credit.value;
	if(keyword == '关键字' && cid == 0)
	{
		alert('请输入搜索关键字 或者选择其中一个行业类目');
		return;
	}
	if(keyword == '' && cid == 0)
	{
		alert('请输入搜索关键字 或者选择其中一个行业类目');
		return;
	}
	if(keyword == '关键字') keyword = '';
	var action = js_root_path+"shop_admin/tb_search/";
	if(stype == 'goods') action += 'get_search_goods';
	else action += 'get_search_shop';
	if( ! p) p = 1;
	var pstr = 'keyword='+escape(keyword)+'&cid='+cid+'&start_commission='+start_commission+'&end_commission='+end_commission+'&start_credit='+start_credit+'&end_credit='+end_credit+'&page_no='+p;
	//alert(pstr);
	$('#tb_content').html("<img src='"+pic_loading_path+"' /> 正在获取数据...");
	$.ajax({
		 type: "POST",
		 url: action,
		 data: pstr,
		 success: function(msg){
			$('#tb_content').html(msg);
			$(".trtoggle tr:even").css("background-color","#F7F7F7");
			$(".trtoggle tr").hover(function(){
				$(this).css("background-color","#D9EBF5");
			},function(){
				if($(".trtoggle tr").index($(this))%2==0)
				{
					$(this).css("background-color","#F7F7F7");
				}
				else
				{
					$(this).css("background-color","transparent");
				}
			});
		 }
	});
}

function get_url(v)
{
	$("#"+input_field_id).val(v);
	$('#tb_dialog_id').dialog('close');
	return ;
}