
function validate_form(formID, fieldStr){
//	alert("Validating Form: " + formID)
	form_valid = true

	if(fieldStr.indexOf(";")){
		fieldArray = fieldStr.split(";")
	}else if(fieldStr.length>0){
		fieldArray = new Array()
		fieldArray[0] = fieldStr
	}
//	alert("Checking Fields: [" +fieldArray.length+ "] " + fieldStr)
	
	if(fieldArray.length>0){
		for(f=0;f<fieldArray.length;f++){
	//	alert("Field ID: " + fieldArray[f] + " Value: " + document.getElementById(fieldArray[f]).value)
			if(document.getElementById(fieldArray[f])){
			
				theField = document.getElementById(fieldArray[f])
				theValue = theField.value
				
				if(theValue.length==0){
					theField.className = "field_invalid"
					form_valid = false	
				}	
			}
		}
	}
	if(form_valid){document.getElementById(formID).submit()}else{alert("Some values entered were either missing or invalid. Please fix the highlighted fields.")}
}

/*************************************************/
/********* VALIDATE AND FORAMT FIELDS ************/
/*************************************************/

function validateField(validateType, fieldID){
	//alert("Validate: " + fieldID + " Type: " + validateType)
	if(validateType=="#"||validateType=="numeric"){validateNumeric(fieldID)} // ALL NUMERIC ONLY FIELDS
}
function validateNumeric(fieldID){
	validField = true
	
	if(document.getElementById(fieldID)){
		
		theField = document.getElementById(fieldID)	
		theFieldValue = theField.value
		for(n=0;n<theFieldValue.length;n++){
	
			characterVal = theFieldValue.substr(n, 1)
			characterVal = characterVal.replace(".", "")
	
			if(characterVal!=parseInt(characterVal)){
				validField = false			
			}
		}
		if(!validField){
			alert("The value entered is not numeric")
			document.getElementById(fieldID).value = ""
			document.getElementById(fieldID).className = "field_invalid"
		}
	}
}
/******************************************************/
/******************************************************/


function formatField(formatType, fieldID){
	var newValue
//	alert("Formating: " + fieldID)
	if(formatType=="#"||formatType=="numeric"){newValue = formatNumeric(document.getElementById(fieldID).value)}
	if(formatType=="$"||formatType=="currency"){newValue = formatCurrency(document.getElementById(fieldID).value)}
	//****************************/
	if(formatType=="%"||formatType=="percentage"){formatPercentage(fieldID)}
	if(formatType=="mm/dd/yyyy"||formatType=="date"){formatDate(fieldID)}
	if(formatType=="phone"){formatPhone(fieldID)}
	
//	alert("Formatted Value: " + newValue)
	if(newValue!=undefined){
		if(document.getElementById(fieldID)){document.getElementById(fieldID).value = newValue}
		if(document.getElementById(fieldID)&&newValue==0){document.getElementById(fieldID).value = ""}
	}
}

function formatNumeric(formatValue){
	validField = true
	var newValue = ""
	
	if(formatValue){
		//FORMATTING A RANDOM VALUE AND RETURNING RESULT
		formatValue = formatValue.toString()
		theLength = formatValue.length
		//alert("Formatting Numeric Value: " + formatValue + " Length: " + theLength)
	
		for(n=0;n<theLength;n++){
			currentChr = formatValue.substr(n,1)
			if(currentChr==parseInt(currentChr)||currentChr=="."){
				newValue += currentChr
			}else if(currentChr!="$"&&currentChr!=","){
				validField = false
			}
		}
		if(!validField){alert("The value entered contained non-numeric characters. They have been automatically removed. Please make sure the data is correct.")}
		
//		alert("Numeric Formatting -- Orig Value: " + formatValue + " New Value: " + newValue)
		formatValue = newValue
		return formatValue
	}
}

