//GENERIC VALIDATION

//CONSTANTS
var ALPHA 			= 'alpha';
var NUMERIC 		= "numeric";
var ALPHANUMERIC 	= "alphanumeric";
var EMAIL			= "email";

var alertElement	= null;


function ageCheckCheckBox(checkboxId){
	var bool 			= false;
	if($("input#"+ checkboxId).attr("checked") == true){
		bool			= true;
	}
	return bool;
}


// validate checkBox and a inputtext field Group 
function validateCheckBoxGroup(groupCheckboxClass, inputFieldId, otherCheckboxId, errorLabelId){
	var bool 			= true;
	var n 		    	= $("." + groupCheckboxClass + ":checked").length;
	otherChecked	    = $("#" + otherCheckboxId).attr('checked');
	bool        = ((n<1 || (otherChecked && n == 1 )) ? false : true);
	if (bool == false){
		var inputValue = $("input#" + inputFieldId).val()
		bool 		   = (inputValue != null && inputValue !=  "");	
	}
	if (bool == false){
		$("label#"+errorLabelId).attr("class", "error");
		if( otherChecked == true ){
			$("input#" + inputFieldId).focus();
		}
	}
	return bool;
}


//This function required JQUERY
function validateInputField( fieldID, type, required, length){
	
	var bool		= true;
	var field 		= $("input#" +  fieldID);
	var value 		= field.val();
	var isEmpty 	= (value == null || value == "");
		
	//1. Validate if is empty and required 
	if(required && isEmpty) bool = false;
		
	//2. Validate if is not required and is empty
	if(!required && isEmpty) bool = true;
		
	//3. Validate value
	if(!isEmpty && bool){		
		switch(type){
			case ALPHA:
				bool = validatedAlpha(value);				
				break;
				
			case NUMERIC:
				bool = !isNaN(value);
				break;
				
			case ALPHANUMERIC:
				//THIS TYPE ALLOW ANYTHING CHARACTER AS VALUE OF THE FIELD
				break;
			
			case EMAIL:
				bool = isValidEmail(value);
				break;
		}
		
		if(length != null && bool){
			bool = (value.length == length);
		}
	}
	
	//4. Display alert icon if it was configured using setAlertElement.
	if(!bool){
		displayIconAlert(fieldID);
	}
	
	return bool;
}

function validateSelectField( fieldID, required ){
	var bool		= true;
	var field 		= $("#" +  fieldID + " option:selected");
	var value 		= field.val();
	var isEmpty 	= (value == null || value == "");
	
	//1. Validate if is empty and required 
	if(required && isEmpty) bool = false;
		
	//2. Validate if is not required and is empty
	if(!required && isEmpty) bool = true;
	
	//3. Display alert icon if it was configured using setAlertElement.
	if(!bool){
		displayIconAlert(fieldID);
	}	
	
	return bool;
}


function setAlertElement(alertID){
	alertElement = $(alertID);
}

function displayIconAlert(fieldID){
	var id		  = "_genAlertIcon";
	var label 	  = $("label[for=" + fieldID + "]");
	
	/*if(label.children("." + id).length == 0){
		var alertIcon = alertElement.clone(true);
		alertIcon.css("visibility","visible");
		var currentClass = alertIcon.attr("class");
		alertIcon.attr("class", id + " " + currentClass);
		//label.append(alertIcon);
		label.after(alertIcon);
	}
	*/
	
	label.attr("class", "error");
}

function removeIconAlerts(){
	$("._genAlertIcon").remove();
}


function validatedAlpha(string){
	var bool   = true;
	var length = string.length;
	for(var i=0; i<length; i++)	{
	  var character	 = string.charAt(i);
	  var chrCode	 = character.charCodeAt(0);
	  if((chrCode > 64 && chrCode<91) || (chrCode > 96 && chrCode<123)){
	  }else{
		  bool = false;
		  break;
	  }
	}
	return bool;
}

function isValidEmail(str) {
	return (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}
