/* IndexController::IndexAction JS */
BRI.namespace('index.index');

BRI.index.index = function() {
    // "private" functions and variables go here

    function countrySetupAutocomplete(id, container) {
	var menuDataSource = new YAHOO.widget.DS_XHR(BRI.baseUrl
		+ "/country_search.php", [ "\n", "\t" ]);
	menuDataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
	menuDataSource.maxCacheEntries = 0;
	menuDataSource.queryMatchSubset = false;

	var AutoComp = new YAHOO.widget.AutoComplete(id, container,
		menuDataSource);
    }

    var publicObject = {
	// "public" functions and variables go here

	init : function() {

	    countrySetupAutocomplete("country", "container_country");
	    countrySetupAutocomplete("nationality", "container_nationality");

	    $E.on("insurepartner0", 'click', function() {
		$('partnershow').style.display = 'none';
		$('travel-plan-all').value = "0"
	    });
	    $E.on("insurepartner1", 'click', function() {
		$('partnershow').style.display = 'block';
		$('travel-plan-all').value = "1"
	    });

	    $E.on("insurechildren0", 'click', function() {
		$('childrenshow').style.display = 'none'
	    });
	    $E.on("insurechildren1", 'click', function() {
		$('childrenshow').style.display = 'block'
	    });

	    $E.on('hearabout', 'change', BRI.index.index.hearAboutChanged);
	    $E
		    .on('other_hearabout', 'focus',
			    BRI.index.index.hearAboutFocussed);
	    $E.on('country', 'blur', BRI.index.index.countryChanged);

	},

	hearAboutChanged : function(e) {
	    var index = $('hearabout').selectedIndex;
	    var value = $('hearabout').options[index].value;
	    if (value == 1 || value == 22 || value == 25 || value == 31) {
		$('other_hearabout').style.display = 'block';
		$('hearabout_label').style.display = 'block';
	    } else {
		$('other_hearabout').style.display = 'none';
		$('hearabout_label').style.display = 'none';
	    }
	    if (value == 1
		    && ($('other_hearabout').value == ''
			    || $('other_hearabout').value == 'Please complete...'
			    || $('other_hearabout').value == 'Please provide name of expat website...' || $('other_hearabout').value == 'Please provide name...')) {

		$('other_hearabout').value = 'Please provide broker name...';
	    }
	    if (value == 22
		    && ($('other_hearabout').value == ''
			    || $('other_hearabout').value == 'Please provide broker name...'
			    || $('other_hearabout').value == 'Please provide name of expat website...' || $('other_hearabout').value == 'Please provide name...')) {

		$('other_hearabout').value = 'Please complete...';
	    }
	    if (value == 25
		    && ($('other_hearabout').value == ''
			    || $('other_hearabout').value == 'Please provide broker name...'
			    || $('other_hearabout').value == 'Please complete...' || $('other_hearabout').value == 'Please provide name...')) {

		$('other_hearabout').value = 'Please provide name of expat website...';
	    }
	    if (value == 31
		    && ($('other_hearabout').value == ''
			    || $('other_hearabout').value == 'Please provide broker name...'
			    || $('other_hearabout').value == 'Please complete...' || $('other_hearabout').value == 'Please provide name of expat website...')) {

		$('other_hearabout').value = 'Please provide name...';
	    }

	},

	hearAboutFocussed : function(e) {
	    if (this.value == "Please complete..."
		    || this.value == "Please provide broker name..."
		    || this.value == "Please provide name of expat website..."
		    || this.value == "Please provide name...") {
		this.value = '';
	    }
	},

	countryChanged : function(e) {
	    var country = $('country').value;
	    if (country == 'UK' || country == 'U.K'
		    || country == 'United Kingdom') {
		$('tax-note').style.fontSize = '1.0em';
		$('tax-note').style.visibility = 'visible';
	    } else {
		$('tax-note').style.fontSize = '1px';
		$('tax-note').style.visibility = 'hidden';
	    }

	    if (country == 'UAE' || country == 'United Arab Emirates'
		    || country == 'Dubai') {
		$('currency-note').style.fontSize = '1.0em';
		$('currency-note').style.visibility = 'visible';
	    } else {
		$('currency-note').style.fontSize = '1px';
		$('currency-note').style.visibility = 'hidden';
	    }

	    var currency = $('currency-select');
	    var numberOfCurrencies = currency.options.length;
	    if (country == 'UAE' || country == 'United Arab Emirates'
		    || country == 'Dubai') {
		if (numberOfCurrencies != 4) {
		    var option = document.createElement('option');
		    option.value = '4';
		    option.text = 'AED UAE Dirhams';
		    currency.options.add(option, numberOfCurrencies);
		    currency.selectedIndex = 3;
		}
	    } else {
		for ( var i = 0; i < currency.options.length; i++) {
		    if (currency.options[i].value == 4) {
			currency.remove(i);
		    }
		}
	    }

	},

	validateYourDetails : function(form) {

	    var result = true, message = "";

	    if (form.surname.value == "") {
		message += "* Surname is empty\n";
	    }

	    if (form.forename.value == "") {
		message += "* Forename is empty\n";
	    }
	    if (form.email.value == "") {
		message += "* Email address is empty\n";
	    }
	    if (form.telephone.value == "") {
		message += "* Telephone is empty\n";
	    }
	    if (form.country.value == "") {
		message += "* Your country of residence is empty\n";
	    }
	    if (form.nationality.value == "") {
		message += "* Your nationality is empty\n";
	    }
	    if (form.nationality.value != ""
		    && form.nationality.value == form.country.value) {
		message += "* You are unable to get a quote if your Country of Residence is the same as your nationality, we can only insure expatriates\n";
	    }

	    var minDob = new Date();
	    minDob.setYear(minDob.getYear() - 18);
	    var maxDob = new Date();
	    maxDob.setYear(maxDob.getYear() - 65);

	    BRI.index.index.fixDates();

	    var validDate = BRI.index.index.validDate(form.dobyear.value,
		    form.dobmonth.value, form.dobday.value);
	    if (!validDate) {
		message += "* Date of birth is invalid\n";
	    } else {
		var dob = new Date(form.dobyear.value, form.dobmonth.value - 1,
			form.dobday.value);
		if (dob > minDob) {
		    message += "* Date of birth is too young\n";
		} else if (dob < maxDob) {
		    message += "* Sorry, we are unable to provide cover for people aged 65 and over unless you are renewing your policy\n";
		}
	    }
	    if (form.insurepartner1.checked) {
		validDate = BRI.index.index.validDate(
			form.partnerdobyear.value, form.partnerdobmonth.value,
			form.partnerdobday.value);
		if (!validDate) {
		    message += "* Your partner's date of birth is invalid\n";
		} else {
		    dob = new Date(form.partnerdobyear.value,
			    form.partnerdobmonth.value - 1,
			    form.partnerdobday.value);
		    if (dob > minDob) {
			message += "* Your partner's date of birth is too young\n";
		    } else if (dob < maxDob) {
			message += "* Your partner's date of birth is too old\n";
		    }
		}
	    }
	    if (form.insurechildren1.checked) {
		if (form.numberofchildren.value == ""
			|| isNaN(form.numberofchildren.value - 0)
			|| form.numberofchildren.value < 1) {
		    message += "* Please enter the number of children you wish to cover\n";
		}
	    }

	    if ($("planstartyear")) {
		validDate = BRI.index.index.validDate($("planstartyear").value,
			$("planstartmonth").value, $("planstartday").value);
		if (!validDate) {
		    message += "* Your requested start date is invalid\n";
		}

		// check within 3 months
		var D1 = new Date();
		var D2 = new Date();
		D1.setUTCFullYear($("planstartyear").value);
		D1.setUTCMonth($("planstartmonth").value - 1);
		D1.setUTCDate($("planstartday").value);
		var numberOfDays = Math.round((D1 - D2) / 86400000);
		if (numberOfDays < 0) {
		    message += "* Your requested start date is in the past\n";
		} else if (numberOfDays > 90) {
		    message += "* Application forms are only valid for 90 days, please enter another start date\n";
		}
	    }

	    if (message != "") {
		message = "The following problems were found:\n\n" + message;
		result = false;
		alert(message);
	    }

	    return result;
	},

	fixDates : function() {
	    if ($("dobday").value.length == 1) {
		$("dobday").value = "0" + $("dobday").value;
	    }
	    if ($("dobmonth").value.length == 1) {
		$("dobmonth").value = "0" + $("dobmonth").value;
	    }
	    if ($("dobyear").value.length == 2) {
		$("dobyear").value = "19" + $("dobyear").value;
	    }

	    if ($("insurepartner1").checked) {
		if ($("partnerdobday").value.length == 1) {
		    $("partnerdobday").value = "0" + $("partnerdobday").value;
		}
		if ($("partnerdobmonth").value.length == 1) {
		    $("partnerdobmonth").value = "0"
			    + $("partnerdobmonth").value;
		}
		if ($("partnerdobyear").value.length == 2) {
		    $("partnerdobyear").value = "19"
			    + $("partnerdobyear").value;
		}
	    }

	    if ($("planstartday")) {
		if ($("planstartday").value.length == 1) {
		    $("planstartday").value = "0" + $("planstartday").value;
		}
		if ($("planstartmonth").value.length == 1) {
		    $("planstartmonth").value = "0" + $("planstartmonth").value;
		}
		if ($("planstartyear").value.length == 2) {
		    $("planstartyear").value = "20" + $("planstartyear").value;
		}
	    }
	},

	validDate : function(y, m, d) {
	    m = m - 1;
	    var test = new Date(y, m, d)
	    var testMonth = test.getMonth();
	    var testDay = test.getDate();
	    return testMonth == m && testDay == d;

	}
    }

    return publicObject;
}();

YAHOO.util.Event.onDOMReady(BRI.index.index.init);

