//no nulls
function NoNulls(object){
	if(object.value.length == 0){
		alert("This field cannot be left blank.");
		object.focus();
	}
}

//Is Number
function IsNumber(sString){
	//(^-?\d\d*$)/
	var numberOnly  = /(^-?\d\d*$)/;
	return numberOnly.test(sString);
}

//Number Only
function NumberOnly(object){
	//(^-?\d\d*$)/
	var numberOnly  = /(^-?\d\d*$)/;
	var sString = object.value;
	if(!numberOnly.test(sString)){
		object.value = sString.substring(0, sString.length-1);
		
	}
	
}

function IsTimeCharacter(sString){
	//(^-?\d\d*$)/
	var numberOnly  = /(^-?\d\d*$)(AM|PM|am|pm)$/;
	return numberOnly.test(sString);
}

//formats the time
function FormatTime(object){
	timeValue = object.value;
	newValue = timeValue;
	
	if((timeValue.length == 3) && timeValue.indexOf(':') == -1){
		newValue = timeValue.substring(0, 1) + ":" + timeValue.substring(1, 3);
	}	
	
	if((timeValue.length == 4) && timeValue.indexOf(':') == -1){
		newValue = timeValue.substring(0, 2) + ":" + timeValue.substring(2, 4);
		
	}else if((timeValue.length == 5) && timeValue.indexOf(':') > 0){
		temp = timeValue.replace(/[\:]/g, "");
		newValue = temp.substring(0, 2) + ":" + temp.substring(2, 4);
	}	
	
	object.value = newValue;	
}


//formats the phone number
function FormatPhone(object){
	temp = object.value;
	number = temp.replace(/[\(\)-]/g, "");
	newNumber = number;
	
	if(!IsNumber(number)){		//if its not a number delete one and repost it
		object.value = temp.substring(0, temp.length-1);
		return true;
	}
		
	if(number.length > 3){
		newNumber = "(" + number.substring(0, 3) + ")" + number.substring(3, 6);
	}
	
	if(number.length > 6){
		newNumber += "-" + number.substring(6, 10);
	}
	
	if(number.length > 10){
		newNumber = number.substring(0, 1) + "(" + number.substring(1, 4) + ")" + number.substring(4, 7)+ "-" + number.substring(7, 11);
	}	
	
	object.value = newNumber;		
}

//checks for valid Zipcodes
function IsZip(object){
	pattern = /^\d+-?\d+$/;
	text = object.value;
	if(!pattern.test(text)){
		alert("This is not a valid zip code.");
		object.select();
		object.focus();
		return false;
	}
	
	return true;
}
//capitalize every word
function Capitalize(object) {
	text = object.value.toLowerCase();
	text = trimAll(text);
    newVal = '';
    text = text.split(' ');
    for(var c=0; c < text.length; c++) {
    	newVal += text[c].substring(0,1).toUpperCase() + text[c].substring(1,text[c].length);
		if(c+1 != text.length){
			newVal += ' ';
		}
    }
    object.value = newVal;
}

function trimAll(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}


function IsState(object){
	stateString = "AL,AK,AS,AZ,AR,CA,CO,CT,DE,DC,FM,FL,GA,GU,HI,ID,IL,IN,IA,KS,KY,LA,ME,MH,MD,MA,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,MP,OH,OK,OR,PW,PA,PR,RI,SC,SD,TN,TX,UT,VT,VI,VA,WA,WV,WI,WY";
	text = object.value.toUpperCase();
	if(stateString.indexOf(text) == -1){
		alert("This is not a valid state.  Please re-enter the state.");
		object.select();
		object.focus();
		return false;
	}
	object.value = text;
	return true;
}

// Checks if time is in HH:MM AM/PM format.
// The seconds and AM/PM are optional.
function IsValidTime(timeString) {

	var timePattern = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeString.match(timePattern);
	if (matchArray == null) {
		alert("Please enter a valid time format.");
		return false;
	}
	
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }
	
	if (hour < 0  || hour > 23) {
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
//	if (hour <= 12 && ampm == null) {
//		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
//			alert("You must specify AM or PM.");
//			return false;
//	   }
//	}
	if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	if (minute<0 || minute > 59) {
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		alert ("Second must be between 0 and 59.");
		return false;
	}
	
	return true;
}

//needs the control name for this so it can reformat the field.
// uses functions IsDate and FormatDate
function CheckDate(CONTROL){ 
	if(!IsDate(CONTROL.value)){ 
		alert("Please enter a valid date format.") 
		return false; 
	} 

	CONTROL.value = FormatDate(CONTROL.value); 
	return true;
} 

//validator allows nulls 
function IsDate(DateToCheck){
	if(DateToCheck == ""){
		return true;
	}

	var m_strDate = FormatDate(DateToCheck);
	if(m_strDate == ""){
		return false;		
	}
	
	var m_arrDate = m_strDate.split("/");
	var m_MONTH = m_arrDate[0];
	var m_DAY = m_arrDate[1];
	var m_YEAR = m_arrDate[2];
	if(m_YEAR.length > 4){
		alert("Please enter a 4 digit year");
		return false;
	}
	m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
	
	var testDate=new Date(m_strDate);
	if(testDate.getMonth()+1 == m_MONTH){
		return true;
	}else{
		return false;
	}
}//end function

function FormatDate(DateToFormat){
	if(DateToFormat==""){
		return "";
	}
		
	var strReturnDate;
	DateToFormat = DateToFormat.toLowerCase();
	var arrDate
	var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var strMONTH;
	var Separator = "/";
	
	arrDate = DateToFormat.split(Separator);
	DateToFormat = "";
	
	for(var iSD = 0; iSD < arrDate.length; iSD++){
		if(arrDate[iSD] != ""){
		DateToFormat += arrDate[iSD] + Separator;
		}
	}
	
	DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
	arrDate = DateToFormat.split(Separator);
	
	if(arrDate.length < 3){
		return "";
	}
	
	var MONTH = arrDate[0];
	var DAY = arrDate[1];
	var YEAR = arrDate[2];
	
	if(parseFloat(arrDate[1]) > 12){
		DAY = arrDate[1];
		MONTH = arrDate[0];
	}
	
	if(parseFloat(DAY) && DAY.toString().length==4){
		YEAR = arrDate[0];
		DAY = arrDate[2];
		MONTH = arrDate[1];
	}
	
	
	for(var iSD = 0;iSD < arrMonths.length;iSD++){
		var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
		var MonthPosition = DateToFormat.indexOf(ShortMonth);
	
		if(MonthPosition > -1){
			MONTH = iSD + 1;
			if(MonthPosition == 0){
				DAY = arrDate[1];
				YEAR = arrDate[2];
			}
			break;
		}
	}
	
	var strTemp = YEAR.toString();
	if(strTemp.length==2){
		if(parseFloat(YEAR)>40){
			YEAR = "19" + YEAR;
		}else{
			YEAR = "20" + YEAR;
		}
	
	}
	
	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
		MONTH = "0" + MONTH;
	}
	
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
		DAY = "0" + DAY;
	}
		
	switch (FormatAs){
		case "dd/mm/yyyy":
			return DAY + "/" + MONTH + "/" + YEAR;
		case "mm/dd/yyyy":
			return MONTH + "/" + DAY + "/" + YEAR;
		case "dd/mmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
		case "mmm/dd/yyyy":
			return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
		case "dd/mmmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
		case "mmmm/dd/yyyy":
			return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}
	
	return DAY + "/" + strMONTH + "/" + YEAR;;

} //End Function
