/* $Id: general.js,v 1.3 2001/12/20 16:11:04 mtorrenc Exp $

   Javascript functions for general eLawForum use

*/

// return true if string is numeric
function is_number(checkStr) {
    for (i = 0; i < checkStr.length && '0123456789'.indexOf(checkStr.charAt(i),0) >= 0; i++);
    return i >= checkStr.length;
}

// alert user to nonnumeric string and reset focus to object
function warn_no_integer(v) {
    if (!(rval = is_number(v.value))) {
       alert('Value must be a whole number with no punctuation');
       v.focus();
    }
    return rval;
}

function beforeCurrentDate(date) {
    currdate = new Date();

    month = date.getMonth();
    day   = date.getDate();
    year  = date.getYear();

    currmonth = currdate.getMonth();
    currday   = currdate.getDate();
    curryear  = currdate.getYear();

    if (year < curryear) {
	return true;
    }
    if (year > curryear) {
	return false;
    }
    if (month < currmonth) {
	return true;
    }
    if (month > currmonth) {
	return false;
    }
    if (day < currday) {
	return true;
    }
    return false;
}

function isDate (month, day, year) {
    if ((is_number(day) == false) || (is_number(year) == false)) {
	return false;
    }
    if (day < 1 || day > 31) { return false; }
    var month=month.toUpperCase();
    if (month=="") return false;
    if ((month=="APRIL" || month=="JUNE" || month=="SEPTEMBER" || month=="NOVEMBER") && (day == 31)) {
	return false; 
    }
    if (month == "FEBRUARY") {
 	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
	    if (day > 29) { return false; } 
	}
	else if (day > 28) { return false; }
    }
    if (year.length != 4) { return false; } 

    return true;
}

function to24Hours (time, ampm) {
    var idx = time.indexOf(":",0);
    var hour = time.substring(0,idx);
    var rest = time.substring(idx,time.length);

    if (hour == 12) hour = 0;

    if (ampm == "PM") hour = parseInt(hour) + 12;

    return (hour + rest);
}

function isDateDigits (month, day, year) {

    if ((is_number(day) == false) || (is_number(month)==false) || (is_number(year) == false)) {
	return false;
    }
    if (month < 1 || month > 12) { return false; }
    if (day < 1 || day > 31) { return false; }
    if ((month == 4 || month == 6 || month == 9 || month == 11) && (day == 31)) {
	return false; 
    }
    if (month == 2) {
 	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
	    if (day > 29) { return false; } 
	}
	else if (day > 28) { return false; }
    }
    if (year.length != 4) { return false; } 

    return true;
}

// taken from http://javascript.internet.com/forms/check-email.html
function checkemail(email) {
    /* The following pattern is used to check if the entered e-mail address
       fits the user@domain format.  It also is used to separate the username
       from the domain. */
    var emailPat=/^(.+)@(.+)$/ ;

    /* The following string represents the pattern for matching all special
       characters.  We don't want to allow special characters in the address. 
       These characters include ( ) < > @ , ; : \ " . [ ]    */
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]" ;

    /* The following string represents the range of characters allowed in a 
       username or domainname.  It really states which chars aren't allowed. */
    var validChars="\[^\\s" + specialChars + "\]" ;

    /* The following pattern applies if the "user" is a quoted string (in
       which case, there are no rules about which characters are allowed
       and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
       is a legal e-mail address. */
    var quotedUser="(\"[^\"]*\")" ;

    /* The following pattern applies for domains that are IP addresses,
       rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
       e-mail address. NOTE: The square brackets are required. */
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/ ;

    /* The following string represents an atom (basically a series of
       non-special characters.) */
    var atom=validChars + '+' ;

    /* The following string represents one word in the typical username.
       For example, in john.doe@somewhere.com, john and doe are words.
       Basically, a word is either an atom or quoted string. */
    var word="(" + atom + "|" + quotedUser + ")" ;

    // The following pattern describes the structure of the user
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$") ;

    /* The following pattern describes the structure of a normal symbolic
       domain, as opposed to ipDomainPat, shown above. */
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    	
    /* Finally, let's start trying to figure out if the supplied address is
       valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
       different pieces that are easy to analyze. */
    var matchArray=email.match(emailPat);
    if (matchArray==null) {
	/* Too many/few @'s or something; basically, this address doesn't
	   even fit the general mould of a valid e-mail address. */
	return false;
    }
    
    var user=matchArray[1];
    var domain=matchArray[2];

    // See if "user" is valid 
    if (user.match(userPat)==null) {
	// user is not valid
	return false;
    }

    /* if the e-mail address is at an IP address (as opposed to a symbolic
       host name) make sure the IP address is valid. */
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
	// this is an IP address
	for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
		return false;
	    }
	}
	return true;
    }
    
    // Domain is symbolic name
    var domainArray=domain.match(domainPat);
    if (domainArray==null) {
	return false;
    }
    
    /* domain name seems valid, but now make sure that it ends in a
       three-letter word (like com, edu, gov) or a two-letter word,
       representing country (uk, nl), and that there's a hostname preceding 
       the domain or country. */

    /* Now we need to break up the domain to get a count of how many atoms
       it consists of. */
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || 
	domArr[domArr.length-1].length>4) {
	// the address must end in a two letter or three letter word or four-letter word.
	return false;
    }
    
    // Make sure there's a host name preceding the domain.
    if (len<2) {
	return false;
    }
    
    // If we've gotten this far, everything's valid!
    return true;
}
    

