/**
 * 	@author Aravintha Krishnan Jagannathan
 * 	@since Moveo 1.0
 * 	Data Validation
 */
 
 /*	global variables */



 /* function - to validate the datatype of all the elements in a page
	input - any object in which the element's datatype to be validated and alert flag if required
			if alert flag is 'true' - displays consolidated alert messages in an alert
			if alert flag is 'false' - returns consolidated alert messages as a string
			if alert flag is other than 'true' or 'false' or even if not mentioned 
			- displays first alert message in an alert and cursor focus to that element

	output - returns true or false or message string, if any
 */
  	function validateDatatype(object, alertFlag) {
		var displayMessage = EMPTY_STRING;
     	setFocusElement(NULL_VALUE);
		alertFlag = checkFlag(alertFlag);
	
 		if (isDocument(object)) {
 			displayMessage = validateDatatypeAllFormElements(alertFlag);
 		}
 		else if (isForm(object)) {
 			displayMessage = validateDatatypeFormElements(object, alertFlag);
 		}
 		else {
 			displayMessage = validateDatatypeElements(object);
 		}

		if (checkAlert(alertFlag, displayMessage) || checkAllAlert(alertFlag, displayMessage)) {
			displayMessage =  DATATYPE_DISPLAY_MESSAGE + displayMessage;
			object = getFocusElement();
 			alertMessage(displayMessage,object);
	 		return FALSE_FLAG;
 		}
 		else if (checkNoAlert(alertFlag, displayMessage)) {
	 		return displayMessage;
 		}
 		return TRUE_FLAG;
 	}


 /* function - to validate the datatype of all the elements in all forms in a page
	input - alert flag string
	output - returns message string, if any or an empty string
 */
	function validateDatatypeAllFormElements(alertFlag) {
	
	 	var displayMessage = EMPTY_STRING;
		var formObjects = document.forms;

		alertFlag = checkFlag(alertFlag);

		for(var i=0; i < formObjects.length; i++) {
			displayMessage += validateDatatypeFormElements(formObjects[i], alertFlag);

			if(checkAlert(alertFlag, displayMessage)) {
				break;
			}
		}
		
		return displayMessage;
	}


 /* function - to validate the datatype of all the elements in a form
	input - gets a form object and alert flag string
	output - returns message string, if any or an empty string
 */
 	function validateDatatypeFormElements(formObject, alertFlag) {
	 	var displayMessage = EMPTY_STRING;
	 	checkboxName=EMPTY_STRING;
	 	radioName=EMPTY_STRING;

		alertFlag = checkFlag(alertFlag);
	 	for(var formLength=0; formLength < formObject.elements.length; formLength++) {
			var elementObject = formObject.elements[formLength];
			displayMessage += validateDatatypeElements(elementObject);
			if(checkAlert(alertFlag, displayMessage)) {
				break;
			}
		} 	
		return displayMessage;
	}

 
 /* function - to validate the datatypes of the element
		 	   element may have an attribute, "datatype" with a value (as mentiond in switch-case) for datatype validation
			   element may have an attribute, "caption" with a value which is to be displayed on the alert box 
			   "datatype" and "caption" attributes are optional for fields not involved in datatype validation
	input - gets an element object
	output - returns message string
 */
	function validateDatatypeElements(elementObject) {
		var displayMessage = EMPTY_STRING;

 		if(elementObject && !isEmpty(elementObject.getAttribute("datatype")) && !isEmpty(elementObject.value) && (elementObject.getAttribute("disabled") != TRUE_STRING && elementObject.getAttribute("disabled") != 'disabled')) {
 			var elementDatatype = elementObject.datatype;
 			var caption = getCaption(elementObject);
 			
 			switch(elementDatatype) {
				case "string":
					if (!isString(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only alphanumeric characters along with " + ALLOWED_STRING + NEW_LINE_FEED;
					break;
				case "alphabet":
					if (!isAlphabet(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only alphabet characters" + NEW_LINE_FEED;
					break;
				case "alpha_numeric":
					if (!isAlphaNumeric(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only alphanumeric characters" + NEW_LINE_FEED;
					break;
				case "numeric":
					if (!isNumeric(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
					break;
				case "positive_integer":
					if (!isPositiveInteger(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
					break;
				case "negative_integer":
					if (!isNegativeInteger(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
					break;
				case "integer":
					if (!isInteger(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
					break;
				case "positive_float":
					if (!isPositiveFloat(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
					break;
				case "negative_float":
					if (!isNegativeFloat(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
					break;
				case "float":
					if(!isEmpty(elementObject.getAttribute("dataformat"))) {
						var decimalPlaces = formObject.elements[formLength].dataformat;
						if (!isFloat(elementObject,decimalPlaces))
							displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
						break;
					}
					else if (!isFloat(elementObject))
						displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts only numeric values" + NEW_LINE_FEED;
					break;
				case "date":
					if (!isDate(elementObject))
						displayMessage += formatString(caption);
					break;
				case "email_id":
					if (!isValidateMailId(elementObject))
					displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "is not in a correct format.<br>ex:- yourcomments@csia.in" + NEW_LINE_FEED;
					break;
				
				case "email_id_type":
					if (!isValidateMailIdType(elementObject))
					displayMessage += formatString(caption);
					break;
					
				case "phone_number":
					if (!isValidatePhoneNumber(elementObject))
					displayMessage += "<br/><b>" + caption + "</b>" + SPACE_STRING + HYPHEN + SPACE_STRING + "accepts a minimum of 7 digits upto to a maximum of 14 digits." + NEW_LINE_FEED + "Leading zeros will be ignored, if any." + NEW_LINE_FEED;
					break;
				default:
			}
		}
	
		if(isNull(getFocusElement()) && !isEmpty(displayMessage)) {
			setFocusElement(elementObject);
		}
		return displayMessage;
	}


 /* function - to validate Alpha-Numeric value
 	input - gets an element Object
 	output - returns true or false
 */
	function isAlphaNumeric(elementObject) {
		var alphaNumericValue=elementObject.value;
		return matchString(alphaNumericValue,ISALPHANUMERIC_REG_EXPR);
	}
	

 /* function - to validate Numeric value
 	input - gets an element Object
 	output - returns true or false
 */
	function isNumeric(elementObject) {
		var numericValue=trimSpace(elementObject.value);
		if(numericValue.indexOf("+") < 1 && numericValue.indexOf("-") < 1) {
			numericValue = 	removeExtraChars(numericValue);
			if(numericValue.indexOf(SPACE_STRING) != -1) {
				numericValue = numericValue.replace(ALL_SPACE_REG_EXPR,EMPTY_STRING);
			}
			if(!isNaN(numericValue)) {
				return TRUE_FLAG;
			}
		}
		return FALSE_FLAG;
	}


 /* function - to validate Integer value
 	input - gets an element Object
 	output - returns true or false
 */
	function isInteger(elementObject) {
		var integerValue=elementObject.value;
		if (isNaN(integerValue) || integerValue.indexOf(".") != -1) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}
	

 /* function - to validate Positive Integer value
 	input - gets an element Object
 	output - returns true or false
 */
	function isPositiveInteger(elementObject) {
		var positiveIntegerValue=elementObject.value;
		if (!isInteger(elementObject) || positiveIntegerValue < 0) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}
	

 /* function - to validate Negative Integer value
 	input - gets an element Object
 	output - returns true or false
 */
	function isNegativeInteger(elementObject) {
		var negativeIntegerValue=elementObject.value;
		if (!isInteger(elementObject) || negativeIntegerValue >= 0) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}
	

 /* function - to validate Positive Float value
 	input - gets an element Object
 	output - returns true or false
 */
	function isPositiveFloat(elementObject) {
		var positiveFloatValue=elementObject.value;
		if (!isFloat(elementObject) || positiveFloatValue < 0) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}
	

 /* function - to validate Negative Float value
 	input - gets an element Object
 	output - returns true or false
 */
	function isNegativeFloat(elementObject) {
		var negativeFloatValue=elementObject.value;
		if (!isFloat(elementObject) || negativeFloatValue >= 0) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}
	

 /* function - to validate Float value
 	input - gets an element Object
 	output - returns true or false
 */
	function isFloat(elementObject, decimalPlaces) {
		var floatValue=elementObject.value;
		if (decimalPlaces == UNDEFINED_STRING || isNaN(decimalPlaces)) {
			decimalPlaces = 0;
		}
		if (isNaN(floatValue) || !isValidDecimalFormat(elementObject, decimalPlaces)) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}
	
 
 /* function - to validate Float value with valid decimal places
 	input - gets an element Object
 	output - returns true or false
 */
	function isValidDecimalFormat(elementObject, decimalPlaces) {
		var elementValue=elementObject.value;
		if(decimalPlaces > 0) {
			var formatDecimal = "/[\.]{1}[0-9]{" + decimalPlaces + "}$/";
			if (elementValue.search(formatDecimal) != -1) {
				return FALSE_FLAG;
			}
		}
		return TRUE_FLAG;
	}


 /* function - to validate Date
 	input - gets an element Object
 	output - returns true or false
 */
	function isDate(elementObject) {
		var dateValue=elementObject.value;
		if (FALSE_FLAG) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}

 
 /* function - to validate e-mail id
 	input - gets an element Object
 	output - returns true or false
 */
	function isValidateMailId(elementObject) {
  		var mailId = elementObject.value;
  		if(mailId == EMPTY_STRING) {
			return FALSE_FLAG;
		}
		
		if(isNotMailChar(mailId)) {
			return FALSE_FLAG;
		}

		if(!isFirstCharValid(mailId)){
			return FALSE_FLAG;
		}
		
		if(!isDomainValid(mailId)){
			return FALSE_FLAG;
		}
		
		return TRUE_FLAG;
	 	//return validateRegularExpression(elementObject,ISEMAILID_REG_EXPR);
	}
 
 /* function - to validate e-mail id type
 	input - gets an element Object
 	output - returns true or false
 */
	function isValidateMailIdType(elementObject) {
  		var mailId = elementObject.value;
  		if(mailId == EMPTY_STRING) {
			return FALSE_FLAG;
		}
		
		if(isNotMailChar(mailId)) {
			return FALSE_FLAG;
		}

		if(!isFirstCharValid(mailId)){
			return FALSE_FLAG;
		}
		
		return TRUE_FLAG;
	 	//return validateRegularExpression(elementObject,ISEMAILID_REG_EXPR);
	}
 
 
/* 	function - to validate any special characters found in the e-mail id other than @, ., -, _
 	input - gets an e-mail id string
 	output - returns true or false
 */
  	 function isNotMailChar(mailId) {
		if(mailId.match(ISMAILID_REG_EXPR)) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}


/* 	function - to validate the first character of the e-mail id
 	input - gets an e-mail id string
 	output - returns true or false
 */
 	function isFirstCharValid(mailId) {
		var mailIdStartsWithAT = mailId.indexOf(CONSTANT_AT);
		if(mailIdStartsWithAT == 0) {
			return FALSE_FLAG;
		}
		return TRUE_FLAG;
	}


/* 	function - to validate the domain of the e-mail id
 	input - gets an e-mail id string
 	output - returns true or false
 */
 	function isDomainValid(mailId) {
		var mailIdATIndex = mailId.indexOf(CONSTANT_AT);
		if(mailIdATIndex == -1) {
			return FALSE_FLAG;
		}
		
		var mailIdAfterAT = mailId.substring(mailIdATIndex + 1);
		var mailIdAfterATIndex = mailIdAfterAT.indexOf(CONSTANT_AT);
		if(mailIdAfterATIndex != -1) {
			return FALSE_FLAG;
		}

		var mailIdStartsWithDot = mailIdAfterAT.indexOf(CONSTANT_DOT);
		var mailIdEndsWithDot = mailIdAfterAT.lastIndexOf(CONSTANT_DOT) - (mailIdAfterAT.length - 1);
		var mailIdWithDot = mailIdAfterAT.indexOf(CONSTANT_DOT);
		var mailIdWithDotDot = mailIdAfterAT.indexOf(CONSTANT_DOT+CONSTANT_DOT);

		if((mailIdStartsWithDot == 0) || (mailIdEndsWithDot == 0) || (mailIdWithDot == -1) || (mailIdWithDotDot != -1)) {
			return FALSE_FLAG;
		}
		
		return TRUE_FLAG;
	}
 
 
 /* function - to validate mobile number
 	input - gets an element Object
 	output - returns true or false
 */
  	function isValidatePhoneNumber(elementObject) {
  		var phoneNo = elementObject.value;
		phoneNo=trimSpace(phoneNo);
		phoneNo=removeZerosInStarting(phoneNo);
  		phoneNo=removeExtraChars(phoneNo);
	
		if(isNaN(phoneNo)) {
			return FALSE_FLAG;
		}

		var length=phoneNo.length;
		switch(length){
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
				return TRUE_FLAG;
		}
		return FALSE_FLAG;
	 	//return validateRegularExpression(elementObject,ISMOBILE_NUMBER_REG_EXPR);
  	}
 
 
 /* function - to remove extra chars in the mobile number string
 	input - gets a string value
 	output - returns a string value
 */
  	function removeExtraChars(phoneNo) {
		phoneNo = phoneNo.replace(INGNORABLE_CHARS_REG_EXPR,EMPTY_STRING);
		return phoneNo;
 	}
 
 
 /* function - to remove all the trailing zeros in a string
 	input - gets a string value
 	output - returns a string value
 */
  	function removeZerosInStarting(phoneNo) {
		while(phoneNo.indexOf(ZERO) == 0){
			phoneNo=phoneNo.substring(1);
		}
		return phoneNo;
 	}
 
 
 /* function - to validate regular expression
 	input - gets an element object and a regular expression
 	output - returns true or false
 */
 	function validateRegularExpression(elementObject, regularExpression) {
	 	var elementValue = elementObject.value;
	 	var elementValues = elementValue.split(SEPARATOR);
	 	for(var valLength=0;valLength<elementValues.length;valLength++) {
	 		if(!matchString(elementValues[valLength],regularExpression) && !isEmpty(trimSpace(elementValues[valLength]))) {
	 			return FALSE_FLAG;
	 		}
	 	}
	 	return TRUE_FLAG;
 	}
 