/**
 * Description: direct page to value in dropdown
 * Params:menu - dropdown object
 *	For What would you like to do today paragraph on the homepage
**/
function jump(menu)
{
	var selIndex = menu.selectedIndex;
	var selValue = menu.options[selIndex].value;
	
	window.location=selValue;

}


/**
 * Description: popup for signin values
 * Params:menu - dropdown object
 *	For secure signin on the homepage
**/
/*  */
function popupHK()
{
	var selectBox = document.getElementById('secure_sign_in');
	
	var selIndex = selectBox.selectedIndex;
	var selValue = selectBox.options[selIndex].value;
	
	var day = new Date();
	var id = day.getTime();
	var tbar = 0;
	var sbar = 0;
	var mbar = 0;
    	var rgn = window.location + '';
    	var url = selValue;
    	
	if (selIndex == 2) {
    		tbar = 1;
    		sbar = 1;
    		mbar = 1;
		
	}
	
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=" + tbar +", scrollbars=1, location=0, statusbar=" + sbar + ", menubar=" + mbar +", resizable=1, width=850, height=600, left = 215, top = 30');");
}


/**
 * Description: direct the first two options in the drop down menu to open in new window. 
 * Params: menu - drop downdown object
 *	Specific to the What would you like to do today paragraph (not on the homepage)
**/
function showPage(dropDown)
{
url = dropDown[dropDown.selectedIndex].value;

if (dropDown.selectedIndex == 1 || dropDown.selectedIndex == 2)
{

window.open(url, 'popup', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=850,height=600,left = 215,top = 30');
}else
{
window.location.href = url;
}

dropDown.selectedIndex = 0;
}



/**
 * IE6 Fix - Dropdowns appearing on top of Explore SLF worldwide overlay

**/
/*
Choose your country
*/
function ChooseYourCountry()
{
	$("#choose-country").hover(
		function(){
			if($("#choose-country-content:visible").size() < 1)
			{
				$("#choose-country-content").show();
				/*
				Hide the <select> menu (if applicable) in the first feature box on the
				right-hand column to fix the annoying IE6 bug where the dropdown is
				visible.
				*/
				$('#content-body div.col-right div.feature div.box select').css('visibility', 'hidden');
				$('#content-sidebar div.feature:first select').css('visibility', 'hidden');
			}
		},
		function() {
			$("#choose-country-content").hide();
			$('#content-body div.clearfix div.col-right div.feature div.box select').css('visibility', 'visible');
			$('#content-sidebar div.feature:first select').css('visibility', 'visible');
		}
	);

	$("#choose-country-content").hover(
		function() {
			$("#choose-country-content").show();
			$('#content-body div.clearfix div.col-right div.feature div.box select').css('visibility', 'hidden');
			$('#content-sidebar div.feature:first select').css('visibility', 'hidden');
		},
		function(){
			$("#choose-country-content").hide();
			$('#content-body div.clearfix div.col-right div.feature div.box select').css('visibility', 'visible');
			$('#content-sidebar div.feature:first select').css('visibility', 'visible');
		}
	);
}




/**
 * IE6 Fix - Dropdowns appearing on the primary navigation overlay

**/
/*
Primary navigation flyout
*/
function primary()
{
	$("#primary").hover(
		function(){
			if($("#primary div.ul div.li :visible").size() < 1)
			{
				$("#primary div.ul div.li").show();
				/*
				Hide the <select> menu (if applicable) in the first feature box on the
				right-hand column to fix the annoying IE6 bug where the dropdown is
				visible.
				*/
				$('#content-body div.col-right div.feature div.box select').css('visibility', 'hidden');
				$('#content-sidebar div.feature:first select').css('visibility', 'hidden');
			}
		},
		function() {
			$("#primary div.ul div.li").hide();
			$('#content-body div.clearfix div.col-right div.feature div.box select').css('visibility', 'visible');
			$('#content-sidebar div.feature:first select').css('visibility', 'visible');
		}
	);

	$("##primary div.ul div.li").hover(
		function() {
			$("#primary div.ul div.li").show();
			$('#content-body div.clearfix div.col-right div.feature div.box select').css('visibility', 'hidden');
			$('#content-sidebar div.feature:first select').css('visibility', 'hidden');
		},
		function(){
			$("#primary div.ul div.li").hide();
			$('#content-body div.clearfix div.col-right div.feature div.box select').css('visibility', 'visible');
			$('#content-sidebar div.feature:first select').css('visibility', 'visible');
		}
	);
}


/**
 * Validating date forms on the investment-linked insurance plan pages 

**/
function validateFormForCurrencyPageHK(){ 
	var dateStr = document.getElementById("D1_day").value + "/"+document.getElementById("D1_mth").value+ "/"+document.getElementById("D1_yr").value;
	if(document.frmSunwealth.buttonclicked.value=="N"){ 
		if(isValidDatefbHK(dateStr)) { 
			return true; 
		} else { 
			return false; 
		} 
	}else{ 
		if(dateStr=="99/99/99"){ 
			return true; 
		}else{
			if(isValidDatefbHK(dateStr)) { 
				return true;
			} else { 
				return false; 
			} 
		} 
	} 
}
function validateFormForCurrencyPageEN(){ 
	var dateStr = document.getElementById("D1_day").value + "/"+document.getElementById("D1_mth").value+ "/"+document.getElementById("D1_yr").value;
	if(document.frmSunwealth.buttonclicked.value=="N"){ 
		if(isValidDatefbEN(dateStr)) { 
			return true; 
		} else { 
			return false; 
		} 
	}else{ 
		if(dateStr=="99/99/99"){ 
			return true; 
		}else{
			if(isValidDatefbEN(dateStr)) { 
				return true;
			} else { 
				return false; 
			} 
		} 
	} 
}

function validateFormForFundPagesEN() { 
	var dateFrm = document.getElementById("s_day").value + "/"+document.getElementById("s_mth").value+ "/"+document.getElementById("s_yr").value; 
	var dateTo = document.getElementById("e_day").value + "/"+document.getElementById("e_mth").value+ "/"+document.getElementById("e_yr").value; 
	if( !isValidDatefbEN(dateFrm) || !isValidDatefbEN(dateTo)){ 
		return false; 
	} 
	var dateFromObj = new Date(document.getElementById("s_yr").value, document.getElementById("s_mth").value-1, document.getElementById("s_day").value); 
	var dateToObj = new Date(document.getElementById("e_yr").value , document.getElementById("e_mth").value-1,document.getElementById("e_day").value); 
	var today = new Date(); 
	var flag = "Y"; 
	var today1 = new Date(today.getFullYear(),today.getMonth(),today.getDate()); 
	var diff=Math.floor((today1.getTime() - dateFromObj.getTime())/(1000 * 60 * 60 * 24)); 
	var diff1=Math.floor((today1.getTime() - dateToObj.getTime())/(1000 * 60 * 60 * 24));
	if((diff < 0) || (diff1 < 0) ){ 
		alert("Please enter the Date no later than "+today.getFullYear()+"/"+(today.getMonth()+1)+"/"+today.getDate()+" (today's date)"); 
		flag = "N" ; 
	} 
	if(dateFromObj.getTime() > dateToObj.getTime()){ 
		alert("Invalid date range, 'From' date later than 'To' date!"); 
		flag = "N" ; 
	}
	if(flag == "N" ){ 
		return false ; 
	}else{ 
		return true ; 
	} 
} 


function validateFormForFundPagesHK() { 
	var temp = document.createElement('div'); 
	var dateFrm = document.getElementById("s_day").value + "/"+document.getElementById("s_mth").value+ "/"+document.getElementById("s_yr").value; 
	var dateTo = document.getElementById("e_day").value + "/"+document.getElementById("e_mth").value+ "/"+document.getElementById("e_yr").value; 
	if( !isValidDatefbHK(dateFrm) || !isValidDatefbHK(dateTo)){ 
		return false; 
	} 
	var dateFromObj = new Date(document.getElementById("s_yr").value, document.getElementById("s_mth").value-1, document.getElementById("s_day").value); 
	var dateToObj = new Date(document.getElementById("e_yr").value , document.getElementById("e_mth").value-1,document.getElementById("e_day").value); 
	var flag = "Y"; 
	var today = new Date(); 
	var today1 = new Date(today.getFullYear(),today.getMonth(),today.getDate()); 
	var diff=Math.floor((today1.getTime() - dateFromObj.getTime())/(1000 * 60 * 60 * 24)); 
	var diff1=Math.floor((today1.getTime() - dateToObj.getTime())/(1000 * 60 * 60 * 24)); 
	if((diff < 0) || (diff1 < 0) ){ 
		temp.innerHTML = "&#35531;&#36664;&#20837;&#26085;&#26399;&#19981;&#24460;&#26044;"+today.getFullYear()+"/"+(today.getMonth()+1)+"/"+today.getDate()+" (today's date)" ; 
		alert(temp.innerHTML); 
		flag = "N" ; 
	} 
	if(dateFromObj.getTime() > dateToObj.getTime()){ 
		temp.innerHTML ="&#26085;&#26399;&#31684;&#22285;&#28961;&#25928;, &#8216;&#30001;&#8217; &#26085;&#26399;&#24460;&#26044; &#8216;&#33267;&#8217; &#26085;&#26399;!"; 
		alert(temp.innerHTML); 
		flag = "N" ; 
	} 
	if(flag == "N" ){ 
		return false ; 
	}else{ 
		return true ; 
	} 
}

function isValidDatefbEN(dateStr) { 
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/; 
	var matchArray = dateStr.match(datePat); 
	if (matchArray == null) { 
		alert("Please Input date!"); 
		return false; 
	} 
	month = matchArray[3]; 
	day = matchArray[1]; 
	year = matchArray[4]; 
	if(month==99 || day==99 || year==99 ){ 
		alert("Please Input date!"); 
		return false; 
	} 
	if ((month==4 || month==6 || month==9 || month==11) && day==31) { 
		alert("Please enter the Date from 1 to 30!") 
		return false 
	} 
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
		if (day>28) { 
			if(!isleap){ 
				alert("Please enter the Date from 1 to 28!"); 
				return false; 
			} 
			if(day>29 && isleap){ 
				alert("Please enter the Date from 1 to 29!"); 
				return false; 
			} 
		}
	} 
	return true;
} 

