
function checkEmail (emailad) {

	var exclude=/[^@\-\.\_\w]|^[_@\.\-]|[\._\-.\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;
	
	if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1)){
		return false;
	}
	else {
		return true;
	}
}

function isNumber(inputStr) {
        for (var i = 0; i < inputStr.length; i++) {
            var oneChar = inputStr.substring(i, i + 1);
            if (oneChar < "0" || oneChar > "9") {
                return false;
            }
        }
        return true;
}

function checkErrors () {
	var f  = window.document.forms["generic"];
	var length = f.elements.length;
	var isOK = notChecked = true;
	var i = 0, j = 0, checks=0;
	
	for (i = 0; i < length - 1; i++) {
		itemName = f.elements [i].name;
		itemType = f.elements [i].type;
			
		// if hidden Just dont check
		if (itemType == 'hidden')
			continue;
		
		if (itemType == 'text' || itemType == 'textarea') {
			formName = itemName;
			formValue = f.elements [formName].value;
					
			if (f.elements [formName + "_Required"].value == "Yes") {
				if (formValue == '')  {
					alert ("The " + f.elements [formName + "_Name"].value + " field is required");
					isOK = false;
					f.elements [formName].focus ();
					break;
				} 
			}
			
			if (f.elements [formName + "_IsNumeric"].value == "Yes") {
				if (formValue != '')  {
				 if (!isNumber (formValue)) {
					alert (f.elements [formName + "_Name"].value + " should be a number");
					isOK = false;
					f.elements [formName].focus ();
					break;
				 } 
				}
			 }
			 
			 if (f.elements [formName + "_IsEmail"].value == "Yes") {
				if (formValue != '')  {
				 if (!checkEmail (formValue)) {
					alert (f.elements [formName + "_Name"].value + " should be a valid email address");
					isOK = false;
					f.elements [formName].focus ();
					break;
				 } 
				}
			 }
			
			//if (f.elements [formName + "_MinLength"].value != '') {
			//	if ((formValue.length) < (f.elements [formName + "_MinLength"].value))
			//	{
			//		alert ("The " + f.elements [formName + "_Name"].value + " field should be alteast " + (f.elements [formName + "_MinLength"].value) + " characters long.");
			//		isOK = false;
			//		f.elements [formName].focus ();
			//		break;
			//	} 
			//}
			 
		 }	
		 
		 if (itemType == 'checkbox')
        {
		   formName = itemName;
		   if (f.elements [formName + "_Required"].value == "Yes") {
			
			    do
			    {
			      if (f.elements[i].checked)
			      {
			         checks++;
			      }
			      i++;
			    } while(f.elements[i].name == formName);

			    --i; 
			    if (checks == 0) 
			    {
			      alert("You must Check " + f.elements [formName + "_Name"].value);
			      isOK = false;
			      break;
			    } 
           }
        }

		if (itemType == 'radio')
        {
           formName =  itemName;
           notChecked = true;
           if (f.elements [formName + "_Required"] != null && f.elements [formName + "_Required"].value == "Yes") {
			do
			{
			   if (f.elements[i].checked)
			      notChecked = false;
			   i++;
			} while (f.elements[i].name == formName);
			--i;
			if(notChecked)
			{
			  alert("You have to select " + f.elements [formName + "_Name"].value);
			  isOK = false;
			  break;
			} 
           }
        }
        
        
        
        if (itemType == 'select-one') {
		   formName = itemName;
		   if (f.elements [formName + "_Required"].value == "Yes") {
			if (f.elements[i].options[0].selected)
			{
			   alert ("You must select a " + f.elements [formName + "_Name"].value)
			   isOK = false;
			   break;
			}  
           }
		}
		
		if (itemType == 'file') {
		   formName = itemName;
		   formValue = f.elements [formName].value;
		   
		   if (f.elements [formName + "_Required"].value == "Yes") {
			if (formValue == '')
			{
			   alert ("You must attach your resume")
			   isOK = false;
			   break;
			}  
           }
		}
		
	}  		
	return isOK;
}

function replace(string,text,by) {
	// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}	

// ***************************** Date Validator **************************/
function CheckDate1(dateStr) {
	var err=0
	a=dateStr
	if (a.length > 10) err=1
	var totalSlash = dateStr.split ("/")
	var fSlash = dateStr.indexOf ('/');
	var sSlash = dateStr.indexOf ('/',fSlash+1)
	b = dateStr.substring (0,fSlash)
	d = dateStr.substring (fSlash+1,sSlash)
	f = parseInt (dateStr.substring (sSlash + 1,dateStr.length))
	
	if (totalSlash.length < 3) err = 1
	if (b<1 || b>12) err = 1
	if (d<1 || d>31) err = 1
	if (f<1900 || f == NaN) err = 1
	if (!(f >0)) err = 1
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31) err=1
	}
	
	if (b==2){
		var g=parseInt(f/4)
		if (isNaN(g)) {
			err=1
		}
		
		if (d>29) err=1
		if (d==29 && ((f/4)!=parseInt(f/4))) err=1
	}
	
	if (err==1) {
		return false;
	}
	
	return true;
}

