/******************************************************************************
* validation.js
* -------------
* This file holds functions relevant to user input field validation.
******************************************************************************/

/******************************************************************************
* function validateDigits(field)
* ----------------------------------------
* Validates a user input field value to be a number 
* digits only.
*
* field - The object field to validate. Should be an object,
*         not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validateDigits(field)
{

//alert (field);
	// only allow numbers to be entered
	var checkOK = "0123456789";
	var checkStr = field.value;
	var allValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length)
	{
	allValid = false;
	break;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid)
	{
//		alert("Please enter only digit characters in the \"numbers\" field.");
//		field.focus();
//		field.select();
		return false;
	}


	// Validate for digits and length.
/*	if (parseInt(field.value) != field.value)
	{
		// The field is not valid, so set the focus on it in the form and select it
		// to enable the user to fix the value.
		field.focus();
		field.select();

		return false;
	}
*/
	// Field value is valid.
	return true;
}

/******************************************************************************
* function validateNumber(field, min, max)
* ----------------------------------------
* Validates a user input field value to be a number combined from
* digits only and in a given length in characters.
*
* field - The object field to validate. Should be an object,
*         not the actual string.
* min - The minimal length allowed for the input value.
* max - The maximal length allowed for the input value.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validateNumber(field, min, max)
{
	if (!min)
	{
		// Default value of 0 characters if no minimum parameter is passed.
		min = 0;
	}
	if (!max)
	{
		// Default value of 10 characters if no maximum parameter is passed.
		max = 10;
	}

	// Validate for digits and length.
	if (((field.value).length < min) || ((field.value).length > max))
	{
		// The field is not valid, so set the focus on it in the form and select it
		// to enable the user to fix the value.
		field.focus();
		field.select();

		return false;
	}

	// Field value is valid.
	return true;
}

/******************************************************************************
* function validateText(field, min, max)
* --------------------------------------
* Validates a user input field value to be in a given length in characters.
*
* field - The object field to validate. Should be an object,
*         not the actual string.
* min - The minimal length allowed for the input value.
* max - The maximal length allowed for the input value.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validateText(field, min, max)
{
	if (!min)
	{
		// Default value of 0 characters if no minimum parameter is passed.
		min = 0;
	}
	if (!max)
	{
		// Default value of 30 characters if no maximum parameter is passed.
		max = 30;
	}

	// Validate for length.
	if (((field.value).length < min) || ((field.value).length > max))
	{
		// The field is not valid, so set the focus on it in the form and select it
		// to enable the user to fix the value.
		field.focus();
		field.select();
		return false;
	}

	// Field value is valid.
	return true;
}

/******************************************************************************
* function validateTextForPipe(text)
* ----------------------------------
* Validates a user input field value not to contain the pipe character ('|').
* The pipe character is used in this application as a value separator
* within one field.
* Example: add_phone11 = phone11|9876543210|John Doe|Jo|Friends
*
* text - The object field to validate. Should be an object,
*        not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validateTextForPipe(text)
{
	var len = (text.value).length;
	var invalid_char = "|";

	return validateTextForForbiddenChar(text, invalid_char);
	// Scan the text and search for the pipe character.
	/*
	for (var i=0; i<len;i++)
	{
		// Check is the invalid char exists in the text.
		if ((text.value).indexOf(invalid_char) >= 0)
		{
			// The text is not valid, so set the focus on it in the form and select it
			// to enable the user to fix the value.
			text.focus();
			text.select();
			return false;
		}
	}

	// Text is valid.
	return true;
	*/
}

