
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("請輸入閣下的姓名");
		return false;
	} else if (!check_email(document.qform.email.value)) {
		alert("請輸入正確的電郵地址");
		return false;
	} else if (trim(document.qform.dayno.value) == "" && trim(document.qform.nightno.value) == "") {
		alert("請輸入閣下的聯絡電話");
		return false;
	} else if (trim(document.qform.query.value) == "") {
		alert("請填寫閣下的意見");
		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_chi/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("/sc_chi/") + 8;
	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"+":"+"//";
			if (locapath.toString().search('/press/')){
				sitepath = sitepath.replace('_c.html', '_e.html');
			}
			if (locapath.toString().search('/nutrient/')){
				sitepath = sitepath.replace('c.shtml', '.shtml');
			}
 
			break;
		case 'tc':
			des_part = tc_path;
			des_ind_part = "cindex.html";
			base_path = "http"+":"+"//";

			break;
		case 'sc':
			des_part = sc_path;
			des_ind_part = "sindex.html";
			//base_path = scserver; 
			base_path = "http"+":"+"//";
			break;
	}

	if (atindex == true){
		tail = sitepath.replace(org_part, des_ind_part);
	}else{
		tail = sitepath.replace(org_part, des_part);
	}
	

	//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 Safty - Consumer Zone";
		initialmsg = "I recommend you to go to Centre for Food Safty - 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];
}

/* ================================================================ */
/*  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.
			//callback(AJAX.responseText, AJAX.status);             	// Pass the response to our processing function
			//alert(AJAX.status);
			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;
			}
		}                                                        	// End Ajax readystate check.
	}
    											// This is the URL we will call.
	AJAX.open("GET", url, true);									// Open the url this object was set-up with.
	AJAX.send(null);                                             // Send the request.
}

// ---------------------------------------------- AJAX function for CFS Latest News ----------------------------------------------- //
function xmlhttpPost(strURL) {
	//alert (strURL);
    var xmlHttpReq = null;			// Initialize the AJAX variable.
	
	if (window.XMLHttpRequest) {                     // Does this browser have an XMLHttpRequest object?
		xmlHttpReq=new XMLHttpRequest();                    // Yes -- initialize it.
	} else {                                         // No, try to initialize it IE style
		xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP");  //  Wheee, ActiveX, how do we format c: again?
	}                                                // End setup Ajax.
	if (xmlHttpReq==null) {                                // If we couldn`t initialize Ajax...
		alert("閣下的瀏覽器不支援 AJAX.");  // Sorry msg.                                               
		return false                                  // Return false, couldn`t set up ajax
	}
	
	//xmlHttpReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=big5');
	//xmlHttpReq.setRequestHeader('Content-Type', 'text/html; charset=big5');. 

	xmlHttpReq.onreadystatechange = function() {    
        if (xmlHttpReq.readyState==4 || xmlHttpReq.readyState=="complete") {
			updatepage(xmlHttpReq.responseText);
        } else {
			document.getElementById("result").innerHTML = "請稍候 ... ";
		}
    }
	xmlHttpReq.open("GET", strURL, true);									// Open the url this object was set-up with.
	xmlHttpReq.send(null);
}

function updatepage(str){
    document.getElementById('result').innerHTML = str;
}
// ---------------------------------------------- AJAX function for CFS Latest News ----------------------------------------------- //