var expDays = 1; // number of days the cookie should last

var path = "/";

var domain;
var locapath = document.location.href;

if (locapath.toString().search('/preview/')!= -1){
	var page  = "/preview";
} else {
	var page  = "";
}

if (locapath.toString().search('/textonly/')!= -1){
	 page += "/textonly/sc_chi/disclaimer/statment.html";
} else {
	 page += "/sc_chi/disclaimer/statment.html";
}

org_path_start = locapath.indexOf("://") + 3;
org_path_end = locapath.length;
domain = locapath.substring(org_path_start, org_path_end);
if (domain.toString().search('/')!= -1){
org_path_start = 0;
org_path_end = domain.indexOf("/");
domain = domain.substring(org_path_start, org_path_end);
}

function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}
function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
//var path = (argc > 3) ? argv[3] : null;  
//var domain = (argc > 4) ? argv[4] : null;  

var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
//((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
//((expires == null) ? "" : ("; expires=")) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
	var count = GetCookie('sccount');
	if(count == null) {
		SetCookie('count','1');
		return 1;
	} else {
		var newcount = parseInt(count) + 1;
		DeleteCookie('sccount');
		SetCookie('sccount',newcount,exp);
		return count;
   }
}
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function check_email(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0 && str.indexOf(".") > str.indexOf("@")+1);
}

function trim(stringToTrim)
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function check_qform(frm) {
	if (trim(document.qform.name.value) == "") {
		alert("Please input your name");
		return false;
	} else if (!check_email(document.qform.email.value)) {
		alert("Please input your valid email address");
		return false;
	} else if (trim(document.qform.dayno.value) == "" && trim(document.qform.nightno.value) == "") {
		alert("Please input your contact number");
		return false;
	} else if (trim(document.qform.query.value) == "") {
		alert("Please input your comments");
		return false;
	} else {
		document.qform.submit();
		return true;
	}
}

function checkCount() {
	var sccount = GetCookie('sccount');
	if (sccount == null) {
		SetCookie('orgiurl',document.location.href, exp);
		window.location.href=page;
	} else {
		//count++;
		//SetCookie('count', count, exp);
	}
}

//if ((locapath.toString().search('/sc_chi/')!= -1)||(locapath.toString().search('cindex.html')!= -1))   {
//	checkCount();
//}

//Finish Check SC statment
var dept = "/";
var domain = "";
var scserver = "http"+"://sc.info.gov.hk/gb/";
var locapath = document.location.href
//var locapath = "http://www.sar-expertcom.gov.hk/textonly/eindex.html";

var atlang,domname,sitepath,atgb,atindex,org_part;
sc_path = "sc_chi/";
sctxt_path = "textonly/sc_chi/";
tc_path = "tc_chi/";
tctxt_path = "textonly/tc_chi/";
en_path = "english/";
entxt_path = "textonly/english/";
atgb = false;
atindex = false;

if (locapath.toString().search('/sc_chi/')!= -1){
	atgb = true;
	//org_path_start = locapath.indexOf("/gb/") +4;
	//org_path_end = locapath.length;
	//sitepath = locapath.substring(org_path_start, org_path_end);
} else {
	atgb = false;
}
sitepath = locapath.substring(7,locapath.length);

if (locapath.toString().search('/textonly/english/')!= -1){
	atlang = "entxt";
	org_part = entxt_path;
} else if (locapath.toString().search('/textonly/tc_chi/')!= -1){
	atlang = "tctxt";
	org_part = tctxt_path;
} else if (locapath.toString().search('/textonly/sc_chi/')!= -1){
	atlang = "sctxt";
	org_part = sctxt_path;
} else if (locapath.toString().search('/english/')!= -1){
	atlang = "en";
	org_part = en_path;
} else if (locapath.toString().search('/tc_chi/')!= -1){
	atlang = "tc";
	org_part = tc_path;
} else if (locapath.toString().search('/sc_chi/')!= -1){
	atlang = "sc";
	org_part = sc_path;
} else if (locapath.toString().search('/textonly/eindex.html')!= -1){
	atlang = "txteindex";
	org_part = "textonly/eindex.html";
	atindex = true;
} else if (locapath.toString().search('/textonly/cindex.html')!= -1){
	atlang = "txtcindex";
	org_part = "textonly/cindex.html";
	atindex = true;
} else if (locapath.toString().search('eindex.html')!= -1){
	atlang = "eindex";
	org_part = "eindex.html";
	atindex = true;
} else if (locapath.toString().search('cindex.html')!= -1){
	atlang = "cindex";
	org_part = "cindex.html";
	atindex = true;
} else if (locapath.toString().search('sindex.html')!= -1){
	atlang = "sindex";
	org_part = "sindex.html";
	atindex = true;
}

