//---------------------- 弹出窗口的通用函数 ------------------------
//file_string为弹出窗口所需要打开的页面名称
//iwidth,iheight为定义弹出窗口的宽及高
//istatus指示是否显示状态条、地址栏等内容
function fun_open_popup(file_string,iwidth,iheight,istatus)
{
  var iLeft,iTop
  
  if(file_string.indexOf("?")>0)
    { file_string = file_string + "&no_cache=" + No_cache(); }
  else
    { file_string = file_string + "?no_cache=" + No_cache(); }
  
  iLeft=(document.body.clientWidth-iwidth)/2;
  iTop=(document.body.clientHeight-iheight)/2;
  
  if(istatus == "no")
    { window.open(file_string, '', 'height=' + iheight + ', width=' + iwidth + ', top=' + iTop + ', left=' + iLeft + ', toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no'); }
  else
    { window.open(file_string, '', 'height=' + iheight + ', width=' + iwidth + ', top=' + iTop + ', left=' + iLeft+ ', toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes'); }
}

//----------- 检查电子邮件地址是否正确 -------------------
function fun_username(str,strname){
  var pattern,err="";
  
  pattern = /^[a-zA-Z](\w){3,19}$/;
  if(str.match(pattern) == null)  err="请输入正确的[" + strname + "],并且用户名长度应介于4-20字符之间.";
  return err;
}

//----------- 检查是否为空 -------------------
function fun_null(str,name)
{
  var err="";
  if(str.length == 0)
    { err="请输入" + name + "！"; }
  return err;
}

//----------- 检查双密码是否正确 -------------------
function fun_password(str1,str2,n)
{
  var pattern,err="";
  pattern = /^[a-zA-Z0-9]{6,19}$/;
  
  if(str1.match(pattern) == null){
     err="请输入有效[密码]，并且密码字符长度应介于" + n + "至20字符之间！.";
     return err;
	}
	
  if(str2.match(pattern) == null){
     err="请输入有效[确认密码]，并且密码字符长度应介于" + n + "至20字符之间！.";
     return err;
	}
	
  if(str1 != str2){
     err=err="新密码与确认密码不匹配！";
	 return err;
	}
  
  return err;
}

//----------- 检查单密码是否正确 -------------------
function fun_single_password(str,n){
  var pattern,err="";
  
  pattern = /^[a-zA-Z0-9]{3,19}$/;
  if(str.match(pattern) == null)  err="请输入有效[密码]，并且密码字符长度应介于" + n + "至20字符之间！.";
  return err;
}

//----------- 检查是否为数字 -------------------
function fun_number(str,name)
{  
  var c1,err="";
  if(str.length != 0)
  { 
    if(isNaN(str)) {err="请在" + name + "中输入数字！"; } 
	else
	  {if(parseInt(str) < 0){err="数字应为正值！";}}
  }
  else
  { err="请输入" + name + "！"; }

  return err;
}

//----------- 检查电子邮件地址是否正确 -------------------
function fun_email(str,strname)
{
  var pattern,err="";
  
   pattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  if(str.match(pattern) == null)  err="请输入正确的" + strname + "！";
  
  return err;
}

//-------------------- 邮政编码检查 -------------------------
function fun_postcode(str,name)
{
  var err="";
  
  if(str !="")
  {
    //err=fun_number(str,"Post Code");
    //if(str.length != 6){err="The postcode should be 6 digitals. ";}
	if(str.length > 10){err="邮政编码长度应小于10个字符！";}
  }

  return err;
}

//----------- 检查日期是否正确 -------------------
function fun_date(str,name)
{
  var c1,c2,c3,err="";
  if(str.length != 0)
  {
    if(str.length <11)
    {
      c1=str.substr(4,1);
      c2=str.substr(7,1);
      if(c1 != "/" | c2 != "/") {err="您输入的" + name + "不正确！";}
      
      for(i=0;i<str.length;i++)
      { 
        c1=str.substr(i,1);
        if(i !=4 & i != 7)
        {
          if(isNaN(parseInt(c1))){err="您输入的" + name + "不正确！";}
        }
      }
      
      c1=str.substr(0,4)
      c2=str.substr(5,2)
      c3=str.substr(8,2)
      if(parseInt(c1) > 2000 | parseInt(c2) > 12 | parseInt(c3) > 31){err="您输入的" + name + "不正确！";}
    }
    else
    { err="您输入的" + name + "不正确！";}
  }
  else
  { err="请输入正确的" + name + "！";}

  return err;
}

