// Glossary Function
function glossary(entry) {
    url = "glossary.html#" + entry
    window.open(url, "glossary", "height=280,width=600,scrollbars=yes,resizable=yes,status");
}


// JavaScript helper functions to perform common form validation tasks.
//
// Author: Filipe Fortes
function notEmpty(Field) {
    return (!(Field == ''));
}

function isNumeric(Field) {
    return ((notEmpty(Field)) && (!isNaN(parseInt(Field))));
}

function isInteger(Field) {
    return (parseInt(Field) == parseFloat(Field));
}

function isNegative(Field) {
    return ((isNumeric(Field)) && (parseInt(Field) < 0));
}

function isNonNegative(Field) {
    return ((isNumeric(Field)) && (parseInt(Field) >= 0));
}

function isPositive(Field) {
    return ((isNumeric(Field)) && (parseInt(Field) > 0));
}

function isNonNegativeInteger(Field) {
    return (isNumeric(Field) && isInteger(Field) && (parseInt(Field) >= 0));
}

function isPositiveInteger(Field) {
    return (isNumeric(Field) && isInteger(Field) && (parseInt(Field) > 0));
}

function isLarger(Field, a) {
    return (parseInt(Field) > parseInt(a));
}

function isLargerFloat(Field, a) {
    return (parseFloat(Field) > parseFloat(a));
}

function isLargerInteger(Field, a) {
    return ((parseInt(Field) > parseInt(a)) && isInteger(Field));
}

function isBetween(Field, a, b) {
    return (notEmpty(Field) && (!isNaN(parseFloat(Field))) && (parseFloat(Field) >= a) && (parseFloat(Field) <= b))
}

function isBetweenInteger(Field, a, b) {
    return (notEmpty(Field) && (!isNaN(parseFloat(Field))) && (parseFloat(Field) >= a) && (parseFloat(Field) <= b) && isInteger(Field))
}


function validate414() {
    if (!isNumeric(document.forms[0].xbar.value)) {
        alert("You must enter a valid number for xbar");
        document.forms[0].xbar.focus();
        return false;
    }

    if (!isNonNegative(parseFloat(document.forms[0].sigma.value))) {
        alert("You must enter a non-negative number for sigma");
        document.forms[0].sigma.focus();
        return false;
    }

    if (!isNumeric(document.forms[0].L.value) && !isNumeric(document.forms[0].U.value)) {
        alert("You must a higher and/or lower limit");
        document.forms[0].L.focus();
        return false;
    }

    if (isNumeric(document.forms[0].L.value) && isNumeric(document.forms[0].U.value) && !isLargerFloat(document.forms[0].U.value, document.forms[0].L.value)) {
        alert("You must enter an upper limit higher than the lower limit");
        document.forms[0].U.focus();
        return false;
    }

    return true;
}

function validaterunplot() {
    if (!isBetween(document.forms[0].shift.value, 0, 5)) {
        alert("You must enter a process shift between 0 and 5");
        document.forms[0].shift.focus();
        return false;
    }

    if (document.forms[0].rule[4].checked) {

        if (!isPositiveInteger(document.forms[0].k.value)) {
            alert("You must enter a positive integer for k");
            document.forms[0].k.focus();
            return false;
        }

        if (!isBetween(document.forms[0].l1.value, 0, 3)) {
            alert("You must enter a number between 0 and 3 for the lower limit");
            document.forms[0].l1.focus();
            return false;
        }

        if ((!isBetween(document.forms[0].u1.value, 0, 3)) && (!isLarger(document.forms[0].u1.value, document.forms[0].l1.value))) {
            alert("You must enter an upper limit between 0 and 3 and higher than the lower limit");
            document.forms[0].u1.focus();
            return false;
        }
    }

    return true;
}


function validatereliability() {
    if (!isPositiveInteger(document.forms[0].n.value)) {
        alert("You must enter a positive integer number of components (n)");
        document.forms[0].n.focus();
        return false;
    }

    if (!isBetween(document.forms[0].p.value, 0, 1)) {
        alert("You must enter a probability between 0 and 1");
        document.forms[0].p.focus();
        return false;
    }

    if (!isPositiveInteger(document.forms[0].k.value)) {
        alert("You must enter a positive integer run length (k)");
        document.forms[0].k.focus();
        return false;
    }

    if ((document.forms[0].system.value == "3") && (parseInt(document.forms[0].k.value) == 1)) {
        alert("You must enter a run length (k) of at least 2");
        document.forms[0].k.focus();
        return false;
    }

    return true;
}