//domain = sitepath.substring(0, sitepath.indexOf("/"));
domain = "202.57.255.178";
subdir = "/preview";

function chglang(lang){

	switch(lang){
		case 'entxt': 
			des_part = entxt_path;
			des_ind_part = "textonly/eindex.html";
			base_path = "http://";
			break;
		case 'tctxt':
			des_part = tctxt_path;
			des_ind_part = "textonly/cindex.html";
			base_path = "http://";
			break;
		case 'sctxt':
			des_part = sctxt_path;
			des_ind_part = "textonly/cindex.html";
			base_path = scserver;
			//base_path = "http://";
			break;
		case 'en':
			des_part = en_path;
			des_ind_part = "eindex.html";
			base_path = "http://";
			break;
		case 'tc':
			des_part = tc_path;
			des_ind_part = "cindex.html";
			base_path = "http://"; 
			if(locapath.toString().search('/press/')){
				sitepath = sitepath.replace('_e.html', '_c.html');
			}
			if(locapath.toString().search('/nutrient/')){
				sitepath = sitepath.replace('.shtml', 'c.shtml');
			}
			break;
		case 'sc':
			des_part = sc_path;
			des_ind_part = "sindex.html";
			//base_path = scserver;
			base_path = "http://";
			if(locapath.toString().search('/press/')){
				sitepath = sitepath.replace('_e.html', '_c.html');
			}
			if(locapath.toString().search('/nutrient/')){
				sitepath = sitepath.replace('.shtml', 'c.shtml');
			}
			//base_path = "http://";
			break;
	}

	if (atindex == true){
		tail = sitepath.replace(org_part, des_ind_part);
	}else{
		tail = sitepath.replace(org_part, des_part);
	}

	//alert(base_path+tail)
	//if(lang == 'sctxt' || lang == 'sc'){
	//	window.location =  "#";
	//} else {
		window.location =  base_path+tail;
	//}

}

function changeLang(lang) {
	var currenturl = document.location.href;
	var indexOfEN = currenturl.indexOf("/english/");
	var indexOfTC = currenturl.indexOf("/tc_chi/");
	var indexOfSC = currenturl.indexOf("/sc_chi/");

	if (indexOfEN != -1) {
		if (lang == 'tc') {
			currenturl = currenturl.substring(0, indexOfEN) + '/tc_chi/' + currenturl.substring(indexOfEN + 9);
		} else if (lang == 'sc') {
			currenturl = currenturl.substring(0, indexOfEN) + '/sc_chi/' + currenturl.substring(indexOfEN + 9);
		}
	} else if (indexOfTC != -1) {
		if (lang == 'en') {
			currenturl = currenturl.substring(0, indexOfTC) + '/english/' + currenturl.substring(indexOfTC + 8);
		} else if (lang == 'sc') {
			currenturl = currenturl.substring(0, indexOfTC) + '/sc_chi/' + currenturl.substring(indexOfTC + 8);
		}
	} else if (indexOfSC != -1) {
		if (lang == 'en') {
			currenturl = currenturl.substring(0, indexOfSC) + '/english/' + currenturl.substring(indexOfSC + 8);
		} else if (lang == 'tc') {
			currenturl = currenturl.substring(0, indexOfSC) + '/tc_chi/' + currenturl.substring(indexOfSC + 8);
		}
	}
	document.location.href = currenturl;
}

function tellafriend( lang )
{
  if (lang == 'eng')
  {
     window.location = subdir + '../tell/tell.asp?base=http%3A//'+ domain + subdir + '../tell/&url=' + escape(window.location);
  }else if (lang == 'chi')
  {
     window.location = subdir + '/tc_chi/tell/tell.asp?base=http%3A//'+ domain + subdir + '/tc_chi/tell/&url=' + escape(window.location);
  }else if (lang == 'schi')
  {
     window.location = subdir + '/sc_chi/tell/tell.asp?base=http%3A//'+ domain + subdir + '/sc_chi/tell/&url=' + escape(window.location);
  }else if (lang == 'eng_txt')  
  {
     window.location = subdir + '/textonly../tell/tell.asp?base=http%3A//'+ domain + subdir + '/textonly../tell/&url=' + escape(window.location);
  }else if (lang == 'chi_txt')
  {
     window.location = subdir + '/textonly/tc_chi/tell/tell.asp?base=http%3A//'+ domain + subdir + '/textonly/tc_chi/tell/&url=' + escape(window.location);
  }else if (lang == 'schi_txt')
  {
     window.location = subdir + '/textonly/sc_chi/tell/tell.asp?base=http%3A//'+ domain + subdir + '/textonly/sc_chi/tell/&url=' + escape(window.location);
  }
  //window.location = '../tell/tell.html?url=' + escape(window.location);
}

