/*
function copyfirstname() {
    document.form1.Name.value =document.form1.field4.value;
}
*/

function copylastname() {
    document.form1.Name_1.value =document.form1.field5.value;
}

function clearme(obj,defaultval) {
    if (obj.value==defaultval) {
        obj.value="";
    }
}

/* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/) */

function isNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

/*
function isPhone(sText) {
    var ValidChars = "0123456789 ";
    var isPhone=true;
    var Char;
    for (i = 0; i < sText.length && isPhone == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            isPhone = false;
        }
    }
    return isPhone;
}
*/


function trim(string) {
    return string.replace(/(^\s+)|(\s+$)/g, "");
}

function echeck(str) {
    str = trim(str);
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)

    if (str.indexOf(at)==-1){
       return false
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false
    }
    if (str.indexOf(at,(lat+1))!=-1){
        return false
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false
    }
    if (str.indexOf(dot,(lat+2))==-1){
        return false
    }
    if (str.indexOf(" ")!=-1){
        return false
    }
    return true
}

function doCheck() {
    thisDoc = document.form1;
    var checkdone = false;
    if (document.form1.field3.options) {
        if (document.form1.field3.options[document.form1.field3.selectedIndex].value == "") {
            alert("Please select a session time");
            return false;
        }
    }
    if (document.form1.field2.options[document.form1.field2.selectedIndex].value == "") {
        alert("Please indicate whether you are a parent or a student");return false;
    }
    if (!echeck(thisDoc.Email1.value)) { 
        alert("Please enter a valid email address");
        return false; 
    }
    if (document.form1.State.options[document.form1.State.selectedIndex].value == "") {
        alert("Please select State");
        return false;
    }
    if (!isNumeric(thisDoc.Zip.value)) { 
        alert("Please enter a valid postcode");
        return false; 
    }
    if (!isNumeric(thisDoc.Homephone.value)) { 
        alert("Please enter a valid home phone without any spaces");
        return false; 
    }
    if (!isNumeric(thisDoc.Workphone.value)) { 
        alert("Please enter a valid mobile phone without any spaces");
        return false; 
    }
    if (!isNumeric(thisDoc.field8.value)) { 
        alert("Please enter a valid number value for the number of seats you want");
        return false; 
    }
    if (!isNumeric(thisDoc.field6.value)) { 
        alert("Please enter a valid number for the year you're studying in 2011");
        return false; 
    }
    if (document.form1.field9.value == "") {
        alert("Please enter where you heard about this site");
        return false;
    }
    if (document.form1.agree.checked == false) {
        alert ("Please tick the checbox to agree to terms");
        return false;
    }
    if (parseInt(thisDoc.field8.value)>7 || parseInt(thisDoc.field8.value)<=0){
        alert ("Number of seats shouldn't be more than 7");
        return false;
    } else if (thisDoc.field4.value == "" || 
               thisDoc.field4.value == "First name" || 
               thisDoc.field5.value == "Last name" || 
               thisDoc.Name.value == "First name" || 
               thisDoc.Name_1.value == "Last name" || 
               thisDoc.Email1.value == "" || 
               thisDoc.field5.value == "" ||
               thisDoc.Address1.value == "" || 
               thisDoc.City.value == ""  || 
               thisDoc.Zip.value == "" ||
               thisDoc.field6.value == ""  || 
               thisDoc.field8.value == "" ) {
        alert("All fields except student first name are required -- please fill out all fields before continuing.");
        return false;
    } else {
        return true;
    }
}

