// GSP20050922
// JScriptGUI.js
function GetMyLanugage()
{
    return document.Form1.myLanguage.value;
}

function CheckLimits(obj)
{
	var value = 0;
	var minLimit = 0;
	var maxLimit = 0;
	
	minLimit = GetMin(obj);
	maxLimit = GetMax(obj);
	
	if(obj.name == 'TextBoxDOB')
	{
		value = document.Form1['TextBoxAge'].value
	}
	else
	{
		value = parseFloat(obj.value);
		if(isNaN(value))
		{
			switch(obj.name)
			{
				case 'TextBoxAge':
					document.Form1['TextBoxAge'].value = minLimit;
					break;
				case 'TextBoxFace':
					document.Form1['TextBoxFace'].value = minLimit;
					break;
				case 'TextBoxPrem':
					document.Form1['TextBoxPrem'].value = minLimit;
					break;
			}
		}	
	}
	
	if (minLimit == 0 && maxLimit == 0) return;
	
	if (value < minLimit || value > maxLimit)   
	{		
		if(value < minLimit)
		{	
			switch(obj.name)
			{
				case 'TextBoxAge':
					//alert('The minimum issue age for this plan is 20');
					alert((GetMyLanugage()=="English") ? AlertMinAge_en : AlertMinAge_fr);
					obj.value = minLimit;
					break;
				case 'TextBoxDOB':
					//alert('The minimum issue age for this plan is 20');
					alert((GetMyLanugage()=="English") ? AlertMinAge_en : AlertMinAge_fr);
					document.Form1['TextBoxAge'].value = minLimit;
					UpdateDOB();
					break;
				case 'TextBoxFace':
					obj.value = minLimit;
					//alert('The minimum Life Insurance Benefit Amount for this plan is $50,000');
					alert((GetMyLanugage()=="English") ? AlertMinFace_en : AlertMinFace_fr);
					break;
				case 'TextBoxPrem':
					obj.value = minLimit;
					//alert('The minimum Monthly Premium amount for this policy is $30');
					alert((GetMyLanugage()=="English") ? AlertMinPrem_en : AlertMinPrem_fr);
					break;
			}
		}	
		else
		{
			switch(obj.name)
			{
				case 'TextBoxAge':
					//alert('The maximum issue age for this plan is 65');
					alert((GetMyLanugage()=="English") ? AlertMaxAge_en : AlertMaxAge_fr);
					obj.value=maxLimit;
					break;
				case 'TextBoxDOB':
					//alert('The maximum issue age for this plan is 65');
					alert((GetMyLanugage()=="English") ? AlertMaxAge_en : AlertMaxAge_fr);
					document.Form1['TextBoxAge'].value = maxLimit;
					UpdateDOB();
					break;
				case 'TextBoxFace':
					//GSP20080116 500000 changed from 300000
					//alert('The maximum Life Insurance Benefit Amount for this plan is $500,000');
					alert((GetMyLanugage()=="English") ? AlertMaxFace_en : AlertMaxFace_fr);
					obj.value = maxLimit;
					//alert(maxLimit);
					break;
				case 'TextBoxPrem':
					//obj.value = maxLimit;
					//obj.value=''
					break;
			}
		}
	
		//obj.value = '';	
		obj.focus();
	}
	else
	{
    	//obj.focus();
    	//alert('i am here');//obj.style.color = 'Black';    
    }
    	
}

function UpdateDOB()
{
	// "dd/mm/yyyy"
	var sDob = document.Form1['TextBoxDOB'].value;
	
	var today = new Date();
	var iYear = today.getFullYear(); 
	var iMonth = today.getMonth()+1;
	var iDay = today.getDate();
	
	iYear -= parseInt(document.Form1['TextBoxAge'].value,10);
	//document.Form1['TextBoxDOB'].value = sDob.substring(0,6) + iYear;
	
	var sMyDOB = "";
	if(iDay < 10)
		sMyDOB += "0" + iDay;
	else
		sMyDOB += iDay;
	
	if(iMonth < 10)
		sMyDOB += "/0" + iMonth;
	else
		sMyDOB += "/" + iMonth;
	
	sMyDOB += "/" + iYear;
	
	document.Form1['TextBoxDOB'].value = sMyDOB;
	
	SetValidateCookies(false);
	//GSP20090605 parameter added to pass focus back to GUI
	SubmitMainForm('TextBoxDOB');
	// mk 8.18  document.Form1['TextBoxDOB'].focus();
	document.Form1['TextBoxStatus'].value="";
}