function check(checkOK, checkStr) {
    for (var i = 0;  i < checkStr.length;  i++) {
	var ch = checkStr.charAt(i);
	for (var j = 0;  j < checkOK.length;  j++) {
	    if (ch == checkOK.charAt(j)) {
		break;
	    }
	}
	if (j == checkOK.length) {
	    return false;
	    break;
	}
    }
    return true;
}

/* Check function to strip all elements from a string except
   elements in another string */

function stripExcept(strOK, stripStr) {
    var newStr = "";
    for (var i = 0;  i < stripStr.length;  i++) {
	var ch = stripStr.charAt(i);
	for (var j = 0;  j < strOK.length;  j++) {
	    if (ch == strOK.charAt(j)) {
		newStr = newStr + ch;
	    } 
	}
    }
    return newStr;
}

function isNumberChar (InString)  {
    if(InString.length!=1)
	return (false);
    RefString="1234567890";
    if (RefString.indexOf (InString, 0)==-1) 
	return (false);
    return (true);
}

function isAlphabeticChar (InString)  {
    if(InString.length!=1) 
	return (false);
    InString=InString.toLowerCase ()
        RefString="abcdefghijklmnopqrstuvwxyz";
    if (RefString.indexOf (InString, 0)==-1) 
	return (false);
    return (true);
}

function stripChar(str1, ch1) {
    var newStr = "";
    var ch;
    for (var i=0; i<str1.length; i++) {
	ch = str1.charAt(i);
	if (ch != ch1) 
	    newStr = newStr + ch;
    }
    return (newStr);
}

function validDate(InString) {
    var curdate = new Date();
    var slash = InString.value.indexOf("../index.html",0);
    if (slash == 1 || slash == 2) {
	var month = InString.value.substring(0,slash);
    }
    else { return false; }

    var string2 = InString.value.substring((slash+1),(InString.value.length));
    slash = string2.indexOf("../index.html",0);
    if (slash == 1 || slash == 2) {
	var day = string2.substring(0,slash);
    }
    else { return false; }

    var year = string2.substring(slash+1,string2.length);

    var docdate = new Date(year, month, day);

    if (docdate <= curdate) {
	return false;
    }

    return true;
}

function compareDates (oldString, newString) {
    // Assumes dates are VALID MM/DD/YYYY format
    var slash = oldString.value.indexOf("../index.html",0);
    var oldmonth = oldString.value.substring(0,slash);

    var string2 = oldString.value.substring((slash+1),(oldString.value.length));
    slash = string2.indexOf("../index.html",0);
    var oldday = string2.substring(0,slash);
    var oldyear = string2.substring(slash+1,string2.length);

    var slash = newString.value.indexOf("../index.html",0);
    var newmonth = newString.value.substring(0,slash);

    var string2 = newString.value.substring((slash+1),(newString.value.length));
    slash = string2.indexOf("../index.html",0);
    var newday = string2.substring(0,slash);
    var newyear = string2.substring(slash+1,string2.length);

    var olddate = new Date(oldyear, oldmonth, oldday);
    var newdate = new Date(newyear, newmonth, newday);

    if (newdate <= olddate) {
	return false;
    }

    return true;
}

function isDateString (InString) {
    var RefString = "1234567890";

    var slash = InString.value.indexOf("../index.html",0);
    if (slash == 1 || slash == 2) {
	var month = InString.value.substring(0,slash);
    }
    else { return false; }

    var string2 = InString.value.substring((slash+1),(InString.value.length));
    slash = string2.indexOf("../index.html",0);
    if (slash == 1 || slash == 2) {
	var day = string2.substring(0,slash);
    }
    else { return false; }

    var year = string2.substring(slash+1,string2.length);

    if ((check(RefString,day) == false) || (check(RefString,month)==false) || (check(RefString,year) == false)) {
	return false;
    }
    if (month < 1 || month > 12) { return false; }
    if (day < 1 || day > 31) { return false; }
    if ((month == 4 || month == 6 || month == 9 || month == 11) && (day == 31)) {
	return false; 
    }
    if (month == 2) {
 	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
	    if (day > 29) { return false; } 
	}
	else if (day > 28) { return false; }
    }
    if (year.length != 4) { return false; } 

    return true;
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
