//

// Form validation

//

function formValidator1(i1) {

	b1 = isEmpty(i1);

	if (b1) {

    	alert('The highlighted fields are required');

        return false;

    }

    return true;

}



function formValidator2(i1, i2) {

	var b1 = isEmpty(i1);

	var b2 = isEmpty(i2);

	if (b1 || b2) {

    	alert('The highlighted fields are required');

        return false;

    }

	return true;

}



function formValidator3(i1, i2, i3) {

	var b1 = isEmpty(i1);

	var b2 = isEmpty(i2);

	var b3 = isEmpty(i3);

	if (b1 || b2 || b3) {

    	alert('The highlighted fields are required');

        return false;

    }

	return true;

}





function formValidator4(i1, i2, i3, i4) {

	var b1 = isEmpty(i1);

	var b2 = isEmpty(i2);

	var b3 = isEmpty(i3);

	var b4 = isEmpty(i4);

	if (b1 || b2 || b3 || b4) {

    	alert('The highlighted fields are required');

        return false;

    }

    return true;

}



function formValidatorTextTextEmailPhone(i1, i2, i3, i4) {

	var b = formValidator4(i1, i2, i3, i4);

	if (b) {

		b = isValidEmail(i3);

		if (b) {

			b = isValidTelephone(i4);

		}

	}

	return b;

}



function isEmpty(i1) {

	var re = /\s/g;

    RegExp.multiline = true;

    var s = i1.value.replace(re, "");

    if (s.length == 0) {

		i1.style.background = 'Yellow';

    	return true;

 	} else {

		i1.style.background = 'White';

	}

   	return false;  

}



function isValidTelephone(i) {

	var b = true;

    var stripped = i.value.replace(/[\(\)\.\-\ ]/g, '');     

	i.style.background = 'White';

   if (i.value == "") {

        alert('You must enter a telephone number');

        i.style.background = 'Yellow';

		b = false;

    } else if (isNaN(parseInt(stripped))) {

       alert('The phone number contains illegal characters');

        i.style.background = 'Yellow';

		b = false;

    } else if (!(stripped.length == 10)) {

        alert('The phone number is the wrong length. Make sure you included an area code');

        i.style.background = 'Yellow';

		b = false;

    } 

    return b;

}



function isValidEmail(i) {

	var b = (i.value.indexOf(".") > 2) && (i.indexOf("@") > 0);

	if (b) {

		i.style.background = 'White';

	} else {

		alert('The e-mail address is invalid');

		i.style.background = 'Yellow';

	}

	return b;

}
