﻿// Authors: Shashwat Parhi, Arpan Dhandhania
// Created: June 7th 2005
// Last Revised: May 17th 2006

var regExp;
var output_str;

function Trim(str)
{
	while (str.substring(0,1) == ' ')
		str = str.substring(1, str.length);

	while (str.substring(str.length-1, str.length) == ' ')
		str = str.substring(0,str.length-1);

	return str;
}

// This function takes two parameters and verifies that the first one follows
// specific rules for passwords and returns true if both parameters are identical
function ValidatePassword(pw1, pw2)
{
var invalid = " "; 			// Invalid character is a space
var minLength = 6; 			// Minimum length

	// check for a value in both fields.
	if (pw1 == '' || pw2 == '')
	{
		alert('Please enter your password twice.');
		return false;
	}
	// check for minimum length

	if (pw1.length < minLength)
	{
		window.alert('Your password must be at least ' + minLength + ' characters long. Try again.');
		return false;
	}
	// check for spaces
	if (pw1.indexOf(invalid) > -1)
	{
		window.alert("Sorry, spaces are not allowed in passwords.");
		return false;
	}
	if (pw1 !=  pw2)
	{
		window.alert("Your password and password confirmation do not match.");
		return false;
	}

	return true;
}

// check if string passed is a validly formed email address
function ValidateEmailAddress(input, title, required)
{
// var regExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var regExp = /^\w+([\.-\\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;	// added '+' in username as per http://en.wikipedia.org/wiki/E-mail_address#Plus_.28or_Minus.29_addressing


	input = Trim(input);

	if (required && !input)
	{
		window.alert(title);
		return false;
	}
	else if (!input)
		return true;

	if (regExp.test(input))
		return true
	else
	{
		window.alert("Sorry, " + input + " is not a valid email address.");
		return false;
	}
}

// this is used for fields like Customer Name, Supplier Name. They can contain any character. They only can't be empty
function ValidateNotEmpty(input, message)
{
	input = Trim(input);

	if (!input)
	{
		window.alert(message);
		return false;
	}
	return true;
}

// checks if input string contains only alphabets
// input: string that has to be validated
// title: string that will be displayed in case of error
// required: (bool) can be empty or not
function ValidateAlpha(input, title, required)
{
var regExp = /[^a-zA-Z\.\s]/;

	input = Trim(input);

	if (required && !input)
	{
		window.alert(title + " must have a value.");
		return false;
	}
	else if (!input)
		return true;

 	if (regExp.test(input))
 	{
		window.alert("Sorry, " + title + " can contain only letters.");
		return false;
	}
	return true;
}

// checks if input string contains only digits
// input: string that has to be validated
// title: string that will be displayed in case of error
// required: (bool) can be empty or not
function ValidateInteger(input, title, required)
{
var regExp = /[^0-9]/;

	input = Trim(input);

	if (required && !input)
	{
		window.alert(title + " must have a value.");
		return false;
	}
	else if (!input)
		return true;

 	if (regExp.test(input))
 	{
		window.alert("Sorry, " + title + " is not a valid integer.");
		return false;
	}
	return true;
}

// checks if input is a number
// input: string that has to be validated
// title: string that will be displayed in case of error
// required: (bool) can be empty or not
function ValidateNumeric(input, title, required)
{
	input = Trim(input);

	if (required && !input)
	{
		window.alert(title + " must have a value.");
		return false;
	}
	else if (!input)
		return true;

	if (isNaN(input))
 	{
		window.alert("Sorry, " + title + " is not a valid number.");
		return false;
	}
	return true;
}

// checks if input string contains only alphabets and digits
// input: string that has to be validated
// title: field name that will be displayed in case of error
// required: (bool) true or false
// strict: (bool) if TRUE, no space or period allowed. if FALSE, we allow spaces and period.
// error_message: if this is not '', print this instead of default message
// fixed by aravind on 25/07/2009 - error_message doesn't have a default value
function ValidateAlphaNumeric(input, title, required, strict, error_message)
{
	var regExp;
	if (strict) regExp = /[^a-zA-Z0-9]/;
	else regExp = /[^a-zA-Z0-9\.\s]/;
	
	if(!error_message) error_message = '';
	input = Trim(input);

	if (required && !input)
	{
		window.alert(title + " must have a value.");
		return false;
	}
	else if (!input)
		return true;

 	if (regExp.test(input))
 	{
		if (error_message != '') 			
 			window.alert(error_message);
		else window.alert("Sorry, " + title + " can contain only letters.");
		return false;
	}
	return true;
}

