//<!--
/*-----------------------------------------------
File Name: common.js
Functions: most used form validdtions routine
Created: 1/1/2002
Updated: 11/1/2004
Maintained by: Hui Zhou
--------------------------------------------------*/

/*
=============================================================
checkLength(objName,len) : CHECKS IF LENGTH IS NOT MORE THAN PERMITTED
=============================================================
*/

function checkLength(objName,len){
/*
   PURPOSE: Check the length from a string.
   IN: objName - the string from the form
	   len - lengtht is required	
*/
var strVal= objName.value;
strVal=strVal.replace(/^[\s]+/g,"");
strVal=strVal.replace(/[\s]+$/g,"");
//alert(":"+strVal+":");
if(strVal.length>len){
return false;
}
return true;
}
/*
=============================================================
isNumeric(str) : CHECKS If THE STRING IS NUMERIC ONLY
=============================================================
*/

function isNumeric(str){
/*
   PURPOSE: Check the numeric type from a string.
   IN: str- the string from the form
*/
var strVal = str;
var strDigits = "0123456789.";
for(var i = 0 ;i<strVal.length;i++){
	if(strDigits.indexOf(strVal.charAt(i))<0){
		return false;
	}
}
return true;
}

/*
=============================================================
isAlphaBets(str) : CHECKS If THE STRING IS ALPHBETS ONLY
=============================================================
*/

function isAlphaBets(str){
/*
   PURPOSE: Check the character type from a string.
   IN: str- the string from the form
*/
var strVal = str.toLowerCase();
//alert(strVal);
var strAlphas = "abcdefghijklmnopqrstuvwxyz-";
for(var i = 0 ;i<strVal.length;i++){
	if(strAlphas.indexOf(strVal.charAt(i))<0){
		return false;
	}
}
return true;
}
/*
=============================================================
changeSize(objName,preferedSize) : CHECKS SIZE OF LIST ELEMENT or Dropdown
=============================================================
*/
function changeSize(objName,preferedSize){
	if (objName.length < preferedSize) 
		objName.size = objName.length;	
	else  
		objName.size = preferedSize;	

}

/*
==========================================================================
checkSelection(objName) : IF NOTHING IS SELECTED SELECT FIRST ONE(ANY)
						  IF EVEN ONE ITEM IS SELECTED DESELECT FIRST(ANY)
==========================================================================
*/

function checkSelection(objName){
var l=0;
  for (var i=0; i<objName.length; i++)
  { 
	if (objName.options[i].selected)
    {	
		l+=1; }
  }
  if (l==0){
	objName.options[0].selected=true;
  }
  if(l>1){objName.options[0].selected=false;}
return true;
}
/*
==========================================================================
checkAll(objName) : Applying to the Dropdown box with All as the default value
==========================================================================
*/

function checkAll(objName){

var l=0;
var bAllSelected = false;
var bOtherSelected = false;

  for (var i=0; i<objName.length; i++){ 

	if (objName.options[i].selected==true && objName.options[i].value=="-99"){	
		bAllSelected = true; 
  	}else if( objName.options[i].selected) {
		bOtherSelected =true;
	}
   }


  if (bAllSelected && bOtherSelected){
	for (var i=0; i<objName.length; i++){ 
		objName.options[i].selected=false;
   	}

	alert("Please select either 'All' or other values from the list.");

	return false;	
  }
 
return true;
}
/////////////////////////////////////////////////////////////////
//CHECKS FOR ZERO LENGTH IF LENGTH IS ZERO RETURNS TRUE
function isZeroLength(objName){
	
	var strVal= objName.value;
	//alert(strVal);
	strVal=strVal.replace(/^[\s]+/g,"");
	strVal=strVal.replace(/[\s]+$/g,"");
	
	objName.value=strVal;
	
	if(strVal.length==0){
		return true;
	}
	
	return false;
	

}
///////////////////////////////////////////////////
//CHECKS IF LENGTH IS EXACT SAME AS WHAT REQUIRED
function checkExactLength(objName,len){
var strVal= objName.value;
strVal=strVal.replace(/^[\s]+/g,"");
strVal=strVal.replace(/[\s]+$/g,"");
//alert(":"+strVal+":");
if(strVal.length==len){
return true;
}
return false;
}
///////////////////////////////////////////////////
//Checks for valid Email address
function validateEmail(objEmail){

var strEmail = objEmail.value;

strEmail=strEmail.replace(/^[\s]+/g,"");
strEmail=strEmail.replace(/[\s]+$/g,"");
	
objEmail.value = strEmail;

//Minimum email address length 1@3.567
if(strEmail.length<7){
	return false;
}
//Check '@'
 if (strEmail.indexOf('@') < 1)
  {
    return false;
  } 
 //Check '.'
if (strEmail.indexOf('.') < 1){
    return (false);
  } 
 
 //check for 'a.@asd.xxx'  scenarios
 var strAdd = strEmail.substring(0,strEmail.indexOf('@'));

 if(strAdd.indexOf('.')+1 == strAdd.length){
	//alert(strAdd);
	return false;
 }
 
 //check for 'a.asda@.xxx' or 'asddas@.xxx'  scenarios
 var strDomain = strEmail.substring(strEmail.indexOf('@')+1,strEmail.length);
  if(strDomain.indexOf('.')<1){
	return false;
 }
 
 //return false;
 return true;
}
/////////////////////////////////////////////////////////////////
/* Javascript functions to remove the bad string for the textbox 
HZ @ERG
*/
 
function RemoveBad2(strTemp) { 
   strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,"");  
    return strTemp;
} 

