// File:    avaUtils.js
// Purpose: JavaScript routines for Accident Victims Association / Lawfinders website
// Author:  Jim Gillespie
// Date:    15 January 2007, modified 29 March 2011

function showEmail(strTitle) {
    var name = "admin";
    var host = "accidentvictims.com.au";
    document.write('<a href="mailto:' + name + '@' + host + '"' + ' title=" ' + strTitle + ' ">' + name + '@' + host + '</a>');
}

function openAboutPrint() {
		window.open("AboutPrinting.php","openAboutPrint","width=590,height=365,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,border=0,screenX=100,left=100,screenY=30,top=60");
}

function printPage() {
		window.print();
}

function handleEnter (field, event) {   // prevents the Enter key submitting a form
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
			break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
}

function validateForm() {
		var vld = true;
    var msg = "";
    var txt = "";
    var cnt = 0;
    if ( document.ReportForm.name.value == "" ) {
        msg = msg + "Please enter your full name\n\n";
        cnt++;
        vld = false;
    }
    // Routine to check valid phone number
    var phoneMsg = checkPhone (document.ReportForm.phone.value);
    if (phoneMsg != "") {
    	msg = msg + phoneMsg;
        cnt++;
        vld = false;
    }
    // Routine to check for valid email address    
    var emailMsg = checkEmail (document.ReportForm.email.value);
    if (emailMsg != "") {
    	msg = msg + emailMsg;
        cnt++;
        vld = false;
    }
    if ( ( document.ReportForm.represented[0].checked == false ) && ( document.ReportForm.represented[1].checked == false ) ) {
        msg = msg + "Please indicate if you are represented: Check Yes or No\n\n";
        cnt++;
        vld = false;
    }
    if (vld == false) {
    	if (cnt == 1)      { txt = "\nThere is one error in the Accident Report Form                \n\n\n"; }
        else if (cnt == 2) { txt = "\nThere are two errors in the Accident Report Form              \n\n\n"; }
        else if (cnt == 3) { txt = "\nThere are three errors in the Accident Report Form            \n\n\n"; }
        else               { txt = "\nThere are four errors in the Accident Report Form             \n\n\n"; }

    	// Popup alert message indicating errors
        alert(txt + msg);
    }
    return vld;
}

function checkPhone (str) {
		var error = "";
    if (str == "") {
			error = "Please enter your contact phone or mobile number\n\n";
        return error;
		}
    // characters outside of 0 through 9 not OK
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
		    if (oneChar < 48 || oneChar > 57) {
        	// allow (, ), space and -
        	if (oneChar == 32 || oneChar == 40 || oneChar == 41 || oneChar == 45) {
            	continue;
            } 
		    	else {
                error = "Please enter only numbers for the phone or mobile number\n\n";
    	    	return error;
            }
		    }
    }
    return "";
}

function checkEmail (str) {
		var error = "";
    str = str.toLowerCase();
    if (str == "") {
			error = "Please enter your email address\n\n";
        return error;
		}
    if (str.indexOf("@") > 1) {
        var address = str.substring(0, str.indexOf("@"));
        var domain  = str.substring(str.indexOf("@") + 1, str.length);
        // at least one top level domain required
        if (domain.indexOf(".") == -1) {
            error = "Please verify the domain portion of the email address\n\n";
            return error;
        }
        // parse address portion first, character by character
        for (var i = 0; i < address.length; i++) {
            oneChar = address.charAt(i).charCodeAt(0);
            // hyphen or dot not allowed in first position; dot in last position
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) || (i == address.length - 1 && oneChar == 46)) {
                error = "Please verify the user name portion of the email address\n\n";
                return error;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue; 
            } 
            else {
                error = "Please verify the user name portion of the email address\n\n";
                return error;
            }
        }
			// parse domain portion
        for (i = 0; i < domain.length; i++) {
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) || ((i == domain.length - 1 || i == domain.length - 2) && oneChar == 46)) {
				    error = "Please verify the domain portion of the email address\n\n";
                return error;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            }
            else {
                error = "Please verify the domain portion of the email address\n\n";
                return error;
            }
        }
    }
    else {
    	error = "Please verify your email address\n\n";
        return error;
    }
 		return "";
}
