/***********************************************************************************
Name of the Component	: JavaScript Component.
Created By		: G.Sivakumar
Created On		: 30-10-2002
Modified By		: S.DHANASEKAR
Last Modified Date	: 13-11-2002
Description		:
			  This is a JavaScript Component file with which user(JSP,
  Servlet, HTML Developers) access all the methods. This component is used to validate
  HTML forms and do some JavaScript methods. This component reduces the workload
  of the developers. Developers can import this component to their program start
  using the methods. The component contains generic method which is normally used
  in all the projects.
***********************************************************************************/

/**
 * Declaration Global Variables.
 */
// Declaration of form variables
var formObj1, formObj2, formObj3;

//Declation of other variables
var alertMsg, elmentName, range1, range2;

/**
 *Method Name	: trim
 *@param	: String
 *@return	: String
 *Description	: This method is used to remove the blank spaces.
 */
function trim(InputString)
{
	// for left trim
	/*reg = /^(\s+)/;
	
	InputString = InputString.replace(reg,RegExp.$1,"");

	// for right trim
	reg = /(\s+)$/;
	InputString = InputString.replace(reg,RegExp.$2,"");
	display("trim ++> "+InputString)*/


	while(InputString.charAt(0)==' ')
	{
		InputString = InputString.substr(1);
	}
	while(InputString.charAt(InputString.length)==' ')
	{
		InputString = InputString.substr(0,InputString.length-1);
	}

	return InputString;
}

/**
 *Method Name	: isEmpty
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form element is empty or
 		  not. And displays error message inside the method along with
		  value given as second parameter, if any.
 */
function isEmpty(formObj, errorMessage)
{
	if(formObj.type=="select-one")
	{
		if(formObj.options[0].selected)
		{
		if(errorMessage)
		{
			display(errorMessage);
		}
		formObj.focus();
		return true;
		}
		else return false;
	}
	if(!formObj.value)
	{
		if(errorMessage)
		{
			display(errorMessage);
		}
		formObj.focus();
		return true;
	}
	else
	{
		return false;
	}
}


/**
 *Method Name	: isEmpty
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form element contains valid Number or
 		  not. And displays error message inside the method along with
		  value given as second parameter, if any.
 */
function isNumber(formObj, errorMessage)
{
	if(isNaN(formObj.value))
	{
		if(errorMessage)
		{
			display(errorMessage );
		}

		formObj.focus();
		return false;
	}
	else
	{
		return true;
	}

}
function isWholeNumber(formObj, errorMessage)
{
	if(isNaN(formObj.value))
	{
		if(errorMessage)
		{
			display(errorMessage );
		}

		formObj.focus();
		return false;
	}
	else
	{
		if((formObj.value.indexOf(".")>-1) || (formObj.value.indexOf("-")>-1))
		{
			display(errorMessage );
			formObj.focus();
			return false;
		}
		return true;
	}

}




/**
 *Method Name	: isValidName
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form element is
 		  proper name type. And displays error message inside
		  the method along with value given as second parameter, if any.
 */
function isValidName(nameObj, errorMessage)
{

	reg = /^[A-Z]+\.[A-Z\s]+$|^[A-Z]+[A-Z\s]+$/i;

	if(reg.test(nameObj.value))
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		nameObj.focus();
		return false;
	}

}
function isAlphaNumeric(nameObj, errorMessage)
{

	reg = /^[A-Z0-9]+$/i;

	if(reg.test(nameObj.value))
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		nameObj.focus();
		return false;
	}

}

function isLastName(nameObj, errorMessage)
{

	reg = /^[A-Z]+[A-Z']*[A-Z]*$/i;

	if(reg.test(nameObj.value))
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		nameObj.focus();
		return false;
	}

}


/**
 *Method Name	: isValidEmail
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form element is
 		  proper email address. And displays error message
		  inside the method along with value given as second parameter,
		  if any.
 */
function isValidEmail(emailObj, errorMessage)
{
	
		if((emailObj.value =='') || (emailObj.value.indexOf("@")==-1) || (emailObj.value.indexOf(".")==-1) )
		{
			alert(errorMessage)
			emailObj.focus()
			return false
		}
		
		for (var i = 1; i < emailObj.length; i++)
		{
			var ch = emailObj.value.substring(i, i + 1);
			if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '.') && (ch != '@') && (ch != '_') && (ch != '-'))
			{
			alert(errorMessage);
			emailObj.focus()
			return false;
			}
		}

	return true
}


