// Declaring valid date character, minimum year and maximum year

var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var defaultEmptyOK=false;
var decimalPointDelimiter=".";


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isZero(s)
{   
	return ((s == null) || (s.length == 0) || (s == 0))
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isFieldEmpty(theField, s){   
	if (isEmpty(theField.value)) {
		alert(s)
		theField.focus()
	    theField.select()
	    return true
		}
    return false;
}

function isFieldZero(theField, s){   
	if (isZero(theField.value)) {
		alert(s)
		theField.focus()
	    theField.select()
	    return true
		}
    return false;
}

function isInteger(s){
	var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

	for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
		}
    return true;
}

function isFieldInteger(theField, s, emptyOK){   
	if (!isInteger(theField.value, emptyOK)) {
		alert(s)
		setTimeout(function () { theField.focus() }, 10);
	    theField.select()
		if (isFieldInteger.arguments.length >= 3)
			theField.value = isFieldInteger.arguments[3]
		return false
		}
    return true;
}

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
	    }
	return true;
}

function isFieldFloat(theField, s, emptyOK){   
	if (!isFloat(theField.value, emptyOK)) {
		alert(s)
		setTimeout(function () { theField.focus() }, 10);
	    theField.select()

        if (isFieldFloat.arguments.length >= 4)
			theField.value = isFieldFloat.arguments[3]
		
		return false
		}
    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 FormatDDMMYYYY2mySQL(dtStr){

	returnString = dtStr
	returnString = returnString.split("/")

	d=parseInt(returnString[0],10)
	m=parseInt(returnString[1],10)
	y=parseInt(returnString[2],10)

	returnString[0]=d
	returnString[1]=m
				
	if (d<10) returnString[0] = '0' + returnString[0]
	if (m<10) returnString[1] = '0' + returnString[1]

	if (y < minYear) y=y+2000
	returnString[2] = y

	returnString.reverse()
	returnString = returnString.join("-")

    return returnString;
}

function DateCompare(dtStr){

	strDate = dtStr.split("/")

	intDay   = parseInt(strDate[0],10)
	intMonth = parseInt(strDate[1],10)
	intYear	 = parseInt(strDate[2],10)

	if (intYear < minYear) intYear=intYear+2000
	var d = new Date()

	if (intYear > d.getFullYear()) return 1
	else if (intYear < d.getFullYear()) return -1
	else {
		if (intMonth > d.getMonth()+1) return 1
		else if (intMonth < d.getMonth()+1) return -1
		else {
			if (intDay > d.getDate()) return 1
			else if (intDay < d.getDate()) return -1
			else return 0
			}
		}
}

function DateCompareToOneMonthInFuture(dtStr){

	strDate = dtStr.split("/")

	intDay   = parseInt(strDate[0],10)
	intMonth = parseInt(strDate[1],10)
	intYear	 = parseInt(strDate[2],10)

	if (intYear < minYear) intYear=intYear+2000
	var d = new Date()
	d.setMonth(d.getMonth() + 1);

	if (intYear > d.getFullYear()) return 1
	else if (intYear < d.getFullYear()) return -1
	else {
		if (intMonth > d.getMonth()+1) return 1
		else if (intMonth < d.getMonth()+1) return -1
		else {
			if (intDay > d.getDate()) return 1
			else if (intDay < d.getDate()) return -1
			else return 0
			}
		}
}

function isDateOneMonthInFuture(dtStr){
    return DateCompareToOneMonthInFuture(dtStr) > 0;
}


function isFutureDate(dtStr){
    return DateCompare(dtStr) > 0;
}

function isPastDate(dtStr){
    return DateCompare(dtStr) < 0;
}

function isDate(dtStr){

	var daysInMonth = DaysArray(12)

	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=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,10)
	day=parseInt(strDay,10)
	year=parseInt(strYr,10)

	if (year < minYear) year=year+2000

	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (year<minYear || year>maxYear){
		alert("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){
		alert("Please enter a valid date")
		return false
	}
return true
}

function isFieldDate(theField, s, emptyOK){
    if (isEmpty(s)) return emptyOK;
	if (!isDate(theField.value)) {
		alert(s)
		theField.focus()
	    theField.select()
	    return false
		}
    return true;
}

function CompareDates(dtStr1, dtStr2){

	strDate1 = dtStr1.split("/")

	intDay1   = parseInt(strDate1[0],10)
	intMonth1 = parseInt(strDate1[1],10)
	intYear1  = parseInt(strDate1[2],10)

	strDate2 = dtStr2.split("/")

	intDay2   = parseInt(strDate2[0],10)
	intMonth2 = parseInt(strDate2[1],10)
	intYear2  = parseInt(strDate2[2],10)
	
	if (intYear1 < minYear) intYear1 = intYear1 + 2000
	if (intYear2 < minYear) intYear2 = intYear2 + 2000

	if (intYear1 > intYear2) return 1
	else if (intYear1 < intYear2) return -1
	else {
		if (intMonth1 > intMonth2) return 1
		else if (intMonth1 < intMonth2) return -1
		else {
			if (intDay1 > intDay2) return 1
			else if (intDay1 < intDay2) return -1
			else return 0
			}
		}
}


function isValidEmail(addr,man,db) {

if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
if (addr == '') return true;
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) alert('email address contains invalid characters');
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
}