// Support Functions
function strip(filter,str){
	var i,curChar;
	var retStr = '';
	var len = str.length;
	for(i=0; i<len; i++){
		curChar = str.charAt(i);
		if(filter.indexOf(curChar)<0) //not in filter, keep it
			retStr += curChar;
	}
	return retStr;
}

function reformat(str){
	var arg;
	var pos = 0;
	var retStr = '';
	var len = reformat.arguments.length;
	for(var i=1; i<len; i++){
		arg = reformat.arguments[i];
		if(i%2==1)
			retStr += arg;
		else{
			retStr += str.substring(pos, pos + arg);
			pos += arg;
		}
	}
	return retStr;
}

//End Support Functions
//Validation Rules
function notEmpty(str){
	if(strip(" \n\r\t",str).length ==0)
		return false;
	else
		return true;
}

function validateInteger(str){
	str = strip(' ',str);
	//remove leading zeros, if any
	while(str.length > 1 && str.substring(0,1) == '0'){
		str = str.substring(1,str.length);
	}
	var val = parseInt(str);
	if(isNaN(val))
		return false;
	else
		return true;
}

function validateFloat(str){
	str = strip(' ',str);
	//remove leading zeros, if any
	while(str.length > 1 && str.substring(0,1) == '0'){
		str = str.substring(1,str.length);
	}
	var val = parseFloat(str);
	if(isNaN(val))
		return false;
	else
		return true;
}

function validateUSPhone(str){
	str = strip("*() -./_\n\r\t\\",str);
	if(str.length == 10 || str.length == 7)
		return true;
	else
		return false;
}

function validateSSN(str){
	str = strip(" -.\n\r\t",str);
	if(validateInteger(str) && str.length == 9)
		return true;
	else
		return false;
}

function validateZip(str){
	str = strip("- \n\r\t",str);
	if(validateInteger(str)&&(str.length==9 || str.length==5))
		return true;
	else
		return false;
}

function validateCC(str,type){
	str = strip("-./_\n\r\t\\",str);
	if(type=="1")
		if(str.charAt(0)!="4")
			return false;
	if(type=="2")
		if(str.charAt(0)!="5")
			return false;
	if(type=="3")
		if(str.charAt(0)!="6")
			return false;
	if(type=="4")
		if(str.charAt(0)!="3")
			return false;
	if(validateInteger(str)&&((str.length==15&&type=="4") || str.length==16))
		return true;
	else
		return false;
}

function validateDate(str){
	var dateVar = new Date(str);
	if(isNaN(dateVar.valueOf()) || (dateVar.valueOf() ==0))
		return false;
	else
		return true;
}

function validateEMail(str){
	str = strip(" \n\r\t",str);
	if(str.indexOf("@")>-1 && str.indexOf(".")>-1)
		return true;
	else
		return false;
}

function validateURL(str){
	str = strip(" \n\r\t",str);
	if ((str.length>=7) && (str.indexOf("http://")>-1))
	 	return true;
	else
		return false;
}

function validateTime(time){

	var i;
	var segments;	// Break up of the time into hours and minutes
	var hour;	// The value of the entered hour
	var minute;	// The value of the entered minute
	var ampm        // The value of AM/PM
	
	time = strip('.',time);
	time = time.replace(".", ":");
	time = strip(' ',time)
	//alert("blah " + time);

	/* Validating standard time */
	
	segments = time.split(":");
		
	if (segments.length == 2) {
		
		// Strip out AM/PM.
		ampm = segments[1].substring(2, segments[1].length); // Note AM/PM must have a space between it and the time				
		// strip out minutes
		segments[1] = segments[1].substring(0,2);
		
		// Turn the segment string into a number			
		for(i = 0; i < 2; i++){				
			segments[i] = segments[i] - 0;
		}

		// Test the hour
		hour = segments[0]; 
		if ((hour > 12) || (hour < 1)) { 
			return false;
		}
			
		// Test the minute
		minute = segments[1];			
		if (( minute >= 60)) {
			return false;
		}
			
		// Check AM and PM
		if ((ampm != "") && ((ampm == "AM") || (ampm == "am") || (ampm == "PM") || (ampm == "pm"))) {	

		//if ((ampm != "") && ((ampm == "a.m.") || (ampm == "p.m."))) {
			return true;
		} else {
			return false;
		}

	} else {

		return false;

	}
}	

