var d = new Date()
var year = d.getFullYear();
var month = d.getMonth() + 1;
var addressPass = false;

passToApplyControl = {

    testvar1: undefined,
    testarr1: {},

    // Function to validate the passToApply Click function
    handleClick: function (element,event) {
        try {
            document.getElementById("formError1").style.display = "none";
            document.getElementById("formError2").style.display = "none";
            document.getElementById("formError3").style.display = "none";
            document.getElementById("formError4").style.display = "none";

            function inputyear() {
                var valueyear = $("#yyyy").val();
                return valueyear;
            }

            function inputmonth() {
                var valuemonth = $("#mm").val();
                return valuemonth;
            }

            var errorTrue = false;
			

            var valueinputyear = inputyear();
            var valueinputmonth = inputmonth();
            //<![CDATA[
            if (((valueinputyear.length == '') || (valueinputmonth.length == '')) || ((valueinputyear == 'YYYY') || (valueinputmonth == 'MM'))) {
                //alert("Must enter values in textboxes");
                document.getElementById("formError1").style.display = "inline";
                errorTrue = true;
            }
            if ((valueinputyear.length != 4) || (valueinputmonth.length != 2)) {
                //alert("Must be 2 characters for MM and 4 characters for YYYY");
                document.getElementById("formError3").style.display = "inline";
                errorTrue = true;
            }
            if ((isNaN(valueinputyear)) || (isNaN(valueinputmonth)) || ((valueinputyear == 'YYYY') || (valueinputmonth == 'MM'))) {
                //alert("Must be numeric");
                document.getElementById("formError2").style.display = "inline";
                errorTrue = true;
            }
            if (((valueinputyear < 1900) || (valueinputyear > year)) || ((valueinputmonth < 01) || (valueinputmonth > 12))) {
                //alert("Must be numeric");
                document.getElementById("formError4").style.display = "inline";
                errorTrue = true;
            }
            else {
                // e.g if 2011 - it would have to be 2007 for this to match (4 years or more)
                if (valueinputyear < year - 3) {
                    addressPass = true;
                }
                // e.g if 2011 - it would have to be 2009 for this to match (2 years or less)
                else if (valueinputyear > year - 3) {
                    addressPass = false;
                }
                else {
                    if (valueinputmonth <= month) {
                        addressPass = true;
                    }
                    else {
                        addressPass = false;
                    }
                }
            }
            //]]>
            var startingOLAFURL = '/personal/savings/g1/instant-access/e-savings/apply-olaf.ashx';

            // Check if any errors are showing
            if (errorTrue == false) {


                // No errors

                // 


                

                // Show the bubble help
                if(addressPass == false){
				// Trigger the help bubble, and extract the help direction from the class

                // Get the direction from the class name. We know it starts with passToApply
                var helpDirection = $(element).attr("class");
                // Split on the '-' character
                var helpDirectionSplit = helpDirection.split('-');
                // Extract the last element
                helpDirection = helpDirectionSplit[helpDirectionSplit.length - 1].toLowerCase();
				 showBubble($(element), helpDirection);
				 }
				else{
					var cinValue = getUrlVars()["CIN"];
					if(cinValue == null){
						if(olafURL.indexOf('?') != -1){
						olafURL += '&CIN=' + valueinputmonth + valueinputyear;
						}
						else{
						olafURL += '?CIN=' + valueinputmonth + valueinputyear;
						}
					}
					else if(cinValue != null){
						if(olafURL.indexOf('?') != -1){
						olafURL += '&CIN=' + cinValue + "|" + valueinputmonth + valueinputyear;
						}
						else{
						olafURL += '?CIN=' + cinValue + "|" + valueinputmonth + valueinputyear;
						}
					}
					window.location.href = olafURL;
				}

                //terry();
                //$(this).attr('class', 'helpTriggerRight');
                //if(!$(event.target).is('.helpTriggerRight')){
                //$(this).click();
                //manuallyClick();
                //}
                /* event.stopPropagation();
                event.preventDefault();
                return false; */
            }
        }
        catch (e) {

        }
    }
}

    // On document.ready, assign the click handler
    $(document).ready(function () {
        //$(".passToApply").click(function (event) {
        $('[class^="js-passToApply"]').click(function (event) {
            passToApplyControl.handleClick($(this),event);
    });
});

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}




