/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
			break;
		}
	}	
}

function isEmail(formElement, message) {
	formElement.value = trim(formElement.value);
	_isEmpty = false;

	var AtSym=formElement.value.indexOf('@');				
	var Period=formElement.value.lastIndexOf('.');		
	var Space=formElement.value.indexOf(' ');				
	var Length=formElement.value.length-1;
	var index = formElement.value.indexOf('@');
	var substr = formElement.value.substring(index+1);
   var index2 = substr.indexOf('@');
	var count=0;
	if (formElement.value == '') {
		_isEmpty = true;
		alert('Enter Email');
		formElement.focus();
	} else {
		if((AtSym<1)||(formElement.value.charAt(0)=='_')||(formElement.value.charAt(Length)=="_")||	//'@' can't be in first position
		(formElement.value.indexOf("_")==AtSym+1)||(formElement.value.charAt(AtSym-1)=="_")||
		(Period<=AtSym+1)||					//Must be atleast one valid char between '@' and '.'
		(Period==Length)||					//Must be atleast one valid char after '.'
		((Space>0) && (Space!=Length))||
		(index2 != -1))                       //No empty spaces permitted
		{
			_isEmpty = true;
			alert(message);
			formElement.focus();
		}
	}
	return _isEmpty;
}
function checkRadio (frmName, rbGroupName) {
 var radios = document[frmName].elements[rbGroupName];
 for (var i=0; i <radios.length; i++) {
  if (radios[i].checked) {
   return true;
  }
 }
 return false;
}

function isMatching(str1,str2,name)
{
 var retval=false;
 if (str1.value != str2.value)
 {
  alert(name);
  str2.focus();
  retval=true;
 }
 return retval;
}
function isEmptyCbox(str,name)
{
 var retval=false;
 if (str.checked == false)
 {
  alert(name);
  str.focus();
  retval=true;
 }
 return retval;
}


function isEmptyLbox(str,name)
{
 var retval=false;
 if (str.value=="0" || str.value=="")
 {
  alert(name);
  str.focus();
  retval=true;
 }
 return retval;
}

function isDatevalid(str,name){
	// Regular expression used to check if date is in correct format
//   var pattern = new RegExp((19|20)[0-9]{2}-(0|1)[0-9]-[0-3][0-9]);

   var pattern = /^\s*(\d{2,4})-(\d{1,2})\-(\d{1,2})\s*$/;;
   
   
   if(pattern.exec(str.value))
   {
      var date_array = str.value.split('-');
      var day = date_array[2];
      var month = date_array[1] - 1;
      var year = date_array[0];
      source_date = new Date(year,month,day);
      if(year != source_date.getFullYear()){
         alert(name);
		 str.focus();
         return true;
      }
      if(month != source_date.getMonth()){
          alert(name);
		 str.focus();
         return true;
      }
      if(day != source_date.getDate()){
          alert(name);
		 str.focus();
         return true;
      }
   }
   else{
       alert(name);
		 str.focus();
      return true;
   }
   return false;
}

function isDOBvalid(Day,Month,Year,name){
	var ArrDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if (Year.value%4==0)													
	{
		ArrDays[1]=29;			
	}
	if ( Day.value > ArrDays[Month.value-1])			
	{
			alert(name);
			Day.focus();
			return true;
	}
}