// checks if input string contains only alphabets, digits, underscore or dash
// input: string that has to be validated
// title: field name that will be displayed in case of error
// required: (bool) true or false
// strict: (bool) if TRUE, no space or period allowed. if FALSE, we allow spaces and period.
// error_message: if this is not '', print this instead of default message
// fixed by aravind on 25/07/2009 - error_message doesn't have a default value
function ValidateAlphaNumericDash(input, title, required, strict, error_message)
{
	var regExp;
	if (strict) regExp = /[^a-zA-Z0-9_-]/;
	else regExp = /[^a-zA-Z0-9\.\s]/;			// note: no _- in this regExp because - was causing problems with range

	if(!error_message) error_message = '';
	input = Trim(input);

	if (required && !input)
	{
		window.alert(title + " must have a value.");
		return false;
	}
	else if (!input)
		return true;

 	if (regExp.test(input))
 	{
		if (error_message != '') 			
 			window.alert(error_message);
		else window.alert("Sorry, " + title + " can contain only letters.");
		return false;
	}
	return true;
}

// added by aravind on 05/08/2009
// validates international phone
// allows space, - , +, ()
// strPhone: phone number to be validated
var validWorldPhoneChars = "()- +";
var minDigitsInIPhoneNumber = 10;
function ValidateInterPhone(strPhone){
	var bracket = 3;
	var brchr = strPhone.indexOf("(");
								   
	strPhone = Trim(strPhone);
	if(strPhone.indexOf("+")>1) return false;
	if(strPhone.indexOf("-")!=-1) bracket = bracket+1;
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket) return false;
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")") return false;
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1) return false;
	s = stripCharsInBag(strPhone, validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// checks if input string could be a valid phone number
// input: string that has to be validated
// title: string that will be displayed in case of error
// required: (bool) can be empty or not
function ValidatePhone(input, title, required)
{
	var regExp = /[^0-9]/;

	input = Trim(input);

	if (required && !input)
	{
		window.alert(title + " must have a value.");
		return false;
	}
	else if (!input)
		return true;
		
	/*
 	if (input.substr(0, 1) == '+')
 		input = input.substr(1);
	
 	if (regExp.test(input))
 	{
		window.alert("Sorry, " + title + " can contain only digits and optionally start with a '+'");
		return false;
	}
	*/
	
	if(ValidateInterPhone(input) == false) {
		window.alert("Sorry, " + title + " can contain only digits with min character limit as " + minDigitsInIPhoneNumber + " and optionally start with a '+' or can have space between or '()'");
		return false;
	}
	return true;
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh =  "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1)
			returnString +=  c;
	}
	return returnString;
}

function daysInFebruary(year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n)
{
	for (var i = 1; i <= n; i++)
	{
		this[i] = 31;
		if (i == 4 || i == 6 || i == 9 || i == 11)
			this[i] = 30;
		if (i == 2)
			this[i] = 29;
	}
	return this
}

// input: date (i.e. 13/10/2004)
function ValidateDate(input, title, required)
{
var daysInMonth = DaysArray(12);
var pos1 = input.indexOf(dtCh);
var pos2 = input.indexOf(dtCh,pos1+1);
var strDay = input.substring(0,pos1);
var strMonth = input.substring(pos1+1,pos2);
var strYear = input.substring(pos2+1);

	if (required && !input)
	{
		window.alert(title + " must have a value.");
		return false;
	}
	else if (!input)				// nothing to validate
		return true;

	strYr = strYear;
	if (strDay.charAt(0) == "0" && strDay.length>1) strDay = strDay.substring(1)
	if (strMonth.charAt(0) == "0" && strMonth.length>1) strMonth = strMonth.substring(1)
	for (var i = 1; i <=  3; i++) {
		if (strYr.charAt(0) == "0" && strYr.length>1) strYr = strYr.substring(1)
	}
	month = parseInt(strMonth)
	day = parseInt(strDay)
	year = parseInt(strYr)

	if (pos1 == -1 || pos2 == -1)
	{
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month == 2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length !=  4 || year == 0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (input.indexOf(dtCh,pos2+1)!= -1 || isInteger(stripCharsInBag(input, dtCh)) == false){
		alert("Please enter a valid date")
		return false
	}
	return true;
}