﻿function isEmailAddr(s) {
    var rv = false
    if ((s == null) || (s.length == 0))
        rv = false;
    else {
        var reEmail = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        rv = reEmail.test(s)
    }
    if (rv) {
        return rv
    } else {
        return false
    }
}

var strRadioValue = "";
function validateRadioCheckBox(theRadioCheckBox){
	// set var radio_choice to false
	var radiocheckbox_choice = false;

	if(theRadioCheckBox){
		// Loop from zero to the one minus the number of radio button selections
		for (counter = 0; counter < theRadioCheckBox.length; counter++){
			// If a radio button has been selected it will return true
			// (If not it will return false)
			if (theRadioCheckBox[counter].checked){
				radiocheckbox_choice = true; 
				strRadioValue = theRadioCheckBox[counter].value;
			}
		}
		if(!radiocheckbox_choice){
			return (false);
		}
			return (true);
	}else{
		return true	
	}
}

function validateSelect(selectbox){
	var iSelect = selectbox.options[selectbox.selectedIndex].value ;
	if(iSelect == 0){
		return false;
	}else{
		return true;
	}
}

function returnObjById(id) {
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}

function testValidation(id) {
    theForm = returnObjById(id)
	
	if (theForm.food_allergies_textbox.value != "true" && theForm.vitamin_mineral_textbox.value != "true" && theForm.pet_textbox.value != "true") {
        alert("Please select a test in step 1")
        return false;
    }

    if (theForm.FirstName.value == "") {
        alert("Please enter your first name.");
        theForm.FirstName.focus();
        return (false);
    }

    if (theForm.Surname.value == "") {
        alert("Please enter your surname.");
        theForm.Surname.focus();
        return (false);
    }

    if (theForm.Street.value == "") {
        alert("Please enter your street name and number.");
        theForm.Street.focus();
        return (false);
    }

    if (theForm.Suburb.value == "") {
        alert("Please enter your suburb.");
        theForm.Suburb.focus();
        return (false);
    }

    if (theForm.City.value == "") {
        alert("Please enter your city.");
        theForm.City.focus();
        return (false);
    }

    if (theForm.Postcode.value == "") {
        alert("Please enter your postcode.");
        theForm.Postcode.focus();
        return (false);
    }

    if (theForm.Phone.value == "") {
        alert("Please enter a contact phone number.");
        theForm.Phone.focus();
        return (false);
    }

    if (theForm.Date_of_Birth.value == "") {
        alert("Please enter your date of birth (dd/mm/yy).");
        theForm.Date_of_Birth.focus();
        return (false);
    }

    if (theForm.Date_of_sample_provided.value == "") {
        alert("Please enter the date you took a hair sample (dd/mm/yy).");
        theForm.Date_of_sample_provided.focus();
        return (false);
    }
    
    if (!isEmailAddr(theForm.Email.value)) {
        alert("Please enter a complete email address.");
        theForm.Email.focus();
        return (false);
    }
	
	if (theForm.food_allergies_textbox.value == "true"
		&& !validateRadioCheckBox(theForm.Digestive) 
			&& !validateRadioCheckBox(theForm.Respiratory) 
				&& !validateRadioCheckBox(theForm.Skin)
					 && !validateRadioCheckBox(theForm.Genitourinary)
					 	&& !validateRadioCheckBox(theForm.Immune)
							&& !validateRadioCheckBox(theForm.Mental_Emotional)
								&& !validateRadioCheckBox(theForm.Musculoskeletal)
									&& theForm.Other_Musculoskeletal.value == "")
	{
        alert("Please enter at least one symptom");
        return (false);
    }
	
	if (theForm.vitamin_mineral_textbox.value == "true" && theForm.Supplements.value == ""){
		alert("Please enter supplements you are taking.");
		theForm.Supplements.focus();
        return (false);
	}

    if (theForm.payment.value == ""){
        alert("Please select your payment type in step 3");
        return (false);
    }

    return (true);
}

