// JAVASCRIPT FUNCTIONS TO RUN THE TIMER.
var dayarray=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

function getthedate()
{
    var mydate=new Date();
    var year=mydate.getYear();
    if (year < 1000)
        year+=1900;
    var day=mydate.getDay();
    var month=mydate.getMonth();
    var daym=mydate.getDate();
    if (daym<10)
        daym="0"+daym;
    var hours=mydate.getHours();
    var minutes=mydate.getMinutes();
    var seconds=mydate.getSeconds();
    var dn="AM";
    if (hours>=12)
        dn="PM";
    if (hours>12)
        hours=hours-12;
    if (hours==0)
        hours=12;
    if (minutes<=9)
        minutes="0"+minutes;
    if (seconds<=9)
        seconds="0"+seconds;
    var cdate=dayarray[day]+", "+montharray[month]+" "+daym+", "+year+" "+hours+":"+minutes+" "+dn;
    if (document.all)
        document.all.clock.innerHTML=cdate;
    else if (document.getElementById)
        document.getElementById("clock").innerHTML=cdate;
    else
    document.write(cdate);
}
if (!document.all&&!document.getElementById)
    getthedate();
function goforit()
{
    //Future Use
}
function goforit1()
{
    if (document.all||document.getElementById)
        setInterval("getthedate()",1000);
}

function validateInt(field)
{
    var initval = field.value;
    var newVal = "";
    var i;
    for (i=0; i<initval.length; i++) {
       var ch = initval.charAt(i);
       if (ch=='-' || (ch >= '0' && ch <='9')) {
           newVal = newVal + ch;
       }
    }
    field.value = newVal;
}

function validateLong(field)
{
    var initval = field.value;
    var newVal = "";
    var i;
    for (i=0; i<initval.length; i++) {
       var ch = initval.charAt(i);
       if (ch=='-' || (ch >= '0' && ch <='9')) {
           newVal = newVal + ch;
       }
    }
    field.value = newVal;
}

function validateBig(field)
{
    var initval = field.value;
    var newVal = "";
    var i;
    for (i=0; i<initval.length; i++) {
       var ch = initval.charAt(i);
       if (ch=='.' || ch=='-' || (ch >= '0' && ch <='9')) {
           newVal = newVal + ch;
       }
    }
    field.value = newVal;
}

function validateFloat(field)
{
    var initval = field.value;
    var newVal = "";
    var i;
    for (i=0; i<initval.length; i++) {
       var ch = initval.charAt(i);
       if (ch=='.' || ch=='-' || (ch >= '0' && ch <='9')) {
           newVal = newVal + ch;
       }
    }
    field.value = newVal;
}

function getNextToken(str)
{
    //skip leading whitespace/delimiters
    var i = 0;
    for (i = 0; i < str.length; i++) {
        var ch = str.charAt(i);
        if (ch != ' ' && ch != '\t' && ch != "," && ch != ":") {
            break;
        }
    }
    if (i == str.length) {
        //end reached, there is no token
        return "";
    }

    //capture token upto next whitespace/delimiters or end of line
    var token = "";
    for (; i < str.length; i++) {
        var ch = str.charAt(i);
        if (ch == ' ' || ch == '\t'  || ch == "," || ch == ":") {
            break;
        }
        else {
            token = token + ch;
        }
    }
    return token;
}

