// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var dtErrMsg = "";

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		dtErrMsg='The date format should be : mm/dd/yyyy'
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		dtErrMsg='Please enter a valid month.'
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		dtErrMsg='Please enter a valid day.'
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		dtErrMsg='Please enter a valid 4 digit year between '+minYear+' and '+maxYear
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		dtErrMsg='Please enter a valid date.'
		return false
	}
return true
}


function isYear(dtStr){
	var strYear=dtStr
	strYr=strYear
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	year=parseInt(strYr)
	if (strYear.length != 4 || year==0 || isInteger(dtStr)==false || year<minYear || year>maxYear){
		dtErrMsg='Please enter a valid 4 digit year between '+minYear+' and '+maxYear
		return false
	}
return true
}


function fnc_year_validate(field_check)
{
	if (field_check.value == "") {
		return true; }
	else {
		if (isYear(field_check.value)==false)
		{
		jAlert(dtErrMsg, 'Notice', function(r) {
		if (r == true) {
		    field_check.value = "";
		   	field_check.focus();
		    return false; }
		});
	    }
	    else
	       return true;
	}
}


function fnc_date_validate(field_check)
{
	if (field_check.value == "") {
		return true; }
	else {
		if (isDate(field_check.value)==false)
		{
		jAlert(dtErrMsg, 'Notice', function(r) {
		if (r == true) {
		    field_check.value = "";
		   	field_check.focus();
		    return false; }
		});
	    }
	    else
	       return true;
	}
}


function fnc_numeric_validate(field_check)
{
	m_fld_convert = parseFloat(field_check.value) ;
	m_fld_length  = (m_fld_convert.toFixed(2)).length ;

	if (isNaN(field_check.value) == true)
	{
	jAlert('Value must be numeric and cannot include comas, letters, or symbols.', 'Notice', function(r) {
	if (r == true) {
	    field_check.value = "";
	   	field_check.focus();
	    return false; }
	});
    }
    else
		if (m_fld_length > 10)
		{
		jAlert('Value length including decimal point and cents cannot be greater than 10 digits.', 'Notice', function(r) {
		if (r == true) {
		    field_check.value = "";
	   		field_check.focus();
		    return false; }
		});
	    }
	    else
		return true;
}


function fnc_email_validate(field_check)
{
m_Chk_At_Pos = field_check.value.indexOf("@")
m_Chk_Stop_Pos = field_check.value.lastIndexOf(".")
m_Email_Valid = true

//if (field_check.value == "") {m_Email_Valid = false}

if (m_Chk_At_Pos == -1 || m_Chk_Stop_Pos == -1) {m_Email_Valid = false}

if (m_Chk_Stop_Pos < m_Chk_At_Pos) {m_Email_Valid = false}

if (m_Chk_Stop_Pos - m_Chk_At_Pos == 1) {m_Email_Valid = false}

if (m_Email_Valid == false)
{
jAlert('Email address must be valid format containing the "@" at sign and "." period.', 'Notice', function(r) {
if (r == true) {
    field_check.value = "";
   	field_check.focus();
    return false; }
});
   }
   else
      return true;
}


function fnc_highlight()
{ 
  var elements = document.getElementsByTagName("input");
  for (i=0; i < elements.length; i++) { 
	if(elements[i].getAttribute('type')=="text" | elements[i].getAttribute('type')=="password"){ 
       elements[i].onfocus=function() {
         this.style.borderColor='#C00000'; 
         this.style.backgroundColor='#FFFFFF';
         this.style.color='#000000'; 
       }; 
       elements[i].onblur=function() {
         this.style.borderColor='#000000'; 
         this.style.backgroundColor='#FFFFFF';
         this.style.color='#000000'; 
      }; 
    } 
  } 
}


function fnc_stop_enter_key(evt) {
   var evt = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
   if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}

document.onkeypress = fnc_stop_enter_key;