//--------------------------------------------- 组合出每页都有的简单查询条件 ----------------------------------------------------
function fun_search_query(frm)
{
  var strquery=" and ";
  var arrRegions;
 
  strquery = strquery + fun_getvalue(frm.dr_type.options[frm.dr_type.selectedIndex].value,"","wine_type_id","i","=");
  
  if(frm.dr_region.options[frm.dr_region.selectedIndex].value != "")
  {
    arrRegions=(frm.dr_region.options[frm.dr_region.selectedIndex].value).split(",");
    if(arrRegions[1] == 0 & arrRegions[2] == 0) strquery = strquery + " and " + fun_getvalue(arrRegions[0],"","nation","i","=");
    if(arrRegions[1] != 0 & arrRegions[2] == 0)
      {
        strquery = strquery + " and " + fun_getvalue(arrRegions[0],"","nation","i","=");
        strquery = strquery + " and " + fun_getvalue(arrRegions[1],"","city","i","=");
      }
    if(arrRegions[1] != 0 & arrRegions[2] != 0)
      {
        strquery = strquery + " and " + fun_getvalue(arrRegions[0],"","nation","i","=");
        strquery = strquery + " and " + fun_getvalue(arrRegions[1],"","city","i","=");
        strquery = strquery + " and " + fun_getvalue(arrRegions[2],"","region","i","=");
      }
  }
  
  if(frm.dr_price.options[frm.dr_price.selectedIndex].value != "") strquery = strquery + " and " + fun_getvalue(arrPrice[parseInt(frm.dr_price.options[frm.dr_price.selectedIndex].value) * 2 - 1],arrPrice[parseInt(frm.dr_price.options[frm.dr_price.selectedIndex].value) * 2],"price","i","between");

  frm.txtQuery.value = strquery;
}

//--------------------------------------------- 组合出每页都有的简单查询条件(short_cut) ----------------------------------------------------
function fun_search_shortcut(frm)
{
  var strquery=" and ";
  var arrType;
  
  if(frm.dr_shortcut.options[frm.dr_shortcut.selectedIndex].value != "")
  {
    arrType=(frm.dr_shortcut.options[frm.dr_shortcut.selectedIndex].value).split(",");
    if(arrType[1] > 0)
	  { 
	    strquery = strquery + fun_getvalue(arrType[0],"","wine_type_id","i","=");
		strquery = strquery + " and " + fun_getvalue(arrType[1],"","main_varietal_id","i","=");
	  }
    else
	  { strquery = strquery + fun_getvalue(arrType[0],"","wine_type_id","i","="); }
	  
	frm.dr_type.value = arrType[0];
	frm.dr_varietal.value = arrType[1]
	frm.txtQuery.value = strquery;
  }
}

//---------------- 针对每一种查询字段，组合出单个查询条件,再在fun_query()中组合出完整的查询语句 ------------------
function fun_getvalue(value1,value2,strfield,strtype,strmethod)
{
  var strReturn;
  if(value1 != "")
    {
      if(strmethod == "=")
        {
          if(strtype == "c"){strReturn = strfield + "='" + value1 + "'";}
          if(strtype == "i"){strReturn = strfield + "=" + value1;}
          
        }
      if(strmethod == "%")
        {
          if(strtype == "c"){strReturn = strfield + " like '%" + value1 + "%'";}
        }
      if(strmethod == "between")
        {
          if(strtype == "i"){strReturn = strfield + " between " + value1 + " and " + value2;}
        }
     }
   else {strReturn="1=1";}
   return strReturn;
}

//-----检测某一表单的文本框内容是否非空且为数字
//-----fnam为表单名,tname表单中具有同一名字的多个文本框（可以有下标）,strname为出错时提示的文本框名称
function check_form_numeric(fname,tname,strname)
{
 var count,result

 result=true;

 if(typeof(eval("document." + fname + "." + tname))!="undefined")
 { 
   count=(eval("document." + fname + "." + tname )).length;

   if (!(isNaN(count)))
      {
        for (i=0;i<count;i++){if (!check((eval("document." + fname + "." + tname))[i].value)) {result=false;}}
      }
   else
      {
        if (!check((eval("document." + fname + "." + tname)).value)){result=false;}
      }
   if (result==false) {alert("请在" + strname + "中输入数字(0到999之间)！ ");}
   return result;
 }
 else {return true;}
}

//------------ 判断是否为数字 ---------------
function  check(num)
{
  //return( (num!="") & (!isNaN(num)) )
  if(num != "")
  {
    if( !isNaN(num) & (parseInt(num)>0) & (parseInt(num)<=999)) 
	  {return true;}
	else 
	  {return false;}
  }
  else
    {return false;}
}

