// ratDateFunctions
// Javascript


// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function doNothing(){}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function trim(str){

	var s=rtrim(str);
	s=ltrim(s);
	return s;

}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function ltrim(str){

	var s=new String(str);
	var len=s.length;
	var i;

	if (len<=0)				//empty string
		{
			return str;
		}
	else						//full string
		{
			while (whitespace.indexOf(s.charAt(0)) == false)		//do this as long as the first character in the string is a space
				{
					s=s.substr(1);
				}
			return s;
		}

}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function rtrim(str){

	//this function simply reverses the string,
	//runs it through ltrim(),
	//reverses it again and returns it.

	var s = new String(str);
	var ar = s.split("");
	ar = ar.reverse();
	s = ar.join("");

	s = ltrim(s);

	ar = s.split("");
	ar = ar.reverse();
	s = ar.join("");

	return s;

}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function ratToTime(strTime){

	var str=new String(strTime);	
	var h;
	var m="00";
	var s="00";
	var ampm;
	var array_time;

	str=str.toUpperCase()

	if ((str.indexOf("A")>0)||(str.indexOf("P")>0))		//am or pm exists in the string
		{																//so parse that out of the str variable now & set ampm accordingly

			if (str.indexOf("A")>0)
				{
					str=str.replace("A","")
					str=str.replace("M","")
					str=trim(str);
					ampm="AM";
				}
			else
				{
					str=str.replace("P","")
					str=str.replace("M","")
					str=trim(str);
					ampm="PM";
				}
		}


			//check for colons.  If none, check for isNaN.
			if (str.indexOf(":")>=0)							//contains at least one colon, 
				{														//so split into array
					array_time=str.split(":");
					if (array_time.length>3)					//more than two colons
						{
							//str="too many colons";
							str=false;
						}
					else
						{
							//first check the hours element
							if (isNaN(array_time[0]))												//hours element isn't numeric!
								{
									str=false;
								}
							else
								{
									if ( (array_time[0]<1) || (array_time[0]>23) )			//more than 23 or less than 1
										{
											//str="less than 1 or more than 23";
											str=false;
										}
									else
										{
											if (ampm!=undefined)											//ampm has already been defined
												{
													if (array_time[0]>12)							//can't be over 12 if ampm was specified!
														{
															//str="can't be over 12 if ampm was specified!"
															str=false;
														}
													else
														{
															h=trim(array_time[0]);
														}
												}
											else															//ampm NEEDS to be defined
												{
													if (array_time[0]>12)							//military time?
														{
															h=trim(array_time[0]);
															ampm="PM";
														}
													else													//let it be A.M.!!!???!!!!
														{
															h=trim(array_time[0]);
															ampm="AM";
														}
												}
										}
								}
							//now check minutes
							if (isNaN(array_time[1]))												//minutes element is not numeric!
								{
									str=false;
								}
							else
								{
									if ( (array_time[1]<0) || (array_time[1]>59) )			//more than 59 or less than 0
										{
											//str="invalid minutes";
											str=false;
										}
									else
										{
											m=trim(array_time[1]);
											if (m.length==1){m = "0" + m}
										}
								}
							//finally, check seconds element, if there is one
							if (array_time.length==3)
								{
									if (isNaN(array_time[2]))								//seconds element is not numeric!
										{
											str=false;
										}
									else
										{
											if ( (array_time[2]<0) || (array_time[2]>59) )	//more than 59 or less than 0
												{
													//str="invalid seconds";
													str=false;
												}
											else
												{
													s=trim(array_time[2]);
													if (s.length==1){s = "0" + s}
												}
										}
								}
							else																	//set the seconds to 00
								{
									s="00";
								}
						}
				}
			else														//no colons
				{
					if ( (isNaN(str)) )							//not a valid hour
						{
							//str="not a valid hour";
							str=false;
						}
					else
						{
							if ( (str<1) || (str>23) )			//more than 23 or less than 1
								{
									//str="less than 1 or more than 23";
									str=false;
								}
							else
								{
									if (ampm!=undefined)											//ampm has already been defined
										{
											if (str>12)							//can't be over 12 if ampm was specified!
												{
													//str="can't be over 12 if ampm was specified!"
													str=false;
												}
											else
												{
													h=trim(str);
												}
										}
									else															//ampm NEEDS to be defined
										{
											if (str>12)							//military time?
												{
													h=trim(str);
													ampm="PM";
												}
											else													//let it be A.M.!!!???!!!!
												{
													h=trim(str);
													ampm="AM";
												}
										}
								}
						}
				}

	//concatenate the string from the various variables
	//as long as str==false
	if (str!=false)
/*
		if (m=="00")
			{str=h + " " + ampm}
		else
*/
			{str=h + ":" + m + " " + ampm}
//		{str=h + ":" + m + ":" + s + " " + ampm}
	//alert(str);
	return str;

}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function ratGetMMDDYYYY(dt){
		
	//this returns in the format mmddyyyy
	var m=dt.getMonth()+1;
	var d=dt.getDate();
	var y=dt.getFullYear();
		
	return m + "/" + d + "/" + y;
}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function ratToDate(strDate){


	//this returns a date object
	var str = new String(strDate);
	var dt = new Date();
	var m;
	var d;
	var y;
	var valid_days=new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	if (str.indexOf("/")==-1)
		{
			//alert("no slashes");
			return false;
		}
	else{

		array_date=str.split("/");
		
		if (array_date.length>3)
			{
				//alert("too many slashes");
				return false;
			}
		else{

			m=array_date[0];
			d=array_date[1];
			if (array_date.length==2)	//need this year
				{y=dt.getFullYear()}
			else					//use passed year (check for 1900-2000)
				{
					y=array_date[2];
					if ((y.length==3)||(y.length==1)||(y.length>4))		//invalid year
						{
							return false;
						}
					else				//year is 2 or 4 digits
						{
							if (y.length==2)			//make it the 21st century
								{y="20" + y}
						}

				}
			
			//now check for valid month/day
			if (m<1||m>12)	//invalid month
				{
					//alert("invalid month");
					return false;
				}
			else{
				if (y%4==0){valid_days[1]=29}	//if it's a leap year
				if (d<1||d>valid_days[m-1])
					{
						//alert("invalid days");
						return false;
					}
				else{
					dt=new Date(m + "/" + d + "/" + y);
					return dt;
				}
				
			}
		}
	}
}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function YearAdd(startDate, numYears)
{
		return DateAdd(startDate,0,0,numYears);
}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function MonthAdd(startDate, numMonths)
{
		return DateAdd(startDate,0,numMonths,0);
}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------

