// VALIDATOR.JS

// VALIDATION CODE FILE MADE BY MANU MAHENDRU, SERCON INDIA PVT. LTD.
// YOU ARE FREE TO USE THIS CODE IN ANY MANNER YOU FEEL LIKE.
// SERCON INDIA PVT. LTD. AND MANU MAHENDRU CANNOT BE HELD RESPONSIBLE 
// FOR ANY DAMAGE OR LOSS THAT MAY OCCUR BY USING THIS CODE.
// IT WILL BE APPRECIATED IF THESE COMMENTS ARE LEFT INTACT IN YOUR CODE.


// THE FIRST PARAMETER OF NAME "text_box" IN FUNCTIONS BELOW IS OF 
// THE TYPE "formname.objectname".



// -------------------------------------------------------------------------
// Function to check if a given letter is a digit :

function is_digit(ch)
{
	if(ch<"0" || ch>"9")
		return false
	
	return true
}


// -------------------------------------------------------------------------
// To check if atleast one of a radio group is selected
function is_radio_selected(button_group, alert_str)
{
	for( var i=0; i<button_group.length; i++)
	{	
		if(button_group[i].checked)  
		    return true
	}
	alert(alert_str)
	return false
}

// -------------------------------------------------------------------------
// To check if a Text Box is empty
function isempty(text_box, alert_str)
{
	var text = text_box.value
	 
	if (text.length==0)
		{ 
			alert(alert_str)
		   	return true
		}
	
	for(var i=0; i<text.length; i++)
	{	
		if(text.charAt(i)!=" ")
		  	return false
	}
	
	alert(alert_str)
	return true
}


// -------------------------------------------------------------------------
// To check if the value entered in a Text Box is only text
//
// The variable "space_allowed" should be set to 0 when the text is not to 
// be allowed to contain spaces. Else it should be set to 1. Any other value 
// will show an error alert and will return false.
function is_only_text(text_box, space_allowed, alert_str)
{
	var text = text_box.value.toLowerCase()
	
	if((space_allowed!=0) && (space_allowed!=1))
	{
		alert("Test Error : Error in calling function 'is_only_text'.\n3 parameters required.")
		return false
	}
	
	if(space_allowed==0)
	{
	for( var i=0; i<text.length; i++)
	{	
		if(text.charAt(i)<"a" || text.charAt(i)>"z")
		{ 
			alert(alert_str)
		   	return false
		}
	}
	}
	
	if(space_allowed==1)
	{
	for( var i=0; i<text.length; i++)
	{	
		if((text.charAt(i)<"a" || text.charAt(i)>"z") && (text.charAt(i)!=" "))
		{ 
			alert(alert_str)
		   	return false
		}
	}
	}
	
	return true
}	


// -------------------------------------------------------------------------
// To check if the value entered in a Text Box is only digits
function is_only_number(text_box, alert_str)
{
	var text = text_box.value.toString()
	for( var i=0; i<text.length; i++)
	{	
		if(text.charAt(i)<"0" || text.charAt(i)>"9")
		{ 
			alert(alert_str)
			return false
		}
	}
	return true
}	


// -------------------------------------------------------------------------
// To check if a text or number is more than the maximum length allowed
// Returns TRUE if the value exceeds the max allowed value

// NOTE : YOU MAY/SHOULD CONTROL THE MAX ALLOWED CHARACTERS IN A TEXT BOX THROUGH HTML
// BY SETTING THE "MAXLENGTH" ATTRIBUTE OF A TEXT BOX
 
function is_greater_than(text_box, max_val, alert_str)
{
	if(text_box.value.length > max_val)
	{
		alert(alert_str)
		return true
	}
	return false
}	



// -------------------------------------------------------------------------
// To check if a text or number is less than the minimum length allowed
// Returns TRUE if the value is less than the least allowed value
function is_less_than(text_box, min_val, alert_str)
{
	if(text_box.value.length < min_val)
	{
		alert(alert_str)
		return true
	}
	return false
}


// -------------------------------------------------------------------------
// To check if the length of text entered in a Text Box is exactly equal to the required length
function is_exactly(text_box, val, alert_str)
{
	if(text_box.value.length != val)
	{
		alert(alert_str)
		return false
	}
	return true
}

