
//These var value may be changed when deployed
var wmx_clid ="asusesmax";
var wmx_cpid="ES340";
var counterURL = "http://track.asus.com/web_service/counter/counter.aspx";
var sr_cid  = "_%BEGIN%_asusesmax_%END%_";
var sr_new = "{isnew}";


//These var value may be changed when deployed
var listTrack = 7;
var trackURL = "http://track.asus.com/web_service/list/add_s.aspx?cli="+wmx_clid+"&li="+listTrack;
var strAccount = encodeURIComponent(wmx_GetCookie("WMX_Account"));
var imgTrack;


// unchange part
var wmx_string, wmx_thisURL, wmx_title; 
wmx_from = document.referrer;
wmx_thisURL = document.URL;
wmx_thisURL = Replace_URL(wmx_thisURL);

// wmx_title = document.title;
wmx_title = pagename;

var sessionKey = "wmx_" + wmx_clid;
var oDate = new Date();
var nStartTime = oDate.getTime();
wmx_counter();


var imgCounter;
function wmx_counter()
{
	if(wmx_title == ""){
		wmx_title = wmx_thisURL;
		var nIndex = wmx_title.indexOf("?");
		if(nIndex != -1) wmx_title = wmx_title.substring(0, nIndex);
		nIndex = wmx_title.indexOf("#");
		if(nIndex != -1) wmx_title = wmx_title.substring(0, nIndex);
		nIndex = wmx_title.lastIndexOf("/");
		if(nIndex != -1) {
			var nLen = wmx_title.length;
			wmx_title = wmx_title.substring(nIndex+1, nLen);
		}
	}
	var strSrc = counterURL+"?client="+wmx_clid+"&campaign="+wmx_cpid+"&url="+escape(wmx_from)+"&pname="+escape(wmx_title)+"&this_url="+escape(wmx_thisURL);
	if (navigator.userAgent.indexOf("IE")> -1) {
		if (imgCounter != null) imgCounter.removeNode(true);
		var imgCounter = document.createElement("div");
		
		if (sr_new == "0")
			imgCounter.innerHTML ="<img width=0 height=0 src="+strSrc+" onerror='wmx_track()'>";
		else
		{		
			imgCounter.innerHTML ="<img width=0 height=0 src="+strSrc+">";
			wmx_track();
		}
	} 
	else {
		document.write("<layer visibility=hide>"); 
		document.write("<img height=1 width=1 border=0 SRC="+strSrc+"></layer>"); 
	}
}
 


function wmx_track()
{
	var oDate1 = new Date();
	var strRef = encodeURIComponent(wmx_from);
	var strThis = encodeURIComponent(wmx_thisURL);
	var strSrc = trackURL+"&11="+wmx_cpid+"&6=W_S_DATE&9="+strRef+"&1="+strThis+"&5=W_S&7=W_S&8=W_S";
	strSrc += "&4="+strAccount+"&2=W_S_UID&10=W_S_IP&3="+wmx_GetSessionCookie()+"&20="+wmx_GetHttpUrl(wmx_thisURL)+"&21="+encodeURIComponent(wmx_GetHttpUrlParam(wmx_thisURL));
	if (navigator.userAgent.indexOf("IE")> -1) {
		if (imgTrack != null) imgTrack.removeNode(true);
		var imgTrack = document.createElement("div");
		imgTrack.innerHTML ="<img width=0 height=0 src="+strSrc+" >";
	} 
	else {
		document.write("<layer visibility=hide>"); 
		document.write("<img height=1 width=1 border=0 SRC="+strSrc+"></layer>"); 
	}
}
 
function wmx_GetSessionCookie()
{
	var oDate = new Date();
	var oDate1 = new Date();
	oDate1.setDate(1);
	oDate1.setHours(0,0,0,0);
	var sValue;
	var strSessionCookie = wmx_GetCookie(sessionKey);

  	if ( strSessionCookie == null) {
		var nPrefix = (oDate.getTime()-oDate1.getTime()) / (1000*60);
		sValue = parseInt(40000*Math.random()) + parseInt(nPrefix * 40000);
  		document.cookie = sessionKey + "=" + escape(sValue)+"; path=/";
	}

	var s = wmx_GetCookie(sessionKey);
	return (s);  
}

