// JavaScript Document

// update 2011-11-26
function CreateHTTPObject()
{
    var xmlhttp;    
    try
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            xmlhttp = false;
        }
    }
    
    if (!xmlhttp && typeof XMLHttpRequest!='undefined')
    {
        try
        {
			xmlhttp = new XMLHttpRequest();
		}
        catch (e)
        {
			xmlhttp=false;
		}
    }
    
    if (!xmlhttp && window.createRequest)
    {
        try
        {
            xmlhttp = window.createRequest();
        }
        catch (e)
        {
            xmlhttp=false;
        }
    }
    
    return xmlhttp;
}



//多次请求 update 2011-11-25
function HandleRequest(xmlHttp, txtDivID)
{
	if(document.getElementById(txtDivID)==null){return;}
	
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			var response = xmlHttp.responseText;
			document.getElementById(txtDivID).innerHTML = response;
		}
		else if(xmlHttp.status == 404)
		{
			document.getElementById(txtDivID).innerHTML = "Requested URL is not found.";
		}
		else if (xmlHttp.status == 403)
		{
			document.getElementById(txtDivID).innerHTML = "Access denied.";
		}
		else
		{
			document.getElementById(txtDivID).innerHTML = "Status is " + xmlHttp.status;
		}
	}
	else 
	{//显示一个等待响应的图片
        document.getElementById(txtDivID).innerHTML = "loading...";
    }
}




//ajax中的分页函数
function PaginationAjax(txtDivID,thisURL,thisURLpara,thisIsBegin)
{
	var xmlHttp = CreateHTTPObject();
	if (!xmlHttp)
	{
		return; //无法创建 xmlhttp 对象
	}
	
	var url;
	if (thisIsBegin=="begin")
	{
		url = thisURL + "?txtDivID_ajax=" + txtDivID + "&thisURL_ajax=" + thisURL + "&" + thisURLpara;
	}
	else
	{
		url = thisURL + "?" + thisURLpara;
	}
//	xmlHttp.open("GET", url, false);
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function(){HandleRequest(xmlHttp, txtDivID)};
	xmlHttp.send(null);
}
//结束 ajax公共



function GetBrowserInfo()
{
	if(navigator.userAgent.indexOf("MSIE")>0)
	{
		return "MSIE";
	}
	if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)
	{
		return "Firefox";
	}
	if(isSafari=navigator.userAgent.indexOf("Safari")>0)
	{
		return "Safari";
	}
	if(isCamino=navigator.userAgent.indexOf("Camino")>0)
	{
		return "Camino";
	}
	if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0)
	{
		return "Gecko";
	}
}

//格式化字符串
String.prototype.Trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g,"");
}


//获取radio单选值
function GetRadioBoxValue(radioName)
{
	var obj = document.getElementsByName(radioName);		//这个是以标签的name来取控件
	for (i=0;i<obj.length;i++)
	{
		if(obj[i].checked)
		{
			return obj[i].value;
		}
	}
	return "undefined";
}



//开始 公共
function IsDigit()
{
	return ((event.keyCode >= 48) && (event.keyCode <= 57));
}


function IsEmailAddress(sEmail)
{
	if ((sEmail=="")||(sEmail.length<5))
	{
		return false;
	}
	if ((sEmail.charAt(0)==".")||(sEmail.charAt(0)=="@")||(sEmail.indexOf('@', 0) == -1)||(sEmail.indexOf('.', 0) == -1)||(sEmail.lastIndexOf("@")==sEmail.length-1)||(sEmail.lastIndexOf(".")==sEmail.length-1))
	{
		return false;
	}
	return true;
}


function InnerHTML(txtDivID,txtValue)
{
	var OHI = document.getElementById(txtDivID);
	if(OHI == null) { 
		return false;
	}
	OHI.innerHTML=txtValue;
}



//搜索
function CF_Search()
{
if ((document.formSearch.q.value == "" )||(document.formSearch.q.value == "Product name or keyword")){
	alert ("Please input a search term.");
	document.formSearch.q.focus();
	return false;
	}
return true;
}



function InputCss(thisInputID,s)
{
	if (s=="focus")
	{
		thisInputID.className="input_focus";
	}
	else if (s=="error")
	{
		thisInputID.className="input_error";
	}
	else
	{
		thisInputID.className="input_normal";
	}	
}
function InputFocus(thisInputID) {
	thisInputID.className="input_focus";
}