function DayAdd(startDate, numDays)
{
		return DateAdd(startDate,numDays,0,0);
}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------


// This function decodes the any string
// that's been encoded using URL encoding technique
function URLDecode(psEncodeString) 
{
  return unescape(psEncodeString); 
}

//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------



function ratToCurrency(n,dollar_sign)
{

	var amt;
	var strArray;
	var cur;
	
	amt=new String(n);
	amt=amt.replace("$","");
	//alert(amt=="");
	if (amt==0) {amt=new String("0")}	
		
	if ((isNaN(amt))||amt=="")			//not a number
		{
			if (amt.length>0)
				{return false;}
		}
	else
		{
			if (amt.indexOf(".")==-1)		//no decimal point passed
				{
					strArray=new Array(amt,0);
				}
			else
				{
					strArray=amt.split(".");
				}
				
			if (strArray[1]!=0)			//get the number and round it
				{
					if (strArray[1].length==1)
						{strArray[1]=strArray[1] + "0"}
					if (strArray[1].length>2)
						{strArray[1]=Math.round(strArray[1].substr(0,2) + "." + strArray[1].substr(2));}
					cur= (strArray[0] + "." + strArray[1]);
				}
			else
				{
					cur= (strArray[0] + ".00");
				}
				
			if (dollar_sign==true)
				{cur="$" + cur;}
				
			return cur;
		}



}