function UpdateAge()
{
	var sDob = document.Form1['TextBoxDOB'].value;
	
	var dmy = sDob.split("/");
	
	var iYear = parseInt(dmy[2],10);  //parseInt(sDob.substring(6,10),10);
	var iMonth = parseInt(dmy[1],10); //parseInt(sDob.substring(3,5),10);
	var iDay = parseInt(dmy[0],10);   //parseInt(sDob.substring(0,2),10);
	
	var myTempDate = new Date();
	myTempDate.setFullYear(iYear,iMonth-1,iDay);
	
	var bOK1 = true;
	var bOK2 = true;
	var bOK3 = true;
	
	if(iYear != parseInt(myTempDate.getFullYear(),10))
	{
		bOK1 = false;
	}
	else
	{
		if(isNaN(dmy[2])) bOK1 = false;
	}
	
	if(iMonth != parseInt(myTempDate.getMonth()+1,10))
	{
		bOK2 = false;
	}
	else
	{
		if(isNaN(dmy[1])) bOK1 = false;
	}
	
	if(iDay != parseInt(myTempDate.getDate(),10))
	{
		bOK3 = false;
	}
	else
	{
		if(isNaN(dmy[0])) bOK1 = false;
	}
	
	if(!bOK1 || !bOK2 || !bOK3)
	{
		//alert('Re-enter Date of Birth in correct format(dd/mm/yyyy)')
		alert((GetMyLanugage()=="English") ? AlertDOBFormat_en : AlertDOBFormat_fr);
		document.Form1.TextBoxDOB.focus();
		return ;
	}
	
	var sMyDOB = "";
	
	if(iDay < 10)
		sMyDOB += "0" + iDay;
	else
		sMyDOB += iDay;
	
	if(iMonth < 10)
		sMyDOB += "/0" + iMonth;
	else
		sMyDOB += "/" + iMonth;
		
	if(iYear < 100)
		sMyDOB += "0/" + iYear;
	else
		sMyDOB += "/" + iYear;
	
	document.Form1['TextBoxDOB'].value = sMyDOB;
	
	document.Form1['TextBoxAge'].value = CalcAge(sMyDOB);
	
	document.Form1['TextBoxStatus'].value = "";
	
	SetValidateCookies(false);
	SubmitMainForm('TextBoxAge');
}

function CalcAge(sDob)
{
	// sDob format: "dd/mm/yyyy"
	var iYear = parseInt(sDob.substring(6,10),10);
	var iMonth = parseInt(sDob.substring(3,5),10);
	var iDay = parseInt(sDob.substring(0,2),10);
	
	var dToday = new Date()
	
	var mdisplayAge = parseInt(dToday.getFullYear(),10) - iYear;
	if(iMonth > dToday.getMonth()+1 || ((iMonth==dToday.getMonth()+1) && (iDay>dToday.getDate())))
	{
		--mdisplayAge;
	}
	return mdisplayAge;
}

/*
Ultimate Age calculator script- By JavaScript Kit (http://www.javascriptkit.com)
Over 200+ free scripts here!
Credit must stay intact for use
*/
function displayage(yr, mon, day, unit, decimal, round) 
{
	var one_day=1000*60*60*24
	var one_month=1000*60*60*24*30
	var one_year=1000*60*60*24*30*12

	today=new Date()
	var pastdate=new Date(yr, mon-1, day)

	var countunit=unit
	var decimals=0
	var rounding=round
	d=new Date()
	//alert(d.getFullYear - yr)
	
/*	mk5.04


finalunit= (countunit=="days")? one_day : (countunit=="months")? one_month : one_year
	decimals=(decimals<=0)? 1 : decimals*10

	if (unit!="years")
	{
		if (rounding=="rounddown")
			//document.write(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
			return Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals
		else
			//document.write(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
			return Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals
	}
	else
	{
		yearspast=today.getFullYear()-yr-1
		tail=(today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0
		pastdate.setFullYear(today.getFullYear())
		pastdate2=new Date(today.getFullYear()-1, mon-1, day)
		tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
		//document.write(yearspast+tail+" "+countunit)
		return yearspast+tail
		
	}*/
}


function GetMin(obj)
{	
	//alert(obj.name);
	switch(obj.name)
	{
		case 'TextBoxAge':
		case 'TextBoxDOB':
			return 20;
		case 'TextBoxFace':
			return 50000;
		case 'TextBoxPrem':
			return 30;
		default: 
			return 0;
	}
	return 0;
}

function GetMax(obj)
{
	switch(obj.name)
	{
		case 'TextBoxAge':
		case 'TextBoxDOB':
			return 65;
		case 'TextBoxFace':
			return 500000;	//GSP20081016 changed from 300000
		case 'TextBoxPrem':
			return 1000000;
		default: 
			return 0;
	}
}

