// A series of validation fundtions for checking a form in order of field entry
// Code by Business Improvement Services web development
// Copyright, all rights reserved

// Global variable ref. accepted terms
var accept = false;

function validatemainForm(form) {
    if (isNotEmpty(form.name)) {
       if (isNotEmpty(form.email)) {
          if (isEMailAddr(form.email)) {
 	        if (isNotEmpty(form.subject)) {
     	      if (isNotEmpty(form.enquiry)) {
          	    return true;
          	 }        
     	   }
          }   
       }
    }
    return false;
}

// validate that the user accepted the terms and conditions other than default
function isaccepted(elem) {
	if (elem.checked) {
		// alert ("Thank you for accepting our terms and conditions");
		accept = true;
		}
	else {
    	alert("You must accept our terms and conditions prior to submitting the form.\r\n Please click the tick box on the form to indicate that you have read\r\n and accept our terms and conditions.\r\n\r\n Thank you\r\n\r\n Aslar");
    	setTimeout("focusElement('checkit', '" + elem.name + "')", 0);    
    }
	return accept;
}

// validate that the user accepted the terms and conditions other than default
function isValidRadio(elem) {
    var valid = false;
	if (elem.checked) return true;
    alert("You must accept our terms and conditions prior to submitting the form.\r\n Please click the tick box on the form to indicate that you have read\r\n and accept our terms and conditions.\r\n\r\n Thank you\r\n\r\n Aslar");
    setTimeout("focusElement('checkit', '" + elem.name + "')", 0);    
    return false;
}

// validate that the user made a selection other than default
function isChosen(elem) {
    if (elem.selectedIndex == 0) {
        alert("Please make a choice from the list.");
        setTimeout("focusElement('checkit', '" + elem.name + "')", 0);    
        return false;
    } else {
        return true;
    }
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
    var str = elem.value;
//    alert("str is: " + str);
    if(str == null || str.length == 0) {
        alert("Please fill in: \"" + elem.name + "\", which is required information.");
        setTimeout("focusElement('checkit', '" + elem.name + "')", 0);        
        return false;
    } else {
        return true;
    }
}

// validates that the entry is formatted as an email address
function isEMailAddr(elem) {
    var str = elem.value;
    str = str.toLowerCase( );
    if (str.indexOf("@") > 1) {
        var addr = 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) {
            alert("Verify the domain portion of the email address.");
            setTimeout("focusElement('checkit', '" + elem.name + "')", 0);
            return false;
        }
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) {
            oneChar = addr.charAt(i).charCodeAt(0);
            // dot or hyphen not allowed in first position; dot in last
            if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
                (i == addr.length - 1 && oneChar == 46)) {
                alert("Verify the user name portion of the email address.");
                setTimeout("focusElement('checkit', '" + elem.name + "')", 0);
                return false;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Verify the user name portion of the email address.");
                setTimeout("focusElement('checkit', '" + elem.name + "')", 0);
                return false;
            }
        }
        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)) {
                alert("Verify the domain portion of the email address.");
                setTimeout("focusElement('checkit', '" + elem.name + "')", 0);
                return false;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Verify the domain portion of the email address.");
                setTimeout("focusElement('checkit', '" + elem.name + "')", 0);
                return false;
            }
        }
        return true;
    }
    alert("The email address may not be formatted correctly. Please verify.");
    setTimeout("focusElement('checkit', '" + elem.name + "')", 0);
    return false;
}

// validates that the entry is a telephone number -  allow digits and brackets with a plus sign
function isNumber(elem) {
    var str = elem.value;
    var oneDecimal = false;
    var oneChar = 0;
    // make sure value hasn't cast to a number data type
    str = str.toString( );
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
        
        // OK for plus sign or brackets
        // OK for one decimal point
        // characters outside of 0 through 9 not OK
        if ((oneChar < 48 || oneChar > 57) && oneChar !== 32 && oneChar !== 40 && oneChar !== 41 && oneChar !== 43 && oneChar !== 45) {
            alert("Enter only a valid telephone number.");
            setTimeout("focusElement('checkit', '" + elem.name + "')", 0);            
            return false;
        }
    }
    return true;
}
   
// validates that the entry is positive digits only
function isDigits(elem) {
    var str = elem.value;
    var oneDecimal = false;
    var oneChar = 0;
    // make sure value hasn't cast to a number data type
    str = str.toString( );
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
        
        // OK for plus sign or brackets
        // OK for one decimal point
        // characters outside of 0 through 9 not OK
        if (oneChar < 48 || oneChar > 57) {
            alert("Enter only digits for the number of people in your group.");
            setTimeout("focusElement('checkit', '" + elem.name + "')", 0);                        
            return false;
        }
    }
    return true;
}
   
   
function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus( );
    elem.select( );
}