
/*
* FILE    : i_validations.js
* Author  : Madhu 
* PURPOSE :
*	Support routines for client side validations, for Text fields, date fields etc.,
*	(Cross-Browser support)
* 
*/

//
// Function to validate Date field
//
function isValidDate(dateStr) {
	
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("Date is not in a valid format.")
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
		return false
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
return true;  // date is valid
}


// function to see if an input value has been entered at all
//
function isEmpty(inputStr){
	if ( inputStr == "" || inputStr == null )
	{
		return true
	}
	return false
}
	
// function to determin if value is in acceptable range
//
function inRange(inputStr, lo, hi){
	var num = parseInt(inputStr, 10)
	if (num < lo || num > hi)
	{
		return false
	}
	return true
}
	
	
// Set focus and select field
//
function select(field){
	field.focus()
	field.select()
}	

// E-Mail Validations 
// Returns True/false
//
// Copyright (c) 2000 internet.com Corp. 
// http://www.webreference.com/js/
// By Tomer Shiran.
//
function isEmail(str) 
{
	if (window.RegExp) 
	{
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
		var reg1 = new RegExp(reg1str);
		var reg2 = new RegExp(reg2str);
		if (!reg1.test(str) && reg2.test(str)) 
		{
			return true;
		}
		return false;
	} 
	else 
	{
		if(str.indexOf("@") >= 0)
		return true;
		return false;
	}
}

//
// Trim function.
function trim(txtWord)
{
	var i, len;
	len=txtWord.length
	for (i=0;i<len;++i)
	{	
		if (txtWord.charCodeAt(0)==32)
			txtWord = txtWord.substr(1);
		else
			break;
	}
	if (txtWord=="")
		return "";
					
	len=txtWord.length;
	for (i=len;i>=0;--i)
	{
		if (txtWord.charCodeAt(txtWord.length-1)==32)
			txtWord = txtWord.substr(-txtWord.length+1, txtWord.length-2);
		else
			break;
	}
	return txtWord;					
}

// Validate for special characters
// field : string
// type  : integer ( 1:number, 2:Characters, 3:Alphanumerics, 4:UserID
//
function validateField(field, type) 
{
	var valid
	var ok = "yes"
	var temp
		
	switch(type)
	{
		// numeric only
		case 1:
			valid = "0123456789";	
			break;
		// chracters only	
		case 2:
			valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			break;
		// both characters and numeric	
		case 3:
			valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			break;
		// for user id	
		case 4:
			valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
			break;
	}
					
	for (var i=0; i<field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") 
	{
		return false
	}
	return true
}
