﻿var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);

function checkTo() {
    if (document.getElementById("to_list").value == "U/L") {
        alert("Choose a destination");
        return (false);
    } else {
        return (true);
    }
}

function checkSubmitFlight() {

      if (checkDates(document.forms['MainForm'].DepartDate, 'Depart date', document.forms['MainForm'].ReturnDate, 'Return date', getCheckedValue(document.MainForm.onewayswitch) == '1') && checkAirportCodeCalendar() && checkTo()) {
        document.MainForm.submit();
        return true;
    } else {
        return (false);
    }
}

function copyValue(obj) {
    document.getElementById("other").value = obj.value;
}

function getFlightPageCookies() {
    if (readCookie('cheapFlightSearchFrom') != null) {
        changeDefaultSelectedValue(document.getElementById("from"), readCookie('cheapFlightSearchFrom'));
    }
    if (readCookie('cheapFlightSearchToList') != null) {
        changeDefaultSelectedValue(document.getElementById("to_list"), readCookie('cheapFlightSearchToList'));
    }
    if (readCookie('cheapFlightSearchOneWaySwitch') != null) {
        changeDefaultRadioSelectedValue(document.MainForm.onewayswitch, readCookie('cheapFlightSearchOneWaySwitch'));
    }
    if (readCookie('cheapFlightSearchTarifKlasse') != null) {
        changeDefaultRadioSelectedValue(document.MainForm.tarif_klasse, readCookie('cheapFlightSearchTarifKlasse'));
    }
    if (readCookie('cheapFlightSearchDepartDate') != null) {
        document.getElementById("DepartDate").value = readCookie('cheapFlightSearchDepartDate');
    } else {
        fillCurrentDate();
    }
    if (readCookie('cheapFlightSearchReturnDate') != null) {
        document.getElementById("ReturnDate").value = readCookie('cheapFlightSearchReturnDate');
    }
    if (readCookie('cheapFlightSearchPaxAdult') != null) {
        changeDefaultSelectedValue(document.getElementById("pax_adult"), readCookie('cheapFlightSearchPaxAdult'));
    }
    if (readCookie('cheapFlightSearchPaxChd') != null) {
        changeDefaultSelectedValue(document.getElementById("pax_chd"), readCookie('cheapFlightSearchPaxChd'));
    }
    if (readCookie('cheapFlightSearchPaxInf') != null) {
        changeDefaultSelectedValue(document.getElementById("pax_inf"), readCookie('cheapFlightSearchPaxInf'));
    }
}

function changeDefaultSelectedValue(objSelect, value) {
    try {
        for (var i = 0; i < objSelect.options.length; i++) {
            if (objSelect.options[i].value == value) {
                objSelect.options[i].selected = true;
                break
            }
        }
    } catch (e) { }
}

function fillCurrentDate() {
    var currentDate = new Date();
    var currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + 14);
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    if (parseInt(day) < 10) {
        day = "0" + day;
    }
    if (parseInt(month) < 10) {
        month = "0" + month;
    }
    var currentDateStr = day + "/" + month + "/" + currentDate.getFullYear();
    document.getElementById("DepartDate").value = currentDateStr;
    document.getElementById("ReturnDate").value = currentDateStr;
}

function changeDefaultRadioSelectedValue(objRadio, value) {
    try {
        if ((value == 1) || (value == 'N')) {
            objRadio[0].checked = true;
            objRadio[1].checked = false;
        } else {
            objRadio[0].checked = false;
            objRadio[1].checked = true;
        }
    } catch (e) { }
}

function updateReturnDate() {
    if ((document.getElementById("DepartDate") != null) && (document.getElementById("DepartDate").value != "")) {
        var leavingDate = document.getElementById("DepartDate").value;
        try {
            if (IsNumeric(leavingDate.substring(0, 2)) && IsNumeric(leavingDate.substring(3, 5)) && IsNumeric(leavingDate.substring(6, 10))) {
                var dateDep = new Date(leavingDate.substring(6, 10), leavingDate.substring(3, 5), leavingDate.substring(0, 2));
                var dateRetPlusWeek;
                if (leavingDate.substring(0, 2) != 31) {
                    dateRetPlusWeek = new Date(leavingDate.substring(6, 10), ((leavingDate.substring(3, 5)) - 1), dateDep.getDate() + 7);
                } else {
                    dateRetPlusWeek = new Date(leavingDate.substring(6, 10), ((leavingDate.substring(3, 5))), dateDep.getDate() + 6);
                }
                document.getElementById("ReturnDate").value = formatInputDate(dateRetPlusWeek);
            }
        } catch (e) {
            return (false);
        }
    } else {
        return (false);
    }
}