//------------------------------------------------------------------------------------
// function: isProperDate
//           Function to tell whether the given date is valid or not
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function CheckDate(argDate) {
	var tmpDay = getDay(argDate)
	var tmpMon = getMonth(argDate)
	var tmpYear = getYear(argDate)

	return isProperDay(tmpDay, tmpMon, tmpYear) && isProperMonth(tmpMon) && isProperYear(tmpYear)
}

//------------------------------------------------------------------------------------
// function: isWhiteSpace
//           Function to check whether the given argument consists of charactes other
//           than a space and \t
//------------------------------------------------------------------------------------
function isWhiteSpace(argWhiteSpace) {
	argWs = argWhiteSpace.toString()
	
	for (var intI=0; intI < argWs.length; intI++)
		if (argWs.charAt(intI) != ' ' && argWs.charAt(intI) != '\t')
			return false
	
	return true
}

//------------------------------------------------------------------------------------
// function: isLeapYear
//           Function to tell, whether the given year is leap year or not
//------------------------------------------------------------------------------------
function isLeapYear(argYear) {
	return ((argYear % 4 == 0) && (argYear % 100 != 0)) || (argYear % 400 == 0) 
}

//------------------------------------------------------------------------------------
// function: daysInMonth
//           Function to return the maximum number of days in a given month of a
//           given year
//------------------------------------------------------------------------------------
function daysInMonth(argMonth, argYear) {
	switch (Number(argMonth)) {
		case 1:		// Jan
		case 3:		// Mar
		case 5:		// May
		case 7:		// Jul
		case 8:		// Aug
		case 10:		// Oct
		case 12:		// Dec
			return 31;
			break;
		
		case 4:		// Apr
		case 6:		// Jun
		case 9:		// Sep
		case 11:		// Nov
			return 30;
			break;
		
		case 2:		// Feb
			if (isLeapYear(argYear))
				return 29
			else
				return 28
			break;
		
		default:
			return 0;
	}
}

//------------------------------------------------------------------------------------
// function: getDateSeparator
//           Function to return the date separator
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getDateSeparator(argDate) {
	// Are there invalid separators?
	if ((argDate.indexOf('-') > 0) && (argDate.indexOf('/') > 0))
		return ' '

	if (argDate.indexOf('-') > 0)
		return '-'
	else
		if (argDate.indexOf('/') > 0)
			return '/'
		else
			return ' '
}

//------------------------------------------------------------------------------------
// function: getYear
//           Function to return the year part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getYear(argDate) {
	var dateSep = getDateSeparator(argDate)
	
	if (dateSep == ' ')
		return 0

	if(argDate.split(dateSep).length == 3)
		return argDate.split(dateSep)[2]
	else
		return 0
}

//------------------------------------------------------------------------------------
// function: getMonth
//           Function to return the month part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getMonth(argDate) {
	var dateSep = getDateSeparator(argDate)
	
	if (dateSep == ' ')
		return 0

	if(argDate.split(dateSep).length == 3)
		return argDate.split(dateSep)[0]
	else
		return 0
}

//------------------------------------------------------------------------------------
// function: getDay
//           Function to return the day part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getDay(argDate) {
	var dateSep = getDateSeparator(argDate)
	
	if (dateSep == ' ')
		return 0

	if(argDate.split(dateSep).length == 3)
		return argDate.split(dateSep)[1]
	else
		return 0
}

//------------------------------------------------------------------------------------
// function: isProperDay
//           Function to tell whether the given day of the given month is valid
//------------------------------------------------------------------------------------
function isProperDay(argDay, argMonth, argYear) {
	if ((isWhiteSpace(argDay)) || (argDay == 0))
		return false

	if ((argDay > 0) && (argDay < daysInMonth(argMonth, argYear) + 1))
		return true
	else 
		return false
}

//------------------------------------------------------------------------------------
// function: isProperMonth
//           Function to tell whether the given month is a valid one
//------------------------------------------------------------------------------------
function isProperMonth(argMonth) {
	if ((isWhiteSpace(argMonth)) || (argMonth == 0))
		return false
	
	if ((argMonth > 0) && (argMonth < 13))
		return true
	else
		return false
}

//------------------------------------------------------------------------------------
// function: isProperYear
//           Function to tell whether the given Year is a valid one
//------------------------------------------------------------------------------------
function isProperYear(argYear) {
	if ((isWhiteSpace(argYear)) || (argYear.toString().length > 4) || (argYear.toString().length == 3))
		return false
	
	switch (argYear.toString().length) {
		case 1:
			if (argYear >=0 && argYear < 10)
				return true
			else
				return false
			
		case 2:
			if (argYear >=0 && argYear < 100)
				return true
			else
				return false
			
		case 4:
			if (((argYear >=1900) || (argYear >=2000)) && ((argYear < 3000) || (argYear < 2000)))
				return true
			else
				return false
		
		default:
			return false
	}
}