function InputCheckInnerHTML(txtDivID,s,str)
{
	if (s=="ok")
	{
//		var txtValue="<img src=\"/images/ico/ico_t.gif\" hspace=\"5\" border=\"0\" align=\"absmiddle\" />";
		var txtValue="<span class=\"alert_ok\">ok</span>";
	}
	else
	{
//		var txtValue="<img src=\"/images/ico/ico_f.gif\" hspace=\"5\" border=\"0\" align=\"absmiddle\" /><br />" + str;
		var txtValue="<span class=\"alert_ng\">X</span><br />" + str;
	}
	InnerHTML(txtDivID,txtValue);
}



//区域显示、隐藏
//操作单个区域显示隐藏
function AreaShowHide(areaID)
{
	var areaID=document.getElementById(areaID);
	if(areaID.style.display=="none")
	{
		areaID.style.display="";
	}
	else
	{
		areaID.style.display="none";
	}
}

//操作多个区域显示隐藏，无菜单
function AreaMultiShowHide(p_area_prefix,p_sum_area,p_curr_area)
{
	var area_id_current = p_area_prefix + p_curr_area;
	for (i=1;i<p_sum_area+1;i++)
	{
		area_id_circle = p_area_prefix + i;
		if (area_id_current == area_id_circle)
		{
			document.getElementById(area_id_circle).style.display = "";
		}
		else
		{
			document.getElementById(area_id_circle).style.display = "none";
		}
	}
}

//操作多个区域显示隐藏，含切换菜单css
function AreaMultiMenuShowHide(mA,aA,iAll,iA,css_on,css_off)
{
	var m,a,cM,cA;
	cM=mA+iA;
	cA=aA+iA;
	for (i=1;i<iAll+1;i++)
	{
		m=mA+i;
		a=aA+i;
		if (document.getElementById(m)!=undefined) {
			if (m==cM)
			{
				document.getElementById(m).className = css_on;
				document.getElementById(a).style.display = "";
			}
			else
			{
				document.getElementById(m).className = css_off;
				document.getElementById(a).style.display = "none";
			}
		}
		else{alert("undefined! -"+m)}
	}
}




//货币类型转换成数字类型
function GetDecimal2Num(f,c)
{
	var t = Math.pow(10, c);
	return Math.round(f * t) / t;
}



//input错误警告，ajax方式
function AlertInputTxt(is_error,id_input,id_span,str_error)
{
	if (is_error=="yes")
	{
		InputCss(id_input,'error');
		InputCheckInnerHTML(id_span,"ng",str_error);
		return false;
	}
	else
	{
		InputCss(id_input,'');
		InputCheckInnerHTML(id_span,"ok","");
		return true;
	}
}





function GotoTop(a, t){
//	window.scroll(0,0);
	a = a || 1;
	t = t || 100;

	var x1 = 0;
	var y1 = 0;
	var x2 = 0;
	var y2 = 0;
	var x3 = 0;
	var y3 = 0;

	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	var x3 = window.scrollX || 0;
	var y3 = window.scrollY || 0;

	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));

	var speed = 1 + a;
	window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
	if(x > 0 || y > 0) {
		var f = "GotoTop(" + a + ", " + t + ")";
		window.setTimeout(f, t);
	}
//	var speed=2; var time=100; var dy=0; var by=0; var wy=0; if(document.documentElement){ dy=document.documentElement.scrollTop||0; } wy=window.scrollY||0; var y=Math.max(wy,Math.max(by,dy)); window.scrollTo(0,Math.floor(y/speed)); if(y>0){  setTimeout("GotoTop()",time); }
}


//begin js for proup
var millisec = 250;
var timeoutId;
var visiblePopup = null;

function PopupShowHide(popdivId)
{
	if(document.getElementById)
	{
		var obj = document.getElementById(popdivId);
		if(obj.style.visibility == "hidden" || obj.style.visibility == "")
		{
			obj.style.visibility = "visible";
    		PopupHide();
			visiblePopup=popdivId;
		}
		else
			PopupHide();
	}
	return false;
}

function HideDelay()
{
	PopupClear();
	timeoutId = setTimeout(function(){PopupHide()}, millisec);
}

function PopupHide()
{
	PopupClear();
	if(visiblePopup!=null)
	{
		var o = document.getElementById(visiblePopup);		
		visiblePopup=null;
		o.style.visibility = "hidden";
	}
	return false;
}

function PopupClear()
{
	if(typeof timeoutId != "undefined")
	{
		clearTimeout(timeoutId);
	}
}
//end for popup




