var alertText = "We did not get all the information we need from you.\n Please fill in the following field(s):\n";
var RadioText = "\nPlease ensure that you have selected\nan option for:\n";
var checkboxText = "\nPlease ensure that you have selected:\n";
var emailText= "\nPlease enter a valid email address";
var alertMessage='';
var FieldList = "";
var checkboxMessage="";
var RadioMessage="";
var emailMessage = "";
var HighLightColour = '#ffa2d5';
var keyword="_Compulsory";
var dot = "    - ";
function isValidEmail(emailAddress) 
{
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
};
function checkCompulsory(form)
{
    if (document.images)
    {
        for (i=0;
        i<form.length;
        i++)
        {
            var tempobj=form.elements[i];
            tempobj.style.backgroundColor="";
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString())
            {
                shortFieldName=tempobj.name.substring(0,tempobj.name.length-keyword.length);
                shortFieldName=shortFieldName.slice(5);
                shortFieldName=shortFieldName.replace(/_/g," ");
                if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==0))
                {
                    FieldList += dot + shortFieldName + "\n";
                    tempobj.style.backgroundColor=HighLightColour;
                } else if (tempobj.type=="checkbox"&&!tempobj.checked)
                {
                    tempobj.style.backgroundColor=HighLightColour;
                    if (checkboxMessage=='')
                    {checkboxMessage=checkboxText;
                    };
                    checkboxMessage += dot + shortFieldName+"\n";
                };
            };
        };
    };
};
function checkCompulsoryRBs(form)
{
    if (document.images)
    {
        var allRBs = new Array();
        var checkedRBs = new Array();
        for (i=0;
        i<form.length;
        i++)
        {
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString())
            {
                if (tempobj.type=="radio")
                {
                    allRBs.push(tempobj.name);
                    if (tempobj.checked)
                    {
                        checkedRBs.push(tempobj.name);
                    };
                };
            };
        };
        var alertRBs = new Array();
        var uniqueRBs = new Array();
        for (count=0;
        count<allRBs.length;
        count++)
        {
            var isIn = false;
            for (countb=0;
            countb<uniqueRBs.length;
            countb++)
            {if (allRBs[count] == uniqueRBs[countb])
                {isIn = true;
                };
                try
                {
                    var RB = eval("form." + uniqueRBs[countb]);
                    for (z=0;
                    z<RB.length;
                    z++)
                    {
                        RB[z].style.backgroundColor = "";
                    }
                } catch(err)
                { };
            };
            if (!isIn)
            { uniqueRBs.push(allRBs[count]);
            };
        };
        for (countc=0;
        countc<uniqueRBs.length;
        countc++)
        {
            var isInb = false;
            for (countd=0;
            countd<checkedRBs.length;
            countd++)
            {if (uniqueRBs[countc] == checkedRBs[countd])
                {isInb = true;
                };
            };
            if (!isInb)
            {alertRBs.push(uniqueRBs[countc]);
            };
        };
        for (counte=0;
        counte<alertRBs.length;
        counte++)
        {
            try
            {
                var RB = eval("form." + alertRBs[counte]);
                for (z=0;
                z<RB.length;
                z++)
                {
                    RB[z].style.backgroundColor = HighLightColour;
                }
            } catch(err)
            { };
            shortName=alertRBs[counte].substring(0,alertRBs[counte].length-keyword.length);
            shortName=shortName.slice(5);
            shortName=shortName.replace(/_/g," ");
            if (RadioMessage=='')
            {RadioMessage=RadioText;
            };
            RadioMessage += dot + shortName + "\n";
        };
    };
};
function ValidateForm()
{  
    if (alertMessage=='')
    {alertMessage = alertText;
    };
    checkCompulsory(document.contactForm);
    //checkCompulsoryRBs(document.contactForm);
    try
    {
        if (document.contactForm.form_email_compulsory.value != null)
        {
            if (document.contactForm.form_email_compulsory.value == "")
            {
            } else
            {
                validEmail = isValidEmail(document.contactForm.form_email_compulsory.value);
                if (validEmail == false)
                {
                    document.contactForm.form_email_compulsory.style.backgroundColor=HighLightColour;
                    emailMessage = emailText;
                };
            };
        };
    } catch(err)
    {   };
    try {
    if (FieldList != "" || emailMessage != "" || checkboxMessage != "" || RadioMessage != "")
    {
        alertMessage += FieldList + checkboxMessage + RadioMessage + emailMessage;
        FieldList = '';
        checkboxMessage = '';
        RadioMessage = '';
        emailMessage = '';
        alert(alertMessage);
        alertMessage='';
    } else
    {
     
        document.contactForm.submit();
    }; } catch(err) {   }
}; 