/**
 *Method Name	: isValidPhone
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form element is
 		  proper phone number. And displays error message
		  inside the method along with value given as second parameter,
		  if any.
 */
function isValidPhone(phoneObj, errorMessage)
{

	reg = /^([0-9\s]+[\s]*[\-]*[\s]*)*[0-9]+$/i;

	if(reg.test(phoneObj.value))
	{

		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}

		phoneObj.focus();
		return false;
	}

}



/**
 *Method Name	: isValidDate
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form element is in
 		  proper date format. And displays error message inside the
		  method along with value given as second parameter, if any.
		  Date Format : DD-MM-YYYY
 */
function isValidDate(dateObj, errorMessage)
{


	var ret = true;
	var message = "";
	var dayArr = new Array(31 , 28 , 31 , 30 , 31 , 30 , 31, 31, 30, 31, 30, 31);
	reg = /^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{2,4}$/i;
	if(reg.test(dateObj.value))
	{
		var stra = dateObj.value.split("-");

		if(stra[0]<1 || stra[1]<1 || stra[2]<1)
		{
			ret = false;
		}
		if(stra[1]>12)
		{
			ret = false;
			message = " Invalid month ";
		}
		if((stra[2]%4)==0 && stra[1]==2 )
		{
			if(stra[0]>29)
			{
				ret = false;
				message = " Invalid Day ";

			}

		}
		if(dayArr[--stra[1]] < stra[0])
		{
			ret = false;
			message = " Invalid Day ";
		}

	}
	else
	{
		ret = false;
	}

	if(ret)
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage + " \n " + message);
		}

		dateObj.focus();
		return false;
	}

}



/**
 *Method Name	: isValidDate
 *@param	: Form Object, Form Object, Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form element is in
 		  proper date format. And displays error message inside the
		  method  along with value given as fourth parameter, if any.
		  Argument 1 : DD
		  Argument 2 : MM
		  Argument 3 : YYYY
*/
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isDate (tday,tmonth,tyear, errorMessage) {
day = tday.value;
month = tmonth.value;
year = tyear.value;
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
	alert('test ' + test);
	if(!(day == test.getDate()))
	{
		display("Please enter a proper Date in "+errorMessage);
		tday.focus();
		return false;
	}
	if (!(month == test.getMonth()))
	{
		display("Please enter a proper Month in "+errorMessage);
		tmonth.focus();
		return false;
	}
    if (!(y2k(test.getYear()) == year))
	{
		display("Please enter a proper Year in "+errorMessage);
		tyear.focus();
		return false;
	}
    return true;
}

function isValidDate1(dayFormObj,monthFormObj, yearFormObj, errorMessage)
{
	if(isDate(dayFormObj,monthFormObj, yearFormObj, errorMessage)==true)
	{
		return true;
	}
	else
	{
		return false;
	}

}

function dateOfBirth(day, month, year, errorMessage)
{
    var test = new Date();
	testValue = (parseInt(y2k(test.getYear()))*365)+((parseInt(test.getMonth())+1)*12)+parseInt(test.getDay());
	orgValue = (parseInt(y2k(year.value))*365)+(parseInt(month.value)*12)+parseInt(day.value);
    if ((testValue-orgValue) < (18*365))
	{
		display(errorMessage);
		day.focus();
		return false;
	}
	return true;
}

/**
 *Method Name	: isSelected
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether atleast one form element
 		  from form element group is selected. And displays error
		  message inside the method along with value given as second parameter
		  , if any.
 */
function isSelected(formObj, errorMessage)
{
	var flag = false;
	for( i=0;i<formObj.length;i++)
	{
		if(formObj[i].checked)
		{
			flag = true;
			break;
		}

	}
	if(flag)
	{

		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		return false;

	}
}


/**
 *Method Name	: isInRange
 *@param	: Form Object, int , int ,String
 *@return	: boolean
 *Description	: This method is used to check whether the form object is in the given
 		  range (specified by range parameters). And displays error
		  message inside the method along with value given as last parameter ,
                  if any.
 */