function myRadioButtonClick(obj)
{
	switch(obj.id)
	{
		case 'RadioButtonFace':
			
			document.Form1.TextBoxFace.disabled = false;
			document.Form1.TextBoxPrem.disabled = true;
			break;
		
		case 'RadioButtonPrem':
		
			document.Form1.TextBoxFace.disabled = true;
			document.Form1.TextBoxPrem.disabled = false;
	}
}

function CheckfaceimpRule()
{
	//alert(document.Form1.TextBoxErr.value);
	switch (document.Form1.TextBoxErr.value)
	{
		case "1":
			//alert('The Life Insurance Benefit Amount has been increased to produce the minimum premium of $30/month');
			alert((GetMyLanugage()=="English") ? AlertFaceIncreased_en : AlertFaceIncreased_fr);
			document.Form1.TextBoxErr.value="0";
			break;
		case "2":
			//alert('The premium you entered produced a Life Insurance Benefit Amount less than the $50,000 minimum for this plan.  The benefit and premium amounts have been increased to reflect the minimum Life Insurance Benefit Amount');
			alert((GetMyLanugage()=="English") ? AlertPremTooLow_en : AlertPremTooLow_fr);
			document.Form1.TextBoxErr.value="0";
			break;
		case "3":
			// GSP20081016 500000 changed from 300000 
			//alert('The premium you entered produced a Life Insurance Benefit Amount greater than the $500,000 maximum for this plan.  The benefit and premium amounts have been reduced to reflect the maximum Life Insurance Benefit Amount');
            alert((GetMyLanugage()=="English") ? AlertPremTooHigh_en : AlertPremTooHigh_fr);			
			document.Form1.TextBoxErr.value="0";
			break;
		// GSP20081017
		case "4":
			//alert('Life Insurance Benefit Amounts in excess of $300,000 are available only in $50,000 increments.  The benefit and premium amounts have been adjusted to reflect the nearest available benefit amount.');
			alert((GetMyLanugage()=="English") ? AlertFaceIncrement_en : AlertFaceIncrement_fr);
			document.Form1.TextBoxErr.value="0";
			
		case "0":
			break;
			
	}		
	document.Form1.TextBoxErr.value="0";
}

function CheckMultipleRule(obj)
{
	// GSP20081016 rewrite the logic. for easy understanding.
	
	var dFace = document.Form1.TextBoxFace.value;
	var dFaceFinal = dFace;
	var iRemain = 0;
	var minLimit = GetMin(obj);
	var maxLimit = GetMax(obj);

	if(dFace >= minLimit && dFace <= 300000)	
	{
		iRemain = dFace % 1000;
		
		if(iRemain != 0 && dFaceFinal > minLimit)
		{
			//alert('The Life Insurance Benefit Amount must be a multiple of $1,000');
			alert((GetMyLanugage()=="English") ? AlertFaceMultiple_en : AlertFaceMultiple_fr);
			
			var iResult = Math.floor(dFace / 1000);
			
			if(iRemain >= 500)
				iResult += 1;
			dFaceFinal = iResult * 1000;
		}
	}
	else if(dFace > 300000 && dFace < maxLimit)
	{
		iRemain = dFace % 50000;
		
		if(iRemain != 0 && dFaceFinal > minLimit)
		{
			//alert('Life Insurance Benefit Amounts in excess of $300,000 are available only in $50,000 increments. We have rounded to the nearest available benefit amount.');
			alert((GetMyLanugage()=="English") ? AlertFaceIncrement1_en : AlertFaceIncrement1_fr);
			
			var iResult = Math.floor(dFace / 50000);
			
			if(iRemain >= (50000/2))
				iResult += 1;
			dFaceFinal = iResult * 50000;
		}
	}
	else if(dFace > maxLimit)
	{
		dFaceFinal = maxLimit;
	}
	else if(dFace < minLimit)
	{
		dFaceFinal = minLimit;
	}
	
	document.Form1.TextBoxFace.value = dFaceFinal;
	
}

function UpdateSmoke(obj)
{
	// submit the form
	SetValidateCookies(false);
	SubmitMainForm(null);				
}

function UpdateGender(obj)
{
	// submit the form
	SetValidateCookies(false);
	SubmitMainForm(null);
}

function UpdatePrem()
{
	//document.Form1.submit();
	//document.form1['textboxface'].value=
	SetValidateCookies(false);
	SubmitMainForm(null);
}

