// FUNCTION VALIDATES FOR ALPHA NUMERIC CARACTERS
//function trim(str)
//function ltrim(str)
//function rtrim(str)
//function isEmpty(str)


// Other functions
 function ltrim(str)
 {
        var whitespace = new String(" \t\r\n");
        var s = new String(str);

        if (whitespace.indexOf(s.charAt(0)) != -1) {

            var j=0, i = s.length;

            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                j++;

            s = s.substring(j, i);
        }
        return s;
}

function rtrim(str)
{
        var whitespace = new String(" \t\r\n");
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            var i = s.length - 1;       
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
              i--;
            s = s.substring(0, i+1);
        }
        return s;
}

function trim( str )
{
  return rtrim( ltrim(str) );
}

function isEmpty( str ){
	return ( trim(str) == "" );  
}


function CheckEMail(MailText)
{
var InValidEmail = false;
var NoOfChars = 0, MultipleAts = false, i=0, NoOfDots=0, NoOfAts=0;
var ch="", Msg=" ", chAt=0;

//  Check if email id is empty
NoOfChars = MailText.length;
if (NoOfChars == 0)
{
    InValidEmail = true;
    Msg = "E-mail cannot be Empty";
}
// check for each individual char
if (InValidEmail == false) 
{
for (i=0; i<NoOfChars && InValidEmail == false; i++)
{
    ch = MailText.charAt(i);
    if(ch == ".") 
    {
        NoOfDots += 1;
        if (MailText.charAt(i+1) == ".") //Check for continuously entered dots
        {
            Msg = "Invalid - Dots cannot appear continously in email";
            InValidEmail = true;
            break;
        }
    }
            
//Not to allow Multiple @s
    if(ch == "@")              
        NoOfAts +=1;
//Avoid any special characters
    if ((ch>="a" && ch <= "z") || (ch>="A" && ch <="Z"));
      else 
        if ((ch >="0" && ch<="9"));
          else 
         if ( (ch !="@") && (ch !="_") && (ch !=".") && (ch !="-"))
          {    
            Msg = "Invalid Character Set (Avoid Special Characters)  in email";
            InValidEmail = true;
         }
}
}
//Check last char is numeric
if (InValidEmail == false) 
{
if ((MailText.charAt(NoOfChars-1)>="0" &&  MailText.charAt(NoOfChars-1)<= "9"))
{
    Msg = "Invalid Email Id (Avoid numbers at the end)";
    InValidEmail = true;
}
}

if (InValidEmail == false) 
{
    if (NoOfDots <= 0 || NoOfAts <=0)   // . & @ don't exist
    {
        Msg = "In Valid Character Set (@ OR . doesn't exist)  in email";
        InValidEmail = true;
    }
    if (NoOfAts > 1)  // Check for multiple @s
    {
        Msg = "In Valid Character Set (Multiple @s)  in email";
        InValidEmail = true;
    }

}
// Placement of the dot and at symbol

if (InValidEmail == false) 
{
    // Check @ is entered as first / last character
    if((MailText.indexOf("@",0) ==0)||(MailText.lastIndexOf("@",NoOfChars-1) == NoOfChars-1))
    {
        Msg = "Check @ is entered as first OR last character  in email";
        InValidEmail = true;
    }
    // Check . is entered as first / last character
    if((MailText.indexOf(".",0) ==0)||(MailText.lastIndexOf(".",NoOfChars-1) == NoOfChars-1))
    {
        Msg = "Check . is entered as first OR last character  in email";
        InValidEmail = true;
    }
    // Check . is entered before and not next to @ 
    if((MailText.indexOf(".",0) - MailText.indexOf("@",0))==1)
    {
        Msg = "Check . is entered before @ and also next to each other  in email";
        InValidEmail = true;
    }
    // Check @ & . is entered next to each other
    chAt=MailText.indexOf("@",0);
    if (MailText.charAt(chAt+1) == ".")
    {
        Msg = "Check @ & . is entered next to each other  in email";
        InValidEmail = true;
    }

}

if(InValidEmail == true)
{
    alert (Msg);
    return false;
}
else
{
//        alert("Correct ID");
    return true;
}
    
}


function checkAllChoosen(fld) {
	var haschecked;
	haschecked = false;
	for (i = 0; i < fld.length; i++) {
		if (fld[i].checked) {
			haschecked = true; break;
		}	
	}	
	if (haschecked)	haschecked = true;
	else {
		alert('Please choose atleast one.');
		haschecked = false;
	}
	return haschecked;
}


function setFieldFocus(frm, obj_fld) {
	var obj_tmp;
	obj_tmp = eval (frm + '.' + obj_fld);
	obj_tmp.focus();
}



function isValidDate(selectedDay,selectedMonth,selectedYear)  // dd,mm,yy
{
         // Array holding the total no of days in a month
         mon=[31,28,31,30,31,30,31,31,30,31,30,31];
                    
//         selectedMonth=parseInt(document.Registration.Months.options[document.Registration.Months.selectedIndex].value);

         if (selectedMonth == 0 )
         {
            alert('Please select month ');
            return false;
         }

//        selectedDay=parseInt(document.Registration.day.options[document.Registration.day.selectedIndex].value);

        if (selectedDay == 0)
        {
             alert('Please select day.');
             return false;
        }

//        selectedYear = parseInt(document.Registration.year.options[document.Registration.year.selectedIndex].value);
        if (selectedYear == 0)
        {
          alert("Please select year ");
          return false;
        }

        if (selectedMonth == 2)
        {
            if ((selectedYear % 4 == 0 && selectedYear % 100 != 0) || (selectedYear % 400 == 0))
            {
                 mon[1]=29;
                 if (selectedDay > 29 )
                 {
                     alert('Invalid Day Entered \n' +'Year ' + selectedYear + ' is a leap year !!!');
                     mon[1]=28;
                     return false;
                  }
            }
       }

       if (selectedDay > mon[selectedMonth - 1])
       {
//           monthName = document.Registration.Months.options[document.Registration.Months.selectedIndex].text ;
//           alert('Invalid Day entered ..\n'+monthName + ' has ' + mon[selectedMonth - 1] + ' days !!');
			alert('Invalid Day entered ..	for the month');
           return false;
       }  
	return true;
}