function isInRange(formObj, fromValue , toValue ,errorMessage)
{
	if(formObj.value >= fromValue && formObj.value <= toValue)
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
			formObj.focus();
		}
		return false;
	}
}


/**
 *Method Name	: isAlphaOnly
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form object is in alphabets.
 		  And displays error message inside the method along with value given
                  as second parameter , if any.
 */
function isAlphaOnly(formObj, errorMessage)
{
	reg = /^[A-Za-z ]+$/i;

	if(reg.test(formObj.value))
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		formObj.focus();
		return false;
	}

}


/**
 *Method Name	: copy
 *@param	: Form Object ,Form Object ,String
 *@return	: boolean
 *Description	: This method is used to copyvalue from one form object to another.
 		  And displays error message inside the method along with value given
                  as last parameter , if any.

 */

function copy(fromFormObj, toFormObj ,errorMessage)
{
	if(fromFormObj.selectedIndex==-1)
	{
		display(errorMessage);
		return false;
	}
	var iCount;
	for(iCount = 0;iCount<fromFormObj.options.length;iCount++)
	{
		if(fromFormObj.options[iCount].selected == true)
		{
			var text = fromFormObj.options[iCount].text;
			var val = fromFormObj.options[iCount].value;
			var iCount1 = 0;
			var present = false;
			if(toFormObj.options.length == 0)
			{
				objOpt = new Option(text,val);
				toFormObj.add(objOpt);
			}//if(toFormObj.options.length == 0)
			else
			{
				var val1 = val;
				var flag = true;
				for(var ICount = 0;ICount < toFormObj.options.length; ICount++)
				{

					if (toFormObj.options[ICount].value == val1)
					{
						flag= false;
						break;
					}
				}
				if(flag)
				{
					objOpt = new Option(text,val);
					toFormObj.add(objOpt);
				}//if(flag)
			}
		}//if(fromFormObj.options[iCount].selected == true)
	}//for(iCount = 0;iCount<fromFormObj.options.length;iCount++)

	return true;
}

/**
 *Method Name	: move
 *@param	: Form Object ,Form Object ,String
 *@return	: boolean
 *Description	: This method is used to move value from one form object to another.
 		  And displays error message inside the method along with value given
                  as last parameter , if any.

 */

function move(fromFormObj, toFormObj ,errorMessage)
{
	if(fromFormObj.selectedIndex==-1)
	{
		display(errorMessage);
		return false; 
	}
	var iCount;
	for(iCount = 0;iCount<fromFormObj.options.length;iCount++)
	{
		if(fromFormObj.options[iCount].selected == true)
		{	
			var text = fromFormObj.options[iCount].text;
			var val = fromFormObj.options[iCount].value;
			var iCount1 = 0;
			var present = false;
			if(toFormObj.options.length == 0)
			{
				objOpt = new Option(text,val);
				toFormObj.add(objOpt);				
				fromFormObj.remove(fromFormObj.selectedIndex);
			}//if(toFormObj.options.length == 0)
			else
			{
				var val1 = val;
				var flag = true;
				for(var ICount = 0;ICount < toFormObj.options.length; ICount++)
				{
	
					if (toFormObj.options[ICount].value == val1)
					{
						flag= false;
						break;
					}
				}
				if(flag)
				{
					objOpt = new Option(text,val);
					toFormObj.add(objOpt);					
					fromFormObj.remove(fromFormObj.selectedIndex);
				}//if(flag)
			}
		}//if(fromFormObj.options[iCount].selected == true)	
	}//for(iCount = 0;iCount<fromFormObj.options.length;iCount++)

	return true;
}


/**
 *Method Name	: clear
 *@param	: Form Object
 *@return	: void
 *Description	: This method is used to clear the form object
 */

function clearField(formObj)
{

	if(formObj.type == "text" || formObj.type == "textarea" || formObj.type == "select-one")
	{

		formObj.value="";
		return;
	}
	else if(formObj.type != "select-multiple")
	{
		formObj.checked = false;
		return;	
	}
	if(formObj.length)
	{
		for(k=0;k<formObj.length;k++)
		{
			if(formObj.type == "select-multiple")
			{
				formObj[k].selected = false;
			}
			else
			{
				formObj[k].checked = false;
			}
		}
	
	}

}

