document.writeln("<div id='msgboxdiv' style='position:absolute; z-index:1; visibility: hidden;'></div>");
function hidebox()
{
	//关闭选择窗口
		document.getElementById("msgboxdiv").style.visibility='hidden';
}

/**
显示的登陆框  obj,msg,x,y
*/
function showmsgbox(clickobj,msg)
{
	var argv = showmsgbox.arguments;
    var argc = showmsgbox.arguments.length;

    var dx = (argc > 2) ? argv[2] : 2;
    var dy = (argc > 3) ? argv[3] :20;
	
	var x,y,intLoop,intWeeks,intDays;
	var DivContent;
	var year,month,day;
	//var o=document.getElementById(clickobj);
	var o=clickobj;
	//显示的位置
	x=o.offsetLeft;
	y=o.offsetTop;
	
	dx=400;
	dy=200;
	while(o=o.offsetParent)
	{
		x+=o.offsetLeft;
		y+=o.offsetTop;
	}

	document.getElementById("msgboxdiv").style.left=x+dx+"px";
	document.getElementById("msgboxdiv").style.top=y+dy+"px";
	document.getElementById("msgboxdiv").innerHTML=msg;
	document.getElementById("msgboxdiv").style.visibility="visible";
}

//判断email地址是否正确
function isEmail(str){ 
	res = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
	var re = new RegExp(res); 
	return !(str.match(re) == null); 
}


//只能输入数字
//onKeyPress="event.returnValue=isNumeric();"
function isNumeric()
{
  return ((event.keyCode >= 48) && (event.keyCode <= 57));
}

/**
*检查是否是合法域名
* 是否是英文字符串
*/
function CheckDomain( String )
{ 
    var Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-";
     var i;
     var c;
      if(String.charAt( 0 )=='-')
	return false;
	if(String.charAt( 0 )=='.')
	return false;
      if( String.charAt( String.length - 1 ) == '-' )
          return false;
	if( String.charAt( String.length - 1 ) == '.' )
          return false;
     for( i = 0; i < String.length; i ++ )
     {
          c = String.charAt( i );
	  if (Letters.indexOf( c ) < 0)
	     return false;
     }
     return true;
}

function getdomain(){
  var arydomain = new Array(".com.cn",".net.cn",".org.cn",".gov.cn",".com",".cn",".net",".cc",".org",".info",".biz",".tv"); 
  var domain = document.domain; 
  var tmpdomain = ""; 
for(var i=0;i<arydomain.length; i++) 
  { 
  tmpdomain = arydomain[i]; 
  if(domain.indexOf(tmpdomain) != -1) 
  { 
  domain = domain.replace(tmpdomain,""); 
  domain = domain.substring(domain.lastIndexOf(".")+1,domain.length); 
domain = domain + tmpdomain; 
break; 
  } 
} 
return domain;
}
function GetCookie(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal (j);

        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}
function getCookieVal (offset){
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

/**
name -value -day - path -domain -secure;
**/
function SetCookie (name, value){
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;

    var expires = (argc > 2) ? getExpDate(argv[2],0,0) : null;
    var path = (argc > 3) ? argv[3] :"/";
    var domain = (argc > 4) ? argv[4] :getdomain();
    var secure = (argc > 5) ? argv[5] :false;
	//expires=getExpDate(365,0,0); 不设置时间 浏览器关闭cookies自动清空

  document.cookie = name + "=" + escape (value) +
     ((expires == null) ? "" : ("; expires=" + expires)) +
     ((path == null) ? "" : ("; path=" + path)) +
     ((domain == null) ? "" : ("; domain=" + domain)) +
     ((secure == true) ? "; secure" : "");
	 //alert(domain);
}

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();
}

//过期时间函数
 function getExpDate(days, hours, minutes)
    {
        var expDate = new Date();
        if(typeof(days) == "number" && typeof(hours) == "number" && typeof(hours) == "number")
        {
            expDate.setDate(expDate.getDate() + parseInt(days));
            expDate.setHours(expDate.getHours() + parseInt(hours));
            expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
            return expDate.toGMTString();
        }
    }
	