function validategeo() {
    if (!isBetween(document.forms[0].p.value, 0, 1)) {
        alert("You must enter a probability between 0 and 1");
        document.forms[0].p.focus();
        return false;
    }

    if (!isPositiveInteger(document.forms[0].k.value)) {
        alert("You must enter a positive integer run length (k)");
        document.forms[0].k.focus();
        return false;
    }

    if (!isPositiveInteger(document.forms[0].last.value)) {
        alert("You must enter a positive integer waiting time");
        document.forms[0].last.focus();
        return false;
    }

    return true;
}

function validateswitching() {
    if (!isPositiveInteger(document.forms[0].n.value)) {
        alert("You must enter a positive integer sample size(n)");
        document.forms[0].n.focus();
        return false;
    }

    if (!isNonNegativeInteger(document.forms[0].c.value)) {
        alert("You must enter a non-negative integer Acceptance Limit (c)");
        document.forms[0].c.focus();
        return false;
    }

    if (document.forms[0].r) {
        if (!isLargerInteger(document.forms[0].r.value, document.forms[0].c.value)) {
            alert("You must enter an integer higher than the Acceptance Limit (c) for the Rejection Limit (r)");
            document.forms[0].r.focus();
            return false;
        }
    }

    if (!isBetween(document.forms[0].percent.value, 0, 100)) {
        alert("You must enter a number between 0 and 100 for the Percent Nonconforming (p)");
        document.forms[0].percent.focus();
        return false;
    }

if (document.forms[0].last) {
            if (!isPositiveInteger(document.forms[0].last.value)) {
            alert("You must enter a positive integer for the number of batches");
            document.forms[0].last.focus();
            return false;
        }
    }

    return true;
}

function validatecsp() {
    if (!isPositiveInteger(document.forms[0].i.value)) {
        alert("You must enter a positive integer Clearance Number (i)");
        document.forms[0].i.focus();
        return false;
    }

    if (!isBetween(document.forms[0].frac.value, 0, 1)) {
        alert("You must enter a sampling fraction between 0 and 1");
        document.forms[0].frac.focus();
        return false;
    }

    if (!isBetween(document.forms[0].p.value, 0, 1)) {
        alert("You must enter a non-conforming probability of between 0 and 1");
        document.forms[0].p.focus();
        return false;
    }

    if (!isPositiveInteger(document.forms[0].last.value)) {
        alert("You must enter a positive integer maximum number of items");
        document.forms[0].last.focus();
        return false;
    }
}

function validate_pcr_double() {
    if (isNaN(parseFloat(document.forms[0].USL.value))) {
        alert("USL must be a valid number");
        document.forms[0].USL.focus();
	return false;
    }

    if (isNaN(parseFloat(document.forms[0].LSL.value))) {
        alert("LSL must be a valid number");
        document.forms[0].LSL.focus();
        return false;    
    }

    if (isNaN(parseFloat(document.forms[0].sigma.value))) {
        alert("The standard deviation must be a valid number");
        document.forms[0].sigma.focus();
	return false;    
    }

    if (document.forms[0].mean.value) {
	if (isNaN(parseFloat(document.forms[0].mean.value))) {
        	alert("The mean must be a valid number");
	        document.forms[0].mean.focus();
		return false;    
	}
    }

    if (parseFloat(document.forms[0].USL.value) <= parseFloat(document.forms[0].LSL.value)) {
        alert("USL must be greater than LSL");
        document.forms[0].USL.focus();
        return false;
    }


    if (parseFloat(document.forms[0].sigma.value) <= 0) {
        alert("You must enter a positive value for the standard deviation");
        document.forms[0].sigma.focus();
        return false;
    }
}

