// JavaScript Document
function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function isblank(s) {
	if ( s == null || s == "" ) return true;
	for ( var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '	') && (c != '')) return false;
	}
	return true;
}

function isNumeric(val) {
	if ( val.match(/^[0-9]+$/) ) return true;
	return false;
}

// check to see if input is a valid email address
function isEmailAddress(val)
{
	if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
	{
		return true;
	} else {
		return false;
	}	
}

function ValidateMe(f) {
	var msg = "";
	var empty_fields = "";
	var errors = "";
	var founderror = false;
	var hours = 0;
	
	// Now check all of the required fields
	// and while we are at it, let's trim all of the strings
	for ( var i = 0; i < f.length; i++ ) {
		var myelement = f.elements[i];
		myelement.value = TrimString(String(myelement.value));
		if ( myelement.required ) {
			if ( myelement.value == null || myelement.value == "" || isblank(myelement.value) ) {
				if ( !founderror ) { myelement.focus(); founderror=true; } // shift focus to first error field
				empty_fields += "\n\t" + myelement.name;
			}
		}
		if ( myelement.numeric ) {
			if ( !isNumeric(myelement.value) ) {
				if ( !founderror ) { myelement.focus(); founderror=true; } // shift focus to first error field
				errors += myelement.name + " must be numeric\n";
			}
		}
		if ( myelement.minimum ) {
			if ( Number(myelement.value) < myelement.minimum ) {
				if ( !founderror ) { myelement.focus(); founderror=true; } // shift focus to first error field
				errors += myelement.name + " must be greater or equal to " + myelement.minimum + "\n";
			}
		}
		if ( myelement.maximum ) {
			if ( Number(myelement.value) > myelement.maximum ) {
				if ( !founderror ) { myelement.focus(); founderror=true; } // shift focus to first error field
				errors += myelement.name + " must be less than or equal to " + myelement.maximum + "\n";
			}
		}
		if ( myelement.isemail ) {
			if ( !isEmailAddress(myelement.value) ) {
				if ( !founderror ) { myelement.focus(); founderror=true; } // shift focus to first error field
				errors += myelement.name + " is not a properly formatted email address\n";
			}
		}
		if ( myelement.maxlength ) {
			if ( myelement.value.length > myelement.maxlength ) {
				if ( !founderror ) { myelement.focus(); founderror=true; } // shift focus to first error field
				errors += myelement.name + " cannot be longer than " + myelement.maxlength + "\n";
			}
		}
	}
	// Format the Date Fields
	var selectedindex = f.ScheduledMonth.selectedIndex;
	var myMin = f.ScheduledMin.value;
	var myMinStr = String(myMin);
	if ( myMin < 10 ) myMinStr = "0" + String(myMin);
	
	var myDay = f.ScheduledDay.value;
	var myDayStr = String(myDay);
	if ( myDay < 10 ) myDayStr = "0" + String(myDay);
	
	var myMonthName = String("Not Selected");
	var myMonthNumber = -1;
	if ( selectedindex >= 0 ) {
		myMonthName = f.ScheduledMonth.options[selectedindex].text;
		myMonthNumber = f.ScheduledMonth.options[selectedindex].value;
	}
	var ScheduledDate = myMonthName + " " + myDayStr + ", " + f.ScheduledYear.value;
	var ScheduledTime = f.ScheduledHour.value + ":" + myMinStr + " " + f.AMPM.value + " " + f.TimeZone.value + " Time Zone";
	f.ScheduledDate.value = ScheduledDate;
	f.ScheduledTime.value = ScheduledTime;
	// Check if date is in the future
	if ( !isFuture(f.ScheduledYear.value, myMonthNumber - 1, f.ScheduledDay.value) ) {
		if ( !founderror ) { f.ScheduledMonth.focus(); founderror=true; } // shift focus to first error field
		errors += "Please pick a date in the future\n";
	}	
	// Check the Participants to be dialed fields
	if ( f.OpAssistOut.checked != true ) {
		var perror = false;
		for ( var i = 1; i <= 40; i++ ) {
			var name = f["PName" + i].value;
			var number = f["PNumber" + i].value;
			if ( ! isblank(name) ) { perror = true; }
			if ( ! isblank(number) ) { perror = true; }
		}
		if ( perror ) {
			if ( !founderror ) { f.OpAssistOut.focus(); founderror=true; } // shift focus to first error field
			errors += "Dial out participants are not allowed unless Operator Assisted Dial Out is checked\n";
		}
	}
	
// Check the Features Field
	if ( f.PasswordProtect.checked == true && f.what_is_the_psw.value == "" ) {
		var perror = true;
			if ( perror ) {
			if ( !founderror ) { f.PasswordProtect.focus(); founderror=true; } // shift focus to first error field
			errors += "You must enter a password if you wish to have 'Password Protection' enabled for your conference\n";
		}
	}	
	
	
	
	// Check the Enhanced Services Field
	if ( f.LiveReport.checked == true && f.email_participant_list.value == "" ) {
		var perror = true;
			if ( perror ) {
			if ( !founderror ) { f.LiveReport.focus(); founderror=true; } // shift focus to first error field
			errors += "You must enter an Email Participant List if you want a Live Call Participation Report emailed to you\n";
		}
	}
	
	
	if ( f.WavFile.checked == true && f.email_recording_file.value == "" ) {
		var perror = true;
			if ( perror ) {
			if ( !founderror ) { f.LiveReport.focus(); founderror=true; } // shift focus to first error field
			errors += "You must enter an Email address under 'Email Recording File to ' if you want a Downloadable Audio Recording emailed to you\n";
		}
	}	

	if ( f.WavFile.checked == true && (f.download_mp3.checked != true && f.download_wav.checked != true ) ) {
		var perror = true;
			if ( perror ) {
			if ( !founderror ) { f.LiveReport.focus(); founderror=true; } // shift focus to first error field
			errors += "You must check either '.mp3' or '.wav' if you want a Downloadable Audio Recording\n";
		}
	}





	if ( f.DigitalReport.checked == true && f.email_replay_file.value == "" ) {
		var perror = true;
			if ( perror ) {
			if ( !founderror ) { f.LiveReport.focus(); founderror=true; } // shift focus to first error field
			errors += "You must enter an Email address under 'Email Replay File to ' if you want a Digital Replay Participation Report emailed to you\n";
		}
	}




	if ( f.CD.checked == true && f.Ship_cd_or_tape.value == "" ) {
		var perror = true;
			if ( perror ) {
			if ( !founderror ) { f.LiveReport.focus(); founderror=true; } // shift focus to first error field
			errors += "You must enter a mailing address under 'Ship CD/Tape to ' if you want a CD or Tape sent to you\n";
		}
	}
	
	
	
	if ( f.Transcription.checked == true && f.email_transcription_file.value == "" ) {
		var perror = true;
			if ( perror ) {
			if ( !founderror ) { f.LiveReport.focus(); founderror=true; } // shift focus to first error field
			errors += "You must enter an E-mail address under 'Email Transcription to ' if you want a Transcription E-emailed to you\n";
		}
	}
	
	
	
	// Check if no errors
	if (!empty_fields && !errors) {
		
		if (document.ConfirmationForm.enablecookies.checked == 1) {SetCookie();}
		return true;
		}
	// we do have errors
	if (errors) {
		msg += "___________________________________________________\n";
		msg += errors;
		msg += "___________________________________________________\n";
	}
	if (empty_fields) {
		msg += "___________________________________________________\n";
		msg += "Please enter data for the following required fields:" + empty_fields + "\n";
		msg += "___________________________________________________\n";
	}
	// Show the alert
	alert(msg);
	return false;
}
