/*
<!---
****************************************************************
//* Author: Cathline Bridges
//* Date Created: May 15, 2008
//*
//* Purpose: This file is a part of the CO ASO Phoenix application.
//*
//* Modify Date    Author                 Reason
//* ----------      ------                 -------------------------------
//* July 1, 2008       Cathline Bridges       Contract transfer
//
//****************************************************************
// All material © 2008 Colorado Access. All rights reserved. 
//****************************************************************
---> 

*/

function characterFormat(type,string) {
if (type == "phone") {
	if (string.length == 10) {
		string = string.substring(0,3) + "-" + string.substring(3,6) + "-" + string.substring(6,10);
		}
	}
	
if (type == "date") {
	if (string.length == 8) {
		string = string.substring(0,2) + "/" + string.substring(2,4) + "/" + string.substring(4,8);
		}
	}
	
if (type == "SSN") {
	if (string.length == 9) {
		string = string.substring(0,3) + "-" + string.substring(3,5) + "-" + string.substring(5,9);
		}
	}
	
if (type == "TIN") {
	if (string.length == 9) {
		string = string.substring(0,2) + "-" + string.substring(2,9);
		}
	}
	
if (type == "routing") {
	if (string.length == 8) {
		string = string.substring(0,4) + "-" + string.substring(4,8);
		}
	}
if (type == "zipcode") {
	if (string.length == 9) {
		string = string.substring(0,5) + "-" + string.substring(5,9);
		}
	}
	
return string;
}

function specialCharacter(string) {
var charMatch = 0;

var specialChar = new Array();
specialChar[0] = "/";
specialChar[1] = "\\";
specialChar[2] = "-";
specialChar[3] = "&";
specialChar[4] = "'";
specialChar[5] = "!";
specialChar[6] = ",";

for (a=0; a < specialChar.length; a++) {
	if (string == specialChar[a]) {
		charMatch = charMatch + 1;
		}
	}
	
if (charMatch == 0) {
	return false;
	}
	
}

function validateCharacter(type,string) {

var numRE = /[\d]/;
var alphaRE = /[ a-zA-Z]/;
var alphaNumRE = /^[ 0-9a-zA-Z\/\\\&\-\,\'\!]$/;
var emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var deaRE = /\w{2}\d{7}/;
var urlRE = /^[0-9a-zA-Z]+([\!\.-\:\?\=\&\#\+\%]*[0-9a-zA-Z])+([\!\.-\:\?\=\&\#\+\%]*[0-9a-zA-Z])+$/;
var zipcodeRE = /^(\d{9}|\d{5}(-\d{4})?)$/;
var floatRE = /^\d+(.\d{2})?$/;

if (string.length > 0) {
	for (a=0; a < string.length; a++) {
		if (type == "alpha") {
			if (alphaRE.test(string.charAt(a)) == false) {
				return false;
				}
			}
		
		if (type == "alphanumeric") {
			if ((alphaNumRE.test(string.charAt(a)) == false)) {
				return false;
				}
			}
			
		if (type == "numeric") {
			if (numRE.test(string.charAt(a)) == false) {
				return false;
				}
			}
		}// end for
		
	if (type == "float") {
		if (floatRE.test(string) == false) {
			return false;
			}
		}
	if (type == "email") {
		if (emailRE.test(string) == false) {
			return false;
			}
		}
	if (type == "DEA") {
		if (deaRE.test(string) == false) {
			return false;
			}
		}
	if (type == "URL") {
		if (urlRE.test(string) == false) {
			return false;
			}
		}
	if (type == "zipcode") {
		if (zipcodeRE.test(string) == false) {
			return false;
			}
		}
		
	}// end if
}

function fieldFormat(type,string,stringName) {
if (type != "number") {
	if (string.search("-") > 0) {
		string = string.split("-").join("");
		}
	if (string.search("/") > 0) {
		string = string.split("/").join("");
		}
	}

var error = 0;

if (type == "phone") {
	if ((string.length != 10 && string.length != 0) || (validateCharacter("numeric",string) == false)) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows a numeric entry format only - you must enter a phone number in XXXXXXXXXX format.";
		}
	}
else if (type == "date") {
	if ((string.length != 8 && string.length != 0) || (validateCharacter("numeric",string) == false) ||
		(isDate(string,stringName) == false)) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows a numeric entry format only - you must enter a " + type + " in XXXXXXXX format.";
		}
	}
else if (type == "routing") {
	if ((string.length != 8 && string.length != 0) || (validateCharacter("numeric",string) == false)) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows a numeric entry format only - you must enter a Routing Number in XXXXXXXX format.";
		}
	}
else if ((type == "SSN") || (type == "TIN")) {
	if ((string.length != 9 && string.length != 0) || (validateCharacter("numeric",string) == false)) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows a numeric entry format only - you must enter a " + type + " in XXXXXXXXX format.";
		}
	}
else if (type == "email") {
	if (validateCharacter("email",string) == false) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows email format only - you must enter a email in address@url.ext format.";
		}
	}
else if (type == "DEA") {
	if (validateCharacter("DEA",string) == false) {
		error = error + 1;
		errorMsg = "The DEA Number field allows alphanumeric format only - you must enter a DEA Number in XX0000000 format.";
		}
	}
else if (type == "URL") {
	if (validateCharacter("URL",string) == false) {
		error = error + 1;
		errorMsg = "The internet address field is not in the correct format.";
				//"The internet address field allows alphanumeric and the following characters only." + 
				//"\n\t colon (:)\t\t period (.)\t\t question mark (?)\n\t dash (-) \t\t equal (=)\t\t ampersand (&)\n\t pound (#)";
		}
	}
else if (type == "zipcode") {
	if ((string.length != 5 && string.length != 9 && string.length != 0) || (validateCharacter("zipcode",string) == false)) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows numeric format only - you must enter a zip code in XXXXX or XXXXXXXXX format.";
		}
	}
else if (type == "float") {
	if (validateCharacter("float",string) == false) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows numeric format and/or a decimal only - you must enter " + stringName + " in X.XX or XX format.";
		}
	}
else if (type == "number") {
	if (validateCharacter("numeric",string) == false) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows numeric characters only.";
		}
	}
else {
	if (validateCharacter(type,string) == false) {
		error = error + 1;
		errorMsg = "The " + stringName + " field allows " + type + " characters only.";
		}
	}


if (error > 0) {
	alert(errorMsg);
	return false;
	}
	
}
/*
String.prototype.trim = function()
{
  return( this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );   
}
*/