function getEnv() {
  var url = document.location.href.toLowerCase();
  if (url.indexOf("//hk.asia.av.dev.hk.agenda-asia.com")>= 0){return "dev";}
  else if (url.indexOf("//hk-staging.asia.acuvue.com")>= 0){return "stag";}
	return "prod";
}
var env = getEnv(); // dev = developement, stag = staging, prod = production

var wwwroot = "http://hk.asia.acuvue.com/av/";
var ssl_wwwroot = "https://hk.asia.acuvue.com/av/";
var us_wwwroot = "http://www.acuvue.com.hk/";
if (env == "dev")
{
   wwwroot = "http://hk.asia.av.dev.hk.agenda-asia.com/av/";
   ssl_wwwroot = "http://hk.asia.av.dev.hk.agenda-asia.com/av/";
   us_wwwroot = "http://www.av.dev.hk.agenda-asia.com/";
} 
else if (env == "stag")
{
	wwwroot = "http://hk-staging.asia.acuvue.com/av/";
	ssl_wwwroot = "https://hk-staging.asia.acuvue.com/av/";
	us_wwwroot = "http://stage.acuvue.com.hk/";
}

var pid = "0", spid = "0", scid = "0", conid = "0";


function QueryString(key) {
 var value = null;
  for (var i = 0; i < QueryString.keys.length; i++) {
   if (QueryString.keys[i] == key) {
    value = QueryString.values[i];
    break;
   }
  }
  return value;
}
//
QueryString.keys = new Array();
QueryString.values = new Array();
function QueryString_Parse() {
 var query = window.location.search.substring(1);
 var pairs = query.split("&");
 for (var i = 0; i < pairs.length; i++) {
  var pos = pairs[i].indexOf('=');
  if (pos >= 0) {
   var argname = pairs[i].substring(0, pos);
   var value = pairs[i].substring(pos + 1);
   QueryString.keys[QueryString.keys.length] = argname;
   QueryString.values[QueryString.values.length] = value;
   /*if (argname == "vtpe") {
    popupType = value;
   } else if (argname == "vid") {
    videoID = value;
   }*/
   switch(argname){
	   case "pid":
	   		pid = value;
	   break;
	   case "spid":
	   		spid = value;
	   break;
	   case "scid":
	   		scid = value;
	   break;
	   case "conid":
	   		conid = value;
	   break;
	   case "ac":
		   var spec0_array = new Array("astig005", "astig006", "astig010", "astig011")
		   var spec1_array = new Array("astig007", "astig008", "astig019", "astig012")
		   var spec = ""
		   for (var i=0; i<spec0_array.length; i++){
			   if (value == spec0_array[i]){
				   spec = "1"
			   }
		   }
		   for (var i=0; i<spec1_array.length; i++){
			   if (value == spec1_array[i]){
				   spec = "2"
			   }
		   }
		   if (spec == "1"){
		 	  pid = 1;
		   }else if (spec == "2"){
			    pid = 1;
				spid = 1;
				scid = 1;
		   }
	   break;
	   default:
	   break;
   }
  }
 }
}
QueryString_Parse();
//	

document.write('<script language="javascript" src="include/js/analytics_astig.js"></script>');
openWin = function(url,name,width,height,xpos,ypos,chrome,scroll,resizable){
  var x, y, w, h, moveX=0, moveY=0, features="";
  chrome = chrome ? "yes" : "no";
  scroll = scroll ? "yes" : "no";
  resizable = resizable ? "yes" : "no";
  features += "toolbar="+chrome+",location="+chrome+",status="+chrome+",menubar="+chrome;
  features += ",scrollbars="+scroll+",resizable="+resizable;
  if(width) features += ",width="+width;
  if(height) features += ",height="+height;
  if(xpos && window.screen){
    w = window.screen.availWidth;
    width = parseInt(width);
    switch(xpos){
      case "left": x = 0; break;
      case "center": x = (w-width)/2; break;
      case "right": x = w-width; break;
      default: x = xpos;
    }
    features += ",screenX="+x+",left="+x;
    var moveX = x;
  }
  if(ypos && window.screen){
    h = window.screen.availHeight;
    height = parseInt(height);
    switch(ypos){
      case "top": y = 0; break;
      case "middle": y = (h-height)/2; break;
      case "bottom": y = h-height; break;
      default: y = ypos;
    }
    features += ",screenY="+y+",top="+y;
    var moveY = y;
  }
  features +=",left="+moveX+",top="+moveY;
//  openWinReference = window.open('',name,features);
var openWinReference;
eval("openWinReference = window.open(url, name, features);");  
//  if(moveX || moveY){
 //   // position the window for browsers that don't recognize screenX, screenY
  //  openWinReference.moveTo(moveX,moveY);
//  }
 //   openWinReference = window.open(url,name);
	openWinReference.focus();
}

