/**
 * @author eli.geske
 */
 

function clearErrors(){
	$(".errorLabel").addClass("form_label");	
	$("span").removeClass("errorLabel");	
	$("input").removeClass("errorInput");
	$("textarea").removeClass("errorInput");
	$("select").removeClass("errorInput");
	$("table").removeClass("errorTable");
}
/**
 * ERROR FLAG - takes the submitted id name and changes the css to bold red for correcting.
 * @param {Object} $id
 */
function errorFlag($name){
	$id = "#"+$name;
	$($id).addClass("errorLabel");
	$($id).removeClass("form_label");
	if($name == "TypeOfLoss" || 
	   $name == "CoveredLocationState" || 
	   $name == "IsHurricaneClaim" || 
	   $name == "IsPropertyInhabitable" || 
	   $name == "State" ||
	   $name == "YearEstablished" ){
			$('select[name='+$name+']').addClass("errorInput");		
	}else{
		$('input[name='+$name+']').addClass("errorInput");
		$('textarea[name='+$name+']').addClass("errorInput");
	}
	
	return true;
}

/**
 * TEXT AREA COUNT - gets the value of the given named text area and returns count to id="chCount"
 * @param {Object} $name
 * @param {Object} $max
 */
function textAreaCount($name, $max){
	$text = 'textarea[name='+$name+']';	
	$desc = $($text).val();
	$desc = $max - $desc.length;
	if($desc < 0){$('#chCount').css("color", "red");}			
	$('#chCount').html($desc);
}

/**
 * PHONE GROUP VALIDATION - checks div grouped phone numbers, validates, and returns array of any error messages or success.
 * @param {Object} group_id
 * @returns $ph_error[array]
 */
function validatePhGroup(){

	$hphone = $("[name='hphone1']").val() + $("[name='hphone2']").val() + $("[name='hphone3']").val();	
	$bphone = $("[name='bphone1']").val() + $("[name='bphone2']").val() + $("[name='bphone3']").val();
	$mphone = $("[name='mphone1']").val() + $("[name='mphone2']").val() + $("[name='mphone3']").val();
	$bext = $("[name='bext']").val();
	$hphone = fullTrim($hphone);
	$bphone = fullTrim($bphone);
	$mphone = fullTrim($mphone);
	
	$ph_error = new Array();
	
	$c = 0;
	// validate hphone
	if($hphone.length > 0){
		if(validateLength($hphone, 10, 10) == false || validateNumeric($hphone) == false){ 
		$ph_error['hphone'] = true;
		$ph_error['hphone_msg'] = "Please enter a valid home phone number."
		}else{$ph_error['hphone'] = false;}
	}else{
		$c = 1;
	}		
	
	// validate bphone & ext
	if($bphone.length > 0 || $bext.length > 0 ){
		if(validateLength($bphone, 10, 10) == false || validateNumeric($hphone) == false){ 
			$ph_error['bphone'] = true;
			$ph_error['bphone_msg'] = "Please enter a valid business phone number."
		}else{$ph_error['bphone'] = false;}
		if(validateNumeric($bext) == false){
			$ph_error['bext'] = true;
			$ph_error['bext_msg'] = "Phone extension can contain numbers only.";
		}else{$ph_error['bext'] = false;}		
	}else{
		$c = $c + 1;
	}
	
	// validate mphone
	if ($mphone.length > 0) {
		if(validateLength($mphone, 10, 10) == false || validateNumeric($mphone) == false){ 
			$ph_error['mphone'] = true;
			$ph_error['mphone_msg'] = "Please enter a valid mobile phone number."
		}else{$ph_error['hphone'] = false;}		
	}else{
		$c = $c + 1;
	}

	// CHECK NUMBERS SUBMITTED
	
	if($c < 3){
		// do check errors
		if($ph_error['hphone'] == true || $ph_error['bphone'] == true || $ph_error['bext'] == true || $ph_error['mphone'] == true ){
			$ph_error['error'] = true; // contains errors
			$ph_error['error_type'] = "phone";
		}else{
			$ph_error['error'] = false; // all valid
		}
		
	}else{
		$ph_error['error'] = true; // contains errors
		$ph_error['error_type'] = "missing";
		$ph_error['missing_msg'] = "At least one phone number is required.";
	}
	$c = 0;
	return $ph_error;	
} 

/**
 * VALIDATE MIN/MAX LENGTH - returns true if string fits within parameters
 * @param {Object} $string
 * @param {Object} $min
 * @param {Object} $max
 */
function validateLength($string, $min, $max){	
	$success = true;
	if($string.length < $min){$success = false;}
	else if($string.length > $max ){$success = false;}
	return $success;
}

/**
 * VALIDATE MAX LENGTH - returns true if string fits within parameters
 * @param {Object} $string
 * @param {Object} $min
 * @param {Object} $max
 */