//以下与多币种相关函数
//function CurrentCurrencyCode(cur_m_v,pa_v_h){
function CurrentCurrencyCode(cur_m_v,cur_all){
	if ((cur_m_v!="")&&(cur_all!="")){
		s_oValue=cur_all.split("|");
		for(i=0;i<s_oValue.length;i++){
			if (cur_m_v!=s_oValue[i]){
				var id_curr_code_v=document.getElementsByName("cc_v_"+s_oValue[i]);
				for(i_cur=0;i_cur<id_curr_code_v.length;i_cur++){
					if (id_curr_code_v[i_cur].style.display!="none"){
						id_curr_code_v[i_cur].style.display="none";
					}
				}
			} else if (cur_m_v==s_oValue[i]) {
				var id_curr_code_v=document.getElementsByName("cc_v_"+cur_m_v);
				for(i_cur=0;i_cur<id_curr_code_v.length;i_cur++){
					if (id_curr_code_v[i_cur].style.display!=""){
						id_curr_code_v[i_cur].style.display="";
					}
				}
			}
		}
		if (GetBrowserInfo()=="Firefox"){document.getElementById("hd_curr_code").innerHTML = cur_m_v;}
	}
}

function ChangeCurrencyCode(cur_m_v,cur_all,p_reload_url){				//修改币种
	InitializeCurrencyCode(cur_m_v);				//重新初始化币种
	CurrentCurrencyCode(cur_m_v,cur_all);			
		
	if (p_reload_url!=""){
		window.location.href=p_reload_url;
	}
}

function InitializeCurrencyCode(cur_m_v){		//初始化币种
	var xmlHttp = CreateHTTPObject();
	if (!xmlHttp)
	{
		return; //无法创建 xmlhttp 对象
	}
		
//	var url = "/ajax/curr_code.asp?curr_code=" + escape(cur_m_v) + "&curr_symbol=" + escape(cur_m_s) + "&show=dv";
	var url = "/ajax/curr_code.asp?curr_code=" + escape(cur_m_v) + "&show=dv";

	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function() {HandleRequest(xmlHttp, "hd_curr_code")};
	xmlHttp.send(null);
}


function AlertFloat(atID,atText,atCSS,atWidth,atTop,atLeft) {			//表单输入错误浮动提示
	var stID="alert_" + atID;
	var str_af_img = ""
	var str_div_css=""
	var str_div_style="";
	if (atCSS=="left") {
		str_div_css="alert_dt_left";
		str_af_img=str_af_img + "<img src=\"/images/ico/at_left.gif\" border=\"0\" class=\"alert_img_left\" />"; }
	else {
		str_div_css="alert_dt_up"; 
		str_af_img=str_af_img + "<img src=\"/images/ico/at_up.gif\" border=\"0\" class=\"alert_img_up\" />";
	}
	str_af_img=str_af_img + "<img src=\"/images/ico/at_close.gif\" border=\"0\" class=\"alert_img_close\" onclick=\"javascript:AlertClean('"+atID+"'); return false;\" />";

	if (atCSS=="left") { str_div_css="alert_dt_left"; } else { str_div_css="alert_dt_up"; }
	if ((atWidth!="")&&(atWidth!=0)) { str_div_style=str_div_style+"width:"+atWidth+"px;" }
	if ((atTop!="")&&(atTop!=0)) { str_div_style=str_div_style+"top:"+atTop+"px;" }
	if ((atLeft!="")&&(atLeft!=0)) { str_div_style=str_div_style+"left:"+atLeft+"px;" }
	
	document.getElementById(stID).className="alert_ot";
	//document.getElementById(stID).innerHTML="<div class=\"" + str_div_css + "\" style=\"" + str_div_style + "\">" + str_af_img + atText + "</div>";
	document.getElementById(stID).innerHTML="<div class=\"alert_dt_err\" >" + atText + "</div>";
}
function AlertClean(atID) {			//清除错误浮动提示
	var stID="alert_" + atID;
	document.getElementById(stID).innerHTML="";
	document.getElementById(stID).className="";
}


//N秒后自动清空指定区域内容
function AreaBlankAuto(rSecs,rSecID,rAreaID){
	if(--rSecs>0) {
		setTimeout("AreaBlankAuto('"+rSecs+"','"+rSecID+"','"+rAreaID+"')",1000);
		if (rSecID!=""){document.getElementById(rSecID).innerHTML = rSecs;}
	} else {
		document.getElementById(rAreaID).innerHTML = "";
	}
}

//自适应ifr高度
function AutoIframeHeight(obj) 
{ 
	var win=obj;	 
	if (document.getElementById)
	{	 
		if (win && !window.opera)	 
		{	 
			if (win.contentDocument && win.contentDocument.body.offsetHeight)	   
				win.height = win.contentDocument.body.offsetHeight; 		
			else if(win.Document && win.Document.body.scrollHeight)	   
				win.height = win.Document.body.scrollHeight;	 
		}	 
	} 
}


