/**
 * 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 mm/dd/yyyy or mm-dd-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 numeric values---------------

function isNumber(Obj)
{
if (Obj.value == '')	
{
//	alert("Please, fillup price");
//	return false;
}
 if (Obj.value!='')
	{
	var strPat=new RegExp("^(\\d{0,7})(\\.\\d{1,2})?$");
		if (Obj.value.match(strPat)==null)
		{
		Obj.value='';
	 	Obj.focus();		
		alert ("(7 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)
				 {
				 alert('Please fill numbers only');
				 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;
   }


//==============special char not allowed================

function spchar_check(ctl){
 var notvalid = "~!@#$%^&*()_+|\?/:;<>',. ";
 var string = ctl.value;
 var length = string.length;
 for(var i=0;i<=length;i++)
	{
		var sp_substring = string.substr(i,1);
		//alert(notvalid.indexOf(sp_substring));
		if(notvalid.indexOf(sp_substring) >= 1 )
		{
		alert("Special characters are not allowed");
		ctl.focus();		
		return false;
		}
	}
}

function spchar_check_withoutSpace(ctl){
 var notvalid = "~!@#$%^&*()_+|\?/:;<>,.";
 var string = ctl.value;
 var length = string.length;
 for(var i=0;i<=length;i++)
	{
		var sp_substring = string.substr(i,1);
		//alert(notvalid.indexOf(sp_substring));
		if(notvalid.indexOf(sp_substring) >= 1 )
		{
		alert("Special characters are not allowed");
		ctl.focus();		
		return false;
		}
	}
}

function spchar_check_withoutSpaceAndApos(ctl){
 var notvalid = "~!@#$%^&*()_+|\?/:;<>,.";
 var string = ctl.value;
 var length = string.length;
 for(var i=0;i<=length;i++)
	{
		var sp_substring = string.substr(i,1);
		//alert(notvalid.indexOf(sp_substring));
		if(notvalid.indexOf(sp_substring) >= 1 )
		{
		alert("Special characters are not allowed");
		ctl.focus();		
		return false;
		}
	}
}

//=============
//fun to check time with 24 hrs format

function IsTime(Obj)
{
if (Obj.value == '')	
{
	//alert("Please, fillup value");
	//return false;
}
 if (Obj.value!='')
	{
	//##:## OR ##
	var strPat=new RegExp("^([0-9]{1})?[0-9]{1}([':']{1}[0-5]{1}[0-9]{1})$");
	var temp=Obj.value;
		temp = temp.substring(0,2);

		if (temp > 24)
		{
			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;
			}
		}
	}
}


