/*  ========================================================
Filename: jsfuncs.js
Called by:
Description/Notes: Javascript routines
Update History:
============================================================ */

//popup window routine takes 4 args - window name, url to load, width,height
function popwin(wname,url,w,h) {
    var sw = screen.width;
    var sh = screen.height;
    var left = (sw - w)/2;
    var top = (sh - h)/2;
    var siz = "width="+w+",height="+h+",top="+top+",left="+left+",screenX="+left+",screenY="+top
    siz += ",scrollbars,resizable";
    var wp=window.open(url, wname, siz);wp.focus();
}

function mailchk(emailStr) {
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; //removed _ from this pattern
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        //alert("Email address seems incorrect (check @ and .'s)");
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            //alert("The username contains invalid characters.");
            return false;
        }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            //alert("The domain name contains invalid characters.");
            return false;
        }
    }
    if (user.match(userPat)==null) {
        //alert("The username doesn't seem to be valid.");
        return false;
    }
    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;
    }
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
            //alert("The domain name does not seem to be valid.");
            return false;
        }
    }
    if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
        //alert("The address must end in a well-known domain or two letter " + "country.");
        return false;
    }
    if (len<2) {
        //alert("This address is missing a hostname!");
        return false;
    }
    return true; //address is ok
}

function fcheck() {
    var missing = ""; // List of fields not completed. If it's empty by the end of the test, all fields have been completed
    // Get form field values
    var name = document.getinfo.name.value.length;
    var company = document.getinfo.company.value.length;
    var address = document.getinfo.address.value.length;
    var postcode = document.getinfo.postcode.value.length;
    var password1 = document.getinfo.password1.value.length;
    var password2 = document.getinfo.password2.value.length;
    var email = document.getinfo.email.value.length;
    var pw1 = document.getinfo.password1.value;
    var pw2 = document.getinfo.password2.value;
    var eml = document.getinfo.email.value;

    // Test the form values
    if (name == 0) {missing += 'Name\n';}
    if (company == 0) {missing += 'Company\n';}
    if (address == 0) {missing += 'Address\n';}
    if (postcode == 0) {missing += 'Postcode\n';}
    if (password1 == 0) {missing += 'Password\n';}
    if (password2 == 0) {missing += 'Confirmation Password\n';}
    if (email == 0) {missing += 'Email\n';}

    // Either give an error message or submit the form
    if (missing.length) {
        var err = 'You must complete all the fields\n\nThe following fields have not been completed : \n\n'+missing;
        alert(err) ;
    }
    else if (pw1 != pw2) {var err = 'Password and Confirmation Password are different';alert(err);}
    else if (!mailchk(eml)) {var err = 'Invalid email address ' + eml;alert(err);}
    else {document.getinfo.submit();}
}


function acheck() {//mailform validation
    var pname = document.forms[0].pname.value;
    var email = document.forms[0].email.value;
    var note = document.forms[0].note.value;
    if (pname.length == 0) {alert('You must enter your name !');}
    else if (email.length == 0) {alert('You must enter your e-mail address !');}
    else if (!mailchk(email)) {var err = 'Invalid email address ' + email;alert(err);}
    else if (note.length == 0) {alert('What? No questions or comments!');}
    else {document.forms[0].submit();}
}