function isValidDatefbHK(dateStr) { 
	var temp = document.createElement('div') 
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/; 
	var matchArray = dateStr.match(datePat); 
	temp.innerHTML ="&#35531;&#36664;&#20837;&#26085;&#26399;!"; 
	if (matchArray == null) { 
		alert(temp.innerHTML); 
		return false; 
	} 
	month = matchArray[3]; 
	day = matchArray[1]; 
	year = matchArray[4]; 
	if(month==99 || day==99 || year==99 ){ 
		alert(temp.innerHTML);
		return false; 
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) { 
		temp.innerHTML = "&#35531;&#36664;&#20837;&#26085;&#26399;&#30001; 1 &#33267; 30 &#26085;!"; 
		alert(temp.innerHTML); 
		return false 
	} 
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
		if (day>28) { 
			if(!isleap){ 
				temp.innerHTML = "&#35531;&#36664;&#20837;&#26085;&#26399;&#30001; 1 &#33267; 28 &#26085;!"; 
				alert(temp.innerHTML); 
				return false; 
			} 
			if(day>29 && isleap){ 
				temp.innerHTML = "&#35531;&#36664;&#20837;&#26085;&#26399;&#30001; 1 &#33267; 29 &#26085;!"; 
				alert(temp.innerHTML); 
				return false; 
			} 
		} 
	} 
	return true; 
}

//Assumption: a hidden fiels is set inside form "frmSunwealth" having name="buttonclicked" 
//This function checks which submit button is clicked.
function setval(){
	document.frmSunwealth.buttonclicked.value = "Y" ;
}