function openproductpage(){
	// for banner in video section 
	doTracking("Products Index Page");
	//window.open("http://www.acuvue.com.hk/products/index.html","newWin","");
	window.open(us_wwwroot+"contact-lenses-products/index.html","newWin","");
}


function openeFPP(){
	//openWin(wwwroot+"promotions/eCampaign.aspx?campaignId=4","eFPP",613,640,"center","middle",false,"scroll");
	window.open(wwwroot+"promotions/index.html","newWin","");
}
function presurvey(){
	//alert(1);
	doTracking("PRESURVEY");
	window.open(wwwroot+"forms/promotions/AstigCampaign/survey/PreSurvey.aspx","newWin","");
}
//hk.asia.acuvue.com/av/forms/
function aafa_form(){
	doTracking("AAFA EFPP FORM");
	window.open(wwwroot+"forms/promotions/eCampaign.aspx?campaignId=4&source=News_Promotions-Main_page-advance_astig_efpp","newWin","");
}
function omega_form(){
//	hk.asia.acuvue.com/av/forms/
	doTracking("OMEGA EFPP FORM");
	//window.open(wwwroot+"forms/promotions/eCampaign.aspx?campaignId=11&source=News_Promotions-Main_page-astig_efpp","newWin","");
	openWin(wwwroot+"promotions/frequentpurchase.aspx","newWin",613,640,"center","middle",false,"scroll");
	
}
function cypress_form(){
//	hk.asia.acuvue.com/av/forms/
	doTracking("CYPRESS EFPP FORM");
	//window.open(wwwroot+"forms/promotions/eCampaign.aspx?campaignId=31","newWin","");
	openWin(wwwroot+"promotions/frequentpurchase.aspx","newWin",613,640,"center","middle",false,"scroll");

}
function new_wearer_form(){	
	doTracking("eTrial FORM");	
	var _astig_l = ""+getCookie('astig_l');
	var _astig_r = ""+getCookie('astig_r');
	
	if(_astig_l == "null"){
		_astig_l = "";
	}
	if(_astig_r == "null"){
		_astig_r = "";
	}
	var form = document.createElement("form");
	form.setAttribute("method", "post");
    form.setAttribute("action", ssl_wwwroot + "forms/promotions/etrial.aspx");
    form.setAttribute("target", "_blank");

    var hiddenField1 = document.createElement("input");    
    hiddenField1.setAttribute("type", "hidden");                  
    hiddenField1.setAttribute("name", "r");
    hiddenField1.setAttribute("value", _astig_r);
    form.appendChild(hiddenField1);
    var hiddenField2 = document.createElement("input");              
    hiddenField2.setAttribute("type", "hidden");        
    hiddenField2.setAttribute("name", "l");
    hiddenField2.setAttribute("value", _astig_l);
    form.appendChild(hiddenField2);
    
    document.body.appendChild(form);              
    form.submit();

}

function new_wearer_form2(){
	// for banner in video section 
	doTracking("eTrial FORM - ALL");
	var _astig_l = ""+getCookie('astig_l');
	var _astig_r = ""+getCookie('astig_r');
	
	if(_astig_l == "null"){
		_astig_l = "";
	}
	if(_astig_r == "null"){
		_astig_r = "";
	}

	var form = document.createElement("form");
	form.setAttribute("method", "post");
    form.setAttribute("action", ssl_wwwroot + "forms/promotions/etrial.aspx");
    form.setAttribute("target", "_blank");

    var hiddenField1 = document.createElement("input");    
    hiddenField1.setAttribute("type", "hidden");                  
    hiddenField1.setAttribute("name", "r");
    hiddenField1.setAttribute("value", _astig_r);
    form.appendChild(hiddenField1);
    var hiddenField2 = document.createElement("input");              
    hiddenField2.setAttribute("type", "hidden");        
    hiddenField2.setAttribute("name", "l");
    hiddenField2.setAttribute("value", _astig_l);
    form.appendChild(hiddenField2);
    
    document.body.appendChild(form);              
    form.submit();
}