/******************************************************************************
* function validateTextForForbiddenChar(invalid_char)
* ----------------------------------
* Validates a user input field value not to contain the pipe character ('|').
* The pipe character is used in this application as a value separator
* within one field.
* Example: add_phone11 = phone11|9876543210|John Doe|Jo|Friends
*
* text - The object field to validate. Should be an object,
*        not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validateTextForForbiddenChar(text, invalid_char)
{
	var len = (text.value).length;

	// Scan the text and search for the pipe character.
	for (var i=0; i<len;i++)
	{
		// Check is the invalid char exists in the text.
		if ((text.value).indexOf(invalid_char) >= 0)
		{
			// The text is not valid, so set the focus on it in the form and select it
			// to enable the user to fix the value.
			text.focus();
			text.select();
			return false;
		}
	}

	// Text is valid.
	return true;
}

/******************************************************************************
* function validateTextForPipe(text)
* ----------------------------------
* Validates a user input field value not to contain the pipe character ('|').
* The pipe character is used in this application as a value separator
* within one field.
* Example: add_phone11 = phone11|9876543210|John Doe|Jo|Friends
*
* text - The object field to validate. Should be an object,
*        not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validateTextForUtf8ProblematicChars(text)
{

	var invalid_char = "";
	var len = (text.value).length;
	
	
	invalid_char = "&";
	if (validateTextForForbiddenChar(text, invalid_char)==false)
		return false;
	
	invalid_char = ">";
	if (validateTextForForbiddenChar(text, invalid_char)==false)
		return false;
	
	invalid_char = "<";
	if (validateTextForForbiddenChar(text, invalid_char)==false)
		return false;

	return true;
}

/******************************************************************************
* function validateTextForWhiteSpaces(text)
* ----------------------------------
* Validates a user input field value not to contain white spaces

* text - The object field to validate. Should be an object,
*        not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validateTextForWhiteSpaces(text)
{

	var invalid_char = "";
	var len = (text.value).length;
	
	invalid_char = " ";
	if (validateTextForForbiddenChar(text, invalid_char)==false)
		return false;
		
	return true;
}

/******************************************************************************
* function validate_login(form)
* -----------------------------
* Validates a user ID and password.
* The user ID input value should be a number only, length 1 to 10 characters.
* The password length must be 1 to 20 characters.
*
* form - The form object that contains the user and password fields
*        to validate.
*
* Returns true if the login syntax is valid, false if not.
******************************************************************************/
function validate_login(form)
{
	var msg = "";
	var success = true;

	// First validate the UID for syntax.
	if (validate_uid(form.uid) == true)
	{
		// UID is ok. So check whether a password was filled.
		// check if user entered password
		if (form.pss.value.length==0) 
		{
			success = false;
			// Please enter password
			msg += "Please enter your username and password";
		}
		// Also check for the password limit of 20 characters.
		else if (validateText(form.pss, 1, 25) == false)
		{
			// Password suntax is not valid.
			success = false;
			// Password is not at least one character or over 20 characters.
			msg += "Password must be between 1-25 characters";
		}
	}
	else
	{
		// mark that the uid validation was not successful.
		success = false;
	}

	// Pop up the message for the user.
	if (msg != "")
	{
		alert (msg);
	}

	return (success);
}

/******************************************************************************
* function validate_uid(uid)
* --------------------------
* Validates a user ID input value to be a number only,
* length 1 to 10 characters.
* Pops up a message for the user if uid is not valid.
*
* uid - The object field to validate. Should be an object,
*       not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validate_uid(uid)
{
	var msg = "";
	var success = true;

	// Validate the uid.
	if (validateText(uid, 1, 16) == false)
	{
		// uid is not valid.
		success = false;
		// Prepare a message for the user.
		msg += "Please enter your username and password";
	}

	// Pop up the message for the user.
	if (msg != "")
	{
		alert (msg);
	}

	return (success);
}

/******************************************************************************
* function validate_contact(name, nick, phone)
* --------------------------------------------
* Validates an address book contact.
* The validation constraints are -
* - name is required.
* - Max length of 25 characters for name and nickname.
* - No pipe character is allowed for name and nickname.
* - Phone number must be 10 digits long.
* Pops up a message for the user upon any validation failure.
*
* name - The name input field object to validate. Should be an object,
*        not the actual string.
* nick - The nickname input field object to validate. Should be an object,
*        not the actual string.
* phone - The phone input field object to validate. Should be an object,
*         not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validate_contact(name, nick, phone)
{
	var msg = "";
	var success = true;
	// Max length for the contact text validation.
	var text_len = 25;
	var min_phone_num_digits = 5;
	var max_phone_num_digits = 10;
	
	if (  (validateTextForUtf8ProblematicChars(phone)==false) ||
		  (validateTextForUtf8ProblematicChars(name)==false) ||
		  (validateTextForUtf8ProblematicChars(nick)==false) )
	{
		success = false;
		msg += "& > <  "; 
		msg += "characters can't be used in a field value \n";
	}
		  
	// Validate for phone number length and digits. 
	/* Commented out for Chile Fix. 1-Nov-2006. */
	else if (validateNumber(phone, min_phone_num_digits , max_phone_num_digits ) == false)
	{
		success = false;
		msg += " Telephone number must be between 5 to 10 digits.\n";
	}
	// validate phone number to be digits only
	else if (validateDigits(phone) == false)
	{
		success = false;
		msg += " Only digits allowed in phone number.\n";
		phone.focus();
		phone.select();		
	}
	
				
	// Validate the nickname.
	// Check for the pipe character. Since "|" is used in the application for values seperator,
	// the user can't use this character as a data in an input field.
	else if (validateTextForPipe(nick) == false)
	{
		success = false;
		msg += " The Pipe character ('|') can't be used in a field value.\n";
	}

	// Validate for nickname length.
	else if (validateText(nick, 0, text_len) == false)
	{
		success = false;
		msg += " A contact nickname should not exceed " + text_len + " characters in length.\n";
	}

	// Validate the name.
	// Check for the pipe character. Since "|" is used in the application for values seperator,
	// the user can't use this character as a data in an input field.
	else if (validateTextForPipe(name) == false)
	{
		success = false;
		msg += " The Pipe character ('|') can't be used in a field value.\n";
	}

	// Validate for name length. Min length is 1, meaning - a name is mandatory.
	else if (validateText(name, 1, text_len) == false)
	{
		success = false;
		msg += " A contact name is mandatory and should not exceed " + text_len + " characters in length.\n";
	}

	// pop up the error messages.
	if (msg != "")
	{
		alert (msg);
	}

	return (success);
}