function formatInputDate(value) {
    var dateToFormat = new Date(value);
    var day = dateToFormat.getDate();
    var month = dateToFormat.getMonth() + 1;
    if (parseInt(day) < 10) {
        day = "0" + day;
    }
    if (parseInt(month) < 10) {
        month = "0" + month;
    }
    var DateStr = day + "/" + month + "/" + dateToFormat.getFullYear();
    return (DateStr);
}

function IsNumeric(value) {
    var objRegExp = /^[0-9]*$/;
    try {
        return (objRegExp.test(value));
    } catch (e) {
        return false;
    }
}

function hideFixFirstDate() {
    if (isIE6) {
        document.getElementById('pax_adult').style.visibility = 'hidden';
    }
}

function showFixFirstDate() {
    if (isIE6) {
        document.getElementById('pax_adult').style.visibility = 'visible';
    }
}

function hideFixSecondDate() {
    if (isIE6) {
        document.getElementById('pax_chd').style.visibility = 'hidden';
        document.getElementById('pax_inf').style.visibility = 'hidden';
    }
}

function showFixSecondDate() {
    if (isIE6) {
        document.getElementById('pax_chd').style.visibility = 'visible';
        document.getElementById('pax_inf').style.visibility = 'visible';
    }
}

function checkAirportCodeCalendar() {
    var numDays = 1;
    if (document.MainForm.pax_adult.value == 0 && document.MainForm.pax_stud.value == 0) {
        alert('You must enter at least 1 Passenger to proceed');
        return false;
    }
    if (document.MainForm.pax_adult.value != 0) {
        document.MainForm.pax.value = document.MainForm.pax_adult.value;
    } else {
        document.MainForm.pax.value = document.MainForm.pax_stud.value;
    }
    if (document.MainForm.onewayswitch[0].checked) {
        createCookie('onewayswitch', 1, numDays);
        document.oz_form.TickType.value = 1;
    } else {
        createCookie('onewayswitch', 0, numDays);
        document.oz_form.TickType.value = 0;
    }
    if (document.MainForm.other.value == '') {
        if (document.MainForm.to_list.value == 'U/L' || document.MainForm.to_list.value == '0') {
            alert('Please choose a destination before proceeding');
            return false;
        } else {
            document.MainForm.to.value = document.MainForm.to_list.value;
        }
    } else {
        if (document.MainForm.other.value.length == 3) {
            document.MainForm.to.value = document.MainForm.other.value;
        } else if (document.MainForm.other.value.length < 6) {
            alert('Specify 3 letter city code or click on "Other cities"!');
            return false;
        } else {
            s = document.MainForm.other.value;
            if (s.substr(s.length - 1, 1) != ')' || s.substr(s.length - 5, 1) != '(') {
                alert('Specify 3 letter city code or click on "Other cities"!');
                return false;
            }
            document.MainForm.to.value = s.substr(s.length - 4, 3);
        }
    }
    if (document.MainForm.onewayswitch[0].checked) {
        createCookie('cheapFlightSearchOneWaySwitch', 1, numDays);
        document.oz_form.TickType.value = 1;
    } else {
        createCookie('cheapFlightSearchOneWaySwitch', 0, numDays);
        document.oz_form.TickType.value = 0;
    }
    if (document.MainForm.tarif_klasse[0].checked) {
        createCookie('cheapFlightSearchTarifKlasse', document.MainForm.tarif_klasse[0].value, numDays)
    } else {
        createCookie('cheapFlightSearchTarifKlasse', document.MainForm.tarif_klasse[1].value, numDays)
    }
    createCookie('cheapFlightSearchPax', document.MainForm.pax.value, numDays)
    createCookie('cheapFlightSearchPaxChd', document.MainForm.pax_chd[document.MainForm.pax_chd.selectedIndex].value, numDays)
    createCookie('cheapFlightSearchPaxInf', document.MainForm.pax_inf.value, numDays)
    createCookie('cheapFlightSearchPaxAdult', document.MainForm.pax_adult[document.MainForm.pax_adult.selectedIndex].value, numDays)
    createCookie('cheapFlightSearchFrom', document.MainForm.from.value, numDays)
    createCookie('cheapFlightSearchDestination', document.MainForm.to.value, numDays)
    createCookie('cheapFlightSearchOther', document.MainForm.other.value, numDays)
    createCookie('cheapFlightSearchToList', document.MainForm.to_list.value, numDays)
    createCookie('cheapFlightSearchDepartDate', document.MainForm.DepartDate.value, numDays)
    createCookie('cheapFlightSearchReturnDate', document.MainForm.ReturnDate.value, numDays)
    if (OzDest(document.MainForm.to.value)) {
        document.oz_form.ArrAptCode.value = document.MainForm.to.value;
        document.oz_form.DepDay.value = document.MainForm.Day.value;
        document.oz_form.RetDay.value = document.MainForm.RetDay.value;
        document.oz_form.DepMonthYear.value = document.MainForm.month_1.value;
        document.oz_form.RetMonthYear.value = document.MainForm.month_2.value;
        document.oz_form.ADT.value = document.MainForm.pax.value;
        document.oz_form.CHD.value = document.MainForm.pax_chd.value;
        document.oz_form.submit();
        return false;
    }
    return true;
}

