// Alpine Computing JavaScript Validation
// Convergicom JavaScript Document

//Start of Login Validation

function validate() {
	
	var info = checkNm();
	info += checkPwd();

    if (info != "") {
        alert(info);
		return false;
	}
	return true;
}

function checkNm() {
	var error = "";
	var user = document.getElementById("username").value;
	if (user == "") {
		error = "Please fill in your User Name \n";
	return error;
	}
	if (user.length > 15) {
    	error = "The username is the too long.\n";
	return error;
	}
	return "";
}

function checkPwd() {
	var error = "";
	var password = document.getElementById("password").value;
	if (password.length < 4) {
		error = "The password is too short";
	return error;
	}
	if (password.length > 15) {
       error = "The password is the too long. \n";
	  return error;
    }
	return "";
}

//Start of Reply Validation
function checkResponse() {
	
	var info = checkReplied();
	info += checkComment();

    if (info != "") {
        alert(info);
		return false;
	}
	return true;
}

function checkReplied() {
	var error = ""
	var name = document.getElementById("replied").value;
	if (name == "") {
		error = "Please fill in your Name in the Replied Field \n";
		return error;
	}
	return "";
}

function checkComment() {
	var error = ""
	var prob = document.getElementById("comment").value;
	if (prob.length < 10) {
		error = "Comment is not long enough \n";
	return error;
	}
	return "";
}
//End of Reply Validation
//Start of Feedback Response Check
function checkFeedback() {
	
	var info = checkSubject();
	info += checkFBComment();

    if (info != "") {
        alert(info);
		return false;
	}
	return true;
}

function checkSubject() {
	var error = ""
	var name = document.getElementById("subject").value;
	if (name == "") {
		error = "Fill in Subject Field \n";
		return error;
	}
	return "";
}

function checkFBComment() {
	var error = ""
	var prob = document.getElementById("comment").value;
	if (prob.length < 10) {
		error = "Comment is not long enough \n";
	return error;
	}
	return "";
}
//End of Feedback Response Check
//Start of Client/Admin New Ticket Check

function adminNewTicket() {

	var info = checkTicUser();
	info += checkTicName();
	info += checkTicEmail();
	info += checkTicPriority();
	info += checkTicCategory();
	info += checkTicSubject();
	info += checkTicComment();
	
	if (info != "") {
		alert(info);
		return false;
	}
	return true;
}

function newTicket() {

	var info = checkTicName();
	info += checkTicEmail();
	info += checkTicPriority();
	info += checkTicCategory();
	info += checkTicSubject();
	info += checkTicComment();
	
	if (info != "") {
		alert(info);
		return false;
	}
	return true;
}

function checkTicUser() {
	var error = ""
	var name = document.getElementById("user").value;
	if (name == "") {
		error = "Please Select a User \n";
		return error;
	}
	return "";
}

function checkTicName() {
	var error = ""
	var name = document.getElementById("name").value;
	if (name == "") {
		error = "Fill in the Name Field \n";
		return error;
	}
	return "";
}

function checkTicPriority() {
	var error = ""
	var name = document.getElementById("priority").value;
	if (name == "") {
		error = "Fill in the Priority Field \n";
		return error;
	}
	return "";
}

function checkTicCategory() {
	var error = ""
	var name = document.getElementById("category").value;
	if (name == "") {
		error = "Fill in the Category Field \n";
		return error;
	}
	return "";
}

function checkTicSubject() {
	var error = ""
	var name = document.getElementById("subject").value;
	if (name == "") {
		error = "Fill in the Subject Field \n";
		return error;
	}
	return "";
}

function checkTicEmail() {
	var error = ""
	var email = document.getElementById("email").value;
	var eFilter=/^.+@.+\..{2,3}$/;
	var illegal= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (email == "") {
		error = "Please fill in your Email Address \n";
	return error;
	}
	if (!(eFilter.test(email))) { 
       error = "Please enter a valid email address.\n";
	  return error;
	}
	if (email.match(illegal)) {
   		error = "The email address contains illegal characters.\n";
	return error;
	}
	return "";
}

function checkTicComment() {
	var error = ""
	var prob = document.getElementById("comment").value;
	if (prob.length < 10) {
		error = "Description is not long enough \n";
	return error;
	}
	return "";
}

// End of Client/Admin New Ticket Check
// Start of Profile Check

function checkAdminProfile() {
	
	var info = checkUserName();
	info += checkPassword();
	info += checkUserType();
	info += checkCompany();
	info += checkContact();
	info += checkAddress();
	info += checkCity();
	info += checkState();
	info += checkZip();
	info += checkPhone();
	info += checkFax();
	info += checkEmail();
	
	if (info != "") {
		alert(info);
		return false;
	}
	return true;
}