function mailThisUrl(lang) {
	var initialsubj;
	var initialmsg;
	
	if (lang == 'eng') {
		initialsubj = "Centre for Food Safety - Consumer Zone";
		initialmsg = "I recommend you to go to Centre for Food Safety - Consumer Zone for more information%0A%0A" + window.location;
	} else {
		initialsubj = "食物安全中心 - 消費者資訊天地";
		initialmsg = "我邀請您到食物安全中心 - 消費者資訊天地%0A%0A" + window.location;
	}
		
	//window.location = "mailto:"+document.eMailer.email.value+"?subject="+initialsubj+"&body="+document.title+" "+u;
	window.location.href = "mailto:?subject="+initialsubj+"&body="+initialmsg;
}

function quickURL(frm_select) {
	if (frm_select.options[frm_select.selectedIndex].value != '')
		document.location = frm_select.options[frm_select.selectedIndex].value;
}

function splitString (stringToSplit,separator) {
 var arrayOfStrings = stringToSplit.split(separator);
 return arrayOfStrings[1];
}

function openWindow(url) {
	win = window.open(url);
	win.window.focus();
}

/* ================================================================ */
/*  Below functions are developed by CAP 4 (Yu-lam HO) in year 2007 - 2008  */
/* ================================================================ */
function openAjax() {
	var ajax; 
	try { 
		ajax = new XMLHttpRequest(); 
	} catch (ee) { 
		try { 
			ajax = new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) { 
			try { 
				ajax = new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (E) { 
				ajax = false; 
			} 
		} 
	} 
	return ajax; 
}

function goToArchiveMonth(lang, yyyy, mm) {
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var year = currentTime.getFullYear();

	var url_path = "cfs_press_archive_" + yyyy + "_" + mm + "_" + lang + ".html";

	check_url(url_path, 'cfs_press', lang);
}

function goToFaArchiveMonth (lang, yyyy, mm) {
	var url_path = "whatsnew_fa_archive_" + yyyy + "_" + mm + "_" + lang + ".html";
	check_url(url_path, 'cfs_fa',lang);
}

function goToTradeCalendar(yyyy, mm) {
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var year = currentTime.getFullYear();

	var url_path = "trade_calendar_" + yyyy + "_" + mm + ".html";

	check_url(url_path, 'trade_cal', '');
}

function check_url (url_path, content, lang) {
	var AJAX = null;				// Initialize the AJAX variable.
	var url = url_path;
	
	AJAX = openAjax();
	
	AJAX.onreadystatechange = function() {                      	// When the browser has the request info..
		if (AJAX.readyState==4 || AJAX.readyState=="complete") { 		//  see if the complete flag is set.
			if (AJAX.status  == 404) {
				if (content == "cfs_press")
					document.location.href = "cfs_press_archive_error_" + lang + ".html";
				else if (content == "cfs_fa")
					document.location.href = "whatsnew_fa_archive_error_" + lang + ".html";
				else if (content == "trade_cal")
					document.location.href = "trade_calendar_notmach.html";
			} else {
				document.location.href = url_path;
			}
		}                                                      	
	}
	
	AJAX.open("GET", url, true);	
	AJAX.send(null);
}

function writePeriod (lang, content, year, month) {
	if (month < 10) month = "0" + month;
	var url_path = "whatsnew_fa_archive_" + year + "_" + month + "_" + lang + ".html";
	var lastModified;
	//alert (url_path);
	xmlhttp = openAjax();
	
	xmlhttp.open("HEAD", url_path, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 404) {
				writePeriod (lang, content, year, month - 1);
			} else if (xmlhttp.status == 200 || month <= 0) {
				//alert(xmlhttp.getResponseHeader("Last-Modified"));
				//document.write year + "=" + month;
			}
		}
	}
	xmlhttp.send(null)
}