/*******************************************************************************************
file name : rmtClient.js
** Provide functions to validate fields before submit
*******************************************************************************************/

// CHECK STRING - ENSURE ALL CHARACTERS ARE ALPHANUMERIC AND ADD ESCAPE CHARACTERS FOR ANY QUOTE MARKS 
function toUnquotedAlphanumeric(checkString)
{
	var newString = "";    // REVISED/CORRECTED STRING 
	var count = 0;         // COUNTER FOR LOOPING THROUGH STRING 

	// LOOP THROUGH STRING CHARACTER BY CHARACTER 
	for (var i = 0; i < checkString.length; i++) {
		var ch = checkString.substring(i, i+1);

		// Do not allow A QUOTE MARK 
		// Do not Allow Carriage Returns, Newlines and Tabs
		// IF THE CHARACTER IS A QUOTE MARK, DON'T INCLUDE IT IN THE REVISED STRING  
		if ((ch != "\"" && ch != "'" && ch != "\n" && ch != "\r" && ch != "\t")) {
			newString += ch; 
		}
		
	}

	if (checkString != newString) {
//		alert("The value you entered contained invalid characters.\nPlease try to avoid using non-alphanumeric characters.\n") 
		// RETURN REVISED STRING 
		return newString;
		} else {
		// RETURN ORIGINAL STRING 
		return checkString;
	}
}

/*******************************************************************************************
Function to validate the Route Mamt Tool Form
1) Need to enter at least one parameter
2) Some field has Max Characters Limit
return false if input parameter does not meet requirement
*******************************************************************************************/

function validateRmt(form) {
	var text;
	var numInput = 0;
	var errors = "";
	
	
// Loop through the elements of the form, looking for all text that has max field, if have max, check each segement that is not exceed the max
	//alert("validateRmt form");

	for(var i = 0; i < form.length; i++) {
		var e = form.elements[i];
		if(e.type == "text"){
			var fieldLen = e.value.length;
			if ( fieldLen > 0) {
				numInput++;						
			
				if (e.max != null) {
				//alert("Verifying element" + e.value + "!! + Max Len=" + e.max);
					if(!checkLen(e.value, e.max)){
						errors += "- The " + e.title + " field contains more than the allowed maximum of " + e.max + " characters for each query field.\n";

					}
				}
				
			} // end if ( fieldLen > 0)
		}

	} // end for


	// if no error, loop through all text textarea and fields convert the Apostrophe to two Apostrophe
	if (numInput > 0 && !errors){
		
		return true;
		
	} else{
		
		if (numInput==0) {
			text  = "___________________________________________________________________________\n\n";
			text += " 			 Invalid Request\n";
			text += " Due to the size of the Database, you are not allowed to query the entire database.\n";
			text += " You can however, download the entire database with the 'Download Database' button.\n";
			text += "___________________________________________________________________________\n\n";
			
		} else if (errors) {
			text  = "___________________________________________________________________________\n\n";
			text += "The form has not been submitted because of the following error(s).\n";
			text += "Please correct these error(s) and re-submit the form.\n"   
			text += "___________________________________________________________________________\n\n";

			text += errors;

		}

		alert(text);
		return false;
	}
}

/*******************************************************************************************
Function to break the String base on the '&', '|', '!'
if the String greater then input parameter length (nLen) return false, else return true
*******************************************************************************************/

function checkLen(strText, nLen){
	var text = "";
	// LOOP THROUGH STRING CHARACTER BY CHARACTER 
	for (var i = 0; i < strText.length; i++) {
		var ch = strText.substring(i, i+1);
		
		// SEE IF THE CHARACTER IS AN ALPHANUMERIC CHARACTER BUT NOT A QUOTE MARK 
		// Allow Carriage Returns, Newlines and Tabs
		if ((ch == "&") || (ch == "|") || (ch == "!")) {
			// Check length
			//alert("Verifying element[" + text + "] length =" + text.length);
			if(text.length > nLen) return false;
			else {
				
				text = "";
			}		
		} else {
			if (ch != " ") text += ch;
		}
		
	} // end for
	
	// Check the last one
	//alert("Verifying element[" + text + "] length =" + text.length);
	if(text.length > nLen) return false;
	
	return true;
	
	
}