// functions.js
// bmcgee@qeh2.com
// 2008-05-15
// functions for the tmmc website
//

// show the errMsgBox with title and message
function errMsg(title, message) {
    var titleID = document.getElementById("divErrTitle");
    titleID.innerHTML = title;
    var messageID = document.getElementById("divErrMessage");
    messageID.innerHTML = message
    var msgBoxID = document.getElementById("errMsgBox");
    msgBoxID.style.display = "";
    window.location = "#errMsgBox";
}

// hide the errMsgBox 
function hideErrMsg() {
    var msgBoxID = document.getElementById("errMsgBox");
    msgBoxID.style.display = "none";
}

// validate sumthin
function validateSumthin() {
    var success = true;
    var msg = "";

    success = false;
    msg = "sumthin hapnd<br>";

    if (!success) errMsg("Error!", msg);
    return success;
}

// validate revnetworking invitation reques
// bmcgee@qeh2.com
// 2009-04-19
function validateInvite() {
    var success = true;
    var msg = "";

    var nameID = document.form1.name;
    if (nameID.value.length <= 0) {
        msg += "Name is invalid.<br>";
        success = false;
    }

    var companyID = document.form1.company;
    if (companyID.value.length <= 0) {
        msg += "Company is invalid.<br>";
        success = false;
    }

    var businessPhoneID = document.form1.businessphone;
    if (businessPhoneID.value.length <= 0) {
        msg += "Business Phone is invalid.<br>";
        success = false;
    }

    var emailID = document.form1.email;
    if (!isEmail(emailID.value)){
        msg += "Email is invalid.<br>";
        success = false;
    }
    
    var dateID = document.form1.eventdate;
    if (dateID.value.length <= 0) {
        msg += "Event Date is invalid.<br>";
        success = false;
    }

    if (!success) errMsg("Error!", msg);
    return success;
}



// 
function stripLeadingZeros(s) {
    var result = s
    while (result.substring(0, 1) == "0") {
        result = result.substring(1, result.length)
    }
    return result
}

// return true if s is an integer value
function isInteger(s) {
    var work = stripLeadingZeros(s);
    return (work != "" && !isNaN(work) && (work / 1) == parseInt(work));
}

// return true if s is a decimal value
function isDecimal(s) {
    var work = stripLeadingZeros(s);
    return (work != "" && !isNaN(work));
}


// return true if s is an integer
function isIntegerOld(s) {
    var i;
    var success = true;
    if (s.length <= 0) {
        success = false;
    } else {
        for (i = 0; i < s.length; i++) {
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) success = false;
        }
    }
    return success;
}

//
function strip(s, str) {
    var i;
    var returnString = "";
    for (i = 0; i < str.length; i++) {
        var c = str.charAt(i);
        if (s.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// how many days in feb of year
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;
}

// Declaring valid date character, minimum year and maximum year
var dtCh = '-';
var tmCh = ':';
var spCh = ' ';
var minYear = 1900;
var maxYear = 2100;

// return true if dateStr is a valid date 
function isDate(dateStr) {
    var daysInMonth = DaysArray(12);
    var pos1 = dateStr.indexOf(dtCh);
    var pos2 = dateStr.indexOf(dtCh, pos1 + 1);
    var strMonth = dateStr.substring(0, pos1);
    var strDay = dateStr.substring(pos1 + 1, pos2);
    var strYear = dateStr.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) {
        return false;
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        return false;
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        return false;
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        return false;
    }
    if (dateStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(strip(dtCh, dateStr)) == false) {
        return false;
    }
    return true;
}

// return true if dateStr is a valid date and time
function isDateTime(dateStr) {
    var daysInMonth = DaysArray(12);
    var pos1 = dateStr.indexOf(dtCh);
    var pos2 = dateStr.indexOf(dtCh, pos1 + 1);
    var pos3 = dateStr.indexOf(spCh, pos2 + 1);
    var pos4 = dateStr.indexOf(tmCh, pos3 + 1);
    var strMonth = dateStr.substring(0, pos1);
    var strDay = dateStr.substring(pos1 + 1, pos2);
    var strYear = dateStr.substring(pos2 + 1, pos3);
    var strHour = dateStr.substring(pos3 + 1, pos4);
    var strMinute = dateStr.substring(pos4 + 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);
    hour = parseInt(strHour);
    minute = parseInt(strMinute);
    if (pos1 == -1 || pos2 == -1) {
        return false;
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        return false;
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        return false;
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        return false;
    }
    var strippedDate = strip(dtCh, dateStr);
    strippedDate = strip(tmCh, strippedDate);
    strippedDate = strip(spCh, strippedDate);

    if (dateStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(strippedDate) == false) {
        return false;
    }
    if (hour > 23 || hour < 0) {
        return false;
    }
    if (minute > 60 || minute < 0) {
        return false;
    }

    return true;
}

// return true if str is a valid email
function isEmail(emailStr) {
    var at = "@";
    var dot = ".";
    var lat = emailStr.indexOf(at);
    var lstr = emailStr.length;
    var ldot = emailStr.indexOf(dot);
    if (emailStr.indexOf(at) == -1) {
        return false;
    }

    if (emailStr.indexOf(at) == -1 || emailStr.indexOf(at) == 0 || emailStr.indexOf(at) == lstr) {
        return false;
    }

    if (emailStr.indexOf(dot) == -1 || emailStr.indexOf(dot) == 0 || emailStr.indexOf(dot) == lstr) {
        return false;
    }

    if (emailStr.indexOf(at, (lat + 1)) != -1) {
        return false;
    }

    if (emailStr.substring(lat - 1, lat) == dot || emailStr.substring(lat + 1, lat + 2) == dot) {
        return false;
    }

    if (emailStr.indexOf(dot, (lat + 2)) == -1) {
        return false;
    }

    if (emailStr.indexOf(" ") != -1) {
        return false;
    }

    return true;
}

// return true if zipStr is a valid 5 digit zip code
function isZip5(zipStr) {
    return (isInteger(zipStr) && zipStr.length == 5);
}

// return true if zipStr is a valid 4 digit zip code
function isZip4(zipStr) {
    return (isInteger(zipStr) && zipStr.length == 4);
}


// return true if phoneStr is a valid phone number
function isPhone(phoneStr) {
    var success = true;
    var strippedPhone = phoneStr;
    strippedPhone = strip(" ", strippedPhone);
    strippedPhone = strip(")", strippedPhone);
    strippedPhone = strip("(", strippedPhone);
    strippedPhone = strip("-", strippedPhone);
    if (!isInteger(strippedPhone)) success = false;
    if (strippedPhone.length != 10) success = false;
    return success;
}