/******************************************************************************
* function validate_device(name)
* ------------------------------
* Validates a device properties.
* The validation constraints for the name -
* - Max length of 30 characters.
* - No pipe character is allowed.
* Pops up a message for the user if any of the fields is not valid.
*
* name - The name input field object to validate. Should be an object,
*        not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validate_device(name)
{
	var msg = "";
	var success = true;
	var text_len = 30;

	// Validate the name.
	// Check for the pipe character. Since "|" is used in the application for values seperator,
	// the user can't use this character as a data in an input field.
	if (validateTextForPipe(name) == false)
	{
		success = false;
		msg += " The Pipe character ('|') can't be used in a field value.\n";
	}

	// Validate for name length.
	if (validateText(name, 0, text_len) == false)
	{
		success = false;
		msg += " A device name should not exceed " + text_len + " characters in length.\n";
	}

	// pop up the error messages.
	if (msg != "")
	{
		alert (msg);
	}

	return (success);
}

/******************************************************************************
* function validate_display_name(name)
* ------------------------------------
* Validates a phone line display name.
* The validation constraints for the name -
* - Max length of 25 characters.
* - No pipe character is allowed.
* Pops up a message for the user if any of the fields is not valid.
*
* name - The name input field object to validate. Should be an object,
*        not the actual string.
* ordinal - The line ordinal number in the installed phone lines DB.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validate_display_name(name, ordinal)
{
	var msg = "";
	var success = true;
	var text_len = 25;

	// Validate the name.
	// Check for the pipe character. Since "|" is used in the application for values seperator,
	// the user can't use this character as a data in an input field.
	if (validateTextForPipe(name) == false)
	{
		success = false;
		msg += "The Pipe character ('|') can't be used in a field value.\n";
	}

	// Validate for name length.
	if (validateText(name, 0, text_len) == false)
	{
		success = false;
		msg += "A display name should not exceed " + text_len + " characters in length.\n";
	}

	// If the name is empty, give it a default name according to the ordinal number.
	if ((trim(name.value)).length == 0)
	{
		name.value = "Line " + ordinal;
	}

	// pop up the error messages.
	if (msg != "")
	{
		alert (msg);
	}

	return (success);
}

/******************************************************************************
* function trim(str)
* ------------------
* Trims white spaces at the beginning ans at the end of a string.
*
* str - The string to trim.
*
* Returns the trimmed string.
******************************************************************************/
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}

/******************************************************************************
* function validate_user_profile
* ------------------------------
* Runs the function 'validate_profile' with the 'myprofilenick' field
* to validate.
*
* Returns the return value of the 'validate_profile' function.
******************************************************************************/
function validate_user_profile()
{
	return (validate_profile(document.hiddenuidcommit.myprofilenick));
}