function validate_pcr_upper() {

    if (isNaN(parseFloat(document.forms[1].u_USL.value))) {
        alert("USL must be a valid number");
        document.forms[1].u_USL.focus();
	return false;
    }

    if (isNaN(parseFloat(document.forms[1].u_mu.value))) {
        alert("The mean must be a valid number");
        document.forms[1].u_mu.focus();
        return false;    
    }

    if (isNaN(parseFloat(document.forms[1].u_sigma.value))) {
        alert("The standard deviation must be a valid number");
        document.forms[1].u_sigma.focus();
	return false;    
    }


    if ((parseFloat(document.forms[1].u_USL.value)) <= (parseFloat(document.forms[1].u_mu.value))) {
        alert("USL must be greater than the mean");
        document.forms[1].u_USL.focus();
        return false;
    }


    if (parseFloat(document.forms[1].u_sigma.value) <= 0) {
        alert("You must enter a positive value for the standard deviation");
        document.forms[1].u_sigma.focus();
        return false;
    }

}

function validate_pcr_lower() {

    if (isNaN(parseFloat(document.forms[2].l_LSL.value))) {
        alert("LSL must be a valid number");
        document.forms[2].l_LSL.focus();
	return false;
    }

    if (isNaN(parseFloat(document.forms[2].l_mu.value))) {
        alert("The mean must be a valid number");
        document.forms[2].l_mu.focus();
        return false;    
    }

    if (isNaN(parseFloat(document.forms[2].l_sigma.value))) {
        alert("The standard deviation must be a valid number");
        document.forms[2].l_sigma.focus();
	return false;    
    }

    if (parseFloat(document.forms[2].l_LSL.value) >= parseFloat(document.forms[2].l_mu.value)) {
        alert("LSL must be smaller than mean");
        document.forms[2].l_LSL.focus();
        return false;
    }


    if (parseFloat(document.forms[2].l_sigma.value) <= 0) {
        alert("You must enter a positive value for the standard deviation");
        document.forms[2].l_sigma.focus();
        return false;
    }
}

function validateemail() {
    emailStr = document.forms[0].email.value;

    /* 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=emailStr.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. */
        alert("Email address seems incorrect (check @ and .'s)")
        return false
    }
    var user=matchArray[1]
    var domain=matchArray[2]

    // See if "user" is valid
    if (user.match(userPat)==null) {
        // user is not valid
        alert("The username doesn't seem to be 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) {
                alert("Destination IP address is invalid!")
            return false
            }
        }
        return true
    }

    // Domain is symbolic name
    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
        alert("The domain name doesn't seem to be valid.")
        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>3) {
       // the address must end in a two letter or three letter word.
       alert("The address must end in a three-letter domain, or two letter country.")
       return false
    }

    // Make sure there's a host name preceding the domain.
    if (len<2) {
       var errStr="This address is missing a hostname!"
       alert(errStr)
       return false
    }

    // If we've gotten this far, everything's valid!
    return true;


}

function validate_cc() {


    if (document.forms[0].cc_type[0].checked || document.forms[0].cc_type[2].checked) {
        if (isNaN(parseFloat(document.forms[0].mean.value))) {
	    document.forms[0].mean.focus();
	    alert("Target mean (or an estimate for the mean) is required");
	    return false;
	}
    }

    if (isNaN(parseInt(document.forms[0].n.value)) || parseInt(document.forms[0].n.value) < 2) {
        alert("Sample size (n) must be a integer greater than or equal to 2");
        document.forms[0].n.focus();
        return false;    
    }

    if (document.forms[0].sd_option[0].checked) {
	if (isNaN(parseFloat(document.forms[0].sigma.value)) || parseFloat(document.forms[0].sigma.value) <= 0) {
	    document.forms[0].sigma.focus();
	    alert("Positive value required for sigma");
	    return false;
	}
    }

    if (document.forms[0].sd_option[1].checked) {
	if (isNaN(parseFloat(document.forms[0].S.value)) || parseFloat(document.forms[0].S.value) <= 0) {
	    document.forms[0].S.focus();
	    alert("Positive value required for S");
	    return false;
	}
    }

    if (document.forms[0].sd_option[2].checked) {
	if (isNaN(parseFloat(document.forms[0].R.value)) || parseFloat(document.forms[0].R.value) <= 0) {
	    document.forms[0].R.focus();
	    alert("Positive value required for R");
	    return false;
	}
    }

}