//-------- 根据参数值决定返回T或F值 -----------
function convert_false(i)
{
  if(i==1) return true;
  if(i==0) return false;
}

//---------------------- 弹出酒产品的详细说明窗口 ------------------------
function fun_detail(file_string,istatus)
{
  var iLeft,iTop
  
  if(file_string.indexOf("?")>0)
    { file_string = file_string + "&no_cache=" + No_cache(); }
  else
    { file_string = file_string + "?no_cache=" + No_cache(); }
  
  iLeft=(document.body.clientWidth-800)/2;
  iTop=(document.body.clientHeight-600)/2;
  
  if(istatus == "no")
    { window.open(file_string, '', 'height=600, width=800, top=' + iTop + ', left=' + iLeft + ', toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no'); }
  else
    { window.open(file_string, '', 'height=500, width=800, top=' + iTop + ', left=' + iLeft+ ', toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes'); }
}

//-------- 根据条件确定View_ShoppingCart的去向 ---------
function fun_viewcart(Product_ID,Packing_ID,Member_ID)
{ 
  var str_url="";
  if(Member_ID == 0){str_url="/cn/b2c/login.asp?url=";}
  
  if (parseInt(Product_ID) == 1)
   {
   
	 
	  document.location=str_url + "/cn/b2c/view_cart.asp";
   }
  else
   { alert("对不起，您尚未选择任何葡萄酒。\n在查看购物车之前请选择葡萄酒！");}
}
//-------- 根据条件确定View_ShoppingCart的去向 ---------
function fun_viewcart_b(Product_ID,Packing_ID,Member_ID)
{ 
  var str_url="";
  if(Member_ID == 0){str_url="../../cn/b2b/login.asp?url=";}
  
  if (parseInt(Product_ID) == 1)
   {
     
     if (parseInt(Packing_ID) == 1)
	  { document.location=str_url + "../../cn/b2b/view_packing.asp"; }
	 else
	  { document.location=str_url + "../../cn/b2b/view_cart.asp"; }
   }
  else
   { alert("对不起，您尚未选择任何葡萄酒。\n在查看购物车之前请选择葡萄酒！");}
}

//------ 清除CACHE的有效方法 -------
function No_cache()
{
  var d=new Date();
  var tReturn;
  
  tReturn=d.getTime();
  return tReturn;
}

//----------- 检查登录对话框中各字段是否为空 ----------------
function login_check(theform)
{
  var strUser=theform.txt_username.value;
  var strPwd=theform.txt_password.value;
  
  if(strUser.length < 4 | strUser.length > 20 | strPwd.length < 4 | strPwd.length > 20)
    { 
     alert("您输入的用户名和密码有误，用户名和密码的长度应该在4-20个字符之间。请重试。");
     return false;
    }
}

//---------- 设定默认聚焦文本框 -------------
function focus_text(theform)
{
  theform.txt_username.focus();
}

//-------- 设定要跳转的页数 ---------
function fun_go_page(theform,ipage)
{ 
  theform.txt_page.value = ipage;
  theform.submit(true);
}

//-------- 如果文本框已经修改，设值为“1” ---------
function fun_change(theform)
{
  theform.txt_change.value="1";
}

//---------  如果文本框已经修改，提问用户是否确认修改有效 ----------
function fun_confirm(theform,bstr)
{
  if(bstr==true)
  {theform.submit(true);}
}

//--------- 当连接到别的页面时检查是否页面内容已做了修改 ------------
function fun_check_change(theform,strurl)
{
  if(fun_save_change(theform) == "no")
  {document.location=strurl;}
}

//--------- 当退出时检查是否页面内容已做了修改 ------------
function fun_check_close(theform)
{
  if(fun_save_change(theform) == "no")
  {window.close();}
}

//--------- 询问是否保存所修改的内容(老版本) ------------
function fun_save_change(theform)
{
  var strReturn="no";
  
  if(theform.txt_change.value == "1")
	{
	  if (confirm("您未保存修改！\nIf you want to save changes, click [OK] and then click [Confirm Modification] at the bottom. \nIf you don't want to save them, just click [Cancel] button."))
		{strReturn = "yes";}
	}
	
  return strReturn;
}

//*******************************************************************
//*******************************************************************
//--------- 询问是否保存所修改的内容(新版本) ------------
function ask_save_change(theform)
{
  var strReturn="no";
  
  if(theform.txt_change.value == "1")
	{
	  if(confirm("您未保存修改！\n\n如果您希望保存修改，请点击[OK]。\n如果您不希望保存修改，则点击[Cancel]。"))
		{strReturn = "yes";}
	}
	
  return strReturn;
}

