var debug = false;

var inputAlphaString = new Array("AlphaString", "USAorNot", "AMPM", "TimeUnknown", "ManageSubscription", 
								 "Daily", "Weekly", "Tarot", "Optin", "curr", "ByPassCap", "Lang", "isFlashOK", 
								 "PopupEmail", "page", "genre", "gender", "MeOrNotMe", "Rs"); 
var inputAlphaNumerical = new Array("AlphaNumerical", "RECALL", "promotionCode", "PromoCode", "CP", "DE", "Advert", "ZipCode", "ConsultationID",
								    "debug", "param", "AVS", "prefixe", "QS", "type", "freg", "tirage", "CL", "EmailStatus", "Go", "rating",
								    "product", "osadcampaign", "SiteName", "ticks", "utm_source", "utm_medium", "utm_content", "refresh", "UserID",
								    "noredir", "adcode", "action", "result", "company", "position", "color", "subject", "problem", "AstrologerID",
								    "emailOption", "Qluser", "clean", "QMMessage", "QMProduct", "Field", "Submit", "sign", "fab_ApplyReduction"); 
var inputAlphaAstroCrypt = new Array("AlphaAstroCrypt", "IDUtilisateur", "CRC", "CR1", "CommandID", "Cm", "c", "c0", "c1", "c2", "Xa", 
									 "IDCommande", "PID", "thedate", "D", "I", "Xtra", "uid", "Z");
var inputAlphaCCNumber = new Array("AlphaCCNumber", "CardNumber");
var inputAlphaCVV2  = new Array("AlphaCVV2", "CVV2");
var inputAlphaDateFormat = new Array("AlphaDateFormat", "date");
var inputAlphaTimeFormat = new Array("AlphaTimeFormat", "time");
var inputAlphaEmail = new Array("AlphaEmail", "email", "FNDest");
var inputAlphaLocation = new Array("AlphaLocation", "city", "state", "country", "pays");
var inputAlphaNames = new Array("AlphaNames", "name", "prenom");
var inputAlphaPassword = new Array("AlphaPassword", "passwd", "password"); 
var inputAlphaQuestion = new Array("AlphaQuestion", "Question", "search", "UserPhoneID", "comment");
var inputAlphaURL = new Array("AlphaURL", "URL"); 
var inputNumericalFloat = new Array("NumericalFloat", "price", "prc", "rand");
var inputNumericalInt = new Array("NumericalInt", "when", "DomaineTarot", "year", "month", "day", "birthday", 
								  "af", "hour", "minute", "carte", "ModeHtml", "Formule", "Q1", "Q2", "Q3", "Q4", 
								  "Q5", "Q6", "Q7", "Q8", "Q9", "Delta", "Prov", "OrderNumber", "mins", "Etat", "remlen",
								  "index", "dw", "w", "ad", "AU", "order_num", "phone_id", "t1", "nbjours", "sql", 
								  "id1", "id2", "id3", "x", "y");
var inputAlphaAddress = new Array("AlphaAddress", "address");

var dateTypeList = new Array(inputAlphaString, inputAlphaNumerical, inputAlphaNames,  
							 inputAlphaCCNumber, inputAlphaCVV2, inputAlphaDateFormat, 
							 inputAlphaTimeFormat, inputAlphaEmail, inputAlphaLocation,
							 inputAlphaPassword, inputAlphaQuestion, inputNumericalInt, 
							 inputAlphaURL, inputNumericalFloat, inputAlphaAstroCrypt, inputAlphaAddress); 
							 
var dateTypeListRegex = new Array("AlphaString", "AlphaNumerical", "AlphaNames",  
								  "AlphaCCNumber", "AlphaCVV2", "AlphaDateFormat", 
							      "AlphaTimeFormat", "AlphaEmailFormat", "AlphaLocation",
							      "AlphaPassword", "AlphaQuestion", "NumericalInt",
							      "AlphaURL", "NumericalFloat", "AlphaAstroCrypt", "AlphaAddress"); 							 

var defaultLenMin = 1;
var defaultLenMax =  -1;
var defaultIsRequired = false; 

function writeDebugMessage(aMessage) {
	if (debug)
		alert(aMessage);	
}

