
///////////////////////////////////////////
// validation.js //////////////////////////
///////////////////////////////////////////
// generic validation for various input
// fields in an HTML form
//
// Author: Craig Denis
// Company: Sonic Boom Creative Media
// Date: May 15/04
///////////////////////////////////////////


function validator(inputType, inputField, inputDescription) {
	var strError = "";
	switch(inputType) {
	// Email
	case 2 :
		if (!validateEmail(inputField.value)) {
			strError = inputDescription;
		}
		break;
	
	// Lists 
	case 3 :
		if (inputField.selectedIndex==0) {
			strError = inputDescription;
		}
		break;
		
	// Radio Buttons
	case 4 :
		if (!validateRadioButtons(inputField)) {
			strError = inputDescription;
		}
		break;
		
	// Text Fields  
	default :
		if (inputField.value==""){
			strError = inputDescription;
		}
		break;
	}
	
	return(strError)
}


function validateEmail(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 validateRadioButtons(radioField) {
	// set var radio_choice to false
	var radio_choice = false;
	
	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < radioField.length; counter++) {
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (radioField[counter].checked)
			radio_choice = true; 
	}
	return (radio_choice);
}