function wmx_GetCookie(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++) {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return null;
}


function wmx_GetHttpUrl(strUrl)
{
	var nPos = strUrl.indexOf("?");
	if (nPos != -1)
		return (strUrl.substring(0,nPos));
	else
		return (strUrl);
}

function wmx_GetHttpUrlParam(strUrl)
{
	var strResParam = "";
	var strUnListParam = "";
	var oParam, oTmpArray;
	var nPos = strUrl.indexOf("?");	
	
	var strFileName = "products.aspx";
	var oArray = ["l1","l2","l3","l4","model"];
	var oValueArray = new Array(oArray.length);
	
	var strTmp = strUrl.substring(0,nPos);
	var nPos2 = strTmp.lastIndexOf("/");

	if (nPos != -1)
		strTmp = strTmp.substring(nPos2+1,strTmp.length);
	else
		strTmp = "";		

		
	if (nPos != -1)
	{
		var strSrcParam = strUrl.substring(nPos+1,strUrl.length);
		oParam = strSrcParam.split('&');
		oParam.sort();
		
		if (strTmp == strFileName)  //  process if filename is products.aspx
		{
			for (i=0; i<oParam.length; i++)
			{	      	      		      		
			   oTmpArray = oParam[i].split('=');
	
			   var nIndex = Common_Array_IndexOf(oArray,oTmpArray[0]);		   
			   if (nIndex >=0)
			   {
			       oValueArray[nIndex] = oTmpArray[1];
			   }		       
			   else
			   {
			   	   if (strUnListParam != "")
			          strUnListParam += "&";		   
			          
			       strUnListParam += oTmpArray[0] + "=" + oTmpArray[1];
			   }		       		
			}
	
			for (i=0; i<oArray.length; i++)
			{
			   if (strResParam != "")
			      strResParam += "&";
			      		
			   if (typeof(oValueArray[i]) == "undefined")
			       strResParam += oArray[i] + "=";		   
			   else		   		      		
			       strResParam += oArray[i] + "=" + oValueArray[i];
			}	    
			
			if (strUnListParam != "")
			   strResParam += "&" + strUnListParam
		}
		else  // Normal process
		{
			for (i=0; i<oParam.length; i++)
			{
			   if (strResParam != "")
			      strResParam += "&";
			      
				strResParam += oParam[i];      
			}
		}		   

	}

	return (strResParam);	
}

function Common_Array_IndexOf(oArray, strValue)
{
    var nIndex = -1;
    for (var i=0; i < oArray.length; i++)
    {
        if (oArray[i] == strValue)
            nIndex = i;
    }
    
    return (nIndex);
}