function UpdateFace()
{
	//The premium you entered produced a Life Insurance Benefit Amount less than the $50,000 minimum for this plan. The Benefit and premium amounts have been increased to reflect the minimum Life Insurance Benefit Amount.
	//The premium you entered produced a Life Insurance Benefit Amount greater than the $300,000 maximum for this plan. The Benefit and premium amounts have been reduced to reflect the maximum Life Insurance Benefit Amount.
	//alert('');
	SetValidateCookies(false);
	SubmitMainForm(null);
}

//function SubmitMainForm()
//{
//    document.Form1.submit();
//}

//GSP20090605 parameter added to pass focus back to GUI
function SubmitMainForm(obj)
{
    if(obj!=null)
    {
        var newUrl = new String(location.href);
        var i = newUrl.indexOf("?focus=",0);
        if(i > 0)
        {
            //urlArray = //newUrl.Split("focus=");
            newUrl = newUrl.substring(0,i);
        }
        
        //GSP20090728 to avoid case sensitive trouble
        newUrl = newUrl.toLowerCase();
        
        var defaultapx = 'webmainform.aspx'; 
        if(newUrl.indexOf(defaultapx)>0)
        {
            document.Form1.action=newUrl + "?focus=" + obj;
        }
        else
        {
            document.Form1.action=newUrl + defaultapx + "?focus=" + obj;
        }
    }
    if(obj != "ButtonLanguage")
	    document.Form1.submit();
}

function ChangeLanguage(obj)
{
    SubmitMainForm(obj.id);
}

function ShowPDF(obj)
{
	window.open("WebPDF.aspx")
}

function CheckName(obj)
{
	//alert(obj.name);
	switch(obj.name)
	{
		case 'ButtonPDF':
			SetValidateCookies(true);
			//SetValidateCookies();	
			break;
		default:
			break;
	}
}

function ShowStatus(obj)
{
    //alert(obj.name);
    switch(obj.name)
    {
	    case 'TextBoxFirstName':
		    //document.Form1['TextBoxStatus'].value = "Enter client's first name";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusFirstName_en : StatusFirstName_fr;
		    break;
	    case 'TextBoxLastName':
		    //document.Form1['TextBoxStatus'].value = "Enter client's last name";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusLastName_en : StatusLastName_fr;
		    break;
	    case 'TextBoxInitial':
		    //document.Form1['TextBoxStatus'].value = "Enter client's middle initial";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusInitial_en : StatusInitial_fr;
		    break;
	    case 'TextBoxAge':
		    //document.Form1['TextBoxStatus'].value = "Enter age at last birthday. Mimnimum 20, maximum 65.";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusAge_en : StatusAge_fr;
		    break;
	    case 'TextBoxDOB':
		    //document.Form1['TextBoxStatus'].value = "Enter Date of Birth (dd/mm/yyyy)";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusDOB_en : StatusDOB_fr;
		    break;
	    case 'TextBoxPrem':
		    //document.Form1['TextBoxStatus'].value = "Minimum $30/month";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusMinPrem_en : StatusMinPrem_fr;
		    break;
	    case 'TextBoxFace':
		    // GSP20081016 changed to 500000 from 300k
		    //document.Form1['TextBoxStatus'].value = "Minimum $50,000, maximum $500,000";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusFace_en : StatusFace_fr;
		    break;
    }
    //obj.focus();
	
    switch(obj.id)
    {
	    case 'RadioButtonListSmoke':
		    //document.Form1['TextBoxStatus'].value = "Non-smoker requires no use of tobacco, marijuana or nicotine substitues in the last 12 months";
		    document.Form1['TextBoxStatus'].value = (GetMyLanugage()=="English") ? StatusSmoke_en : StatusSmoke_fr;
		    break;
    }
}

function HideStatus()
{
	document.Form1['TextBoxStatus'].value = "";
}