function validateMaxLength($string, $max){	
	$success = true;
	if($string.length > $max ){$success = false;}
	return $success;
}
/**
 * REMOVE ALL WHITE SPACE - input a string and outputs it without any spaces
 * @param {Object} $string
 */
function fullTrim($string){
	$string = $string.replace(' ','');
	return $string;
}

/**
 * VALIDATE RADIO GROUP - checks to see if a radio button has been selected and returns true or false
 * @param {Object} $group_id
 */
function validateRadio($group_id){
	if($(group_id + ' > input:checked').length < 1){return false; }
	else{ return true; }
} 

 /**
  * VALIDATE REQUIRED - input a string returns true if a character count > 0 exists
  * @param {Object} $string
  */
function validateRequired($string){
 	if($string.length < 1){return false;}
	else{return true;}
}  

/**
 * VALIDATE NUMERIC - input a string returns true if it contains only numbers otherwise returns false.
 * @param {Object} $string
 */
function validateNumeric($string){
 	if (isNaN($string)) {return false;}
	else{return true;}
} 

/**
  * VALIDATE ALPHA/NUMERIC/SPACE - input a string and validates whether or not it contains only ALPHA/NUMERIC, NO Special Characters
  * @param {Object} $string
  */
function validateAlphaNum($string){
   if($string.search(/[^a-zA-Z0-9 ]/g) != -1){return false;}      
   else{return true;}
}


 /**
  * VALIDATE ALPHA- input a string and validates whether or not it contains only ALPHA, NO Special Characters or Spaces.
  * @param {Object} $string
  */
function validateAlpha($string){  
     if($string.search(/[^a-zA-Z ] \. /g) != -1){return false;}      
   else{return true;}
}

 /**
  * VALIDATE EMAIL - simple regex check for email layout for @, dot and returns true if passes
  * @param {Object} $email
  */
function validateEmail($email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test($email) == false){return false;}      
   else{return true;}	  
}

/**
 * VALIDATE DATE - Checks date format and not past current year, MM/DD/YYYY
 * @param {Object} $date
 */
function validateDate($date){

	var d = new Date();
	var curr_year = d.getFullYear();

	if($date.length != 10){return false;}
	else{ 
		$data_array = $date.split("/");
		if($data_array.length != 3 ||
		   $data_array[0].length != 2 ||
		   $data_array[1].length != 2 ||
		   $data_array[2].length != 4){ alert('array length not 3'); return false;}
		else{
			$data_array[0] = parseInt($data_array[0]);
			$data_array[1] = parseInt($data_array[1]);
			$data_array[2] = parseInt($data_array[2]);
			
			if($data_array[0] > 12 ||  
			   $data_array[1] > 31 || 
			   $data_array[2] > curr_year){
				return false;				
			}else{
				return true;
			}
		}
	}			
}

/**
 * VALIDATE CURRENT YEAR OR EARLIER - Check submitted year to make sure it isn't a future date.
 * @param {Object} $year
 */
function validateCurrYear($year){
	var d = new Date();
	var curr_year = d.getFullYear();
	
	if($date > curr_year){return false;}
	else{return true;}
}

/**
 * VALIDATE THOUSANDS COMMA SEPARATOR - Limit to 999,999,999 length.  Pass $number returns true if it fits 
 * the thousands comma separator pattern. If you have a regex for this, let me know. ejgeske@yahoo.com
 * @param {Object} $number
 */
function validateNumberComma($number){
	$success = true;
	$count = $number.length;	
	$clean = $number.replace(/,/g,'');
	$array = $number.split(',');

	// check last array for contents (weird, javascript will count the last array if empty, but says its undefined.)
	if($array.length - 1 != 0){
		if($array[$array.length - 1].length != 3){ $success = false;};
	};	
	
	// if more than three arrays. false 999,999,999 limit remember.
	if($array.length > 3){$success = false;}	
	// check $clean if numeric only
	if(validateNumeric($clean) == false || $count > 11){
		$success = false;
	}else{
		// Check first array
		if($array[0]){
			if ($array[0].length < 1 || $array[0].length > 3){
				$success = false;
			}
			// Check first number in array to ensure it is 1 or higher,  but allows just 0
			else{
				if($array[0].slice(0, 1) == 0) {
					$success = false;
				}
			}
		}else{
			$success = false;
		}
		// Check second array
		if($array[1]){
			if($array[1].length != 3){
				$success = false;
			}	
		}		
		//checks next array
		if ($array[2]) {
			if ($array[2].length != 3) {
				$success = false;
			}
		}
	}
	return $success;
}

/**
 * CHECK BOOLEAN - enter string tells whether is boolean. simple. 
 * @param {Object} $string
 */
function isBoolean($string){
	if($string == '1' || $string == '0'){ return true; alert('true'); }
	else{ return false; alert('false'); }
}