function formatCurrency(formatValue){
	validField = true
	var newValue
//	alert("Orig Value: " + formatValue)
	
//	alert("Formatting Currency Value: " + formatValue)
	if(formatValue){
	
		formatValue = formatNumeric(formatValue)
		formatValue = formatValue.toString()
		
		if(formatValue.indexOf(".")>0){
//			alert("Spliting Integer")
			theFieldIntDecArray = formatValue.split(".")
			theFieldInt = theFieldIntDecArray[0]
			theFieldDec = theFieldIntDecArray[1]
		}else{
			theFieldInt = formatValue
			theFieldDec = "00"
		}
//		alert("Orig Value: " + formatValue + " Interger Value: " + theFieldInt + " Decimal Value: " + theFieldDec)
		
//		if(theFieldDec!=parseInt(theFieldDec)){validField = false}
	
		digitCounter = 0
		newValue = "." + theFieldDec
		
		for(c=0;c<=theFieldInt.length;c++){
			currentInt = theFieldInt.substr(theFieldInt.length-c,1) 
			
//			if(currentInt!=parseInt(currentInt)&&currentInt.length>0){
//				validField = false
//			}else{
				if(digitCounter==3&&c!=theFieldInt.length){
					digitCounter = 0
					currentInt = "," + currentInt
				}
				newValue = currentInt + newValue
//			}
			digitCounter = digitCounter + 1
		}
		//******** RESULTS ********//
			
//		if(!validField){
//			alert("The value entered contained non-numeric values. Please use only numeric characters.")
//			newValue = ""
//			return newValue
//		}else{
//			alert("The value entered is valid.")

//			alert("New Value: " + newValue)
			newValue = "$" + newValue	
			return newValue
//		}
	}
}
function formatDate(fieldID){
	
	theDates = new Array()
		errorMsg = false
	theNewVal = ""
	
	if(document.getElementById(fieldID)){
	
		theVal = document.getElementById(fieldID).value
		if(theVal.length>0){
			theVal = theVal.replace(".", "/")
			theVal = theVal.replace(".", "/")
			theVal = theVal.replace("-", "/")
			theVal = theVal.replace("-", "/")

			theDates = theVal.split("/")

			/* DETERMINE IF THE VALUE IS A STRING OF NUMBERS (8 CHARC LONG) */
			if(parseInt(theVal)==theVal&&theVal.length==8){
				for(i=0;i<theVal.length;i++){
						theNewVal = theNewVal + theVal.substr(i, 1)
						if(i==1||i==3){theNewVal = theNewVal + "/"}
				}
				theVal = theNewVal
			}

			/* CHECK THE DAY AND MONTH RANGES */
			if(theDates.length==3){
				if(parseInt(theDates[0])==theDates[0]&&theDates[0]>=1&&theDates[0]<=12){/*Valid*/}else{errorMsg=true}
				if(parseInt(theDates[1])==theDates[1]&&theDates[1]>=1&&theDates[1]<=31){/*Valid*/}else{errorMsg=true}
				if(parseInt(theDates[2])==theDates[2]&&theDates[2]>=1900&&theDates[2]<=3000){/*Valid*/}else{errorMsg=true}
			}else{
				errorMsg = true
			}

			/* DISPLAY ERROR MESSAGE IF APPROPRIATE */
			if(errorMsg){
				alert("The date is not formatted correctly. Please use the format [ mm / dd / yyyy ].")
				document.getElementById(fieldID).value = ""
			}else{
				document.getElementById(fieldID).value = theVal
			}
		}
	}
}
function formatPercentage(fieldID){
	
// 	........ STILL NOT TESTED JUST COMMENTED OUT .......//
//	alert("This script has not been tested for multiple data types...")
	
	if(document.getElementById(fieldID)){
		currentValue = document.getElementById(fieldID).value
		currentValue = currentValue.replace("%", "")
		
		if(currentValue.indexOf(".") >= 0){
			newValue = currentValue
		}else{
			newValue = currentValue/100
		}
		document.getElementById(fieldID).value = newValue + "00 %"
	}
}
function formatPhone(fieldID){

}

//************* DO NOT TOUCH ******************
/*CHANGE STYLE FOR INPUT FIELDS WHEN USING IE*/
IEFocusScript = function() {
	var inputElements = document.getElementsByTagName("INPUT");
	for (var i=0; i<inputElements.length; i++) {
		if(inputElements[i].type!="checkbox"&&inputElements[i].type!="radio"){
			//alert("Input type: " + inputElements[i].type)
			inputElements[i].onfocus=function() {
				this.className+=" IEFocusStyle";
			}
			inputElements[i].onblur=function() {
				this.className=this.className.replace(new RegExp(" IEFocusStyle\\b"), "");
			}
		}else{
			inputElements[i].className = "checkboxRadio"
			//inputElements[i].style.border = 0
			//inputElements[i].style.background = "#ffffff"
		}
	}
	var selectElements = document.getElementsByTagName("SELECT");
	for (var i=0; i<selectElements.length; i++) {
		selectElements[i].onfocus=function() {
			this.className+=" IEFocusSelect";
			//window.status = "ITEM FOCUSED"
		}
		selectElements[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" IEFocusSelect\\b"), "");
			//window.status = "ITEM BLURRED"
		}
	}
	var textareaElements = document.getElementsByTagName("TEXTAREA");
	for (var i=0; i<textareaElements.length; i++) {
		textareaElements[i].onfocus=function() {
			this.className+=" IEFocusTextarea";
			//window.status = "ITEM FOCUSED"
		}
		textareaElements[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" IEFocusTextarea\\b"), "");
			//window.status = "ITEM BLURRED"
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", IEFocusScript);