function SetValidateCookies(isFullCalc)
{
	if(isFullCalc)
	{
		if(document.Form1.TextBoxFirstName.value == "" || document.Form1.TextBoxLastName.value == "")
		{
			//alert("First and last name of client must be entered");
			alert((GetMyLanugage()=="English") ? AlertClientName_en : AlertClientName_fr);
			document.Form1.TextBoxFirstName.focus();
			var expires = "";
			var value = "undoneclientname";
			var name = "valcookie";
			document.cookie = name+"="+value+expires+"; path=/";
			return;
		}
		
		/* mk
		if(document.Form1.TextBoxLastName.value == "")
		{
			alert("You did not enter the lastname");
			var expires = "";
			var value = "undonename";
			var name = "valcookie";
			document.cookie = name+"="+value+expires+"; path=/";
			return;
		}*/
		

		{// Agent
			var abc = "";
			var nameEQ = "userdata" + "=";
			var ca = document.cookie.split(';');
			var bFind = false;
			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) 
				var abc= c.substring(nameEQ.length,c.length);
				var abc2=abc.split("&");
				if (abc2[0].substring(0,9)=="Agentname")
				{
					var abc3=abc2[0].split("=")
					bFind = true;
					if (abc3[1]=="")
					{
						//alert('The advisor name must be entered before illustration can be run.');
						alert((GetMyLanugage()=="English") ? AlertAdvisorName_en : AlertAdvisorName_fr);
						var expires = "";
						var value = "undoneagentname";
						var name = "valcookie";
						document.cookie = name+"="+value+expires+"; path=/";
						//location.href="webformagent.aspx"; we need to redirect.
						return;
					}
				}
			}
		
			if(!bFind)
			{
				//alert('The advisor name must be entered before illustration can be run');
				alert((GetMyLanugage()=="English") ? AlertAdvisorName_en : AlertAdvisorName_en);
				var expires = "";
				var value = "undoneagentname";
				var name = "valcookie";
				document.cookie = name+"="+value+expires+"; path=/";
				return;
			}
			
		}
	}

	if(isNaN(document.Form1['TextBoxAge'].value))
	{
		//alert('please put date in dd/mm/yyyy format or correct age')
		alert((GetMyLanugage()=="English") ? AlertDateFormat_en : AlertDateFormat_fr);
		var expires = "";
		var value = "undoneage";
		var name = "valcookie";
		document.cookie = name+"="+value+expires+"; path=/";
		return;
	}
	
	if(isFullCalc)
	{
		var expires = "";
		var value = "done";
		var name = "valcookie";
		document.cookie = name+"="+value+expires+"; path=/";
	}
}

function NotAllowSpecialChar(e)
{
    var keyVal =(window.event) ? event.keyCode : e.keyCode;
    if (window.event) keyVal = window.event.keyCode;
    if((window.event.shiftKey))
    {
        if((keyVal > 48 && keyVal < 57))
        {
            return false;
        }
        else if((keyVal > 96 && keyVal < 105))
        {
            return false;
        }
        else if((keyVal == 46))
        {
            return false;
        }
        else if((keyVal == 8))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        if((keyVal > 48 && keyVal < 57))
        {
            return true;
        }
        else if((keyVal > 96 && keyVal < 105))
        {
            return true;
        }
        else if((keyVal == 46))
        {
            return true;
        }
        else if((keyVal == 8))
        {
            return true;
        }
        else if((keyVal == 9))  //Tab
        {
            return true;
        }
        else if((keyVal == 57))
        {
            return true;
        }
        else if((keyVal == 48))
        {
            return true;
        }
        else if((keyVal > 65 && keyVal < 90))
        {
            return true;
        } 
        else if(keyVal==65)                       
        {
            return true;
        }
        else if(keyVal==90)                       
        {
            return true;
        }
        //else if(keyVal==32) //Space                  
        //{
        //    return true;
        //}
        else if((keyVal == 16))
        {
            return false;
        }
        else
        {
            return false;
        }   
    }
}

function showToolTip() 
{	
    var myText = (GetMyLanugage()=="English") ? StatusSmoke_en : StatusSmoke_fr;
    
    var toolTip = document.getElementById("spnToolTip");		
    toolTip.style.top = window.event.clientY + 10; 		
    toolTip.style.left = window.event.clientX;		
    toolTip.innerHTML = myText;		
    toolTip.style.visibility = 'visible';		
    toolTip.style.background = 'lightyellow';	
}	

function showToolTipMsg()
{
    var myText = (GetMyLanugage()=="English") ? StatusSmoke_en : StatusSmoke_fr;
    //GSP20090806 Since Mozilla doesn't support tooltip, we have to go this way.
    alert(myText, "info");
}

//function showToolTip(text) 
//{		
//    var toolTip = document.getElementById("spnToolTip");		
//    toolTip.style.top = window.event.clientY + 10; 		
//    toolTip.style.left = window.event.clientX;		
//    toolTip.innerHTML = text;		
//    toolTip.style.visibility = "visible";		
//    toolTip.style.background = 'lightyellow';	
//}	

//function showToolTipColorBG(text,color) 
//{		
//    var toolTip = document.getElementById("spnToolTip");		
//    toolTip.style.top = window.event.clientY + 10; 		
//    toolTip.style.left = window.event.clientX;		
//    toolTip.innerHTML = text;		
//    toolTip.style.visibility = "visible";		
//    toolTip.style.background = color;	
//}	

function hideToolTip() 
{		
    document.getElementById("spnToolTip").style.visibility = "hidden";	
    document.Form1['TextBoxStatus'].value = "";
}		