//适用在静态页中获取值
function GetValueByID(aid){
	var oVBI = document.getElementById(aid);
	if (oVBI==null){ return 0;}
	return oVBI.value;
}

//适用在静态页中获取值
function SetValueByID(aid, val){
	var oVBI = document.getElementById(aid);
	if (oVBI==null){ return;}
	oVBI.value = val;
}


//后台purl: pro_ajax_RangePrice_admin.asp刷新产品区间价格,currCode-货币代码如 USD 对应 $US
//前台purl: /ajax/pro_ajax_RangePrice_page.asp (没用)
//应用于每个静态页header
function ProRangePriceAjax(purl, txtDivID, proID, currCode)
{
	if(document.getElementById(txtDivID)==null){return "";}
	
	var strSendTo="proID="+escape(proID)+"&currCode="+escape(currCode)+"&rnd="+Math.random();
	var url = purl +"?" + strSendTo;
	
	var xmlHttp = CreateHTTPObject();
	if (!xmlHttp)
	{
		return; //无法创建 xmlhttp 对象
	}
	//alert(url);
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function () { HandleRequest(xmlHttp, txtDivID); };
	xmlHttp.send(null);

}

//读取指定名称的cookie
function GetCookieByKey(keyname)
{
	var cookies= document.cookie;
	if (cookies==null){return "";}
	var start= cookies.indexOf(keyname + "=");
	if(start==-1)
	{
		return "";	
	}
	var end = cookies.indexOf(";", 1 + start);
	if(end==-1){
		end	 = cookies.length;
	}
	var arrkeys = cookies.substring(start, end).split('&')[0].split('=');
	return unescape(arrkeys[1]);	
} 



//计算单款产品金额小计(价格x数量): 多币种
function ProQtySubTotal(thisInputID,thisInputValueOri,ProPrice,thisSpanID, ProID, currCode)
{
	//thisInputID = document.getElementById("oQty");
	if(thisInputID==null || thisInputID==""){return;}
	
	var thisInputValue;
	var thisSubTotalValue;
	var txt_thisSpan="";
	thisInputValue=thisInputID.value;
	
	if(currCode==null) { currCode='';}
	
	if ((ProPrice!="")&&(ProPrice!=null)&&(ProPrice!=0))
	{
		if(thisInputValue>100000){thisInputID.value=100000;}

		//if (thisInputValue!=thisInputValueOri)	//此处bug,当==原值时不执行,不能更新
		//{
			var ProPrice=ProPrice.replace(",","");
			
			var xmlHttp = CreateHTTPObject();
			if (!xmlHttp)
			{
				return; //无法创建 xmlHttp 对象
			}
	
			var url = "/ajax/curr_qty_subtotal.asp?ProID="+escape(ProID)+"&currCode="+escape(currCode) + "&ProPrice=" + escape(ProPrice) + "&Qty=" + escape(thisInputValue);
//			alert(url);
//			xmlHttp.open("GET", url, false);
			xmlHttp.open("GET", url, true);
			xmlHttp.onreadystatechange = function(){HandleRequest(xmlHttp, thisSpanID)};
			xmlHttp.send(null);
		//}
	}
}



function ChkScoreNum(currObj){
	var s = currObj.value;
	currObj.value = s.replace(/[^\d]/g,"");	
}

//return bool
function regExTest(s, re){
   var r = re.test(s); 
   return(r); 
}



function HideOrVisible(id, eventb){
	var oid= document.getElementById(id);
	if (oid==null) return;
	if(eventb){
		oid.style.display="block";
		oid.style.visibility="visible";
	}
	else{
		oid.style.display="none";
		oid.style.visibility="hidden";
	}
	
}


/*email来访跟踪*/
function emailDistrib(){
	var thisURL,nLength,offSet,paraInfo;
	thisURL = window.location.href;				//获取当前页面的url
	nLength = thisURL.length;					//获取url的长度
	offSet = thisURL.indexOf("?");				//设置参数字符串开始的位置
	if(offSet>0) {
		paraInfo = thisURL.substr(offSet,nLength)+"&t="+Math.random();//取出参数字符串 这里会获得类似“id=1”这样的字符串
	}
	else {
		paraInfo = "?t="+Math.random();
	}
	setTimeout(function(){
		var xmlHttp = CreateHTTPObject();
		if (!xmlHttp)
		{
			return; //无法创建 xmlHttp 对象
		}
		xmlHttp.open("GET","/ajax/email_Distrib.asp"+paraInfo,false);
		xmlHttp.send(null);
//		alert(xmlHttp.responseText);
	},3000);
}