/**
 *Method Name	: clearAll
 *@param	: Form Object
 *@return	: void
 *Description	: This method is used to clear the form object
 */

function clearAll(formObj)
{
	display(formObj.elements.length);
	for(j=0;j<formObj.elements.length;j++)
	{	
		clearField(formObj.elements[j]);
	}
	
}

/**
 *Method Name	: refresh
 *@param	: void
 *@return	: void
 *Description	: This method is used to refresh the page
 */

function refresh()
{
	window.location.reload();
}


/**
 *Method Name	: isValidPin
 *@param	: Form Object, String
 *@return	: boolean
 *Description	: This method is used to check whether the form object is in proper
 		  pin code format. And displays error message inside the method
                  along with value given as second parameter , if any.
 */
function isValidPin(formObj, errorMessage)
{
	reg = /^([0-9]+[\s]*)*[0-9]+$/i;
	
	if(reg.test(formObj.value))
	{
		
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}

		formObj.focus();
		return false;
	}
}


/**
 *Method Name	: isEqual
 *@param	: Form Object, Form Object ,String
 *@return	: boolean
 *Description	: This method is used to check whether the two form objects are equal.
 		  And displays error message inside the method along with value given
                  as last parameter , if any.
 */
function isEqual(formObj1, formObj2, errorMessage)
{
	if(formObj1.value == formObj2.value)
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		return false;
	}
}


/**
 *Method Name	: isValidStartEndDate
 *@param	: Form Object, Form Object ,String
 *@return	: boolean
 *Description	: This method is used to check whether the form objects are in proper
 		  date format. And displays error message inside the method along
                  with value given as last parameter, if any.
		  Date Format : DD-MM-YYYY
 */
function isValidStartEndDate(startDate, endDate,errorMessage)
{
	
	if(!(isValidDate(startDate) && isValidDate(endDate) ) )
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		return false;
	}
	stra = startDate.value.split("-");
	dt1= new Date(stra[2] , --stra[1] , stra[0]);
	stra = endDate.value.split("-");
	dt2= new Date(stra[2] , --stra[1] , stra[0]);
	if(dt1<=dt2)
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		return false;
	}
	
}


/**
 *Method Name	: isValidStartEndDate
 *@param	: Form Object, Form Object, Form Object, Form Object, Form Object, Form Object,String
 *@return	: boolean
 *Description	: This method is used to check whether the form element is in
 		  proper date format. And displays error message inside the
		  method  along with value given as last parameter, if any.
		  Argument 1 : DD
		  Argument 2 : MM
		  Argument 3 : YYYY
 */
function isValidStartEndDate1(dayStartDate, monthStartDate, yearStartDate, dayEndDate , monthEndDate, yearEndDate, errorMessage)
{
	if(!(isValidDate1(dayStartDate, monthStartDate, yearStartDate) && isValidDate1(dayEndDate , monthEndDate, yearEndDate)))
	{
		if(errorMessage)
		{
			display(errorMessage);
		}
		return false;
	}

	dt1= new Date( yearStartDate.value, monthStartDate.value-1, dayStartDate.value);
	dt2= new Date( yearEndDate.value, monthEndDate.value-1, dayEndDate.value);
	if(dt1<=dt2)
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		return false;
	}
	
}

/**
 *Method Name	: selectAll
 *@param	: Form Object
 *@return	: boolean
 *Description	: This method is used to

 */
function selectAll(formObj)
{
	for(i=0;i<formObj.length;i++)
	{
		if(formObj.type=="select-multiple")
		{
			formObj[i].selected = true;
		}
		else
		{
			formObj[i].checked = true;
		}
		
	}
	return true;

}



/**
 *Method Name	: display
 *@param	: String
 *@return	: none
 *Description	: This method is used display the alert message to the users.
 */
function display(alertMsg)
{
	alert(alertMsg);
}

function lessthanToday(dayStartDate, monthStartDate, yearStartDate, errorMessage)
{
	dt1= new Date( yearStartDate.value, monthStartDate.value-1, dayStartDate.value);
	dt2= new Date();
	if(dt1<=dt2)
	{
		return true;
	}
	else
	{
		if(errorMessage)
		{
			display(errorMessage );
		}
		return false;
	}
	
}