//取得？后面的参数	如：Request("a")=1；
function Request(strName)
{
//var strHref = "www.abc.com/index.html?a=1&b=1&c=测试测试";
var strHref = window.location.href;
//alert(strHref);
var intPos = strHref.indexOf("?"); //取得?所在的位置
var strRight = strHref.substr(intPos + 1);//取得?后面的一串字符

var arrTmp = strRight.split("&");//以&为分隔符将得到的字符串赋给数组arrTemp
for(var i = 0; i < arrTmp.length; i++)
{
var arrTemp = arrTmp[i].split("=");

if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
//如果传入的字符strName等于a或b或c，就返回对应的字符
}
return "";
}


function $_() {
  var elements = new Array();
  
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;
      
    elements.push(element);
  }
  
  return elements;
}

//判断是有有选项选中
function isselectd(objname){
	var isok=false;
	var o = document.getElementsByName(objname)  ;
	if(o==null) return false;
	for(var i=0 ; i<o.length ; i++){
			if(o[i].checked==true){
				 isok=true;
				 break;
			}
	}
	return isok;
}

//单选选中
function radioinit(objname , v){
	var o = document.getElementsByName(objname)  ;
	if(o==null) return;
	for(var i=0 ; i<o.length ; i++){
		if(o[i].value == v){
			o[i].checked = true ;
		}
	}
}

//多选选中
function  checkboxinit(objname , v){
	var o = document.getElementsByName(objname)  ;
	if(o==null) return;
	var val = v.split(",") ;
	for(var i=0 ; i<o.length ; i++){
		for(var j=0 ; j<val.length ; j++){
			if(o[i].value == val[j]){
				o[i].checked = true ;
			}
		}
	}
}

//下拉框选中
function selected(objname,v) {
	var o = document.getElementById(objname);
	if(o==null) return;
		for(var i=0 ; i<o.length ; i++){
			if(o[i].value == v){
				o[i].selected = true ;
			}
		}
}

function CheckAll(form)
  {
  for (var i=0;i<form.elements.length;i++)
    {
    var e = form.elements[i];
    if (e.Name != "chkAll")
       e.checked = form.chkAll.checked;
    }
 }

//回车所触发的事件
function KeyDown(evt,functions)
{
if(evt==null){evt=window.event}
	if(evt.keyCode==13  || evt.which == 13)  
	{ 
		evt.returnValue=false;
		evt.cancel = true;
		eval(functions);
	}   
}

/**
单张图片长宽重设
id 				图片
resizeWidth		图片最大宽度
resizeHeight	图片最大高度
**/
function imgresize(img,resizeWidth,resizeHeight)
{
       // resizeWidth=600;//最大宽度
       // resizeHeight=800;//最大高度
	    var newwidth=img.width;
		var newheight=img.height;
		var resizeWidth,resizeHeight;
          if (newheight>resizeHeight)
                 {newwidth=newwidth*(resizeHeight/newheight);
                 newheight=newheight*(resizeHeight/newheight);}
          if (newwidth>resizeWidth)        
                  {newheight=newheight*(resizeWidth/newwidth);
                newwidth=newwidth*(resizeWidth/newwidth);}
        img.width=newwidth;
        img.height=newheight;
}

// 增加点击量
function Click(tag,id){
  var url="/ajax.do?actiontype=click&tag="+tag+"&id="+id;
  $.get(url);
}

//ReplaceAll('','') 函数
String.prototype.ReplaceAll = stringReplaceAll;
function  stringReplaceAll(AFindText,ARepText){
  raRegExp = new RegExp(AFindText,"g");
  return this.replace(raRegExp,ARepText)
}

// Trim() 函数
String.prototype.Trim = function() 
{ 
return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 

function addBookmark(title,url){ 
if(typeof(title)=="undefined") title=document.title;
if(typeof(url)=="undefined") url=location.href;
if (window.sidebar) { 
	window.sidebar.addPanel(title, url,""); 
} else if( document.all ) {
	window.external.AddFavorite( url, title);
} else if( window.opera && window.print ) {
	return true;
}
}


//设为首页
function setHomepage()
{
 if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
       document.body.setHomePage('http://www.ffcrafts.com/');
 
    }
    else if (window.sidebar)
    {
    if(window.netscape)
    {
         try
   {  
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
         }  
         catch (e)  
         {  
    alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );  
         }
    } 
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage','http://www.ffcrafts.com/');
 }
}