function wheretonwpromotion(){
	doTracking("NEW WEARER PROMOTION ECP LIST");
	//window.open(wwwroot+"forms/promotions/newwearer/info.aspx","new","");
	//window.open(wwwroot+"forms/promotions/newwearer/info.aspx#ecplist","new","");
	
	var _astig_l = ""+getCookie('astig_l');
	var _astig_r = ""+getCookie('astig_r');
	
//	if(_astig_l != ""){
		var form = document.createElement("form");
		form.setAttribute("method", "post");
	    form.setAttribute("action", wwwroot+"forms/promotions/newwearer/info.aspx#ecplist");
	    form.setAttribute("target", "_blank");
	
	    var hiddenField1 = document.createElement("input");        
	    hiddenField1.setAttribute("type", "hidden");                   
	    hiddenField1.setAttribute("name", "r");
	    hiddenField1.setAttribute("value", _astig_r);
	    form.appendChild(hiddenField1);
	    var hiddenField2 = document.createElement("input");       
	    hiddenField2.setAttribute("type", "hidden");                     
	    hiddenField2.type = "hidden";
	    hiddenField2.setAttribute("name", "l");
	    hiddenField2.setAttribute("value", _astig_l);
	    form.appendChild(hiddenField2);
	    
	    document.body.appendChild(form);                
	    form.submit();
 // 	}
}

function opennwpage(){
	doTracking("NEW WEARER PROMOTION BRANDSITE PAGE");
	window.open("http://www.acuvue.com.hk/welcome");
}

function openpromotionpage(){
	doTracking("NEW WEARER PROMOTION TO PROMOTION PAGE");
	window.open("http://hk.asia.acuvue.com/av/promotions/index.html");
}

function opennwtandc(){
	doTracking("NEW WEARER PROMOTION TNC");
	openWin("nw_promo_tnc.html","nWinTC",613,640,"center","middle",false,"scroll");
}


function whereToFind(){
	//alert(1);
	doTracking("WHERETOFIND");
	window.open(wwwroot+"products/WhereToFind.aspx?productid=14","_ecp","");
}
function contactus(){
	window.open(wwwroot+"forms/contactUs.aspx","new","");
	
}
function legal_notice(){
	window.open(us_wwwroot+"common/legal-notice.html","new","");
	
}
function our_credo(){
	window.open(us_wwwroot+"about-acuvue/our-credo.html","new","");
	
}
function privacy(){
	window.open(us_wwwroot+"common/privacy-policy.html","new","");
	
}
function wheretopromotion(){
	doTracking("PROMOTION ECP LIST");
window.open(wwwroot+"forms/promotions/AstigCampaign/WhereToFind.aspx","new","");
}
function whereToFind2(){
	openWin("product/wheretofind.aspx","_ecp",800,600,"center","middle",false,"scroll");
}
function shareWithFriend(){
	doTracking("FORWARD TO FRIEND");
	var path = "/";
	var title = "ACUVUE%26reg%3B%20ADVANCE%26trade%3B%20for%20ASTIGMATISM%20%E6%95%A3%E5%85%89%E6%8B%8B%E6%A3%84%E5%9E%8B%E9%9A%B1%E5%BD%A2%E7%9C%BC%E9%8F%A1";
	openWin(wwwroot+"forms/products/forwardFriends.aspx?ub=3&url="+path+"&title="+title,"ForwardFriends",613,650,"center","middle",false,"scroll");
}
function product_info1(){
	//openWin(us_wwwroot+"products/product_information.html#1","nWin",794,600,"center","middle",false,"scroll");
	openWin(us_wwwroot+"contact-lenses-products/product-information.html#1","nWin",794,600,"center","middle",false,"scroll");
}
function product_info2(){
	//openWin(us_wwwroot+"products/product_information.html#3","nWin",794,600,"center","middle",false,"scroll");
	openWin(us_wwwroot+"contact-lenses-products/product-information.html#3","nWin",794,600,"center","middle",false,"scroll");
}
function product_info3(){
	//openWin(us_wwwroot+"products/product_information.html#2","nWin",794,600,"center","middle",false,"scroll");
	openWin(us_wwwroot+"contact-lenses-products/product-information.html#2","nWin",794,600,"center","middle",false,"scroll");
}

function define_product_page(){
	//window.open(us_wwwroot+"products/av2usp_advance_astig.html","new","");
	window.open(us_wwwroot+"contact-lenses-products/oasys-contact-lenses-astigmatism.html","new","");
}

function acuvue_hk(){
	window.open(us_wwwroot,"new","");
}
function writeObject(str){
	document.write(str);
}

function openQuest(){
//	alert("ok");
	openWin("quest.html","Quest",630,620,"center","middle",false,"");
}