///////////////////////////////////////////////////////////////////////
function RemoveBad(objName) { 
	var strTemp = objName.value;
    strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+/g,""); 
	objName.value = strTemp;
//   return strTemp;
} 

///////////////////////////////////////////////////////////////
function is9999(selection){
    if(selection =="-9999"){
      return true;
    }else{
     return false;
   }
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////
function clearList(list) 
    {
    var i = 0;
    var o = list.options;
    if(o.length<0) 
		i=0;
	else 
		i=o.length;
    for (i ; i >= 0; --i)
		o[i] = null;
    list.disabled = true;
    }
///////////////////////////////////////////////
function addToList(list, text_in, value_in)
    {
    var o = list.options;
    var nIdx;
    
   

	if (o.length < 0) //IE for Mac 4.5 sets length to -1 if list is empty
		nIdx = 0;
	else
		nIdx = o.length;	
	//alert(nIdx);

	o[nIdx] = new Option(text_in, value_in);
	list.disabled = false;
    }
//////////////////////////////////////////////
/*
functions to check approriate date format
*/
var err=-1;
function validateDate(dateVal,zeroOK,pastOK){

var strErrMsg = new Array(13);
var toDay = new Date();
var thisYear=toDay.getYear();
//Fix for NetScape
if(navigator.appName=="Netscape"){
thisYear=eval(thisYear+1900);
}
strErrMsg[0] = "Please enter proper date in 'MM/DD/YYYY' format";
strErrMsg[1] = "Please enter four digits for year in 'MM/DD/YYYY' format";
strErrMsg[2] = "Day is not valid in 'MM/DD/YYYY' format";
strErrMsg[3] = "Month is not valid in 'MM/DD/YYYY' format";
strErrMsg[4] = "Year is not valid in 'MM/DD/YYYY' format";
strErrMsg[5] = "Please enter Month between 01 and 12 in 'MM/DD/YYYY' format";
strErrMsg[6] = "Please enter day value between 01 and 31 in 'MM/DD/YYYY' format";
strErrMsg[7] = "Please enter day value between 01 and 30 in 'MM/DD/YYYY' format";
strErrMsg[8] = "Please enter day value between 01 and 28 in 'MM/DD/YYYY' format";
strErrMsg[9] = "Please enter day value between 01 and 29 in 'MM/DD/YYYY' format";
strErrMsg[10] = "Day value must be greater than Zero in 'MM/DD/YYYY' format";
strErrMsg[11] = "Date  must be a valid future date in 'MM/DD/YYYY' format";

strErrMsg[12] = "Date can not be beyond " + (eval(thisYear+1));

if(!chkdate(dateVal,zeroOK,pastOK)){
	alert(strErrMsg[err]);
	//objName.focus();
	return false;
}
return true;
}
///////////////////////////////////////////////////////////
function chkdate(dateVal,zeroOK,pastOK) {

var toDay=new Date();
toDay.setHours(00);
toDay.setMinutes(00);
toDay.setSeconds(00);
var thisYear =toDay.getYear();
var myBrowserName = navigator.appName;

if(myBrowserName=="Netscape"){
	//alert(navigator.appName);
	thisYear=eval(thisYear+1900);
}
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var bFound = false;

//var datefield = objName;

var strDigits="0123456789"
var strSepArr = new Array("-","/");

//strDate = datefield.value;
strDate = dateVal;

if(zeroOK==true && strDate.length==0){
return true;
}  
if (strDate.length <8 ||strDate.length > 10) {
	err = 0;
	return false;
}
for (i = 0; i< strSepArr.length; i++) {
if (strDate.indexOf(strSepArr[i]) != -1) {
strDateArray = strDate.split(strSepArr[i]);
if (strDateArray.length != 3) {
err = 0;
return false;
}
else {
strMonth = strDateArray[0];
strDay  = strDateArray[1];
strYear = strDateArray[2];
}
bFound = true;
   }
}
if (bFound == false) {
	err = 0;
	return false;
}
if (strYear.length != 4) {
	err = 1;
	return false;	
}
for(x=0;x<strDay.length;x++){
if(strDigits.indexOf(strDay.charAt(x))<0){
err = 2;
//alert(strDay);
return false;
}
}
for(x=0;x<strMonth.length;x++){
if(strDigits.indexOf(strMonth.charAt(x))<0){
err = 3;
return false;
}
}
for(x=0;x<strYear.length;x++){
if(strDigits.indexOf(strYear.charAt(x))<0){
err = 4;
//alert(err);
return false;
}
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}

intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
err = 3;
return false;
}
intYear = parseInt(strYear, 10);
//alert(intYear);
if (isNaN(intYear)){
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 10;
return false;
}

if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 8;
return false;
}
}
}
//alert("Date is :" + intYear + "/" + intMonth + "/" + intday);
var myDate=new Date(intYear,intMonth-1,intday);

//if (pastOK ==false && Date.parse(strDate)<(toDay)){
if(pastOK==false && Date.parse(myDate.toLocaleString())<Date.parse(toDay.toLocaleString())){
err=11;
return false;
}
//alert("" + intYear + ":" + eval(thisYear+1));

if(intYear>eval(thisYear+1)){
err=12;
return false;
}
return true;
}
//////////////////////////////////////////////////////////
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
///////////////////////////////////////////////////////////////////////
/*
==================================================================
LTrim(string) : Returns a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string to be trimed - LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from a string.
   IN: str - the string to be trimed - RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from the string.
   IN: str - the string to be trimed - Trim

   Return value: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

//-->