//--------- 当连接到别的页面时检查是否页面内容已做了修改 ------------
function prompt_check_change(theform,strurl)
{
  if(ask_save_change(theform) == "no")
    {
	  if(strurl.length != 0) document.location=strurl;
	  else {history.go(-1);}
	}
  else
    {
	  theform.txt_url.value=strurl;
	  theform.submit(true);
	}
}

//--------- 当关闭页面时检查是否页面内容已做了修改 ------------
function prompt_check_close(theform)
{
  if(ask_save_change(theform) == "no")
    {window.close();}
  else
    {
	  theform.txt_url.value="close";
	  theform.submit(true);
	}
}

//--------- 当连接到别的页面时检查是否页面内容已做了修改(新2001-4-11) ------------
function prompt_save_change(theform,strurl,strexec)
{
  if(ask_save_change(theform) == "no")
    {document.location=strurl;}
  else
    {
	  if(eval(strexec) != false)
	  {
	    theform.txt_url.value=strurl;
	    theform.submit(true);
	  }
	}
}

//--------- 当关闭页面时检查是否页面内容已做了修改(新2001-4-11) ------------
function prompt_close_window(theform,strexec)
{
  if(ask_save_change(theform) == "no")
    {window.close();}
  else
    {
	  if(eval(strexec) != false)
	  {
	    theform.txt_url.value="close";
	    theform.submit(true);
	  }
	}
}


//--------- 询问是否删除Check_Box指定的内容 ------------
function prompt_delete_checkbox(theform,strerr)
{
    var bwarning=false;
    if(theform.cb_delete.name == "cb_delete")//---- 单个Check Box时用这种方法 ---------
	  {if(theform.cb_delete.checked == true) bwarning=true;}
    else                             //---- 多个Check Box时用这种方法 ---------
	  {
	    for(var i=0;i<theform.cb_delete.length;i++) 
	    {if(theform.cb_delete[i].checked == true) bwarning=true;}
	  }

    if(bwarning == true)
      {if(confirm("您确定要删除" + strerr + "吗？")) theform.submit(true);}
	else
	  {theform.submit(true);}
}

//--------- 询问是否删除Check_Box指定的内容(不一定使用) ------------
function ask_delete_checkbox(theform)
{
    var bwarning=false;
    if(theform.cb_delete.name == "cb_delete")//---- 单个Check Box时用这种方法 ---------
	  {if(theform.cb_delete.checked == true) bwarning=true;}
    else                             //---- 多个Check Box时用这种方法 ---------
	  {
	    for(var i=0;i<theform.cb_delete.length;i++) 
	    {if(theform.cb_delete[i].checked == true) bwarning=true;}
	  }

    if(bwarning == true)
      {
	    if(confirm("您确实要删除它们吗？")) return true;
	    else return false;
	  }
	else
	  {return true;}
}

//*******************************************************************
//*******************************************************************

//-------------------- 进行关键词查询 -----------------------
function fun_search_keywords(theform)
{
   if(theform.txt_keywords.value == "")
    { 
	  alert("请输入您要搜索的关键字. ");
	  return false;
	}
  else
    {
      var searchfor,i,keyword,condition;
      condition="";
      searchfor =  theform.txt_keywords.value.replace("'","''");
      var strSearch = new String(searchfor);
      i = strSearch.indexOf(' ');
      
      if(i>-1){
      
      if (i==0){
      strSearch=strSearch.substring(1,strSearch.length);
      
      }
      while(i>0)
	  {
	   keyword = strSearch.substring(0,i);
	   condition += " and wine_name like '%"+keyword+"%'";
	   
	   strSearch = strSearch.substring(i+1,strSearch.length);
	   
	   i = strSearch.indexOf(' ');  
	  
	  }
	   condition += " and wine_name like '%"+strSearch+"%'";
	   
	  }else{
	   condition += " and wine_name like '%"+strSearch+"%'";}
		   
		   
		     
      
      
	  theform.txtQuery.value=condition;
	}
}
//--------- 弹出窗口与父窗口的显示关系 ---------
function check_opener_status(strurl,itype)
{
  if(self.opener.closed == true)
	  {
		  document.location = strurl;
		}
	else
	  {
		  if(itype == 1) self.opener.document.location = self.opener.document.location.href;
			if(itype == 2) self.opener.document.frm_destinaiton.submit(true);
			self.close();
		}
}

