﻿    var strErrorColor = '#ad0321';
    var strNormalColor = '#000000';
    var prefix = "ctl00_ContentPlaceHolder1_";
    function validateForm() {
        var blnError = false;
        if (document.getElementById(prefix + 'txtFirstName').value <= '' || document.getElementById(prefix + 'txtFirstName').value == "First Name:" || document.getElementById(prefix + 'txtFirstName').value.length <2) {
            document.getElementById(prefix + 'txtFirstName').style.color = strErrorColor;
            blnError = true;
        }
        else
            document.getElementById(prefix + 'txtFirstName').style.color = strNormalColor;

        if (document.getElementById(prefix + 'txtLastName').value <= '' || document.getElementById(prefix + 'txtLastName').value == "Surname:" || document.getElementById(prefix + 'txtLastName').value.length < 2) {
            document.getElementById(prefix + 'txtLastName').style.color = strErrorColor;
            blnError = true;
        }
        else
            document.getElementById(prefix + 'txtLastName').style.color = strNormalColor;

        if (document.getElementById(prefix + 'txtEmail').value <= '' || document.getElementById(prefix + 'txtEmail').value == "Email:") {
            document.getElementById(prefix + 'txtEmail').style.color = strErrorColor;
            blnError = true;
        }
        else {
            if (!isEmail(document.getElementById(prefix + 'txtEmail').value)) {
                document.getElementById(prefix + 'txtEmail').style.color = strErrorColor;
                blnError = true;
            }
            else
                document.getElementById(prefix + 'txtEmail').style.color = strNormalColor;
        }
        if (document.getElementById(prefix + 'txtContactPhone').value <= '' || document.getElementById(prefix + 'txtContactPhone').value == "Contact No.:") {
            document.getElementById(prefix + 'txtContactPhone').style.color = strErrorColor;
            blnError = true;
        }
        else {
            if (replaceAll(document.getElementById(prefix + 'txtContactPhone').value).search("^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$") == -1)
            {
               document.getElementById(prefix + 'txtContactPhone').style.color = strErrorColor;
               blnError = true;
            }
            else
                document.getElementById(prefix + 'txtContactPhone').style.color = strNormalColor;
        }

        if (document.getElementById(prefix + 'txtPostcode').value <= '' || document.getElementById(prefix + 'txtPostcode').value == "Postcode:") {
            document.getElementById(prefix + 'txtPostcode').style.color = strErrorColor;
            blnError = true;
        }
        else {
            document.getElementById(prefix + 'txtPostcode').style.color = strNormalColor;
        }
        if (document.getElementById(prefix + 'lstSuburb').value <= '') {
            document.getElementById(prefix + 'lstSuburb').style.color = strErrorColor;
            blnError = true;
        }
        else
            document.getElementById(prefix + 'lstSuburb').style.color = strNormalColor;

        if (!blnError) {
            document.getElementById('divError').style.visibility = 'hidden';
            return true;
        }
        else {
            document.getElementById('divError').style.visibility = 'visible';
            return false;
        }
    }

    function populate(obj, val) {
        if (obj.value.length <= 0)
            obj.value = val;
    }

    function clearField(obj, val) {
        if (obj.value == val) {
            obj.value = '';
        }
    }

    function changeFont(obj) {
        obj.style.color = strNormalColor;
    }

    function updateSuburbs(val) {
        var objSelect = document.getElementById(prefix + 'lstSuburb');
        if (val.length >= 4) {
            objSelect.length = 0;
            objSelect.disabled = true;
            var randomnumber = Math.floor(Math.random() * 99999999)
            var xmlObj = getXMLDocument('/ajax/suburb_lookup.aspx?input=' + val + '&rnd=' + randomnumber);
            if (xmlObj.childNodes.length > 0) {
                var nodes = xmlObj.getElementsByTagName("rs");
                if (nodes.length > 0) {
                    for (var i = 0; i < nodes.length; i++) {
                        var opt = document.createElement('option');
                        opt.value = nodes[i].childNodes[0].nodeValue + ' - ' + nodes[i].getAttribute('state');
                        opt.text = nodes[i].childNodes[0].nodeValue + ' - ' + nodes[i].getAttribute('state')

                        try {
                            objSelect.add(opt, null);
                        }
                        catch (ex) {
                            objSelect.add(opt); // IE only
                        }
                    }
                    objSelect.disabled = false;
                }
                else {
                    objSelect.length = 0;
                    var opt = document.createElement('option');
                    opt.value = '';
                    opt.text = 'No match found';
                    try {
                        objSelect.add(opt, null);
                    }
                    catch (ex) {
                        objSelect.add(opt); // IE only
                    }
                    objSelect.disabled = true;
                }
            }
            else {
                objSelect.length = 0;
                var opt = document.createElement('option');
                opt.value = '';
                opt.text = 'No match found';
                try {
                    objSelect.add(opt, null);
                }
                catch (ex) {
                    objSelect.add(opt); // IE only
                }
                objSelect.disabled = true;
            }
        }
        else {
            objSelect.length = 0;
            objSelect.length = 0;
            var opt = document.createElement('option');
            opt.value = '';
            opt.text = 'No match found';
            try {
                objSelect.add(opt, null);
            }
            catch (ex) {
                objSelect.add(opt); // IE only
            }
            objSelect.disabled = true;
        }
    }

    function isEmail(argvalue) {

        if (argvalue.indexOf(" ") != -1)
            return false;
        else if (argvalue.indexOf("@") == -1)
            return false;
        else if (argvalue.indexOf("@") == 0)
            return false;
        else if (argvalue.indexOf("@") == (argvalue.length - 1))
            return false;

        // arrayString = argvalue.split("@"); (works only in netscape3 and above.)
        var retSize = customSplit(argvalue, "@", "arrayString");

        if (arrayString[1].indexOf(".") == -1)
            return false;
        else if (arrayString[1].indexOf(".") == 0)
            return false;
        else if (arrayString[1].charAt(arrayString[1].length - 1) == ".") {
            return false;
        }

        return true;

    }

    function customSplit(strvalue, separator, arrayName) {
        var n = 0;

        if (separator.length != 0) {
            while (strvalue.indexOf(separator) != -1) {
                eval("arr" + n + " = strvalue.substring(0, strvalue.indexOf(separator));");
                strvalue = strvalue.substring(strvalue.indexOf(separator) + separator.length,
          strvalue.length + 1);
                n++;
            }
            eval("arr" + n + " = strvalue;");
            arraySize = n + 1;
        }
        else {
            for (var x = 0; x < strvalue.length; x++) {
                eval("arr" + n + " = \"" + strvalue.substring(x, x + 1) + "\";");
                n++;
            }
            arraySize = n;
        }

        eval(arrayName + " = new makeArray(arraySize);");

        for (var i = 0; i < arraySize; i++)
            eval(arrayName + "[" + i + "] = arr" + i + ";");

        return arraySize;
    }
    function makeArray(IntarrSize) {

        for (var n = 0; n < IntarrSize; n++)
            this[n] = "";

        return this;

    }