function checkAdminProfile2() {
	
	var info = checkPassword();
	info += checkUserType();
	info += checkCompany();
	info += checkContact();
	info += checkAddress();
	info += checkCity();
	info += checkState();
	info += checkZip();
	info += checkPhone();
	info += checkFax();
	info += checkEmail();
	
	if (info != "") {
		alert(info);
		return false;
	}
	return true;
}

function checkAdminProfile3() {
	
	var info = checkPassword();
	info += checkUserType();
	info += checkCompany();
	info += checkContact();
	info += checkAddress();
	info += checkCity();
	info += checkState();
	info += checkZip();
	info += checkPhone();
	info += checkFax();
	info += checkEmail();
	
	if (info != "") {
		alert(info);
		return false;
	}
	return confirm('Are your sure you want to delete this user');
}

function checkProfile() {
	
	var info = checkPassword();
	info += checkCompany();
	info += checkContact();
	info += checkAddress();
	info += checkCity();
	info += checkState();
	info += checkZip();
	info += checkPhone();
	info += checkFax();
	info += checkEmail();
	
	if (info != "") {
		alert(info);
		return false;
	}
	return true;
}

function checkUserName() {
	var error = ""
	var name = document.getElementById("username").value;
	if (name.length < 6) {
		error = "Username must be at least 6 charcters long \n";
		return error;
	}
	if (name.length > 15) {
		error = "Username can't be longer than 15 characters \n";
		return error;
	}
	return "";
}

function checkPassword() {
	var error = ""
	var name = document.getElementById("password").value;
	if (name.length < 6) {
		error = "Password must be at least 6 charcters long \n";
		return error;
	}
	if (name.length > 15) {
		error = "Password can't be longer than 15 characters \n";
		return error;
	}
	return "";
}

function checkContact() {
	var error = ""
	var name = document.getElementById("contact").value;
	if (name == "") {
		error = "Please fill in the primary contact \n";
		return error;
	}
	return "";
}

function checkCompany() {
	var error = ""
	var name = document.getElementById("company").value;
	if (name == "") {
		error = "Please fill in your Company Name \n";
		return error;
	}
	return "";
}

function checkUserType() {
	var error = ""
	var name = document.getElementById("type").value;
	if (name == "") {
		error = "Please Select the User Permission Type \n";
		return error;
	}
	return "";
}

function checkAddress() {
	var error = ""
	var prob = document.getElementById("address").value;
	if (prob == "") {
		error = "Please fill in your Address \n";
	return error;
	}
	return "";
}

function checkCity() {
	var error = ""
	var prob = document.getElementById("city").value;
	if (prob == "") {
		error = "Please fill in your City \n";
	return error;
	}
	return "";
}

function checkState() {
	var error = ""
	var prob = document.getElementById("state").value;
	var checkval = isNaN(prob);
	if (prob.length != 2) {
		error = "Please fill in your State Abbreviation \n";
	return error;
	}
	if (checkval == false) {
		error = "Illegal characters in State Field \n";
	return error;
	}
	return "";
}

function checkZip() {
	var error = ""
	var prob = document.getElementById("zip").value;
	var checknum = isNaN(prob);
	if (prob.length != 5) {
		error = "The zip code is the wrong length \n";
	return error;
	}
	if (checknum == true) {
		error = "Illegal characters in zip code \n";
	return error;
	}
	return "";
}

function checkPhone() {
	var error = ""
	var num = document.getElementById("phone").value;
	var strip = num.replace(/[\(\)\.\-\ ]/g, '');
	if (num == "") {
		error = "Please fill in your Phone Number \n";
	return error;
	}
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(strip))) {
   		error = "The phone number contains illegal characters. \n";
	return error;
	}
	if (!(strip.length == 10)) {
		error = "The phone number is the wrong length, please enter a 10 digit number. \n";
	return error;
	}
	return "";
}

function checkFax() {
	var error = ""
	var num = document.getElementById("fax").value;
	var strip = num.replace(/[\(\)\.\-\ ]/g, '');
	if (num == "") {
		error = "Please fill in your Fax Number \n";
	return error;
	}
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(strip))) {
   		error = "The fax number contains illegal characters. \n";
	return error;
	}
	if (!(strip.length == 10)) {
		error = "The fax number is the wrong length, please enter a 10 digit number. \n";
	return error;
	}
	return "";
}

function checkEmail() {
	var error = ""
	var email = document.getElementById("email").value;
	var eFilter=/^.+@.+\..{2,3}$/;
	var illegal= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (email == "") {
		error = "Please fill in your Email Address \n";
	return error;
	}
	if (!(eFilter.test(email))) { 
       error = "Please enter a valid email address.\n";
	  return error;
	}
	if (email.match(illegal)) {
   		error = "The email address contains illegal characters.\n";
	return error;
	}
	return "";
}

// End of Profile Check
// Message
function checkClose() {
	
var closeTic = confirm('Are your sure you want to close this ticket');
return closeTic;
}