//--------- 父窗口的URL ---------
function get_opener_url(theform)
{
  theform.txt_parent_url.value=self.opener.document.location.href;
}

//----------- Select All CheckBox -------------
function select_all(theform,objall,objselect,objchange)
{
  //  theform代表Form对象
  //  objall代表Select All CheckBox对象
	//  objselect代表各checkbox对象
	//  objchange代表一隐藏文本框对象，指明当前页面是否有内容修改
  if(objall.checked == true)
	{
	  if(objselect.name == "cb_delete")//---- 单个Check Box时用这种方法 ---------
	    {objselect.checked=true;}
	  else                             //---- 多个Check Box时用这种方法 ---------
	    {for(var i=0;i<objselect.length;i++) objselect[i].checked=true;}
	}
  else
	{
	  if(objselect.name == "cb_delete")//---- 单个Check Box时用这种方法 ---------
	    {objselect.checked=false;}
	  else                             //---- 多个Check Box时用这种方法 ---------
	    {for(var i=0;i<objselect.length;i++) objselect[i].checked=false;}
	}
	
  objchange.value="1";
}

//----------- (新)使弹出窗口始终最大化 --------------
function window_position()
{
  window.moveTo(1,1);
  if (document.all) {
	  top.window.resizeTo(screen.availWidth,screen.availHeight-2);
  }
  else if (document.layers) {
	  if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
		  top.window.outerHeight = screen.availHeight;
		  top.window.outerWidth = screen.availWidth;
	 }
   }
}

//------------------------- (新)检测某一表单的同一类文本框内容是否非空且为数字 -------------------------------
//------ theform为表单名,txtname表单中具有同一名字前缀的多个文本框（如txt_price***等） -------------------
//------ str_id所有的相应ID号，strerr为相应的出错信息 ----------------------------------------------------
function verify_form_numeric(theform,txtname,str_id,strname,strerr)
{
    var arrtxt=str_id.split(",");
    var objtxt;
	for(var i=0;i<arrtxt.length;i++)
	{ 
	  var objtxt=eval("document." + theform + "." + txtname + arrtxt[i]);
	  if(typeof(objtxt) == "object")
	  {
	    if(objtxt.value.length > 0)
		{
		  if(isNaN(objtxt.value))
		  {
		    alert("请在" + strname + "区域中输入数字！");
		    return false;
		  }
		}
		else
		{
		  alert("请在" + strname + "区域中输入值！");
		  return false;
		}
	  }
	}
}

//----------- 列标排序 -------------
function fun_sort_column(theform,column_name)
{
  if(theform.txt_column.value == column_name)
  {
    if(theform.txt_sort.value == 'DESC'){theform.txt_sort.value = 'ASC';}
	else{theform.txt_sort.value = 'DESC';}
  }
  else
  {
    theform.txt_column.value = column_name;
	theform.txt_sort.value = 'DESC';
  }
  
  theform.submit(true);
}

//---------- 判断用户是否选择[Other]城市 --------------
function fun_check_city(theform,city_id)
{
  if(parseInt(city_id) == 0)
    {
	  div_city.style.display = "block";
	  theform.txt_city.focus();
	}
  else
    {
	  theform.txt_city.value="";
	  div_city.style.display = "none";
	}
}

//---------- 判断用户是否选择[Other]国家 --------------
function fun_check_country(theform,country_id)
{
  if(parseInt(country_id) == 0)
    {
	  div_country.style.display = "block";
	  theform.txt_country.focus();
	}
  else
    {
	  theform.txt_country.value="";
	  div_country.style.display = "none";
	}
}

//---------- 决定酒标的显示尺寸 --------------

function display_image(strSRC,iWH,iHH)
{ 
  var bigimg=new Image();
  bigimg.src=strSRC
  var iwidth=bigimg.width;
  var iheight=bigimg.height;
  
  if (bigimg.complete) {
	  var imgstr="<img src='" + strSRC + "' border='0' align='absmiddle'";

	  if(iwidth>iWH | iheight>iHH) 
	  {
	  	
	    if(iwidth>iWH & iheight>iHH)
		  {imgstr=imgstr + "' width='" + iWH + "' height='" + iHH + "' ";}
		else
		  {
		    if(iwidth>iWH) 
			  {imgstr=imgstr + strSRC + "' width='" + iWH + "' ";}
			else
			  {imgstr=imgstr + strSRC + "' height='" + iHH + "' ";}
		  }
	  }
	  document.write(imgstr + ">");
  } else document.write('<img src="' + strSRC + '" width="' + iWH + '" align="absmiddle" height="' + iHH + '" border="0">');
}