function checkAirportCode() {
    if (document.MainForm.pax_adult.value == 0 && document.MainForm.pax_stud.value == 0) {
        alert('You must enter at least 1 Passenger to proceed');
        return false;
    }
    if (document.MainForm.pax_adult.value != 0) document.MainForm.pax.value = document.MainForm.pax_adult.value
    else
        document.MainForm.pax.value = document.MainForm.pax_stud.value
    if (document.MainForm.onewayswitch[0].checked) {
        createCookie('cheapFlightSearchOneWaySwitch', 1, numDays);
        document.oz_form.TickType.value = 1;
    } else {
        createCookie('cheapFlightSearchOneWaySwitch', 0, numDays);
        document.oz_form.TickType.value = 0;
    }
    if (document.MainForm.other.value == '') {
        if (document.MainForm.to_list.value == 'U/L' || document.MainForm.to_list.value == '0') {
            alert('Please choose a destination before proceeding');
            return false;
        } else {
            document.MainForm.to.value = document.MainForm.to_list.value;
        }
    } else {
        if ((document.MainForm.other.value).length == 3) {
            document.MainForm.to.value = document.MainForm.other.value;
        } else if ((document.MainForm.other.value).length < 6) {
            alert('Specify 3 letter city code or click on "Other cities"!');
            return false;
        } else {
            s = document.MainForm.other.value;
            if (s.substr(s.length - 1, 1) != ')' || s.substr(s.length - 5, 1) != '(') {
                alert('Specify 3 letter city code or click on "Other cities"!');
                return false;
            }
            document.MainForm.to.value = s.substr(s.length - 4, 3);
        }
    }
    if (!CheckDate()) {
        return false;
    }
    if ((document.MainForm.month_1.selectedIndex * 31 + document.MainForm.Day.selectedIndex) > (document.MainForm.month_2.selectedIndex * 31 + document.MainForm.RetDay.selectedIndex)) {
        alert('Return date is before departure date.\nPlease correct and try again.');
        return false;
    }
    createCookie('cheapFlightSearchTarifKlasse', document.MainForm.tarif_klasse.value, numDays)
    createCookie('cheapFlightSearchPax', document.MainForm.pax.value, numDays)
    createCookie('cheapFlightSearchPaxChd', document.MainForm.pax_chd[document.MainForm.pax_chd.selectedIndex].value, numDays)
    createCookie('cheapFlightSearchPaxStudent', document.MainForm.pax_stud.value, numDays)
    createCookie('cheapFlightSearchPaxAdult', document.MainForm.pax_adult[document.MainForm.pax_adult.selectedIndex].value, numDays)
    createCookie('cheapFlightSearchFrom', document.MainForm.DepAptCode.value, numDays)
    createCookie('cheapFlightSearchDestination', document.MainForm.to.value, numDays)
    createCookie('DepDay', document.MainForm.Day.value, numDays)
    createCookie('DepMonYr', document.MainForm.month_1.value, numDays)
    createCookie('RetDay', document.MainForm.RetDay.value, numDays)
    createCookie('RetMonYr', document.MainForm.month_2.value, numDays)
    createCookie('cheapFlightSearchOther', document.MainForm.other.value, numDays)
    createCookie('cheapFlightSearchToList', document.MainForm.to_list.value, numDays)
    if (OzDest(document.MainForm.to.value)) {
        document.oz_form.ArrAptCode.value = document.MainForm.to.value;
        document.oz_form.DepDay.value = document.MainForm.Day.value;
        document.oz_form.RetDay.value = document.MainForm.RetDay.value;
        document.oz_form.DepMonthYear.value = document.MainForm.month_1.value;
        document.oz_form.RetMonthYear.value = document.MainForm.month_2.value;
        document.oz_form.ADT.value = document.MainForm.pax.value;
        document.oz_form.CHD.value = document.MainForm.pax_chd.value;
        document.oz_form.submit();
        return false;
    }
    return true;
}
dateToday = new Date();
thisYear = dateToday.getFullYear();
thisMonth = dateToday.getMonth() + 1;
thisDate = dateToday.getDate();
var MonthArray = new Array();

