/* 
    Document   : myLibrary.js
    Created on : October 20
    Author     : Derek De Jager
    Description: javascript for sales.html
*/

String.prototype.initCaps = function(){
    var text = this.replace(/\b\w+\b/g, function(word){
        return (word.substring(0,1).toUpperCase() + word.substring(1,word.length).toLowerCase());
    });
    return text;
}

Date.prototype.toTimeString = function(clockType){
    if (clockType == 12){
	var ampm = "";
	var curr_hour = this.getHours();
	var curr_min = this.getMinutes();
	var curr_sec = this.getSeconds();
	if (curr_hour < 12)
	{
	    ampm = "AM";
	}
	else
	{
	    ampm = "PM";
	}
	if (curr_hour == 0)
	{
	    curr_hour = 12;
	}
	if (curr_hour > 12)
	{
	curr_hour = curr_hour - 12;
	}
	
	//2 digit minutes
	curr_min = curr_min + "";

	if (curr_min.length == 1)
	{
	curr_min = "0" + curr_min;
	}
	//2 digit seconds
	curr_sec = curr_sec + "";

	if (curr_sec.length == 1)
	{
	    curr_sec = "0" + curr_sec;
	}
	return(curr_hour + ":" + curr_min + ":" + curr_sec + " " + ampm);
   
    }
    else{
	return("kk:mm:ss");
    }   
}

Date.prototype.toDayOfWeekString = function(){
        if(this.getDay() == 0){
            return("Sunday");
        }else if(this.getDay() == 1){
            return("Monday");
        }else if(this.getDay() == 2){
            return("Tuesday");
        }else if(this.getDay() == 3){
            return("Wednesday");
        }else if(this.getDay() == 4){
            return("Thursday");
        }else if(this.getDay() == 5){
            return("Friday");
        }else{ 
            return("Saturday");
        }
}

Date.prototype.toMonthFullTextString = function(){
    if(Date.getMonth() == 0){
        return("January")
    }else if(Date.getMonth() == 1){
        return("February")
    }else if(Date.getMonth() == 2){
        return("March")
    }else if(Date.getMonth() == 3){
        return("April")
    }else if(Date.getMonth() == 4){
        return("May")
    }else if(Date.getMonth() == 5){
        return("June")
    }else if(Date.getMonth() == 6){ 
        return("July")
    }else if(Date.getMonth() == 7){
        return("August")
    }else if(Date.getMonth() == 8){
        return("September")
    }else if(Date.getMonth() == 9){
        return("October")
    }else if(Date.getMonth() == 10){
        return("November")
    }else{
        return("December");
    }

}
Date.prototype.toSlashDateString = function(){
    var month = this.getMonth();
    month++;
    var day = this.getDate();
    var year = this.getFullYear();
    
    return(month + "/" + day + "/" + year);     
}

Number.prototype.toCurrencyString = function(){
    nStr = this.toFixed(2);
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)){
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return "$" + x1 + x2;
}

Number.prototype.toPercentString = function(){
    nStr = this.toFixed(2);
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)){
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2 + "%";
}