//End Validation Rules
//Formatting functions

function formatPhone(str){
	str = strip("*() -./_\n\r\t\\",str);
	if(str.length==10)
		return reformat(str,"(",3,") ",3,"-",4);
	if(str.length==7)
		return reformat(str,"",3,"-",4);
}

function formatSSN(str){
	str = strip(" -.\n\r\t",str);
	return reformat(str,"",3,"-",2,"-",4);
}

function formatZip(str){
	str = strip("- \n\r\t",str);
	if(str.length==5)
		return str;
	if(str.length==9)
		return reformat(str,"",5,"-",4);
}

function formatCC(str,type){
	str = strip("-./_\n\r\t\\",str);
	switch(type){
		case "1": 
			return reformat(str,"",4,"-",4,"-",4,"-",4);
			break;
		case "2": 
			return reformat(str,"",4,"-",4,"-",4,"-",4);
			break;
		case "3": 
			return reformat(str,"",4,"-",4,"-",4,"-",4);
			break;
		case "4":
			return reformat(str,"",4,"-",6,"-",5);
	}
}

function formatDate(str,style){

	var dateVar = new Date(str);
	// var year = dateVar.getYear();
	var year = dateVar.getFullYear();
	if(year < 2000) year += 100;
		
	switch(style){

		case "MM/DD/YY":

			return (dateVar.getMonth() + 1) + "/" + dateVar.getDate() + "/" + year;
			break;

		case "DD/MM/YY":

			return dateVar.getDate() + "/" + (dateVar.getMonth() + 1) + "/" + year;
			break;

		case "Month Day, Year":

			return getMonthName(dateVar) + " " + dateVar.getDate() + ", " + year;
			break;

		case "Day, Month Day, Year":

			return getDayName(dateVar) + ", " + getMonthName(dateVar) + " " + dateVar.getDate() + ", " + year;
			break;

		default:

			var monthstring = (dateVar.getMonth() + 1);
			if(monthstring < 10) monthstring = "0" + monthstring;

			var daystring = dateVar.getDate();
			if(daystring < 10) daystring = "0" + daystring;

			var yearstring = year;
			if(yearstring < 10) yearstring  = "0" + yearstring;

			return monthstring + "/" + daystring + "/" + yearstring;
			break;

	}

}

function formatTimeAsMilitary(time){
	
	var i;
	var segments;	// Break up of the time into hours and minutes
	var hour;	// The value of the entered hour
	var minute;	// The value of the entered minute
	var ampm;        // The value of AM/PM
	
	time = strip('.',time);

	time = time.replace(".", ":");
	time = strip(' ',time);
	segments = time.split(":");

	// Strip out AM/PM.
	ampm = segments[1].substring(2, segments[1].length); // Note AM/PM must have a space between it and the time				

	// strip out minutes
	segments[1] = segments[1].substring(0,2);

	hour = segments[0]; 
	minute = segments[1];			

	theTime = hour + minute;
	theTime = theTime - 0;

	if((ampm =="am") || (ampm=="AM")) ampm = "a.m."
	if((ampm =="pm") || (ampm=="PM")) ampm = "p.m."
			
	// Check For PM
	if ((ampm != "") && ((ampm == "a.m.") || (ampm == "p.m."))) {
		theTime = theTime += 1200;
	}

	//alert(theTime);

	return theTime;
	
}

function convertToTime(temptime){

	// convert to time

}

function convertToDate(tempdate){

	tempdate = tempdate.split("/");
	tempdate[2] = "20" + tempdate[2]
	//tempdate[1] = tempdate[1] - 1;
	tempdate = tempdate[0] + "/" + tempdate[1] + "/" + tempdate[2];

	//var thedate = new Date(tempdate[2],tempdate[0],tempdate[1])
	var thedate = new Date(tempdate);

	return thedate

}


//End Formatting Functions


// SEARCH FOR A CHARACTER
// sub is character to look for
// string is the data to search through

function returnStringCount(sub, string) {
 var theCount = string.indexOf(sub,0) + 1;
 return theCount;
}

