//
// Javascript Date Object helper functions
//
Date.prototype.toBasicDate=function() {
    var months = new Array("January", "February", "March", "April", "May", "June", 
                           "July", "August", "September", "October", "November", "December");

    return "" + months[this.getMonth()] + " " + this.getDate() + ", " + ((this.getYear() < 1000 ? 1900 : 0) + this.getYear());
};
Date.prototype.toBasicTime=function() {
    var str = (this.getHours() > 12 ? this.getHours() - 12 : this.getHours()).toString();
    str += (this.getMinutes() > 9) ? ":" + this.getMinutes().toString() : ":0" + this.getMinutes().toString();
    str += (this.getHours() > 11) ? " PM" : " AM";
    return str;
};
Date.prototype.toServerGmt=function() {
    return this.getUTCFullYear().toString() + 
        "-" + (this.getUTCMonth() < 9 ? "0" : "") + (this.getUTCMonth() + 1).toString() +
        "-" + (this.getUTCDate() < 10 ? "0" : "") + this.getUTCDate().toString() + 
        " " + (this.getUTCHours() < 10 ? "0" : "") + this.getUTCHours().toString() +
        ":" + (this.getUTCMinutes() < 10 ? "0" : "") + this.getUTCMinutes().toString() + 
        ":" + (this.getUTCSeconds() < 10 ? "0" : "") + this.getUTCSeconds().toString();
};
Date.prototype.toSQLString=function() {
    var str = this.getFullYear().toString();
    str += "-" + (this.getMonth() < 9 ? "0" : "") + (this.getMonth() + 1).toString();
    str += "-" + (this.getDate() < 10 ? "0" : "") + this.getDate().toString();
    str += " " + (this.getHours() < 10 ? "0" : "") + this.getHours().toString();
    str += ":" + (this.getMinutes() < 10 ? "0" : "") + this.getMinutes().toString();
    str += ":" + (this.getSeconds() < 10 ? "0" : "") + this.getSeconds().toString();
    return str;
};
Date.prototype.toUSAString=function(){
    var str = (this.getMonth() < 9 ? "0" : "") + (this.getMonth() + 1).toString();
    str += "/" + (this.getDate() < 10 ? "0" : "") + this.getDate().toString();
    str += "/" + this.getFullYear().toString();
    str += " " + (this.getHours() < 10 ? "0" : "") + this.getHours().toString();
    str += ":" + (this.getMinutes() < 10 ? "0" : "") + this.getMinutes().toString();
    str += ":" + (this.getSeconds() < 10 ? "0" : "") + this.getSeconds().toString();
    return str;
};
Date.prototype.toFileString=function(){
    var str = (this.getMonth() < 9 ? "0" : "") + (this.getMonth() + 1).toString() + "-";
    str += (this.getDate() < 10 ? "0" : "") + this.getDate().toString() + "-" + this.getFullYear().toString() + "-";
    str += (this.getHours() < 10 ? "0" : "") + this.getHours().toString();
    str += (this.getMinutes() < 10 ? "0" : "") + this.getMinutes().toString();
    str += (this.getSeconds() < 10 ? "0" : "") + this.getSeconds().toString();
    return str;
};
//
// Javascript String Object helper functions
//
String.prototype.isEmpty=function() {
    return (this == null || this.length == 0);
};
String.prototype.trim=function() {
    var s = this.replace(/^\s+/g,"");
    return s.replace(/\s+$/g,"");
};
String.prototype.isEmail=function() {
    var at=this.indexOf('@');
    var dot=this.lastIndexOf('.');
    return !(at < 1 || dot < 3 || dot >= this.length-1);
};
String.prototype.isValidPassword=function() {
    if(this.length<7)
        return false;
    else if(doesContain(this,"0123456789!@$?_-")==0)
        return false;
    else if(doesContain(this,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")==0)
        return false;
    else
        return true;
};
String.prototype.padLeft=function(c,length){
    var str = this;
    if (isNaN(length) == false && this.length < length && c != null && c.length == 1){
        for(var i=0; i<length-this.length; i++){
            str = c + str;
        }
    }
    return str;
}
String.prototype.getAlphaNumeric=function(){
    return this.replace(/[^a-zA-Z0-9]+/g,'');
};
function doesContain(strInput,strCheck) {
    var nCount=0;
    for(var i=0;i<strInput.length;i++) {
        if(strCheck.indexOf(strInput.charAt(i))>-1) {
            nCount++;
        }
    }
    return nCount++;
}

function formatFileSize(size){
    if (isNaN(size)) return size;
    var fsize=parseFloat(size);
    var suffix="bytes";
    if (fsize>1024.0){
        fsize = (fsize*1.0)/1024.0;
        suffix="KB";
        if (fsize>1024.0){
            fsize=fsize/1024.0;
            suffix="MB";
            if (fsize>1024.0){
                fsize=fsize/1024.0;
                suffix="GB";
            }
        }
    }
    return (Math.round(fsize*100)/100).toString() + " " + suffix;
}

function TimezoneDetect(){
    var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
    var intOffset = 100; //set initial offset high so it is adjusted on the first attempt
    var intMonth;
    var intHoursUtc;
    var intHours;
    var intDaysMultiplyBy;
   
    //go through each month to find the lowest offset to account for DST
    for (intMonth=0;intMonth < 12;intMonth++){
        //go to the next month
        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);
       
        //To ignore daylight saving time look for the lowest offset.
        //Since, during DST, the clock moves forward, it'll be a bigger number.
        if (intOffset > (dtDate.getTimezoneOffset() * (-1))){
            intOffset = (dtDate.getTimezoneOffset() * (-1));
        }
    }
 
    return intOffset;
}

function DstDetect(){
    var dtDstDetect = new Date();
    var dtDstStart = '';
    var dtDstEnd = '';
    var dtDstStartHold = ''; //Temp date hold
    var intYearDayCount = 732; //366 (include leap year) * 2 (for two years)
    var intHourOfYear = 1;
    var intDayOfYear;
    var intOffset = TimezoneDetect(); //Custom function. Make sure you include it.
    
    //Start from a year ago to make sure we include any previously starting DST
    dtDstDetect = new Date()
    dtDstDetect.setUTCFullYear(dtDstDetect.getUTCFullYear() - 1);
    dtDstDetect.setUTCHours(0,0,0,0);
  
    //Going hour by hour through the year will detect DST with shorter code but that could result in 8760
    //FOR loops and several seconds of script execution time. Longer code narrows this down a little.
    //Go one day at a time and find out approx time of DST and if there even is DST on this computer.
    //Also need to make sure we catch the most current start and end cycle.
    for(intDayOfYear = 1; intDayOfYear <= intYearDayCount; intDayOfYear++){
        dtDstDetect.setUTCDate(dtDstDetect.getUTCDate() + 1);
                   
        if ((dtDstDetect.getTimezoneOffset() * (-1)) != intOffset && dtDstStartHold == ''){
            dtDstStartHold = new Date(dtDstDetect);
        }
        if ((dtDstDetect.getTimezoneOffset() * (-1)) == intOffset && dtDstStartHold != ''){
            dtDstStart = new Date(dtDstStartHold);
            dtDstEnd = new Date(dtDstDetect);
            dtDstStartHold = '';
 
            //DST is being used in this timezone. Narrow the time down to the exact hour the change happens
            //Remove 48 hours (a few extra to be on safe side) from the start/end date and find the exact change point
            //Go hour by hour until a change in the timezone offset is detected.
            dtDstStart.setUTCHours(dtDstStart.getUTCHours() - 48);
            dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() - 48);
           
            //First find when DST starts
            for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){
                dtDstStart.setUTCHours(dtDstStart.getUTCHours() + 1);
               
                //If we found it then exit the loop. dtDstStart will have the correct value left in it.
                if ((dtDstStart.getTimezoneOffset() * (-1)) != intOffset){
                    break;
                }
            }
 
            //Now find out when DST ends
            for(intHourOfYear=1; intHourOfYear <= 48; intHourOfYear++){
                dtDstEnd.setUTCHours(dtDstEnd.getUTCHours() + 1);
               
                //If we found it then exit the loop. dtDstEnd will have the correct value left in it.
                if ((dtDstEnd.getTimezoneOffset() * (-1)) != (intOffset + 60)){
                    break;
                }
            }
           
            //Check if DST is currently on for this time frame. If it is then return these values.
            //If not then keep going. The function will either return the last values collected
            //or another value that is currently in effect
            if ((new Date()).getTime() >= dtDstStart.getTime() && (new Date()).getTime() <= dtDstEnd.getTime()){
                return new Array(dtDstStart,dtDstEnd);
            }
 
        }       
    }
    return new Array(dtDstStart,dtDstEnd);
}

function getScrollTop() {
    var top = document.body.scrollTop;
    if (top == 0) {
        if (window.pageYOffset) {
            top = window.pageYOffset;
        } else {
            top = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }
    }
    return top;
}
function getScrollLeft() {
    var left = document.body.scrollLeft;
    if (left == 0) {
        if (window.pageXOffset) {
            left = window.pageXOffset;
        } else {
            left = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
        }
    }
    return left;
}
function cbPrompt(str,def)
{
    if (window.attachEvent && window.showModalDialog) { 
        var settings = "dialogWidth: 290px; dialogHeight: 200px; center: yes; edge: raised; scroll: auto; status: no;";
	    return window.showModalDialog("iePrompt.html" + (def ? "?def=" + def : ""), str, settings);
    } else {
        return prompt(str,def);
    }
}