//替换默认不存在的图片
function defaultimg(img){
	$("img").bind('error',function(){$(this).attr("src","http://img.ffcrafts.com/images/nopic.gif"); })
 }

//更新验证码
function imgload(imgID){
	imgID.src = "/inc/authImage.jsp?"+Math.random();
} 

function uploadimg(objid,ptype,event)
{
		var showx=400;
		var showy=300;
		if(event!=null){
			showx = event.screenX-180;// - event.offsetX - 1 //- 210 ; // + deltaX;
			showy = event.screenY - event.offsetY + 18; // + deltaY;
		}
		window.open('/inc/upload.jsp?objid='+objid+'&ptype='+ptype+'&t='+Math.random(), 'upimg','scrollbars=no,status=no,width=360,height=130,Left='+showx+',Top='+showy);

}
function countryChange(obj){
	$_("countryimg").src="/images/country/"+obj+".gif";
}

function clearNull(form){
	var ostr="document."+form+".elements"
	var o=eval(ostr);
    for(var  i=0;i<o.length;i++){
		if(typeof(o[i].value)=="string"){
		o[i].value=o[i].value.replace("null","");
		o[i].value=o[i].value.replace("Null","");
		}
	}   
}

function setpagesize(size){
	SetCookie("pagesize",size);
	window.location.reload();
}
function setkeyvalue(key,value){
	SetCookie(key,value);
	window.location.reload();
}

//信息 id 列表
var ids=",";
var _type="ids";
// type= ids/tids
function init(type){
	_type=type;
	ids=GetCookie(type);
	if(ids==null) ids=",";
}
		
function add_Item(id){
	if(ids.indexOf(","+id+",")==-1&&Math.round(id)==id){
		ids+=id+",";
		saveIds();
	}
}

function remove_Item(id){
		var v= id+",";
		var idst=ids;
		ids=idst.ReplaceAll(v,"");
		saveIds();
}

function addItem(o){
	if(o.checked==false){
		if(ids.indexOf(","+o.value+",")!=-1){
			var v= o.value+",";
			var idst=ids;
			ids=idst.ReplaceAll(v,"");
			saveIds();
		}
	}else{
			var v=o.value;
			if(Math.round(v)==v){
			ids+=o.value+",";
			saveIds();
			}
	}
}
		
function addBasket() {
	var num=0;
	$("input").each(function(i){
		 if($(this).attr("checked")){
		   		num++;
				add_Item($(this).val());	
		   }
		});
	if (num==0) {
		alert("No item(s) selected");
	} else {
		window.location=location.href;
	}
}

function selectall(key) {
	var num=0;
	$("input").each(function(i){
		 if($(this).attr("type")=="checkbox"){
			 	if(key=="yes"){
					add_Item($(this).val());
//					$(this).attr("checked","true");
				}else{
					remove_Item($(this).val());
				}
		   }
		});
	$("input").each(function(i){
	if(ids.indexOf(","+$(this).val()+",")!=-1){
		  $(this).attr("checked","checked");
	 }else{
		 $(this).attr("checked","");
	}
	});
}



function saveIds(){
	//name -value -day - path -domain -secure;
	SetCookie (_type, ids);
			
}
	
function inquiryNow(cid){
	if(typeof(cid)=="number"){
		window.open('http://www.ffcrafts.com/common/inquiry.jsp?cid='+cid);
	}else{
		SetCookie("inquirytype",cid);
		if(ids==","){
			alert("No item(s) selected");
		}else{
			if(location.href.indexOf(".21food.com")==-1){
				window.open('http://www.ffcrafts.com/common/inquiry.jsp?type='+cid+'&ids='+ids);
			}else{
				window.open('http://www.ffcrafts.com/common/inquiry.jsp');
			}
		}
	}
}

function inquiryNow(id,type){
	var par="";
	if(type=="product")par="pid="+id;
	if(type=="trade") par="tid="+id;
		window.open('http://www.ffcrafts.com/common/inquiry.jsp?'+par);
}

function checkedinit(){
//初始化
	$(function (){
		$("input").each(function(i){
		if(ids.indexOf(","+$(this).val()+",")!=-1&&!$(this).attr("disabled")){
			  $(this).attr("checked","checked");
		 }
		});
	})
}

function isChinese(s){        
var patrn=/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;        
if(!patrn.exec(s)){        
	return false;        
}       
else{        
	return true;        
}        
}

