//Fuctions to mimmick LTrim,  RTrim, and Trim...

function LTrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1) {
		// We have a string with leading blank(s)...
		var j=0, i = s.length;
		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
			// Get the substring from the first non-whitespace
			// character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str) {
	// We don't want to trip JUST spaces, but also tabs,
	// line feeds, etc.  Add anything else you want to
	// "trim" here in Whitespace
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		// We have a string with trailing blank(s)...
		var i = s.length - 1;       // Get length of string
		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}
	return s;
}

function Trim(str){
	return RTrim(LTrim(str));
}

function validateDropDownDate(formName, arg) {
	//The function validates the date selected and resets the selection if it is invalid

	var	dayBox = eval(formName + '.day_' + arg);
	var	monthBox = eval(formName + '.month_' + arg);
	var	yearBox = eval(formName + '.year_' + arg);

	if (monthBox.value==2) {
		if (yearBox.value % 4 != 0) {	//Normal year, Feburary has 28.	
			if (dayBox.value>28) {
				dayBox.selectedIndex=27;
			}
		} else {						//Leap year, Feburary has 29.
			if (dayBox.value>29) {
				dayBox.selectedIndex=28;
			}
		}
	
	}  else {
		//Months that have 30 days
		if ((monthBox.value==4) || (monthBox.value==6) || (monthBox.value==9) || (monthBox.value==11)) {
			if (dayBox.value>30) {
				dayBox.selectedIndex=29;
			}
		} else {
		//Okey-dokey
		}	
	}
}
function validateStringDate(arg,dateType,allowEmpty) {
	//function validates date format (dd/mm/yy or mm/yy) entered as string.
	var isOK=true;
	arg=Trim(arg);
	if (!((arg.length==0) && allowEmpty)) {
		if (dateType==1) { //date is dd/mm/yy
			var dArray=arg.split('/');
			var dd = parseInt(dArray[0],10);
			var mm = parseInt(dArray[1],10);
			var yy = parseInt(dArray[2],10);
			if (isNaN(dd) || isNaN(mm) || isNaN(yy)) {
				isOK=false;
			} else {
				if ((dd<1) || (dd>31) || (mm>12) || (mm<1) || (yy<1900) || (yy>9999)) {
					isOK=false;
				} else {
					//Check the number of days against the month
					if (yy%4 == 0) { //Leap year
						if ((mm==2) && (dd>29)) {
							isOK=false;
						}
					} else { 		//Normal year
						if ((mm==2) && (dd>28)) {
							isOK=false;
						}
					}							
					if ((mm==4) || (mm==6) || (mm==9) || (mm==11)) {
						if (dd>30) {
							isOK=false;
						}
					} else {
						if (dd>31) {
							isOK=false;
						}
					}
				}
			}
		} else { 	//date is mm/yy
			var dArray=arg.split('/');
			var mm = parseInt(dArray[0],10);
			var yy = parseInt(dArray[1],10);
		
			if (isNaN(mm) || isNaN(yy)) {
				isOK=false;
			} else {
				if ((mm>12) || (mm<1) || (yy<1) || (yy>2999)) {
					isOK=false;
				}
			}
		}
	}
	return isOK;
}



function dateGreater(arg1,arg2,equalOK) {
	//Function checks whether arg2 is greater date than arg1. Years below 30 are assumed to be in 21 century.
	var isGreater = false;
	var fullDate = ((arg1.length>7) && (arg2.length>7));
	
	if (fullDate) {
	
		var dArray1=arg1.split('/');
		var dd1 = parseInt(dArray1[0],10);
		var mm1 = parseInt(dArray1[1],10);
		var yy1 = parseInt(dArray1[2],10);
	
//		if (yy1<30) {
//			yy1+=2000
//		} else {
//			yy1+=1900
//		}
	
		var dArray2=arg2.split('/');
		var dd2 = parseInt(dArray2[0],10);
		var mm2 = parseInt(dArray2[1],10);
		var yy2 = parseInt(dArray2[2],10);

//		if (yy2<30) {
//			yy2+=2000
//		} else {
//			yy2+=1900
//		}
	
		if (yy2>yy1) {
			isGreater=true;
		} else { 
			if (yy2==yy1) {
				if (mm2>mm1)  {
					isGreater=true;
				} else {
					if ((mm2==mm1) && (dd2>dd1)) {
						isGreater=true;
					} else {
						isGreater=false;
					}
				}
			} else {
				isGreater=false;
			}
		}	
	} else {
		var dArray1=arg1.split('/');
		var mm1 = parseInt(dArray1[0],10);
		var yy1 = parseInt(dArray1[1],10);
	
//		if (yy1<30) {
//			yy1+=2000
//		} else {
//			yy1+=1900
//		}
	
		var dArray2=arg2.split('/');
		var mm2 = parseInt(dArray2[0],10);
		var yy2 = parseInt(dArray2[1],10);

//		if (yy2<30) {
//			yy2+=2000
//		} else {
//			yy2+=1900
//		}
	
		if (yy2>yy1) {
			isGreater=true;
		} else { 
			if (yy2==yy1) {
				if (mm2>mm1)  {
					isGreater=true;
				} else {
					isGreater=false;
				}
			} else {
				isGreater=false;
			}
		}	
	}
	if (equalOK) {
		if (fullDate) {
			if ((yy2==yy1) && (mm2==mm1) && (dd2==dd1)) {
				isGreater=true;
			}
		} else {
			if ((yy2==yy1) && (mm2==mm1)) {
				isGreater=true;
			}
		}
	}
	return isGreater;
}


function autoComplete(box,dateType) {
	//var box = eval(boxName);
	var arg = box.value;
	if (!(arg.length==0)) {
		if (dateType==1) {
			var dArray=arg.split('/');
			var dd = parseInt(dArray[0],10);
			var mm = parseInt(dArray[1],10);
			var yy = parseInt(dArray[2],10);
			if (isNaN(dd) || isNaN(mm) || isNaN(yy)) {
				
			} else {
				if (dd.toString().length==1) {
					dd="0"+dd;
				}
				
				if (mm.toString().length==1) {
					mm="0"+mm;
				}

				if (yy.toString().length<4) {
					if (yy<30) {
						yy=yy+2000;
					} else {
						yy=yy+1900;
					}
				}
				box.value=dd+"/"+mm+"/"+yy;
			}
		} else {
			var dArray=arg.split('/');
			var mm = parseInt(dArray[0],10);
			var yy = parseInt(dArray[1],10);
			if (isNaN(mm) || isNaN(yy)) {
				
			} else {
				if (mm.toString().length==1) {
					mm="0"+mm;
				}

				if (yy.toString().length<4) {
					if (yy<30) {
						yy=yy+2000;
					} else {
						yy=yy+1900;
					}
				}
				box.value=mm+"/"+yy;
			}
		}
	}
}
