var dayNames = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
var dayCounts = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
var monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']
var catMap = {
    Turists: 'Turists and visitors to my community',
    Residents: 'Local residents'
}
var timezones = [
['Pacific', '(GMT-08:00) Pacific Time'],
['Mountain', '(GMT-07:00) Mountain Time'],
['Central', '(GMT-06:00) Central Time'],
['Eastern', '(GMT-05:00) Eastern Time'],
]
var gmt2001 = 978307200000
var userContact
var userTzName
var userTzOffset
var timeShift = 0
var switchMilitaryTime = false
function userTz(tzName) {
    if((x = myDateTzOffset(tzName)))
        userTzName = tzName, userTzOffset = x, userTzNative = false
    else if((x = myDateTzOffset(tzName = myDateTzLocalDefault())))
        userTzName = tzName, userTzOffset = x, userTzNative = true
    else
        alert('Error code INVENTORY: ' + tzName)
    var now = new MyDate()
    today = new MyDate(now.getFullYear(), now.getMonth(), now.getDate());
}
function setCookie(c_name,value,expiredays) {
    // From http://www.w3schools.com/JS/js_cookies.asp
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function cleanUpDate(input, original) {
    var thisYear = new MyDate().getFullYear();
    var tmpStr = (input.
        replace(/(?:^\s+|\s+$)/g, "").
        replace(/\s\s+/g, " ").
        replace(/^(\d\d?)[\/. -](\d\d?)$/, thisYear + "-$1-$2").
        replace(/^(\d\d?)[\/. ](\d\d?)[\/. ](\d\d\d\d|\d?\d)$/, "$3-$1-$2").
        replace(/^(\d\d\d\d)[\/. -]?(\d\d)[\/. -]?(\d\d)$/, "$1-$2-$3").
        replace(/\b(\d)\b/g, "0$1").
        replace(/^(\d\d)\b/g, "20$1"));
    var tmpArr = tmpStr.split(/\D+/);
    var tmpDate;
    try {
        if(tmpArr[0]*1 == NaN || tmpArr[1]*1 == NaN || tmpArr[2]*1 == NaN)
            tmpDate = new MyDate();
        else
            tmpDate = new MyDate(tmpArr[0], tmpArr[1] - 1, tmpArr[2]);
    } catch(ex) {
        tmpDate = new MyDate('invalidate');
    }
    if(!tmpDate.getTime() || tmpDate.getTime() < new MyDate(2001,0,1).getTime())
        if(original)
            if(original.getYear)
                tmpDate = original;
            else
                return original;
        else
            return '';
    return formatDate(tmpDate);
}
function cleanUpTime(input, original) {
    var tmpStr = (input.
        replace(/(?:^\s+|\s+$)/g, "").
        replace(/\s\s+/g, " ").
        replace(/a/, "A").
        replace(/p/, "P").
        replace(/^(\d\d?)(?:[^AP\d]*([AP]))?\D*$/g, "$1:00 $2M").
        replace(/^(\d\d?)\D+(\d\d?)\D+\d+/, "$1:$2").
        replace(/^(\d\d?)(\d\d)(?=[\sa-z]|$)/i, "$1:$2"));
    var tmpArr = tmpStr.split(/\D+/);
    var tmpHour = parseInt(tmpArr[0]);
    var tmpMinute = parseInt(tmpArr[1]);
    if(tmpStr.indexOf('A') != -1 && tmpHour == 12)
        tmpHour = 0;
    else if(tmpStr.indexOf('P') != -1 && tmpHour != 12)
        tmpHour += 12;
    else if(tmpHour == 24)
        tmpHour = 12;
    var tmpDate = new MyDate(2009, 0, 0, tmpHour, tmpMinute, 0, 0);
    if(tmpHour*0!=0 || tmpMinute*0!=0 || !tmpDate.getTime())
        if(original)
            if(original.getYear)
                tmpDate = original;
            else
                return original;
        else
            return '';
    return formatTime(tmpDate)
}
function myDateTzLocalDefault() {
    return myDateTzSpell(new Date(2009, 6, 1).toString().replace(/.*?GMT-\d\d\d\d \(([EMCP][a-z]+ Daylight Time)\).*|.*? ([EMCP]DT) \d\d\d\d.*/g, "$1$2"));
}
function myDateTzSpell(tzName) {
    if(tzName == 'EDT' || tzName == 'Eastern Daylight Time')
        return 'Eastern'
    if(tzName == 'CDT' || tzName == 'Central Daylight Time')
        return 'Central'
    if(tzName == 'MDT' || tzName == 'Mountain Daylight Time')
        return 'Mountain'
    if(tzName == 'PDT' || tzName == 'Pacific Daylight Time')
        return 'Pacific'
    return tzName
}
function myDateTzOffset(tzName) {
    if(tzName == 'Eastern')
        return 1000*60*60*-5
    if(tzName == 'Central')
        return 1000*60*60*-6
    if(tzName == 'Mountain')
        return 1000*60*60*-7
    if(tzName == 'Pacific')
        return 1000*60*60*-8
    return null
}
function MyDate(iYearTime, iMonth, iDate, iHour, iMinute, iSecond, iMs, tzDst) {
    this.isDuringDst = function () {
        if(!tzDst)
            return false
        tzDst = false
        var is = false
        var month = this.getMonth()
        if(month > 2 && month < 10)
            is = true
        else {
            var day = this.getDay()
            var date = this.getDate()
            var hours = this.getHours()
            if(month == 2)
                is = (7-day + date > 14 && (day != 0 || date > 14 || hours >= 2))
            else if(month == 10)
                is = (date < 8 && 7-(day?day:7) + date < 8 && (day != 0 || hours < 1))
        }
        tzDst = true
        return is
    }
    this.getYear =
    this.getFullYear = function () {
        var tmp = parseInt((fastTime - gmt2001 + userTzOffset)  / 1000 / 60 / 60 / 24) + 1
        var year = 2001
        while(true) {
            var x = year % 4 == 0 ? 366 : 365
            if(tmp > x) {
                year++
                tmp -= x
            } else
                return year
        }
    }
    this.getMonth = function () {
        var tmp = parseInt((fastTime - gmt2001 + userTzOffset + (this.isDuringDst() ? 1000 * 60 * 60 : 0))  / 1000 / 60 / 60 / 24) + 1
        var year = 2001
        while(true) {
            var x = year % 4 == 0 ? 366 : 365
            if(tmp > x) {
                year++
                tmp -= x
            } else
                break
        }
        var month = 0
        while(true) {
            x = month == 1 && year % 4 == 0 ? 29 : dayCounts[month]
            if(tmp > x) {
                month++
                tmp -= x
            } else
                return month
        }
    }
    this.getDate = function () {
        var tmp = parseInt((fastTime - gmt2001 + userTzOffset + (this.isDuringDst() ? 1000 * 60 * 60 : 0))  / 1000 / 60 / 60 / 24) + 1
        var year = 2001
        while(true) {
            var x = year % 4 == 0 ? 366 : 365
            if(tmp > x) {
                year++
                tmp -= x
            } else
                break
        }
        var month = 0
        while(true) {
            x = month == 1 && year % 4 == 0 ? 29 : dayCounts[month]
            if(tmp > x) {
                month++
                tmp -= x
            } else
                return tmp
        }
    }
    this.getHours = function () {
        return parseInt((fastTime - gmt2001 + userTzOffset + (this.isDuringDst() ? 1000 * 60 * 60 : 0))  / 1000 / 60 / 60) % 24
    }
    this.getMinutes = function () {
        return parseInt((fastTime - gmt2001 + userTzOffset + (this.isDuringDst() ? 1000 * 60 * 60 : 0))  / 1000 / 60) % 60
    }
    this.getSeconds = function () {
        return parseInt((fastTime - gmt2001 + userTzOffset + (this.isDuringDst() ? 1000 * 60 * 60 : 0))  / 1000) % 60
    }
    this.getMilliseconds = function () {
        return parseInt(fastTime - gmt2001 + userTzOffset + (this.isDuringDst() ? 1000 * 60 * 60 : 0)) % 1000
    }
    this.getDay = function () {
        return parseInt((fastTime - gmt2001 + userTzOffset + (this.isDuringDst() ? 1000 * 60 * 60 : 0))  / 1000 / 60 / 60 / 24 + 1) % 7
    }
    this.getTime = function () {
        return fastTime
    }
    this.toString = function () {
        return this.getYear() + '-' + this.getMonth() + '-' + this.getDate() + ' ' + this.getHours() + ':' + this.getMinutes() + ':' + this.getSeconds() + ' (' +dayNames[this.getDay()] + ') (' +  userTzName + ' ' + (this.isDuringDst() ? 'Daylight' : 'Standard') + ' Time)'
    }
    if(iYearTime != null && iYearTime*0+1!=1) {
        fastTime = '-' * 1
        return
    }
    if(tzDst != false)
        tzDst = true
    iYearTime = parseInt(iYearTime*1)
    iMonth = parseInt(iMonth*1)
    iDate = parseInt(iDate*1)
    iHour = parseInt(iHour*1)
    iMinute = parseInt(iMinute*1)
    iSecond = parseInt(iSecond*1)
    iMs = parseInt(iMs*1)
    if(iMonth*0+1!=1) {
        iMonth = iDate = iHour = iMinute = iSecond = iMs = null
        var fastTime = iYearTime*0+1==1 ? iYearTime : new Date().getTime() + timeShift
        if(fastTime < gmt2001)
            throw new Error('MyDate does not support pre-2001 dates')
    } else {
        if(iDate*0+1!=1)
            iDate = 1
        if(iHour*0+1!=1)
            iHour = 0
        if(iMinute*0+1!=1)
            iMinute = 0
        if(iSecond*0+1!=1)
            iSecond = 0
        if(iMs*0+1!=1)
            iMs = 0
        while(iMs > 999)
            iSecond++, iMs -= 1000
        while(iMs < 0)
            iSecond--, iMs += 1000
        while(iSecond > 59)
            iMinute++, iSecond -= 60
        while(iSecond < 0)
            iMinute--, iSecond += 60
        while(iMinute > 59)
            iHour++, iMinute -= 60
        while(iMinute < 0)
            iHour--, iMinute += 60
        while(iHour > 23)
            iDate++, iHour -= 24
        while(iHour < 0)
            iDate--, iHour += 24
        while(iMonth > 11)
            iYearTime++, iMonth -= 12
        while(iMonth < 0)
            iYearTime--, iMonth += 12
        if(iDate > (iMonth == 1 && iYearTime % 4 == 0 ? 29 : dayCounts[iMonth])) {
            do {
                iDate -= (iMonth == 1 && iYearTime % 4 == 0 ? 29 : dayCounts[iMonth])
                if(++iMonth > 11)
                    iYearTime++, iMonth -= 12
            } while(iDate > (iMonth == 1 && iYearTime % 4 == 0 ? 29 : dayCounts[iMonth]))
        } else if(iDate <= 0) {
            do {
                if(--iMonth < 0)
                    iYearTime--, iMonth += 12
                if(iMonth == 1 && (iYearTime % 4) == 0)
                    iDate += 29
                else
                    iDate += dayCounts[iMonth]
            } while (iDate <= 0)
        }
        while(iMonth-- > 0)
            if(iMonth == 1 && iYearTime % 4 == 0)
                iDate += 29
            else
                iDate += dayCounts[iMonth]
        if(iYearTime < 2001)
            throw new Error('MyDate does not support pre-2001 years: ' + iYearTime)
        else if(iYearTime > 2100)
            throw new Error('MyDate does not support post 2100 years')
        else
            fastTime = gmt2001 + (((iYearTime - 2001) * 365 + parseInt((iYearTime - 2001) / 4)) * 24 * 60 * 60 * 1000) - userTzOffset
        fastTime += (iDate - 1) * 24 * 60 * 60 * 1000
        var dstIs = this.isDuringDst()
        if(dstIs) {
            fastTime += 1000 * 60 * 60 * 24
            var dstWillEnd = !this.isDuringDst()
            fastTime -= 1000 * 60 * 60 * 24
        } else {
            fastTime += 1000 * 60 * 60 * 24
            var dstWillStart = this.isDuringDst()
            fastTime -= 1000 * 60 * 60 * 24
        }
        var fromMidnight = ((iHour * 60 + iMinute) * 60 + iSecond) * 1000 + iMs
        if(dstIs)
            fastTime -= 1000 * 60 * 60
        if(dstWillStart && fromMidnight >= 1000 * 60 * 60 * 2)
            fromMidnight -= 1000 * 60 * 60
        if(dstWillEnd && fromMidnight >= 1000 * 60 * 60 * 1)
            fromMidnight += 1000 * 60 * 60
        fastTime += fromMidnight
    }
    if(fastTime*0+1 != 1) {
        throw new Error('Problem')
    }
}
function formatDateTime(dateObject) {
    return formatDate(dateObject) + ' ' + formatTime(dateObject);
}
function formatDate(dateObject) {
    if(!dateObject)
        return '';
    if(!dateObject.getFullYear)
        dateObject = new MyDate(dateObject);
    var tmpMonth = dateObject.getMonth() + 1;
    var tmpDate = dateObject.getDate();
    if(tmpMonth < 10)
        tmpMonth = '0' + tmpMonth;
    if(tmpDate < 10)
        tmpDate = '0' + tmpDate;
    return dateObject.getFullYear() + '-' + tmpMonth + '-' + tmpDate;
}
function formatTime(tmpDate, seconds) {
    if(!tmpDate)
        return '';
    if(!tmpDate.getFullYear)
        tmpDate = new MyDate(tmpDate);
    var tmpHour = tmpDate.getHours();
    var tmpMinute = tmpDate.getMinutes();
    var ampm = '';
    if(!switchMilitaryTime)
        if(tmpHour > 11)
            ampm = ' PM';
        else
            ampm = ' AM';
    if(!switchMilitaryTime)
        if(tmpHour == 0)
            tmpHour = 12;
        else if(tmpHour > 12)
            tmpHour -= 12;
    if(tmpMinute < 10)
        tmpMinute = '0' + tmpMinute;
    if(seconds) {
        var tmpSeconds = tmpDate.getSeconds();
        if(tmpSeconds < 10)
            tmpSeconds = '0' + tmpSeconds;
    }
    return tmpHour + ':' + tmpMinute + (seconds ? ':' + tmpSeconds : '') + ampm;
}