/******************************************************************************
* function validate_profile(name)
* -------------------------------
* Validates a profile nickname.
* The validation constraints for the name -
* - Max length of 25 characters.
* - No pipe character is allowed.
* Pops up a message for the user if any of the fields is not valid.
*
* name - The name input field object to validate. Should be an object,
*        not the actual string.
*
* Returns true if the field is valid, false if the field is not valid.
******************************************************************************/
function validate_profile(name)
{
	var msg = "";
	var success = true;
	var text_len = 25;

	// Validate the name.
	// Check for the pipe character. Since "|" is used in the application for values seperator,
	// the user can't use this character as a data in an input field.
	if (validateTextForPipe(name) == false)
	{
		success = false;
		msg += " The Pipe character ('|') can't be used in a field value.\n";
	}

	// Validate for name length.
	if (validateText(name, 0, text_len) == false)
	{
		success = false;
		msg += " A profile nickname should not exceed " + text_len + " characters in length.\n";
	}

	// pop up the error messages.
	if (msg != "")
	{
		alert (msg);
	}

	return (success);
}
/******************************************************************************
* function validate_passwords(old_password, new_password, confirm_new_password)
* --------------------------------------------
* Validates all passwords (new and old).
* The validation constraints are -
* - old_password is required.
* - Max length of 25 characters for name and nickname.
* - No pipe character is allowed for name and nickname.
* - Phone number must be 10 digits long.
* Pops up a message for the user upon any validation failure.
*
* old_password - The old_password input field object to validate. Should be an object,
*        not the actual string.
* new_password - The new_password input field object to validate. Should be an object,
*        not the actual string.
* confirm_new_password - The confirm_new_password input field object to validate. Should be an object,
*         not the actual string.
*
* Returns true if the fields are valid, false if the fields is not valid.
******************************************************************************/
function validate_passwords(current_password, new_password, confirm_new_password,password_change_nothing_entered_msg)
{

	var msg = "";
	var success = true;
	// Max length for the contact text validation.
	var min_password_chars = 6;
	var max_password_chars = 25;
		
	//var text_len = 25;
	
	
if (current_password.value.length == 0 && new_password.value.length == 0 && confirm_new_password.value.length == 0) {
	 alert(password_change_nothing_entered_msg);
	 return false;
}



	// Validate for current_password length. Min length is 1, meaning - a current_password is mandatory.
	if (validateText(current_password, min_password_chars, max_password_chars) == false)
	{
		success = false;
		msg += " Password is mandatory and it's length should be between 6 to 25 characters, please try again.\n";
	}

	// Validate for new_password length. Min length is 1, meaning - a new_password is mandatory.
	else if (validateText(new_password, min_password_chars, max_password_chars) == false)
	{
		success = false;
		msg += " Password is mandatory and it's length should be between 6 to 25 characters, please try again.\n";
	}

	// Validate for confirm_new_password length. Min length is 1, meaning - a confirm_new_password is mandatory.
	else if (validateText(confirm_new_password, min_password_chars, max_password_chars) == false)
	{
		success = false;
		msg += " Password is mandatory and it's length should be between 6 to 25 characters, please try again.\n";
	}

	
	else if (  (validateTextForUtf8ProblematicChars(current_password)==false) ||
		  (validateTextForUtf8ProblematicChars(new_password)==false) ||
		  (validateTextForUtf8ProblematicChars(confirm_new_password)==false) )
	{
		success = false;
		msg += "& > <  "; 
		msg += "characters can't be used in a field value \n";
	}
	
	else if (  (validateTextForWhiteSpaces(current_password)==false) ||
		  (validateTextForWhiteSpaces(new_password)==false) ||
		  (validateTextForWhiteSpaces(confirm_new_password)==false) )
	{
		success = false;
		msg = "white spaces can't be used \n";
	}
	
		  
	
	// Validate the current_password.
	// Check for the pipe character. Since "|" is used in the application for values seperator,
	// the user can't use this character as a data in an input field.
	else if (validateTextForPipe(current_password) == false)
	{
		success = false;
		msg += " The Pipe character ('|') can't be used in a field value.\n";
	}


	// Validate the new_password.
	// Check for the pipe character. Since "|" is used in the application for values seperator,
	// the user can't use this character as a data in an input field.
	else if (validateTextForPipe(new_password) == false)
	{
		success = false;
		msg += " The Pipe character ('|') can't be used in a field value.\n";
	}


	else if (validateTextForPipe(confirm_new_password) == false)
	{
		success = false;
		msg += " The Pipe character ('|') can't be used in a field value.\n";
	}

	
	else if (new_password.value != confirm_new_password.value) {
		success = false;
		msg += " The passwords you have entered are not identical, please try again .\n";
	}
	
	else if (new_password.value == current_password.value) {
		success = false;
		msg += " Your new password is the same as the old password, please select a different password .\n";
	}
	

	// pop up the error messages.
	if (msg != "")
	{
		alert (msg);
	}

	return success;
}

