/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {
			
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(dateStr) {

		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat); // is the format ok?

		if (matchArray == null) {
		alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
		return false;
		}

		month = matchArray[1]; // p@rse date into variables
		day = matchArray[3];
		year = matchArray[5];
		

		if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
		}

		if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
		}

		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
		}

		if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn`t have " + day + " days!");
		return false;
		}
		}
		return true; // date is valid
}

function trim(inputString) {
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


//fun to check phone no with (###)###-#### format

function isPhoneRegExp(Obj)
{
if (Obj.value == '')	
{
	//alert("Please, fillup value");
	//return false;
}
 if (Obj.value!='')
	{
	var strPat=new RegExp("^['(']{1}[0-9]{3}[')']{1}[0-9]{3}-[0-9]{4}$");
		if (Obj.value.match(strPat)==null)
		{
		Obj.value='';
	 	Obj.focus();		
		alert ("Phone no should be in (###)###-#### format \n\n e.g. \n (123)456-7890")
		return false;
		}
		else
		{
		Obj.value=trimZero(Obj.value);
		return true;
		}
	}
}

//fun to check time with 12:00 or 12 format

function isTimeRegExp(Obj)
{
if (Obj.value == '')	
{
	//alert("Please, fillup value");
	//return false;
}
 if (Obj.value!='')
	{
	//##:## OR ##
	var strPat=new RegExp("^([0-1]{1})?[0-9]{1}([':']{1}[0-5]{1}[0-9]{1})?$");
	var temp=Obj.value;
		temp = temp.substring(0,2);

		if (temp > 12)
		{
			Obj.value='';
		 	Obj.focus();		
			alert ("Time should be in ##:## or #:## format \n\n e.g. \n\n 12:30 \n 1:30 \n 4:50 \n 1:05")
			return false;
		}
		else
		{
			if (Obj.value.match(strPat)==null)
			{
			Obj.value='';
		 	Obj.focus();		
			alert ("Time should be in ##:## or #:## format \n\n e.g. \n\n 12:30 \n 1:30 \n 4:50 \n 1:05")
			return false;
			}
			else
			{
			Obj.value=trimZero(Obj.value);
			return true;
			}
		}
	}
}


//----------Fun To check numeric values---------------

function isNumber(Obj)
{
if (Obj.value == '')	
{
	//alert("Please, fillup value");
	//return false;
}
 if (Obj.value!='')
	{
	var strPat=new RegExp("^(\\d{0,5})(\\.\\d{1,2})?$");
		if (Obj.value.match(strPat)==null)
		{
		Obj.value='';
	 	Obj.focus();		
		alert ("(5 integers.2 decimals)\nValid Formats are \n\nEx. 12345 \n\nEx. 12345.90 \n\nEx. 75 or 10.75 ")
		return false;
		}
		else
		{
		Obj.value=trimZero(Obj.value);
		return true;
		}
	}
}



//------------------------
function trimZero(str) 
{
  while (str.charAt(0) == '0')
    str = str.substring(1);
  return str;
}


//======================

function IsNumeric(strString)
		   //  check for valid numeric strings	
		   {
		   var strValidChars = "0123456789";
		   var strChar;
		   var blnResult = true;

		   if (strString.length == 0) return false;

		   //  test strString consists of valid characters listed above
		   for (i = 0; i < strString.length && blnResult == true; i++)
			  {
			  strChar = strString.charAt(i);
			  if (strValidChars.indexOf(strChar) == -1)
				 {
				 blnResult = false;
				 }
			  }
		   return blnResult;
		   }
//======================

//----------Fun To - numeric values---------------

function isMinusNumber(obj){
//alert(obj.value);
   var strValidChars = "-0123456789";
   var strChar;
   var blnResult = true;
   var strString;
   strString = obj.value;

   if (strString.length == 0) return 1;
   //  test strString consists of valid characters listed above
	   for (i = 0; i < strString.length && blnResult == true; i++)
		  {
		  strChar = strString.charAt(i);
		  if (strChar=="-"){
			if(i>0){
			obj.value="";
			return false;
			}	
		  }
		  if (strChar==0){
			obj.value=trimZero(obj.value);
                  }	
		  if (strValidChars.indexOf(strChar) == -1)
			 {
			 blnResult = false;
			 }
		  }
	   return blnResult;
   }

//---------------------------

//==============MG groudp=======

function isDateMM(objMM){
var objValue = trim(objMM.value)
	if(isNaN(objMM.value)){
		objMM.value='';
		objMM.focus();
		return false;
	}
	if (objValue.length == 0){
		return false;
	}
	if (objValue<1 || objValue>12){
		alert("Month Should be between 1 to 12");
		objMM.value='';
		objMM.focus();
		return false;
	}
}
function isDateDD(objMM,objDD){
var month = trim(objMM.value);
var day = trim(objDD.value);

	if(isNaN(objDD.value)){
		objDD.value='';
		objDD.focus();
		return false;
	}
	if (day.length == 0){
		return false;
	}
	if (day<1 || day>31){
		alert("Day Should be between 1 to 31");
		objDD.value='';
		objDD.focus();
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+ month +" doesn`t have 31 days!")
		objDD.value='';
		objDD.focus();
		return false;
	}
	if ((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && day==30) {
		alert("Month "+ month +" doesn`t have 30 days!")
		objDD.value='';
		objDD.focus();
		return false;
	}
}

function isDateYY(objYY,objMM,objDD){
var month = objMM.value;
var day = objDD.value;
var year = objYY.value;

	if(isNaN(year)){
		objYY.value='';
		objYY.focus();
		return false;
	}
	if (year.length == 0){
		return false;
	}
	if(year.length != objYY.maxLength){
		alert('Please, Enter in YYYY format. \n\n e.g. 2003');
		objYY.focus();
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn`t have " + day + " days!");
		objMM.value=='';
		objYY.value=='';
		objMM.focus();
		return false;
		}
	}
	
}

//---------------------------

function isPhone(obj){
	if(obj.value.length==0){
		return false;
	}
	if (IsNumeric(obj.value)==false){
		alert("Please enter valid value.");
		obj.value='';
		obj.focus();
	}
	if(obj.value.length != obj.maxLength){
		alert('Please, Enter Valid value.');
		obj.focus();
	}
}

function isEmail(obj){
	if(obj.value.length==0){
		return false;
	}
	if(echeck(obj.value)==false){
		obj.focus();
	}
}

function isDateMMDDYY(obj){
	if(obj.value==""){
		return false;
	}
	if(isDate(obj.value)==false){
		obj.value="";
		obj.focus();
		return false;
	}
}

function IsNumericOnBlur(obj){
	if(trim(obj.value)==""){
		return false;
	}
	if(IsNumeric(obj.value)==false){
		alert("Please, Numeric value");
		obj.value="";
		obj.focus();
		return false;
	}
	
}