function setDefaultValue(aType, aParam) {
	switch(aType) {
		case "AlphaDateFormat":
			defaultLenMax = 10;
			break;
		case "AlphaTimeFormat":
			defaultLenMax = 14;
			break;
		case "NumericalInt":
			if (aParam.toLowerCase().match("day") != null || 
				aParam.toLowerCase().match("month") != null || 
				aParam.toLowerCase().match("hour") != null || 
				aParam.toLowerCase().match("minute") != null) {
				defaultLenMin = 1;
				defaultLenMax = 2;
			} else {
				defaultLenMax = 20;
			}
			break;
		case "AlphaAstroCrypt":
			defaultLenMax = 20;
			break;
		case "AlphaCCNumber":
			defaultLenMin = 10;
			defaultLenMax = 20;
			break;
		case "AlphaLocation":
		case "NumericalFloat":
		case "AlphaEmailFormat":
		case "AlphaNames":
			defaultLenMax = 40;
			break;
		case "AlphaURL":
		case "AlphaNumerical":
		case "AlphaQuestion":
		case "AlphaString":
		case "AlphaAddress":
			if (aParam.toLowerCase().match("zipcode") != null) {
				defaultLenMin = 0;
				defaultLenMax = 6;
			} else {
				defaultLenMax = 500;
			}
			break;
		case "AlphaPassword":
			//defaultLenMin = 6;
			defaultLenMax = 30;
			break;
		case "AlphaCVV2":
			defaultLenMin = 3;
			defaultLenMax = 4;
			break; 
		default:
			break; //nothing				
	}
}