function openpopupDest() {
    window.open('/includes/selectdest.asp', 'new1', 'toolbar=no,location=no,statusbar=no,menubar=no,resizable=yes,width=400,height=220,top=220,left=220');
}

function OzDest(strDest) {
    return false;
    if (strDest == 'ADL') return true;
    if (strDest == 'ASP') return true;
    if (strDest == 'AKL') return true;
    if (strDest == 'DPS') return true;
    if (strDest == 'BKK') return true;
    if (strDest == 'BOS') return true;
    if (strDest == 'BNE') return true;
    if (strDest == 'CNS') return true;
    if (strDest == 'CHC') return true;
    if (strDest == 'DEW') return true;
    if (strDest == 'HKG') return true;
    if (strDest == 'HNL') return true;
    if (strDest == 'KUL') return true;
    if (strDest == 'LAX') return true;
    if (strDest == 'MEL') return true;
    if (strDest == 'NYC') return true;
    if (strDest == 'PER') return true;
    if (strDest == 'SIN') return true;
    if (strDest == 'SYD') return true;
    if (strDest == 'WLG') return true;
    return false;
}

function addDays(myDate, days) {
    return new Date(myDate.getTime() + days * 24 * 60 * 60 * 1000);
}
var numDays = addDays(new Date(), 5)

function createCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function checkSunHotelsFields() {
    var missinginfo = "";
    if (document.book.tempArea.value == "") {
        missinginfo += "\n     -  You must select a Destination";
    }
    if (document.book.tempBoardCategory.value == "-") {
        missinginfo += "\n     -  You must select a Board Option";
    }
    if (missinginfo != "") {
        missinginfo = "\nYou must fill in the following fields correctly:\n" + missinginfo + "\n\nPlease re-enter your information and submit it again. Thank You!";
        alert(missinginfo);
        return;
    } else document.book.submit();
}
var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var 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) {
    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 isDateValid(dtStr, name) {
    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)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        alert(name + ": the date format should be : dd/mm/yyyy")
        return false
        
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        alert(name + ": please enter a valid month")
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        alert(name + ": please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        alert(name + ": please enter a valid 4 digit year between " + minYear + " and " + maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        alert(name + ": please enter a valid date")
        return false
    }
   
    return true
}

function dateCheck(input, name) {
    if (input.value != null && input.value.length > 0) {
        if (!isDateValid(input.value, name)) {

               return false;
        }
    } else {
        alert(name + ": no date provided");
        return false;
    }
    return true;
}

function checkDates(inputDepart, nameDepart, inputReturn, nameReturn, oneway) {



  
   
   
   
    if (oneway) {
        return dateCheck(inputDepart, nameDepart);
    } else {
        if (!dateCheck(inputDepart, nameDepart) || !dateCheck(inputReturn, nameReturn)) {
                 
           return false;
        } else {
            dateDepart = new Date(getDateFromFormat(inputDepart.value, "dd/MM/yyyy"));
            dateReturn = new Date(getDateFromFormat(inputReturn.value, "dd/MM/yyyy"));
            if (dateDepart > dateReturn) {
                alert("Your desired return date must be greater or equal to your departure date");
                return false;
            } else {
                  return true;
            }
        }
    }
}