// -------------------------------------------------------------------------
// To check the validity of an emaill address
function email_ok(text_box)
{
	// FUNCTION RETURNS FALSE IF ANYTHING IS WRONG WITH THE EMAIL ADDRESS

	var email = text_box.value.toLowerCase()
	var at_the_rate = 0
	var dot = 0
	alert_str = "You have entered an invalid email address. Please correct it."
	
	// Email address must contain a "@" and "."
	for(var i=0; i<email.length; i++)
	{
		if(email.charAt(i) == "@")
			at_the_rate = 1
		if(email.charAt(i) == ".")
			dot = 1
	}
	if ((at_the_rate == 0) || (dot == 0))
	{
		alert(alert_str)
		return false
	}
	
	// Characters can only be letters, digits, underscore, '@', and '.'
	var ch
	for(var i=0; i<email.length; i++)
	{
		ch = email.charAt(i)
		if(((ch < "a") || (ch > "z")) && ((ch < "0") || (ch > "9")) && (ch != "_") && (ch != "@") && (ch != "."))
		{
			alert(alert_str)
			return false
		}	
	}
	
	// Value of first letter must be an alphabet
	if(email.charAt(0)<"a" || email.charAt(0)>"z")
	{
		alert(alert_str)
		return false
	}
	
	// Value of last letter must be an alphabet or a digit
	var last_ch = email.charAt(email.length -1)
	
	if((last_ch<"a" || last_ch>"z") && (last_ch<"0" || last_ch>"9"))
	{
		alert(alert_str)
		return false
	}

	// There must be atleast one letter between "@" and ","
	var at_location = email.indexOf("@")
	var dot_location = email.indexOf(".",at_location)

	if (dot_location - at_location <= 1)
	{
		alert(alert_str)
		return false
	}
	
	// CONTROL REACHES HERE ONLY IF EVERYTHING IS OK WITH THE EMAIL ADDRESS
	return true
}


// -------------------------------------------------------------------------
// Function to check if any item has been selected from a select list
// TRUE is returned if the first item has been selected.
// To use this, set the first item in the list to be empty. 
function is_none_selected(select_item, alert_str)
{
	if(select_item.selectedIndex == 0)
	{
		alert(alert_str)
		return true
	}
	return false
}



// -------------------------------------------------------------------------
// Function to check for valid mobile - cellular - phone numbers
// FIRST function : mobile_country - checks for a valid country code; like +91 for India
// SECOND funtion : mobile_ok - checks for a valid mobile number of 10 digits in length

function mobile_country(mob_country_text, alert_str)
{// THIS FUNCTION RETURNS FALSE FOR ANY ERROR IN THE COUNTRY CODE
	
	var mob1 = mob_country_text.value

	if(mob1!="")// REQUIRED BECAUSE A MOBILE NUMBER CANNOT BE A MANDATORY FIELD
	{	
		// FIRST CHARACTER OF COUNTRY CODE MUST BE "+" OR "0"
		if((mob1.charAt(0)!="+") && (mob1.charAt(0)!="0"))
		{
			alert(alert_str)
			return false
		}

		// COUNTRY CODE MUST BE 3 CHARACTERS IN LENGTH
		if(mob1.length!=3)
		{
			alert(alert_str)
			return false
		}
		
		// LAST TWO CHARACTERS MUST BE ONLY DIGITS
		for (var i=1; i<mob1.length; i++)
			if(mob1.charAt(i)<"0" || mob1.charAt(i)>"9")
			{
				alert(alert_str)
				return false
			}
		
		// BOTH THE LAST TWO CHARACTERS CANNOT BE ZERO
		if(mob1.charAt(1)==0 && mob1.charAt(2)==0)
		{
			alert(alert_str)
			return false
		}
	}	
	return true
}

function mobile_ok(mob_text, alert_str)
{// THIS FUNCTION RETURNS FALSE FOR ANY ERROR WITH THE MOBILE NUMBER.

	if(mob_text.value!="")// REQUIRED BECAUSE A MOBILE NUMBER CANNOT BE A MANDATORY FIELD
	{
		// USING THE ABOVE WRITTEN FUNCTION TO CHECK IF MOBILE NUMBER CONTAINS ANYTHING OTHER THAN DIGITS
		if(!(is_only_number(mob_text, alert_str)))
			return false

		// USING THE ABOVE WRITTEN FUNCTION TO CHECK IF MOBILE NUMBER IS EXACTLY 10 DIGITS IN LENGTH
		if(!(is_exactly(mob_text, 10, alert_str)))
			return false
	}
	return true
}


// -------------------------------------------------------------------------
// Function to check that if a mobile country code is entered, then a mobile 
// cellular phone number is also entered, and vice versa.
//
// The first parameter is the country code text box, and the second is the 
// mobile phone text box.
// Returns TRUE if both or none are entered.

function is_mobile_consistent(mob1, mob2)
{// THE FIRST PARAMETER IS THE COUNTRY CODE DIALOG BOX AND THE SECIND ONE 
 // IS THE MOBILE CELLULAR NUMBER
 
	var num1 = mob1.value
	var num2 = mob2.value
	
	if((num1!="") && (num2==""))
	{
		alert("You have entered the \"Country Code\" for your cellular number but have not entered your cellular number itself.\n\nPlease enter either BOTH or NONE of the cellular number and the country code.")
		return false
	}
	
	if((num2!="") && (num1==""))
	{
		alert("You have entered a \"Cellular Number\" but have not entered a country code for your cellular number.\n\nPlease enter either BOTH or NONE of the cellular number and the country code.")
		return false
	}
	
	return true
}


