function trim(strn)
{ //ltrim /^\\s+/, rtrim /\\s+$/, trim /^\\s+|\\s+$/
  strn = strn.replace(/( + +)/g,' ');
  strn = strn.replace(/^\s+/,'');
  strn = strn.replace(/\s+$/,'');
  return strn;
}

function validString(object,rExpr,min,max,objectName,req)
{
 strn = object.value;
 re = new RegExp(rExpr);
 if (strn=="") 
    {
    if (req) { alert("You are required to input " + objectName); object.focus(); object.select(); return false; }
    else return true;
    }
 if (!re.test(strn)) { alert("Invalid " + objectName); object.focus(); object.select(); return false; }
 if ((strn.length<min) && (min>0)) 
    { alert(objectName + " must not be less than " + min + " chars!!"); object.focus(); object.select(); return false; }
 if ((strn.length>max) && (max>0))
    { alert(objectName + " must not be more than " + max + " chars!!"); object.focus(); object.select(); return false; }
    
 if (rExpr.indexOf("-()")>-1) //Phone number validation
 {
   
   if (strn.indexOf("--")>-1) { alert("Invalid " + objectName); object.focus(); object.select(); return false; }
   parsedPhone = strn.replace(/[\(\)\.\-]/g,'');
   if ((parsedPhone.length<min) && (min>0)) { alert(objectName + " must not be less than " + min + " chars!!"); object.focus(); object.select(); return false; }
   if ((parsedPhone.length>max) && (max>0)) { alert(objectName + " must not be more than " + max + " chars!!"); object.focus(); object.select(); return false; }   
   
 }
 return true; 
}

function checkDate(mm1,dd1,yyyy1)
{
  leapYear = false;
  
  mm = parseInt(trim(mm1));
  dd = parseInt(trim(dd1));
  yyyy = parseInt(trim(yyyy1));
  
  
  days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
 
  if (((yyyy%4==0) && (yyyy%100!=0)) || (yyyy%400==0)) leapYear = true;  

  
	  if (isNaN(mm)|| (mm<1) || (mm>12)) { alert("Invalid month"); return 1; }
	  maxDays = days[mm-1]; if (leapYear && mm==2) maxDays++;
	  if (isNaN(dd) || (dd<1) || (dd>maxDays)) { alert("Invalid day"); return 2; }  
	   if (isNaN(yyyy) || (yyyy<1900) || (yyyy>2999)) { alert("Invalid Year"); return 3; }
	  return 0;
  }


function validateChoice(object,msg)
{
  radioCount  = object.length + "";
  if (radioCount=="undefined")
  {
    if (object.checked) return true;
    object.focus();
  }
  else
  {
    for(i=0;i<object.length;i++)
    {
    if (object[i].checked) return true;
    }
    object[0].focus();   
  }
  alert(msg);  
  return false;
}