function validateDate(hidden, viewed)
{
    var initval = viewed.value;

    // validate the month
    var month = getNextToken(initval);
    if (month!="Jan" && month!="Feb" && month!="Mar" && month!="Apr" && month!="May" && month!="Jun" &&
        month!="Jul" && month!="Aug" && month!="Sep" && month!="Oct" && month!="Nov" && month!="Dec")
    {
        alert("Invalid month: '" + month + "'. Use format 'MMM dd, yyyy h:mm:ss TT'");
        viewed.value="";
        hidden.value="";
        return;
    }

    initval = initval.substring(initval.indexOf(month) + month.length);

    // validate the day
    var day = getNextToken(initval);
    if (isNaN(day)) {
        alert("Invalid day: '" + day + "'. Use format 'MMM dd, yyyy h:mm:ss TT'");
        viewed.value="";
        hidden.value="";
        return;
    }
    if (month=="Jan" || month=="Mar" || month=="May" || month=="Jul" || month=="Aug" || month=="Oct" || month=="Dec") {
        if (day < 0 || day > 31) {
            alert("Invalid day for specified month: '" + day + "'.");
            viewed.value="";
            hidden.value="";
            return;
        }
    }
    if (month=="Apr" || month=="Jun" || month=="Sep" || month=="Nov") {
        if (day < 0 || day > 30) {
            alert("Invalid day for specified month: '" + day + "'.");
            viewed.value="";
            hidden.value="";
            return;
        }
    }
    if (month=="Feb") {
        if (day < 0 || day > 29) {
            alert("Invalid day for specified month: '" + day + "'.");
            viewed.value="";
            hidden.value="";
            return;
        }
    }

    initval = initval.substring(initval.indexOf(day) + day.length);

    // validate the year
    var year = getNextToken(initval);
    if (isNaN(year) || (year.length != 4)) {
        alert("Invalid year: '" + year + "'. Use format 'MMM dd, yyyy h:mm:ss TT'");
        viewed.value="";
        hidden.value="";
        return;
    }

    var newVal = Date.parse(viewed.value);

    if (isNaN(newVal)) {
        alert("Not able to parse '"+viewed.value+"' as a date, please edit again.");
        viewed.value="";
        hidden.value="";
        return;
    }
    hidden.value = newVal;
    viewed.value = formatDate(newVal);
}

function showDate(date)
{
    document.write(formatDate(date));
}

function formatDate(dateLong)
{
    //if the value passed is null, display date as empty string
    if(dateLong == 'null') {
        return "";
    }
    //if the value passed is 0, this is the null-date value, and
    //display date as empty string.  (Yes, there is a particular date/time
    //in history that can not be dislpayed, but this is the value that
    //indicates that there is no value set....
    if(dateLong == 0) {
        return "";
    }
    // line below is a single generic date format for all locales
    return getDateStr(dateLong);

    //below is an alternative way to format, that makes use of a user's
    //locale settings, but unfortunately can not always be parsed!
    //It would be best to use the user's setting, but JS appears broken.
    //var dateObj=new Date();
    //dateObj.setTime(dateLong);
    //return dateObj.toLocaleString();
}

function showTimeZone()
{
    var d = (new Date()).getTimezoneOffset();
    if (d > 0) {
        sign = "-";
    }
    else {
        sign = "+";
    }
    d = Math.abs(d);
    var hours = d/60;
    hours = Math.floor(hours);
    if (hours<10) {
        hours = "0"+hours;
    }
    var mins = d%60;
    if (mins<10) {
        mins = "0"+mins;
    }
    document.write("<font color=\"gray\">Timezone: GMT"+ sign + hours+":"+mins+"</font>");

}

function getDateStr(date)
{
    if(date == 'null') {
        return "a null date was passed to the getDateStr routine.";
    }
    var mon,now,hour,min,ampm,time,str,tz,end,beg;
    mon=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
    var dateObj=new Date();
    dateObj.setTime(date);

    hour=dateObj.getHours();

    min=dateObj.getMinutes();
    ampm=(hour>=12)?"PM":"AM";
    hour=(hour==0)?12:(hour>12)?hour-12:hour;
    min=(min<10)?"0"+min:min;
    var sec = dateObj.getSeconds();

    tz="";
    str=dateObj.toString();
    end=str.lastIndexOf(" ");
    beg=str.lastIndexOf(" ",end-1)+1;
    if (end-beg==3) {
        tz=" "+str.charAt(beg)+"T";
    }
    var result=mon[dateObj.getMonth()]+" "+dateObj.getDate()+", "+dateObj.getFullYear()+" "+hour+":"+min+":"+sec+" "+ampm;
    return result;
}

// Note: deprecated function.  Keep only for compatibility
function setTimeValue(field,newVal){
    field.value = Date.parse(newVal);
}

// Note: deprecated function.  Keep only for compatibility
function setDateValue(field,newVal){
    field.value = getDateStr(newVal);
}