// -------------------------------------------------------------------------
// Function to check values of two seperate text boxes. Returns TRUE only if both values are same.
// Can be used for password, and email double checks

function double_check_ok(text1, text2, alert_str)
{
	var t1 = text1.value.toLowerCase()
	var t2 = text2.value.toLowerCase()
	
	if(t1.length!=t2.length)
	{
		alert(alert_str)
		return false
	}

	for(var i=0; i<t1.length; i++)
	{
		if(t1.charAt(i)!=t2.charAt(i))
		{
			alert(alert_str)
			return false
		}
	}
	return true
}


// -------------------------------------------------------------------------
// Function that checks the validity of a password chosen/entered by the user. 
// Note that this does NOT include the double_check_ok functionality. You will 
// have to use a separate funtion for that.

// The password must begin only with a letter or digit.
// The password can only contain a letter/digit/or an underscore

function is_password_ok(pass, alert_str)
{
	var word = pass.value.toLowerCase()
	
	// CHECKING THE FIRST CHARACTER IS A LETTER OR A DIGIT
	if((word.charAt(0)<"a" || word.charAt(0)>"z") && (word.charAt(0)<"0" || word.charAt(0)>"9"))
	{
		alert("The first character of your password must be a digit\(0-9\) or a letter\(a-z,A-Z\).\n\nPlese correct the password you have chosen.")
		return false
	}
	
	// THE PASSWORD MUST ONLY CONTAIN LETTERS, DIGITS, AND AN UNDERSCORE
	for(var i=0; i<word.length(); i++)
	{
		if((word.charAt(i)<"a" || word.charAt(i)>"z") && (word.charAt(i)<"0" || word.charAt(i)>"9") && (word.charAt(i)!="_"))
		{
			alert("The password can only contain letters\(a-z,A-Z\), digits\(0-9\), and an underscore.\nMake sure that your password does not contain any spaces.\n\nPlese correct the password you have chosen.") 
			return false
		}
	}

	return true
}



// -------------------------------------------------------------------------
// Function that checks validity of the date entered by the user to 
// be of the form "dd-mm-yyyy".

function is_date_ok(date_text,alert_str)
{
	var text = date_text.value
	
// Characters at position 0,1,3,4,6,7,8,9 should be digits
	if(!(
	(is_digit(text.charAt(0))) 
	&& (is_digit(text.charAt(1))) 
	&& (is_digit(text.charAt(3))) 
	&& (is_digit(text.charAt(4))) 
	&& (is_digit(text.charAt(6))) 
	&& (is_digit(text.charAt(7))) 
	&& (is_digit(text.charAt(8))) 
	&& (is_digit(text.charAt(9)))
	))
	{
		alert(alert_str);
		return false;
	}
	

// Characters at position 2,5 should be '-'
	if((text.charAt(2)!="-")||(text.charAt(5)!="-"))
	{
		alert(alert_str);
		return false;
	}

// Extracting dd, mm, and yyyy in separate variables from the date field
	var dd = parseInt(text.substr(0,2))
	var mm = parseInt(text.substr(3,2))
	var yyyy = parseInt(text.substr(6,4))
	

// "dd" must be between 1 and 31
	if(dd<1 || dd>31)
	{
		alert("The day you have entered in the date field must be between\n1 and 31. Please correct it.");
		return false;
	}
	
	
// "mm" must be between 1 and 12
	if(mm<1 || mm>12)
	{
		alert("The month you have entered in the date field must be between\n1 and 12. Please correct it.");
		return false;
	}


// "yyyy" must be between 1900 and 2099
	if(yyyy<1900 || yyyy>2099)
	{
		alert("The year you have entered in the date field must be between\n1900 and 2099. Please correct it.");
		return false;
	}
	
	
// For months 4,6,9,11 the number of days must be less than 30
	if((mm==4)||(mm==6)||(mm==9)||(mm==11))
	{
		if((dd<1)||(dd>30))
		{
		alert("The month you have entered does not contain the number of\ndays you have specified in the date of the month.\nPlease correct it.");
		return false;
		}
	}
	
	
// For the month of February, the number of days must be 28 or 29 :
	if(mm==2)
	{
		if((yyyy==1900) || ((yyyy%4)!=0))
		// If not a leap year :
		{
			if((dd<1)||(dd>28))
			{
				alert("The year you have entered in not a leap year. The number of days\nin the month of February must be less than or equal to 28.\nPlease correct it.");
				return false;
			}	
		}
		else
		{
			if((dd<1)||(dd>29))
			{
				alert(alert_str);
				return false;
			}	
		}
		
	}
	
	return true;
}


// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