function getCheckedValue(radioObj) {
    if (!radioObj) return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined) if (radioObj.checked) return radioObj.value;
    else
        return "";
    for (var i = 0; i < radioLength; i++) {
        if (radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function _isInteger(val) {
    var digits = "1234567890";
    for (var i = 0; i < val.length; i++) {
        if (digits.indexOf(val.charAt(i)) == -1) {
            return false;
        }
    }
    return true;
}

function _getInt(str, i, minlength, maxlength) {
    for (var x = maxlength; x >= minlength; x--) {
        var token = str.substring(i, i + x);
        if (token.length < minlength) {
            return null;
        }
        if (_isInteger(token)) {
            return token;
        }
    }
    return null;
}

function getDateFromFormat(val, format) {
    val = val + "";
    format = format + "";
    var i_val = 0;
    var i_format = 0;
    var c = "";
    var token = "";
    var token2 = "";
    var x, y;
    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth() + 1;
    var date = 1;
    var hh = now.getHours();
    var mm = now.getMinutes();
    var ss = now.getSeconds();
    var ampm = "";
    while (i_format < format.length) {
        c = format.charAt(i_format);
        token = "";
        while ((format.charAt(i_format) == c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
        }
        if (token == "yyyy" || token == "yy" || token == "y") {
            if (token == "yyyy") {
                x = 4;
                y = 4;
            }
            if (token == "yy") {
                x = 2;
                y = 2;
            }
            if (token == "y") {
                x = 2;
                y = 4;
            }
            year = _getInt(val, i_val, x, y);
            if (year == null) {
                return 0;
            }
            i_val += year.length;
            if (year.length == 2) {
                if (year > 70) {
                    year = 1900 + (year - 0);
                } else {
                    year = 2000 + (year - 0);
                }
            }
        } else if (token == "MMM" || token == "NNN") {
            month = 0;
            for (var i = 0; i < MONTH_NAMES.length; i++) {
                var month_name = MONTH_NAMES[i];
                if (val.substring(i_val, i_val + month_name.length).toLowerCase() == month_name.toLowerCase()) {
                    if (token == "MMM" || (token == "NNN" && i > 11)) {
                        month = i + 1;
                        if (month > 12) {
                            month -= 12;
                        }
                        i_val += month_name.length;
                        break;
                    }
                }
            }
            if ((month < 1) || (month > 12)) {
                return 0;
            }
        } else if (token == "EE" || token == "E") {
            for (var i = 0; i < DAY_NAMES.length; i++) {
                var day_name = DAY_NAMES[i];
                if (val.substring(i_val, i_val + day_name.length).toLowerCase() == day_name.toLowerCase()) {
                    i_val += day_name.length;
                    break;
                }
            }
        } else if (token == "MM" || token == "M") {
            month = _getInt(val, i_val, token.length, 2);
            if (month == null || (month < 1) || (month > 12)) {
                return 0;
            }
            i_val += month.length;
        } else if (token == "dd" || token == "d") {
            date = _getInt(val, i_val, token.length, 2);
            if (date == null || (date < 1) || (date > 31)) {
                return 0;
            }
            i_val += date.length;
        } else if (token == "hh" || token == "h") {
            hh = _getInt(val, i_val, token.length, 2);
            if (hh == null || (hh < 1) || (hh > 12)) {
                return 0;
            }
            i_val += hh.length;
        } else if (token == "HH" || token == "H") {
            hh = _getInt(val, i_val, token.length, 2);
            if (hh == null || (hh < 0) || (hh > 23)) {
                return 0;
            }
            i_val += hh.length;
        } else if (token == "KK" || token == "K") {
            hh = _getInt(val, i_val, token.length, 2);
            if (hh == null || (hh < 0) || (hh > 11)) {
                return 0;
            }
            i_val += hh.length;
        } else if (token == "kk" || token == "k") {
            hh = _getInt(val, i_val, token.length, 2);
            if (hh == null || (hh < 1) || (hh > 24)) {
                return 0;
            }
            i_val += hh.length;
            hh--;
        } else if (token == "mm" || token == "m") {
            mm = _getInt(val, i_val, token.length, 2);
            if (mm == null || (mm < 0) || (mm > 59)) {
                return 0;
            }
            i_val += mm.length;
        } else if (token == "ss" || token == "s") {
            ss = _getInt(val, i_val, token.length, 2);
            if (ss == null || (ss < 0) || (ss > 59)) {
                return 0;
            }
            i_val += ss.length;
        } else if (token == "a") {
            if (val.substring(i_val, i_val + 2).toLowerCase() == "am") {
                ampm = "AM";
            } else if (val.substring(i_val, i_val + 2).toLowerCase() == "pm") {
                ampm = "PM";
            } else {
                return 0;
            }
            i_val += 2;
        } else {
            if (val.substring(i_val, i_val + token.length) != token) {
                return 0;
            } else {
                i_val += token.length;
            }
        }
    }
    if (i_val != val.length) {
        return 0;
    }
    if (month == 2) {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
            if (date > 29) {
                return 0;
            }
        } else {
            if (date > 28) {
                return 0;
            }
        }
    }
    if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
        if (date > 30) {
            return 0;
        }
    }
    if (hh < 12 && ampm == "PM") {
        hh = hh - 0 + 12;
    } else if (hh > 11 && ampm == "AM") {
        hh -= 12;
    }
    var newdate = new Date(year, month - 1, date, hh, mm, ss);
    return newdate.getTime();
}

function createCookie(name, value, 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 = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    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) {
    createCookie(name, "", -1);
}