function reserveTrial(epgID){
	doTracking("eTrial FORM");	
	var _astig_l = ""+getCookie('astig_l');
	var _astig_r = ""+getCookie('astig_r');
	
	if(_astig_l == "null"){
		_astig_l = "";
	}
	if(_astig_r == "null"){
		_astig_r = "";
	}

	var form = document.createElement("form");
	form.setAttribute("method", "post");
    form.setAttribute("action", ssl_wwwroot + "forms/promotions/etrial.aspx");
    form.setAttribute("target", "_blank");

    var hiddenField1 = document.createElement("input");    
    hiddenField1.setAttribute("type", "hidden");                  
    hiddenField1.setAttribute("name", "r");
    hiddenField1.setAttribute("value", _astig_r);
    form.appendChild(hiddenField1);
    var hiddenField2 = document.createElement("input");              
    hiddenField2.setAttribute("type", "hidden");        
    hiddenField2.setAttribute("name", "l");
    hiddenField2.setAttribute("value", _astig_l);
    form.appendChild(hiddenField2);
    
    document.body.appendChild(form);              
    form.submit();
}

function openform(){
	doTracking("PROMOTION FORM");
	openWin(wwwroot+"forms/promotions/astigcampaign/index.aspx","nWinForm",613,640,"center","middle",false,"scroll");
}

function opentandc(){
	openWin("tnc.html","nWinTC",613,640,"center","middle",false,"scroll");
}
function openquiztandc(){
	openWin("quiz_tnc.html","nWinQTC",595,400,"center","middle",false,false);
}
function openwinnerlist(){
	doTracking("LUCK DRAW RESULT");
	openWin("luckydraw_result.html","nWinResult",613,640,"center","middle",false,"scroll");
}
function openecp(){
	//openWin("tnc.html","nWinECP",613,640,"center","middle",false,"scroll");
	//window.open("http://hk.asia.acuvue.com/av/products/WhereToFind.aspx");
	window.open(wwwroot+"products/WhereToFind.aspx");

}
function openWhichCon(){ 
	//alert("open which");
	//openWin("http://www.acuvue.com.hk/acuvue_care/acuvue_selection.html","nWin1",595,400,"center","middle");
	openWin(us_wwwroot+"about-contact-lenses/buying/contact-lens-advisor.html","nWin1",640,600,"center","middle");
}

function callFacilitate(pagename){	
	var page_desc = "";
	var flash_url = "";
		
	page_desc = "Astig Minisite - " + pagename;
	flash_url = NI_URL.substring(0,NI_URL.lastIndexOf("/")) + "/flash/" + pagename;		

	
	if(pagename.indexOf("_") > 0){
		NI_SERVICE = pagename.substring(0,pagename.indexOf("_"));
	}else if (pagename.indexOf("_") == 0){
		page_desc = "Astig Minisite - " + NI_SERVICE + pagename;
		flash_url = NI_URL.substring(0,NI_URL.lastIndexOf("/")) + "/flash/" + NI_SERVICE+pagename;	
	}else{
		NI_SERVICE = pagename;
	}
	
	ni_TrackHit(global_te_server, global_te_sitecode, NI_DESCRIPTION, NI_SECTION, NI_SERVICE, NI_TRIGGER, NI_AMOUNT, NI_ADCAMPAIGN, page_desc, flash_url, 1, NI_BASKET_ADD, NI_BASKET_REMOVE, NI_PARAMETERS, NI_IDENTIFIER, NI_PRODUCTS);
}

function doTracking(pagename){	
	callFacilitate(pagename);
}


//
function setCookie(name, value, expires, path, domain, secure) 
{
	var today = new Date();
	today.setTime(today.getTime());
	if (expires)
	{
	  expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime()+(expires));
	document.cookie = name + "=" + escape(value) + 
	((expires)? ";expires=" + expires_date.toGMTString() : "") + 
	((path) ? ";path=" + path : "" ) + 
	((domain) ? ";domain=" + domain : "" ) +
	((secure) ? ";secure" : "" );
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));  
}

function setSource(){
	var loc = document.location.href;
	var ref = document.referrer;

	var lastLoc = getCookie('astig_l');
	var dom = document.domain;
	if (loc.indexOf("ac=") >=0){
		setCookie('astig_l', loc, 1, '/', '', '');		
	}
	if (ref.indexOf(dom) < 0 && ref.indexOf("acuvue") < 0 && ref != ""){
		setCookie('astig_r', ref, 1, '/', '', '');
	}
}
//
function getVisitCookie(){
		var mainMenuObj = isInternetExplorer ? document.all.navigation : document.navigation;
		mainMenuObj.SetVariable("siteCookie", "");
}
//
function setVisitCookie(){
}

function contactus(){
	window.open(wwwroot+"forms/contactUs.aspx","new","");
	
}
function legal_notice(){
	window.open(us_wwwroot+"common/legal-notice.html","new","");
	
}
function our_credo(){
	window.open(us_wwwroot+"about-acuvue/our-credo.html","new","");
	
}
function privacy(){
	window.open(us_wwwroot+"common/privacy-policy.html","new","");
	
}