var jsduration = 1;
var jsfrom = 0;
var jsto = 1;
var viewportwidth;
var viewportheight;
function reloadPage(link)
{
	window.location = '../'+ link;
}
function connectionAjax(URL,pars,divid,lang)
{
	myAjax = new Ajax.Request(URL,{
       			 	 	 	method:'get',
       			 	 	 	parameters: pars, 
       			 	 		onComplete: function(originalRequest)
       			 	 								{
       			 	 									$(divid).innerHTML = originalRequest.responseText;
       			 	 									switch(lang)
       			 	 									{
       			 	 										case 'ch':
       			 	 											$('email').value='註冊接收相關新聞';
       			 	 										break;
       			 	 										case 'en':
       			 	 											$('email').value='SIGN UP FOR NEWSLETTER';
       			 	 										break;
       			 	 									}
       			 	 									$('email').className = 'text-label';
       			 	 									popupDiv('divMsgBox','divMsgBoxTable');
       			 	 								}
       			 	 	 	});		 	 	 	
}
function signupNewsletter(lang)
{
	var status = true;
	switch(lang)
	{
		case"ch":
  		if ($('email').value == "") 
  		{
  			alert("請輸入電郵地址!");
    		$('email').focus();
    		status = false;
    		return false;
  		}     
  		if ($('email').value != "") 
  		{
  			var cemail = ($('email').value).split('@');
    		if(cemail.length > 1)
    		{
  				var cemail1 = (cemail[1]).split('\.');
    			if(cemail[0] == '' || cemail[1] == '' || cemail1.length < '2')
	    		{
      			alert("請輸入正確的電郵地址!");
        		$('email').focus();
        		status = false;
        		return false;
      		}
    		}
    		else
    		{
    			alert("請輸入正確的電郵地址!");
      		$('email').focus();
      		status = false;
      		return false;
    		}         		
			}
		break;
		case"en":
  		if ($('email').value == "") 
  		{
  			alert("Please enter a valid email address!");
    		$('email').focus();
    		status = false;
    		return false;
  		}     
  		if ($('email').value != "") 
  		{
  			var cemail = ($('email').value).split('@');
    		//if(cemail.length > 1 && cemail1.length > 1)
    		if(cemail.length > 1)
    		{
  				var cemail1 = (cemail[1]).split('\.');
    			if(cemail[0] == '' || cemail[1] == '' || cemail1.length < '2')
	    		{
      			alert("Please enter a valid email address!");
        		$('email').focus();
        		status = false;
        		return false;
      		}
    		}
    		else
    		{
    			alert("Please enter a valid email address!");
      		$('email').focus();
      		status = false;
      		return false;
    		}         		
			}
		break;
				
	}
	if(status == true)
	{
		pars = 'email='+$('email').value+'&lang='+lang;
		URL = "../xml/getnewsletter.php";
		$('MsgBoxContact').innerHTML = "";
		connectionAjax(URL,pars,'MsgBoxContact',lang);
	}
}
function popupDiv(divID,mainTable)
{ 	
 	viewportwidth = window.innerWidth;
 	viewportheight = window.innerHeight	
 	if (typeof window.innerWidth != 'undefined')
 	{
  	viewportwidth = window.innerWidth;
    viewportheight = window.innerHeight;
 	}
 	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0)
 	{
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}
 	else
 	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}	
	if(viewportheight > $(mainTable).getAttribute("height"))
	{
		viewportwidth  = Math.ceil(Math.round(viewportwidth / 2) - Math.round($(mainTable).getAttribute("width") / 2));		
		if(viewportheight > $(mainTable).getAttribute("height"))
			viewportheight = Math.ceil(Math.round(viewportheight / 2) - Math.round($(mainTable).getAttribute("height") / 2));
		this.popup = $(mainTable);	
		$(mainTable).setStyle({top: viewportheight, left: viewportwidth });	
	}
	else
	{
		viewportwidth  = Math.ceil(Math.round(viewportwidth / 2) - Math.round($(mainTable).getAttribute("width") / 2));		
		this.popup = $(mainTable);
		$(mainTable).setStyle({ left: viewportwidth });
	}
	$(divID).setStyle({top: document.body.scrollTop });	
	$(divID).appear({ duration: jsduration, from: jsfrom, to: jsto });
	$(mainTable).setStyle({top: document.body.scrollTop + viewportheight });
	$(mainTable).appear({ duration: jsduration, from: jsfrom, to: jsto });
}
function closepopupDiv(divID,mainTable)
{
	$(divID).fade({ duration: jsduration, from: jsto, to: jsfrom });
	$(mainTable).fade({ duration: jsduration, from: jsto, to: jsfrom });
}
