var ID_COOKIE_NAME = 'ECOSMEMID';
var CTRY_COOKIE_NAME = 'ECOSCTRY';
var SERVER_URL = 'http://www.ecosreg.com/ecosway/';
var SERVER_URL_SHORT = 'http://www.ecosreg.com/';
var LANG_COOKIE_NAME = 'LANG';

function resetId() {
  document.main.shopperid.value = '';
}

function shopperidEnter(act, ctry, lang) {
  var shopperid = document.main.shopperid.value;
  if (window.event && window.event.keyCode == 13)
  {
    if(!shopperid || shopperid == '' || shopperid == null || shopperid == 'null') {
	  if(lang == 'TW')
		alert('請輸入業主編碼');
	  else
	    alert('请输入业主编码');
	} else {
      homepage_redirect(shopperid, ctry, act, lang);
	}
  } else
    return true;
}

function index_homepage_redirect(ctry, lang) {
  var shopperid = document.main.shopperid.value;
  
  if(!shopperid || shopperid == '' || shopperid == null || shopperid == 'null') {
    if(lang == 'zh_TW')
      alert('請輸入業主編碼');
    else if(lang == 'zh_CN')
	  alert('请输入业主编码');
    else
	alert('Please enter Business Owner ID');
  } else {  
    homepage_redirect(shopperid, ctry, 'HOME', lang);
  }
}

function register_homepage_redirect(ctry, lang) {
  var shopperid = document.main.shopperid.value;
  
  if(!shopperid || shopperid == '' || shopperid == null || shopperid == 'null') {
    if(lang == 'zh_TW')
      alert('請輸入業主編碼');
    else if(lang == 'zh_CN')
	  alert('请输入业主编码');
    else
	alert('Please enter Business Owner ID');
  } else {  
    homepage_redirect(shopperid, ctry, 'REG', lang);
  }
}

function renewal_homepage_redirect(ctry, langSel) {
  if(ctry == 'CN')
	document.header.action = SERVER_URL + 'cn/MemberRenewalLogin_cn.jsp?common_locale=' + langSel;
  else
	document.header.action = SERVER_URL + 'hk/MemberRenewalLogin_hk.jsp?common_locale=' + langSel;
	
//document.header.submit();
window.location = document.header.action;
}

function homepage_redirect(shopperid, ctry, action, lang)
{
  var ctryId = readCookie(CTRY_COOKIE_NAME);//ignore ctry if have value
  if (ctryId == null || ctryId == '')
	ctryId = ctry;
  
  createCookie(ID_COOKIE_NAME, shopperid, '');
  document.main.action = SERVER_URL + 'Redirect.jsp?shopperid=' + shopperid + '&mallind=' + ctryId + '&action=' + action + '&common_locale=' + lang;
  document.main.submit();
}

function indexPopup(ctry, lang)
{
	var memberId = readCookie(ID_COOKIE_NAME);	
	var popupPage = '';
	
	createCookie(CTRY_COOKIE_NAME,ctry,'');
		
	if(memberId == '' || memberId == null || memberId == 'null') {
		if ( (ctry == 'CN') || (ctry == 'MC') )
		{
			if(lang == 'zh_CN')
				popupPage = 'china/zh_cn/ID_Country.html';
			else
				popupPage = 'china/tw_cn/ID_Country.html';
		} 
		else if (ctry == 'HK')
			popupPage = 'hongkong/tw_hk/ID_Country.html';
	
		window.open(popupPage,'blank','width=470,height=180,toolbar=no,statusbar=no,resizable=no,scrollbars=no');
	}
	else {
		if (ctry == 'CN')
			window.location = SERVER_URL_SHORT + 'china/zh_cn/freedom_cn.html';
		else if (ctry == 'MC')
			window.location = SERVER_URL_SHORT + 'china/tw_cn/freedom_cn.html';
		else
			window.location = SERVER_URL_SHORT + 'hongkong/tw_hk/freedom_hk.html';
	}
}

function registerPopup(ctry, lang)
{
	var memberId = readCookie(ID_COOKIE_NAME);
	var ctryId = readCookie(CTRY_COOKIE_NAME);//ignore ctry if have value
  	if (ctryId == null || ctryId == '')
		ctryId = ctry;
	if(memberId == '' || memberId == null || memberId == 'null') {
		window.open('Reg_ID.html','blank','width=470,height=180,toolbar=no,statusbar=no,resizable=no,scrollbars=no');
	}
	else {
		if(ctryId == 'CN')
			window.location = SERVER_URL + 'cn/MemberRegisterBO_cn.jsp?referId=' + memberId + '&common_locale=' + lang;
		else if(ctryId == 'HK')
			window.location = SERVER_URL + 'hk/MemberRegisterBO_hk.jsp?referId=' + memberId + '&common_locale=' + lang;
		else if(ctryId == 'MC')
			window.location = SERVER_URL + 'mc/MemberRegisterBO_mc.jsp?referId=' + memberId + '&common_locale=' + lang;
		
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') 
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) 
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie() {    
	createCookie(ID_COOKIE_NAME,"",-1);
	createCookie(CTRY_COOKIE_NAME,"",-1);
	createCookie(LANG_COOKIE_NAME,"",-1);
}

function submitSearchProductForm(){
  var myForm = document.header;
  var searchProductType = myForm.searchProductType.options[myForm.searchProductType.selectedIndex].value;

  if (isEmpty(myForm.searchStr.value) && searchProductType == "1"){
    alert("请输入产品编号。");
  }else if (isEmpty(myForm.searchStr.value) && searchProductType == "2"){
    alert("请输入产品名称。");
  }else{
    myForm.submit();
  } 
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}