// Declaring valid date character, minimum year and maximum year
var splashUrl = "";
switch (window.location.hostname) {
    case "webelieve.steinlager.debug.shift.co.nz":
        splashUrl = "http://steinlager.debug.shift.co.nz/Splash.aspx";
        break
    case "steinlager.test.shift.co.nz":
        splashUrl = "http://steinlager.test.shift.co.nz/Splash.aspx";
        break
    case "qa.webelieve.steinlager.co.nz":
        splashUrl = "http://qa.steinlager.co.nz/Splash.aspx";
        break
    case "webelieve.steinlager.co.nz":
    case "www.webelieve.co.nz":
    case "webelieve.co.nz":
        splashUrl = "http://steinlager.co.nz/Splash.aspx";
        break
    default:
        splashUrl = "http://steinlager.co.nz/Splash.aspx";
        break
}

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isDate(dtStr) {
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strDay = dtStr.substring(0, pos1)
    var strMonth = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    var month = parseInt(strMonth);
    var day = parseInt(strDay);
    var year = parseInt(strYr);
    if (pos1 == -1 || pos2 == -1) {
        $("#dobError").append("<p>The date format should be : dd/mm/yyyy</p>");
        $('#pro_birth0').focus();
        return false;
    }

    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        $("#dobError").append("<p>Please select a valid date of birth</p>");
        $('#pro_birth0').focus();
        return false;
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        $("#dobError").append("<p>Please select a valid date of birth</p>");
        $('#pro_birth1').focus();
        return false;
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {

        $("#dobError").append("<p>Please select a valid date of birth</p>");
        $('#pro_birth2').focus();
        return false;
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        $("#dobError").append("<p>Please select a valid date of birth</p>");
        $('#pro_birth0').focus();
        return false;
    }
    return true
}

function checkAge(dateVal) {
    var today = new Date();
    var d = dateVal.split('/');
    var by = Number(d[2]); var bm = Number(d[1]) - 1; var bd = Number(d[0]);
    var bday = new Date(by, bm, bd)
    var age = 0; var dif = bday;
    while (dif < today) {
        var dif = new Date(by + age, bm, bd);
        age++;
    }
    age += -2;

    if (age < 18) {
        alert('not 18');
        $("#dobError").append("<p>You are not over 18</p>");
        return false;
    }
    return true;
}

//Cookie things
function makeCookie(cookieName, cookieData, days) {

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = cookieName + "=" + cookieData + expires + "; path=/";
}

function readCookie(name) {
    //alert("read cookies!");
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    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) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) { makeCookie(name, "", -1); }

// user validated age
function validateAge() {

    makeCookie('SteinlagerAccessNZ', 'true', 0);
    var lastPage = readCookie('lastPage');
    if (lastPage == null) {
        window.location.href = 'home.html';
    } else {
        window.location.href = lastPage;
    }
    return false;
}


function checkAgeVerified() {
    var over18 = readCookie('SteinlagerAccessNZ');
    if (over18 == 'false' || over18 == null) {
        makeCookie('lastPage', location.href, 30);
        window.location = splashUrl + '?ReturnUrl=' + location.href;
        return false;
    }
    else {
        eraseCookie('lastPage');
        return true;
    }
}
