//filter file format
function filterFileType(obj, acceptOnly){
	var ext3 = obj.value.substring(obj.value.length - 4, obj.value.length );
	var ext4 = obj.value.substring(obj.value.length - 5, obj.value.length );
	for(var i=0; i < acceptOnly.length; i++){
		if( ext3.toLowerCase() == acceptOnly[i] || ext4.toLowerCase() == acceptOnly[i]){
			return true;
		}
	}
	alert("Illegal file type used. Only a file with one of these file extension are allowed in this field(" + acceptOnly.toString() + ").");
	return false;
}
//check if valid date
function isDate(obj){
	var fVal = obj.value;
	var date1 = new Date(obj.value);
	if(fVal.length == 0 || date1.getFullYear() > 0){
		if(fVal.length > 0 && fVal.length < 10 ){
			date1.setYear(date1.getFullYear()+ 100);
			obj.value = formatDate(date1, 'mm/dd/yyyy');
		}
		return true;
	}
	return false;
}

// Check for blank space
function isBlank(obj){
	if(obj.value.length > 0) {
		for(var i=0; i < obj.value.length; i++){
			if(obj.value.charAt(i) != " "){
				return false;
			}		
		}
	}
	return true;
}

// Check for blank space
function lengthExceeds(obj, len){
	if(obj.value.length > len) {
		return true;
	}
	return false;
}

// check if text lenght correct
function isCorrectLength(textVal, low, high){		
	if(textVal.length > low && textVal.length < high){
		return true;
	}
	return false;
}

function replaceIlligalChars(obj){
	var isIlligal = false;
	var correctInput = "";
	var str_val = obj.value;
	for (i=0; i < str_val.length; i++) {
		ch = obj.value.charAt(i);
		if(ch == "&" && str_val.substring(i, i+5) != "&amp;"){	
			correctInput += "&amp;";
			isIlligal = true;
		}else{
			correctInput += ch;
		}
	}
	obj.value = correctInput;
	return isIlligal;
}

function isUsername(str_input) {
	for (i=0; i < str_input.length; i++) {
		ch = str_input.charAt(i);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < '0' || ch > '9') && (ch != "_")){
		    alert('Invalid characters in Username');
		    return false;
		    i = str_input.length;
		}
      }
	if (i < 4 || i > 20){
		alert('Username should be between 4 and 20 characters length');
		return false;
	}
	return true;
}

/**************************/

//Check phone number validity
function isPhoneNumber(obj){
	if(obj.value.length > 0){
		if(!exceedsLength(obj, 12)) return false;
		var phone_num = obj.value;
		phone_num = phone_num.replace('-', '');
		phone_num = phone_num.replace('-', '');
		if(isNaN(phone_num)){
			obj.select();
			obj.focus();		
			alert("The value entered is not a valid number.  Please use numbers and '-' in this field.");
			return false;
		}
		if(obj.value.charAt(3) != '-' || obj.value.charAt(7) != '-'){
			alert("Phone number entered is not in a invalid format!  Please use this format 405-555-5555.");
			obj.focus();
			obj.select();
			return false;
		}
	}
	return true;
}
//Check email validity
function isEmail(obj){	
	var email = obj.value;
	var atCount = 0;
	if(email.length != 0 ){
		for(i=0; i < obj.value.length; ++i) {
			if(obj.value.charAt(i) == '@')  atCount++;
		}
		if(atCount != 1){
			return false;
		}
	}
	return true;
}
//Check phone number validity
function isSSN(obj){
	if(obj.value.length > 0){
		if(!exceedsLength(obj, 12)) return false;
		var phone_num = obj.value;
		phone_num = phone_num.replace('-', '');
		phone_num = phone_num.replace('-', '');
		if(isNaN(phone_num)){
			obj.select();
			obj.focus();		
			alert("The value entered is not a valid number.  Please use numbers and '-' in this field.");
			return false;
		}
		if(obj.value.charAt(3) != '-' || obj.value.charAt(7) != '-'){
			return false;
		}
	}
	return true;
}
function ssnMask(obj){
	var ssn = obj.value;
	var lastChar = ssn.substring(ssn.length-1, ssn.length);
	if(ssn.length > 11 || isNaN(lastChar) ){
		obj.value = ssn.substring(0, ssn.length-1);
	}else{
		if(ssn.length == 3 || ssn.length == 6){
			obj.value = ssn + "-";
		}
	}
}
function phoneMask(obj){
	var phone = obj.value;
	var lastChar = phone.substring(phone.length-1, phone.length);
	if(phone.length > 12 || isNaN(lastChar) ){
		obj.value = phone.substring(0, phone.length-1);
	}else{
		if(phone.length == 3 ){
			obj.value = phone + "-" ;
		}else if(phone.length == 9 ){
			obj.value = phone.substring(0, phone.length-2) + "-" + phone.substring(phone.length-2, phone.length);
		}
	}
}