function Replace_URL(strSrcURL)
{  
   // -----------------------------------------------
   
   var oSrcArray = new Array(37);
   var oAllowArray = new Array(37);
   
   oSrcArray[0] = "http://taiwan.asus.com.tw/";
   oAllowArray[0] = "http://tw.asus.com/";
   
   oSrcArray[1] = "http://www.asus.gr/";
   oAllowArray[1] = "http://gr.asus.com/";  

   oSrcArray[2] = "http://www.asus.nl/";
   oAllowArray[2] = "http://nl.asus.com/"; 

   oSrcArray[3] = "http://france.asus.com/";
   oAllowArray[3] = "http://fr.asus.com/";
   
   oSrcArray[4] = "http://www.asus.es/";
   oAllowArray[4] = "http://es.asus.com/";  

   oSrcArray[5] = "http://www.asus.fi/";
   oAllowArray[5] = "http://fi.asus.com/"; 

   oSrcArray[6] = "http://www.asus.it/";
   oAllowArray[6] = "http://it.asus.com/";
   
   oSrcArray[7] = "http://www.asus.ch/";
   oAllowArray[7] = "http://ch.asus.com/";  

   oSrcArray[8] = "http://www.asus.cz/";
   oAllowArray[8] = "http://cz.asus.com/"; 

   oSrcArray[9] = "http://www.asus.dk/";
   oAllowArray[9] = "http://dk.asus.com/";
   
   oSrcArray[10] = "http://www.asus.se/";
   oAllowArray[10] = "http://se.asus.com/";  

   oSrcArray[11] = "http://www.asus.no/";
   oAllowArray[11] = "http://no.asus.com/"; 

   oSrcArray[12] = "http://www.asus.com.pl/";
   oAllowArray[12] = "http://pl.asus.com/";
   
   oSrcArray[13] = "http://www.asustek.pl/";
   oAllowArray[13] = "http://pl.asus.com/";  

   oSrcArray[14] = "http://www.asus.pl/";
   oAllowArray[14] = "http://pl.asus.com/"; 

   oSrcArray[15] = "http://www.asus.at/";
   oAllowArray[15] = "http://de.asus.com/";
   
   oSrcArray[16] = "http://at.asus.com/";
   oAllowArray[16] = "http://de.asus.com/";  

   oSrcArray[17] = "http://www.asuscom.de/";
   oAllowArray[17] = "http://de.asus.com/"; 

   oSrcArray[18] = "http://www.asus.de/";
   oAllowArray[18] = "http://de.asus.com/";
   
   oSrcArray[19] = "http://www.asus.com.mx/";
   oAllowArray[19] = "http://ar.asus.com/";  

   oSrcArray[20] = "http://mx.asus.com/";
   oAllowArray[20] = "http://ar.asus.com/"; 

   oSrcArray[21] = "http://www.asus.com.ar/";
   oAllowArray[21] = "http://ar.asus.com/";
   
   oSrcArray[22] = "http://my.asus.com/";
   oAllowArray[22] = "http://my.asus.com/";  

   oSrcArray[23] = "http://www.asus.com.au/";
   oAllowArray[23] = "http://au.asus.com/"; 

   oSrcArray[24] = "http://www.asus.com.ph/";
   oAllowArray[24] = "http://ph.asus.com/";
   
   oSrcArray[25] = "http://www.asus.ph/";
   oAllowArray[25] = "http://ph.asus.com/";  

   oSrcArray[26] = "http://www.asus.sg/";
   oAllowArray[26] = "http://sg.asus.com/"; 

   oSrcArray[27] = "http://www.asus.co.th/";
   oAllowArray[27] = "http://th.asus.com/";
   
   oSrcArray[28] = "http://www.asuscom.ru/";
   oAllowArray[28] = "http://ru.asus.com/";  

   oSrcArray[29] = "http://www.asus.co.jp/";
   oAllowArray[29] = "http://jp.asus.com/"; 

   oSrcArray[30] = "http://www.asus-korea.co.kr/";
   oAllowArray[30] = "http://kr.asus.com/";
   
   oSrcArray[31] = "http://www.asus.com.cn/";
   oAllowArray[31] = "http://cn.asus.com/";  

   oSrcArray[32] = "http://www.asus.com.tr/";
   oAllowArray[32] = "http://tr.asus.com/"; 

   oSrcArray[33] = "http://www.asus.lk/";
   oAllowArray[33] = "http://lk.asus.com/";

   oSrcArray[34] = "http://www.asus.com.tw/";
   oAllowArray[34] = "http://www.asus.com/";  

   oSrcArray[35] = "http://www.asus.net.nz/";
   oAllowArray[35] = "http://www.asus.com/"; 

   oSrcArray[36] = "http://www.asus.co.nz/";
   oAllowArray[36] = "http://www.asus.com/";

   // -----------------------------------------------   


   var strResultUrl = strSrcURL;
	
   for (i=0; i<oSrcArray.length; i++)
   {
      strResultUrl = strSrcURL.replace(oSrcArray[i],oAllowArray[i]);
      if (strSrcURL != strResultUrl)
         break;
   }
	
   return strResultUrl;
   
}