function getRegex(aType) {
	var regexStr = "";
	switch(aType) {
		case "AlphaDateFormat":
			regexStr = /^[\d]{8}|^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;							//DDDDMMYY | DD-MM-YYYY
			break;
		case "AlphaTimeFormat":
			regexStr = /^\d{1,2}(\:|\.)\d{2}\1\d{2}\s{1}[AP]{1}[M]{1}$|^\d{1,2}(\:|\.)\d{2}$/i;	 //HH:MM:SS AM|PM  - HH:MM
			break;
		case "AlphaEmailFormat":
			regexStr = /^[A-Z0-9_\.\-]+\@(?:[0-9A-Z][A-Z0-9\-]+\.)+[A-Z]{2,4}$/i;				// email format
			break;
		case "AlphaNames":
			regexStr = 	/^[A-Z\'\s\-]+$/i;														//letter, space, quote,	dash
			break;
		case "AlphaString":																						
			regexStr = /^[A-Z]+$/i;																//letters only		
			break;
		case "NumericalInt":																	
			regexStr = /^-*[\d]+$/;																//negative, positive numbers	
			break;
		case "NumericalIntAbs":	
		case "AlphaCVV2":
			regexStr = /^[\d]+$/;																//absolute positive numbers		
			break; 
		case "NumericalFloat":
			regexStr = /^-*[\d]+$|^-*[\d]+\.[\d]+$/;											//decimal or regular numbers
			break;
		case "AlphaURL":
			regexStr = /^((http)s?\:\/{2}){1}[A-Z0-9-_]+\.[A-Z0-9-_%&\?\/.=]+$/i;				//http(s)://xxx.
			break;
		case "AlphaNumerical":
		case "AlphaPassword":
		case "AlphaAstroCrypt":
			regexStr = 	/^[A-Z0-9]+$/i;															//number and letters only	
			break;
		case "AlphaLocation":
			regexStr = /^[A-Z\'\s\-\'\)\(\]\[\,]+$/i;											//letters, space, quote, dash, brackets, parenthesis
			break;
		case "AlphaAddress":
			regexStr = /^[A-Z0-9\'\s\-.#\/\)\(\]\[\,]+$/i;										//letters, space, quote, dash, brackets, parenthesis
			break;	
		case "AlphaCCNumber":
			regexStr = /^[\d ]+$/;																//number and space only			
			break;
		case "AlphaQuestion":
			regexStr = /./;
			break;
		default:
			break; //nothing				
	}
	return regexStr;
}

function testing() {
	var str = "Birthminute";
	var aVal = "10";
	if (isValidated(str, aVal)) {
		writeDebugMessage("valid");
	} else {
		writeDebugMessage("invalid");
	}
}

function getDataTypeIndex(aField) {
	for(var i = 0; i < dateTypeList.length; i++){
		if (arrayPosition(dateTypeList[i], aField) > -1) {	
			writeDebugMessage("Match: " + dateTypeList[i][arrayPosition(dateTypeList[i], aField)] + "<br>");
			return i;	
		}		
	}
	writeDebugMessage("No Match: " + aField + "<br>");
	return -1;	
}

function arrayPosition(anArray, anItem) {
	for(var i = 0; i < anArray.length; i++){
		if  (anItem.toLowerCase().match(anArray[i].toLowerCase()) != null) {
			//if the form name is shorter than 4, then make sure the form name match exactly with what in the list.
			if ((anArray[i].length > 3 && anItem.length > 3)  || anItem.length == anArray[i].length)  
				return i;
		}				
	}
	return -1; 
}	
   
function isValidated(aField, aValue){
	//if (aField == "AlphaAddress") debug = true;

	aValue = aValue + "";
	writeDebugMessage("Form Name: " + aField + "---> value : " + aValue + " length: " + aValue.length +"<br>");
	/*if (!Application("JScriptErrorCheck")){
		if (aValue.length > 0 && aValue != "undefined")	
			return true;
		else
			return false;
	}*/
	
	if (aValue.length <= 0 || aValue == "undefined")	
			return false;
	
	var dataTypeIndex = getDataTypeIndex(aField);
	if (dataTypeIndex < 0) {
		return false;
	} else if (aValue == "undefined") {
		return false;	
	} else {	
		setDefaultValue(dateTypeListRegex[dataTypeIndex], aField);
		return isValidatedExtended(aField, aValue, defaultLenMin, defaultLenMax, defaultIsRequired);
	}		
}

function isValidatedExtended(aField, aValue, aLenMin, aLenMax, isRequired){
	aValue = aValue + "";
	var dataTypeIndex = getDataTypeIndex(aField);
	if (dataTypeIndex < 0) {   //check to see if field is in the list
		writeDebugMessage(aField + " is not in the list<br>");
		return false;
	} else if (isRequired && (aValue.length <= 0 || aValue == "undefined")) {			  //check make sure the input value is not empty if required	
		writeDebugMessage("required and empty<br>");
		return false;
	} else if (aLenMin > 0 && aValue.length < aLenMin) {	  //check make sure the minimum length is satisfied if specified	
		writeDebugMessage("min length problem: " + aValue + " shorter than " + aLenMin + "<br>");
		return false;	
	} else if (aLenMax > 0 && aValue.length > aLenMax) {	  //check make sure the maximum length is satisfied if specified
		writeDebugMessage("max length problem: " + aValue + " longer than " + aLenMax + "<br>");
		return false;		
	} else {
		writeDebugMessage(dateTypeListRegex[dataTypeIndex]+" regex: ");														
		return validateInput(aValue, getRegex(dateTypeListRegex[dataTypeIndex]));
	}	
		
}

function checkFieldJS(aField, aValue, message) {
	if (isValidated(aField, aValue)) 
		return "&nbsp;";
	else 
		return message;
}

function validateInput(aValue, aRegex) {
	writeDebugMessage(aRegex + "<br>");
	writeDebugMessage("Regex test: " + aRegex.test(aValue) + "<br><br>");
	
	if(!aRegex.test(aValue))
		return false;	
	else
		return true;	
}

function validateInputV2(aValue, aDataTypeIndex) {
	var aRegex
	if (-1 == aDataTypeIndex) 
		aRegex = eval(Application("AstroInputRegexFailSafe"));
	else
		aRegex = getRegexV2(aDataTypeIndex);
	writeDebugMessage(aRegex + "<br>");
	writeDebugMessage("Regex test: " + aRegex.test(aValue) + "<br><br>");
	
	if(!aRegex.test(aValue))
		return false;	
	else
		return true;	
}

function validateInputV3(aValue, aRegex) {
	aRegex = eval(aRegex)
	writeDebugMessage(aRegex + "<br>");
	writeDebugMessage("Regex test: " + aRegex.test(aValue) + "<br><br>");
	
	if(!aRegex.test(aValue))
		return false;	
	else
		return true;	
}


function getRegexV2(aDataTypeIndex) {
	return getRegex(dateTypeListRegex[aDataTypeIndex]);
}

//testing();
